var w C_d C C_i C_fd C_md C_fi C_mi gama_dc gama_ic gama_md gama_fd gama_fi gama_mi gama_sd pi_c pi_d pi_i pi_fd pi_md pi_mi pi_fi gama_estr r_d k q i R_K mb Y_oil Y_Fd Y_Md L_f K_f K_m L_m mc_F mc_M mc_f_i mc_m_i rer x delta_ex pi_e g dc si b T t_d t_vat y oil im fr liqu pi_tar pi_str y_str A_LF a A_LM eps_beta eps_i eps_m lamda_F lamda_M lamda_f_i lamda_m_i tau_trf_fi tau_trf_mi eps_liqu; % eps_l lamda_w L gini TG EG E_star eps_gini omega

varexo  e_beta e_i e_m e_a e_aml e_f e_f_i e_m_i e_tau_trf_fi e_tau_trf_mi e_g e_dc e_oil e_pi_tar e_liqu e_delta_ex e_pi_str e_y_str  e_lf  e_kf e_km e_afl e_b; % e_w e_l  e_lm 

parameters etac etad etai alphac alphadf alphaif gama_fibar gama_mibar gama_fdbar gama_mdbar gama_dcbar gama_icbar h beta delta sigma_c phi R_kbar sigma_m r_dbar alphafd alphamd rho_a rho_afl rho_aml rho_F rho_M rho_f_i rho_m_i rho_tau_trf_fi rho_tau_trf_mi theta_F theta_M theta_f_i theta_m_i eta_str rho_g rho_dc t_dbar_tbar t_vatbar_tbar tau_d tau_vat rho_oil t_gbar oil_gbar dc_gbar b_gbar sibar dc_gbar_mbbar frbar_mbbar rho_theta rho_pi_tar rho_eps_liqu varpi_pi varpi_y varpi_rer c_ybar i_ybar g_ybar rho_beta rho_i rho_m rho_pi_str rho_y_str yf_ybar ym_ybar oil_ybar cf_imbar cm_imbar oil_frbar pe_xx_frbar im_frbar x_ybar im_ybar k0 k1 k2 rho_lm rho_lf rho_kf rho_km rho_b psi; % psi P_Cbar


psi=0.11;	% ??? ??? ???? ????? ???? ?? ????? ???????????
etac=1.5; 	% ??? ??????? ??????? ????? ? ???????
etad=0.3; 	% ??? ??????? ??????? ????? ????? ? ??????? ???????? ?????
etai=0.25; 	% ??? ??????? ??????? ????? ??????? ? ??????? ???????? ???????
alphac=0.9; 	% ??? ???? ??????? ????? ?? ?? ????
alphadf=0.7; 	% ???? ???? ???? ????? ????? ?? ?? ???? ?????
alphaif=0.14; 	% ???? ??????? ????? ??????? ?? ?? ??????
gama_dcbar=0.9849; 	% ???? ???? ???? ?????????? ?? ???? ???? ???? ?????
gama_icbar=0.9357; 	% ???? ???? ???? ??????? ?? ???? ???? ???? ?????
gama_fdbar=0.9; 	% ???? ???? ???? ????????? ????? ?? ???? ???? ??????????
gama_mdbar=0.9; 	% ???? ???? ???? ???????????? ????? ?? ???? ???? ??????????
gama_fibar=0.92; 	% ???? ???? ???? ????????? ??????? ?? ???? ???? ???????
gama_mibar=0.94; 	% ???? ???? ???? ???????????? ??????? ?? ???? ???? ???????
h=0.3; 	% ???? ??????? ????? ?????
beta=0.97; 	% ??? ????? ?????
delta=0.0139; 	% ??? ??????? ??????
sigma_c=1.571; 	% ??? ??? ??????? ??? ???? ?? ????
phi=3.943; 	% ??? ???? ????? ????? ?????? ?????
R_kbar=0.046; 	% ??? ?????? ??????
sigma_m=1.315; 	% ??? ??? ????? ????? ???
r_dbar=13; 	% ????? ??? ???? ????
theta_F=0.5; 	% ???? ????? ??? ?????????? ???? ????? ?? ???? ?? ????? ???? ??? ??????
theta_M=0.75; 	% ???? ????? ??? ?????????? ???? ???????? ?? ???? ?? ????? ???? ??? ??????
theta_f_i=0.2; 	% ???? ????? ??? ????????? ???? ????? ?? ???? ?? ????? ???? ??? ??????
theta_m_i=0.2; 	% ???? ????? ??? ????????? ???? ???????? ?? ???? ?? ????? ???? ??? ??????
eta_str=3.5; 	% ??? ??????? ??? ??????? ????? ? ??????? ?? ??????? ?????
sibar=0.8; 	% 
t_gbar=0.3; 	% ??? ???????? ??????? ?? ????? ????
oil_gbar=0.4; 	% ??? ???????? ???? ?? ????? ????
b_gbar=0.3; 	% ??? ????? ?????? ?? ????? ????
dc_gbar=0.31; 	% ???? ???? ???? ???? ?? ????? ????
dc_gbar_mbbar=0.113; 	% ???? ???? ???? ???? ?? ???? ????
c_ybar=0.51; 	% ???? ???? ?? ?????
i_ybar=0.321; 	% ???? ?????? ?? ?????
g_ybar=0.12; 	% ???? ????? ???? ?? ?????
varpi_pi=-1.548; 	% ???? ????? ???? ?? ???? ??? ????? ????? ????
varpi_y=-1.7; 	% ???? ????? ????? ?? ???? ??? ????? ????? ????
varpi_rer=0.8; 	% ???? ????? ??? ??? ????? ?? ???? ??? ????? ????? ????
t_dbar_tbar=0.8404; 	% ???? ????????? ?????? ?? ?? ??????
t_vatbar_tbar=0.1596; 	% ???? ?????? ?? ???? ?????? ?? ?? ??????
tau_d=0.738; 	% ??? ???????? ?????? ?????? ???? ?? ?????
tau_vat=0.65; 	% ??? ?????? ?? ???? ?????? ???? ?? ?? ????
frbar_mbbar=0.502; 	% ???? ???? ?????? ??? ????? ???? ????? ?? ???? ????
alphafd=0.58; 	% ??? ????? ??? ?? ????? ???? ?????
alphamd=0.54; 	% ??? ????? ??? ?? ????? ???? ????????
yf_ybar=0.13; 	% ???? ????? ????????? ?? ?? ?????
ym_ybar=0.67; 	% ???? ????? ???????????? ?? ?? ????? 
oil_ybar=0.2; 	% ???? ???? ?????? ??? ??? ?? ?? ?????
cf_imbar=0.5; 	% ???? ???? ???? ????? ?? ?? ??????
cm_imbar=0.5; 	% ???? ???? ???? ???????? ?? ?? ??????
oil_frbar=1.681; 	% ???? ?????? ???? ?? ???? ?????? ??? ???? ?????
pe_xx_frbar=0.5125; 	% ???? ?????? ??? ???? ?? ???? ?????? ??? ???? ?????
im_frbar=1.2873; 	% ???? ?????? ?? ?? ???? ?????? ??? ???? ?????
x_ybar=0.08; 	% ???? ?????? ??? ???? ?? ????? ??
im_ybar=0.234; 	% ???? ?????? ?? ?? ???????
k0=0.9; 	% ???? ?????? ?????????? ?? ???? ??? ????? ???? 
k1=-1.9; 	% ???? ????? ???? ?? ???? ??? ????? ???? 
k2=-1.55; 	% ???? ????? ???? ????? ????? ?? ???? ???? ?? ???? ??? ????? ???? 
rho_a=0.764; 	% ???? ?????? ?????????? ????? ????????
rho_afl=0.92; 	% ???? ?????? ?????????? ????? ???????? ????? ??? ???? ?????
rho_aml=0.94; 	% ???? ?????? ?????????? ????? ???????? ????? ??? ???? ????????
rho_F=0.48; 	% ???? ?????? ?????????? ????-?? ???? ???? ?????
rho_M=0.5; 	% ???? ?????? ?????????? ????-?? ???? ???? ????????
rho_f_i=0.85; 	% ???? ?????? ?????????? ???? ??????? ???? ?????
rho_m_i=0.8; 	% ???? ?????? ?????????? ???? ??????? ???? ????????
rho_tau_trf_fi=0.84; 	% ???? ?????? ?????????? ????? ????? ??? ????? ??????? ???? ?????
rho_tau_trf_mi=0.8; 	% ???? ?????? ?????????? ????? ????? ??? ????? ??????? ???? ????????
rho_g=0.66; 	% ???? ?????? ?????????? ????? ????? ????
rho_dc=0.7; 	% ???? ?????? ?????????? ????? ???? ???? ?? ???? ?????
rho_oil=0.249; 	% ???? ?????? ?????????? ????? ????? ????
rho_theta=0.397; 	% ???? ?????? ?????????? ????? ???? ?? ???? ??? ????? ????? ????
rho_pi_tar=0.817; 	% ???? ?????? ?????????? ????? ???? ???
rho_eps_liqu=0.556; 	% ???? ?????? ?????????? ????? ????
rho_beta=0.66; 	% ???? ?????? ?????????? ????? ???????
rho_i=0.6; 	% ???? ?????? ?????????? ????? ????????????
rho_m=0.554; 	% ???? ?????? ?????????? ?????? ???
rho_pi_str=0.8; 	% ???? ?????? ?????????? ????? ???? ?????
rho_y_str=0.8; 	% ???? ?????? ?????????? ????? ????? ?????
rho_b=0.62; 	% ???? ?????? ?????????? ?????
rho_lm=0.9; 	% ???? ?????? ?????????? ????? ??? ???????
rho_lf=0.8; 	% ???? ?????? ?????????? ????? ??? ??? ???????
rho_km=0.8; 	%
rho_kf=0.9; 	%

%P_Cbar=10;	% ????? ???? ???? ??

model(linear);
% total consumption euation and its details;
C_d=C-etac*gama_dc;
gama_dc=pi_d-pi_c+ gama_dc(-1);

C_i=C-etac*gama_ic;
gama_ic=pi_i-pi_c+ gama_ic(-1);

C_fd=C_d-etad*gama_fd;
gama_fd=pi_fd-pi_d+ gama_fd(-1);

C_md=C_d-etad*gama_md;
gama_md=pi_md-pi_d+ gama_md(-1);

C_fi=C_i-etai*gama_fi;
gama_fi=pi_fi-pi_i+gama_fi(-1);

C_mi=C_i-etai*gama_mi;
gama_mi=pi_mi-pi_i+gama_mi(-1);

% Inflation equations and its details;

pi_c=alphac*(gama_dcbar^(1-etac))*pi_d+(1-alphac)*(gama_icbar^(1-etac))*pi_i;
pi_d=alphadf*(gama_fdbar^(1-etad))* pi_fd +(1-alphadf)*(gama_mdbar^(1-etad))* pi_md;
pi_i= alphaif*(gama_fibar^(1-etai))* pi_fi +(1-alphaif)*(gama_mibar^(1-etai))*pi_mi;

% Houshold first order conditions;
C=(h/(1+h))*C(-1)+(1/(1+h))*C(+1)-((1-h)/(sigma_c*(1+h)))*(r_d- pi_c(+1))+eps_beta;
k= (1-delta)*k(-1)+delta*(i+phi*eps_i);
q= ((1-delta)/(1-delta+R_kbar))*q(+1)+(R_kbar/(1-delta+R_kbar))*R_K(+1)-(r_d- pi_c(+1));
i=(1/(1+beta))*i(-1)+(beta/(1+beta))*i(+1)+(1/(phi*(1+beta)))*q+eps_i;

eps_beta=rho_beta*eps_beta(-1)+e_beta;
eps_i=rho_i*eps_i(-1)+e_i;

% Money demand;
sigma_m*mb=sigma_c*(C-h*C(-1))-(1/r_dbar)*r_d+eps_m;
eps_m=rho_m*eps_m(-1)+e_m;

% Real wage
% w=(beta/(1+beta))*w(+1)+(1/(1+beta))*w(-1)+(beta/(1+beta))* pi_c(+1)-((1+beta*tau_w)/(1+beta))* pi_c+(tau_w/(1+beta))* pi_c(-1)-(1/(1+beta))*(((1-beta*theta_w)*(1-theta_w))/((1+(1+lamdaw_bar)*sigma_l/lamdaw_bar)*theta_w))*(w-sigma_l*L-(sigma_c/(1-h))*(c-h*c(-1))-eps_l-lamda_w);
% eps_l=rho_l*eps_l(-1)+e_l; 
% lamda_w=rho_w*lamda_w(-1)+e_w;

 L_f =-w+(1+psi)*R_K+ K_f(-1);
 L_m =-w+(1+psi)*R_K+ K_m(-1);
R_K=i-k(-1)+gama_sd;
gama_sd=pi_c-pi_d+gama_sd;

% L=a*L_f+(1-a)*L_m
% K=

% Gini Function
% Gini=alpha1*TG+alpha2*E_star+alpha3*EG+alpha4*Omega+eps_gini;
%  eps_gini=rho_gini* eps_gini(-1)+e_gini;

% production and productivity;
y=yf_ybar*Y_Fd+ ym_ybar*Y_Md+ oil_ybar*Y_oil;
Y_oil=rer+oil;
Y_Fd=a+alphafd*(A_LF+L_f)+(1-alphafd)*K_f(-1);
Y_Md=a+alphamd*(A_LM+L_m)+(1-alphamd)*K_m(-1);

a=rho_a*a(-1)+e_a;
A_LF=rho_afl* A_LF(-1)+e_afl;
A_LM=rho_aml* A_LM(-1)+e_aml;

% Philips curves and marginal cost for domestic product;
pi_fd=(beta/(1+beta))* pi_fd(+1)+(1/(1+beta))* pi_fd(-1)+(1/(1+beta))*(((1-beta*theta_F)*(1-theta_F))/theta_F)*mc_F+lamda_F;

pi_md=(beta/(1+beta))*pi_md(+1)+(1/(1+beta))*pi_md(-1)+(1/(1+beta))*(((1-beta*theta_M)*(1-theta_M))/theta_M)*mc_M+lamda_M;

mc_F=-a+alphafd*(w)+ (1-alphafd)*R_K-alphafd*A_LF;
mc_M=-a+alphamd*(w)+ (1-alphamd)*R_K-alphamd*A_LM;
lamda_F=rho_F* lamda_F(-1)+e_f;
lamda_M=rho_M* lamda_M(-1)+e_m;
% Philips curves and marginal cost for imports;
pi_fi=(beta/(1+beta))*pi_fi(+1)+(1/(1+beta))*pi_fi(-1)+(1/(1+beta))*(((1-beta*theta_f_i)*(1-theta_f_i))/theta_f_i)*mc_f_i+lamda_f_i;

lamda_f_i=rho_f_i* lamda_f_i(-1)+e_f_i;
mc_f_i=tau_trf_fi+rer-gama_fi;
tau_trf_fi=rho_tau_trf_fi* tau_trf_fi(-1)+e_tau_trf_fi;

pi_mi=(beta/(1+beta))*pi_mi(+1)+(1/(1+beta))*pi_mi(-1)+(1/(1+beta))*(((1-beta*theta_m_i)*(1-theta_m_i))/theta_m_i)*mc_m_i+lamda_m_i;

lamda_m_i=rho_m_i* lamda_m_i(-1)+e_m_i;
mc_m_i=tau_trf_mi+rer-gama_mi;
tau_trf_mi=rho_tau_trf_mi* tau_trf_mi(-1)+e_tau_trf_mi;

% real exchange;
rer=delta_ex+pi_str- pi_c +rer(-1);

% demand export;
x=y_str-eta_str*gama_estr;
gama_estr=pi_e-pi_str+gama_estr(-1);
pi_e=pi_d-delta_ex;

% government;
g= rho_g*g(-1)+e_g;
dc= rho_dc*dc(-1)+e_dc;
g=t_gbar*T+oil_gbar*(rer+oil)+dc_gbar*(dc-dc(-1))+b_gbar*b-b_gbar*sibar*(si+b(-1));
%sibar*P_Cbar*(si+pi_c)=(r_dbar/(1+r_dbar))*r_d;
b= rho_b*b(-1)+e_b;

% Tax and oil revenue;
T=(t_dbar_tbar)*t_d+(t_vatbar_tbar)*t_vat;
t_d=tau_d*y;
t_vat=tau_vat*(C);
oil= rho_oil*oil(-1)+e_oil;

% Monetary policy;
liqu=rho_theta*liqu(-1)+varpi_pi*( pi_c-pi_tar)+varpi_y*y+varpi_rer*rer+eps_liqu;
liqu=mb-mb(-1)+ pi_c;
pi_tar=rho_pi_tar*pi_tar(-1)+e_pi_tar;
eps_liqu=rho_eps_liqu*eps_liqu(-1)+e_liqu;

% exchange rate reaction function;
delta_ex=k0*delta_ex(-1)+k1*( pi_c -pi_tar)+k2*(rer+fr-mb)+e_delta_ex;

% the aggregate resource constraint
y=c_ybar*C+i_ybar*i+ g_ybar*g+x_ybar*(rer+x+gama_estr) +oil_ybar*(rer+oil)-im_ybar*( im+rer);

% foreign countries;
pi_str=rho_pi_str*pi_str(-1)+e_pi_str;
y_str=rho_y_str*y_str(-1)+e_y_str;

% Extra
%L_f=rho_lf* L_f(-1)+e_lf;
%L_m=rho_lm* L_m(-1)+e_lf;
%K_f=rho_kf* K_f(-1)+e_kf;
K_m=rho_km* K_m(-1)+e_km;

% central bank balance sheet;
mb =(dc_gbar_mbbar)*dc+frbar_mbbar*(rer+fr);
fr=fr(-1)-pi_str + oil_frbar*oil +pe_xx_frbar*(gama_estr+x) -im_frbar*(im-rer);
im=cf_imbar*C_fi+ cm_imbar*C_mi;

end; 

shocks;
var e_g= 0.1;
end;

model_diagnostics;
stoch_simul (qz_zero_threshold =0.00000000000003);


