You are on page 1of 9

2.

2 EXERCISES WITH MATLAB

2.2.1 Standard Normal Distribution

Load dat1_3 containing the random sequences x and y of two bivariate normal distributed
randomvariables. Transform the random samples such that they obey a standard normal
distribution.Make sure that your transformation is correct by showing the theoretical and the via
density estimated density of the transformed random sequence x in a diagram.

Program

clear all;
close all;
load dat1_3

Meanx = sum(x) / 1000;


Meany = sum(y) / 1000;
X = (x - Meanx)/ cov(x);
[prob,x_cord] = density(X);
z = -4 + 8/1000 : 8/1000 : 4;
Y = (1 / sqrt(2 * pi)) * exp ( (- z .^ 2)/2);
x_cord2 = -4 + 8/1000 : 8/1000 : 4;
plot(x_cord,prob,'--',x_cord2,Y)
grid on
title('STANDARD NORMAL DISTRIBUTION(DENSITY PLOT)')
legend('Practical Density','Theorectical Density','tr')
xlabel('X')
ylabel ('fx(x)')

Density function
function [prob,x_cord] = density(x)
[ten_div x_cord] = hist(x);
interval = x_cord(2) - x_cord(1);
l=length(x);
prob = ten_div * (1 / (interval*l));
end
Output
STANDARD NORMAL DISTRIBUTION(DENSITY PLOT)
0.4
Practical Density
0.35 Theorectical Density

0.3

0.25
f x (x )

0.2

0.15

0.1

0.05

0
-4 -3 -2 -1 0 1 2 3 4
X

2.2.2 Exponential Distribution


Load the on [0, 1] uniformly distributed random sequence from dat1_1 and transform it as
explained in section 2.1.2 with α = 1/ 2 . Calculate the mean value and variance of the
transformedsequence and compare the results with the theoretical values from section 2.1.2.
Show the in section2.1.2 calculated and via density estimated density function of the
transformed random sequence in a figure.

program

clear all;
close all;
load dat1_1;

X=abc;
Y=-2*log(X);
mean_p = mean(Y);
variance_p = var(Y);
mean_theory=2;
variance_theory=4;

[pd_p,k]=density(Y);
Y_theory=.001:.001:16;
pd_Y_theory=0.5*exp(-0.5*Y_theory);
plot(k,pd_p,'-',Y_theory,pd_Y_theory,'--','linewidth',2)
grid on
title('EXPONENTIAL DISTRIBUTION (DENSITY PLOT)')
legend('Practical Density','Theoretical Density','tr')
xlabel('X')
ylabel ('f(x)'

Output

Mean practical= 2.0275


Variance practical= 4.4518
Mean theoretical=2.00
Variance theoretical=4.00

EXPONENTIAL DISTRIBUTION (DENSITY PLOT)


0.5
Practical Density
0.45 Theoretical Density

0.4

0.35

0.3
f(x)

0.25

0.2

0.15

0.1

0.05

0
0 2 4 6 8 10 12 14 16
X
2.2.3 Sum of random numbers

Generate two on [0, 1] uniformly distributed random sequences of the length 1000 after setting
the initial value to 0. Save both random sequences in dat2_1. Then add the two sequences
element-by-element. Calculate the mean value and variance of the sum and compare the results
withthe theoretical values from section 2.1.3. Display the in section 2.1.3 calculated and via
densityestimated density function in a diagram.

program

clc
clear all;
rand('state',0);
dat2_1=rand(1000,2);
def=dat2_1;
x=sum(dat2_1,2);
mean_x=mean(x);
variance_x=var(x);
[x_pra_density,x_cord]=density(x);

%theory
n=1;
for i=.001:.001:2
x_theory(n)=i;
if n<=1000
x_theory(n)=x_theory(n);
else
x_theory(n)=2-x_theory(n);
end
x_cord_theory(n)=i;
n=n+1;
end
plot(x_cord,x_pra_density,'-',x_cord_theory,x_theory,'
.','linewidth',2);
grid on;
title('SUM OF RANDOM VARIABLES (DENSITY PLOT)');
legend('Practical Density','Theoretical Density');
xlabel('x');
ylabel ('fx(x)');
Output

Mean practical= 1.0014


Variance practical= 0.1766
Mean theoretical=1
Variance theoretical=0.166

SUM OF RANDOM VARIABLES (DENSITY PLOT)


1
Practical Density
0.9 Theoretical Density

0.8

0.7

0.6
fx(x)

0.5

0.4

0.3

0.2

0.1

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x

2.2.4 Product of random numbers

Multiply both on [0, 1] uniformly distributed random sequences from dat2_1 element-
byelement.Calculate the mean value and variance of the product and compare the results with the
theoretical values derived in section 2.1.4. Depict the in section 2.1.4 calculated and via
density estimated density function in a figure.

Program

clear all;
clc;
load dat2_1 def;

x=def(:,1);
y=def(:,2);
z=x.*y
mean_z=mean(z);
var_z=cov(z);
[prob,x_cord]=density(z);
n=1;
for i=0:1/1000:1
y_cord2(n)=-log(i);
n=n+1;
end
x_cord2=0:1/1000:1;
plot(x_cord,prob,'--',x_cord2,y_cord2,’line width’,2);

grid on;
title('Product of two random variables');
legend('Practical Density','Theoretical Density','tr');
xlabel('x');
ylabel ('fx(x)');

Output
Mean practical=0.254
Variance practical=0.053
Mean theoretical=0.25
Variance theoretical=0.05
Product of two random variables
7
Practical Density
Theoretical Density
6

4
fx (x )

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
2.2.5 χ2-distribution

Generate four standard normal distributed random sequences of the length 1000, where before
theinitial value has to be set to 0. Save the data in dat2_2. Determine the sum of squares of the
four random sequences element-by-element. Calculate the mean value and variance of the sum
and compare the results with the theoretical values obtained in section 2.1.5. Present the in
section2.1.5 calculated and via density estimated density function in a diagram.

Program

clear all;
close all;
randn('state',0);
% standard normal distributed randon sequences
x1 = randn(1,1000);
x2 = randn(1,1000);
x3 = randn(1,1000);
x4 = randn(1,1000);
save dat2_2 x1 x2 x3 x4
x11 = x1.*x1;
x22 = x2.*x2;
x33 = x3.*x3;
x44 = x4.*x4;

% X = x1^2 + x2^2 + x3^2 + x4^2


x = x11 + x22 + x33 + x44;
Mean_x = mean(x);
Variance_x = var(x);
mean_theory=4;
variance_theory=8;
[x_pra_density,x_cord] = density(x);
x_cord_theory = min(x_cord):0.01:max(x_cord);

% fx(x) = (x^((n/2)-1) * exp(-x/2))/(2^n/2 * ((n/2)-1)!)


pd_theory1 = x_cord_theory.* exp(-(x_cord_theory/2));
pd_theory = (1/4) * pd_theory1;

plot(x_cord_theory,pd_theory,'-',x_cord,x_pra_density,'--','linewidth',2)
grid on
title('CHI-SQUARE DISRIBUTION(DENSITY PLOT)')
legend('Theoretical','Practical','tr')
xlabel('x')
ylabel('fx(x)')
Output

Mean practical=3.9874
Variance practical=7.5348
Mean theoretical=4
Variance theoretical=8

CHI-SQUARE DISRIBUTION(DENSITY PLOT)


0.2
Theoretical
0.18 Practical

0.16

0.14

0.12
fx(x)

0.1

0.08

0.06

0.04

0.02

0
0 2 4 6 8 10 12 14 16 18 20
x

2.2.6 Normal distribution from uniform distribution

Now use again dat2_1 containing two on [0, 1] uniformly distributed random sequences. Build
y1 as in section 2.1.6 explained. Calculate the mean value and variance of y1 and compare the
results with the theoretical values determined in section 2.1.6. Show the in section 2.1.6
calculatedand via density estimated density function in a figure.

Program

clear all;
close all;
load dat2_1 def;
x=def(:,1);
y=def(:,2);
% Generating the sequence Y1
Y1 = sqrt(-2*log(x)).*sin(2*pi*y);
mean_p_Y1 = mean(Y1);
var_p_Y1 = var(Y1);
% Density function
[pd_pra_Y1,x_cord_pra] = density(Y1);
x_cord_theory = min(x_cord_pra) : 0.01 :max(x_cord_pra);
pd_theory_Y1= 1/sqrt(2*pi)*exp(-x_cord_theory.^2/2);

plot(x_cord_theory,pd_theory_Y1,'--',x_cord_pra,pd_pra_Y1,'-','linewidth',2);
grid on
title('NORMAL DISTRIBUTION FROM UNIFORM DISTRIBUTION(DENSITY PLOT)')
legend('TheoreticaL','Practical','tr')
xlabel('x values')
ylabel ('density')

Output

Mean practical=0.0371
Variance practical=1.0174
Mean theoretical=0
Variance theoretical=1
NORMAL DISTRIBUTION FROM UNIFORM DISTRIBUTION(DENSITY PLOT)
0.4
TheoreticaL
0.35 Practical

0.3

0.25
density

0.2

0.15

0.1

0.05

0
-3 -2 -1 0 1 2 3
x values

You might also like