%Exampel model

close all

var ygap, ynhat, yhat, yfhat, e, i, s, ph, pih, pi, rn, pf, pif, a;
varexo eps_a, eps_pif, eps_yf, eps_ygap, eps_yhat, eps_z, eps_pih; 
 
parameters alpha, betta, phi, gamma, theta, eta, rho_yf, rho_pif, rho_a,  
sigma, phi_pi, phi_y;
 
% Degree of openness
alpha = 0.4;
 
% Time preference
betta  = 0.99;
 
% Inverse of labor supply elasticity
phi   = 3;
 
% Substitutability between good produced in different foreign countries, from viewpoint of domestic consumer
gamma = 1;
 
% Price stickiness
theta   = 0.75;
 
% The substitutability between domestic and foreign goods from the viewpoint %of the domestic consumer
 
eta=1;
 
% Measures the wealth effect of labor supply, sigma<1 the substitution effect % on LS from a higher real wage dominates and leads to higher employment
% when sigma>1 the converse is true, a higher real wage gives a smaller %marginal utility of consumption and employment is lowered
% when sigma=1 the utility of consumption is logarithmic and the effects %cancel each other out

sigma   = 1;
 
 
% Persistence parameters for AR-processes
rho_yf    = 0.9;
rho_pif   = 0.8;
rho_a     = 0.8;
 
% Weights for Taylor rule
phi_pi  = 1.5;
phi_y   = 0;
 
model;

# omega   = sigma*gamma+(1-alpha)*(sigma*eta-1);
# lambda  = ((1-betta*theta)*(1-theta))/theta;
# rho     = -log(betta);
# sigma_a = sigma/((1-alpha)+alpha*omega);


 
% Exogenous shocks in AR(1)
 
yfhat=rho_yf*yfhat(-1)+eps_yf;
pif=rho_pif*pif(-1)+eps_pif;
a=rho_a*a(-1)+eps_a;
 
% DIS 
ygap=ygap(+1)-(1/sigma_a)*(i-pih(+1)-rn)+eps_ygap;
 
% NKPC 
pih=betta*pih(+1)+lambda*(sigma_a+phi)*ygap+eps_pih;

pi=pih+alpha*s-alpha*s(-1);

% Taylor rule DITR
i=rho+phi_pi*pih+phi_y*ygap+eps_z;
 
%Natural interest rate
rn=rho-(sigma_a*(1+phi)/(sigma_a+phi))*(1-rho_a)*a
+((sigma_a*phi)/(sigma_a+phi))*((sigma_a/sigma)-1)*yfhat(+1)
-((sigma_a*phi)/(sigma_a+phi))*((sigma_a/sigma)-1)*yfhat;
 
%Natural output
ynhat=((1+phi)/(sigma_a+phi))*a-((sigma-sigma_a)/(sigma_a+phi))*yfhat;
yhat=ynhat+ygap+eps_yhat;
 
 
%nominal effective exchange rate as a function of ToT
%and the relative price between foreign and domestic compostie goods
%as in gali eq (16) page 156
e=s-pf+ph;
 
% terms of trade gali (29)
s=sigma_a*(yhat-yfhat);
 
%price levels ad hoc
ph=pih+ph(-1);

pf=pif+pf(-1);
end;

%variabels available from simulation 
varobs ygap yhat pih pif rn e s;

%variables observable in real life
%varobs yhat e i pih pif; 

%% steady_state(betta) gives the ss value for rho=-log(betta)

initval;

ynhat   = 0;
yhat    = 0;
ygap    = 0;
i       = steady_state_rho(betta);
pih     = 0;
pif     = 0;
pi      = 0;
ph      = 0;
pf      = 0;
yfhat   = 0;
a       = 0;
e       = 0;
rn      = steady_state_rho(betta);
s       = 0; 
end;
steady;

shocks;
var eps_ygap    = 0.001;
var eps_yhat    = 0.001;
var eps_z       = 0.001;
var eps_pih     = 0.001;

var eps_a       = 0.001;
var eps_pif     = 0.001;
var eps_yf      = 0.001;

%stderr eps_a,   inv_gamma_pdf,     0.1,      inf; 
%stderr eps_pif, inv_gamma_pdf,     0.1,      inf;
%stderr eps_yf,  inv_gamma_pdf,     0.1,      inf;

end;


%fixed deep parameters alpha, betta, gamma, theta, eta, sigma,
%                       rho_yf, rho_a, phi_pi, phi_y

estimated_params_init(use_calibration);
end;

estimated_params;


phi,         gamma_pdf,  3,     0.02;
alpha,       beta_pdf, 0.4,     0.02;
end; 

estimated_params_init(use_calibration);
end;

identification(ar=10);

%estimation(datafile=simuldataSMOE,nobs=500,first_obs=500, 
%mh_replic=2000,mh_nblocks=2,mh_drop=0.45,mh_jscale=0.8,mode_compute=6); 

