Sei sulla pagina 1di 7

NDSU

Monte Carlo Simulations and MATLAB

ECE 341

Monte Carlo Simulations and MATLAB


Definitions:
Monte Carlo Simulation: A simulation of a random process using a large number of computer generated samples. MATLAB: Similar to SciLab Mean: The center of mass for a pdf or it's average. Also called the 1st moment. Variance: The mean squared difference between a pdf and it's average.

Discussion:
Today, we're going to be looking at how to probability density functions for discrete random processes using MATLAB. We'll also be looking at ways to describe these pdf's, such as their mean, variance, moments, etc. as well as how to compute them numerically. Following lectures will look at how to compute these numbers analytically. The nice thing about Monte-Carlo simulations is you don't need to know calculus, or much else for that matter, to get an answer. It also works for any random process. The problem is you never quite know what the mean should be exactly - only approximately. (To know the exact mean, you would need an infinite sample size.)

To start out, let's look at a Bernoulli trial with a probability of heads being 0.6 (p = 0.6) one more time. In MATLAB, you can generate 1000 such trials with the following code: First, generate 1000 random numbers in the interval [0,1]:
-->X = rand(1000,1);

The result is true (1) 60 percent of the time:


-->f = (X < 0.6);

The number of successes (p) and failures (q) is then:


-->p = sum(f); -->q = sum(1-f); -->[p,q] 630. 370.

or if you like graphs:


-->bar([0,1], [q, p]/1000)

JSG

rev August 25, 2011

NDSU

Monte Carlo Simulations and MATLAB

ECE 341

Result for a Monte-Carlo simulation with p=0.6 and sample size 1,000 Something appears amiss: you should get a success 60% of the time. Here, it was 63% of the time. If you want a more accurate result, you need a larger sample size. Repeating with 100,000 random numbers:
-->X = rand(100000,1); -->f = (X < 0.6); -->p = sum(f); -->q = sum(1-f); -->[p,q] 60163. 39837.

you now see that the result was true 60.163% of the time. Note that: For a Monte-Carlo simulation to have results which are close to the 'correct' value, you may need very large sample sizes. Even then, Monte-Carlo simulations will not give you exact answers - only ballpark results. How large the sample size needs to be to get the right answer is addressed after the 2nd midterm.

Sample Mean and Standard Deviation:


The sample mean (or first moment) is the probability weighted result of the random process:

x=

1 n

fi i=0

in MATLAB:
-->m1 = sum(p)/100000 m1 = 0.60163

Note that the mean for a Bernoulli trial is p (0.6) and for a Binomial distribution is np (60,000 here).

JSG

rev August 25, 2011

NDSU

Monte Carlo Simulations and MATLAB

ECE 341

The sample variance is the mean squared difference from the function and it's mean:
-->var = sum((f-0.6) .^2)/100000 0.239674

This isn't quite as obvious, but the variance for a Bernoilli trial should be pq (0.24) - we'll derive that next class.

Problem: Determine using a Monte-Carlo simulation (i.e. MATLAB) a) the pdf, b) the mean, and c) the variance for the following distributions: Geometric: The number of times you have to roll a die until you get a '1'. Pascal: The number of times you haev to roll a die until you get four ones. Dice: The sum of rolling three 6-sided dice. Dice take 2: The sum of a 4, 6, 8, and 10 sided die.

Geometric:
MATLAB Code: Let the sample size be 1000 (larger would be better)
Y = zeros(10000,1); for i=1:10000 n = 0; for j=1:100 if (rand() < p) n = n + 1; if (n==1) Y(i)=j; end end end end

The number of times you rolled the dice once:


sum(Y == 1) 1647.

The number of times you rolled the dice twice:


sum(Y == 2) 1440.

Putting this in a loop to save time:


for i=1:20 f(i) = sum(Y == i); end bar(f/1000) xlabel('Number of Rolls'); ylabel('Probabilty of Y rolls')

JSG

rev August 25, 2011

NDSU

Monte Carlo Simulations and MATLAB

ECE 341

pdf for a Geometric distribution: number of rolls to get a 1 on a 6-sided die.

This sort of looks like a decaying exponential. We'll verify this when we get to Geometric series in a few days.

To find the average number of times you roll the dice:

x = f ( x) x
x=1

Here, I only went out to 20 die rolls (it should be infinity):


X = [1:20]'; sum(X .* f/10000) 5.3251

On average, you will roll the dice approximately 5.3251 times. This is approximate since the sample size was only 10,000 rather than infinite. To find the variance, find the probability weighted sum of the square of the difference from the number of rolls and it's average

s = f ( x) ( x x) 2
x=1

var = sum((N - avg).^2 var = 18.857314

.* (f/10000))

The variance is a measure of the spread. Approximately, three times the square root of the variance (the standard deviation) is the spread: Spread 3 18.85 = 13

JSG

rev August 25, 2011

NDSU Pascal:

Monte Carlo Simulations and MATLAB

ECE 341

Find the number of times you have to roll dice until you get four 1's.
Y = zeros(10000,1); for i=1:10000 n = 0; Y(i) = 200; for j=1:200 if (rand() < p) n = n + 1; if (n==4) Y(i)=j; end end end end f = zeros(200,1); for i=1:200 f(i) = sum(Y == i); end

The pdf is then:


bar(f/10000)

pdf for a Pascal distribution: number of rolls to get four 1's on a 6-sided die. It's a little noisy - that's typical with Monte Carlo simulations. As the sample size goes to infinity, it'll get smoother. The mean and standard deviation is then computed same as before
avg = sum(X .* f/10000) 24.1363 var = sum((X - avg).^2 120.93272 .* (f/10000))

On average, you have to roll the dice 24.13 times before you'll get four 1's. The data will have a spread of approximately: spread 3 120.9 = 33
JSG 5 rev August 25, 2011

NDSU Dice: Rolling 3d6:

Monte Carlo Simulations and MATLAB

ECE 341

Problem: Generate three random numbers from 1..6, each number with equal probability:
d1 = int(rand(10000,1)*6+1); d2 = int(rand(10000,1)*6+1); d3 = int(rand(10000,1)*6+1);

The sum of three six-sided dice is then:


Y = sum(d1+d2+d3);

The pdf is:


for i=1:18 f(i) = sum(Y == i); end bar(f/10000) xlabel('Sum of Dice Value'); ylabel('Probability')

pdf for the sum of rolling three six-sided dice The mean and variance are:
-1->X = [1:18]'; -1->avg = sum(X .* f/10000) avg = 10.4878 -1->var = sum((X - avg).^2 var = 8.8212512 .* (f/10000))

The average number you will roll is 10.48. The spread is about 8.9 (three times the standard deviation).

JSG

rev August 25, 2011

NDSU

Monte Carlo Simulations and MATLAB

ECE 341

Dice: Rolling d4 + d6 + d8 + d10:


Problem: Generate four random numbers from 1..6, each number with equal probability:
d1 d2 d3 d4 = = = = int(rand(10000,1)*4+1); int(rand(10000,1)*6+1); int(rand(10000,1)*8+1); int(rand(10000,1)*10+1);

Y = d1+d2+d3+d4; f = zeros(30,1); for i=1:30 f(i) = sum(Y == i); end bar(f/10000) xlabel('Sum of Dice Value'); ylabel('Probability')

pdf for the sum of a 4, 6, 8, and 10 sided die


X = [1:30]'; avg = sum(X .* f/10000) 16.0199 var = sum((X - avg).^2 17.707904 .* (f/10000))

The total will have an average of 16.02 with a spread of 12.6 (approximately)

JSG

rev August 25, 2011

Potrebbero piacerti anche