% First model in Macroeconomic Effect of Financial Shocks by Urban Jermann
% und Vincenzo Quadrini

var z xi k b n w c d y m_tilde R mu phi_d;
% z: stochastic level of productivity
% xi: the probability that the lender can recover k(+1)
% k: input of capital
% b: intertemporal liability
% n: labor input
% w: wage
% c: consumption
% d: equity payout
% y: gross revenue
% m_tilde: effective discount factor
% R: gross interest rate
% mu: lagrange multiplier
% phi_d: FOC of equity payout cost with respect to d

varexo eps_z eps_xi;
% eps_z: residual of z, productivity shock
% eps_xi: residual of xi, financial shock

parameters beta tau d_bar alpha theta delta xi_bar kappa rho_z sigma_z rho_xi sigma_xi;

beta   = 0.9825;    % discount factor
tau    = 0.3500;    % tax advantage
d_bar  = 0.1100;    % equity payout at steady state
alpha  = 1.8834;    % utility parameter
theta  = 0.3600;    % production technology
delta  = 0.0250;    % depreciation rate
xi_bar = 0.1634;    % enforcement parameter at steady state
kappa  = 0.1460;    % payout cost parameter
rho_z  = 0.9457;    % autoregressive parameter of z
sigma_z  = 0.0045;  % standard deviation of productivity shock
rho_xi = 0.9703;    % autoregressive parameter of xi
sigma_xi = 0.0098;  % standard deviation of financial shock

model;

% (1) % FOC of the equity payout cost with respect to b
exp(phi_d) = 1+2*kappa*(exp(d)-d_bar); 
% (2) % definition of discount factor
exp(m_tilde) = beta*exp(c)*exp(phi_d)/(exp(c(+1))*exp(phi_d(+1))); 
% (3) % AR equation of productivity level z
z = rho_z*z(-1)+sigma_z*eps_z; 
% (4) % AR equation of xi
xi = rho_xi*xi(-1)+sigma_xi*eps_xi; 
% (5) % gross revenue function
exp(y) = exp(z)*exp(k(-1))^theta*exp(n)^(1-theta);
% (6) % FOC of household utility function with respect to n 
exp(w)/exp(c) = alpha/(1-exp(n)); 
% (7) % FOC of household utility function with respect to b
1/exp(c)=beta*((exp(R)-tau)/(1-tau))/exp(c(+1));
% (8) % budget constraint of household 
exp(w)*exp(n)+exp(b(-1))-exp(b)/exp(R)+exp(d)-exp(c)=0;
% (9) % FOC of firm with respect to n
(1-theta)*exp(z)*exp(k(-1))^theta*exp(n)^(-theta)= exp(w)/(1-exp(mu)*exp(phi_d));
% (10) % FOC of firm with respect to k(+1)
exp(m_tilde)*(1-delta+(1-exp(mu(+1))*exp(phi_d(+1)))*theta*exp(z(+1))*exp(k)^(theta-1)*exp(n(+1))^(1-theta))+xi_bar*exp(xi)*exp(mu)*exp(phi_d)=1;
% (11) % FOC of firm with respect to b(+1)
exp(R)*exp(m_tilde)+xi_bar*exp(xi)*exp(mu)*exp(phi_d)*exp(R)*(1-tau)/(exp(R)-tau)=1;
% (12) % budget constraint of the firm 
(1-delta)*exp(k(-1))+exp(y)-exp(w)*exp(n)-exp(b(-1))+exp(b)/exp(R)-exp(k)-(exp(d)+kappa*(exp(d)-d_bar)^2)=0;
% (13) % enforcement constraint of the firm
xi_bar*exp(xi)*(exp(k)-exp(b)*(1-tau)/(exp(R)-tau))=exp(y);

end;

initval;

z = 0;
xi= 0;
k = log(10);
b = log(4);
n = log(0.3);
w = log(2);
c = log(0.8);
d = log(0.1);
y = log(1);
m_tilde = log(beta);
R = log(1/beta);
mu= log(0.04);
phi_d = 1+2*kappa*(log(0.1)-d_bar);

end;

shocks;

var eps_z; stderr 1;
var eps_xi; stderr 1;

end;

steady; 

check;

stoch_simul(order=1, irf=30) b n y d;