You are on page 1of 12

2.

2 EXERCISES WITH MATLAB


2.2.1 Standard normal distribution
MATLAB CODE

clc;
close all;
clear all;
load dat1_3;
num=30;
mean_x=mean(x);
sd_x=std(x);
l=length(x);
mean_y=mean(y);
sd_y=std(y);

for i=1:l
x_bar(i)=(x(i)-mean_x)/sd_x;
y_bar(i)=(y(i)-mean_y)/sd_y;
end
[x_density_pract,x_cord_pract]=density(x_bar,num);

[y_density_pract,x_cord_pract_y]=density(y_bar,num);

m=-4;
n=m;
inc=.008;
for i=1:1:1000
x_theo(i)=m+inc;
y_theo(i)=n+inc;
m=x_theo(i);
n=y_theo(i);
end

for i=1:1000
m=m+inc;
x_density_theo(i) = (1/sqrt(2*pi))*exp(-((x_theo(i)^2)/2));
y_density_theo(i) = (1/sqrt(2*pi))*exp(-((y_theo(i)^2)/2));
end

x_cord_theo=-3.992:.008:4;
subplot(2,1,1);

plot(x_cord_theo,x_density_theo,'g',x_cord_pract,x_density_pract,'x','linewidt
h',2)

grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('X')
ylabel ('fx(x)')

1
hold on;
plot(x_cord_pract,x_density_pract,':','linewidth',2);
subplot(2,1,2);

plot(x_cord_theo,y_density_theo,'g',x_cord_pract_y,y_density_pract,
'x','linewidth',2)
grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('y')
ylabel ('fy(y)')
hold on;
plot(x_cord_pract_y,y_density_pract,':','linewidth',2);

OUTPUT
Figure window

2
2.2.2 Exponential Distribution

MATLAB CODE
%Exponential Distribution
clc;
clear all;
close all;
num=30;
load dat1_1;
Xn=pas_value;
Yn=-2*log(sort(Xn));
mean_pract = mean(Yn);
variance_pract = var(Yn);
mean_theoretical=2; %theoretical mean=1/alpha=1/(1/2)
variance_theoretical=4;%theoretical variance=1/alpha^2=1/(1/4)
fprintf('PRACTICAL VALUE OF MEAN = %f \n',mean_pract)
fprintf('PRACTICAL VALUE OF VARIANCE = %f \n',variance_pract)
fprintf('THEORETICAL VALUE OF MEAN = %f \n',mean_theoretical)
fprintf('THEORETICAL VALUE OF VARIANCE = %f \n',variance_theoretical)
[pd_pract,x_cord_pract]=density(Yn,num);

Yn_theo=.001:.001:16;
pd_Yn_theo=.5*exp(-0.5*Yn_theo);

plot(Yn_theo,pd_Yn_theo,'g',x_cord_pract,pd_pract,'x','linewidth',2)

grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('Yn')
ylabel ('fyn(Yn)')
hold on;
plot(x_cord_pract,pd_pract,'--');

OUTPUT

PRACTICAL VALUE OF MEAN = 2.027534


PRACTICAL VALUE OF VARIANCE = 4.451815
THEORETICAL VALUE OF MEAN = 2.000000
THEORETICAL VALUE OF VARIANCE = 4.000000

3
Figure window

4
2.2.3 Sum of random numbers
MATLAB CODE

clc
clear
rand('state',0);
num=20;
dat2_1=rand(1000,2);
pass_value=dat2_1;
z=sum(dat2_1,2);
mean_z=mean(z);
variance_z=var(z);
[z_pract_density,x_cord]=density(z,num);
% x_cord =linspace(min(z), max(z),10);
%theory
n=1;
for i=.001:.001:2
z_theo(n)=i;
if n<=1000
z_theo(n)=z_theo(n);
else
z_theo(n)=2-z_theo(n);
end
x_cord_theo(n)=i;
n=n+1;
end

plot(x_cord_theo,z_theo,'g',x_cord,z_pract_density,'x','linewidth',2)
grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('Yn')
ylabel ('fyn(Yn)')
hold on;
plot(x_cord,z_pract_density,'--');

fprintf('MEAN = %f\n',mean_z);
fprintf('variance = %f\n',variance_z);

OUTPUT
OBSERVED MEAN = 1.001405
OBSERVED VARIANCE = 0.176638

THEORETICAL MEAN=1
THEORETICAL VARIANCE=.1666

5
Figure window

6
2.2.4 Product of random numbers
MATLAB CODE

clc;
clear all;
close all;
num=30;
load dat2_1
x=pass_value;
x1=x(:,1);
x2=x(:,2);
X=x1.*x2;
mean_X=mean(X);
variance_X=var(X);
[pd_pract_X,x_cord_pract]=density(X,num);
% x_cord_pract=linspace(min(X),max(X),10);

n=1;
for i=.001:.001:1
pd_theo_x(n)=-log(i);
n=n+1;
end
[x_cord_theo]=.001:.001:1;

fprintf('OBSERVED MEAN = %f\n',mean_X);


fprintf('OBSERVED VARIANCE = %f\n',variance_X);

plot(x_cord_theo,pd_theo_x,'g',x_cord_pract,pd_pract_X,'x','linewidth',2)

grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('X')
ylabel ('fx(X)')
hold on;
plot(x_cord_pract,pd_pract_X,'--');

OUTPUT
OBSERVED MEAN = 0.254443
OBSERVED VARIANCE = 0.053
THEORETICAL MEAN=0.25
THEORETICAL VARIANCE=.0486

7
Figure window

8
2.2.5 Chi square Distribution

MATLAB CODE
clc
clear
randn('state',0);
x=randn(1000,4);
z=sum(x.^2,2);
mean_pract=mean(z);
variance_pract=var(z);
mean_theoretical=4;
variance_theoretical=8;
num=30;
[z_density_pract,x_cord]=density(z,num);

x_cord_theo = min(x_cord):0.01:(max(x_cord));
pd_theo1= x_cord_theo .* exp(-( x_cord_theo/2));
pd_theo = (1/4) * pd_theo1;

plot(x_cord_theo,pd_theo,'g',x_cord,z_density_pract,'x','linewidth',2)

grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('Z')
ylabel ('fz(Z)')
hold on;
plot(x_cord,z_density_pract,'--');

fprintf('OBSERVED VALUE OF MEAN = %f \n',mean_pract)


fprintf('OBSERVED VALUE OF VARIANCE = %f \n',variance_pract)
fprintf('THEORETICAL VALUE OF MEAN = %f \n',mean_theoretical)
fprintf('THEORETICAL VALUE OF VARIANCE = %f \n',variance_theoretical)

OUTPUT

OBSERVED VALUE OF MEAN = 3.987396


OBSERVED VALUE OF VARIANCE = 7.534794
THEORETICAL VALUE OF MEAN = 4.000000
THEORETICAL VALUE OF VARIANCE = 8.000000

9
Figure window

10
2.2.6 Normal Distribution from Uniform distribution

MATLAB CODE

clc;
clear all;
close all;
load dat2_1;
x=pass_value;
x1=x(:,1);
x2=x(:,2);
y1 = sqrt(-2*log(x1)).*sin(2*pi*x2);

mean_pract_y1=mean(y1);
var_pract_y1=var(y1);
num=30;
[pd_pract_y1,x_cord_pract]=density(y1,num);

x_cord_theo=min(x_cord_pract):.01:max(x_cord_pract);
pd_theo_y1=1/sqrt(2*pi)*exp(-x_cord_theo.^2/2);

plot(x_cord_theo,pd_theo_y1,'g',x_cord_pract,pd_pract_y1,'x','linewidth',2);
grid on
title('PROBABILITY DENSITY FUNCTION')
legend('Theoretical Plot','Practical Plot','tr')
xlabel('Y1')
ylabel ('fy1(Y1)')
hold on;
plot(x_cord_pract,pd_pract_y1,'--');
fprintf('OBSERVED VALUE OF MEAN = %f \n',mean_pract_y1)
fprintf('OBSERVED VALUE OF VARIANCE = %f \n',var_pract_y1)

OUTPUT

OBSERVED VALUE OF MEAN = 0.037115


OBSERVED VALUE OF VARIANCE = 1.017367
THEORETICAL VALUE OF MEAN = 0
THEORETICAL VALUE OF VARIANCE = 1

11
Figure window

12

You might also like