Previous Up Next

9.3.14  Random variables: random_variable randvar

The randvar command produces an object representing a random variable. The value(s) can be generated subsequently by calling sample (see Section 9.3.1), rand (see Section 9.3.1), randvector (see Section 9.3.15) or randmatrix (see Section 9.3.16).
random_variable is a synonym for randvar.


Examples.


Some continuous distributions can be defined by specifying their first and/or second moment.
Examples.


Uniformly distributed random variables can be defined by specifying the support as an interval.
Examples:


The following examples demonstrate various ways to define a discrete random variable.
Examples:


Discrete random variables can be used to approximate custom continuous random variables. For example, consider a probability density function f as a mixture of two normal distributions on the support S=[−10,10]. You can sample f in N=10000 points in S.
Input:

F:=normald(3,2,x)+normald(-5,1,x):;
c:=integrate(F,x=-10..10):;
f:=unapply(1/c*F,x):;
X:=randvar(f,range=-10..10,10000):;

Now generate 25000 values of X and plot a histogram:
Input:

R:=sample(X,25000):;
hist:=histogram(R,-10,0.1):;
PDF:=plot(f(x),display=red+line_width_2):;
hist,PDF

Output:

Sampling from discrete distributions is fast: for instance, generating 25 million samples from the distribution of X which has about 10000 outcomes takes only couple of seconds. In fact, the sampling complexity is constant. Also, observe that the process isn’t slowed down by spreading it across multiple calls of randvector.
Input:

for k from 1 to 1000 do randvector(25000,X); od:;

Evaluation time: 2.12


Independent random variables can be combined in an expression, yielding a new random variable. In the example below, you define a log-normally distributed variable Y from a variable X with standard normal distribution.
Input:

X:=randvar(normal):; mu,sigma:=1.0,0.5:;
Y:=exp(mu+sigma*X):;
L:=randvector(10000,Y):;
histogram(L,0,0.33)

Output:

It is known that E[Y]=eµ+σ2/2. The mean of L should be close to that number.
Input:

mean(L); exp(mu+sigma^2/2)

Output:

  3.0789,3.0802


In case a compound random variable is defined as an expression containing several independent random variables X,Y,… of the same type, you sometimes need to prevent its evaluation when passing it to randvector and similar functions.


Example.
Input:

X:=randvar(normal):; Y:=randvar(normal):;

If you want to generate, for example, the random variable X/Y, you would have to forbid automatic evaluation of the latter expression; otherwise it would reduce to 1 since X and Y are both normald(0,1).
Input:

randvector(5,eval(X/Y,0))

Output (for example):


−0.358479277895,5.03004946974,−5.5414073892,−0.885656967277,−2.63689662108

To save typing, you can define Z with eval(∗,0) and pass eval(Z,1) to randvector or randmatrix.
Input:

Z:=eval(X/Y,0):; randvector(5,eval(Z,1))

Output (for example):


0.404123429613,−4.06194898981,0.00356038536404,1.61619003525,−2.85682173195

Parameters of a distribution can be entered as symbols to allow (re)assigning them at any time.
Input:

purge(lambda):;
X:=randvar(exp,lambda):;
lambda:=1:;

Now execute the following command line several times in a row. The parameter λ is updated in each iteration.
Input:

r:=rand(X); lambda:=sqrt(r)

Output (by executing the above command line three times):

8.5682,2.9272
1.5702,1.2531
0.53244,0.72968

Previous Up Next