You are on page 1of 3

MIDTERM ASSIGNMENT

In this assignment you are going to numerically explore some of the concepts that were discussed in
class. Please follow the instructions and provide detailed answers to the questions within. Your results
and answers should be summarized in a report which includes:
requested plots,
all Matlab codes you have used to run the tests,
and answers to the questions.
Note that
Not more than 2 students are allowed to work on each report.
The report should be submitted a week before the final exam.
The score for this work will make 40% (Magen) of your final mark.
1. R ELEVANT M ATLAB COMMANDS
In what follows lowercase letters x, y will sometimes be used to denote Matlab vectors (rather than
random variables). The i-th element in a vector x is written as x(i).
You will probably need the following commands when writing down your code:
Matlab allows generating (pseudo) random numbers from various distributions. Here are some
useful ones:
unifrnd(a, b, m, 1)
generates a m 1 vector of random numbers according to uniform distribution on the interval
[a, b].
normrnd(, , m, 1)
generates a m 1 vector of random numbers according to a Gaussian distribution with mean
and variance 2 .
binornd(1, q, m, 1)
generates a m 1 vector of random numbers according to a Bernoulli distribution, i.e.
Pr(x = 1) = q,

Pr(x = 0) = 1 q

If x is a vector of random numbers generated as described above then you can compute the
average (mean) and the sample variance (see below) of all these numbers using
mean(x),

var(x)

You may also get an approximation of the density underlying the random numbers in x by asking
Matlab to plot a histogram. The command is
hist(x, n)
where n is the number of bins along the X axis. Matlab counts the number of values of x that
fall within any bin and plot this number as a bar in the place of the underlying bin. If x contains
many numbers generated from a given distribution and you take n large enough then you should
see a plot which resembles the density function.
1

MIDTERM ASSIGNMENT

2. T HE ASSIGNMENT
(1) The law of large numbers (LLN) tells us that
N
1 X
lim
g(Xi ) = E [g(x)]
N N

a.s.

i=1

where x is a random variable and g() a function. We are going to check this numerically.
(a) Generate a vector x of N random numbers from a Bernoulli distribution with parameter q =
0.3. Compute their average and call it SN . Repeat the experiment for N = 2, 3, . . . , 5000.
Plot the numbers SN with respect to N . Explain how this figure supports the LLN.
(b) Repeat (1.a) with a Gaussian distribution whose mean and variance are = 0 and 2 = 1.
Again, explain how the plot you got supports the LLN.
(c) Generate a vector x of N random numbers from a Bernoulli distribution with parameter
q = 0.6. Compute the average of (x(i) q)2 , i = 1, . . . , N and call it SN . Repeat the
experiment for N = 2, 3, . . . , 5000. Plot the numbers SN with respect to N . Explain how
this figure supports the LLN.
(d) Generate two vectors of N random numbers each from a Gaussian distribution with mean
= 0 and variance 2 = 1. Call them x and y. Compute the average of x(i)y(i) and call
it SN . Repeat the experiment for N = 2, 3, . . . , 5000. Plot the numbers SN with respect to
N . Explain how this figure supports the LLN. Explain why the values of SN converge to
this particular value.
(2) The central limit theorem (CLT) tells us that the LLN estimate converges with a rate of 1N to
the expectation E[g(x)] and that its distribution around this mean is Gaussian. We are going to
check this numerically.
(a) Generate M = 100 vectors xj , j = 1, . . . , M each of which contains N random numbers
from a Bernoulli distribution with parameter q = 0.3. Compute the average for each vector
j
. Compute the sample variance
xj and call this number SN
N

M
1 X j
=
(SN )2 ,
M
j=1

M
1 X j
=
SN
M
j=1

or equivalently, using Matlab command,


N = var([S1N , . . . , SM
N ])
Repeat the experiment for N = 2, 3, . . . , 100. Plot the numbers N with respect to N .
Explain how this figure supports the CLT.
(b) Repeat (2.a) for a Gaussian distribution with mean = 0 and variance 2 = 1.
(c) Generate M = 1000 vectors xj , j = 1, . . . , M each of which contains N random numbers
from a Bernoulli distribution with parameter q = 0.3. Compute the average for each vector
j
xj and call this number SN
. Define a vector
 1

M
VN = SN
, . . . , SN
Draw a histogram for VN using N = 10, N = 100, and N = 1000. Explain how this
supports the CLT.
(3) Recall the Buffons needle problem presented in class. It allows us to approximate the number
by generating random numbers for x (the distance between the middle of needle to the closest
parallel line) and , the orientation of the needle. We have taken x and as uniform random

MIDTERM ASSIGNMENT

variables on the intervals x U [0, b] and [0, /2]. The needle crosses a line whenever
x a cos(). Here 2a and 2b are the length of needle and the distance between any nearby
parallel lines, respectively.
Write down a code to simulate the experiment by generating random numbers for x and : for
each pair of random numbers x and check if the needle crossed a line. Run N = 1000 tests
and count the number of times the needle crossed lines. Use this to compute an estimate of .
(4) Run the Buffons needle test for N = 102 , . . . , 105 and plot the estimate value of against N .
What would be the best approximation you could get with such a method? Why do you think
this is so?
(5) Here are two ways of generating half a circle using random variables.
Method 1: Generate a vector x of N random numbers from uniform distribution U [1, 1]. Comp
pute y(i) = 1 x(i)2 , i = 1, . . . , N . The two vectors x and y hold the coordinates of N
points on the circle.
Method 2: Generate a vector t of N random numbers from uniform distribution U [0, ]. Compute x(i) = cos(t(i)) and y(i) = sin(t(i)), i = 1, . . . , N . The two vectors x and y hold the
coordinates of N points on the circle.
Which of the methods is a better one? (Hint: gain intuition by simulating this for a large
number N ). Do you have an idea why?

Good Luck.

You might also like