randvar (alias: random_variable) takes a probability distribution specification as its argument and returns an object representing a random variable. Its value(s) can be generated subsequently by calling sample, rand, randvector or randmatrix.
The probability distribution is specified as a sequence of arguments. The supported types are: uniform, normal, binomial, multinomial, negbinomial, Poisson, Student, Fisher-Snedecor, Cauchy, Weibull, beta, gamma, chi-square, geometric, exponential and discrete.
The usual way to specify a continuous distribution is to pass the probability density function as the first argument, followed by one or more (numeric) parameters. However, it can also be defined by specifying its type and first and/or second moment (the mean and/or the standard deviation/variance); the supported types are: normal, uniform, binomial, Poisson, geometric, exponential, gamma, beta and Weibull. Additionally, a uniform distribution can be defined by specifying its range as an interval. The arguments are entered in form:
To create a discrete random variable one can pass either
The weights are automatically scaled by the inverse of their sum to obtain the values of the probability mass function. If a function f is given instead of a list of weights, then wk=f(a+k) for k=0,1,…,b−a unless N is given, in which case wk=f(xk) where xk=a+(k−1) b−a/N and k=1,2,…,N. The resulting random variable X has values in {0,1,…,n−1} for 0-based modes (e.g. Xcas) resp. in {1,2…,n} for 1-based modes (e.g. Maple). If the list V of custom objects is given, then V[X] is returned instead of X. If N is given, then vk=xk for k=1,2,…,N.
To define a random variable with a Fisher-Snedecor distribution (two degrees of freedom), input:
Output:
To generate some values of X, input:
A possible output:
Input:
A possible output:
To define a random variable with multinomial distribution, input:
Output:
Input:
A possible output:
Some continuous distributions can be defined by specifying its first and/or second moment. Input:
A possible output:
Input:
A possible output:
Input:
Output:
Input:
Output:
Input:
A possible output:
Input:
Output:
Input:
A possible output:
Input:
Output:
Uniformly distributed random variables can be defined by specifying the support as an interval. Input:
A possible output:
Input:
A possible output:
The following examples demonstrate various ways to define a discrete random variable. Input:
A possible output:
Input:
A possible output:
Input:
^
2,range=-10..10):;Output:
Input:
Output:
|
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 with support S=[−10,10]. We sample f with N=10000 points in S. Input:
Now we generate 25000 values of X and plot a histogram:
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. For example, input:
Evaluation time: 2.12
Independent random variables can be combined in an expression, yielding a new random variable. In the example below, we define a log-normally distributed variable Y from a variable X with the standard normal distribution. Input:
Output:
It is known that E[Y]=eµ+σ2/2. Hence the mean of L should approximate that number. Input:
^
2/2)
We might obtain:
In case a compound random variable is defined as an expression containing several independent random variables X,Y,… of the same type, it is sometimes needed to prevent its evaluation when passing it to randvector and similar functions. For example, input:
If we wanted to generate, for example, the random variable X/Y, we 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:
We might obtain:
To save typing, one can define Z with eval(∗,0) and pass eval(Z,1) to randvector or randmatrix. Input:
We might obtain:
Parameters of a distribution can be entered as symbols to allow (re)assigning them at any time. Input:
Now execute the following command line several times in a row. The parameter λ is updated in each iteration.
We might obtain: