-
Notifications
You must be signed in to change notification settings - Fork 6
/
panel_thresh_endogeneous.adj.asv
432 lines (389 loc) · 10.3 KB
/
panel_thresh_endogeneous.adj.asv
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
%%%%%%%%%IVTAR_Panel.M%%%%%%%%%
%%%%%%%%%%%
% Panel Threshold Estimation with Endogeneous Variable %
%%%%%%%%%%%
% Stephanie Kremer, Alex Bick, Dieter Nautz
% Based on a MATLAB proccedure IVTAR.M written by Bruce E. Hansen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Data %
%function ivtarpanel;
% load Develop_transformed_data.txt;
% z=Develop_transformed_data;
% load Develop_instrumentsall.txt;
% p=Develop_instrumentsall;
load Develop_transformed_data.txt;
z=Develop_transformed_data;
load Develop_instrumentsall.txt;
p=Develop_instrumentsall;
grgdp = z(:,1); % average_growthrate_gdp
inflat = z(:,2); % average_inflationrate
population = z(:,3); % average_growthrate_pop
invest = z(:,4); % average_investmentshare
tot = z(:,5); % average_terms of trate rate
sdtot = z(:,6); % standarddeviation_terms of trate
openess = z(:,7); % average_log_openess
sdopen = z(:,8); % log_standarddeviation_openess
initial = z(:,9);
country = z(:,10);
KK = z(:,11);
t = z(:,12);
largeT = z(:,13);
instr1 = p(:,1); % instrumentsa
instr2 = p(:,2);
instr3 = p(:,3);
instr4 = p(:,4);
instr5 = p(:,5);
instr6 = p(:,6);
instr7 = p(:,7);
y=grgdp;
q=inflat;
c=inflat; %exogeneous variable; regime-dependent
z1=initial; %endogeneous; regime-independent
z2=[population, invest, tot, sdtot, openess, sdopen]; %exogeneous regime-independent
x=[instr1 instr2 instr3 instr4 instr5 instr6 instr7]
%%%%%%Fixed Effects Transformation***********
yt = tr(y,largeT,t);
ct = tr(c,largeT,t);
zt1= tr(z1,largeT,t);
k=length(z2(1,:));
zt2=zeros(length(yt(:,1)),k);
i=1;
while i<=k
zt2(:,i)=tr(z2(:,i),largeT,t);
i=i+1;
end;
ii=1;
for i=1:length(q(:,1))
if t(i)<largeT(i)
qt(ii)=q(i);
ii=ii+1;
end;
end;
qt=qt';
%%%%%%%%Define Parameters****************
conf_=0.90;
conf1_=0.80;
conf2_=1;
reduced=0;
%%%%%%%%2SLS*************************
xx=[x,zt2];
z1hat=xx*regress(zt1,xx);
zhat=[z1hat,zt2];
%%%%%%%%%%%%%%%%%%
n=length(yt(:,1));
xx=[zhat,tr(c,largeT,t)];
k=length(xx(1,:));
e=yt-xx*regress(yt,xx);
s0=det(e'*e);
n1=round(.05*n)+k;
n2=round(.95*n)-k;
qs=sortrows(q,1);
qs=qs(n1:n2);
qs=unique(qs);
qn=length(qs(:,1));
sn=zeros(qn,1);
r=1;
while r<=qn
d=(q<=qs(r));
xxx=[xx,tr(c.*d,largeT,t),tr(d,largeT,t)]; %regime-specific constant inserted here%
xxx=xxx-xx*regress(xxx,xx);
ex=e-xxx*regress(e,xxx);
sn(r)=det(ex'*ex);
r=r+1;
end;
[temp,r]=min(sn);
smin=sn(r);
qhat=qs(r);
d=(q<=qhat);
xxx=[zhat,tr(c.*d,largeT,t),tr(d,largeT,t)]; %regime-specific constant inserted here%
dd=1-d;
xxx=[xxx,tr(c.*dd,largeT,t)];
beta=regress(yt,xxx);
yhat=xxx*beta;
e=yt-yhat;
lr=n*(sn/smin-1);
sig2=smin/n;
i=length(zhat(1,:));
beta1=beta(i+1);
beta2=beta(i+3); %add 3 because of the regime-specific constant%
if length(yt(1,:))>1
eta1=1;
eta2=1;
else
r1=(ct*(beta1-beta2)).^2;
r2=r1.*(e.^2);
qx=[qt.^0,qt.^1,qt.^2];
qh=[qhat.^0,qhat.^1,qhat.^2];
m1=(r1'/qx')';
m2=(r2'/qx')';
g1=qh*m1;
g2=qh*m2;
eta1=((g2'/g1')/sig2')';
sigq=sqrt(mean((qt-mean(qt)').^2)');
hband=2.344*sigq/(n^(.2));
u=(qhat-qt)/hband;
u2=u.^2;
f=mean((1-u2).*(u2<=1))'*(.75/hband);
df=-mean(-u.*(u2<=1))'*(1.5/(hband^2));
eps=r1-qx*m1;
sige=(eps'*eps)/(n-3);
hband=sige/(4*f*((m1(3)+(m1(2)+2*m1(3)*qhat)*df/f)^2));
u2=((qhat-qt)/hband).^2;
kh=((1-u2)*.75/hband).*(u2<=1);
g1=mean(kh.*r1)';
g2=mean(kh.*r2)';
eta2=((g2'/g1')/sig2')';
end;
c1=-2*log(1-sqrt(conf_));
lr0=(lr>=c1);
lr1=(lr>=(c1*eta1));
lr2=(lr>=(c1*eta2));
if max(lr0)==1
[temp,indtemp1]=min(lr0);
revlr0=lr0(1);
for ii=2:length(lr0)
revlr0=[lr0(ii);revlr0];
end;
[temp,indtemp2]=min(revlr0);
disp(' ')
qcf0=[qs(indtemp1),qs(qn+1-indtemp2)];
else
qcf0=[qs(1),qs(qn)];
end;
if max(lr1)==1
[temp,indtemp1]=min(lr0);
revlr1=lr1(1);
for ii=2:length(lr1)
revlr1=[lr1(ii);revlr1];
end;
[temp,indtemp2]=min(revlr1);
qcf1=[qs(indtemp1),qs(qn+1-(indtemp2))];
else
qcf1=[qs(1),qs(qn)];
end;
if max(lr2)==1
[temp,indtemp1]=min(lr2);
revlr2=lr2(1);
for ii=2:length(lr2)
revlr2=[lr2(ii);revlr2];
end;
[temp,indtemp2]=min(revlr2);
qcf2=[qs(indtemp1),qs(qn+1-indtemp2)];
else
qcf2=[qs(1),qs(qn)];
end;
figure;
%clr = ones(qn,1)*c1;
plot(lr);
%plot(qs,lr,qs,clr,qs,clr*eta1,qs,clr*eta2);
grid on;
title('Confidence Interval Construction for Threshold');
xlabel('Threshold Variable Develop');
ylabel('Likelihood Ratio Sequence in Gamma');
legend('LR_n(Gamma)','90% Critical','Hetero Corrected-1','Hetero Corrected-2');
%%%%%%%%%%%%
z=[zt1,zt2];
da=(q<=qhat);
db=1-da;
zi=[zt1,zt2,tr(c.*da,largeT,t),tr(da,largeT,t),tr(c.*db,largeT,t)]; %regime-specific constant inserted here%
xi=[x,zt2,tr(c.*da,largeT,t),tr(da,largeT,t),tr(c.*db,largeT,t)]; %regime-specific constant inserted here%
yi=yt;
[beta,se,jstat] = gmm_linear(yi,zi,xi);
betal=beta-se*1.96;
betau=beta+se*1.96;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=length(yt(:,1));
xx=[zhat,tr(c,largeT,t)];
k=length(xx(1,:));
e=yt-xx*regress(yt,xx);
s0=det(e'*e);
n1=round(.05*n)+k;
n2=round(.95*n)-k;
qs=sortrows(q,1);
qs=qs(n1:n2);
qs=unique(qs);
qn=length(qs(:,1));
sn=zeros(qn,1);
r=1;
while r<=qn
d=(q<=qs(r));
xxx=[xx,tr(c.*d,largeT,t),tr(d,largeT,t)]; %regime-specific constant inserted here%
xxx=xxx-xx*regress(xxx,xx);
ex=e-xxx*regress(e,xxx);
sn(r)=det(ex'*ex);
r=r+1;
end;
[temp,r]=min(sn);
smin=sn(r);
qhat=qs(r);
d=(q<=qhat);
xxx=[zhat,tr(c.*d,largeT,t),tr(d,largeT,t)]; %regime-specific constant inserted here%
dd=1-d;
xxx=[xxx,tr(c.*dd,largeT,t)];
betaf=regress(yt,xxx);
yhat=xxx*betaf;
e=yt-yhat;
lr=n*(sn/smin-1);
sig2=smin/n;
i=length(x(1,:));
betaf1=betaf(i+1);
betaf2=beta(i+3);
if length(yt(1,:))>1
eta1=1;
eta2=1;
else
r1=(ct*(betaf1-betaf2)).^2;
r2=r1.*(e.^2);
qx=[qt.^0,qt.^1,qt.^2];
qh=[qhat.^0,qhat.^1,qhat.^2];
m1=(r1'/qx')';
m2=(r2'/qx')';
g1=qh*m1;
g2=qh*m2;
eta1=((g2'/g1')/sig2')';
sigq=sqrt(mean((qt-mean(qt)').^2)');
hband=2.344*sigq/(n^(.2));
u=(qhat-qt)/hband;
u2=u.^2;
f=mean((1-u2).*(u2<=1))'*(.75/hband);
df=-mean(-u.*(u2<=1))'*(1.5/(hband^2));
eps=r1-qx*m1;
sige=(eps'*eps)/(n-3);
hband=sige/(4*f*((m1(3)+(m1(2)+2*m1(3)*qhat)*df/f)^2));
u2=((qhat-qt)/hband).^2;
kh=((1-u2)*.75/hband).*(u2<=1);
g1=mean(kh.*r1)';
g2=mean(kh.*r2)';
eta2=((g2'/g1')/sig2')';
end;
c1=-2*log(1-sqrt(conf1_));
lr0=(lr>=c1);
lr1=(lr>=(c1*eta1));
lr2=(lr>=(c1*eta2));
if max(lr0)==1
[temp,indtemp1]=min(lr0);
revlr0=lr0(1);
for ii=2:length(lr0)
revlr0=[lr0(ii);revlr0];
end;
[temp,indtemp2]=min(revlr0);
qcf0i=[qs(indtemp1),qs(qn+1-indtemp2)];
else
qcf0i=[qs(1),qs(qn)];
end;
if max(lr1)==1
[temp,indtemp1]=min(lr0);
revlr1=lr1(1);
for ii=2:length(lr1)
revlr1=[lr1(ii);revlr1];
end;
[temp,indtemp2]=min(revlr1);
qcf1i=[qs(indtemp1),qs(qn+1-(indtemp2))];
else
qcf1i=[qs(1),qs(qn)];
end;
if max(lr2)==1
[temp,indtemp1]=min(lr2);
revlr2=lr2(1);
for ii=2:length(lr2)
revlr2=[lr2(ii);revlr2];
end;
[temp,indtemp2]=min(revlr2);
qcf2i=[qs(indtemp1),qs(qn+1-indtemp2)];
else
qcf2i=[qs(1),qs(qn)];
end;
%%%%%%%%%%%%%%%%%%
if conf2_==0
qcf=qcf0i;
elseif conf2_==1
qcf=qcf1i;
elseif conf2_==2
qcf=qcf2i;
end;
qq=unique(q);
qqcf2=(qq<=qcf(2));
ind=0;
for i=1:length(qqcf2)
if qqcf2(i)==1
if ind==0
temp=qq(i);
ind=1;
else
temp=[temp;qq(i)];
end;
end;
end;
qq=temp;
qqcf1=(qq>=qcf(1));
ind=0;
for i=1:length(qqcf1)
if qqcf1(i)==1
if ind==0
temp=qq(i);
ind=1;
else
temp=[temp;qq(i)];
end;
end;
end;
qq=temp;
clear qqcf1;
clear qqcf2;
ind=0;
i=1;
while i<=length(qq(:,1));
qi=qq(i);
dai=(q<=qi);
dbi=1-dai;
yi=yt;
zi=[zt1,zt2,tr(c.*dai,largeT,t),tr(dai,largeT,t),tr(c.*dbi,largeT,t)]; %regime-specific constant inserted here%
xi=[x,zt2,tr(c.*dai,largeT,t),tr(dai,largeT,t),tr(c.*dbi,largeT,t)]; %regime-specific constant inserted here%
[betafi,sei,jstati] = gmm_linear(yi,zi,xi);
betafil=min([(betafi-sei*1.96),betal]')';
betafiu=max([(betafi+sei*1.96),betau]')';
%GMM/IV; Use alternatively:
%[betai,sei,jstati] = gmm_linear(yi,zi,xi);
%betail=min([(betai-sei*1.96),betal]')';
%betaiu=max([(betai+sei*1.96),betau]')';
%And insert the alternative in Output
i=i+1;
end;
z=length(zhat(1,:));
% eqhat=exp(qhat);
% eqcf0=exp(qcf0)
% eqcf1=exp(qcf1)
% eqcf2=exp(qcf2)
disp(' ');
disp(' ');
fprintf('Threshold Estimate (in levels): %f\n',exp(qhat));
fprintf('Confidence Interval - Uncorrected: %f %f\n',exp(qcf0(1)),exp(qcf0(2)));
fprintf('Confidence Interval - Het Corrected Quad: %f %f\n',exp(qcf1(1)),exp(qcf1(2)));
fprintf('Confidence Interval - Het Corrected NP: %f %f\n',exp(qcf2(1)),exp(qcf2(2)));
disp(' ');
disp(' ');
fprintf('Regime-independent regressors: %f\n');
disp(' ');
disp('Estimates S.E. Lower Upper');
for i=1:z
fprintf('%f %f %f %f\n',betaf(i),se(i),betafil(i),betafiu(i));
end;
disp(' ');
fprintf('Regime-dependent regressors: %f\n');
disp(' ');
fprintf('Regime 1 : Threshold variable less than %f\n',exp(qhat));
fprintf('Number of observations: %f\n',sum(da));
disp(' ');
disp('Estimates S.E. Lower Upper');
for i=z+1:z+2
fprintf('%f %f %f %f\n',betaf(i),se(i),betafil(i),betafiu(i));
end;
disp(' ');
fprintf('Regime 2 : Threshold variable greater than %f\n',exp(qhat));
fprintf('Number of observations: %f\n',sum(db));
disp(' ');
disp('Estimates S.E. Lower Upper');
for i=z+3:z+3
fprintf('%f %f %f %f\n',betaf(i),se(i),betafil(i),betafiu(i));
end;
disp(' ');