You are on page 1of 5

Exp No.

3
EEE 4554
Exp Name: Estimating population mean and standard deviation,
Markov Chain, Covariance and Correlation
% Standard t-test for confidence of means & Chi-Square Test for the Standard Deviation
clear all;
close all;
no_samples_x = 1e2;
no_samples_y = 1e2;
no_experiment=1e4;
for i=1:no_experiment,
x = randn(1, no_samples_x);
y = randn(1, no_samples_y);
mean_x(i) = mean(x);
var_x(i) = var(x);
mean_y(i) = mean(y);
var_y(i) = var(y);
end;
figure(1);
hist((mean_x - 0) * sqrt(no_samples_x) ./ sqrt(var_x), 100);
% %test of confidence interval
% ci = x_bar +- t(s / sqrt(n))
t_values = -10:0.1:10;
t_pdf = tpdf(t_values, no_samples_x - 1);
figure(2);
plot(t_values, t_pdf);
%% 95% confidence interval..
t_value = abs(tinv(0.025, no_samples_x -1)); % t value for 95% confidence interval
count = 0;
for i=1:no_experiment,
ci_high = mean_x(i) + t_value .* sqrt(var_x(i)) ./ sqrt(no_samples_x);
ci_low = mean_x(i) - t_value .* sqrt(var_x(i)) ./ sqrt(no_samples_x);
if ci_high >= 0 & ci_low <= 0,
count = count + 1;
end;
end;
count ./ no_experiment %Percentage that mean = 0 falls between confidence interval for 1e3
experiments

Prepared by M.A. Naser, Ridwan Asst. Prof, EEE, IUT

%% Chi-square test
sigma_0 = 1.0;
chi_values = 0:0.2:200;
chi_pdf = chi2pdf(chi_values, no_samples_y - 1);
figure(3);
plot(chi_values, chi_pdf);
% %95% confidence
ls_chi_value = abs(chi2inv(0.05, no_samples_y -1)); % Lower Side Chi square value for 95%
confidence level
us_chi_value = abs(chi2inv(0.95, no_samples_y -1)); % Upper Side Chi square value for 95%
confidence level
% Both Side Chi square value for 95% confidence level
ds_chi_value_ls= abs(chi2inv(0.025, no_samples_y -1));
ds_chi_value_us= abs(chi2inv(0.975, no_samples_y -1));
u_count = 0;
l_count = 0;
ds_count = 0;
for i=1:no_experiment,
T_value = (no_samples_y - 1) .* (sqrt(var_y(i)) / (sigma_0)).^2;
if T_value <= us_chi_value,
u_count = u_count + 1;
end;
if T_value >= ls_chi_value,
l_count = l_count + 1;
end;
if T_value >= ds_chi_value_ls & T_value <= ds_chi_value_us
ds_count = ds_count + 1;
end
end;
u_count ./ no_experiment % Percetage of upper side test for 1e3 experiments
l_count ./ no_experiment % Percetage of lower side test for 1e3 experiments
ds_count ./ no_experiment % Percetage of double side test for 1e3 experiments

Prepared by M.A. Naser, Ridwan Asst. Prof, EEE, IUT

Markov chain
% Sequence Generation with defined transition probability
clear all;
close all;
state_value = [ 0 1 ];
tran_prob = [ .6 .4;.4 .6 ];
tran_prob_cdf = [ .6 1;.4 1 ];
x(1) = 1;
for i = 2:1e2
current_state = find(x(i-1) == state_value);
current_transition_prob = tran_prob_cdf(current_state , :);
next_state = min(find(current_transition_prob > rand));
x(i) = state_value(next_state);
end
figure;
stem(x);
title('Markov chain sequence');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%
% Entropy of 1st order, two state markov chain
clear all;
close all;
% H_overall = -0.5*(0.8*log2(0.8) + 0.2*log2(0.2)) - 0.5*(0.8*log2(0.8) +
0.2*log2(0.2))
% H(1) = -0.5*log2(0.5) - 0.5*log2(0.5);
% P = [ 0.5 0.5 ];
% tran_prob = [ 0.8 0.2 0.2 0.8 ];
H_overall = -0.8*(0.9*log2(0.9) + 0.1*log2(0.1)) - 0.2*(0.9*log2(0.9) +
0.1*log2(0.1))
H(1) = -0.8*log2(0.8) - 0.2*log2(0.2);
P = [ 0.8 0.2 ];
tran_prob = [ 0.9 0.1 0.9 0.1 ];
for i = 2 : 16
rep_prob = repmat(P', [1 2]);
reshape_prob = reshape(rep_prob', 1, 2^i);
rep_tran_prob = repmat(tran_prob, 1, 2^(i-2));
mult_prob = reshape_prob .* rep_tran_prob;
P = mult_prob;
H(i) = sum(-P .* log2(P))/i;
end
figure(1);
plot(H);
Prepared by M.A. Naser, Ridwan Asst. Prof, EEE, IUT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%
% Information for defined sequence
close all;
clear all;
Priori = [0.8 0.2];
% Initial occurance for P('0') = 0.8 & P('1') = 0.2
Posteriori = [0.9 0.1; 0.9 0.1];
dataStream = (randn(1, 1e2) > 0);
dataStream(1) = 0;
% Define first bit is '0'
P(1) = Priori(1);
I(1) = - log2(P(1));
% Information of symbol which is single bit long
for i = 2:length(dataStream),
P(i) = P(i-1) * Posteriori(dataStream(i-1) + 1, dataStream(i) + 1);
I(i) = -log2(P(i));
end;
figure(1);clf;
stem(I);
title('Information of the signal with progressive data');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H_overall =
0.4690

Prepared by M.A. Naser, Ridwan Asst. Prof, EEE, IUT

Correlation and Covariance


of a Random Signal
x = [3 1 6 3 4];
y = [1 5 3 4 3];
mx = mean(x)
my = mean(y)
mxy = mean(x.*y)
% Standard Dev. from built-in Matlab Functions
std(x,1)
std(y,1)
% Standard Dev. from Equation Above (same result as std(?,1))
sqrt( 1/5 * sum((x-mx)._2))
sqrt( 1/5 * sum((y-my)._2))
cov(x,y,1)
corrcoef(x,y)

Prepared by M.A. Naser, Ridwan Asst. Prof, EEE, IUT

You might also like