You are on page 1of 2

1

clear all;
close all;
n = 10000;
Y = zeros(n,1);
X = zeros(n,1);
V = zeros(n,1);
%data generating process
for i=1:n
X(i) = normrnd(0,4);
V(i) = normrnd(0,2);
Y(i) = 4+2*X(i)+normrnd(0,1);
end
X_measure = zeros(n,1);
for i=1:n
X_measure(i) = X(i)+V(i);
end
X_ols = [ones(n,1),X_measure];
%estimate the model using OLS
beta_ols = inv(X_ols'*X_ols)*X_ols'*Y
%find estimated variances of alpha and beta
Error = zeros(n,1);
Error = Y-beta_ols(2)*(X+V)-beta_ols(1);
s_squared = Error'*Error/(n-2);
A = inv(X_ols'*X_ols);
Var_alpha = s_squared*A(1,1)
Var_beta = s_squared*A(2,2)
%test the null hypothesis that alpha = 4
t_stat_alpha = (beta_ols(1)-4)/sqrt(Var_alpha)
%test the null hypothesis that beta = 2
t_stat_beta = (beta_ols(2)-2)/sqrt(Var_beta)
%reestimate the model using assumption that x~N(3,4) instead
%data generating process
n = 10000;
Y = zeros(n,1);
X = zeros(n,1);
V = zeros(n,1);
for i=1:n
X(i) = normrnd(3,4);
V(i) = normrnd(0,2);
Y(i) = 4+2*(X(i)+V(i))+normrnd(0,1);
end
X_measure = zeros(n,1);
for i=1:n
X_measure(i) = X(i)+V(i);
end
2
X_ols = [ones(n,1),X_measure];
%estimate the model using OLS
beta_ols_new = inv(X_ols'*X_ols)*X_ols'*Y
beta_ols =
4.0370
1.5991
Var_alpha =
0.0014
Var_beta =
6.6942e-05
t_stat_alpha =
0.9968
t_stat_beta =
-48.9935
beta_ols_new =
4.0044
1.9967
Published with MATLAB R2014a

You might also like