Title: Matlab Programming
1Matlab Programming
Statistical Simulations
2Monte Carlo Method
a statistical simulation that relies on the
probabilities involved with the repeated use of
random numbers
3A Monte Carlo Method Scenario
Flipping a Coin
4A Simple Probability Problem
We have a bag containing 50 golf balls. They are
all white, except for one blue ball. If we
randomly select one ball at a time and remove it
from the bag, what is the expected number of
balls that we need to select to get the blue ball?
5Code For One Trial
let the blue ball be represented by a 1
generated by randperm number_of_balls
50 ballPicks randperm(number_of_balls)
counter 1 while ballPicks(counter) 1
counter counter1 end
6Complete Code
let the blue ball be represented by a 1
generated by randperm number_of_balls
50 trials 1000 for i 1trials
ballPicks randperm(number_of_balls)
counter 1 while ballPicks(counter) 1
counter counter1 end
trialResults(i) counter end aveNumPicks
mean(trialResults)
7Results of Code
Trials
Average Number of Picks
1000
25.2720
Results should converge to 25.5
25.4638
5000
25.5192
10,000
50,000
25.5466
100,000
25.4893
8Monte Carlo Integration
y 1/2
9Monte Carlo Integration
Integral (Percentage of Points Below line y
1/2)(Area of Range of Random Points)
10Problem
Use Monte Carlo Integration to solve
p
ò
sin(x) dx
0
11p
ò
sin(x) dx
0
y
1
Random Point Range 0 x p 0 y 1
x
p
12Monte Carlo Integration Code
numTrials 1000 counter 0 for i
1numTrials random_x pirand(1)
random_y rand(1) if random_y lt
sin(random_x) counter counter 1
end end proportion of random points under
curve proportion counter/numTrials area of
range of possible random points area
pi1 solution proportionarea
13Results of Code
Trials
Solution
1000
2.0138
Solution should converge to 2
2.0339
5000
2.0053
10,000
50,000
1.9830
100,000
1.9945
1,000,000
2.0013