
%following are the ?endogenous ?variables:

var Y, C, L, I, K, gc, W, b, d,Cf, Ch, ly, ll, lc,lcf, lch, li, lk, a, lgc, lw, lb, ld ,P,Pf,Ph, PI, i,If, Ih 
  s,laI, bh, Rem, Pinv, Pif, Q, A, bc, R, lp,lpf, lph, lbh, lrem, lpif, lih, lif, lq, lbc, lr, llai, lA, lpin, lpi, lin; %% G lg

%following are the shocks

varexo ETA_A, ETA_D, ETA_R, ETA_RE, ETA_F, ETA_P;

parameters PHI, tau, Gamai, RHOi, DELTA, ALPHA, Phit, DELTAg, muss, muA, tauk, psi,Gamah, RHOh,
BETA, RHOA, RHOd, RHOr, GamaA, OMEGA, RHORem, RHOIf, RHOf, gBAR, ABAR, dBAR, bcBAR, RemBAR, RBAR, IfBAR, PifBAR,ISTAR,gg ; 



%Calibrated parameters or initial values of estimated parameters

ALPHA= 0.76; % 0.75 share of labor in production function
BETA= 0.98; % discount factor
DELTA= 0.016; % 0.015 depreciation rate of capital
PHI = 2.2; %2.25; % adjustment cost parameter
tau = 0.033;%0.034; % tax on labor income
Gamah=0.6; %degree of home bias in consumption
Gamai = 0.65;% share of domestic investment in total investment
RHOi = 0.16; % elasticity of substitution between local and foreign investment
RHOh= 0.22; % 0.55 elasticity of substitution between home and imported goods
Phit = 0.33;% 0.33 share of private capital in total capital used in production
DELTAg = 0.02;% depreciation rate of public capital
muss = 0.1; % share of government spending on public investment
muA = 0.092; % public investment related to increase in aid
tauk = 0.02; %tax on capital
RHOA = 0.9; %degree of persistence of increase in aid
RHOd = 0.089;% deposit drawn down rate
RHOr = 0.9; % degree of commitment to depreciation target in reserves
GamaA = 1; % spending speed of aid
psi = 2; % inverse of labor supply elasticity
OMEGA = 0.8; %aid absorption
RHORem = 0.078; % degree of persistance of increase in remittances
RHOIf = 0.08;  % degree of persistance of increase in foreign investment
RHOf = 0.999; % degree of persistance of increase in investment price
gg= 0; % growth rate of output

%%%steady state ratios and values

gBAR= 0.16; %steady state level of government spending

ABAR= 0.11;% steady state level of aid

dBAR= 0.022; % 0.22 ss level pf deposits

bcBAR= 0.2; % 0.7ss level of debt

RemBAR= 0.16;%0.16 % ss level of remittances

ISTAR=  0.021; %% % world interest rate

PifBAR= 2.2; % 50 minimum cost of investment

RBAR = 0.26; % reserves target

IfBAR = 0.079; % ss level of foreign investment


%Steady state^

PIss=1; % ss inflation
ass=1; %% ss fact. productivity
dss=dBAR;
phss=1.5; %%% mean phss/pss steady state ratio of domestically produced goods prices to cpi
pfss=1.08; %%% mean pfss/pss steady state ratio of imported goods prices to cpi
pss=5; %%pss= (Gamah*(phss^(1-RHOh)) + (1-Gamah)*(pfss^(1-RHOh)))^(1/(1-RHOh));
sss=phss/pfss; %%sss=1
iss= ISTAR;
Pifss=PifBAR;
Ifss=IfBAR; % ratio of foreign investment to total investment
%%Ihss=1-Ifss; % ratio of domestically investment to total investment
%%Pinvss=(Ihss/Gamai)^(1/RHOi);
%%Pifss=((Pinvss^(1-RHOi)-Gamai)/(1-Gamai))^(1/(1-RHOi));
Pinvss=((Pifss^(1-RHOi))*(1-Gamai)+Gamai)^(1/(1-RHOi));
Iss=Ifss/(1-Gamai)*((Pifss/Pinvss)^(-RHOi));
Ihss=Iss*(Gamai)*((1/Pinvss)^(-RHOi));

laIss = Pinvss;
Rss=RBAR;
Ass=ABAR;
dss=dBAR;

%%Pinvss=(Ihss/Gamai)^(1/RHOi); %%%Ihss/Iss=(Gamai)*((1/Pinvss)^(-RHOi));
%%Pifss= ((Ifss/(1-Gamai))^(-1/RHOi))*Pinvss; %%%Ifss=(1-Gamai)*((Pifss/Pinvss)^(-RHOi));%%% on exprime ce ratio Ifss/Iss=(1-Gamai)*((Pifss/Pinvss)^(-RHOi))
PIss=iss*BETA;
Kss=Iss/DELTA;
Yss=Kss*DELTA*laIss/(1-ALPHA);
bcss=bcBAR;
bhss= (sss*Ass -(iss-1)*bcss)/(1-(1+iss)/(2+2*gg));%%%bhss=bhBAR
Remss=RemBAR;
css=(1-tau)*Yss + bhss*(1/BETA -1)+ sss*Remss; %%(1-tau)*Wss*lss + bhss*(1/BETA -1)+ sss*Remss= (1-tau)*Wss/lss^psi; 
lss= ((1-tau)*ALPHA*Yss/css)^(1/(1+psi));      %%%css=(1-tau)*Wss/(lss^psi);
Wss=(ALPHA)*Yss/lss;
gcss= tau*Wss*lss + tauk*Kss +sss*Ass - (iss-1)*bcss;
qss=muss*gcss/DELTAg;
bss= bhss+bcss;

 
%%%%%%additional ss equations

chss= Gamah*((phss)^(-RHOh))*css;
cfss= (1-Gamah)*((pfss)^(-RHOh))*css;


% RBC model


model;

Ch= Gamah*((Ph/P)^(-RHOh))*C;

Cf= (1-Gamah)*((Pf/P)^(-RHOh))*C;

P = (Gamah*(Ph^(1-RHOh)) + (1-Gamah)*(Pf^(1-RHOh)))^(1/(1-RHOh));

P(-1)*PI=P; %%%%PI= P/P(-1)

i = ISTAR + PHI*(exp(d-dBAR)-1);

s = Pf/Ph;

PI*C = i(-1)*bh(-1)+PI*( (1-tau)*(W*L)-bh + s*Rem);

L^psi= (1-tau)*W/C;

PI(+1)/C = BETA*(i)*(1/C(+1));

Pinv= (Gamai + (1-Gamai)*(Pif^(1-RHOi)))^(1/(1-RHOi));

Ih= Gamai*((1/Pinv)^(-RHOi))*I;

Pif*((I*If/Gamai)^(1/RHOi))=Pinv;    %%%% If= (1-Gamai)*((Pif/Pinv)^(-RHOi))*I; %%%%

%I = ((Gamai^(1/RHOi))*Ih^((RHOi-1)/RHOi) + ((1-Gamai)^(1/RHOi))*If^((RHOi-1)/RHOi))^(RHOi/(RHOi-1));

K(+1) = I + (1-DELTA)*K;

Y = a*(L^ALPHA)*((K^Phit)*(Q(-1)^(1-Phit)))^(1-ALPHA);

Q= (1-DELTAg)*Q(-1) + muss*gBAR + muA*(gc-gBAR);

%G=Y/Y(-1);

b = bh + bc;

bh = ((1+i)/(1+PI)*(1+gg))*((1 + (Rem(-1)*s(-1))/Y(-1))/(1 + (Rem*s)/Y))*bh(-1) - (tau*(W*L) + tauk*K -gc);

PI*gc = PI*tau*(W*L) + PI*tauk*K + PI*s*A - PI*d - d(-1) + PI*b-b(-1) - (i(-1) -1)*bc(-1); %%% gc = tau*(W*L) + tauk*K + s*A - (d - d(-1)/PI) + (b-b(-1)/PI) - (i(-1) -1)*bc(-1)/PI

PI*bc - bc(-1) = PI*d - d(-1) - s*(R-R(-1))*PI; 


Pinv*(1+PHI*(I/K-DELTA))= laI;
((1-ALPHA)*(Y(+1)/K(+1)) - Pinv(+1)*((PHI/2)*(I(+1)/K(+1)-DELTA)^2- PHI*(I(+1)/K(+1)-DELTA)*I(+1)/K(+1)) + laI(+1)*(1-DELTA))= laI;

W=ALPHA*Y/L;

PI*(A + Rem - I) = PI*(C + gc - Y) - (i(-1) -1)*bc(-1) + PI*bh - bh(-1) + PI*R- R(-1); %%current account or tb


%%%exogenous processes

A(+1) = ABAR + RHOA*(A-ABAR) + ETA_A;

d(+1) = RHOd*d + (1-RHOd)*dBAR + (1-GamaA)*s*(A(+1) -ABAR) + ETA_D;

R(+1) = RHOr*R + (1-RHOr)*RBAR + (1-OMEGA)*(A(+1) - ABAR) + ETA_R;

Rem(+1) = RemBAR + RHORem*(Rem -RemBAR) + ETA_RE;

If(+1) = IfBAR + RHOIf*(If-IfBAR) + ETA_F;

Pif(+1) = PifBAR + RHOf*(Pif - PifBAR) + ETA_P;%%%Pif(+1) = Pifss + RHOf*(Pif - Pifss) + ETA_P;


%%% define log transformations of the endogenous variables

ly = log(Y);
ll = log(L);
lc = log(C);
lin=log(i);
li = log(I);
lw = log(W);
lk = log(K);
lch= log(Ch);
lcf= log(Cf);
lpi= log(PI);
lp = log(P);
lpf= log(Pf);
lph= log(Ph);
lbh= log(bh);
lrem=log(Rem);
lpin=log(Pinv);
lpif=log(Pif);
lih=log(Ih);
lif=log(If);
lq = log(Q);
lb = log(b);
lbc= log(bc);
ld = log(d);
lr = log(R);
llai= log(laI);
lgc= log(gc);
lA = log(A);
%lg = log(G);


end;


%%%%Initial guesses for the computation of steady state

initval;
C=css;
Cf=cfss;
Ch=chss;
L=lss;
K=Kss;
Y=Yss;
I=Iss;
If=Ifss;
Ih=Ihss;
W=Wss;
i=iss;
gc= gcss;
b = bss;
bc=bcss;
bh=bhss;
d = dss;
Pf= pss*pfss;
Ph= pss*phss;
P = pss;
PI=PIss;
Pinv= Pinvss;
Pif=Pifss;
Q=qss;
s=sss;
Rem=Remss;
laI=laIss;
A=ABAR;
%G=gg;
R=Rss;
%lg=log(gg);
lA=log(ABAR);
lr=log(Rss);
llai=log(laIss);
lc=log(css);
lcf=log(cfss);
lch=log(chss);
ll=log(lss);
lk=log(Kss);
ly=log(Yss);
li=log(Iss);
lif=log(Ifss);
lih=log(Ihss);
lw=log(Wss);
lgc = log(gcss);
lb = log(bss);
lbc=log(bcss);
lbh=log(bhss);
ld =log(dss);
lpf= log(pss*pfss);
lph= log(pss*phss);
lp =log(pss);
lpin=log(Pinvss);
lpif=Pifss;
lq=log(qss);
lrem=log(Remss);

a=ass;

ETA_A=0;
ETA_D=0;
ETA_R=0;
ETA_RE=0;
ETA_F=0;
ETA_P=0;
end;


shocks;
var ETA_A; stderr 0.07;
var ETA_RE; stderr 0.32;
var ETA_F; stderr 0.08;
end;

steady(solve_algo=0); check;
stoch_simul(order=1,periods=100,irf=100)ly ll lc li lk a lgc lw lb ld;
%%%Notice: Here you ask Dynare to analyze the impulse responses
%%%%and the moments of the log-transformations.





