Monte Carlo Simulations and MATLAB
Monte Carlo Simulations and MATLAB
Monte Carlo Simulations and MATLAB
ECE 341
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);
JSG
NDSU
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.
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
NDSU
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
JSG
NDSU
ECE 341
This sort of looks like a decaying exponential. We'll verify this when we get to Geometric series in a few days.
x = f ( x) x
x=1
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
.* (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
NDSU Pascal:
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
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
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);
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
NDSU
ECE 341
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')
The total will have an average of 16.02 with a spread of 12.6 (approximately)
JSG