In: Statistics and Probability
I have the following assignment for probability class:
I am supposed to write a routine (code) in MATLAB that does solve the following problem for me:
a) Generate N=10000 samples of a Uniform random variable (X) using the rand () command. This Uniform RV should have a range of -1 to +3.
b) Generate (Estimate) and plot the PDF of X from the samples.
You are not allowed to use the Histogram () command, any commands from the Matlab Statistics toolbox, or any routines from the companion textbook website!
c) Compute the expected value of X (average value) from the samples.
d) Generate (Estimate) and plot the CDF of X from the PDF in part b. e) Derive a new random variable Y using Y=X 2 f) Repeat steps b, c, d for the derived random Y.
Be sure to place appropriate axis labels and titles on your Matlab plots.
Matlab code with comments
Get this plot
get this
get these plot
Now do the above for
get these
the code in text format (tabs might not be retained)
clear;
%part a)
%set the number of samples
N=10000;
%uniform random between [a,b] is generated using
a+(b-a)*rand()
a=-1;
b=3;
x=a+(b-a)*rand(N,1);
%part b) Estimate the pdf
%set the number of classes
breaks=20;
%set the class width
cw=ceil(range(x))/breaks;
%set the lower class interval of the first class as the minimum of
x
lc=floor(min(x));
%get the frequency for each class
for i=1:breaks
freq(i)=length(find(x<lc+cw))-length(find(x<lc));
%store the mid point
mid(i)=lc+cw/2;
%increment the lc to the next class
lc=lc+cw;
end
% get the empirical pdf
epdf=freq/(N*cw);
%plot this
figure(1);
bar(mid,epdf);
title('PDF of X');
xlabel('X');
ylabel('Density');
%part c) Expected value
fprintf('Expected value of X is %.4f\n',mean(x));
%part d) generate the cdf
ecdf=cumsum(freq)/N;
%plot this
figure(2);
plot(mid,ecdf);
title('CDF of X');
xlabel('X');
ylabel('Cumulative Probability');
%repeat for Y=X2
%part a)
y=x.^2;
%part b) Estimate the pdf
%set the number of classes
breaks=20;
%set the class width
cw=ceil(range(y))/breaks;
%set the lower class interval of the first class as the minimum of
x
lc=floor(min(y));
%get the frequency for each class
for i=1:breaks
freq(i)=length(find(y<lc+cw))-length(find(y<lc));
%store the mid point
mid(i)=lc+cw/2;
%increment the lc to the next class
lc=lc+cw;
end
% get the empirical pdf
epdf=freq/(N*cw);
%plot this
figure(3);
bar(mid,epdf);
title('PDF of Y');
xlabel('Y');
ylabel('Density');
%part c) Expected value
fprintf('Expected value of Y is %.4f\n',mean(y));
%part d) generate the cdf
ecdf=cumsum(freq)/N;
%plot this
figure(4);
plot(mid,ecdf);
title('CDF of Y');
xlabel('Y');
ylabel('Cumulative Probability');