// replication of model: Consumption, housing collateral, and Canadian Business Cycle
// Ian Christensen etal., working paper 29, Bank of Canada, 2009. 

// Created by Miroslav Hloušek in January 2013

var 
// basic endogenous variables
c1 c2 C R pi qh qk h1 h2 mju b1 b2 Yd L1 L2 w1 w2 k Rk uc1 uc2  //22
pid pd mc pim pm s Ym Y piw1 piw2 Ih Ik gdp H Yx        //15
rp bf Yf pif Rf G                                         //6
px pix  // alternative specification of export relations
L piw  // measurement variables

// endogenous variables with exogenous dynamics
Ak, Ah, j, z, epsilon_b, epsilon_L, epsilon_dp, epsilon_s, epsilon_x, epsilon_m, pi_T;

// Declaration of exogenous variables (shocks)
varexo
eps_Ak,   // capital productivity
eps_Ah,   // housing productivity
eps_j,   // housing demand
eps_z,   // technology 
eps_b,   // discount factor shock (intertemporal preferences)
eps_L,   // labor supply
eps_dp,  // price mark-up (domestic intermediates)
eps_s,   // exchange rate (risk premium)
eps_x,   // export demand
eps_m,   // import demand
eps_T,   // inflation target
eps_mp,  // monetary policy 

eps_Yf,   // foreign output
eps_pif,   // foreign inflation 
eps_Rf,  // foreign interest rate
eps_G;   // government expenditures

parameters 
//% deep parameters
beta,   // discount factor patient HH
beta2,  // discount factor impatient HH
betaf,  // discount factor for foreign economy
deltah,  // depreciation rate (housing)
deltak,  // depreciation rate (capital)
gamma,  // capital share (in production function)
jei,      // weight of housing in U 
eta,   // inverse of Frisch elasticity of labor supply
alpha,  // patient HH labor (wage) share 
b,      // habit in consumption
thetad, // Calvo parameter - domestic intermediates
thetam, // Calvo parameter - imported intermediates
thetaw, // Calvo parameter - imported intermediates 
thetax, // Calvo parameter - exported goods
omega,  // share of imported goods in CPI index
phi,    // elasticity of substitution between domestic and imported intermediates   
phis,   // sensitivity of risp premium to exchange rate
phirp,   // sensitivity of risk premium to foreign debt
psik,    // capital adjustment cost
psih,    // housing adjustment cost
m,     // loan-to-value ratio (impatient HH)
rhoR,  // AR parameter of interest rate - MP rule
rhopi, // weight to inflation - MP rule
rhoY,  // weight to inflation - MP rule
phif,  // weight to RER in demand for exports
rpss,  // steady state risk premium (gross)... 1 + rpss)

// shocks 
rhoAk,  // AR parameter - captial productivity shock
rhoAh,  // AR parameter - housing productivity shock
rhoj,  //  AR parameter - housing demand shock
rhoz,  // AR parameter - technology shock
rhob,  // AR parameter - discount factor shock
rhoL,  // AR parameter - labor supply shock
rhodp,  //  AR parameter - domestic prices shock
rhos,  // AR parameter - (risk premium) UIP shock
rhox,  // AR parameter - export demand shock
rhom,  // AR parameter - import demand  shock
rhoT,  // AR parameter - inflation target shock

// foreign sector
rhoYf, // AR parameter - foreing output
rhopif, // AR parameter - foreign inflation
rhoRf,  // AR parameter - foreign interest rate
rhoG,   // AR parameter - govenrment expenditure

// steady state parameters
epsilonss_dp,

// compounded parameters: see below
gammah1, gammah2, gammak, mh, X,
zeta1, zeta2, zeta3, zeta4, zeta5, zeta6, zeta7, zeta8, zeta9,
lambdad, lambdam,
c1toY, c2toY, qkIktoY, qhIhtoY, YxtoY,
c2toYd, qhh2toYd, w2L2toYd, Rb2toYd, b2toYd,
c1toYd, qhh1toYd, w1L1toYd, Rb1toYd, b1toYd, RkktoYd, sRfrpbftoYdpif, sbftoYd, sbftoY,
h1toH, h2toH,
c1toC, c2toC, YtoC, YtoGDP, ImptoGDP, GtoGDP,
YmtoY;

//************************************************************************** 
//% Setting of numerical values for parameters

// calibrated params
//% deep parameters
beta = 0.99;    // discount factor patient HH
beta2  = 0.98;  // discount factor impatient HH
betaf = 0.995;   // discount factor in foreign economy
deltah  = 0.005;  // depreciation rate (housing)
deltak = 0.05;  // depreciation rate (capital)
gamma = 0.3;  // capital share (in production function)
jei  = 0.1;      // weight of housing in U 
eta = 1;  // inverse of Frisch elasticity of labor supply
alpha = 0.4;  // patient HH labor (wage) share 
b = .5;      // habit in consumption
thetad = 0.66;  // Calvo parameter - domestic intermediates
thetam = 0.66; // Calvo parameter - imported intermediates
thetaw = 0.66; // Calvo parameter - imported intermediates 
thetax = 0.66; // Calvo parameter - exported goods
omega = 0.7;  // share of imported goods in CPI index
phi = 1;    // elasticity of substitution between domestic and imported intermediates   
phis = 1;  // sensitivity of risk premium to exchange rate  ?
phirp = 0.1;  // sensitivity of risk premium to foreign debt  ?
psik = 2;   // capital adjustment cost
psih = 2;    // housing adjustment cost
m = 0.75;      // loan-to-value ratio (impatient HH)
rhoR = 0.8;  // AR parameter of interest rate - MP rule
rhopi = 1.5; // weight to inflation - MP rule
rhoY = 0.1;  // weight to inflation - MP rule
phif = 0.5;  // weight to RER in demand for exports  ?

rhoAk = 0.75;  // AR parameter - captial productivity shock
rhoAh = 0.75;  // AR parameter - housing productivity shock
rhoj = 0.8;  //  AR parameter - housing demand shock
rhoz = 0.75;  // AR parameter - technology shock
rhob = 0.75;  // AR parameter - discount factor shock
rhoL = 0.75;  // AR parameter - labor supply shock
rhodp = 0.75;  //  AR parameter - domestic prices shock
rhos = 0.75;  // AR parameter - (risk premium) UIP shock
rhox = 0.75;  // AR parameter - export demand shock
rhom = 0.75;  // AR parameter - import demand  shock
rhoT = 0.75;  // AR parameter - inflation target shock
rhoYf = 0.75; // AR parameter - foreing output
rhopif = 0.75; // AR parameter - foreign inflation
rhoRf = 0.75;   // AR parameter - foreign irate
rhoG = 0.75;    // AR parameter - government expenditures

// standard deviations of shocks
sigma_Ak = 0.01;    // std - capital productivity
sigma_Ah = 0.01;    // std - housing productivity
sigma_j = 0.01;   // std - housing demand
sigma_z = 0.01;   // std - technology 
sigma_b = 0.01;   // std - discount factor shock (intertemporal preferences)
sigma_L = 0.01;   // std - discount factor shock (intertemporal preferences)
sigma_dp = 0.01;  // std - price mark-up (domestic intermediates)
sigma_s = 0.01;   // std - exchange rate (risk premium)
sigma_x = 0.01;   // std - export demand
sigma_m = 0.01;   // std - import demand
sigma_T = 0.0001;   // std - inflation target
sigma_mp = 0.0001;  // std - monetary policy 
sigma_Yf  = 0.01;   // std - foreign output
sigma_pif = 0.01;   // std - foregin inflation 
sigma_Rf = 0.01;  // std - foreign interest rate
sigma_G = 0.01;   // std - government expenditure

// steady state values
epsilonss_dp = 6;  // implies markup 20 %
YtoC = 1/0.51;    // from data
YtoGDP = 1.38;
ImptoGDP = 0.6;
GtoGDP = 0.20;

//% Defined parameters
lambdad = 1;    // relative prices Pd/P
lambdam = 1;    // relative prices Pm/P
gammah1 = beta*(1-deltah);    // compouned parameters
gammah2 = beta2*(1-deltah);
gammak = beta*(1-deltak);
mh = m*(beta-beta2);
X = epsilonss_dp/(epsilonss_dp-1);  // markup
zeta1 =  jei/(1-beta*(1-deltah));
zeta2 =  jei/(1-beta2*(1-deltah)-m*(beta-beta2));
zeta3 = beta/(1-beta*(1-deltak));
zeta4 = 1/(X*(1+(m*(1-beta)-deltah)*zeta2));
zeta5 = zeta2*zeta4;
zeta6 = (1-omega)*lambdad^phi;
zeta7 = omega*lambdam^phi;
rpss = betaf/beta;
zeta8 = log(rpss) / phirp;
zeta9 = (1 + 1/X + m*zeta2*zeta4*(1-beta) - zeta8/zeta6*(1/beta - 1) - zeta3*deltak/X)/(1+zeta1*deltah);
c2toYd = zeta4;
qhh2toYd = zeta5;
w2L2toYd = 1/X;
Rb2toYd = m*zeta5;
b2toYd = beta*m*zeta5;
c1toYd = zeta9;
qhh1toYd = zeta1*zeta9;
w1L1toYd = 1/X;
Rb1toYd = -m*zeta5;
b1toYd = -beta*m*zeta5;
RkktoYd = 1/X;
sbftoYd = zeta8/zeta6;
sRfrpbftoYdpif = zeta8/(zeta6*beta);
sbftoY = zeta8;

// market clearing
c1toY = c1toYd*zeta6;
c2toY = c2toYd*zeta6;
qkIktoY = zeta3*zeta6*deltak/X;
qhIhtoY = zeta6*deltah*(zeta1*zeta9 +zeta2*zeta4); 
YxtoY = zeta8*((1-beta)/beta)+zeta7; 
h1toH = (zeta1*zeta9)/(zeta1*zeta9 + zeta2*zeta4);
h2toH = (zeta2*zeta4)/(zeta1*zeta9 + zeta2*zeta4);
c1toC = zeta9*zeta6*YtoC;
c2toC = zeta4*zeta6*YtoC;

//trade balance
YmtoY = zeta7;
bftoY = zeta8;

model(linear);
// definition of marginal utility (unconstr. HH)
uc1 = -(c1 - b*c1(-1)) + epsilon_b;

// definition of marginal utility (constr. HH)
uc2 = -(c2 - b*c2(-1)) + epsilon_b;

// Euler equation (unconstr. HH)
uc1 = uc1(+1) + R - pi(+1);

// housing demand  (unconstr. HH)
qh = gammah1* qh(+1) + (1-gammah1)*(j-h1) - uc1 + gammah1*uc1(+1); 

// capital supply (unconstr. HH)
qk = gammak* qk(+1) + (1-gammak)*Rk(+1) - uc1 + uc1(+1); 

// Euler equation (constr. HH)
uc2 = beta2/beta * (uc2(+1) + R - pi(+1)) + (1-beta2/beta)*mju;

// housing demand (constr. HH)
qh = (gammah2+mh)* qh(+1) + (1-gammah2-mh)*(j-h2) - uc2 + gammah2*uc2(+1) + mh*(mju + pi(+1) - R); 

// borrowing constraint  (constr. HH)
b2 = qh(+1) + h2 + pi(+1) - R;

// production function 
Yd = (1-gamma)*alpha*L1 + (1-gamma)*(1-alpha)*L2 + gamma*k(-1) + z;

// Phillips curve for domestic intermediates 
pid = beta* pid(+1) + (1-thetad)*(1-beta*thetad)/thetad * (mc + epsilon_dp);  //  

// definition of marginal cost (labor demand for unconstrained workers)
mc = w1 + L1 - Yd;

// definition of marginal cost  (labor demand for unconstrained workers)
mc = w2 + L2 - Yd;

// definition of marginal cost (capital demand)
mc = Rk + k(-1) - Yd;  

// definition of domestic intermediate relative price
pid - pi = pd - pd(-1);

// Phillips curve for imported intermediates
pim = beta* pim(+1) + (1-thetam)*(1-beta*thetam)/thetam * (s - pm);

// definition of domestic intermediates relative price
pim - pi = pm - pm(-1);

// demand for home intermediates
 Yd = - phi*(pd) + Y;

// demand for imported intermediates
Ym = -phi*(pm) + Y + epsilon_m; 

// cpi index in changes
pi = (1-omega)*pid + omega*pim;

// cpi inflation
//pi = p - p(-1);

// Wage Phillips curves
piw1 =  beta* piw1(+1) + (1-thetaw)*(1-beta*thetaw)/thetaw * ((eta-1)*L1 - uc1  - w1 + epsilon_L);

// Wage Phillips curves
piw2 =  beta2* piw2(+1) + (1-thetaw)*(1-beta2*thetaw)/thetaw * ((eta-1)*L2 - uc2  - w2 + epsilon_L);

// nominal wage growth
piw1 = w1 - w1(-1) + pi;

// nominal wage growth
piw2 = w2 - w2(-1) + pi;

// Capital production of housing
qh = psih*(Ih - H(-1)) - Ah;

// Capital production of housing
//qk = psik*(Ik - k(-1)) - Ak;
// or alternatively
 qk = psik*((1-beta)*Ik - Ik(-1) + beta*Ik(+1)) - Ak;

// law of motion for capital
k = deltak*Ik + (1-deltak)*k(-1);

// law of motion for capital
H = deltah*Ih + (1-deltah)*H(-1);

// identity for housing
H = h1toH * h1 + h2toH * h2;

// definition of aggregate consumption
C = c1toC * c1 + c2toC * c2;

// monetary policy rule
R = rhoR * R(-1) + (1-rhoR)*(rhopi*(pi - pi_T) + rhoY*gdp) + eps_mp;

//market clearing condition
//Y = zeta6* (c1toYd * c1 + c2toYd * c2 + (zeta3*deltak/X)*Ik + (zeta5*deltah/h2toH) * Ih + 0.3 * Yx);
//Y = c1toY * c1 + c2toY * c2 + qkIktoY * (Ik + qk) + qhIhtoY * (Ih + qh) + YxtoY * Yx;
Y = c1toY * c1 + c2toY * c2 + qkIktoY * Ik + qhIhtoY * Ih + YxtoY * Yx;

// measurement definition of GDP
gdp = YtoGDP * Y - ImptoGDP* Ym + GtoGDP * G; 
//gdp =  Y - s*Ym + G;

// budget constraint of borrowers
c2toYd * c2 + qhh2toYd*(h2 - (1-deltah)*h2(-1)) = w2L2toYd * (w2 + L2) - Rb2toYd * (R(-1) + b2 - pi) + b2toYd*b2;

// budget constraint of borrowers
c1toYd * c1 + qhh1toYd*(h1 - (1-deltah)*h1(-1)) + sRfrpbftoYdpif * (R(-1) + rp(-1) + bf(-1) - pif) - sbftoYd*(s+bf) = w1L1toYd* (w1 + L1) - Rb1toYd*(R(-1) + b1 - pi) + b1toYd*b1 + RkktoYd*(Rk+k); 

// overal borrowing constraint
0 = b1 + b2;

// demand for exports
Yx = Yf - phif*px + epsilon_x;

// Phillips curve for exports
pix = betaf * pix(+1) + (1-thetax)*(1-beta*thetax)/thetax *  (- s);  //
// where export price inflation 
pix = px - px(-1);

// or more simply (demand for exports)
//Yx =  phif*s + Yf + epsilon_x;

// UIP condition
R - pi(+1) - Rf + pif(+1) = (1+phis)*(s(+1) - s) + phis * (s - s(-1)) + rp;

// risk premium
rp = phirp * sbftoY * (s + bf - Y) + epsilon_s;

// trade balance  // not needed?  
//YxtoY * Yx - YmtoY * (s + Ym) = sbftoY/beta * (bf(-1) + s + Rf(-1) + rp(-1) - pif) - sbftoY *(bf + s);

// Foreign block
// output
Yf = rhoYf * Yf(-1) + eps_Yf;

// inflation
pif = rhopif * pif(-1) + eps_pif;

// interest rate
Rf = rhoRf * Rf(-1) + eps_Rf;

// government expenditures
G = rhoG * G(-1) + eps_G;

// AR procesess for shocks 
// capital 
Ak = rhoAk * Ak(-1) + eps_Ak;
// housing
Ah = rhoAh * Ah(-1) + eps_Ah;
// housing demand 
j = rhoj * j(-1) + eps_j;
// technology
z = rhoz * z(-1) + eps_z;
// discount factor 
epsilon_b = rhob * epsilon_b(-1) + eps_b;
// labor supply
epsilon_L = rhoL * epsilon_L(-1) + eps_L;
// price markup (domestic intermediates)
epsilon_dp = rhodp * epsilon_dp(-1) + eps_dp;
// risk premium  (real exchnge rate)
epsilon_s = rhos * epsilon_s(-1) + eps_s;
// import demand 
epsilon_m = rhom * epsilon_m(-1) + eps_m;
// export demand
epsilon_x = rhox * epsilon_x(-1) + eps_x;
// inflation target
pi_T = rhoT * pi_T(-1) + eps_T;
// measurement equations
// labor
L = alpha*L1 + (1-alpha)*L2;
// wage inflation
piw = alpha*piw1 + (1-alpha)*piw2;
end;                       


shocks;
var eps_Ak; stderr sigma_Ak;
var eps_Ah; stderr sigma_Ah;
var eps_j; stderr sigma_j;
var eps_z; stderr sigma_z;
var eps_b; stderr sigma_b;
var eps_L; stderr sigma_L;
var eps_dp; stderr sigma_dp;
var eps_s; stderr sigma_s;
var eps_x; stderr sigma_x;
var eps_m; stderr sigma_m;
var eps_T; stderr sigma_T;
var eps_mp; stderr sigma_mp;
//var eps_Yf; stderr sigma_Yf;
//var eps_pif; stderr sigma_pif;
//var eps_Rf; stderr sigma_Rf;
//var eps_G; stderr sigma_G;
end;

steady;
check; 
stoch_simul(ar=5,irf=20) C Ik Ih gdp R pi qh qk Y; 




