/***************************************************************************/ /* */ /* Title: DOITL.G */ /* includes doitl (procedure), calloptl (sub-procedure) */ /* */ /* modified from doitc to do levels for lars/nber project */ /* Written: John H. Cochrane */ /* by: University of Chicago Graduate School of Business */ /* 1101 E. 58th Street, Rosenwald 2xx */ /* Chicago, Ill 60637 */ /* ph#: (312) 702-3059 */ /* e-mail: fac_jhc@gsbacd.uchicago.edu */ /* */ /*Modified: Caleb C. Wong */ /* by: University of Chicago Department of Economics */ /* 1126 East 59th St. */ /* Chicago, Ill 60637 */ /* ph#: (312) 702-8254 */ /* e-mail: wongcc@sam.spc.uchicago.edu */ /* */ /* Purpose: DOITC sets up the data, factors, and weighting matrix to */ /* do the iterated search of the technology &/or consumption */ /* parameters, plus estimation of additional factors. CALLOPTC */ /* is the procedure, called by DOITC, to do the actual iteration. */ /* CALLOPTC uses GAUSS application program OPTMUM to search for */ /* optimal parameter values. */ /* */ /* */ /* */ /***************************************************************************/ /* ------------------------------------ */ /* Part I: (1) Call GAUSS library files */ /* (2) Reset graphic globals */ /* ------------------------------------ */ library pgraph,optmum,gmm; graphset; #include optmum.ext; /* -----------------------------------*/ /* Part II: Procedures */ /* -----------------------------------*/ /* ------------------------------------------------------------------------- */ /* CALLOPTC.G */ /* */ /* USAGE: */ /* {alpha,delta,mpk,jt,covs,b,f,allr,ik,reti} = */ /* callopt(alpha,delta,mpk,selpar,invest,rassetx,xtrfact,inst,rf,W,howto,b);*/ /* */ /* INPUTS: (see description under doitc proc) */ /*OUTPUTS: (see description under doitc proc) */ /* */ /* -------------------------------------------------------------------------- */ proc(13) = calloptl(alpha,delta,mpk,selpar,invest,rassetx,inst, W,howto,b); local ik,tpar,jt,gradnt,retcode,reti,m,f,lgt,b,allr,qmat; ik = makeik(delta,invest); tpar = encodel(alpha,delta,mpk,selpar); call passtol(invest,rassetx,inst, ik,W,selpar,howto,alpha,delta,mpk,b); output off; {tpar,jt,gradnt,retcode} = optmum(&findjtl,tpar); output on; if retcode /= 0 ; "Optmum terminated abnomally. retcode=" retcode; endif; {reti,ik,m,f,lgt,b,allr,qmat} = passbakl; {alpha,delta,mpk} = decodel(tpar,selpar,alpha,delta,mpk); retp(alpha,delta,mpk,jt,b,f,allr,ik,reti,tpar,lgt,qmat,m); endp; /* ------------------------------------------------------------------------- */ /* DOITC.G */ /* */ /* USAGE: */ /* {alpha1,delta1,mpk1,gam1,b1} = */ /* doit(alpha,delta,mpk,selpar,howto,invest,rassetx,xtrfact,inst, */ /* rf,nwlags,step2,ergrf,invlbl,xtrlbl,errbars); */ /* INPUTS: */ /* alpha,delta,mpk: default parameter vectors, rows() = cols(invest). */ /* selpar: 3x1 vector, which of {alpha delta mpk} to search over */ /* howto: 3x1 vector controls levels vs. excess returns as factors*/ /* whether extra factors are included and cond/uc moments*/ /* invest: matrix of investment series 47:1 to 90:4 */ /* rassetx: matrix of excess asset returns 47:2 to 90:4 */ /* xtrfact: matrix of extra factors to try 47:2 */ /* inst: matrix of instruments, 0 if just constant, 47:1 to 90:4*/ /* (program lags twice and chops returns to fit) */ /* rf: risk free rate series to form excess inv. ret. 47:2 */ /* nwlags: kx1 vector of newey west lags to use */ /* step2: 1: do second stage gmm, 0: don't */ /* ergrf: 1: make graphs Er vs. pred. 1st stage|2nd|see? */ /* invlbl: label conformable to cols(invest) */ /* xtrlbl: label conformable to cols(xtrafact) */ /* errbars 0: no std error bars; 1: insert std error bars */ /* xtrcode factor code to indicate whether to use as RHS variable */ /* in OLS regression */ /* */ /* OUTPUTS: */ /* {alpha1,delta1,mpk1,b1,gam1} = first stage gmm; use for successsive calls*/ /* */ /* Notes: */ /* investment returns are automatically added to asset returns for testing.if*/ /* an extra factor is also a return, you must manually add it to rassetx */ /* */ /* */ /* */ /* ------------------------------------------------------------------------- */ proc(5) = doitl(alpha,delta,mpk,selpar,howto,invest,rassetx,inst, nwlags,step2,invlbl); clearg ik,tpar,jt,gradnt,retcode,reti,m,f,lgt,b,label,W, D0,qmat,varmat,se,trash,T,sig,sigin,doff,pval, alpha1,delta1,mpk1,b1,ninv,npar,nb,parvec,jtur,chi2,blank, table,mask,fmt,allr,dev, select,ninst,dofobs, stderr,i,j1,junk,ej,pj,pval2,lgt, deltas,iteridx,rhow,lastw,oldchi,bT,allrT,tpT,trash; /* --------------------------------------------------------------- */ /* 1. Generate output statements, given values passed by switches */ /* --------------------------------------------------------------- */ ninv = cols(invest); /* these need to be straightened out; */ nb = ninv; if rows(invlbl) /= cols(invest); "Fix investment label"; endif; if howto[2] .== 1 ; "Using unscaled factors"; endif; if howto[2] .== 2; "using scaled factors"; endif; if howto[3] == 1; "Using unconditional momemts"; endif; if howto[3] == 2; "Using scaled returns"; endif; _opgdprc = 0; /* put in analytical gradients at some point */ /* --------------------------------------------------- */ /* 4. do 1st-stage GMM with identity weighting matrix */ /* --------------------------------------------------- */ w = 0; /* findjt makes it an identity matrix */ b = 0; /* only needs b when doing grads */ {alpha,delta,mpk,jt,b,f,allr,ik,reti,tpar,lgt,qmat,m} = calloptl(alpha,delta,mpk, selpar,invest,rassetx,inst,W,howto,b); alpha1 = alpha; /* save first stage parameters */ delta1 = delta; mpk1 = mpk; b1 = b; /* ----------------------------------------------- */ /* 5. Print first stage parameter estimates, se's;*/ /* call finds0.g to construct weighting matrix */ /* ----------------------------------------------- */ ""; "2. First stage GMM (Identity weighting matrix). "; gosub outpar; w = inv(findS0(f,nwlags)); gosub calcse; gosub outse; gosub chisq; /* Hansen-Jaganathan Specification Error Test */ "";"Hansen-Jaganathan Spec. Errors -- 1st Stage"; (lgt'inv((1/rows(allr))*allr'allr)*lgt)^.5; /* ------------------------------------------------------------------ */ /* 6. Second-stage GMM estimation using the returned weighting matrix */ /* ------------------------------------------------------------------ */ if step2 == 2; ""; "3. Iterating GMM (optimal weighting matrix) "; lastw = eye(cols(f)); /* use w? */ rhow = .5; /* w is updated by w = rho* new w + (1-rho) last w */ iteridx = 1; do while iteridx <= 100; w = rhow*inv(findS0(f,nwlags)) + (1-rhow)*lastw; lastw = w; {alpha,delta,mpk,jt,b,f,allr,ik,reti,tpar,lgt,qmat,m} = calloptl(alpha,delta,mpk,selpar, invest,rassetx,inst,W,howto,b); format /RD 10,4; "Iterating GMM for " iteridx "th time; JT = " jt "%ch" 100*(jt-oldchi[1])/jt; if iteridx == 1; oldchi = jt; else; oldchi = jt|oldchi; endif; if iteridx > 5; if (maxc(abs(oldchi[1:4]-oldchi[2:5])./jt) < 0.05) ; break; endif; endif; iteridx = iteridx + 1; if iteridx == 200; "iterated GMM did not converge"; endif; endo; endif; if step2 == 1; ""; "3. Second stage gmm "; ""; format /RD 4,0; "Using " nwlags "newey-west lags"; ""; w =invpd(findS0(f,nwlags)); {alpha,delta,mpk,jt,b,f,allr,ik,reti,tpar,lgt,qmat,m} = calloptl(alpha,delta,mpk,selpar, invest,rassetx,inst,W,howto,b); endif; gosub outpar; gosub calcse; gosub outse; gosub chisq; gosub overid; retp(alpha,delta,mpk,b,m); /* ---------------------------------- */ /* Part III: Subroutines */ /* ---------------------------------- */ /* --------------------------------------------------------------------- */ /* B. OUTPAR: Generate and format the output of the estimated parameters */ /* --------------------------------------------------------------------- */ outpar: "Parameter estimates" ; parvec = selif(alpha|delta|mpk,selpar.*.ones(ninv,1)); format /RD 10,3; ((100*parvec)|b)'; /* mpk multiplied by 100 */ return; /* ---------------------------------------------------------- */ /* C. OUTSE: Setup the tables for the se's & t-values */ /* ---------------------------------------------------------- */ outse: "s.e." ((100*se[1:cols(invest)])|se[cols(invest)+1:rows(se)])'; " t " ((parvec|b)./se)'; return; /*----------------------------------------------------------- */ /* D. CHISQ: Chi-Square tests of the b's */ /*----------------------------------------------------------- */ chisq: " "; "Chi-squared tests on coefficients"; label = blank | label; format /RD 10,2; $label'; let label = "chi^2"; $label chi2'; format /RD 10,0; let label = "DF"; $label doff'; format /RD 10,3; let label = "% p val" ; $label pval'; return; /* ---------------------------------------------------------- */ /* D. CALCSE: Construct the Chi-Square tests for joint b=0 */ /* for all selected sets of the factors */ /* ---------------------------------------------------------- */ calcse: /* ---------------------------------------- */ /* 1. calculate standard errors a la Hansen */ /* ---------------------------------------- */ tpar = encodel(alpha,delta,mpk,selpar); call passtol(invest,rassetx,inst, ik,W,selpar,2|howto[2:3],alpha,delta,mpk,b); /* note howto[1]=2: hold b fixed */ D0 = gradp(&findgtl,tpar); call passbakl; D0 = D0~Qmat; /* add deriv wrt b */ T = rows(f); varmat = 1/T*inv(D0'W*D0); /* note vcov of transformed par's */ se = diag(varmat)^(1/2); /* ----------------------------------------------- */ /* 2. pvalues for tests on joint parameter vector */ /* ----------------------------------------------- */ /* ---- joint all production coeffs ---- */ npar = rows(parvec) ; let label = "coeffs" ; doff = npar; trash = varmat[1:npar,1:npar]; if not (abs(det(trash)) < 1E-30) ; chi2 = parvec'inv(varmat[1:npar,1:npar])*parvec; pval = 100*cdfchic(chi2,doff); else; "variance matrix singular"; chi2 = -99; pval = -99; endif; /* ---- joint all b ---- */ nb = rows(b); label = label | "all b"; doff = doff|nb; trash = varmat[npar+1:npar+nb,npar+1:npar+nb]; if not (abs(det(trash)) < 1E-30); chi2 = chi2 | b'inv(trash)*b; pval = pval | 100*cdfchic(chi2[rows(chi2)],doff[rows(doff)]); else; "variance matrix singular"; chi2 = chi2 | -99; pval = pval | -99; endif; ""; return; /* ---------------------------------------------------------- */ /* D. OVERID: Construct the overidentifying restrictions test */ /* ---------------------------------------------------------- */ overid: /* ------------------------------- */ /* 1. overidentifying restrictions */ /* ------------------------------- */ ""; label = "Unrestr"; doff = cols(allr)-rows(tpar)-rows(b); if doff > 0; chi2 = jt; pval2 = 100*cdfchic(chi2,doff); else; doff "degrees of freedom, no overidentifying restrictions tests"; goto endovid; endif; "Chi squared tests on JT for overidentifying restrictions" ; format /RD 10,4; label = blank | label; $label'; format /RD 10,2; " JT" chi2'; format /RD 10,0; " DF" doff'; format /RD 10,3; " % p value" pval2'; endovid: return; endp;