Computer Simulations and Monte Carlo Methods
Computer Simulations and Monte Carlo Methods
Computer Simulations and Monte Carlo Methods
PLANNING
Strategic/tactical
planning Model Scoping
Model building
Data acquisition
Model Modeling
Validation
VERIFICATION
Experiments
Analysis
APPLICATION
Fig.1 Overview of the simulation methodology
Fundamentals of Performance evaluation of computer and
Telecommunication Systems-Obaidat & Boudriga, John wiley, 2010 Implementation/
documentation
Actual World Study of Simulation
System
Model
under study
Simulation
experiment
Analysis of
simulation
Result
Changed system
no
Code Study of document
no and reporting
no results
If verified
If validated Implementation
no Yes
Experiment w/ Experiment w/
actual system model of system
Physical Mathematical
Model Model
Analytical Simulation
Model
Model
Fig.4
Focus of class
5
Probability and Statistics for Computer Scientists-Michael Baron, CRC,2007.
Monte Carlo methods are mostly used for the computation of probabilities,
expected values, and other distribution characteristics. Recall that probability can
be defined as long-run proportion. With the help of random number generators,
computer can actually simulate long run. Then, probability can be estimated by a
mere computation of the associated frequency. Similarly, one can estimate
expectations, variances, and other distribution characteristics from a long run of
simulated random variables.
What is Monte Carlo ?
Monte Carlo is an administrative area of the principality of
Monaco.
A. N. Kolmogorov (12.4.1903-20.10.1987)
• Monte Carlo methods are often used when
simulating physical and mathematical systems.
Integral solved
with Monte Carlo
1
x = (random#)
y = (random#)
distance = sqrt (x^2 + y^2)
if distance.from.origin (less.than.or.equal.to) 1.0
let hits = hits + 1.0
Cont....
area.of .circle #.of .dots.inside.circle
area.of .square total.number.of .dots
I f ( x) dx
a
s area.under.curve f ( x)dx
a
N Total.area ( M R)(b a)
b
s
f ( x)dx ( M R)(b a)
a N
This must now be corrected for the fact that the line y = -R has
been used as the baseline for the integral instead of the line
y = 0. This is accomplished by subtracting the rectangular
area R(b-a). b
s
then f ( x)dx ( M R)(b a) R(b a)
a N
Types of distribution
𝟏
ഥ = (𝑿𝟏 + 𝑿𝟐 + ⋯ … . +𝑿𝒏 ) and the distribution of 𝑿𝟏 , 𝑿𝟐 , … . , 𝑿𝒏 has a mean 𝝁 and standard deviation 𝝈, we
Since 𝑿 𝒏
observe that
𝟏 𝟏
𝑬 𝑿ഥ = 𝑬𝑿𝟏 + 𝑬𝑿𝟐 + ⋯ . +𝑬𝑿𝒏 = 𝒏𝝁 = 𝝁
𝒏 𝒏
And
𝟏 𝟏 𝝈𝟐
𝒗𝒂𝒓 𝑿 ഥ = 𝒗𝒂𝒓𝑿 𝟏 + 𝒗𝒂𝒓𝑿 𝟐 + ⋯ . +𝒗𝒂𝒓𝑿 𝒏 = 𝒏𝝈 𝟐
=
𝒏𝟐 𝒏𝟐 𝒏
n = 20 n = 200 n = 2000
b=
2.296 2.069 2.000
(ans.=2)
b = 2
0.847 0.091 0.0054
(ans.=0)
31
Method of Generation of Random Numbers
Analysis of Computer and Communication Networks, Fayez Gebali, Springer,2008
Continuous Case
Suppose we have a random variable 𝑿 with known pdf and cdf and we have
another random variable 𝒀 that is a function of 𝑿:
𝒀 = 𝒈(𝑿)
Here 𝑿 is named the source random variable and 𝒀 is named the target random
variable. We are interested in finding the pdf and cdf of 𝒀 when the pdf and cdf of
𝑿 are known. The probability that 𝑿 lies in the range 𝒙 and 𝒙 + 𝒅𝒙 is given by
𝑷 𝒙 ≤ 𝑿 ≤ 𝒙 + 𝒅𝒙 = 𝒇𝑿 𝒙 𝒅𝒙
But this probability must equal that 𝒀 lies in the range 𝒚 and 𝒚 + 𝒅𝒚. Thus we can
write
𝒇𝒀 𝒚 𝒅𝒚 = 𝒇𝑿 𝒙 𝒅𝒙
where 𝒇𝒀 (𝒚) is the pdf for the random variable 𝒀 and it was assumed that the
function 𝒈(𝒙) was monotonically increasing with 𝒙.
If 𝑔 was monotonically decreasing with 𝑥 , then we would have
𝑓𝑌 𝑦 𝑑𝑦 = −𝑓𝑋 𝑥 𝑑𝑥
The last two equations define the fundamental law of probabilities , which is given
by
𝑑𝑥
𝑓𝑌 𝑦 𝑑𝑦 = 𝑓𝑋 𝑥 𝑑𝑥 ⟹ 𝑓𝑌 𝑦 = 𝑓𝑋 𝑥
𝑑𝑦
since 𝑓𝑌 𝑦 and 𝑓𝑋 𝑥 are always positive.
# Suppose our source random variable is uniformly distributed and our target
random variable 𝑌 is to have a pdf given by
𝑓𝑌 𝑦 = 𝜆𝑒 −𝜆𝑦 ; 𝜆 > 0
Derive 𝑌 as a function of 𝑋.
Sol.: this example is fundamentally important since it shows how we can find the
transformation that allows us to obtain random numbers following a desired pdf
given the random numbers for the uniform distribution.
Discrete Case
In the discrete case the fundamental law of probability gives
𝑝𝑌 𝑦 = 𝑝𝑋 𝑥
where 𝑝𝑋,𝑌 𝑥, 𝑦 corresponds to the pmf of the source / target random variable.
The values of 𝑌 are related to the values of 𝑋 by the equation
𝑌 = 𝑔(𝑋)
The procedure for deriving the pmf of 𝑌 given the pmf for 𝑋 is summarized as
follows:
1. For each value of 𝑥 obtain the corresponding value 𝑝 𝑋 = 𝑥 through
observation or modeling.
2. Calculate 𝑦 = 𝑔 𝑥 .
3. Associate 𝑦 with probability 𝑝 𝑋 = 𝑥 .
• Lehmer, 1948
• Most typical so-called random number generator
• Algorithm : I n1 (aI n c) mod(m)
– a,c >=0 , m > I0,a,c
• Advantage :
– Very fast
• Problem :
– Poor choice of the constants can lead to very poor sequence
– The relationship will repeat with a period no greater than m
(around m/4)
• C complier RAND_MAX : m = 32767
RANDU Generator
• 1960’s IBM
• Algorithm
I n 1 (65539 I n ) mod(2 ) 31
Inversion Method
To generate a number obeying a given distribution , we rely on the fundamental
law of probabilities discussed earlier. When 𝑋 has a uniform distribution over
the range 0 ≤ 𝑥 ≤ 1, we may use the following procedure for obtaining 𝑦.
We have
𝑑𝑥
= 𝑓(𝑦)
𝑑𝑦
Here 𝑓 𝑥 = 1 and 𝑓(𝑦) is the function describing the pdf of the target
distribution. Therefore integrating the foregoing
𝑦
0 𝑓 𝑧 𝑑𝑧 = 𝑥 ⟹ 𝐹 𝑦 = 𝑥 ⟹ 𝐹 −1 𝑥 = 𝑦.
The procedure then to obtain the random numbers corresponding to 𝑦 is to
generate 𝑥 according to any standard random number generator producing a
uniform distribution . Then use the above equation to provide us with 𝑦. Thus
the target random number value 𝑦 is obtained according to the following steps:
1. Obtain the source random number 𝑥 having a uniform distribution.
2. Lookup the value of 𝐹(𝑦) that is numerically equal to 𝑥 according to 𝐹 𝑦 = 𝑥.
3. Find the corresponding value of 𝑦 according to 𝐹 −1 𝑥 = y. If the inverse
function is not available , then lookup table is used and 𝑦 is chosen according to
the criterion 𝐹(𝑦) ≤ 𝑥 ≤ 𝐹(𝑦 + 𝜖), where 𝑦 + 𝜖 denotes the next entry in the
table.
Graphically the procedure is summarized in Fig.2
𝐹(𝑦)
Input 𝑥
𝑦
output 𝑦
Fig.2
Sampling from a distribution
• We have a uniform distribution, how do we
sample from
– An exponential distribution?
– Other continuous distributions?
– A Poisson distribution?
Exponential distribution
• Time between
radioactive decays.
• Distance a photon
travels before an
interaction. Probability density function (pdf)
• Mean 1/, variance
1/
(Wikipedia)
x
Sampling from the exponential
distribution
• Sample u from [0,1).
• Locate u on y-axis.
• Find x = F-1(u).
u
x
Acceptance-rejection
www.mathworks.com
Acceptance-rejection