// REPLICATING LIU (2006): A Small New Keynesian Model of the New Zealand Economy  
// RBNZ Discussion Paper: DP2006/3

// PREAMBLE
var y q r pi pif s c rstar ystar pih psi mc a es eq epih epif er;

// y        output
// q        real exchange rate
// r        nominal interest rate
// pi       overall inflation
// pif      import inflation
// s        terms of trade (TOT)
// c        consumption
// rstar    foreign (real) interest rate
// ystar    foreign output
// pih      domestic inflation
// psi      law of one price (LOOP) gap
// mc       marginal costs
// a        productivity
// es       TOT shock
// eq       UIP shock
// epih     domestic inflation shock
// epif     imported inflation shock
// er       monetary shock


varexo eps_a eps_s eps_q eps_pih eps_pif eps_r eps_ystar eps_rstar;

parameters beta alpha h sigma phi eta thetah thetaf rhoa phi1 phi2 rhor lam1 rhorstar lambdah lambdaf;

beta    =   0.99;    // discount rate
alpha   =   0.40;    // openness of the economy
h       =   0.92;    // external habit formation
sigma   =   0.39;    // inverse elasticity of intertemporal substitution
phi     =   1.82;    // inverse elasticity of labour supply
eta     =   0.85;    // elasticity of substitution between home & foreign goods
thetah  =   0.75;    // fraction of domestic firms that cannot reset prices
thetaf  =   0.72;    // fraction of importers that cannot reset prices
rhoa    =   0.98;    // AR term of domestic technological progress
phi1    =   1.45;    // coefficient of inflation in Taylor Rule
phi2    =   0.41;    // coefficient of output in Taylor Rule
rhor    =   0.72 ;    // coefficient of interest rate smoothing in Taylor Rule
lam1    =   0.79;    // AR term of foreign output
rhorstar=   0.83;    // AR term of foreign interest rate

// Calculations

lambdah = (1-beta*thetah)*(1-thetah)/thetah;      // coefficient of MC in domestic NKPC
lambdaf = (1-beta*thetaf)*(1-thetaf)/thetaf;      // coefficient of MC in importers' NKPC


// MODEL EQUATIONS

model(linear);

// LOOP gap
psi = -q - (1-alpha)*s;

// Goods market clearing condition
y = (2-alpha)*alpha*eta*s + (1-alpha)*c + alpha*eta*psi + alpha*ystar;

// Taylor Rule
r = rhor*r(-1) + (1-rhor)*phi1*pi + (1-rhor)*phi2*y + er;    

// TOT with measurement error
s - s(-1) = pif - pih + es;

// Firm's marginal cost
mc = sigma/(1-h)*c - sigma/(1-h)*h*c(-1) + phi*y + alpha*s - (1+phi)*a;

// Overall inflation
pi = (1-alpha)*pih + alpha*pif;

// UIP condition with risk premium shock
q - q(-1) =  rstar(-1) - (r(-1) - pi) + eq;   

// Domestic inflation
pih = beta*(1-thetah)*pih(+1) + thetah*pih(-1) + lambdah*mc + epih;

// Import inflation
pif = beta*(1-thetaf)*pif(+1) + thetaf*pif(-1) + lambdaf*psi + epif;

// Consumption Euler equation and international risk sharing condition
ystar = h*ystar(-1) + (1-h)/sigma*q + c(+1) - h*c - (1-h)/sigma*r + (1-h)/sigma*pi(+1);

// Exogenous technological progress
a = rhoa*a(-1) + eps_a;

// Exogenous foreign output
ystar = lam1*ystar(-1) + eps_ystar;

// Exogenous foreign interest rate
rstar = rhorstar*rstar(-1) + eps_rstar;

// Other exogenous processes

es     = 0*es(-1) + eps_s;
eq     = 0*eq(-1) + eps_q;
epih   = 0*epih(-1) + eps_pih;
epif   = 0*epif(-1) + eps_pif;
er     = 0*er(-1) + eps_r;

end;

// CALCULATION OF THE STEADY STATE

steady;

check;

// SHOCK STRUCTURE

shocks;
var eps_s;      stderr 9.16; 
var eps_q;      stderr 6.07;
var eps_pih;    stderr 1.57;
var eps_pif;    stderr 3.39;
var eps_r;      stderr 0.77;
var eps_a;      stderr 0.80;
var eps_ystar;  stderr 0.69;
var eps_rstar;  stderr 0.54;
end;

// STOCHASTIC SIMULATION

//stoch_simul(periods=2000) y c r pi pih pif psi s rstar ystar;


// OBSERVED VARIABLES
//
//
//
//
//
//ESTIMATION
//
estimated_params;
h,  beta_pdf, 0.7, 0.2;
sigma, normal_pdf, 1.0, 0.25;  
phi, gamma_pdf, 1.0, 0.3;   
eta, gamma_pdf, 1.0, 0.3;  
thetah, beta_pdf, 0.5, 0.25;
thetaf, beta_pdf, 0.5, 0.25;
rhoa, beta_pdf,  0.5, 0.2;
phi1, gamma_pdf, 1.5, 0.25; 
phi2, gamma_pdf, 0.25, 0.1; 
rhor, beta_pdf, 0.5, 0.2;
lam1, beta_pdf, 0.5, 0.2;
rhorstar, beta_pdf, 0.5, 0.2;
stderr eps_s, inv_gamma_pdf, 2, inf;
stderr eps_q, inv_gamma_pdf, 2, inf;
stderr eps_pih, inv_gamma_pdf, 2, inf; 
stderr eps_pif, inv_gamma_pdf, 2, inf;
stderr eps_r, inv_gamma_pdf, 2, inf;
stderr eps_a, inv_gamma_pdf, 2, inf;
stderr eps_ystar, inv_gamma_pdf, 2, inf; 
stderr eps_rstar, inv_gamma_pdf, 2, inf; 
end;

varobs y pi pif r s q ystar rstar;

estimation(datafile=data2,nobs=43,mh_replic=10000,mh_nblocks=2,mh_drop=0.45,mh_jscale=0.4);



