-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jtest_with_two_ivs.m
57 lines (48 loc) · 1.12 KB
/
Jtest_with_two_ivs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
clear
rho = 0.5;
pi = [1;0.1;0.01];
alpha = [1;0];
beta = [1;1];
n = 100;
rep = 1000;
testsize=0.05;
b2sls = zeros(rep,size(beta,1));
b2step = zeros(rep,size(beta,1));
Jstat = zeros(rep,1);
rejection = zeros(rep,1);
for i=1:rep
for K=100:100:rep
if i==K
i
end
end
u=mvnrnd([0;0],[[1,rho];[rho,1]],n);
z=mvnrnd([0;0],[[1,rho];[rho,1]],n);
x=[ones(n,1),z]*pi+u(:,2);
y=[ones(n,1),x]*beta+z*alpha+u(:,1);
Z=[ones(n,1),z];
X=[ones(n,1),x];
Pz=Z/(Z'*Z)*Z';
b=(X'*Pz*X)\X'*Pz*y;
b2sls(i,:)=b';
omega = diag((y-X*b).^2);
S=(Z'*omega*Z/n);
Pw = Z/S*Z';
% Sc=(Z'*omega*Z/n)-(Z'*(y-X*b)/n)*(Z'*(y-X*b)/n)';
% Pw = Z/Sc*Z';
b_egmm = (X'*Pw*X)\X'*Pw*y;
b2step(i,:)=b_egmm';
nJn=n*(Z'*(y-X*b_egmm)/n)'/S*(Z'*(y-X*b_egmm)/n);
% nJn=n*(Z'*(y-X*b_egmm)/n)'/Sc*(Z'*(y-X*b_egmm)/n);
Jstat(i,1)=nJn;
rejection(i,1)=chi2cdf(nJn,1,"upper")<testsize;
end
% mean(b2sls,1)
% std(b2sls)
% mean(b2step,1)
% std(b2step)
% mean(Jstat,1)
% std(Jstat)
chi = chi2rnd(1,rep,1);
mean(rejection,1)
histogram(chi);hold on;histogram(Jstat);hold off;