```
// Model_fttb.mod
// Upstream sector produces investment goods, downstream produces consumption. Two stochastic trends with
// shocks vu and vd. Five stages of production of investment goods, capital adjustment costs in each sector.
// July 2023
// endogenous variables
var nd,nu,n,A,Lam,gu,gd,iig,cg,kug,kdg,kappa,wipg,ndg,nug;
model_local_variable C1,C2,D1,D2,gc;
trend_var(growth_factor=gu) Bu;
trend_var(growth_factor=gd) Bd;
var(deflator=Bu) iu,id,ii,z0u,z0d,z1u,z1d,z2u,z2d,z3u,z3d,z4u,z4d,Z,wip,iw;
var(deflator=Bu(+1)) ku,kd;
var(deflator=Bu^.33*Bd^.67) c;
var(deflator=Bu^(-.67)*Bd^.67) pu,pd,qu,qd,mu;
var(deflator=Bu^(-.33)*Bd^(-.67)) chi;
varexo vu,vd;
parameters sigma,alpha,phi,del,eta,bet,gam,nupar,S,gamu,gamd,rhou,rhod,thetaud,thetadu;
S=4; //Stages of production
sigma=0.8;
//sigma=0.5;
alpha=1/(S+1);
del = 0.1;
phi = 0.33;
eta=.1716;
bet = .99;
gamu=.013; gamd=.0046;
nupar = 1;
gam = .25;
rhou=0; rhod=0;
//C1 = (del1+gu)^(1/eta); C2 = (del2+gu)^(1/eta);
//D1 = (del1+gu)/(1-eta); D2 = (del2+gu)/(1-eta);
thetaud = 0;
thetadu=0.254; thetadu=0;
//gc=(1+gamd)^.67*(1+gamu)^.33-1;
//muu=0.1; mud=0.1;
model;
// first order conditions
//growth processes
log(gu)=(1-rhou)*log(1+gamu)+rhou*log(gu(-1))+vu;
log(gd)=(1-rhod)*log(1+gamd)+rhod*log(gd(-1))+thetadu*log(Bu(-1)/Bd(-1))+thetadu*log((1+gamu)/(1+gamd))+vd;
#gc=(1+gamd)^.67*(1+gamu)^.33-1; //consumption growth
#C1 = (del+gamu)^(1/eta); #C2 = (del+gamu)^(1/eta); //capital adjustment costs calibration
#D1 = (del+gamu)/(1-eta); #D2 = (del+gamu)/(1-eta);
chi = 1/c; //u'(c)
//FOCs for id,iu,kd,ku given capital adjustment costs
pd = qd*C1*(id/kd(-1))^(-1/eta);
pu = qu*C2*(iu/ku(-1))^(-1/eta);
qd = bet*(chi(+1)/chi)*(qd(+1)*(1-del-C1/(1-eta)*(id(+1)/kd)^(1-1/eta)+D1)+phi*Lam(+1)*(Bd(+1)/Bu(+1))^(1-phi)*(kd/(Bu(+1)*nd(+1)))^(phi-1));
qu = bet*(chi(+1)/chi)*(qu(+1)*(1-del-C2/(1-eta)*(iu(+1)/ku)^(1-1/eta)+D2)+mu(+1)*phi*A(+1)*(ku/(Bu(+1)*nu(+1)))^(phi-1));
//labor supply
chi*(1-phi)*Bd*Lam*(kd(-1)/(Bd*nd))^phi=gam*n^(1/nupar);
pd*chi*(z0d/(alpha*id))^(-1/sigma)*(1-phi)*A*Bu*(ku(-1)/(Bu*nu))^phi=gam*n^(1/nupar);
//investment goods at each stage
bet^4*pu(+4)*(chi(+4)/chi)*(z4u/(alpha*iu(+4)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet^3*pu(+3)*(chi(+3)/chi)*(z3u/(alpha*iu(+3)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet^2*pu(+2)*(chi(+2)/chi)*(z2u/(alpha*iu(+2)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet*pu(+1)*(chi(+1)/chi)*(z1u/(alpha*iu(+1)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
pu*(z0u/(alpha*iu))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet*pd(+1)*(chi(+1)/chi)*(z1d/(alpha*id(+1)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet^2*pd(+2)*(chi(+2)/chi)*(z2d/(alpha*id(+2)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet^3*pd(+3)*(chi(+3)/chi)*(z3d/(alpha*id(+3)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
bet^4*pd(+4)*(chi(+4)/chi)*(z4d/(alpha*id(+4)))^(-1/sigma)=pd*(z0d/(alpha*id))^(-1/sigma);
//downstream production
c=Lam*kd(-1)^phi*(Bd*nd)^(1-phi);
//capital accumulation
kd(-1)*(1-del)+(C1/(1-1/eta)*(id/kd(-1))^(1-1/eta)+D1)*kd(-1)=kd;
ku(-1)*(1-del)+(C2/(1-1/eta)*(iu/ku(-1))^(1-1/eta)+D2)*ku(-1)=ku;
//investment from stages of production
(alpha^(1/sigma)*z0d^((sigma-1)/sigma)+alpha^(1/sigma)*z1d(-1)^((sigma-1)/sigma)+alpha^(1/sigma)*z2d(-2)^((sigma-1)/sigma)+alpha^(1/sigma)*z3d(-3)^((sigma-1)/sigma)+alpha^(1/sigma)*z4d(-4)^((sigma-1)/sigma))^(sigma/(sigma-1))=id;
(alpha^(1/sigma)*z0u^((sigma-1)/sigma)+alpha^(1/sigma)*z1u(-1)^((sigma-1)/sigma)+alpha^(1/sigma)*z2u(-2)^((sigma-1)/sigma)+alpha^(1/sigma)*z3u(-3)^((sigma-1)/sigma)+alpha^(1/sigma)*z4u(-4)^((sigma-1)/sigma))^(sigma/(sigma-1))=iu;
//upstream production
A*ku(-1)^phi*(nu*Bu)^(1-phi)=Z;
//works-in-process inventories
wip = z1u(-1)+z2u(-1)+z2u(-2)+z3u(-1)+z3u(-2)+z3u(-3)+z4u(-1)+z4u(-2)+z4u(-3)+z4u(-4)+z1d(-1)+z2d(-1)+z2d(-2)+z3d(-1)+z3d(-2)+z3d(-3)+z4d(-1)+z4d(-2)+z4d(-3)+z4d(-4);
wipg=(wip(+1)-wip)/wip;
iw = z1u-z1u(-1)+z2u-z2u(-2)+z3u-z3u(-3)+z4u-z4u(-4)+
z1d-z1d(-1)+z2d-z2d(-2)+z3d-z3d(-3)+z4d-z4d(-4);
//aggregate labor and investment
nd+nu = n;
ii=iu+id;
//growth rates of investment, consumption, labor, and capital
iig=(ii-ii(-1))/ii(-1);
cg=(c-c(-1))/c(-1);
ndg=(nd-nd(-1))/nd(-1);
nug=(nu-nu(-1))/nu(-1);
kug=(ku(+1)-ku)/ku;
kdg=(kd(+1)-kd)/kd;
//Lam=A; //This is for a shock to both Lam and A, perfectly correlated.
Lam=1; //This is if only A is shocked
A=1; //This is if only Lam is shocked
mu=pd*(z0d/(alpha*id))^(-1/sigma);
kappa=(ku(-1)+kd(-1))/n;
//kappa=(ku+kd)/n;
Z=z0u+z0d+z1u+z1d+z2u+z2d+z3u+z3d+z4u+z4d;
end;
steady_state_model;
gu=1+gamu;
gd=1+gamd;
A=1;
Lam=1;
pd=alpha^(1/(1-sigma))*(1+(gu/bet)^(1-sigma)+(gu^2*bet^(-2))^(1-sigma)+(gu^3*bet^(-3))^(1-sigma)+(gu^4*bet^(-4))^(1-sigma))^(1/(1-sigma));
pu=pd;
kappa=(pd*(gu/bet-(1-del))/phi)^(1/(phi-1));
n=(((1-phi)*kappa^phi)/(gam*(kappa^phi-((kappa*(del+gamu))*alpha)*(pd^sigma*(1+bet^sigma*gu^(1-sigma)+bet^(2*sigma)*gu^(2*(1-sigma))+bet^(3*sigma)*gu^(3*(1-sigma))+bet^(4*sigma)*gu^(4*(1-sigma)))))))^(nupar/(1+nupar));
ii=kappa*n*(del+gamu);
Z=alpha*ii*(pd^sigma*(1+bet^sigma*gu^(1-sigma)+bet^(2*sigma)*gu^(2*(1-sigma))+bet^(3*sigma)*gu^(3*(1-sigma))+bet^(4*sigma)*gu^(4*(1-sigma))));
c=kappa^phi*n-Z;
//k=kappa*n;
nd=c/kappa^phi;
nu=n-nd;
kd=kappa*nd;
ku=kappa*nu;
id=(del+gamu)*kd;
iu=(del+gamu)*ku;
z0d=id*alpha*pd^sigma;
z0u=iu*alpha*pu^sigma;
z1d=id*alpha*(pd*bet/gu)^sigma*gu;
z1u=iu*alpha*(pu*bet/gu)^sigma*gu;
z2d=id*alpha*(pd*bet^2*gu^(-2))^sigma*gu^2;
z2u=iu*alpha*(pu*bet^2*gu^(-2))^sigma*gu^2;
z3d=id*alpha*(pd*bet^3*gu^(-3))^sigma*gu^3;
z3u=iu*alpha*(pu*bet^3*gu^(-3))^sigma*gu^3;
z4d=id*alpha*(pd*bet^4*gu^(-4))^sigma*gu^4;
z4u=iu*alpha*(pu*bet^4*gu^(-4))^sigma*gu^4;
qd=pd;
qu=pu;
mu=1;
cg=gu^.33*gd^.67-1;
nug=0; ndg=0;
iig=gu-1;
kug=gu-1;
kdg=gu-1;
//gc=(1+gamd)^.67*(1+gamu)^.33-1;
chi=1/c;
wip = z1u/gu+z2u/gu+z2u/(gu^2)+z3u/gu+z3u/(gu^2)+z3u/(gu^3)+z4u/gu+z4u/(gu^2)+z4u/(gu^3)+z4u/(gu^4)+z1d/gu+z2d/gu+z2d/(gu^2)+z3d/gu+z3d/(gu^2)+z3d/(gu^3)+z4d/gu+z4d/(gu^2)+z4d/(gu^3)+z4d/(gu^4);
wipg=gu-1;
iw=wip*(gu-1);
end;
steady;
check;
shocks;
var vu; stderr 0.02;
//var vd; stderr 0.02;
end;
//stoch_simul(irf=40, periods=2000, drop=200);
stoch_simul(irf=150);
//simul(periods=100);
```