
//% This is a model for analyzing the transmission mechanism with emphasis 
//% on the credit channels and the role of house prices along the line of 
//% Iacoviello (AER, 2005).




//% Declaration of endogenous variable
//% basic endogeneous variables 
//% marginal utilitis, allocations to K, prices to pi, Endogenous variables with exogenous dynamics

// notation Savers '1' borrowers '2'

Var uh1 uh2 uc1 uc2 un1 un2 h1 h2 H c1 c2 C N n1 n2 Y b1 b2 K w psi r pic pih A q;

//% Declaration of exogeneous variables (shocks)

varexo
eA;


//% deep parameters
parameters beta1 beta2 delta alpha gamma m eta rho v sigma R omega sigmaA;
 

//% Setting of numerical values for parameters



beta1 = 0.99;
beta2 = 0.95;
delta = 0.03;
alpha = 0.66;
m = 0.89;
gamma = 0.6;
rho = 0.03;
eta = 0.5;
sigma = 1.01;
R = 1/beta1;
v=0.9;
omega=0.3;
sigmaA = 2.24;

model;
//% Savers

//% 1 savers capital euler (r(+1) known at time t)

exp(uc1) = beta1*exp(uc1(+1))*(exp(r)+1-delta);

//% 2 savers bonds euler

exp(uc1) = beta1*exp(uc1(+1))*(exp(R)/exp(pic(+1)));

//% 3 savers unit price of housing

exp(uh1)+beta1*exp(uc1(+1))*exp(q(+1)) = exp(uc1)*exp(q);

//% 4 savers marginal utilty of consumption

exp(uc1) = (((1-gamma)^(1/eta))*exp(c1)^(-1/eta))/((((1-gamma)^(1/eta))*exp(c1)^(eta-1/eta))+(((gamma)^(1/eta))*exp(h1)^(eta-1/eta)));

//% 5 savers marginal utilty of housing

exp(uh1) = (((gamma)^(1/eta))*exp(h1)^(-1/eta))/((((1-gamma)^(1/eta))*exp(c1)^(eta-1/eta))+(((gamma)^(1/eta))*exp(h1)^(eta-1/eta)));

//% 6 savers marginal disutility of working

exp(un1) = -v*exp(n1)^(sigma);

//% 7 savers consumption working trade off

exp(un1) = exp(uc1)*exp(w);


//% 8 savers resource constraint (CHECK HOUSING TIMING ASSUMPTION!!)

exp(c1)+exp(q)*(exp(h1)-exp(h1(-1)))+exp(R(-1))*exp(b1(-1))/exp(pic)+exp(K) = exp(b1)+exp(w)*exp(n2)+(exp(r(-1))+(1-delta))*exp(K(-1));


//% borrowers

//% 9 borrowers bonds euler


//% problems with :: R*psi = 1-beta2*uc2(+1)/uc2*(R/pic(+1));

exp(psi) = exp(uc2)-beta2*exp(uc2(+1))*exp(R)/exp(pic(+1));

//% 10 borrowers unit price of housing

exp(uh2)+beta2*exp(uc2(+1))*exp(q(+1))+m*(exp(q)/exp(R))*exp(psi)*exp(pih) = exp(uc2)*exp(q);

//% 11 borrowers consumption working trade off

exp(un2) = exp(uc2)*exp(w);

//% 12 borrowers marginal utilty of consumption

exp(uc2) = (((1-gamma)^(1/eta))*exp(c2)^(-1/eta))/((((1-gamma)^(1/eta))*exp(c2)^(eta-1/eta))+(((gamma)^(1/eta))*exp(h2)^(eta-1/eta)));

//% 13 borrowers marginal utilty of housing

exp(uh2) = (((gamma)^(1/eta))*exp(h2)^(-1/eta))/((((1-gamma)^(1/eta))*exp(c2)^(eta-1/eta))+(((gamma)^(1/eta))*exp(h2)^(eta-1/eta)));

//% 14 borrowers marginal disutility of working

exp(un2) = -v*exp(n2)^(sigma);

//% 15 borrowers resource constraint (CHECK HOUSING TIMING ASSUMPTION!!)

exp(c2)+exp(q)*(exp(h2)-exp(h2(-1)))+exp(R(-1))*exp(b2(-1))/exp(pic) = exp(b2)+exp(w)*exp(n2);

//% 16 borrowers borrowing constaint 

exp(b2) = m*exp(q(+1))*exp(h2)*(exp(pih(+1)))/exp(R);

//% firms

//% 17 firms production function

exp(Y)=exp(A)*exp(K)^(alpha)*exp(N)^(1-alpha);

//% 18 firms wage equation 

exp(w)=(1-alpha)*exp(Y)/exp(N);

//% 19 firms return on capital equation 

exp(r)=(alpha)*exp(Y)/exp(K);

//% aggregate relationships

//% 20 aggregate resource constaint

exp(Y)=exp(c1)+exp(c2)+exp(K)-(1-delta)*exp(K(-1));

//% 21 aggregate labour supply

exp(N)=omega*exp(n1)+(1-omega)*exp(n2);

//% 22 aggregate consumption

exp(C)=exp(c1)+exp(c2);

//% 22,23 aggregate housing stock (inelastic supply, normalised to 1)

exp(H) = exp(h1)+exp(h2);

exp(H) = 1;

//% 24 aggregate bonds (supply matches demand)

0=exp(b1)+exp(b2);

//% exogenous AR(1)-dynamics

//% 25 technology shocks to A

exp(A)=rho*exp(A(-1))+exp(eA);

end;                       

//% End of model block



//% calculation of steady state

//% initval;
//% A=0;
//% c1 = 5 ;
//% c2 = 0.5 ;
//% h1 = 0.6;
//% h2 = 0.4;
//% uc1 = 1; 
//% uc2 = 1;
//% un1 = 1;
//% un2 = 1;
//% N = 2;
//% Y = 12;
//% K = 20;
//% b1 = 1;
//% b2 = 1;
//% pic = 1.05;
//% pih = 1.05;
//% n1 = 1;
//% n2 = 2;


maxit_=20; 
steady;
solve_algo =0 ;
check;

shocks;

var eA;
stderr sigmaA;
end;

stoch_simul(irf=20) R pic q Y;