% Model2nov Loglinear

%----------------------------------------------------------------
% 0. Housekeeping
%----------------------------------------------------------------

close all

%----------------------------------------------------------------
% 1. Defining variables
%----------------------------------------------------------------

var c d m n q x
y y_c y_d n_c n_d
 w q_d i r_k lambda 
mu mc R inf_c inf_d 
rr_c rr_d k k_c k_d;
varexo u;

parameters beta b gamma eta varphi theta eps_c eps_d omega delta_d delta alpha rhoS psi_y psi_inf tau kc_k nc_n i_yd yd_y 
r_k_ss mc_ss n_ss k_ss i_ss y_ss w_ss d_ss c_ss y_c_ss y_d_ss n_c_ss k_c_ss k_d_ss   ;
  
%----------------------------------------------------------------
% 2. Calibration
%----------------------------------------------------------------

//parameters

//taste parameters
  beta         = 1/(1.03^0.25);  //time discount factor
  b            = 0;         //habit formation (estimate by CEE, 2005)
  gamma        = 0.304103;       //consistent with steady state share nondurable production in Y=0.2
  eta          = 10;             //?? =1? (estimate by CEE, 2005)
  varphi       = 1;              //Frisch labor supply
  
//market structure
  theta        = 2/3;            //price stickiness of nondurable goods
  eps_c        = 11;             //such that steady state mark-up on nondurable goods is roughly 10%
  eps_d        = 11;             //sucht that steady state mark-up on durable goods is roughly 10% 
  omega        = 0;              //lagged inflation indexation (baseline value)

//technology
  delta_d      = 0.01;           //depreciation durable goods  (Monacelli, 2009)
  delta        = 0.025;          //depreciation captial, annual rate 10% (CEE, 2005)
  alpha        = 0.36;           //such that labor share in total income (CEE, 2005)
  
//monetary policy
  rhoS         = 0.8;             //parameter of nominal interest rate smoothing (CEE, 2005)
  psi_inf      = 1.5;             //weight attached to total inflation in Taylor rule (CEE, 2005)
  tau          = 0;               //weight attached to durable inflation in Taylor rule
  psi_y        = 0.1;             //weight attached to output distortions in Taylor rule (CEE, 2005)
//?? do we allow for constant money growth?? mu=1.017 (postwar quarterly average gross growth rate) ??

//shocks
  sigmaR        = 0.6;

//steady state values
  r_k_ss     = 1/beta-(1-delta);
  mc_ss      = (eps_d-1)/eps_d;
  n_ss       = 1/3;
  k_ss       = n_ss*(r_k_ss/(mc_ss*alpha))^(1/(alpha-1));
  i_ss       = k_ss*delta;
  y_ss       = (n_ss^(1-alpha))*k_ss^alpha;
  w_ss       = mc_ss*(1-alpha)*y_ss/n_ss;
  d_ss       = (y_ss-delta*k_ss)/(delta_d + ((1-beta*b)/(1-b))*((1-gamma)/gamma)*(1-beta*(1-delta_d)));
  c_ss       = ((1-beta*b)/(1-b))*((1-gamma)/gamma)*(1-beta*(1-delta_d))*d_ss;
  y_c_ss     = c_ss;
  y_d_ss     = delta_d*d_ss + i_ss;
  n_c_ss     = mc_ss*(1-alpha)*y_c_ss/w_ss;
  k_c_ss     = y_c_ss*mc_ss*alpha/r_k_ss;
  k_d_ss     = y_d_ss*mc_ss*alpha/r_k_ss;
  
//steady state ratios
  kc_k       = k_c_ss/k_ss;
  nc_n       = n_c_ss/n_ss;
  i_yd       = i_ss/y_d_ss;
  yd_y       = y_d_ss/y_ss;
%----------------------------------------------------------------
% 3. Model
%----------------------------------------------------------------

model; 
  //households
  varphi*n                          = w + lambda;
  -c + b*c(-1) + beta*b*(c(+1)-b*c) = lambda*(1-beta*b);
  - eta*q                           = (1/(1-beta))*R + lambda;
  lambda                            = R + lambda(+1) - inf_c(+1);
  q_d + lambda                      = beta*(1-delta_d)*(lambda(+1) + q_d(+1) + d) - d;

//firm input demand
  w                                 = mc + y - n;
  r_k                               = mc + y_c - k_c(-1);
  r_k                               = mc + y_d - k_d(-1);

//technology
  y_c                               = (1-alpha)*n_c + alpha*k_c(-1);
  y_d                               = (1-alpha)*n_d + alpha*k_d(-1);
  k                                 = kc_k*k_c + (1-kc_k)*k_d;

//firm output supply
  inf_c          = (omega/(1+beta*omega))*inf_c(-1) + (beta/(1+beta*omega))*inf_c(+1) + mc*(1-theta)*(1-theta*beta)/(theta*(1+omega*beta)); 
  mc                                = q_d;

//capital supplier
  k                                 = (1-delta)*k(-1) + delta*i;
  lambda                            = lambda(+1) + (1-beta*(1-delta_d))*r_k(+1);
   
//market clearing
  y_c                               = c;
  y_d                               = (1-i_yd)*(d - (1-delta_d)*d(-1)) + i_yd*i;
  n                                 = nc_n*n_c + (1-nc_n)*n_d;

//monetary policy and other
  R                                 = u + rhoS*R(-1) + (1-rhoS)*(psi_inf*((1-tau)*inf_c + tau*inf_d) + psi_y*y);
  rr_d                              = R - inf_d(+1); 
  rr_c                              = R - inf_c(+1);                                                         
  inf_d                             = inf_c + q_d - q_d(-1);                                                 
  y                                 = yd_y*y_d + (1-yd_y)*y_c;   
  inf_c(+1) + m(+1)                 = mu + m; 
  mu + m                    = q(+1);
  x                                 = q(+1)-q;                                                      

end;

%----------------------------------------------------------------
% 4. Computation
%----------------------------------------------------------------

initval; //(25 variables)
  x=0;c      = 0;
  d      = 0;
  m      = 0;
  n      = 0;
  q      = 0;

  y      = 0;
  y_c    = 0;
  y_d    = 0;
  n_c    = 0;
  n_d    = 0;

  w      = 0;
  q_d    = 0;
  r_k    = 0;
  lambda = 0;
  mu     = 0;

  i      = 0;
  mc     = 0;
  R      = 0;
  inf_c  = 0;
  inf_d  = 0;

  rr_c   = 0;
  rr_d   = 0;
  k      = 0;
  k_c    = 0;
  k_d    = 0;
end;

shocks;
var u; stderr sigmaR;
end;

steady;

resid(1);

check;

stoch_simul(order = 1, irf=25);

%----------------------------------------------------------------
% 5. Some Results
%----------------------------------------------------------------