Monte Carlo Simulation
ME/AE 301
Probabilistic Engineering Design
Outline
n Introduction – What’s simulation?
n Procedure
n Details of the procedure
n Examples
n MATLAB source code for the example
n Error estimation
n Advanced simulation technique
ME301 – Probabilistic Engineering Design
Introduction
n What’s computer simulation?
n Simulation is the technique of a building a
model of a real or proposed system so that
the behaviour of the system under specific
conditions may be studied based the model.
n Simply speaking, computer simulation is
doing experiment on computer.
ME301 – Probabilistic Engineering Design
Types of Computer Simulation
n Continuous system simulation
n FEA
n Dynamics simulation
n Mechanical systems
n MATLAB simulink
n Discrete event simulation
n Events occur randomly.
n Monte Carlo simulation
n Operations research, manufacturing process
plans, etc.
ME301 – Probabilistic Engineering Design
Monte Carlo Simulation
n The Monte Carlo method provides
approximate solutions to a variety of
mathematical problems by performing
statistical sampling experiments on a
computer.
n It can be used to problems with no
probabilistic content (e.g. numerical
integration) as well as to those with
inherent probabilistic structure (uncertainty
analysis).
ME301 – Probabilistic Engineering Design
A Classical Example – Buffon's
Needle Problem
n Estimating π (17th century)
n Experiment
n Drop a needle at random on grid of parallel lines
n Count the number of the needle hitting the grid
n Estimate π
ME301 – Probabilistic Engineering Design
Buffon's Needle Problem
n If X <=Lsin(θ),
n The needle will intersect the grid
n P (needle intersects the grid) = 2L / (πD).
(L<=D)
n If we drop the needle N times and count R
intersections
n P= R/ N, and
n π = 2L/(PD)=2LN/(RD) X L
D
θ
ME301 – Probabilistic Engineering Design
Buffon's Needle Experiment
ME301 – Probabilistic Engineering Design
MCS for Uncertainty Analysis
n Given: distributions of input variables X
n Find: Probabilistic characteristics of
Y = g(X)
n Sampling on X -> Y = g(X) -> Samples of
Y
n We will use the similar approach as
Buffon's Needle Experiment to do MCS
ME301 – Probabilistic Engineering Design
Procedure of Monte Carlo
Simulation
Distributionsofof
Distributions
inputvariables
variables
input
Step1:1:Sampling
Step Samplingof
ofrandom
randomvariables
variables
Generatingsamples
Generating samplesofofrandom
randomvariables
variables
Samplesofofinput
input
Samples
variables
variables
Step2:2:Numerical
NumericalExperimentation
Experimentation Analysis Model
Step
Evaluatingperformance
Evaluating performancefunction
function
X Analysis Model Y
Y=g(X)
Samplesofofoutput
output
Samples
variables
variables
Step3:3:Statistic
Step StatisticAnalysis
Analysison onmodel
modeloutput
output
Extractingprobabilistic
Extracting probabilisticinformation
information
Probabilistic
Probabilistic
characteristicsofofoutput
output
characteristics
variables
variables
ME301 – Probabilistic Engineering Design
Step 1: Sampling on random
variables
n Generate samples of input random
variables according to their distributions
n Two steps f (x) X
Random
variables x
[0, 1] uniform
[0,
[0, 1]
1] uniform
uniform variables z
variable
variable Transformation
Transformation
fZ(z)
generator
generator
0 1
ME301 – Probabilistic Engineering Design
[0,1] Uniform Variable Generator
n Mechanical Methods
n Tossing dice, drawing balls
n Physical noise
n Computational Methods
n The values and length of the sequence are
machine and algorithm dependent.
n Seed number: determines where to start
Start from here
0.1243 0.7534 0.2177
… … … 0.0156
ME301 – Probabilistic Engineering Design
[0,1] Uniform Variable Generator
(Cont.)
n MATLAB random variable generator
n rand() – [0,1] uniform variable generator
n Example: z=rand(20,1)
n Generate 20 [0,1] uniformly distributed
variables.
0.8381 0.6813 0.8318 0.7095 0.3046 0.1934 0.3028 0.1509 0.3784 0.8537
0.0196 0.3795 0.5028 0.4289 0.1897 0.6822 0.5417 0.6979 0.8600 0.5936
ME301 – Probabilistic Engineering Design
Transformation – The Inverse
Method
n Transform z into x that follows a given
distribution FX(x)
−1
z = FZ ( z ) = FX ( x), x = F ( z ) X
cdf FX ( x )
x X
ME301 – Probabilistic Engineering Design
Examples
n Example 1
n If X is uniformly distributed within the range
[a, b],
x−a
FX ( x ) = = z, x = a + z ( b − a )
b− a
n Example 2
n If X : N ( µ X , σ X )
x − µX −1
FX ( x ) = Φ = z, x = µ X + σ X Φ ( z )
σX
ME301 – Probabilistic Engineering Design
Step 2: Numerical Experimentation
n Suppose N sets of random variable have
been generated,
x i = ( xi1 , xi 2 ,L, xin ), i = 1, 2,L , N
N - # of simulations, n - # of RVs
n N samples of the response variables are
calculated as yi = g ( x i ), i = 1,2,L N
x1, x2,…, xN Analysis Model y1, y 2,…, y N
Y=g(X)
ME301 – Probabilistic Engineering Design
Step 3: Statistic Analysis on
model output
N
1
n Mean Y = ∑ y i
N i =1
N
1
n Variance σ Y2 = ∑
N − 1 i =1
( y i − Y ) 2
n The probability of failure
+∞
p f = P { g ≤ 0} = ∫ f X ( x ) dx = ∫ I ( x ) f X ( x ) dx
−∞
# of failures g ( X )≤0
N Nf Indicator function
1
=
N
∑ I (x ) = N 1 if g (x ) ≤ 0
i =1
I (x ) =
0 otherwise
ME301 – Probabilistic Engineering Design
Step 3 (Cont.)
n cdf 1 N
FY ( y ) = P {g ≤ y} =
N
∑ ( yi )
I '
i =1
1 if g (x) ≤ y
I (x ) =
'
0 otherwise
n pdf
dFY ( y )
fY ( y ) = Numerical method
dy
ME301 – Probabilistic Engineering Design
Example
L Py
t Px
w
Allowable deflection D0 = 3 ''
L = 100'' , t = 2'' , w = 4'' , E = 30 × 106 psi
X = ( Px , Py ), Px ~ N (500,100)lb, Py ~ N (1000,100)lb
2
4 L Py Px
3 2
g ( X ) = D0 − 2 + 2
Ewt t w
Failure mode: the tip displacement > D0. p f = P {g ( X ) < 0}
ME301 – Probabilistic Engineering Design
.
10 Simulations
Px Py Y
1 456.7435 981.3291 0.8397
2 333.4416 1072.5791 1.2171
3 512.5332 941.1683 0.6502
4 528.7676 1218.3186 0.4574
5 385.3529 986.3604 1.0938
6 619.0915 1011.3931 0.2136
7 618.9164 1106.6768 0.1752
8 496.2367 1005.9281 0.6820
9 532.7292 990.4352 0.5522
10 517.4639 916.7651 0.6419
ME301 – Probabilistic Engineering Design
100 and 1000 Simulations
ME301 – Probabilistic Engineering Design
100000 Simulations
n The more simulations, a more accurate
result is obtained.
ME301 – Probabilistic Engineering Design
Error Estimation
n The accuracy is dependent on the number
of sample
n What is the error with N simulations?
n How many samples are enough given an
error?
Nf 1 N
n pf =
N
=
N
∑ I (x )
i =1
is also a random
variable.
ME301 – Probabilistic Engineering Design
Error Estimation (Cont.)
n It can be shown that
(1 − pTf ) − T
(1 p f )
P −u1−α / 2 T
< p f − p f < u1−α / 2
T
T =1−α
Np f Np f
pTf is the true value; 1−α is the confidence level.
u1−α / 2 is the percentile value.
n Therefore the error is There is a 100(1-a)%
chance that the probability
(1 − p Tf ) of failure will be in the
ε % = u1−α / 2 range of p f ± p f ε /100
NpTf
with N simulations.
ME301 – Probabilistic Engineering Design
Error Estimation (Cont.)
n 95% confidence level
(1 − p Tf )
ε % ≈ 200 T
Np f
n For the beam problem,
p f = 0.04143, and N = 100000
(1 − 0.04143)
ε % ≈ 200 = 3.0422%
100000 × 0.04143
p f = [0.0401, 0.0427]
ME301 – Probabilistic Engineering Design
Error Estimation (Cont.)
n A higher reliability (or lower probability of
failure) requires a larger number of
simulations.
(1 − pTf )
N= (95% confidence)
40000 p (ε % )
T 2
f
p f = 0.001 and ε = 20% N = 10 5
p f = 0.001 and ε = 10% N = 399,600
ME301 – Probabilistic Engineering Design
Features of Monte Carlo
Simulation
n Monte Carlo simulation is easy to use for engineers
who have only basic working knowledge of probability
and statistics.
n Monte Carlo simulation is feasible to use for virtually
any performance functions and distributions.
n Monte Carlo simulation is computationally robust; with
sufficient number of simulations, it can always
converge.
n The problem dimension (the number of random
variables) does not affect the accuracy of Monte Carlo
simulation. This feature is beneficial to large scale
engineering problems.
n For reliability analysis, Monte Carlo simulation is
generally computationally expensive. The higher the
reliability is, the larger the simulation size is needed.
ME301 – Probabilistic Engineering Design
Advanced Monte Carlo
Simulation
n Purpose: improve efficiency.
n Method: reduce the error of the estimation
of probability of failure with the same
number of simulation.
n Reduce the variance of the estimation.
n Methods include
n Conditional expectation
n Antithetic variables
n Importance sampling
ME301 – Probabilistic Engineering Design
Matlab Code for the Example
Input
n clear all;
n close all;
n %Define input variables
n t=4;
n w=2;
n E=30e6;
n L=100;
n D0=3;
n Px_mean=500;
n Px_std=100;
n Py_mean=1000;
n Py_std=100;
ME301 – Probabilistic Engineering Design
Steps 1 and 2
n % Step 1 - Sampling on random variables
n randn('state',0)
n N=1e5;
n Px_sample=normrnd(Px_mean,Px_std,N,1);
n Py_sample=normrnd(Py_mean,Py_std,N,1);
n % Step 2 - Experimentation
n g=D0-
4*L^3/E/w/t*((Py_sample/t^2).^2+(Px_sample/w^
2).^2).^0.5;
ME301 – Probabilistic Engineering Design
Step 3
n % Step 3 - Statistical analysis
n Nf=sum(g <0);
n Pf=Nf/N;
n % Plot
n plot(Px_sample,Py_sample,'.');
n xlabel('Px');
n ylabel('Py');
n text(500,2600,['Number of failures = ',num2str(Nf)]);
n text(500,2400,['Number of simulations = ',num2str(N)]);
n text(500,2200,['Probability of failure = ',num2str(Pf)]);
n text(200,1500,'Safe region');
n text(700,1500,'Failure region');
n hold
n ezplot('((30e6*2*4*3/4/100^3)^2-
Px^2/2^4)^0.5*4^2',[100,900]);
ME301 – Probabilistic Engineering Design
MPP Based Importance
Sampling
n The general MCS is
not efficient. X2
MPP (the white circle)
n Search the MPP.
n Shift the centers the
distributions to the
MPP.
Limit state g ( X )=0
n Importance sampling
distributions
n Sample from Safe region g (X ) > 0
importance sampling X1
distributions.
ME301 – Probabilistic Engineering Design
Importance Sampling
n MCS
N
1 Nf
p f = ∫ I [ g ( X )] f X (x )dx = I (x ) =
N
=
N
∑ I (x )
i =1
i
n Importance sampling
n Importance sampling density hX( x)
f X (x)
p f = ∫ I [ g ( X )] f X ( x )dx = ∫ I [ g ( X )] hX ( x )dx
hX (x )
1 N f X (x i )
= ∑ I [ g ( x i )]
N i=1 hX ( x i )
ME301 – Probabilistic Engineering Design
Example
L Py
t Px
w
Allowable deflection D0 = 3 ''
L = 100'' , t = 2'' , w = 4'' , E = 30 × 106 psi
X = ( Px , Py ), Px ~ N (500,100)lb, Py ~ N (1000,100)lb
2
4 L Py Px
3 2
g ( X ) = D0 − 2 + 2
Ewt t w
Failure mode: the tip displacement > D0. p f = P {g ( X ) < 0}
ME301 – Probabilistic Engineering Design
.
Example
Original distributions
Px ~ N (500,100), Py ~ N (1000,100)
MPP
x = (673.67, 1016.38)
*
Importance sampling distributions
Px ~ N (673.67,100)lb, Py ~ N (1016.38,100)lb
ME301 – Probabilistic Engineering Design
Results (Pf = 0.04143, N=100,1000
from general MCS)
General MCS
• Pf = 0.027 (inaccurate)
• N = 1000
Importance sampling
• Pf = 0.040976
• N = 200
ME301 – Probabilistic Engineering Design