Random Intro Text
Random Intro Text
1 Random Numbers
In order to use the Monte Carlo method, we need to be able to generate random numbers;
that is, a sequence of numbers with the property that it is not possible to predict the next
number knowing all of the previous values. Random numbers can come from a variety of
probability distributions. A probability distribution is a formula, table or graph that provides
the probability associated with each value of a random variable. Two most commonly used
distributions are uniform and normal.
>> x = rand(100,1);
A useful way to visualize random numbers is to use a histogram plot of the values. The
Matlab function hist is used for this purpose:
>> N = hist(x);
This bins the elements of x into 10 equally spaced containers, and returns in N the number
of elements in each container. One can use more (or less) than 10 containers. For example:
1
bins the elements of x into 8 equally spaced containers, and returns in N the number of
elements in each container.
Note that with only 100 random numbers, we do not see the “uniform” distribution
(i.e., the heights of each container in the histogram plot are not approximately the same).
However, by constructing a larger set of random numbers, the uniform distribution becomes
more apparent. Try:
>> x = rand(10000,1);
>> hist(x)
Note that in this case, the heights of the containers are now approximately equal.
In some cases we may want the random numbers to be in an interval other than (0, 1),
or we may want to generate a sequence of random integers. Here are some examples:
>> x = 3*rand(100,1) + 7;
creates a vector containing 100 entries uniformly distributed in the interval (7, 10).
• To generate a sequence of n uniformly distributed random integers in the set {0, 1, . . . , k},
use floor((k+1)*rand(n,1)). For example,
>> x = floor(11*rand(20,1));
>> x = floor(51*rand(20,1)+100);
2
1.5
0.5
0
−1 −0.5 0 0.5 1 1.5 2
3
0.25
0.2
0.15
0.1
0.05
0
0 1 2 3 4 5 6 7 8 9 10
4
µ = 0, σ = 1 µ = 0, σ = 2
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
−10 −5 0 5 10 −10 −5 0 5 10
µ = 0, σ = 0.5 µ = 5, σ = 0.5
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0 0
−10 −5 0 5 10 −10 −5 0 5 10
Figure 3: Plot of the PDF for a normal probability distribution for various values of µ and
σ.
5
2 Monte Carlo Method
The Monte Carlo method can be used to determine an unknown quantity through exper-
imentations involving random processes. We illustrate the approach through an example
that can be used to estimate the value of π. Suppose we have a square target with vertices
(1,1), (1,-1), (-1,1) and (-1,1), with a circle having radius equal to 1 inside the square. That
is:
1.5
0.5
−0.5
−1
−1.5
−1.5 −1 −0.5 0 0.5 1 1.5
Suppose we now throw darts randomly at the target, and that they can land anywhere
on the square with equal probability (i.e., a uniform distribution). Then if we throw enough
darts, it is reasonable to use the approximation:
Number of throws inside circle Area of circle
≈ .
Number of throws Area of square
Problem 4 Show that this experiment can be used to approximate the value of π as:
Number of throws inside circle
π≈4 . (1)
Number of throws
We will design a Matlab experiment to simulate this experiment, and hence compute
an approximation of π. First, the following code can be use to construct the dart board:
figure
plot([-1,1,1,-1],[-1,-1,1,1])
plot([-1,1,1,-1,-1],[-1,-1,1,1,-1])
6
axis([-1.5,1.5,-1.5,1.5])
axis square
hold on
theta = linspace(0,2*pi,200);
plot(cos(theta), sin(theta),’r’)
We now want to generate random dart throws at the board. That is, we want to generate
random points (x, y) with −1 ≤ x ≤ 1 and −1 ≤ y ≤ 1. We can do this (for, say, 100 dart
throws) as follows:
x = 2*rand(100,1) - 1;
y = 2*rand(100,1) - 1;
If we want to visualize where these dart throws hit the board, we simply plot the points:
plot(x, y, ’o’)
Since only 100 random throws were used, it is unlikely that this estimate for π will be very
accurate. Better accuracy can be obtained by increasing the number of throws.
Problem 5 Try increasing the number of throws to 1000, 10000 and 100000. What is the
estimated value of π that you compute? What do you conclude about using a Monte Carlo
method for computing an approximation of π?