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]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22d%22%3A%22Uniform%22%2C%22n%22%3A%22X1%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22a%22%3A%220%22%2C%22b%22%3A%2210%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22d%22%3A%22Uniform%22%2C%22n%22%3A%22X2%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22a%22%3A%220%22%2C%22b%22%3A%2210%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22n%22%3A%22Y%22%2C%22h%22%3Afalse%2C%22sh%22%3Atrue%2C%22t%22%3A1%2C%22v%22%3A%22Math.sqrt%28X1%20%2B%20X2%29%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Main%22%2C%22e%22%3A%22%22%2C%22s%22%3A1%2C%22m%22%3A%22rejection%22%7D%2C%22met%22%3A%7B%22sm%22%3A%2225000%22%7D%7D%5D')

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]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22d%22%3A%22Uniform%22%2C%22n%22%3A%22X1%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22a%22%3A%220%22%2C%22b%22%3A%2210%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22d%22%3A%22Uniform%22%2C%22n%22%3A%22X2%22%2C%22o%22%3Atrue%2C%22p%22%3A%7B%22a%22%3A%220%22%2C%22b%22%3A%2210%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22n%22%3A%22Y%22%2C%22h%22%3Afalse%2C%22sh%22%3Afalse%2C%22t%22%3A1%2C%22v%22%3A%22Math.sqrt%28X1%20%2B%20X2%29%22%7D%2C%7B%22i%22%3A0%2C%22h%22%3Atrue%2C%22n%22%3A%22Z%22%2C%22sh%22%3Atrue%2C%22t%22%3A3%2C%22v%22%3A%22Y%22%2C%22min%22%3A%22%22%2C%22max%22%3A%22%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Main%22%2C%22e%22%3A%22%22%2C%22s%22%3A%225%22%2C%22m%22%3A%22rejection%22%7D%2C%22met%22%3A%7B%22sm%22%3A%225000%22%7D%7D%5D')

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]:
iframer('https://statsim.com/app/?a=%5B%7B%22b%22%3A%5B%7B%22d%22%3A%22Categorical%22%2C%22n%22%3A%22X%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22ps%22%3A%220.3%2C%200.3%2C%200.2%2C%200.2%22%2C%22vs%22%3A%225%2C%2010%2C%2015%2C%2020%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%221%22%7D%2C%7B%22d%22%3A%22Binomial%22%2C%22n%22%3A%22Y%22%2C%22o%22%3Afalse%2C%22p%22%3A%7B%22p%22%3A%220.5%22%2C%22n%22%3A%2210%22%7D%2C%22sh%22%3Afalse%2C%22t%22%3A0%2C%22dims%22%3A%22X%22%7D%2C%7B%22n%22%3A%22Z%22%2C%22h%22%3Afalse%2C%22sh%22%3Atrue%2C%22t%22%3A1%2C%22v%22%3A%22sum%28Y%29%22%7D%5D%2C%22mod%22%3A%7B%22n%22%3A%22Main%22%2C%22e%22%3A%22%22%2C%22s%22%3A1%2C%22m%22%3A%22rejection%22%7D%2C%22met%22%3A%7B%22sm%22%3A%2220000%22%7D%7D%5D')
By Anton Zemlyansky in