Program 1

advertisement
Program 1
/* specifying some initial constants */
nu_min = 0; /* the allowed range */
nu_med = 1; /*
for nju
*/
nu_max = 21;
y_max = 200;
N = 1000;
N2 = 2000; /* sample size */
/* pre-computed array of scale factors in student density function (the factor depends only on nju) */
nus = seqa(nu_max, (nu_med-nu_max)/N, N) | seqa(nu_med, (nu_min-nu_med)/N, N);
numul = gamma((nus+1)/2) ./ (sqrt(pi*nus).*gamma(nus/2)) | 0;
nus = nus | 0; /* the limit of <nju multiplier> at nju->0 is zero */
/* this procedure returns the value of this multiplier, given nju */
proc get_numul( nu );
local i;
if nu>=nu_max; retp( numul[1] ); endif;
if nu<=nu_min; retp( numul[2*N+1] ); endif;
if nu>=nu_med;
i = floor((nu_max-nu)/(nu_max-nu_med)*N + 1);
else;
i = floor((nu_med-nu)/(nu_med-nu_min)*N + 1 + N);
endif;
if numul[i] eq numul[i+1]; retp(numul[i]); endif;
retp( numul[i] + (numul[i+1] - numul[i]) * (nu-nus[i])/(nus[i+1]-nus[i]) );
endp;
proc numul_lnder(nu);
retp( (get_numul(nu+0.01)-get_numul(nu-0.01))/(0.02*get_numul(nu)) );
endp;
/* returns density of Student's distribution */
/* y - vector of variables, mu,s2,nu - distribution parameters (scalars) */
proc pdfstudent(y, mu,s2,nu);
retp( 1/sqrt(s2) * get_numul(nu) * exp(-(nu+1)/2 * ln(1+1/(nu*s2)*(y-mu).*(y-mu))) );
endp;
/* generates a random sample out of a student's distribution with given parameters */
y = seqa(-y_max, y_max/N2, 2*N2+1);
smpl = zeros(N2,1);
proc(0)= create_sample(mu, s2, nu);
local cdf, x, i, j;
cdf = cumsumc(pdfstudent(y, mu,s2,nu)) * y_max/N2;
print cdf[2*n2+1];
x = sortc(rndu(N2,1),1);
j = 1;
for i(1, N2, 1);
do while cdf[j+1] < x[i]; j=j+1; endo;
smpl[i] = y[j] + (y[j+1] - y[j])*(x[i]-cdf[j])/(cdf[j+1]-cdf[j]);
endfor;
endp;
proc fct(x,y);
retp( -1/2*ln(x[2]) + ln(get_numul(x[3])) - (x[3]+1)/2 .* ln(1 + (y-x[1]).*(y-x[1])./(x[3]*x[2])) );
endp;
proc fct2(x);
retp( -sumc(fct(x,smpl))/N2 );
endp;
proc fct3(x);
local eqns, ymsq, denom;
ymsq = (smpl-x[1]).*(smpl-x[1]);
denom = (ymsq + x[2]*x[3]);
if x[2]<=0 or x[3]<=0; retp( sumc( (smpl-x[1])./denom ) | 100 | 100 ); endif;
eqns = sumc( (smpl-x[1])./denom ) |
sumc( (ymsq - x[2])./denom ) |
sumc( 1/2*(1+x[3])/x[3]*ymsq./denom - 1/2*ln(1/(x[2]*x[3])*denom) + numul_lnder(x[3]) );
retp(eqns);
endp;
/* main code goes here */
library maxlik, optmum, nlsys;
mu = rndn(1,1);
s2 = rndu(1,1)*3 + 2;
nu = rndu(1,1)*3 + 2;
mu0 = rndn(1,1);
s20 = rndu(1,1)*3 + 2;
nu0 = rndu(1,1)*3 + 2;
tetaexact = mu|s2|nu;
tetastart = mu0|s20|nu0;
create_sample(mu,s2,nu);
{tetamaxlik, f,g,cov,retcode} = maxlik(smpl, 1, &fct, tetastart);
{tetaoptmum, f,g,retcode} = optmum(&fct2, tetastart);
{tetaeqsolve, retcode} = eqSolve(&fct3, tetastart);
print(tetastart~tetaexact~tetamaxlik~tetaoptmum~tetaeqsolve);
Program 4-2
library pgraph;
beta_H0 = 0|1;
load Data[234,8] = "FwdSpot3.dat";
Data = ln(Data); // Taking logs of the rates (and logs of months&years also :)
/*-------------------------------------------------------------------------------------*
*
Part 1. GMM estimation of the model
*-------------------------------------------------------------------------------------*/
T = 230;
// vector of dependent variables
Y = Data[5:(T+4),5] - Data[2:(T+1),5]; // exchange rate depreciation
// regressors matrix (note: the actual definition of forward price differs from its description!)
X = ones(T,1) ~ (Data[5:(T+4),8] - Data[2:(T+1),5]);
//instrumental variables
erd = Data[4:(T+3),5] - Data[1:T,5]; // lagged exchange rate depriciation
lfp = Data[4:(T+3),8] - Data[1:T,5]; // lagged forward premium
Z = X ~ lfp ~ erd;
{beta_GMM, V_beta, Wald, Jtest} = my_IV(X, Y, Z, 0);
print "Beta_GMM is:" beta_GMM;
print "Beta variance: " V_beta;
print "Wald stat: " Wald;
print "J-statistic: " Jtest;
/*-------------------------------------------------------------------------------------*
*
Part 2. Bootstrap
*-------------------------------------------------------------------------------------*/
load Data[234,8] = "FwdSpot1.dat";
Data = ln(Data);
// creating main (true) sample
T = 232;
Y = Data[3:(T+2),5] - Data[2:(T+1),5]; // exchange rate depreciation
X = ones(T,1) ~ (Data[3:(T+2),8] - Data[2:(T+1),5]);
erd = Data[2:(T+1),5] - Data[1:T,5]; // lagged exchange rate depriciation
lfp = Data[2:(T+1),8] - Data[1:T,5]; // lagged forward premium
Z = X ~ lfp ~ erd;
// main sample estimation
{beta_GMM, V_beta, Wald, Jtest} = my_IV(X, Y, Z, 0);
Mcenter = meanc(Z.*(Y-X*beta_GMM)); // recentralization parameter in bootstrap
beta_H0 = beta_GMM;
// starting bootstrap:
B = 1000; // number of bootstrap samples
L = 8;
// block length (in overlapping block bootstrap)
Wb = zeros(B,1); // bootstrap Wald stats
Jb = zeros(B,1); // bootstrap J-test stats
for i(1, B, 1);
// overlapping blocks bootstrap: generating sample
indices = trunc(rndu(ceil(T/L), 1)*(T-L+1))+1;
for j(1, L-1, 1);
indices = indices~(indices[.,1]+j);
endfor;
indices = vecr(indices);
indices = indices[1:T];
Xb = X[indices,.];
Yb = Y[indices,.];
Zb = Z[indices,.];
// performing GMM
{beta_b, Vb_beta, Wb[i], Jb[i]} = my_IV(Xb, Yb, Zb, Mcenter);
endfor;
Wb = sortc(Wb, 1);
Jb = sortc(Jb, 1);
print "Bootstrap W 95% quantile = " Wb[round(0.95*B)];
print "Bootstrap J 95% quantile = " Jb[round(0.95*B)];
// Procedure for IV estimation
proc(4)= my_IV(X, Y, Z, A);
local beta_2SLS, beta_GMM, Weff, QQ, Qzx, Vze, V_beta, ze, Wald, Jtest;
// Estimating beta by GMM:
Qzx = 1/T * Z'X;
beta_2SLS = invpd(Qzx'invpd(Z'Z)*Qzx) * Qzx'invpd(Z'Z)*(1/T*Z'Y - A);
ze = Z.*(Y-X*beta_2SLS);
// as we are dealing with time series, asymptotic variance should have a long-run form
Weff = invpd(1/T*ze'ze);
QQ = invpd(Qzx'Weff*Qzx);
beta_GMM = QQ * Qzx'Weff*1/T*Z'Y;
ze = Z.*(Y-X*beta_GMM);
Vze = andrews_variance(ze'ze, 5); // 5 ~= 4*(T/100)^(1/3)
V_beta = QQ * Qzx'*Weff*Vze*Weff*Qzx * QQ; // asymptotic variance, robust form
// in case of bootstrap, beta_H0 = beta_GMM_in_main_sample
Wald = T * (beta_GMM - beta_H0)'invpd(V_beta)*(beta_GMM - beta_H0);
Jtest = T * (meanc(ze) - A)'Weff*(meanc(ze) - A);
retp(beta_GMM, V_beta, Wald, Jtest);
endp;
// Procedures for estimating of Long-run variance
proc parzen_kernel(x);
if abs(x)>=1;
retp(0);
elseif abs(x)>=1/2; retp(2*(1-abs(x))^3);
else;
retp(1-6*x^2+6*abs(x)^3);
endif;
endp;
proc andrews_variance(z, m);
local T, i, j, k, V, O;
T = rows(z); k = cols(z);
V = zeros(k, k);
for j(-m, m, 1);
O = zeros(k, k);
for i(1 + 1/2*(j+abs(j)), T + 1/2*(j-abs(j)), 1);
O = O + z[i,.]'z[i,.];
endfor;
V = V + parzen_kernel(j/(m+1))*1/T*O;
endfor;
retp(V);
endp;
Download