Stochastic Simulation: APPM 7400
Stochastic Simulation: APPM 7400
Stochastic Simulation: APPM 7400
APPM 7400
Simulation
!!!
Random Numbers
What is a random number?
A random number is a number
uniformly distributed
that is
between zero and one!
Random Numbers
A random number stream is a sequence of
independent
random
numbers.
Random Numbers
Random numbers are the basic
blocks for all simulation
algorithms!
building
simulators, to have
random number generator
Random Numbers
As important as this is, this course
on RNGs.
starting with
a decent RNG and we will go from there! (including
evaluation of the RNG)
Random Numbers
A random number generator is an
algorithm
used by a computer to generate a stream of independent
realizations of a uniform(0,1) random variable.
CPU can
produce a stream of seemingly independent
uniform(0,1) numbers
but they are actually pseudo-random
numbers.
a-b|m
a=b(mod m)
b(mod m)=a
For example,
7(mod 2) = 1
7.2(mod 2) = 1.2
7(mod 9) = 7
Choose
a modulus m>0
a multiplier a such that 0<a<m
Produce a sequence
xn = (axn-1+b)(mod m)
3.
un = xn/m
x0=0,
a=1,
b=2,
m=3
x1 = (ax0+b)(mod
m)
x3 = (ax2+b)(mod
m)
= 2(mod 3) = 2
= 3(mod 3) = 0
x2 = (ax1+b)(mod
m)
x4 = (ax3+b)(mod
m)
= 4(mod 3) = 1
= 2(mod 3) = 2
0,2,1,0,2,1,0,2,1,0,2,1,0,2,1
which we divide by m=3 to get the uniform sequence of independent numbers:
0, 2 , 1, 0, 2 , 1, 0, 2 , 1, 0, 2 , 1, 0, 2 , 1, ...
3 3 3 3 3 3 3 3 3 3
(bad!)
is given by m
number is repeated)
a 2 3, b 0, m 2
16
31
RANDU
100,000 values
L
O
O
K
S
U
N
I
F
O
R
M
RANDU
100,000 values
15 parallel
planes!
(bad!)
Tests of RNGs
Tests for independence
Tests for uniformity
Independence
runs up and runs down
runs above and below the mean
length of runs test
autocorrelation test
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
The first 21 numbers in a list of 100,000 generated by my RNG. (read across rows,
order is important!)
Independence
+ 0.7327477
0.6935230
0.2356527
+ 0.4611925
0.1976950
0.0562540
+ 0.8739298
0.4151190
0.4061648
0.2835899
0.2453624
0.8346362
0.6285227
+ 0.7430346
0.3687941
0.1235015
+ 0.1849467
0.0082797
+ 0.8593466
0.4350651
Independence
2n
-1
E[Rn]
3
(Why?
and
16n
29
Var[Rn]
90
Independenc
R
Zn : n n N(0,1)
n
So, if the observed uniform draws are really independent, roughly 95% of the time we
should see this test statistic between 1.96 and 1.96.
Independence
2(100000)
-1
100000
3
and
16(100000)-29
100000
90
z 0.50755
We passed!
Independence
16(100000)-29
100000
90
-1.96 Z 1.96
R
100000
should be between
We passed!
Independence
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
etc
Independence
0.73274767607
+
0.69352298968
+
0.23565273400
-
0.46119246644
-
0.19769494628
-
0.05625394357
-
0.87392980821
+
0.41511904218
-
0.40616476953
-
0.28358993554
-
0.24536241676
-
0.83463619173
+
This slide: n=21, runs = 8
0.36879405984
Overall: n=100,000, runs = 49,903
0.00827973058
-
0.62852266468
+
0.74303463833
+
0.12350150713
-
0.18494672983
-
0.85934662808
+
0.43506513399
-
etc
Independence
1
E[Rn]
2
(Why?
and
n
-1
Var[Rn]
4
Independenc
R
Zn : n n N(0,1)
n
So, if the observed uniform draws are really independent, roughly 95% of the time we
should see this test statistic between 1.96 and 1.96.
Independence
100000
100000
2
and
100000-1
100000
4
z -0.616647
(49690.598, 50310.402)
We passed!
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
0.00827973058
0.85934662808
0.43506513399
Independence
(Up/Down)
0.51241930545
0.73274767607
0.69352298968
0.23565273400
0.46119246644
0.19769494628
0.05625394357
0.87392980821
0.41511904218
0.40616476953
0.28358993554
0.24536241676
0.83463619173
0.62852266468
0.74303463833
0.36879405984
0.12350150713
0.18494672983
Frequency
0.00827973058
0.85934662808
0.43506513399
This slide:
Run Length
1
10
Independence
(Up/Down)
2
2
3
2
E :
n(i 3i 1) - (i 3i i 4)
i
(i 3)!
and
E
2
n 1 n!
Independence
(Up/Down)
test.
Under Ho :
(O i E i )
W
Ei
i 1
k
W~ (k 1)
Independence
(Up/Down)
Under Ho :
W~
(k 1)
2
is true
2
critical
(k 1value
) (upper tail).
Independence
(Up/Down)
Observed
Expected
41,721
41,666.75
18,386
18,333.1
5,178
5,277.647
1,145
1,150.752
220
203.3635
45
30.31132
3.912875
0.445829
Independence
(Up/Down)
So,
2
2
(
41721
41666
.
75
)
(
18386
18333
.
1
)
W
41666.75
18333.1
No!
Independence
(Up/Down)
Independence
(Up/Down)
Observed
Expected
41,721
41,666.75
18,386
18,333.1
5,178
5,277.647
1,145
1,150.752
220
203.3635
6 or 6more
48
45
30.31132
34.72019
3.912875
W 7.837548
8
2 (5)0.445829
11.0705
0.95
Independence
Autocorrelation Test
Let be the lag j autocorrelation :
(X ,X ) : Corr(X ,X )
j
i i j
i i j
Cov(X , X )
Cov(X ,X )
i i j
i i j
Var(X) Var(X )
Var(X )
1
i
i j
Independence
Autocorrelation Test
EX X
-E X E X
i i j
i
i j
j
Var(X )
1
2
EX X - EX
0 j j
(under Ho)
Var(X )
1
12 E[X X ] - 3
0 j
(uniform)
Independence
Autocorrelation Test
12 E[X X ] - 3
j
0 j
j,n
12
h
i 1
XX
i i j - 3
h
(h is the largest integer so that you dont go past the end of the sequence)
Independence
Autocorrelation Test
( 0 j 0)
j
( 0 for some j)
j
is too
large or too small
j,n
n E
j,n
13n
n Var
j,n
(n1)2
Independence
Autocorrelation Test
Test statistic:
-
n
Z j,n
j
Reject
People generally perform this test for some small lags (ex: j=1,2,,10)
Independence