Consider and random variable RV X that is exponentially dist
Consider and random variable (RV) X that is exponentially distributed with
E[X]=5. Using MATLAB, do the following:
a. Generate 1,000,000 samples of this random variable.
b. Plot in figure 1 the histogram of the generated samples (bar chart) and
include the labels.1
c. Plot in figure 2 the p.d.f of the generated samples and compare it to the
theoretical p.d.f of exponential R.V. In your plot use the following
settings:
i. Use black line for the theoretical p.d.f.
ii. Use red diamonds for the experimental one.
iii. Include axes labels, title and legend.
d. Plot in figure 3 the c.d.f of the generated samples and compare it to the
theoretical c.d.f of exponential R.V. In your plot use the following
settings:
i. Use black line for the theoretical c.d.f.
1
ii. Use red diamonds for the experimental one.
iii. Include axes labels, title and legend.
e. Submit your MATLAB code with your comments on EACH LINE
Solution
%a. Generating the samples
%mean is 5, so distribute is exp( -x/5 )
samples = exprnd(5,100000,1); %returns 100000*1 array with sample numbers
%b. Plotting the histogram
mini = min( samples );
maxi = max( samples );
bins= 50;
h = hist(samples, bins);
xs = zeros( bins ,1);
for i=1:bins
xs(i,1) = mini + ((i-1)*(maxi- mini))/bins;
end
figure;
bar( xs, h );
xlabel( \'bins\' );
ylabel( \'histogram\' );
%c. PDF
figure;
z = zeros( bins, 1);
theoriticalY = zeros( bins, 1 );
for i=1:size(z,1)
z(i,1) = 0.2*(h(1,i)/max(h));
theoriticalY(i,1) = 0.2*exp(-0.2*xs(i,1));
end
plot( xs, z, \'rd\');
hold on;
plot( xs, theoriticalY, \'b\');
xlabel( \'x\' );
ylabel( \'pdf of exp(-0.2x)\' );
title( \'Experimental and theoretical PDF of exp R.V.\');
legend( \'Experimental PDF\', \'Theoritical PDF\' );
%d. CDF
figure;
z = zeros( bins, 1);
theoriticalY = zeros( bins, 1 );
tillNow = 0;
for i=1:size(z,1)
tillNow = tillNow + h(1,i);
z(i,1) = (tillNow/100000);
theoriticalY(i,1) = 1 - exp(-0.2*xs(i,1));
end
plot( xs, z, \'rd\');
hold on;
plot( xs, theoriticalY, \'b\');
xlabel( \'x\' );
ylabel( \'cdf of exp(-0.2x)\' );
title( \'Experimental and theoretical CDF of exp R.V.\');
legend( \'Experimental CDF\', \'Theoritical CDF\' );
![Consider and random variable (RV) X that is exponentially distributed with E[X]=5. Using MATLAB, do the following: a. Generate 1,000,000 samples of this random Consider and random variable (RV) X that is exponentially distributed with E[X]=5. Using MATLAB, do the following: a. Generate 1,000,000 samples of this random](/WebImages/13/consider-and-random-variable-rv-x-that-is-exponentially-dist-1013815-1761523602-0.webp)
![Consider and random variable (RV) X that is exponentially distributed with E[X]=5. Using MATLAB, do the following: a. Generate 1,000,000 samples of this random Consider and random variable (RV) X that is exponentially distributed with E[X]=5. Using MATLAB, do the following: a. Generate 1,000,000 samples of this random](/WebImages/13/consider-and-random-variable-rv-x-that-is-exponentially-dist-1013815-1761523602-1.webp)