//***************************************************************************************
//********************REPLICATION OF THE FRBNY DSGE MODEL********************************
//********************by BERTHEAU, FRANCESCHI, LE FUR, PANON*****************************
//***************************************************************************************


//******************************************************
//******************DECLARATION OF THE VARIABLES********
//******************AND PARAMETERS**********************
//******************************************************


var 
c                   // Consumption
L                   // Labor supply (total available hours are normalized to one)
// m                // Mt(j): Money holdings
xi                  // Xi_t: multiplicator
w                   // Wt(j) is the wage paid to Lt(j)
wtilde              // Households that can optimize at time t choose a wage Wt(j)tilde to maximize (6)
y                   // Final good
pi                  // Inflation
mc                  // Nominal marginal cost
k                   // Effective capital
qk                  // Tobin's q, price of installed capital
i                   // Investments (amount)
kbar                // Installed capital (bought by entrepreneurs from capital producers at the end of current period)
rk                  // Rental rate for unit of effective capital
u                   // Capital utilisation rate
r                   // Nominal interest rate (gross, so R>1)
Rktilde             // Nominal return to capital for entrepreneurs (gross, so Rktilde>1)
n                   // Entrepreneurs' net worth
z                   // Growth rate of productivity
phi                 // Preference shifter
lambda_f            // Net mark-up that intermediate firms would like to charge over marginal costs 
mu                  // Marginal Efficiency of Investment shock  
sigma_omega         // Spread-shock: an increase in volatility increases the perceived riskiness of borrowers-->increases the cost of capital
g                   // Government spending 
Y_obs               // Real Output growth (% annualised)
L_obs               // Hours worked (%)
LS_obs              // Labor Share  (%)
pi_core_obs         // Inflation (% annualised)
FFR_obs             // Interest rate (% annualised)
SP_obs              // Spread (% annualised)
;                  
 
   
varexo  
ez                   // Innovation of the unit-rooted productivity growth process
ephi                 // Innovation of the labour supply shock
elambda_f            // Innovation of the intermediate goods producers mark-up shock
emu                  // Innovation of the MEI shock
esigma_omega         // Innovation of the default cut-off shock (idiosyncratic random disturbance to entrepreneurs' capital productivity)
eg                   // Innovation of the government spending (demand shock)
eps_R      ;         // Innovation of the current period unanticipated shock (mon. policy)
           

parameters alpha beta nu_l nu_m h lambda_w zeta_w chi pi_star gamma delta gamma_e zeta_spb lambda_f_ss rho_r psi_pi psi_y ddS dda zeta_p zeta_spsigma_omega zeta_nRktilde zeta_nr zeta_nn zeta_nqk zeta_nsigma_omega rho_z rho_lambda_f rho_mu rho_g rho_sigma rho_phi r_ss L_adj SP_star LS_star g_ss; 
beta    = 0.9950;    // Consumer's discount rate (from CMR) 
nu_l    = 2;         // Curvature on Disutility of Labor
nu_m    = 2;         // Curvature on Utility of money
h       = .7;        // Habit persistence
zeta_w  = .8;        // Calvo wage rigidity
lambda_w= .3;        // Affects the elasticity of substitution between differentiated labor services.(mark-up wages)
chi     = .1;        // Weight on utility of money
pi_star = 2;         // Inflation target. Useful for people stuck with their former wages (with proba zeta_w )
gamma   =2.75/100;   // exp(gamma) is growth rate of the economy (useful for people stuck with their former wages)
delta = .025;        // Depreciation rate
gamma_e=.99;         // Fraction of surviving entrepreneurs to the shock at each period
zeta_spb=0.05;       // Elasticity of spread to leverage
lambda_f_ss=0.15;    // Steady state value of the mark up shock
rho_r=0.5;           // Persistence of moneatry policy
psi_pi=2;            // Monetary policy stuff
psi_y=0.2;           // Monetary policy stuff
ddS=4;               // Second derivative of S
dda=0.2;             // Second derivative of a
zeta_p=0.75;         // Calvo parameter: share of firms that can't adjust their prices
alpha= .33;          // Elasticity of capital with respect to output
rho_z=0.4;           // Coefficient of the AR(1) process 
rho_phi=0.75;        // Coefficient of the AR(1) process 
rho_lambda_f=0.75;   // Coefficient of the AR(1) process
rho_mu=0.75;         // Coefficient of the AR(1) process
rho_g=0.75;          // Coefficient of the AR(1) process
rho_sigma=0.75;      // Coefficient of the AR(1) process
r_ss=1.5;            // Steady state (gross) nominal interest rate    
L_adj=253.5;         // Captures the units of measured hours worked
SP_star=2;           // Spread in steady state
LS_star=exp(-58);    // Labour share in steady state
g_ss=0.3;            // Government spending in steady state


//To be found on 7.46-7.47 p. 39 of the notes that we used - These values come directly from the Matlab code published

zeta_spsigma_omega=0.0293;                                                 //elasticity of the spread to volatility of the spread shock
zeta_nRktilde=1.5884;                                                      //elasticity of net worth to return on capital
zeta_nr=0.5861;                                                            //elasticity of net worth to nominal interest rate
zeta_nqk=0;                                                                //elasticity of net worth to the cost of capital
zeta_nn=1.0023;                                                            //elasticity of net worth to net worth itself
zeta_nsigma_omega=0.004;                                                   //elasticity of net worth to the volatility of sigma_omega (variance of the shock to entrepreneurs)

//NB: parameters "chi" and "pi_star" are not used



//***********************************************
//******************MODEL************************
//***********************************************



model(linear);
//Shorthand for steady state values; some are derived directly from the published code, namely n_ss, L_ss, g_ss,

#rk_ss=beta^(-1)*exp(gamma) - (1-delta);                                   // Rental rate of capital
#w_ss= (1/(1+lambda_f_ss)*(alpha^alpha)*(1-alpha)^(1-alpha)*               // Optimal wage
rk_ss^(-alpha))^(1/(1-alpha));                                            
#L_ss=1;                                                                   // Labour supply at SS
#ratioIK_ss=1-((1-delta)/(exp(gamma)));                                    // Investment to capital ratio
#upsilon_ss=1;                                                             //
#n_ss=11.9196;                                                             // Steady state entrepreneurs' net worth
#k_ss=(alpha/(1-alpha))*(w_ss/rk_ss)*L_ss;                                 // Capital stock
#kbar_ss=exp(gamma)*k_ss;                                                  // Effective capital
#i_ss=kbar_ss*ratioIK_ss;                                                  // Investments                                                     
#y_ss=(k_ss^alpha)*(L_ss^(1-alpha));                                       // Output (with fixed cost it'd include PHI)
#c_ss=(y_ss/g_ss)-i_ss;                                                    // Consumption
#realised= Rktilde-pi;                                                     // Shorthand for realised return on capital  
#spread = Rktilde(+1) - r;                                                 // Shorthand for expected excess return on capital


z=rho_z*z(-1)+ez;
phi=rho_phi*phi(-1)+ephi;
lambda_f=rho_lambda_f*lambda_f(-1)+elambda_f;
mu=rho_mu*mu(-1)+emu;
sigma_omega=rho_sigma*sigma_omega(-1)+esigma_omega;
g=rho_g*g(-1)+eg;

//Here we put the loglin'd equations from the published paper

xi=r+xi(+1)-z(+1)-pi(+1);                                                  // Consumption euler equation
                                               
(exp(gamma)-h*beta)*(exp(gamma)-h)*xi = -(exp(2*gamma)-beta*h^2)*c
+h*exp(gamma)*c(-1) - h*exp(gamma)*z + 
beta*h*exp(gamma)*c(+1) + beta*h*exp(gamma)*z(+1);                         // Marginal utility of consumption

kbar(-1)=-(1-(i_ss/k_ss))*z + (1-(i_ss/k_ss))*kbar(-2) + (i_ss/k_ss)*mu
+ (i_ss/k_ss)*i;                                                           // Capital stock

k= u-z +kbar(-2);                                                          // Effective capital

rk_ss*rk=dda*u;                                                            // Capital utilisation; dda is a parameter

i= (1/(1+beta))*(i(-1) - z) + (beta/(1+beta))*(i(+1) + z(+1)) +
(1/((1+beta)*ddS*exp(2*gamma)))*qk + (1/((1+beta)*ddS*exp(2*gamma)))*mu;   // Optimal investment decision; ddS is a parameter

realised=(rk_ss/(rk_ss+(1-delta)))*rk + ((1-delta)/(rk_ss+(1-delta)))*qk 
-qk(-1);                                                                   // Realised real return to capital

spread= zeta_spb*(qk + kbar(-1) - n) + zeta_spsigma_omega*sigma_omega;         // Spread

n=zeta_nRktilde*(Rktilde-pi)-zeta_nr*(r(-1)-pi)+zeta_nqk*(qk(-1)+kbar(-2))+
zeta_nn*n(-1) - gamma_e*(upsilon_ss/n_ss)*z - zeta_nsigma_omega*sigma_omega(-1);    // Entrepreneurs' net worth

w=w(-1)-pi+((1-zeta_w)/zeta_w)*wtilde;                                     // Evolution of the aggregate nominal wage

(1 + nu_l*((1 + lambda_w)/lambda_w))*wtilde +
(1 + zeta_w*beta*nu_l*((1 + lambda_w)/lambda_w))*w = zeta_w*beta*
(1 + nu_l*((1 + lambda_w)/lambda_w))*(wtilde(+1) + w(+1)) + phi
+ (1 - zeta_w*beta)*(nu_l*L - xi) + zeta_w*beta*(1 + nu_l*
((1 + lambda_w)/lambda_w))*(pi(+1) + z(+1));                               // Optimal reset wage

pi = beta*pi(+1) + (((1 - zeta_p*beta)*(1 - zeta_p))/zeta_p)*mc +
(1/zeta_p)*(((1 - zeta_p*beta)*(1 - zeta_p)*lambda_f_ss)/(1 + 
lambda_f_ss))*lambda_f;                                                    // Phillips curve

mc=(1-alpha)*w+alpha*rk;                                                   // Marginal cost (or labour share)

y=alpha*k+(1-alpha)*L;                                                     // Production fuction

k=w-rk+L;                                                                  // Capital labour ratio

y = g + (c_ss/(c_ss + i_ss))*c + (i_ss/(c_ss + i_ss))*i +
((rk_ss*k_ss)/(c_ss + i_ss))*u;                                            // Resource constraint

r=rho_r*r(-1)+(1-rho_r)*(psi_pi*(pi+pi(-1)+pi(-2)+pi(-3))+psi_y*(y+z+      // Policy rule 
+z(-1)+z(-2)-y(-4)+z(-3)))+ eps_R+eps_R(+4);                               



//Measurement equations

Y_obs=400*(y-y(-1)+z+gamma);

L_obs=100*(L+log(L_adj));

LS_obs=100*(L+w-y+log(LS_star));

pi_core_obs=400*(pi+log(pi_star));

FFR_obs=400*(r+log(r_ss));

SP_obs=400*(spread +SP_star);

end;

varobs Y_obs L_obs LS_obs pi_core_obs FFR_obs SP_obs;

observation_trends;
Y_obs (z+gamma);
L_obs(log(L_adj));
LS_obs (w-y+log(LS_star));
pi_core_obs (log(pi_star));
FFR_obs (log(r_ss));
SP_obs (SP_star);

end;

//***********************************************
//******************ESTIMATION*******************
//***********************************************

//  ****PRIORS****
// As in table 1 of the paper

estimated_params;

alpha, beta_pdf, .33, .02; 
 
zeta_p, beta_pdf, .75, .1;
 
delta, beta_pdf, .025, .01;

ddS, gamma_pdf, 4, 1.5;

h, beta_pdf, .7, .05;

dda, gamma_pdf, .2, .10;  

nu_l, gamma_pdf, 2, .75; 

nu_m, gamma_pdf, 2, .75; 

zeta_w, beta_pdf, .75, .1;

r_ss, gamma_pdf, 1.5, 1;

psi_pi, gamma_pdf, 2, 0.25; 

psi_y, gamma_pdf, 0.2, 0.1; 

rho_r, beta_pdf, .5, .2; 

pi_star, normal_pdf, 2, 0.25;

SP_star, gamma_pdf, 2, 0.5;  

zeta_spb, beta_pdf, .05, .02;

// Exogenous processes - level

gamma, gamma_pdf, 2.75/100, .5; 

chi, gamma_pdf, 0.1, 0.1; 

g_ss, gamma_pdf, 0.3, 0.1;

L_adj, normal_pdf, 253.5, 5;

// Exogenous processes - autocorrelation

rho_z, beta_pdf, 0.4, 0.25;

rho_phi, beta_pdf, 0.75, 0.15;

rho_lambda_f, beta_pdf, 0.75, 0.15;

rho_mu, beta_pdf, 0.75, 0.15;

rho_g, beta_pdf, 0.75, 0.15;

rho_sigma, beta_pdf, 0.75, 0.15;

// Exogenous processes - standard deviation

stderr ez, inv_gamma_pdf, 0.3, 4;

stderr ephi, inv_gamma_pdf, 3, 4.00;

stderr elambda_f, inv_gamma_pdf, 0.2, 4.00;

stderr emu, inv_gamma_pdf, 0.75, 4.00;

stderr eg, inv_gamma_pdf, 0.5, 0.4;

stderr esigma_omega, inv_gamma_pdf, 0.05, 4.00;


end;
check;
shocks;

//***********************************************
//******************SHOCKS***********************
//***********************************************

//Set of shocks, expected to be 7

var ez;              stderr .3;
var ephi;            stderr 3;
var elambda_f;       stderr .2;
var emu;             stderr .75;
var esigma_omega;    stderr .05;
var eg;              stderr .5;
var eps_R;           stderr .2;
//var 

end;

stoch_simul(periods=1000, irf=20);

estimated_params_init(use_calibration);

end;

estimation(datafile=datadelnegro2, mh_replic=20000, mh_jscale=0.038376, mode_compute=6);





