Sample solutions for HW3

Math 381
Solutions to HW #3
Greenbaum
February 4, 2007
Solutions for HW #3
Problem 1
One way to make a discrete random variable K taking values 1 . . . 6 from a uniform(0,1) variable
U is to break the interval [0,1] into sixths and see which sixth K lands in. For convenience,
I’ve put this in a function. This is placed in a separate file called roll fair die.m.
function [k] = roll_fair_die
% rolls a fair die and returns 1, ..., 6 with equal probability
u = rand;
if (u < 1/6)
k = 1;
elseif (u < 2/6)
k = 2;
elseif (u < 3/6)
k = 3;
elseif (u < 4/6)
k = 4;
elseif (u < 5/6)
k = 5;
else
k = 6;
end
1
Problem 2
Here’s the code to simulate rolling a fair die 1000 times and plot the number of 1’s, 2’s, etc.
bins = zeros(6,1);
for count = 1:1000
k = roll_fair_die;
bins(k) = bins(k) + 1;
% count the number of times each number is generated
end;
bar(bins); % plot the bins
This gives the plot
180
160
140
120
100
80
60
40
20
0
1
2
3
4
5
6
The expected value and variance of the random variable K are found by:
1
7
E(K) = (1 + 2 + 3 + 4 + 5 + 6) = = 3.5
6
2
1
91
E(K 2 ) = (12 + 22 + 32 + 42 + 52 + 62 ) =
6
6
35
2
2
= 2.916667
var(K) = E(K ) − E(K) =
12
Let A1000 be the average value of rolling a die 1000 times. Then we can get a histogram for
A1000 by simulating it 100 times as follows:
A = zeros(100,1);
for n_tries = 1:100
bins = zeros(6,1);
for count = 1:1000
k = roll_fair_die;
A(n_tries) = A(n_tries) + k;
% add each value k
2
end;
A(n_tries) = A(n_tries) / 1000;
end;
hist(A);
% divide by 1000 to get the average of k
% plot a histogram of the average values
The histogram should look something like:
25
20
15
10
5
0
3.35
3.4
3.45
3.5
3.55
3.6
3.65
3.7
We may compute the mean and variance of A1000 easily using the CLT. Since A1000 is the
mean of 1000 identical, independent K’s, we have
E(A1000 ) = E(K) = 7/2 = 3.5
var(A1000 ) = var(K)/1000 = 0.00291667
p
stddev(A1000 ) = var(K) = 0.0540062.
Now repeat the process with a loaded die with probabilities
x
p(x)
1
1/10
2
1/10
3
1/10
4
1/10
5
1/10
%%%% beginning of roll_loaded_die.m
function [k] = roll_loaded_die
6
1/2
%%%%
u = rand;
if (u < 1/10)
k = 1;
elseif (u < 2/10)
k = 2;
elseif (u < 3/10)
k = 3;
elseif (u < 4/10)
k = 4;
3
elseif (u < 5/10)
k = 5;
else
k = 6;
end
%%%% end of roll_loaded_die.m
%%%%
bins = zeros(6,1);
for count = 1:1000
k = roll_loaded_die;
bins(k) = bins(k) + 1;
% count the number of times each number is generated
end;
bar(bins); % plot the bins
A = zeros(200,1);
for n_tries = 1:200
bins = zeros(6,1);
for count = 1:1000
k = roll_loaded_die;
A(n_tries) = A(n_tries) + k;
% add each value k
end;
A(n_tries) = A(n_tries) / 1000; % divide by 1000 to get the average of k
end;
hist(A); % plot a histogram of the average values
This time, even though the probability density for a single die is very skewed, the central limit
theorem still forces the average A1000 to be approximately normal.
600
500
400
300
200
100
0
1
2
3
4
4
5
6
45
40
35
30
25
20
15
10
5
0
4.25
4.3
4.35
4.4
4.45
4.5
4.55
4.6
4.65
4.7
In this case, we have:
1
1
1
1
1
1
9
E(K) =
1 + 2 + 3 + 4 + 5 + 6 = = 4.5
10
10
10
10
10
2
2
1
1
1
1
1
1
47
2
2
2
2
2
2
2
E(K ) =
1 + 2 + 3 + 4 + 5 + 6 =
= 23.5
10
10
10
10
10
2
2
2
9
13
47
2
2
−
=
= 3.25
var(K) = E(K ) − E(K) =
2
2
4
And by the Central Limit Theorem,
E(A1000 ) = E(K) = 9/2 = 4.5
var(A1000 ) = var(K)/1000 = 0.00325
p
stddev(A1000 ) = var(A1000 ) = 0.0570088
5
Problem 3
x2
+ y 2 = 1 by plotting random points in the
4
rectangle [−2, 2] × [−1, 1]. The Matlab code to do this is below:
We wish to estimate the area inside the ellipse
% plot the ellipse
x = [-2 : 0.01 : 2];
y = sqrt(1 - x.^2/4);
plot(x,y,’-r’, x,-y,’-r’);
hold on
% plot 1000 random points in the ellipse and count how many landed inside
inside = 0; % number that landed inside
for counter = 1:1000
% pick a random point in [-2,2]x[-1,1]
x = 4*rand - 2;
y = 2*rand - 1;
% plot the point
plot(x,y,’.’);
% if the point is inside the ellipe, increment our count
if (x^2 / 4 + y^2 <= 1)
inside = inside + 1;
end;
end;
% Find the area of the ellipse. Since the total area of the rectangle
% [-2,2]x[-1,1] is 8, multiply the fraction of those in the ellipse
% by 8 to get the approximate area of the ellipse.
area = inside / 1000 * 8
Plotting the points and the ellipse looks like:
6
1
0.8
0.6
0.4
0.2
0
−0.2
−0.4
−0.6
−0.8
−1
−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
and (this time) we get 787/1000 inside, with an estimated area of 6.2960.
Now, to can see how good this estimate is. Let Xi be the random variable that is 1 if the ith
point is in the ellipse and 0 otherwise. Since Xi is 1 with probability approximately 0.787 and
0 otherwise, we have:
E(Xi ) = 0.787 · 1 + 0.213 · 0 = 0.787
E(Xi2 ) = 0.787 · 12 + 0.213 · 02 = 0.787
var(Xi ) = E(Xi2 ) − E(Xi )2 = 0.167631
p
stddev(Xi ) = var(Xi ) = 0.409428
Now, let A1000 =
1
1000
P1000
i=1
Xi . Then the Central Limit theorem gives
E(A1000 ) = E(Xi ) = 0.787
var(A1000 ) = var(Xi )/1000 = 0.000167631
p
stddev(A1000 ) = var(A1000 = 0.0129472
The quantity 8A1000 estimates the area. We have:
E(8A1000 ) = 8E(A1000 ) = 6.296
var(8A1000 ) = 64var(A1000 ) = 0.0107284
p
stddev(8A1000 ) = var(A1000 ) = 0.103578
We can use this to make a confidence intervals for our estimated area. For example, with
one-sigma (68%) confindence, the estimate will be within 0.103578 of the true value; with
two-sigma (95%) confidence, our estimated area will be within 2 · 0.103578 = 0.207 of the true
area.
7