Sheet2 Sol
Sheet2 Sol
Dr. A. Alpers
Tutorial in Week 2
Exercise 5. Let X1 , . . . , Xn be exponentially distributed random variables (i.e., their pdfs are
fXi (t) = λe−λt ) with λ = 0.2. Using matlabs exprnd command draw nTrials = 104 samples of
each Xi and plot a histogram of
(a) X1 and
√
(b) n · n1 ni=1 Xi − µ for n = 1, 5, and 30.
P
How do your plots compare with the results predicted by the central limit theorem?
Solution. Note that 1/λ is required as input to exprnd, hence exprnd(1//lambda,1,nTrials
generates the required number of samples from the distribution. It is known (or can be looked up)
that mean and variance of the exponential distribution are µ = 1/λ and σ 2 = 1/λ2 , respectively.
Results are shown in Fig. 1.
Here is the matlab code.
1 clear all ;
2 n =5; lambda =0.2;
3 nTrials =10^4;
4 a = -15; b =15;
5
6 sigma2 =1/ lambda .^2; mu =1/ lambda ;
7
8 u = zeros (1 , nTrials ) ;
9 for i =1: n
10 u = u + exprnd ( mu ,1 , nTrials ) ;
11 end ;
12 u = sqrt ( n ) *( u - n * mu ) / n ;
13
14 [f , x ] = hist (u ,150) ;
15 dx = diff ( x (1:2) ) ;
16 bar (x , f / ( sum ( f ) * dx ) ) ; hold on ; xlim ([ a b ]) ; yLimits = get ( gca , ’ YLim ’) ;
17
18 if ( n ==0)
19 plot ([0:0.1: b ] , exp ( -([0:0.1: b ]) * lambda ) * lambda , ’r ’ , ’ LineWidth ’ ,2) ; m = mean ( u ) , plot ([ m , m ] ,[0 , yLimits (2) ] , ’k ’) ;
% title ( sprintf ( ’ n =% d ’ , n ) ) ;
20 else
21 m = mean ( u ) ; plot ([ m , m ] ,[0 , yLimits (2) ] , ’k ’) ; m
22 error = mean ( abs ( f / ( sum ( f ) * dx ) -1/ sqrt (2* pi * sigma2 ) * exp ( -0.5*( x ) .^2/ sigma2 ) ) ) ;
23 xx = a :0.1: b ; plot ( xx ,1/ sqrt (2* pi * sigma2 ) * exp ( -0.5*(( xx ) .^2/ sigma2 ) ) , ’r ’ , ’ LineWidth ’ ,2) ; title ( sprintf ( ’n =% d (
mean error = %.5 f ) ’ ,n , error ) ) ;
24 end ;
CLTfigures2.m
Exercise 6. Let X be a standard Cauchy random variable, i.e., X has the pdf
1
fX (x) = , x ∈ R.
π(1 + x2 )
Page 1 of 5
n=1 (mean error = 0.00893)
0.2 0.2
0.18 0.18
0.16 0.16
0.14 0.14
0.12 0.12
0.1 0.1
8 · 10−2 8 · 10−2
6 · 10−2 6 · 10−2
−2
4 · 10 4 · 10−2
2 · 10−2 2 · 10−2
0 0
−14−12−10 −8 −6 −4 −2 0 2 4 6 8 10 12 14 −14−12−10 −8 −6 −4 −2 0 2 4 6 8 10 12 14
9 · 10−2 8
−2
8 · 10 7
7 · 10−2
6
6 · 10−2
5
5 · 10−2
4
4 · 10−2
3
3 · 10−2
2
2 · 10−2
1 · 10−2 1
0 0
−14−12−10 −8 −6 −4 −2 0 2 4 6 8 10 12 14 −14−12−10 −8 −6 −4 −2 0 2 4 6 8 10 12 14
Figure 1: Illustration of the central limit theorem: Histogram√ of samples from the exponential
distribution with λ = 0.2 (upper left) and histograms of n · n1 ni=1 Xi − µ for n = 1, 5, 30
P
where the Xi are exponentially distributed with λ = 0.2 (hence, µ = 1/λ). Number of samples
is 104 . For n = 1, 5, 30 the pdf for the normal X ∼ N (0, σ 2 ) with σ 2 = 1/λ2 is shown in red; the
respective means of the histogrammed samples are shown as black lines.
Page 2 of 5
0.35 0.7
0.3 0.6
0.25 0.5
0.2 0.4
fY
fX
0.15 0.3
0.1 0.2
5 · 10−2 0.1
0 0
−10 −8 −6 −4 −2 0 2 4 6 8 10 0 1 2 3 4 5 6 7 8 9 10
x y
Figure 2: Pdfs of X and Y. Note that the pdf of Y is fY (y) = 2fX (x), x ≥ 0.
30
25
20
(X1 +...+Xn )/n
15
10
0
0 100 200 300 400 500 600 700 800 900 1,000
n
1
Pn
Figure 3: Four realizations of n i=1 Yi .
1 n =20;
2 nTrials =10^3;
3 MAXN =10^3;
4
5 u = abs ( tan ( pi *( rand (1 , nTrials ) -0.5) ) ) ; % Abs of Cauchy distributed random variable ( by inverse CDF )
6 u2 = abs ( tan ( pi *( rand (1 , nTrials ) -0.5) ) ) ;
7 u3 = abs ( tan ( pi *( rand (1 , nTrials ) -0.5) ) ) ;
8 u4 = abs ( tan ( pi *( rand (1 , nTrials ) -0.5) ) ) ;
9
10 for i =1: MAXN
11 m ( i ) = mean ( u (1: i ) ) ;
12 m2 ( i ) = mean ( u2 (1: i ) ) ;
13 m3 ( i ) = mean ( u3 (1: i ) ) ;
14 m4 ( i ) = mean ( u4 (1: i ) ) ;
15 end ;
16
17 figure , plot (1:1: MAXN , m (1:1: end ) , ’b ’ , ’ LineWidth ’ ,2) ; hold on ;
18 plot (1:1: MAXN , m2 (1:1: end ) , ’k ’ , ’ LineWidth ’ ,2) ;
19 plot (1:1: MAXN , m3 (1:1: end ) , ’ color ’ ,[0.5 0.2 0.2] , ’ LineWidth ’ ,2) ;
20 plot (1:1: MAXN , m4 (1:1: end ) , ’ color ’ ,[0.2 0.5 0.2] , ’ LineWidth ’ ,2) ;
21 xlabel ( ’n ’) ; ylabel ( ’( X_1 +...+ X_n ) / n ’) ;
CLTfigures2.m
Exercise 7. Consider the random variable Y := e−X , where X is a random variable uniformly
Page 3 of 5
distributed on the interval (0, 1). Give precise values of E[Y ] and var(Y ).
Solution. By definition and simple calculus,
Z 1
1
E[Y ] = e−x dx = [−e−x ]10 = 1 − .
0 e
Moreover,
1
3e−2 2 1
Z
1 1
2
var(Y ) = E[Y ] − E[Y ] = 2
e−2x dx − (1 − )2 = [−e−2x /2]10 − (1 − )2 = − + − .
0 e e 2 e 2
Exercise 8. Consider the task of computing
1
e−x
Z
dx.
0 1 + x2
(b) Approximate the integral via direct Monte Carlo. Give plots of µ̂n and the (theoretical)
RMSE as a function of n.
Solution. Here is the matlab code. Fig. 4 shows a sample path of the µ̂n and the correspond-
ing RMSE.
1 nTrials =6*10^2;
2 u = rand (1 , nTrials ) ;
3 gx = exp ( - u ) ./(1+ u .^2) ;
4 for n =1: nTrials
5 muhat ( n ) = mean ( gx (1: n ) ) ; % Main part : computing the mu_n
6 end ;
7 fun = @ ( x ) exp ( - x ) ./(1+ x .^2) ; % This is only used to compute the integral ( mu and sigma2 )
8 fun2 = @ ( x ) ( exp ( - x ) ./(1+ x .^2) ) .^2; % you can skip this
9 mu = integral ( fun ,0 ,1) % you can skip this
10 sigma2 = integral ( fun2 ,0 ,1) - mu ^2; % you can skip this
11
12 figure , plot (1: nTrials , muhat , ’b ’ , ’ LineWidth ’ ,2) ; xlabel ( ’n ’) ; ylabel ( ’\ mu_n ’) ; % plots mu_n
13 hold on ; plot (1: nTrials , mu * ones (1 , nTrials ) , ’r ’ , ’ LineWidth ’ ,2) ;
14
15 figure , plot (1: nTrials , abs ( muhat - mu ) , ’b ’ , ’ LineWidth ’ ,2) ; xlabel ( ’n ’) ; ylabel ( ’ RMSE of \ mu_n ’) ; hold on ;
16 plot (1: nTrials , sqrt ( sigma2 ) ./ sqrt (1: nTrials ) , ’k ’ , ’ LineWidth ’ ,2) ;
MCintegralexample2.m
Page 4 of 5
0.65 0.3
0.6
0.25
0.55
0.2
0.5
RMSE of µn
µn
0.45 0.15
0.4
0.1
0.35
5 · 10−2
0.3
0.25 0
0 50 100 150 200 250 300 350 400 450 500 550 600 0 50 100 150 200 250 300 350 400 450 500 550 600
n n
(a) (b)
Figure 4: Direct Monte Carlo for Exercise 8. (a) Sample path of µ̂n as a function of n (blue) and
√
µ = 0.5248 (red), (b) corresponding (realized) RMSE as a function of n (blue) and plot of σ/ n
(black).
Page 5 of 5