
%following are the ?endogenous ?variables:

var Y, C, L, I, K, gc, W, b, d,Cf, Ch,  a,Pf,Ph, PI, i,If, Ih, ly ll lc li lk  lgc lw  ld
  s,laI, bh, Rem, Pinv, Pif, Q, A, bc, R;
%%lb lbh lbc lp lph lpf ly, ll, lc,lcf, lch, li, lk, lgc, lw, ld ,lrem, lpif, lih, lif, lq, lr, llai, lA, lpin, lpi, lin
%following are the shocks

varexo ETA_A, ETA_D, ETA_R, ETA_RE, ETA_F, ETA_a, ETA_bc; %% ETA_pi;

parameters PHI, tau, Gamai, RHOi, DELTA, ALPHA, Phit, DELTAg, muss, muA, tauk, psi,Gamah, RHOh,RHOp, Kappa,
BETA, RHOA, RHOd, RHOr, GamaA, OMEGA,ome, RHORem,RHObc, RHOIf,gBAR, aBAR, ABAR, dBAR, RemBAR, bcBAR, RBAR, IfBAR,ISTAR, RHOpi;%%PIbar; 



%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.22; % 0.015 depreciation rate of capital
PHI = 2.2; %2.25 ou 2.2; % adjustment cost parameter (governs size of installation cost)
tau = 0.033;%0.034; % tax on labor income
Gamah=0.4; %degree of home bias in consumption (0.61)
Gamai = 0.5;% share of domestic investment in total investment
RHOi = 0.16; % elasticity of substitution between local and foreign investment
RHOh= 0.3; % 0.55 elasticity of substitution between home and imported goods
Phit = 0.4;% 0.33 share of private capital in total capital used in production
DELTAg = 0.02;% depreciation rate of public capital
muss = 0.05; % share of government spending on public investment
muA = 0.19; % 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 = 0.2; % spending speed of aid
psi = 2; % inverse of labor supply elasticity
OMEGA = 0.8;%aid absorption
ome=1/(ALPHA-DELTA+1) ; %% stochastic discount factor
RHORem = 0.078; % degree of persistance of increase in remittances
RHOIf = 0.08;  % degree of persistance of increase in foreign investment
RHObc = 0.999; % degree of persistance of increase in central bank bonds
RHOp= 0.9;
RHOpi= 0.99;
Kappa=0.0173671414;
%PIbar=1;

%%%steady state ratios and values


ABAR= 0.11;% steady state level of aid

dBAR= 0.048; % 0.22 ss level pf deposits

RemBAR= 0.16;%0.16 % ss level of remittances

%%ISTAR=  0.021; %% % world interest rate

RBAR = 0.026; % reserves target

IfBAR = 0.02; % ss level of foreign investment
bcBAR= 0.022;
laIss=1;

aBAR=0.76948979;

%%%Steady state

ass=aBAR;
Pifss=laIss;
Ifss=IfBAR;
Pinvss=((Pifss^(1-RHOi))*(1-Gamai)+Gamai)^(1/(1-RHOi));
Iss=Ifss/(1-Gamai)*((Pifss/Pinvss)^(-RHOi));
Ihss=Iss*(Gamai)*((1/Pinvss)^(-RHOi));

Kss=Iss/DELTA; %%%Iss= DELTA*Kss;
Yss=Kss*(1/ome+(DELTA-1))/(1-ALPHA);
%%%Yss=Kss*Pinvss/((1-ALPHA)/ALPHA);
sss=1;
PIss=1; %%PIss=PIbar; %%%PIss= ISTAR*BETA;
ISTAR=1/BETA;
iss= ISTAR;

Ass=ABAR;
Remss=RemBAR;
dss=dBAR; %% dBAR>RBAR
Rss=RBAR;
bcss=bcBAR;
%%%bcss=dss-sss*Rss;
gcss= tau*Yss*ALPHA + tauk*Kss +sss*Ass - (iss-1)*bcss*1/PIss; %%%-(dss-dss/PIss) -(bss-bss/PIss)
bhss= (gcss-tau*Yss*ALPHA -tauk*Kss)*1/(1-(1+iss)/(1+PIss));
gBAR= gcss; %steady state level of government spending

bss= bcss + bhss;

css= (1-tau)*Yss*ALPHA + bhss*(1/BETA-1) + sss*Remss;
lss=(css/(Yss*(1-tau)*ALPHA))^(-1/(1+psi)); 

wss=ALPHA*Yss/lss;
phss=1; %%% mean phss/pss steady state ratio of domestically produced goods prices to cpi
pfss=1; %%% mean pfss/pss steady state ratio of imported goods prices to cpi
%%pss= (Gamah*(phss^(1-RHOh)) + (1-Gamah)*(pfss^(1-RHOh)))^(1/(1-RHOh)); %% pss=5
qss=muss*gcss/DELTAg; 

chss= Gamah*((phss)^(-RHOh))*css;
cfss= (1-Gamah)*((pfss)^(-RHOh))*css;




% RBC model


model;

1/C = BETA*(1/C(+1))*(i/PI(+1));
L= ((1-tau)*W/C)^(1/psi);  
C + bh= i(-1)*bh(-1)/PI+(1-tau)*(W*L)+ s*Rem;

Ch= Gamah*((Ph)^(-RHOh))*C;

Cf= (1-Gamah)*((Pf)^(-RHOh))*C;

%%P = (Gamah*(Ph^(1-RHOh)) + (1-Gamah)*(Pf^(1-RHOh)))^(1/(1-RHOh));

PI(+1)=RHOpi*PI + Kappa*Y;

%%PI(+1)=PIbar + RHOpi*(PI-PIbar) + ETA_pi;  %%%PI= (P-P(-1))/P;

i = ISTAR + PHI*(exp(d-dBAR)-1);

s = Pf/Ph;

Ih= Gamai*((1/Pinv)^(-RHOi))*I;
If= (1-Gamai)*((Pif/Pinv)^(-RHOi))*I; %%%% Pif*((I*If/Gamai)^(1/RHOi))=Pinv %%%% 
Pinv= (Gamai + (1-Gamai)*(Pif^(1-RHOi)))^(1/(1-RHOi));
%I = ((Gamai^(1/RHOi))*Ih^((RHOi-1)/RHOi) + ((1-Gamai)^(1/RHOi))*If^((RHOi-1)/RHOi))^(RHOi/(RHOi-1));

Pinv*(1+PHI*(I/K-DELTA))= laI;
((1-ALPHA)*(Y(+1)/K(+1)) - Pinv*((PHI/2)*((I(+1)/K(+1))-DELTA)^2- PHI*((I(+1)/K(+1))-DELTA)*(I(+1)/K(+1))) + laI(+1)*(1-DELTA))*ome= laI;
K(+1) = I + (1-DELTA)*K;
Q= (1-DELTAg)*Q(-1) + muss*gBAR + muA*(gc-gBAR);
Y = a*(L^ALPHA)*((K(-1)^Phit)*(Q(-1)^(1-Phit)))^(1-ALPHA);
W=ALPHA*Y/L; %% W=Y/L;

gc = tau*(W*L) + tauk*K + s*A - (d - d(-1)/PI) + (b-b(-1)/PI) - (i(-1) -1)*bc(-1)/PI;
bh = ((1+i)/(1+PI))*((1 + (Rem(-1)*s(-1))/Y(-1))/(1 + (Rem*s)/Y))*bh(-1) - (tau*(W*L) + tauk*K -gc);

bc - bc(-1)/PI = d - d(-1)/PI - s*(R-R(-1)); 

b = bh + bc;

%%%exogenous processes

A(+1) = ABAR + RHOA*(A-ABAR) + ETA_A;

d = RHOd*d(-1) + (1-RHOd)*dBAR + (1-GamaA)*s*(A -ABAR) + ETA_D;

R = RHOr*R(-1) + (1-RHOr)*RBAR + (1-OMEGA)*(A - ABAR) + ETA_R;

Rem = RemBAR + RHORem*(Rem(-1) -RemBAR) + ETA_RE;

If = IfBAR + RHOIf*(If(-1)-IfBAR) + ETA_F;

a= aBAR + RHOp*(a(-1) - aBAR)+ ETA_a;

bc= bcBAR + RHObc*(bc(-1)-bcBAR) + ETA_bc;

%%% define log transformations of the endogenous variables

ly = log(Y);
ll = log(L);
lc = log(C);
li = log(I);
lw = log(W);
lk = log(K);
ld = log(d);
lgc= log(gc);


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= pfss;
Ph= phss;
%%P = pss;
PI=PIss;
Pinv= Pinvss;
Pif=Pifss;
Q=qss;
s=sss;
Rem=Remss;
laI=laIss;

A=Ass;

R=Rss;

a=ass;

ly = log(Yss);
ll = log(lss);
lc = log(css);
li = log(Iss);
lw = log(wss);
lk = log(Kss);
ld = log(dss);
lgc= log(gcss);

%ETA_pi=0;
ETA_A=0;
ETA_D=0;
ETA_R=0;
ETA_RE=0;
ETA_F=0;
ETA_a=0;
ETA_bc=0;
end;


shocks;
var ETA_A; stderr 0.07;
var ETA_RE; stderr 0.1;
var ETA_F; stderr 0.08;
end;

resid(1);


steady(solve_algo=0);

check;
stoch_simul(order=1,periods=0,irf=100, conditional_variance_decomposition=[1,4,8,10,100])Y L C I K Q W gc;
%%stoch_simul(order=3,periods=1000,irf=100)Y L C I K Q W gc P;
%%stoch_simul(order=1,periods=1000, hp_filter=1600,irf=100)Y L C I K Q W gc P;
%%stoch_simul(order=1,periods=1000,irf=100)ly ll lc li lk a lgc lw b ld;
%%%Notice: Here you ask Dynare to analyze the impulse responses
%%%%and the moments of the log-transformations.





