% dynare loglinear_Calvo_simul.mod 
var w r C x psi phi N s i pi a m cp ygaphp ffrqhp inflgdpq;
parameters constepinf rgm ti ALFAI ALFAPAI ALFAY sigma newphi gamma alpha beta epselon teta chi mu rhom rhoA rhocp rhop CALI;
varexo ee uu ucp epsyhathp epsffrqhp;

sigma=1;        % elasticity of marginal utility of consumption
newphi=1;       % elasticity of labour supply
gamma=0;        % real wage rigidity parameter
alpha=0;        % decreasing returns to scale parameter Yit = At*Nit^(1-alpha)
beta=0.99;      % subjective discount factor
epselon=10;     % Dixit-Stiglitz elasticity of substitution among goods
teta=0.75;      % Calvo parameter
chi=0;          % degree of price indexation
mu=0;           % degree of backward looking indexation 
ti = 1.00^0.25;
rhom = 0.25;       % persistence of the inflation target shock
rhoA=0.25;       % persistence of technology shock
rhocp=0.25;      % persistence of cost-push shock
rgm=ti;
constepinf=log(ti); % inflationtarget

% TAYLOR RULE CALIBRATION
ALFAI=0.5;        % degree of interest rate inertia in Taylor rule
ALFAPAI=2;    % coefficient on inflation gap
ALFAY=0.5/4;      % coefficient on output gap

model(linear);
#aux=teta*rgm^((epselon-1)*(1-chi))/(1-teta*rgm^((epselon-1)*(1-chi)));
#xss=((1-teta*rgm^((epselon-1)*(1-chi)))/(1-teta))^(1/(1-epselon));
#sss=((1-teta)*(xss)^(-epselon/(1-alpha)))/(1-teta*rgm^(epselon*(1-chi)/(1-alpha)));

C=C(+1)-sigma^(-1)*(i-pi(+1));
w=gamma*w(-1)+(1-gamma)*(newphi*N+sigma*C)+cp;
x=aux*(pi-mu*chi*pi(-1));
(1+epselon*alpha/(1-alpha))*x=psi-phi;
psi=(1-teta*beta*rgm^(epselon*(1-chi)/(1-alpha)))*(w-(1/(1-alpha))*a+((1-alpha)^(-1)-sigma)*C)+(teta*beta*rgm^(epselon*(1-chi)*(1-alpha)^(-1)))*(psi(+1)+(epselon/(1-alpha))*pi(+1)-mu*chi*epselon/(1-alpha)*pi);
phi=(1-teta*beta*rgm^((epselon-1)*(1-chi)))*(1-sigma)*C+(teta*beta*rgm^((epselon-1)*(1-chi)))*(phi(+1)+chi*mu*(1-epselon)*pi+(epselon-1)*pi(+1));
N=s+(1-alpha)^(-1)*(C-a);
s=(-epselon*(1-teta*rgm^(epselon*(1-chi)/(1-alpha)))/(1-alpha))*x+((-epselon*chi*mu /(1-alpha))*teta*rgm^(epselon*(1-chi)/(1-alpha)))*pi(-1)+((epselon/(1-alpha))*teta*rgm^(epselon*(1-chi)/(1-alpha)))*pi+(teta*rgm^(epselon*(1-chi)/(1-alpha)))*s(-1);
i = m + ALFAPAI*(pi-constepinf)+C*ALFAY;
i=r+pi(+1);
a=rhoA*a(-1)+ee;
m=rhom*m(-1)+uu;
cp=rhocp*cp(-1)+ucp;
ygaphp = C + epsyhathp;
ffrqhp = i + epsffrqhp; 
inflgdpq = pi + constepinf; 

end;

initval;
a=0;
i=0;
pi=ti;
r=0;
C=0;
phi=0;
x=0;
s=0;
w=0;
N=0;
psi=0;
cp=0;
end;

steady;
check;

shocks;
var ee; stderr 0.25;
var uu; stderr 0.25;
var ucp; stderr 0.25;
end;

estimated_params;
teta, 0.98, 0.90,0.99,BETA_PDF,0.97,0.10;
sigma,1.5676,0.25,3,NORMAL_PDF,1.5676,0.375;
alpha,0.33,0.01,1.0,NORMAL_PDF,0.33,0.05;
newphi,1.4039,0.25,10,NORMAL_PDF,1.4039,0.75;
ALFAPAI,2,1.0,5,NORMAL_PDF,2,0.25;
ALFAY,0.05,0.000,0.5,NORMAL_PDF,0.05,0.05;
chi,0.3658,0.01,0.99,BETA_PDF,0.5,0.15;
epselon,10,2,15,NORMAL_PDF,10,1.5;
rhom,.9 ,.01,.9999,BETA_PDF,.5,0.174396;
rhoA,.9 ,.01,.9999,BETA_PDF,.5,0.174396;
rhocp,.9 ,.01,.9999,BETA_PDF,.5,0.174396;
stderr ee,0.50,0.01,3,INV_GAMMA_PDF,0.50,2;
stderr uu,0.50,0.01,3,INV_GAMMA_PDF,0.50,2;
stderr ucp,0.50,0.01,3,INV_GAMMA_PDF,0.50,2;
constepinf,0.005,0.0001,0.0306,NORMAL_PDF,0.005,0.1;
end;


varobs ygaphp ffrqhp inflgdpq;
estimation(datafile=data_us,first_obs=1,mode_compute=6,mode_check,mh_replic=50000,mh_nblocks=10,mh_jscale=.4);