0% found this document useful (0 votes)
20 views14 pages

Simulation of Random Variables

This document describes various methods for generating random numbers and simulating random variables. It explains that random numbers are necessary for simulating stochastic processes and that Excel includes the function RAND() to generate uniformly random numbers between 0 and 1. It also summarizes some algorithms such as the middle squares, middle products, and constant multiplier methods for generating pseudorandom numbers. Finally, it presents methods such as inverse transformation, acceptance-rejection, composition, and convolution to simulate other distributions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Simulation of Random Variables

This document describes various methods for generating random numbers and simulating random variables. It explains that random numbers are necessary for simulating stochastic processes and that Excel includes the function RAND() to generate uniformly random numbers between 0 and 1. It also summarizes some algorithms such as the middle squares, middle products, and constant multiplier methods for generating pseudorandom numbers. Finally, it presents methods such as inverse transformation, acceptance-rejection, composition, and convolution to simulate other distributions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Simulation of random variables

2.1 Production of numbers with random statistical behavior and


uniform in [0, 1].
Random variables are those that have behavior in reality. By
for example, the number of customers arriving at a bank each hour depends on the moment
of the day, of the week, and of other factors.

The generation of random or stochastic variables means obtaining variables


that follow a specific probability distribution. It requires two stages:
generate uniformly distributed random numbers (R) generate with R and with the
probability distributions of random or stochastic variables.

The generation of simulated statistics, that is, the values of the variables
random, they have an entirely numerical nature and must be supported by
random numbers, generated by some method.

1
f ( x )= ,a>0 , b >0
( b−a)

If the (0,1) method is divided into n classes, or equal-length sub-intervals, the


the expected number of observations in each interval is N/n, where N is the number
total observations.

The probability of observing a value in a particular interval is independent of


the previously observed values.

Randomly "selected numbers" are useful in various applications, among which


we can mention:

Simulation in Monte Carlo methods: a natural process is simulated in


computational form. These applications are carried out in very diverse fields
in order to emulate different behaviors: physics (for example, to simulate
collisions between particles), engineering (design of hydraulic works, bridges,
etc.), capital investments, networks, customer services, call centers, etc. The
computer simulation is a powerful tool for
understand the nature of complex systems.
Sampling: in order to select a sub-sample from a population.
Numerical analysis: some techniques to solve analysis problems
complex numbers have been developed using random numbers.
Programming: the generation of random values can be useful to put to
test the effectiveness of an algorithm. They are also useful in cryptology
2.1.1 Use of the generator included in the spreadsheet
The RANDOM() function in Excel

Spreadsheets like Excel (and any standard programming language) are


capable of generating pseudo-random numbers from a distribution
uniform between 0 and 1. These types of pseudo-random numbers are the elements
basics from which any computer simulation is developed. In
Excel, is it possible to obtain a pseudo-random number -coming from a distribution
uniform between 0 and 1 - using the RANDOM function:

The numbers generated by the RANDOM function have two properties that
they make them comparable to completely random numbers:

Every time the RANDOM function is used, any real number between 0 and 1
it has the same probability of being generated (hence the name distribution
uniform.
The different generated numbers are statistically independent from each other.
from others (that is, the value of the number generated at a given moment does not
depends on those generated previously).
The RAND function is a volatile function in Excel. This means that every time
we press the F9 key or change any of the model inputs, all the cells
where the RANDOM function appears will be recalculated automatically.

2.1.2 Theory: congruent methods


Some of the methods used for generating random numbers are:

Mean square algorithm:

Start with a positive integer of 4 (2n) digits and call it Z0(X0), called
seed.
Student Z0(X0) squared to obtain a number of 8 (4n) digits. If it is
If necessary, add zeros to the left to make it exactly 8 digits.
Take the 4 (2n) central digits as the next number of 4 (2n) digits and
call him Z1(X1).
Place the decimal point to the left of Z.1(X1) to obtain the first number
pseudo-random U(0,1).
I continued generating pseudo-random numbers U(0,1) in this way.

Algorithm of average products:


Select seed (Xo) with D digits (D>3)
Select a seed (X1) with D digits (D>3)
Let Y0=X0*X1. Let X2 be the D digits of the center and let ri=.0 D digits of the center.
Let Yi = Xi * Xi + 1; let Xi + 2 = the D digits of the center, and let ri + 1 = 0.D digits of the center.
for all i=1,2,3,…n.
Continue generating random numbers.

Constant multiplier algorithm:

Select a seed (X0) with D digits (D > 3).


Select a constant (a) with D digits (D > 3).
Let Y0 = a * X0; let X1 = the D digits of the center and let r1 = 0.D digits of
center.
Let Yi = a * Xi; let Xi+1 be the D digits of the center and let ri+1 = 0.D digits of
center for all i = 1, 2, 3,..., n.
Repeat step 4 until you obtain the desired n random numbers.

Linear algorithm:

This algorithm generates a sequence of integers through the equation

Xi+1= ( aXi+c ) mod ( m ) i=0,1,2,3,…,n

Where:

Xo>0, seed
a=1+4k, multiplicative constant, k must be an integer
c prime a m, additive constant
m=2^g is the modulus, where g is an integer
From the integers generated by the previous equation, to obtain the numbers
pseudo-randoms (0,1) the equation is used

Xi
ri= i=0,1,2,3,…,n
m−1

Multiplicative congruential algorithm:

Xi+1= aXi
( m
)mod I=0,1,2,3,…,n
( )

Where:
Xo must be an odd number
a=3+8k, multiplicative constant, k= 0, 1, 2, 3,…
m=2^g is the modulus, where g is an integer
Xi
ri= i=0,1,2,3,…,n
m−1

Additive congruential algorithm:

Xi= (X+i−1X mod


)
I− (i=n+
m ) 1,n+2 , n +3 , … , N

This algorithm requires having a prior sequence of n integers.


X 1 , X 2 , X3 … And to obtain the pseudorandom numbers, the following is used:

Xi
ri= i=0,1,2,3,…,n
m−1

Quadratic Congruential Algorithm


2
Xi+1=(aXi+bXi+ ) m( i=0,1,2,3,…,
c mod ) N

Where:

a must be an even number


c must be an odd number
m=2^g, g is an integer
Xi
ri= i=0,1,2,3,…,n
m−1

Blum and Shub Algorithm

2
Xi+1=(X i m) o d m
( i) =0,1,2,3,…,n

Xi
ri= i=0,1,2,3,...,n
m−1

2.2 Simulation of other random variables


Inverse transformation method

It is the most direct method to generate a random variable. Let it be

F ( z ) ,a≤z≤b

A distribution function whose inverse distribution function is:

F −1 ( u=∈
) f { [
1≤u≤0u≥∈
,zFba,: ] ( ) }
Let U be a random variable of

u ( 0.1 )

It is verified that

z=F −1 (U )

It has the distribution function F. The test follows from the observation that

( z=) pr[ F −1 (U)≤ z


prZ≤ ]= pr [ U ≤ F( z) ]=F ( z )
This immediately suggests the following generation scheme:

Inverse transformation method algorithm

Purpose: to generate Z randomly from

F ( z) ≤
azb

Ability to evaluate

F −1 u,
( ) 0 ≤ u ≤ 1

Output:Z

Method: Randomly generate U of

U ( 0.1 )

Z←F U−1 ( )

Return Z

Example. The exponential distribution

Let's assume you have an exponential distribution with mean beta. The function
probability density is:
The cumulative distribution function is:

Acceptance-Rejection Method

This method is more probabilistic than the previous one. Investment methods,
composition and convolution are direct generation methods, in the sense that
they deal directly with the distribution function. The acceptance-rejection method is
less direct in their approach.

This method will be applied in the case where the random variable is continuous, the case
Discrete is analogous and is addressed in Prob 8.9.

In this case, we have the density function f(x) of the variable and we need a
function t(x) that bounds it, that is t(x) ^3f(x) "x. It should be noted that t(x) is not, in
general, a density function

But the function r(x)=t(x)/c is clearly a density function. (We assume that t
It is such that c<¥). We must be able to generate (we hope in an easy and quick way) a
value of the random variable that follows the function r(x). the general algorithm is as follows
continue

Generate x that follows the distribution r(x)

Generate u ~ U (0,1), independent of x

Then return x if not repeat the algorithm.

The algorithm continues to repeat until a value is generated that is accepted.


To minimize the number of points rejected, the function t(x) must
be the minimum function that bounds f(x)

Composition method

This method will be applicable when the density function is easy to

Let n be the number of pieces into which the function has been divided.

Each of the fragments can be expressed as the product of a function of


distribution and a weight

And we can obtain the global distribution function as

The method consists of generating two random numbers, one is used to select a
piece and the other is used to generate a value of a variable that follows the distribution
From that piece. The value of the obtained variable is the sought value.

The general algorithm is as follows:

Generate u1, u2 ~ U(0,1)

If u1=w1 then generate x~f1(x)

If not

If u1=w1+w2 then generate x~f2(x)

Convolution method

Many random variables including normal, binomial, poisson, gamma, erlang,


etc., can be expressed exactly or approximately by the linear sum of
other random variables.
The convolution method can be used as long as the random variable x is
can be expressed as a linear combination of k random variables:

At this moment, it is necessary to generate k random numbers (u1, u2,…, uk) for
generate (x1, x2,…xk) random variables using one of the previous methods and
so that you can obtain a value of the variable that you want to obtain through convolution.

2.2.1 Theory: inverse transformation, composition, convolution, and others


procedures
Inverse transformation method

The inverse transformation method can be used to simulate random variables.


continuous, which is achieved through the cumulative function f(x) and the generation of
pseudo-random numbers rᵢ~U(0,1) .

The method consists of:

Define the Density function f(x) that represents the variable to be modeled,
Calculate the cumulative function f(x)
Clear the random variable x and obtain the inverse cumulative function f ( x−1
)
Generate the random variables x, replacing values with numbers
pseudo-random rᵢ~U(0,1) in the inverse cumulative function

The inverse transform method can also be used to simulate


discrete random variables, as in Poisson distributions,
Bernoulli, binomial, geometric, general discrete, etc. The generation takes place at
through the cumulative probability P(x) and the generation of pseudo-random numbers
rᵢ~U (0,1).

Convolution method

The probability distribution of the sum of two or more random variables


independent is called the convolution of the distributions of the variables
originals. The convolution method is then the sum of two or more variables
random to obtain a random variable with the probability distribution
desired. It can be used to obtain variables with Erlang and binomial distribution.

Methodology

Generate random numbers (Y1, Y2, Y3... Yn)


With one (or more depending on the method to be used) of the random numbers,
random variable components are generated (X1, X2, X3,..., Xn)
A value of the variable is obtained by linear summation of the random variables.
components

Uniform distribution

From the density function of uniform random variables between a and b.

The cumulative function is obtained

By equating the cumulative function F(x) with the pseudo-random number rᵢ ~ U(0,1), and
solving for x gives:

Bernoulli Distribution

Based on the probability distribution of Bernoulli random variables with


media
x 1−x
p ( x=p(1−
) p) parax=0.1

The probabilities for x=0 and x=1 are calculated, to obtain

By accumulating the values of p(x), we obtain:


Generating pseudo-random numbers rᵢ~U(0,1) the rule is applied:

2.2.2 Inverse functions of spreadsheets, usable as simulators


Function construction in Excel using VBA

Since in certain simulations the variables to be simulated do not follow any of the
distributions that incorporate spreadsheet applications, should proceed to their
Simulation. Basically, there are two possibilities:

Manual execution of the necessary calculations in the spreadsheet itself.


Appropriate function programming using VBA.
The first of the techniques may pose the problem of having intermediate calculations.
What is necessary to obtain the final value. In this case, for each random roll.
the simulation would need to be recalculated and saved using macros in a table, in which
I would only see the value obtained. Additionally, in the event that it is necessary to carry out
another simulation using the same probability distribution should be redone
calculations again. On the other hand, this possibility has the advantage that it does not require
any knowledge in programming to be able to carry it out.

The second technique, which involves knowing programming in Visual Basic for Applications
(VBA), it highlights the advantage that through a function added to the spreadsheet
like any other function that the application itself includes, it can be
call as many times as necessary and in as many cells as needed to simulate without
no limitation. Additionally, you can protect your content so that it is not visible
by the user so that they only need to know the syntax of the function for their
utilization. In this way, it also prevents manipulations that could yield results
incorrect

2.3 Simulation of special variables: tables

You might also like