3.2 Function of Random Variables

How to use a random variable in your model, function of random variables, accumulators, using random variable as a parameter

In [1]:
var iframer = require('./iframer')

Most of the time we are not interested in distribution of random variable we just created. That's why the checkbox Show in Output is unchecked by default. What we are interested in is a function of that variable. In other words we need it to be transformed and used as a part of the whole model. The good news are it's as easy as using a regular Data constant. After you created a random variable and chose a name for it, use it elsewhere by its name.

Random variables in expressions

Random variable can be a part of expression. Just compose a formula that incorporates that variable. Expression will automatically have random output and can be used elsewhere as another random variable. Let's create two random variables uniformly distributed on the range between 0 and 10 then sum those variables and find square root of the result. Call variables X1 and X2, expression that will handle the combined result will be called Y. The formula for the expression:

  • Math.sqrt(X1 + X2), where Math.sqrt() is a built-in function that squares value inside the brackets and X1 and X2 are our uniform random variables

We will run our model using the Rejection sampling algorithm generating 25000 samples:

In [2]:

Random variables in iterations

When modeling time-dependent models and using accumulators there're two possible cases:

  1. Random variable is generated on each iteration. Let's say you have 100 time steps and you need to have a random value on each step.
  2. Random variable is generated once per sample and is kept fixed during iterations. Let's say you generate 1000 samples with 100 iterations on each sample. The random variable is generated only 1000 times. Not 100000 times as in previous case.

To make a random variable fixed for all iteration cycles choose Samples once checkbox on the bottom of random variable block.

Let's modify the previous model and make the second variable X2 fixed, change number of iterations to 5 and add accumulator block Z that accumulates value Y on each iteration

In [3]:

Random variables as parameters

In previous examples we used random variables as parts of expression or accumulators. However it's also possible to use them as parameters of other blocks. You can create a random variable that will set a maximum value for accumulator, or will be a parameter of another variable distribution. Below a random variable X that takes values 5, 10, 15, 20 with probabilities 0.3, 0.3, 0.2, 0.2 is used as a parameter of another random variable Y to set its size as a random array with binomial distribution (0.5, 10) of its members. Then expression Z calculates the sum of all values of the array Y and outputs results.

In [4]:
By Anton Zemlyansky in