Simulation: An Introduction
Simulation: An Introduction
Chapter 10
Simulation: An Introduction
& %
1
' $
Definition
& %
2
' $
Use of Simulation
• Simulation is used in many contexts, including the modeling of
natural systems or human systems in order to gain insight into
their functioning.
• Simulation can be used to show the eventual real effects of
alternative conditions and courses of action.
& %
3
' $
& %
4
' $
Example 1
Simulation
• Generate 1000 random samples each of size 4 from a normal
distribution.
• For each sample, compute the value of the statistic t.
• Construct a histogram for these 1000 realizations of the
statistic t.
& %
5
' $
Example 1
Result:
• The histogram matches well with the t(3) distribution.
& %
6
' $
& %
8
' $
Example 3
• What is the probability that the needle intersects a line?
• Answer:(2l)/πd
• Can we get the answer through simulations?
• A website gives the visualization of the experiment
http://www.metablake.com/pi.swf
& %
9
' $
Example 3
The experiment
• Simulate the throwing of a needle into a grid of parallel lines,
say N times
• Count the number of times the needle intersects a line, say n
times
• Then n/N gives estimate of the probability that the needle
intersects a line
& %
10
' $
& %
11
' $
& %
12
' $
Example 3: R Code
> # Buffon’s needle
> # X∼U(0,d/2), t∼U(0,pi) where d is the distance between 2
parallel lines
> # A needle of length L cut one of the lines if x<L/2*sin(t)
> # Theoretical Probability=2*L/(pi*d)
>ns=50000 ; d=2
>L=1 ; d2=d/2
> #Theoretical answer
>2*L/(pi*d)
[1]0.3183099
>x=runif(ns,0,d2)
>t=runif(ns,0,pi)
>length(x[x<L/2*sin(t)])/ns
[1] 0.3186
& %
13
' $
& %
14
' $
Congruential generators
• Congruential generators are defined by
Xi = (aXi−1 + c)modM
Xi = aXi−1 modM
Remarks
• M + 1 values {X0 ,X1 ,...XM } cannot be distinct and at least
one value must occur twice, as Xi and Xi+k , say.
• Xi , Xi+1 , ...Xi+k−1 is repeated as Xi+k , Xi+k+1 , ...Xi+2k−1 .
• The sequence Xi is periodic with period k ≤ M .
• For multiplicative generators, the maximal period is M − 1.
• If 0 ever occurs, it is repeated indefinitely.
• One of our primary objectives is to use a generator with as
large period as possible.
• However, a large period does not guarantee a good generator.
& %
16
' $
& %
17
' $
& %
18
' $
& %
19
' $
& %
20
' $
Exponential distribution
• If X follows an exponential distribution with parameter λ
(i.e.E(X) = λ), then
∫ x ∫ x/λ
1 t
F (x) = P r(X ≤ x) = e λ dt = e−y dy = 1 − e−x/λ
0 λ 0
& %
21
' $
Weibull distribution
• If X follows a Weibull distribution with parameter β, then it
can be shown that
& %
22
' $
Cauchy distribution
• If X follows a Cauchy distribution with parameter µ and σ,
then it can be shown that
1 1 x−µ
F (x) = + tan−1 ( )
2 π σ
for x in (−∞, ∞) Note: f (x) = 1
πσ(1+( x−µ 2
.
σ ) )
& %
23
' $
& %
24
' $
Algorithm to generate a normal random variable
Polar algorithm (Modified Box-Muller algorithm)
& %
26
' $
& %
27
' $
& %
28
' $
& %
29
' $
& %
30
' $
& %
31
' $
& %
32
' $
& %
33
' $
& %
34
' $
& %
35
' $
& %
36
' $
& %
37
' $
In R
> # Generate Chi-square r. v.
> n=100
> p=10
> x=rchisq(n,df=p)
>x
& %
38
' $
& %
39
' $
& %
40
' $
& %
41
' $
Γ( k+1
2 ) 1 1
f (x) = k
√ 2 k+1
for − ∞ < x < ∞
Γ( 2 ) x
kπ (1 + k ) 2
In R
> # Generate t r. v.
> n=100
> k=5
> x=rt(n,df=k)
>x
& %
42
' $
& %
43
' $
In R
> # Generate F r. v.
> n=100
> n1=5
> n2=10
> x=rf(n,df1=n1,df2=n2)
>x
& %
44
' $
& %
45
' $
In R
> # Generate Binomial r.v.
> nn=100
> n=10
> p=0.3
> x=rbinom(100,size=n,prob=p)
>x
& %
46
' $
& %
47
' $
e−λ λx
f (x) = for x = 0, 1, 2, · · ·
x!
In R
> # Generate Poisson r. v.
> n=100
> lambda=3
> x=rpois(100,lambda)
>x
& %
48
' $
& %
49
' $
Function to generate Hypergeometric r.v.
To generate random numbers from Hypergeometric distribution
(n, N, S)
S N −S
x n−x
f (x) = for x = 0, 1, 2, · · · , min(n, S)
N
n
In R
> # Generate hypergeometric r.v.
> ns=100;n=10
> S=20;N=50
> x=rhyper(ns,S,N,n)
>x
& %
50
' $
In R
> # Generate Negative Binomial r. v.
> n=100
> r=10
> p=0.3
> x=rnbinom(n,size=r,prob=p)
>x
& %
51