% Jason Jones - Model 2 from Canzoneri's notes Calvo wage setting
%   one sector no gov stuff yet, the real version, no habit persistence
%---------------------------------------------------------
%Housekeeping
%----------------------------------------------------------

close all;

%----------------------------------------------------------
%defining variables
%----------------------------------------------------------
var lm, C, K, I, r, w, N, mc, Z, lmk, pstar, pa, pb, inf, Y, DP, i, winf, wstar, wb, wa; 

varexo eps_z; 
   
parameters delta, psi, fi, sig, mu_p, alpha, beta, kappa, chi, phi, mu_w, rho_z, sd_z, stuff, om;
%----------------------------------------------------------
%calibration
%----------------------------------------------------------
delta = .025;    % depreciation
psi = 8;         % captial adjustment cost parameter from "Cost of Nomial Inertia in NNS Models" 
fi = .25;        % capital share from same
sig = 6;         %  elas of sub in consumption aggregator - notes   
mu_p = sig/(sig-1);  
alpha = .67;      % prob prices are not reset - notes
beta = .99;       % discount rate
kappa = 1;        % weight on disutility of labor
chi = 6;          % Frish elasticity  - notes
phi = 6;          % elas of sub in labor aggregator - notes  
mu_w = phi/(phi-1);
stuff = (fi^fi)*((1-fi)^(1-fi));
om = .75;          %  prob wage is not reset - notes

rho_z = .8;        % productivity AR process - notes
sd_z = .009;

%----------------------------------------------------------------
%model
%----------------------------------------------------------------
% note  inf = Pt/Pt-1 so log(inf) is what we traditionaly call inflation and is what is used in policy rule

model;
lm = (1/C);                                                  //household and firm foc's  //1
K = (1-delta)*K(-1) + I - .5*psi*(((I/K(-1))-delta)^2)*K(-1);                            //2
r = w*(fi/(1-fi))*(N/K(-1));                                                             //3   
mc = (r^fi)*(w^(1-fi))/(Z*stuff);                                                        //4   
lm = lmk - lmk*psi*((I/K(-1))-delta);                                                    //5   
lmk = beta*(lm(1)*r(1) + lmk(1)*((1-delta)-.5*psi*(((I(1)/K)-delta)^2) + psi*((I(1)/K)-delta)*(I(1)/K)));  //6

pstar = mu_p*(pb/pa);                                     //price block                 //7
pb = alpha*beta*(inf(1)^sig)*pb(1) + lm*mc*Y;                                           //8
pa = alpha*beta*(inf(1)^(sig-1))*pa(1) +lm*Y;                                           //9
1 =(1-alpha)*pstar^(1-sig) + alpha*inf^(sig-1);                                         //10

Y = (Z*(K(-1)^fi)*N^(1-fi))/DP;                           // production block           //11
DP = ((1-alpha)*(1/pstar)^sig) + alpha*DP(-1)*(inf)^sig;                                //12

C = Y - I;                                                // mkt clearing               //13         

i = ((beta*(lm(1)/lm)*(inf(1)^-1))^-1)-1;                  // household foc wrt B        //14
// (1+i)^-1 = beta*(lm(1)/lm)*inf(1)^-1;

wstar^(1+phi*chi) = mu_w*kappa*(wb/wa);                 // wage block                   //15
wb = om*beta*(winf(1)^(phi*(1+chi)))*wb(1) + N^(1+chi);                                 //16    
wa = om*beta*(winf(1)^(phi-1))*wa(1) + lm*N*w;                                          //17
1 = (1-om)*wstar^(1-phi) + om*winf^(phi-1);                                             //18
w = w(-1)*winf/inf;                                                                     //19

log(Z) = rho_z*log(Z(-1)) + eps_z;      //AR(1) process for tech                        //20
i = -log(beta)+1.08*log(inf);           //policy rule from "Macro Policy in the EMU"    //21
// i = (1/beta)-1 + 1.08*log(inf);
end;

%-----------------------------------------------------------------
%Computation
%-----------------------------------------------------------------
% my notes on the side assume that variables ordered above are as stated

initval;
i = -log(beta);    //  this and next combo of 21 and 14
inf = 1;           
pstar = 1;         // from 10
DP = 1;            // from 12
Z = 1;             // from 20
mc = 1/mu_p;       // steady state pb divided by steady state pa put into 7
r = (1-(beta*(1-delta)))/beta;      // from 6
w = (mc*(stuff)*(r^-fi))^(1/(1-fi));  // from 4
N = ((w/(kappa*mu_w))*((((w/r)^fi)*((fi/(1-fi))^fi)-delta*(w/r)*(fi/(1-fi)))^-1))^(1/(1+chi));  //w from 3 = w from 15-19, sub out lm with 1, sub out C with 13, sub out Y using 11 and I using 2, then sub out K using 3, all that is left is N solve
K = (w/r)*(fi/(1-fi))*N;  // from 3 
I = delta*K;              // from 2
Y = (K^fi)*(N^(1-fi));    // from 11
C = Y - I;                // from 13
lm = 1/C;                 //  from 1
lmk = lm;                 // from 5
pb = ((1-alpha*beta)^-1)*lm*mc*Y;    //from 8 
pa = ((1-alpha*beta)^-1)*lm*Y;       //from 9

wstar = 1;                          // rest just combo of 15-19
winf = 1;
wa = ((1-om*beta)^-1)*lm*N*w;
wb = ((1-om*beta)^-1)*N^(1+chi);
end;

resid(1);

shocks;
var eps_z; stderr sd_z;

end;

steady(solve_algo = 2);
check;

stoch_simul (order =1, irf = 20) lm, C, K, I, r, w, N, mc, Z, lmk, pstar, pa, pb, inf, Y, DP, i;