
// This is a model for analyzing the effect of fiscal policy when Rule of Thumb consumers
// are introduced in a small open economy 
// Gitte (May 2013).

//addpath 'C:\Program Files (x86)\dynare\4.3.0\matlab';

// Declaration of endogenous variable
var co_hat, // Variables for the households
cr_hat,
c_hat,
no_hat,
nr_hat,
n_hat,
w_hat,
pi_h_hat,
pi_f_hat,
pi_hat,
ph_hat,
pf_hat,
p_hat,
tr_hat,

// Variables for trade
s_hat,
e_hat,
q_hat,
r_hat,
r_star_hat,
nx_hat,

// Variables for firm
mc_hat,
a_hat,
y_hat,
p_opt_hat,

// Variables for fiscal policy
b_hat,
g_hat,
t_hat,

// Variables for world
c_star_hat,
y_star_hat,
g_star_hat,
p_star_hat,
pi_star_hat,

// Variables for monetary policy
rr_hat,
x_hat,
i_hat;

varexo w;

parameters 
//Deep parameters
lambda,     // Share of domestic households, that are RoT consumers
alpha,      // Share of foreign goods in aggregate consumption
eta,        // The elasticity of substitution between foreign and domestic goods
epsilon,    // The elasticity of substitution between varieties (produced within any given country)
gamma,      // The substitutability between goods produced in different foreign countries
beta,       // Rate of time preference
sigma,      // The degree of relative risk aversion for consumption, same for all households
phi,        // The degree of relative risk aversion for leirure, same for all households
theta,      // Fraction of intermediate firms, that cannot reach price in a given period
mu,         // The (log of) gross markup in steady state; a function of epsilon
tau,        // Employment subsidy (offset the __ of monopolistic competition for intermediate firms)
nu,         // The log of the employment subsidy
rho_a,      // AR parameter in law of motion for (log) technology
stdev_w,    // Std.dev of shoch in above AR(1) process
phi_pi,     // The inflation parameter in Taylor rule
phi_x,      // The output-gap resounse in the Taylor rule
R,          // The steady state real interest rate
B,          // The steady state real level of debt
P,
C,
G,
T,
N,
W,

//Big Ratios
gamma_c,
gamma_g,
gamma_l,
omega,
varkappa,
varpi,
sigma_a,
big_gamma,
big_psi,
big_upsilon_c,
big_upsilon_g,
big_upsilon_g_star,
big_theta,
sigma_f;

% *********************
% CALIBRATED PARAMETERS
% *********************
% Setting numerical values for parameters. Set in accordance with Gali&Monacelli (2005), except lambda and gamma_c
lambda=0.5;
alpha=0.4;
eta=1;
epsilon=6;
gamma=1;
beta=0.99;
sigma=1;
phi=3;
theta=0.75;
gamma_c=0.2;
rho_a=0.66;
stdev_w=0.0071;
phi_pi=0.3;
phi_x=0.5;
B=1;
P=1;
G=0.02;
T=0.025;
N=1.031726859474427;

C=N-G;
W=C^sigma*N^phi;
R=1/beta;
mu=log(epsilon/(1-epsilon));
tau=1-(1/(1-epsilon))/(1-alpha); % This value ensures optimal output in steady state
nu=log(1-tau);
gamma_g=1-gamma_c;
gamma_l=C/(W*N);
omega=(gamma-eta)+(eta-1/sigma)*(1-alpha);
varkappa=(1-alpha)*(1-lambda)+alpha;
varpi=(1-beta*theta)*(1-theta)/theta;
sigma_a=sigma/(alpha*omega+varkappa*(1-alpha));

big_psi=(varkappa^2*sigma_a-sigma*(1-lambda))/(phi*gamma_c+varkappa*sigma_a);
big_gamma=(1+phi)/(phi+varkappa*sigma_a/gamma_c);
big_upsilon_c=(varkappa*sigma_a*(1-varkappa)-sigma*lambda)/(phi+varkappa*sigma_a/gamma_c);
big_upsilon_g=varkappa*sigma_a/(phi+varkappa*sigma_a/gamma_c)*gamma_g/gamma_c;
big_upsilon_g_star=(sigma*(1-lambda)-varkappa^2*sigma_a)/(phi+varkappa*sigma_a/gamma_c)*gamma_g/gamma_c;

big_theta=gamma_c*(omega-varkappa);
sigma_f=(1-alpha/sigma*big_theta/gamma_c*sigma_a)*sigma;

model(linear);

////***********   1) Optimizing (Ricardian) HHs ****************************************************
w_hat-p_hat=sigma*co_hat+phi*no_hat;
co_hat=co_hat(+1)-1/sigma*(i_hat-pi_hat(+1));

////***********   2) Rule of thumb (Non-Riccardian) HHs ********************************************
w_hat-p_hat=sigma*cr_hat+phi*nr_hat;
cr_hat=1/gamma_l*(w_hat-p_hat+nr_hat)-1/gamma_c*tr_hat;

////***********   3) Aggregating HH sector *********************************************************
c_hat=lambda*cr_hat+(1-lambda)*co_hat;
n_hat=lambda*nr_hat+(1-lambda)*no_hat;

////***********   4) Prices and exchange rates *****************************************************
s_hat=pf_hat-ph_hat;
s_hat=e_hat+p_star_hat-ph_hat;

p_hat=ph_hat+alpha*s_hat;

pi_h_hat=pi_hat-alpha*(s_hat-s_hat(-1));
pi_hat=p_hat-p_hat(-1);

pf_hat=e_hat+p_star_hat;
q_hat=(1-alpha)*s_hat;

co_hat=c_star_hat+q_hat/sigma;
r_hat=r_star_hat+(e_hat(+1)-e_hat);
i_hat=r_hat+pi_h_hat;
// s_hat=(r_star_hat-pi_star_hat(+1))-(r_hat-pi_h_hat(+1))+s_hat(+1);


////***********   5) Firms ************************************************************************
mc_hat=w_hat-ph_hat-a_hat;
y_hat=n_hat+a_hat;
a_hat=a_hat(-1)+w;

p_opt_hat=ph_hat(-1)+beta*theta*(p_opt_hat(+1)-ph_hat)+pi_h_hat+(1-beta*theta)*mc_hat;

////***********   6) Fiscal policy *****************************************************************

b_hat=R*b_hat(-1)+R/B*(P*G*(g_hat(-1)+ph_hat(-1))-P*T*(p_hat(-1)+t_hat(-1))+tau*N*(y_hat(-1)-a_hat(-1)));
g_hat=0;
tr_hat=0;
////***********   7) equillibrium in the private sector *********************************************

y_hat=gamma_c*varkappa*co_hat+gamma_c*(1-varkappa)*cr_hat+gamma_g*g_hat+gamma_c*alpha*omega/sigma*s_hat;

y_star_hat=gamma_c*c_star_hat+gamma_g*g_star_hat;

nx_hat=gamma_c*alpha*lambda*(co_hat-cr_hat)+gamma_c*alpha*(omega/sigma-1)*s_hat;

pi_h_hat=beta*pi_h_hat(+1)+varpi*mc_hat;

x_hat=mc_hat/(phi+varkappa*sigma_a/gamma_c);

rr_hat  =   -sigma_f/(varkappa*gamma_c)*(big_gamma)*(1-rho_a)*a_hat
           +1/(varkappa*gamma_c)*(alpha*big_theta/gamma_c*sigma_a*varkappa+sigma_f*big_psi)*(y_star_hat(+1)-y_star_hat)
           +1/(varkappa*gamma_c)*(sigma_f*big_upsilon_c-(1-varkappa)*(sigma*gamma_c-alpha*big_theta*sigma_a))*(cr_hat(+1)-cr_hat)
           +1/(varkappa*gamma_c)*(sigma_f*big_upsilon_g+gamma_g*alpha*big_theta/gamma_c*sigma_a)*(g_hat(+1)-g_hat)
           +1/(varkappa*gamma_c)*(sigma_f*big_upsilon_g_star-gamma_g*(alpha*big_theta/gamma_c*sigma_a+sigma))*(g_star_hat(+1)-g_star_hat);


////***********   7) Monetary policy *********************************************
% *********************			
% TAYLOR RULE
% *********************
//r_hat=rr_hat+phi_pi*pi_h_hat+phi_x*x_hat;

% *********************			
% PRICE STABILITY
% *********************
ph_hat=0;
pi_h_hat=0;

% *********************			
% EXCHANGE RATE PEG
% *********************
//e_hat=0;

////***********   8) No foreign shocks *******************************************
y_star_hat=0;
g_star_hat=0;
p_star_hat=0;
pi_star_hat=0;
end;

% *********************			
% Setting initial values
% *********************
initval;
co_hat=0; 
cr_hat=0;
c_hat=0;
no_hat=0;
nr_hat=0;
n_hat=0;
w_hat=0;
pi_h_hat=0;
pi_f_hat=0;
pi_hat=0;
ph_hat=0;
pf_hat=0;
p_hat=0;
tr_hat=0;

s_hat=0;
e_hat=0;
q_hat=0;
r_hat=0;
r_star_hat=0;
nx_hat=0;

mc_hat=0;
a_hat=0;
y_hat=0;
p_opt_hat=0;

b_hat=0;
g_hat=0;
t_hat=0;

c_star_hat=0;
y_star_hat=0;
g_star_hat=0;
p_star_hat=0;
pi_star_hat=0;

rr_hat=0;
x_hat=0;
i_hat=0;
end;

steady; // Finds the steady state of the model
check;  // Checks for stationarity (posts the eigenvalues)

shocks;
var w = stdev_w^2;
end;

stoch_simul(periods=100);