% First model in Macroeconomic Effect of Financial Shocks by Urban Jermann
% and 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: derivative of cost of payout wrt. 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 roh_z sig_z rho_xi sig_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
sig_z  = 0.0045; % standard deviation of productivity shock
rho_xi = 0.9703; % autoregressive parameter of xi
sig_xi = 0.0098; % standard deviation of financial shock

model;

exp(phi_d) = 1+2*kappa*(exp(d)-d_bar); %1% cost of payout phi=d+kappa*(d-d_bar)^2
exp(m_tilde) = beta*(exp(c)/exp(c(+1)))*(exp(phi_d)/exp((phi_d(+1)))); %2%
z(+1) = rho_z*z+sig_z*eps_z(+1); %3%
xi(+1) = rho_xi*xi+sig_xi*eps_xi(+1); %4%
exp(y) = exp(z)*exp(k(-1))^theta*exp(n)^(1-theta); %5% gross revenue function
exp(w)/exp(c) = alpha/(1-exp(n)); %6% FOC of household utility function wrt. n
1/exp(c)=beta*((exp(R)-tau)/(1-tau))/exp(c(+1)); %7% FOC of household utility function wrt. b
exp(w)*exp(n)+exp(b)-exp(b(+1))/exp(R)+exp(d)-exp(c)=0; %8% budget constraint of household
(1-theta)*exp(z)*exp(k(-1))^theta*exp(n)(-theta)= exp(w)/(1-exp(mu)*exp(phi_d)); %9% FOC of firm wrt. n
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;
%10% FOC of firm wrt. k(+1)
exp(R)*exp(m_tilde)+xi_bar*exp(xi)*exp(mu)*exp(phi_d)*exp(R)*(1-tau)/(exp(R)-tau)=1; %11% FOC of firm wrt. b(+1)
(1-delta)*exp(k)+exp(y)-exp(w)*exp(n)-exp(b)+exp(b(+1))/exp(R)-exp(k(+1))-(exp(d)+kappa*(exp(d)-d_bar)^2)=0;
%12% budget constraint of the firm
xi_bar*exp(xi)*(exp(k(+1))-exp(b(+1))*(1-tau)/(exp(R)-tau))=exp(y);
%13% enforcement constraint of the firm
 
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_stimul(order=1, irf=20) b n d y;

