
% .mod file for estimating nistic̣'s model (stock prices in a new-keynesian set up) 

var xhat, pihat, rhat, shat, epsx, epspi, epss endo; % endogenous variables and AR(1) processes
varexo ex, epi, er, es; % shocks 

parameters psi, beta, k, lambda, phipi, phix, phir, rhox, rhopi, rhos, mux; % all but variances (come later)

// initial guess for the parameter vector
 
//structural model
psi = 0.05; 
beta = .99; 
lambda = 0.1;
k = .65;
//trule
phipi = 1.5;
phix = .25;
//phis = .05;
phir = .7;
//arshocks
rhox = .7; 
rhop = .7; 
rhos = .7; 
mux = .5;
//shocks variances % with dynare 4 you don't have to guess their initial value apparently ... 
//ex = 0.4; 
//epi = 0.4; 
//es = 0.9;
//er = 0.6;


model;
xhat = (1/(1+psi))*xhat(+1)+(psi/(1+psi))*shat-(1/(1+psi))*(rhat-pihat(+1))+(1/(1+psi))*epsx;
//shat = (beta/(1+psi))*shat(+1)-exp(loglambda)*xhat(+1)-(rhat-pihat(+1))+epss; % log-transform of the lambda parameter
shat = (beta/(1+psi))*shat(+1)-lambda*xhat(+1)-(rhat-pihat(+1))+epss;
pihat = (beta/(1+psi))*pihat(+1)+k*xhat+epspi;
rhat = (1-phir)*(phipi*pihat+phix*xhat)+phir*rhat(-1)+er;
//rhat = (1-phir)*(phipi*pihat+phix*xhat+phis*shat)+phir*rhat(-1)+er; % TR with stock prices
//epsx=rhox*epsx(-1)+ex-mux*ex(-1);
epsx=rhox*epsx(-1)+ex-mux*endo(-1);
endo=ex;
epss=rhos*epss(-1)+es;
epspi=rhopi*epspi(-1)+epi;
end;

initval;
xhat = 0;
shat = 0;
pihat = 0;
rhat = 0;
epsx = 0;
epss = 0;
epspi = 0;
ex = 0;
es = 0;
epi = 0;
er = 0;
end;

shocks;
var ex; stderr 0.1;
var es; stderr 0.1;
var epi; stderr 0.1;
var er; stderr 0.1;
end;

steady(solve_algo=0);  check;

estimated_params;
//structural model
psi, uniform_pdf, .5, 0.2887; % uniform: mean = (b-a)/2, variance = (b-a)^2/12
//beta, beta_pdf, .99, 0.01; 
//lambda, gamma_pdf, 0.1, 0.25;
//loglambda, uniform_pdf, -2.30, 0.2;
k, gamma_pdf, .5, 0.1;
//trule
phipi, gamma_pdf, 1.5, 0.2;
phix, gamma_pdf, .25, 0.2;
//phis, uniform_pdf, 0.5, 0.2887;
phir, beta_pdf, .75, 0.1;
//ar1shocks
rhox, beta_pdf, 0.7, 0.1; 
rhopi, beta_pdf, 0.7, 0.1; 
rhos, beta_pdf, 0.7, 0.1; 
mux, beta_pdf, 0.5, 0.1;
//shocks variances
stderr ex, inv_gamma_pdf, 0.1, 0.25; 
stderr epi, inv_gamma_pdf, 0.1, 0.25; 
stderr es, inv_gamma_pdf, 0.1, 0.25;
stderr er, inv_gamma_pdf, 0.1, 0.25;
end;

//varobs xhat pihat rhat shat;
varobs xhat pihat rhat shat;

// comments ...
// databases: "spdatabase": all the gaps computed with the hp filter
//           "spdatabaseycbo": output gap with the potential output computed with the cbo estimates, the sp gap with the hp, sample: 1966q1-2006q4
//                             1st obs: 1966Qq1; 66th obs: 82q3
// data demeaned with the option 'prefilter=1'
// end comments ...


estimation(datafile=spdatabaseycbo66q106q4,prefilter=1,first_obs=66,mh_jscale=.575,mh_drop=.7,mode_compute=4,mode_check,mh_nblocks=1,bayesian_irf,mh_replic=0); % ycbo seems to work better
//estimation(datafile=spdatabaseycbo66q106q4,prefilter=1,first_obs=66,mh_jscale=.575,mh_drop=.7,mode_compute=0,mode_file=spestdynare_mode,mh_nblocks=2,bayesian_irf,moments_varendo,forecast=10,mh_replic=2000); % ycbo seems to work better                                                                                  


