simulation and random number generation · 2006. 10. 23. · pseudo-random number generation to fit...

24
Simulation and Random Number Generation Summary Discrete Time vs Discrete Event Simulation Random number generation Generating a random sequence Generating random variates from a Uniform distribution Testing the quality of the random number generator Some probability results Evaluating integrals using Monte-Carlo simulation Generating random numbers from various distributions Generating discrete random variates from a given pmf Generating continuous random variates from a given distribution

Upload: others

Post on 13-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Simulation and Random Number Generation

Summary

Discrete Time vs Discrete Event Simulation

Random number generationGenerating a random sequence

Generating random variates from a Uniform distribution

Testing the quality of the random number generator

Some probability results

Evaluating integrals using Monte-Carlo simulation

Generating random numbers from various distributionsGenerating discrete random variates from a given pmf

Generating continuous random variates from a given distribution

Page 2: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Discrete Time vs. Discrete Event Simulation

Solve the finite difference equation { }1 1min 100, 0.1 logk k k k kx x x x u+ −= − + + +

Given the initial conditions x0, x-1 and the input function uk, k=0,1,…we can simulate the output!

This is a time drivensimulation.

For the systems we are interested in, this method has two main problems.

-10 0 10 20 30 40 50-150

-100

-50

0

50

100

k

x(k)

Issues with Time-Driven Simulation

System Input

2

1 if truck arrives at ( )

0 otherwise

tu t

⎧= ⎨⎩

1

1 if parcel arrives at ( )

0 otherwise

tu t

⎧= ⎨⎩

System Dynamics

1 2

1 2

( ) 1 if ( ) 1, ( ) 0,

( ) ( ) 1 if ( ) 0, ( ) 1

( ) otherwise

x t u t u t

x t x t u t u t

x t

+

+ = =⎧⎪= − = =⎨⎪⎩

t

u1(t)

t

u2(t)

t

x(t)

∆t

Inefficient: At most steps nothing happens

Accuracy: Event order in each interval

Page 3: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Time Driven vs. Event Driven Simulation Models

Time Driven Dynamics

1 2

1 2

( ) 1 if ( ) 1, ( ) 0,

( ) ( ) 1 if ( ) 0, ( ) 1

( ) otherwise

x t u t u t

x t x t u t u t

x t

+

+ = =⎧⎪= − = =⎨⎪⎩

Event Driven Dynamics

( )1 if

,1 if

x e af x e

x e d

′+ =⎧′ = ⎨ ′− =⎩

State is updated only at the occurrence of a discrete event

In this case, time is divided in intervals of length ∆t, and the state is updated at every step

Pseudo-Random Number Generation

It is difficult to construct a truly random number generator.

Random number generators consist of a sequence of deterministically generated numbers that “look” random.

A deterministic sequence can never be truly random.

This property, though seemingly problematic, is actually desirable because it is possible to reproduce the results of an experiment!

Multiplicative Congruential Method

xk+1 = a xk modulo m

The constants a and m should follow the following criteria For any initial seed x0, the resultant sequence should look like random

For any initial seed, the period (number of variables that can be generated before repetition) should be large.

The value can be computed efficiently on a digital computer.

Page 4: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Pseudo-Random Number Generation

To fit the above requirements, m should be chosen to be a large prime number.

Good choices for parameters are

m= 231-1 and a=75 for 32-bit word machines

m= 235-1 and a=55 for 36-bit word machines

Mixed Congruential Method

xk+1 = (a xk + c) modulo m

A more general form:

where n is a positive constant

xk/m maps the random variates in the interval [0,1)

1

modn

j k jkj

a xx m−=

⎛ ⎞= ⎜ ⎟⎝ ⎠∑

Quality of the Random Number Generator (RNG)

The random variate uk=xk/m should be uniformly distributed in the interval [0,1).

Divide the interval [0,1) into k subintervals and count the number of variates that fall within each interval.Run the chi-square test.

The sequence of random variates should be uncorrelated

Define the auto-correlation coefficient with lag k > 0 Ckand verify that E[Ck] approached 0 for all k=1,2,…

where ui is the random variate in the interval [0,1).

( )( )1 12 2

1

1 n k

k i i ki

C u un k

+=

= − −− ∑

Page 5: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Some Probability Review/Results

Probability mass functions

X1 2 3

Pr{X=1}=p1

Pr{X=2}=p2

Pr{X=3}=p3

( ) { }

{ }

Pr

Pr

X

x

i

F X xx

X i=−∞

= ≤

= =∑

Cumulative mass functions

X1 2 3

p1

p1+ p2

p1+ p2 + p3

1

{ }Pr 1i

X i∞

=−∞

= =∑

Some Probability Results/Review

Probability density functions

Pr{x=x1}= 0!

x0

fX(x)

x1

{ }

( )1

1

1 1Prx

X

x

x X x

f dxxε

ε

ε ε+

− ≤ ≤ + =

= ∫

Cumulative distribution

( ) { }Pr

( )

X

x

X

F X xx

f y dy−∞

= ≤

= ∫

x0

FX(x)

1

Page 6: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Independence of Random Variables

Joint cumulative probability distribution.

{ }( , ) Pr ,XYF x y X x Y y= ≤ ≤

Independent Random Variables.

{ } { } { }Pr , Pr PrX C Y D X C Y D∈ ∈ = ∈ ∈

For discrete variables

{ } { } { }Pr , Pr PrX x Y y X x Y y= = = = =

For continuous variables

( , ) ( ) ( )XY X Yf x y f x f y= ( , ) ( ) ( )XY X YF x y F x F y=

Conditional Probability

Conditional probability for two events.

{ } { }{ }

PrPr |

Pr

ABA B

B=

Bayes’ Rule.

Total Probability

{ } { } { }Pr Pr | Prk kk

X x X x Y y Y y= = = = =∑

{ } { }{ } { } { } { }

{ }Pr Pr | Pr

Pr | Pr |Pr Pr

AB B A AB A A B

A B= ⇒ =

Page 7: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Expectation and Variance

Expected value of a random variable

[ ] ( )XE xf x dxX∞

−∞

= ∫[ ] Pr{ }i ii

E x X xX∞

=−∞

= =∑Expected value of a function of a random variable

( )[ ] ( ) ( )XE g f x dxg xX∞

−∞

= ∫( )[ ] ( )Pr{ }i ii

E g x X xg X∞

=−∞

= =∑

Variance of a random variable

[ ] [ ]( )2var E X EX X⎡ ⎤= −⎣ ⎦

Covariance of two random variables

The covariance between two random variables is defined as

[ ] [ ]( ) [ ]( )cov , E X E Y EX Y X Y= ⎡ ⎤− −⎣ ⎦

[ ] [ ] [ ] [ ] [ ][ ][ ] [ ] [ ]

cov , E XY XE YE E EX Y Y X X Y

E E EXY X Y

= − − +

= −

The covariance between two independent random variables is 0 (why?)

[ ] { }

{ } { } [ ] [ ]

Pr ,

Pr Pr

i j i ji j

i i j ji j

E x y X x Y yXY

x X x y Y y E EX Y

= = =

= = = =

∑∑

∑ ∑

Page 8: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Markov Inequality

If the random variable X takes only non-negative values, then for any value a > 0.

{ } [ ]Pr

E XX aa

≥ ≤

Note that for any value 0<a < E[X], the above inequality says nothing!

Chebyshev’s Inequality

If X is a random variable having mean µ and variance σ2, then for any value k > 0.

{ } 2

1Pr kX

kσµ ≥ ≤−

µ

σ

kσkσ

x

fX(x)

This may be a very conservative bound! Note that for X~N(0,σ2), for k=2, from Chebyshev’s inequality Pr{.}<0.25, when in fact the actual value is Pr{.}< 0.05

Page 9: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

The Law of Large Numbers

Weak: Let X1, X2, … be a sequence of independent and identically distributed random variables having mean µ. Then, for any ε >0

1 ...Pr 0, as nX X

nn

εµ⎧ ⎫+ + > → →∞−⎨ ⎬⎩ ⎭

Strong: with probability 1,

1 ...lim n

n

X X

→∞

+ +=

The Central Limit Theorem

Let X1, X2, …Xn be a sequence of independent random variables with mean µi and variance σι2. Then, define the random variable X,

1 ... nX X X= + +Let

[ ] [ ]11

...n

n ii

E E X XXµ µ=

= = + + = ∑( )22 2

1

n

i

E X ισ σµ=

⎡ ⎤= =−⎣ ⎦ ∑Then, the distribution of X approaches the normal distribution as n increases, and if Xi are continuous, then

( ) ( )2 / 21

2x

Xf exσµ

π− −≈

Page 10: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Chi-Square Test

Let k be the number of subintervals, thus pi=1/k, i=1,…,k.

Let Ni be the number of samples in each subinterval. Note that E[Ni]=Npi where N is the total number of samples

Null Hypothesis H0: The probability that the observed random variates are indeed uniformly distributed in (0,1).

Let T be

( )2

2

1 1

1 k k

i i ii ii

k NT N Np N

Np N k= =

⎛ ⎞= − = −⎜ ⎟⎝ ⎠

∑ ∑

Define p-value= PH0{T>t} indicate the probability of observing a value t assuming H0 is correct.

For large N, T is approximated by a chi-square distribution with k-1degrees of freedom, thus we can use this approximation to evaluate the p-value

The H0 is accepted if p-value is greater than 0.05 or 0.01

Monte-Carlo Approach to Evaluating Integrals

Suppose that you want to estimate θ, however it is rather difficult to analytically evaluate the integral.

( )1

0

g dxxθ = ∫ Suppose also that you don’t want to use numerical integration.

Let U be a uniformly distributed random variable in the interval [0,1) and ui are random variates of the same distribution. Consider the following estimator.

( )1

1ˆn

ii

g un

θ=

= ∑

Also note that:

( )[ ] as E ng U→ →∞

( ) ( )[ ]1

0

g du E gu Uθ = =∫

Strong Law of Large Numbers

Page 11: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Monte-Carlo Approach to Evaluating Integrals

Use Monte-Carlo simulation to estimate the following integral.

( )b

a

g dxxθ = ∫Let y=(x-a)/(b-a), then the integral becomes

What if ( )0

g dxxθ∞

= ∫

( ) ( )( ) ( )1 1

0 0

g dy h dyy a yb a b aθ = =+− −∫ ∫

Use the substitution y= 1/(1+x),

What if ( )1 1 1

1 1

0 0 0

... ,..., ...n ng x x dx dxθ = ∫ ∫ ∫

Example: Estimate the value of π.

(-1,-1) (1,-1)

(1,1)(-1,1)

Area of Circle

Area of square 4

π=

Let X, Y, be independent random variables uniformly distributed in the interval [-1,1]

The probability that a point (X,Y) falls in the circle is given by

{ }2 2Pr 14

X Yπ

+ ≤ =

SOLUTION

Generate N pairs of uniformly distributed random variates (u1,u2) in the interval [0,1).

Transform them to become uniform over the interval [-1,1), using (2u1-1,2u2-1).

Form the ratio of the number of points that fall in the circle over N

Page 12: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Discrete Random Variates

Suppose we would like to generate a sequence of discrete random variates according to a probability mass function

{ }0

Pr , 0,1,..., , 1N

j j jj

X x p j N p=

= = = =∑

x

1

u

X

Inverse Transform Method

0 0

1 0 0 1

1

0 0

if

if

if j j

j i ii i

x u p

x p u p p

X

x p u p−

= =

≤⎧⎪ ≤ ≤ +⎪⎪⎪= ⎨⎪ ≤ ≤⎪⎪⎪⎩

∑ ∑

M M

M M

Discrete Random Variate Algorithm (D-RNG-1)

Algorithm D-RNG-1Generate u=U(0,1)If u<p0, set X=x0, return;If u<p0+p1, set X=x1, return;…Set X=xn, return;

Recall the requirement for efficient implementation, thus the above search algorithm should be made as efficient as possible!

Example: Suppose that X∈{0,1,…,n} and p0= p1 =…= pn= 1/(n+1), then

( )1X un= ⎢ ⎥+⎣ ⎦

Page 13: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Discrete Random Variate Algorithm

D-RNG-1: Version 1Generate u=U(0,1)If u<0.1, set X=x0, return;If u<0.3, set X=x1, return;If u<0.7, set X=x2, return;Set X=x3, return;

Assume p0= 0.1, p1 = 0.2, p2 = 0.4, p3 = 0.3. What is an efficient RNG?

D-RNG-1: Version 2Generate u=U(0,1)If u<0.4, set X=x2, return;If u<0.7, set X=x3, return;If u<0.9, set X=x1, return;Set X=x0, return;

More Efficient

Geometric Random Variables

Let p the probability of success and q=1-p the probability of failure, then X is the time of the first success with pmf

{ } 1Pr iX i pq −= =Using the previous discrete random variate algorithm, X=j if

{ } { }1

1 1

Pr Prj j

i i

X i U X i−

= =

= ≤ < =∑ ∑

{ } { }1

1

1

Pr 1 Pr 1 1j

j

i

X i X j q−

=

= = − > − = −∑

11 1j jq U q−⇒ − ≤ < −

11j jq U q −⇒ < − ≤

{ }min : 1jX j q U⇒ = < −

( ) ( ){ }( )( )

min : log log 1

log 1min :log

X j j q U

Uj jq

= < −

⎧ ⎫⎪ ⎪−= >⎨ ⎬⎪ ⎪⎩ ⎭

As a result:

( )( )

log 11

logU

Xq

⎢ ⎥−= +⎢ ⎥⎣ ⎦

Page 14: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Poisson and Binomial Distributions

Poisson Distribution with rate λ.

{ }Pr , 0,1,...!

ieX i i

i

λλ −

= = =

The binomial distribution (n,p) gives the number of successes in n trials given that the probability of success is p.

{ }( )

( )!Pr , 0,1,...,1

! !n iin

X i p i npi n i

−= = =−−

Note: 1 1i ip pi

λ+ =

+

Note: 1

1

1 1i i

n pp p

i p+

−=

+ −

Accept/Reject Method (D-RNG-AR)

Suppose that we would like to generate random variates from a pmf {pj, j≥0} and we have an efficient way to generate variates from a pmf {qj, j≥0}.

Let a constant c such that

for all such that 0jj

j

pc j p

q≤ >

In this case, use the following algorithm

D-RNG-AR:1. Generate a random variate Y from pmf {qj, j≥0}. 2. Generate u=U(0,1)3. If u< pY/(cqY), set X=Y and return;4. Else repeat from Step 1.

Page 15: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Accept/Reject Method (D-RNG-AR)

Show that the D-RNG-AR algorithm generates a random variate with the required pmf {pj, j≥0}.

{ }Prip X i= = =

{ }Pr and stop after X i k= =

{ }Pr Not stop up to 1k − =

Therefore

{ } { }1

Pr not accepted| Prk

j

Y Y j Y j−

⎛ ⎞= =⎜ ⎟

⎝ ⎠∑

i ii

i

p pq

cq c=

{ } { }1

Pr not stop for 1,..., 1 Pr and stop at iteration k

k X i k∞

=

− =∑

{ } { } { }Pr | and is accepted Pr Y accepted| PrX i Y i Y i Y i= = = = =

1 pi/cqi qi

1

1

k

j

jj j

pq

cq

−⎛ ⎞⎛ ⎞

−= =⎜ ⎟⎜ ⎟⎜ ⎟⎜ ⎟⎝ ⎠⎝ ⎠∑

11

1k

c

−⎛ ⎞−⎜ ⎟⎝ ⎠

1

1

11

ki

ik

pp

cc

−∞

=

⎛ ⎞= =−⎜ ⎟⎝ ⎠

∑ ip

D-RNG-AR Example

Determine an algorithm for generating random variates for a random variable that take values 1,2,..,10 with probabilities 0.11, 0.12, 0.09, 0.08, 0.12, 0.10, 0.09, 0.09, 0.10, 0.10 respectively.

max 1.2i

ii

pc

q

⎧ ⎫= =⎨ ⎬

⎩ ⎭

D-RNG-1: Generate u=U(0,1)k=1;

while(u >cdf(k))

k=k+1;

x(i)=k;

D-RNG-AR: u1=U(0,1), u2=U(0,1)

Y=floor(10*u1 + 1);

while(u2 > p(Y)/c)

u1= U(0,1); u2=U(0,1);

Y=floor(10*rand + 1);

y(i)=Y;

Page 16: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

The Composition Approach

Let X1 have pmf {qj, j≥0} and X2 have pmf {rj, j≥0} and define

{ }Pr (1 ) , 0j j jX j p aq a r j= = = + − ≥

Suppose that we have an efficient way to generate variates from two pmfs {qj, j≥0} and {rj, j≥0}

Suppose that we would like to generate random variates for a random variable having pmf, a ∈(0,1).

1

2

with probability

with probability 1-

X aX

X a

⎧= ⎨⎩

Algorithm D-RNG-C: Generate u=U(0,1)If u <= a generate X1

Else generate X2

Continuous Random VariatesInverse Transform Method

Suppose we would like to generate a sequence of continuous random variates having density function FX(x)

Algorithm C-RNG-1: Let U be a random variable uniformly distributed in the interval (0,1). For any continuous distribution function, the random variate X is given by

u

X x

1FX(x)

( )1XX F U−=

Page 17: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Example: Exponentially Distributed Random Variable

Suppose we would like to generate a sequence of random variates having density function

( ) xXf ex

λλ −=SolutionFind the cumulative distribution

( )0

1x

y xXF e dy ex

λ λλ − −= = −∫Let a uniformly distributed random variable u

( ) 1 xXu F ex

λ−= = − ⇒ ( )ln 1 xu λ= − ⇒− ( )1

ln 1x uλ

= − −

Equivalently, since 1-u is also uniformly distributed in (0,1)

( )1

lnx uλ

= −

Convolution Techniques and the ErlangDistribution

Suppose the random variable X is the sum of a number of independent identically distributed random variables

1

n

ii

X Y=

= ∑

An example of such random variable is the Erlang with order n which is the sum of n iid exponential random variables with rate λ.

( )( )

!

n x

X

exf xn

λλ −

=

Algorithm C-RNG-Cv:Generate Y1,…,Yn from the given distributionX=Y1+Y2+…+Yn.

Page 18: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Accept/Reject Method (C-RNG-AR)

Suppose that we would like to generate random variates from a pdf fX(x) and we have an efficient way to generate variates from a pdf gX(x).

Let a constant c such that ( )( )

for all X

X

f x c xg x

In this case, use the following algorithm

C-RNG-AR:1. Generate a random variate Y from density gX(x). 2. Generate u=U(0,1)3. If u< fX(Y)/(cgX(Y)), set X=Y and return;4. Else repeat from Step 1.

Accept/Reject Method (C-RNG-AR)

The C-RNG-AR is similar to the D-RNG-AR algorithm except the comparison step where rather than comparing the two probabilities we compare the values of the density functions.

TheoremThe random variates generated by the Accept/Reject method have density fX(x).The number of iterations of the algorithm that are needed is a geometric random variable with mean c

Note: The constant c is important since is implies the number of iterations needed before a number is accepted, therefore it is required that it is selected so that it has its minimum value.

Page 19: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

C-RNG-AR Example

Use the C-RNG-AR method to generate random variates X that are normally distributed with mean 0 and variance 1, N(0,1).

212

| |

2( )

2

xXf x e

π−=

First consider the pdf of the absolute value of |X|.

We know how to generate exponentially distributed random variates Y with rate λ=1.

( ) , 0xYg x e x−= ≥

Determine c such that it is equal to the maximum of the ratio

( )

212| | ( ) 2

2

x xX

Y

f xe

g x π−= 2e

⇒ =

C-RNG-AR Example

C-RNG-AR for N(0,1):u1=U(0,1), u2=U(0,1);

Y= -log(u1);

while(u2 > exp(-0.5(Y-1)*(Y-1)))

u1= U(0,1); u2=U(0,1);

Y= -log(u1);

u3= U(0,1);

If u3 < 0.5 X=Y;

Else X= -Y;

Suppose we would like Z~N(µ, σ2), then

:Z Xσ µ= +

Page 20: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Generating a Homogeneous Poisson Processes

A homogenous Poisson process is a sequence of points (events) where the inter-even times are exponentially distributed with rate λ (The Poisson process will be studied in detail during later classes)

Let ti denote the ith point of a Poisson process, then the algorithm for generating the first N points of the sequence {ti, i=1,2,…,N} is given by

Algorithm Poisson-λ: k=0, t(k)=0;While k<N

k= k+1;Generate u=U(0,1)t(k)= t(k-1) – log(u)/lambda;

Return t.

Generating a Non-Homogeneous Poisson Processes

Suppose that the process is non-homogeneous i.e., the rate varies with time, i.e., λ(t) ≤λ, for all t<T.

Let ti denote the ith point of a Poisson process, and τ the actual time, then the algorithm for generating the first N points of the sequence {ti, i=1,2,…,N} is given by

Algorithm Thinning Poisson-λ: k=0, t(k)=0, tau= 0;While k<N

Generate u1=U(0,1);tau= tau – log(u1)/lambda;Generate u2= U(0,1);If(lambda(tau)\lambda < u2)

k= k+1, t(k)= tau;

Return t.

Page 21: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Generating a Non-Homogeneous Poisson Processes

Again, suppose that the process is non-homogeneous i.e., the rate varies with time, i.e., λ(t) ≤λ, for all t<T but now we would like to generate all points ti directly, without thinning.

Assuming that we are at point ti, then the question that we need to answer is what is the cdf of Si where Si is the time until the next event

{ }| | ( ) Pr |iS i iF s S s t t= < = = ( ){ }0

1 exps

it y dyλ− − +∫

Thus, to simulate this process, we start from t0 and generate S1 from FS1 to go to t1=t0+S1. Then, from t1, we generate S2 from FS2 to go to t2=t1+S2 and so on.

Example of Non-Homogeneous Poisson Processes

Suppose that λ(t)= 1/(t+α), t ≥0, for some positive constant a. Generate variates from this non-homogeneous Poisson process.

First, let us determine the rate of the cdf

( )0

s

it y dyλ + =∫

Inverting this yields

( )0

1s

i

dya t y

=+ +∫ log i

i

s t a

t a

+ +⎛ ⎞⎜ ⎟+⎝ ⎠

1 exp logi

iS

i

s t aF

t a

⎧ ⎫+ +⎛ ⎞= − − =⎨ ⎬⎜ ⎟+⎝ ⎠⎩ ⎭

1 i

i i

t a s

s t a s t a

+− =

+ + + +

( )1

1Si

it a uF

u− +=

Page 22: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Example of Non-Homogeneous Poisson Processes

Inverse Transform( )1

1Si

it a uF

u− +=

Thus we start from t0=0

( )0 1 11 0

1 11 1

t a u aut t

u u

+= + =

− −

( ) 21 1 22 1

2 21 1

ut a t aut t

u u

+ += + =

− −…

( )1 11 1 1

i i i ii i

i i

t a u t aut t

u u− −

+ += + =

− −

The Composition Approach

( )1 1

( ), 1, 0, 1,...,n n

iX i i i

i i

F rG x r r i nx= =

= = > =∑ ∑

Suppose that we have an efficient way to generate variates from cdfs G1(x),…, Gn(x).

Suppose that we would like to generate random variates for a random variable having cdf

Algorithm C-RNG-C: Generate u=U(0,1)

If u<p1, get X from G1(x), return;

If u<p1+p2, get X from G2(x), return;

Page 23: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Polar Method for Generating Normal Random Variates

( )2 2 11 1 2 222 2

1 1 1( , )

22 2

x y x yXYf x y e e e

ππ π−− − += =

Let X and Y be independent normally distributed random variables with zero mean and variance 1. Then the joint density function is given by

X

Y

θ

R

Then make a variable change2 2r x y= + arctan

y

xθ =

The new joint density function is12

1 1( , )

2 2r

Rf r eθπ

−Θ =

Uniform in the interval [0,2π]

Exponential with rate 1/2

(-1,1)

(-1,-1)

(1,1)

(1,-1)

Polar Method for Generating Normal Random Variates

Algorithm C-RNG-N1: Generate u1=U(0,1), u2=U(0,1);R= -2*log(u1); W= 2*pi*u2;

X= sqrt(R) cos(W); Y= sqrt(R) sin(W);

But, sine and cosine evaluations are inefficient!Algorithm C-RNG-N2: 1. Generate u1=U(0,1), u2=U(0,1);2. Set V1= 2*u1-1, V2= 2*u2-1;

3. S=V1*V1+V2*V2;

4. If S > 1, Go to 1

5. R= sqrt(-2*log(S)/S);

6. X= R*V1;

7. Y= R*V2;

Generates 2 independent RVs

(V1,V2)

Page 24: Simulation and Random Number Generation · 2006. 10. 23. · Pseudo-Random Number Generation To fit the above requirements, m should be chosen to be a large prime number. Good choices

Simulation of Discrete Event Systems

INITIALIZE

EVENT CALENDAR

e1 t1

e2 t2…CLOCK

STRUCTURERNG

TIMESTATE

Update Statex’=f(x,e1)

Update Timet’=t1

Delete Infeasible

EventsAdd New Feasible Events

Verification of a Simulation Program

Standard debugging techniquesDebug “modules” or subroutines

Create simple special cases, where you know what to expect as an output from each of the modules

Often choosing carefully the system parameters, the simulation model can be evaluated analytically.

Create a trace which keeps track of the state variables, the event list and other variables.