Professional Documents
Culture Documents
Introduction To Stochastic and Monte Carlo Method
Introduction To Stochastic and Monte Carlo Method
Lecture # 08
Introduction to Modeling
Disclaimer: This lecture note was edited from various sources for the solely of teaching and learning purposes. It may contain copyrighted materials from their respective
owners; therefore, apart form teaching and learning purposes, this lecture note may not be reproduced, stored, or transmitted in any form or by any means.
Introduction
3
http://www.flightglobal.com/blogs/wp-content/uploads/mt/
flightglobalweb/blogs/hyperbola/2012/05/10/HMS-Prins-of-Wales-
Queen-Elizabeth-class-aircraft-carrier.jpg
Simulation is used in many contexts, such as
simulation of technology for performance
optimization, safety engineering, testing, training,
education, and video games.
Often, computer experiments are used to study
simulation models.
Simulation is also used with scientific modelling of
natural systems or human systems to gain insight into
their functioning.
Model detail
Whether a model is good or not depends on the
extent to which it provides understanding.
All the models are simplification of reality: Exact
copy of a reality can only be the reality itself.
There is always a trade off as to what level of
detail is included in the model:
Too little detail: risk of missing relevant
interactions.
Too much detail: Overly complicated to
understand.
Simulation is thus the manipulation of a model in
such a way that it operates in time or space to
summarize it.
Why use simulation?
11
Time Time
(Fridays)
20
ContinuousState and Discrete State Models:
Example: Time spent by students in a weekly class
vs. Number of jobs in Q.
(b) Discrete
(a) Continuous state
state
Time Number
spent of
by jobs
students in the
queue
Time Time
(Fridays)
Other type models
21
Static
and Dynamic Models:
CPU scheduling model vs. E = mc2
Deterministic and Probabilistic Models:
Output
Output
Input Input
Simulation with respect to results
Deterministic: established or decided beyond
dispute or doubt
Stochastic: randomly determined; having a random
probability distribution or pattern that may be
analyzed statistically but may not be predicted
precisely.
Deterministic simulation
A model that does not contain probability.
Every run will result the same.
Single run is enough to evaluate the result
Stochastic simulation
A model that contains probability.
Units, process, events or their parameters are
initiated randomly using random numbers.
Ifdifferent runs are initiated with different random
number seeds, every run will result differently.
Multiple runs are required to evaluate the results.
Statistics such as averages, standard deviations
are used for evaluation.
Stochastic vs. Deterministic
26
System Model
1
Deterministic Deterministic
3
2
Stochastic Stochastic
4
Stochastic Modeling is a method in which one or
more variables within the model are random.
Monte Carlo methods can be used to study both
deterministic and stochastic problems.
For a stochastic model, it is often natural and easy
to come up with a stochastic simulation strategy due
to the stochastic nature of the model, but depending
on the question asked a deterministic method may
be used.
The use of a stochastic method is often motivated by
the fact that a deterministic method to answer the
same question is not available, that it is too
complicated to be practically useful, or that it is
computationally intractable, which is often the case
if the problem is high dimensional.
On the other hand, when a deterministic method is
applicable, it is often preferable due to the very
slow convergence of Monte Carlo methods.
No matter the reason for using Monte Carlo
methods, they will inevitably require many random
numbers.
The quality of the pseudo random generators is
crucial to the correctness of the results computed
with a stochastic algorithm, and the speed with
which they are generated is vital to the
performance of the stochastic method.
Stochastic Methods
Introduction
31
1000
4000
800
Frequency of x
Frequency of x
3000
600
2000
400
1000
200
0 0
0 0.2 0.4 0.6 0.8 1 -5 0 5
x x
>> x = rand
x=
0.3557
To
make arrays of random numbers,
>> x = rand(1,4)
x=
0.0806 0.2473 0.3669 0.7020
>> x = rand(2,3)
x=
0.1725 0.7048 0.9343
0.4748 0.1282 0.3119
A coin-tossing simulation
By inspecting the histogram of the uniformly
distributed random numbers, observe that half of
the values are between 0 and 0.5, and the other
half are between 0.5 and 1.0.
That is, P(0.5 > x 0) = P(1.0 > x 0.5) = 0.5.
We can use this to simulate a coin toss:
x = rand
if (x < 0.5),
toss=1 % Head
else
toss=0 % Tail
end
The expression x < 0.5 evaluates to 1 if true and 0
if false.
Therefore, we can express the coin toss more
compactly like this:
>>x = rand
toss = (x < 0.5)
Ifwe want to simulate a bunch of coin tosses, we
can do it with almost the same code.
Generate a vector of random numbers, then, the
expression x < 0.5 evaluates to a vector of 1s and
0s.
Ntoss = 100;
x = rand(1, Ntoss);
toss = (x < 0.5);
To
check how each toss goes:
>> n=numel(find(toss==0))
n=
55
>> n=numel(find(toss==1))
n=
45
Youcan make the coin toss biased.
Suppose you want P(Head) = 0.6, just change the
expression for toss to
16000
Frequency of x
10000
h = findobj(gca,... 8000
6000
'Type','patch'); 4000
set(h,'FaceColor',... 2000
'w','EdgeColor','b')
0 1 2 3 4 5 6
x
16000
14000
12000
Frequency of x 10000
8000
6000
4000
2000
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x
Now just add 1 to shift the numbers to the correct
range.
Nroll=10000;
x = fix(6*rand(1, Nroll)) + 1;
[h, xbin] = hist(x, 1:6);
stem(xbin, h) , xlabel('x'), ylabel('Frequency of x');
x1=numel(find(x==1))
18000
16000
14000
12000
Frequency of x 10000
8000
6000
4000
2000
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
x
Simulating risks and rewards
Many engineering decisions are analyzed in terms
of risks vs. benefits.
When there is a random component to the process
being studied, a simulation can be helpful.
As usual, games of chance provide simple
examples.
Example
Suppose a coin is tossed 5000 times. Each time heads
occurs, we win a dollar, otherwise we lose a dollar. Let
S(n) be our accumulated winnings after n tosses. Let us
consider how many times during the 5000 tosses S(n)
will go from a positive balance to a negative balance,
or vice versa.
Solution
The texts solution tosses the coins one at a time, and
either increments or decrements the score
depending on the outcome of each toss.
We can adapt the coin toss procedure developed
earlier.
For demonstration purposes, well use just 6 tosses.
Ntoss = 6;
u = rand(1, Ntoss)
u=
0.7382 0.1763 0.4057 0.9355 0.9169
0.4103
We want to register a score of 1 for tosses that
result in tails and +1 for tosses that result in heads.
An easy way to do this is to begin with a vector of
1s:
s = -1 * ones(1, Ntoss)
s=
-1 -1 -1 -1 -1 -1
Now, we can use the find command to identify those
turns where the toss is heads.
h = find(u<0.5)
h=
2 3 6
Change the corresponding elements of the score
vector to 1s.
s(h) = 1
s=
-1 1 1 -1 -1 1
Finally,we can use the cumsum command to
calculate our total winnings at each turn.
winnings = cumsum(s)
winnings =
-1 0 1 0 -1 0
Hereis the code for 5000 tosses, plus a graph of
the accumulated winnings for two different games.
Ntoss = 5000;
s = -1 * ones(1, Ntoss);
u = rand(1, Ntoss);
h = find(u < 0.5);
s(h) = 1;
% The two lines can be combined: s (u<0.5) = 1
winnings = cumsum(s);
plot(1:Ntoss, winnings)
xlabel('Toss')
ylabel('S(n)')
winnings(Ntoss)
40 60
20 50
40
0
30
-20
S(n)
S(n)
20
-40
10
-60
0
-80 -10
-100 -20
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Toss Toss
Monte Carlo Methods
Introduction
76
10000
8000
6000
4000
2000
0
0 1 2 3 4 5 6 7
Continuous random outcomes
Simulations often require continuous random
outcomes, for example:
>>X= rand(100000,1);
>>hist(X,100)
100,000 random outcomes stored in a vector X
X is distributed 1200
outcome between 0
and 1 is equally 200
likely. 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Using random numbers to estimate
Populate a square with n
randomly-placed points
[the blue and red points]
-1.0 x< +1.0
-1.0 y< +1.0
Count the number of points m that lie inside a circle
of unit radius [the blue points]
Then
m/n /4 n = 100000;
x = 2*rand(n,1)-1;
4m/n y = 2*rand(n,1)-1;
m =sum(x.^2+y.^2<1);
disp(4*m/n)
>>mc_pi
3.14836
clear xc yc;
n = 10000;
x = 2*rand(n,1)-1;
y = 2*rand(n,1)-1;
j=0;
for i=1:n
if x(i)^2 + y(i)^2 < 1
j=j+1;
xc(j)=x(i);
yc(j)=y(i);
end
end
plot(x, y, 'r.') % Red dots
hold on % More plotting to come
plot(xc,yc,'b.') % Blue dots
hold off % Finished plotting
Generally a factor of 1
a factor of 10
0.6
improvement in
0.4
0.2
accuracy.
0
accuracy -1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Example 1
Findthe maximum of the following function in the
range 0 x <
= 0.5 + sin 3 2
Solution
Generate a large number of random values x in the
range 0 x < ,
Evaluate f(x) at each point, and
Record the position of the largest value.
n = 100000;
x= pi*rand(n,1); % 0 x< pi
f= x.*(0.5+exp(-x).*sin(x.^3)).^2; 1
x (0.5+exp(-x) sin(x3))2
0.4
0.2
f(2.990116) = 0.905360 0
0 0.5 1 1.5
x
2 2.5 3 3.5
Convergence
n
101 f(2.461499) = 0.774204
102 f(3.006002) = 0.890796
103 f(2.991390) = 0.905266
104 f(2.989944) = 0.905358
105 f(2.990159) = 0.905360
106 f(2.990135) = 0.905360
107 f(2.990134) = 0.905360
Optimization
Similarto the idea of obtaining the maximum or
minimum of a function, we sometimes wish to
optimize a system; i.e. maximize or minimize a
target quantity.
Example 2
We have a rectangular sheep enclosure (sides a and
b) constructed from 200 meters of fencing. We want
to optimize the area of the enclosure. i.e. find the
values of a and b such that the area = a b is
maximized under the constraint 2a + 2b = 200
meters (a + b = 100).
Solution
The solution to this problem is to search a =
0:0.1:100 and use max(a*b) to find the optimal
value of a.
Alternatively, in the Monte Carlo approach vector a
is a set of random values 0<a<100
n = 100000;
a = 100*rand(n,1); % 0 < a < 50
b = 100 - a; % b = 100 a
[A, i] = max(a.*b);
fprintf('a=%f, area=%f\n', a(i), A)
a=49.999780, area=2500.000000
Convergence
n
101 a=47.952550, area=2495.807948
102 a=49.578598, area=2499.822420 a=50
1/0.083390 = 11.991846
Convergence
n
101 1/0.100000 = 10.000000
102 1/0.110000 = 9.090909
103 1/0.070000 = 14.285714
104 1/0.082500 = 12.121212
105 1/0.082070 = 12.184720
106 1/0.083394 = 11.991270
107 1/0.083366 = 11.995341
Exercise
Bacteria are grown in culture dishes in a laboratory.
Experience tells us that on average in this lab 20% of
the dishes become contaminated by unwanted
bacteria (thus spoiling the culture). If the lab is
growing bacteria in ten dishes, what is the probability
that more than half of the dishes will become
contaminated? Use Monte Carlo method to solve this
problem!
Solution
We have ten dishes, P(contamination of each dish)
= 0.2
Use a Monte Carlo experiment to test each dish
against the probability of 0.2.
Repeat this n times and count the number of times m
where more than 5 dishes become contaminated.
Then m/n P(more than 5 dishes are
contaminated) as n
n=10000; m= 0;
for i=1:n
k = sum(rand(10,1)<0.2);
if k > 5 % greater than 5 dishes spoiled
m= m+1;
end
end
disp(m/n)
Convergence: Analytical calculation gives
n 0.637% (Binomial
101 0 distribution).
102 0 This method works, but
103 0.0050000 requires very large statistics
104 0.0059000 to obtain good accuracy.
105 0.0061300
106 0.0063660
107 0.0063631
References
109