Motivation
Our simulator needs random variables
Generating Random Numbers Examples: Arrival times, service times
Random variables are simulated using random numbers
We must therefore be able to generate random
numbers
We use a deterministic algorithm
In fact, we generate pseudo-random numbers
Introduction to Simulation WS05/06 - L 06 1/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 2/36 Sanja Lazarova-Molnar
Properties of RNG Properties of RNG
A sequence of computer-generated RN must be... Properties of a pseudo-random number generator:
uniformly distributed on (0..1) Independence
independent
Uniformity
Speed
Counterexamples:
Portability
Long cycle
0.11, 0.88, 0.23, 0.79, 0.15, 0.33, 0.93, 0.14 Replicability
0.11, 0.19, 0.23, 0.31, 0.45, 0.52, 0.66, 0.75
Introduction to Simulation WS05/06 - L 06 3/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 4/36 Sanja Lazarova-Molnar
Linear Congruential Method Linear Congruential Method
The most important type of RNG is called The LCM needs three integer parameters a, c and m
The Linear Congruential Method (LCM)
It generates a sequence of integers 0<Xi <m :
The LCM is used in almost all simulators
Xi = (a ·Xi -1+ c) mod m
It (can) fulfil all the desired properties
The random numbers 0<Ri <1 that we need are then
It is very easy to implement
Ri = Xi / m
The starting value X0 is called the seed
Introduction to Simulation WS05/06 - L 06 5/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 6/36 Sanja Lazarova-Molnar
Linear Congruential Method Linear Congruential Method
The sequence of RN is periodic Example: a = 13, m = 64, c = 0, X0 = 1, 2, 3, and 4
The quality of the RN produced depends on a, c and m
i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
How to best choose a, c and m ?
Xi 1 13 41 21 17 29 57 37 33 45 9 53 49 61 25 5 1
Choice is made in order to achieve maximum period P Xi 2 26 18 42 34 58 50 10 2
Different choices yield strongly differing P Xi 3 39 59 63 51 23 43 47 35 7 27 31 19 55 11 15 3
Xi 4 52 36 20 4
Introduction to Simulation WS05/06 - L 06 7/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 8/36 Sanja Lazarova-Molnar
Linear Congruential Method The Combined LCM
Examples: Sometimes the period of an LCM is not long enough
m = 2b c z 0 a = 4k+1 c relatively prime to m The combined LCM yields longer periods
P=m
The CLCM is based on the following observation:
m = 2b c = 0 X0 is odd a = 8k + 3 or a = 8k+5
P = m/4 Given independent, discrete-valued RVs Wi,1, Wi,2 ... Wi,k
with Wi,1 ~ U (0, m1-2)
(There is lots of theory on random number generation!)
then Wi = (6 Wi,j ) mod m1-1 is ~ U (0, m1-2)
Introduction to Simulation WS05/06 - L 06 9/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 10/36 Sanja Lazarova-Molnar
The Combined LCM The Combined LCM
Example : Definition of the CLCM:
Consider two dice W1, W2 ~ U [0..5]
Given Xi,1, Xi,2 ... Xi,k from different LCM with c =0,
The result for the sum is: with mj prime and Pj = mj -1
xi 0 1 2 3 4 5 6 7 8 9 10
p(xi) 1 2 3 4 5 6 5 4 3 2 1 Compute Xi = (6 (-1)j -1 Xi,j) mod m1 -1
Set yi = 6 xj , j mod 6 = i and compute p(yi): Then the following period P can be achieved:
yi 0 1 2 3 4 5
p(yi) 6 6 6 6 6 6 P = (m1-1) (m2-1) ... (mk-1) / 2k-1
The result is ~ U [0..5]
Introduction to Simulation WS05/06 - L 06 11/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 12/36 Sanja Lazarova-Molnar
Tests for Random Numbers The Null Hypothesis
It is important that the RN generated are The null hypothesis is an important concept in statistics
uniformly distributed on (0..1)
independent We say for example: H0: Ri ~ U [0,1]
"The numbers Ri are distributed according to U [0,1]"
There are many tests for checking these properties,
including
Our tests attempt to reject this null hypothesis
Frequency test
Runs test
Failure to reject H0 means that no evidence of non-uniformity
Autocorrelation test could be found
Poker test
This does not imply that the Ri are uniformly distributed!
Introduction to Simulation WS05/06 - L 06 13/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 14/36 Sanja Lazarova-Molnar
Frequency Tests Kolmogorov–Smirnov Test
Frequency tests test for uniformity Given
Sample random numbers to be tested Ri , 1 d i d N
They compare the RNs with the theoretical distribution Proposed distribution F (x)
There are two important tests: Compute the sample distribution SN (x):
Kolmogorov - Smirnov test
Chi-square test SN (x) = (# Ri d x ) / N
SN (x) should approximate F (x)
Null hypothesis: H0: Ri ~ F
Introduction to Simulation WS05/06 - L 06 15/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 16/36 Sanja Lazarova-Molnar
Kolmogorov–Smirnov Test Kolmogorov–Smirnov Test
Compute N =4
D = max | SN (x) – F (x) | 1
SN (x)
0.75 F (x)
The distribution of D is known when Ri ~ F
Its values depend on N and D 0.5
Choose the level of significance D
0.25
Compare D with the critical value DD from distribution
0
If D > DD then reject H0
R1 R2 R3 R4
Introduction to Simulation WS05/06 - L 06 17/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 18/36 Sanja Lazarova-Molnar
Chi-Squared Test Chi-Squared Test
The Chi-squared test is very important
The F distribution is a standard distribution
It divides the sample data into k classes The F distr. has a parameter f = k - 1 (d.o.f.)
The value of F is described by the F distribution
It uses the sample statistic
The Chi-squared test:
F02 = 6 (Oi - Ei)2 / Ei Compute F
Choose D
where
Compare F with tabulated value FDf
Oi is the observed number of samples in a class
Ei is the expected number of samples in a class If F !FDf then reject H0
Introduction to Simulation WS05/06 - L 06 19/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 20/36 Sanja Lazarova-Molnar
Chi-Squared Test Runs Test
Meaning of the chi-squared test: A run is a sequence of ascending or descending values
0.1 0.2 0.4 0.6 0.5 0.3 0.2 0.1
2 < < < > > > >
F f
One run of length 3 and one run of length 4
0.1 0.5 0.4 0.6 0.2 0.9 0.7 0.8
< > < > < > <
D
FDf Seven runs of length 1
Introduction to Simulation WS05/06 - L 06 21/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 22/36 Sanja Lazarova-Molnar
Runs Test Runs Test
We can count Example: Lengths of runs
Number of runs
Lengths of runs Yi = # of runs of length i in a sequence of length N:
Number of runs above/below 0.5
2
... E (Yi ) [ N (i 2 3i 1) (i 3 3i 2 i 4)]
(i 3)!
In all cases, the procedure is:
Choose N =100:
Make the count
Choose level of significance
E(Y1)=33.42, E(Y2)=18.10, E(Y3)=5.30
Compare count to expected value using known
distribution
Hypothesis: The set of numbers is independent
Introduction to Simulation WS05/06 - L 06 23/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 24/36 Sanja Lazarova-Molnar
Runs Test Autocorrelation
Compute: i Ei Oi (Ei - Oi)2/Ei Autocorrelation measures dependencies in sequence
1 33.42 30 0.25 Example:
2 18.10 14 0.93 0.1 0.4 0.9 0.7 0.2 0.8 0.5 0.6 0.9
3 5.30 7 0.66 (Every third value is large)
6 1.94
Positive autocorrelation: (Ri - 0.5) · (Ri+m - 0.5) > 0
Negative autocorrelation: (Ri - 0.5) · (Ri+m - 0.5) < 0
Choose D = 0.1 and set f = k - 1 = 2
F = 1.94 < FDf = 4.61 Method:
Compute (Ri - 0.5) · (Ri+m - 0.5) for different m
No reason to reject the hypothesis of independence Compare with known distribution
Introduction to Simulation WS05/06 - L 06 25/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 26/36 Sanja Lazarova-Molnar
No Test is Perfect! No Test is Perfect!
No test for random numbers is perfect! Plot results for LCM a =37, c =1, m =64 in (x,y) pairs:
Consider the sequence
1
0.2 0.8 0.7 0.3 0.4 0.6 0.9 0.1
0.9
This passes all our tests but it is not independent! 0.8
0.7
0.6
Take numbers as (x,y) pairs:
0.5
0.4
(0.2, 0.8) (0.7, 0.3) (0.4, 0.6) (0.9, 0.1)
0.3
0.2
x
0.1
x
0
0 0.2 0.4 0.6 0.8 1
x
x
Introduction to Simulation WS05/06 - L 06 27/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 28/36 Sanja Lazarova-Molnar
No Test is Perfect! Generating Random Variates
The IBM generator RANDU: a =216+3, c =0, m =231 How to generate random numbers of any distribution:
Plot results in (x, y, z) triplets:
Given:
a U (0,1) random number generator
a cdf F (x) that describes the desired distribution
Then:
1. Generate y ~ U (0,1)
2. Compute x = F -1(y)
Required when your simulator doesn't provide the
distribution you need
Introduction to Simulation WS05/06 - L 06 29/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 30/36 Sanja Lazarova-Molnar
Generating Random Variates The Exponential Distribution
How to generate random numbers of any distribution: The cdf of the exponential distribution:
F(x) = 1 e Ox
1
F(x)
x
~U(0,1) Inversion:
y = 1 e Ox
x = 1/O · log(1 y)
0 x
~F
Introduction to Simulation WS05/06 - L 06 31/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 32/36 Sanja Lazarova-Molnar
The Normal Distribution The Weibull Distribution
There is no closed form for the Normal cdf The cdf of the Weibull distribution:
Solutions: E
Approximate numerically
F(x) = 1 e– (x/D)
Approximate using alternative formulae
The same is true for the lognormal distribution Inversion: E
y = 1 e–(x/D)
x = D · ( log(1 y))1/E
Introduction to Simulation WS05/06 - L 06 33/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 34/36 Sanja Lazarova-Molnar
RN in the Simulation RN in the Simulation
How does the simulator use the RN? Sample each RV and choose the smaller value:
Consider this simple model, starting in state A:
1
0.9
0.8
0.7 X
0.6
0.5
RV X is "fast", RV Y is "slow": 0.4
0.3
X
1
0.2
0.9
0.1
0.8
0.7
0
0.6
0.5
0.4
In this case, almost always, X will be chosen
0.3
0.2
When will the simulator choose Y?
0.1
0 Many replications will be needed!
Introduction to Simulation WS05/06 - L 06 35/36 Sanja Lazarova-Molnar Introduction to Simulation WS05/06 - L 06 36/36 Sanja Lazarova-Molnar