% Function USER_MOD
%
% Purpose:    User input - model & measurement equations; see
%             mypdf for model details
%
% Format:     user_mod
%
% Input:      P         parameter structure
%             V         variable structure
%             para      model parameters
%             ssp       steady state/implied parameters
%
% Output:     G0,G1,Psi,Pi,CC - state equation (gensys.m input)
%             Z,D,Sigma_u     - measurement equation
%
% Written by Fei Tan, Saint Louis University
% Updated: December 15, 2016

%% User Input Start Here

%---------------------------------------------
%              GENSYS Equations
%---------------------------------------------

% Home economy
%---------------------------------------------
j = j+1;   % (1) Resource constraint
%---------------------------------------------
G0(j,V.y) = 1;
G0(j,V.c) = -ssp(P.c_y)*(1-ssp(P.alpha));
G0(j,V.tot) = -ssp(P.c_y)*ssp(P.alpha)*(para(P.eta)*(1-ssp(P.alpha))+para(P.eta_f));
G0(j,V.psi_F) = -ssp(P.c_y)*ssp(P.alpha)*para(P.eta_f);
G0(j,V.y_f ) = -ssp(P.c_y)*ssp(P.alpha);
G0(j,V.i) = -ssp(P.i_y);
%---------------------------------------------
j = j+1;   % (2) Production function
%---------------------------------------------
G0(j,V.y) = 1;
G0(j,V.k) = -para(P.Omega);
G0(j,V.n) = -(1-para(P.Omega));
G0(j,V.eps_y) = -1;
%---------------------------------------------
j = j+1;   % (3) Euler equation
%---------------------------------------------
G0(j,V.c) = 1+para(P.h); 
G1(j,V.c) = para(P.h);
G0(j,V.E_c) = -1;
G0(j,V.r) = (1-para(P.h))/para(P.sigma);
G0(j,V.E_pi) = -(1-para(P.h))/para(P.sigma);
G0(j,V.eps_c) = -(1-para(P.h))/para(P.sigma)*(1-para(P.rho_c));
%---------------------------------------------
j = j+1;   % (4) Investment
%---------------------------------------------
G0(j,V.i) = 1;
G1(j,V.i) = 1/(1+ssp(P.beta));
G0(j,V.E_i) = -ssp(P.beta)/(1+ssp(P.beta));
G0(j,V.q) = -1/para(P.chi)/(1+ssp(P.beta));
G0(j,V.eps_i) = -1/para(P.chi)/(1+ssp(P.beta));
%---------------------------------------------
j = j+1;   % (5) Law of motion of capital
%---------------------------------------------
G0(j,V.k) = 1;
G1(j,V.k) = 1-ssp(P.delta);
G1(j,V.i) = ssp(P.delta);
G1(j,V.eps_i) = ssp(P.delta);
%---------------------------------------------
j = j+1;   % (6) Capital rental rate
%---------------------------------------------
G0(j,V.r_k) = 1; 
G0(j,V.k) = 1;
G0(j,V.n) = -1; 
G0(j,V.w) = -1;
G0(j,V.r) = -(para(P.nu_n)-para(P.nu_k)); 
%---------------------------------------------
j = j+1;   % (7) Real price of capital
%---------------------------------------------
G0(j,V.q) = 1;
G0(j,V.E_r_k) = -ssp(P.beta)*ssp(P.R_k);
G0(j,V.E_q) = -ssp(P.beta)*(1-ssp(P.delta));
%--------------------------------------------
j = j+1;   % (8) Real exchange rate
%--------------------------------------------
G0(j,V.rer) = 1;
G0(j,V.psi_F) = -1;
G0(j,V.tot) = -(1-ssp(P.alpha));
%--------------------------------------------
j = j+1;   % (9) Time-differences in the terms of trade:
%--------------------------------------------
G0(j,V.tot) = 1;
G1(j,V.tot) = 1;
G0(j,V.pi_F) = -1;
G0(j,V.pi_H) = 1;
%--------------------------------------------
j = j+1;   % (10) CPI inflation
%--------------------------------------------
G0(j,V.pi) = 1;
G0(j,V.pi_H) = -1;
G0(j,V.tot) = -ssp(P.alpha);
G1(j,V.tot) = -ssp(P.alpha);
%--------------------------------------------
j = j+1;   % (11) Real wage
%--------------------------------------------
G0(j,V.w) = 1;
G1(j,V.w) = 1;
G0(j,V.pi) = 1;
%--------------------------------------------
j = j+1;   % (12) Domestic firms’ Phillips curve
%--------------------------------------------
G0(j,V.pi_H) = 1+ssp(P.beta)*para(P.delta_H);
G1(j,V.pi_H) = para(P.delta_H);
G0(j,V.E_pi_H) = -ssp(P.beta);
G0(j,V.mc) = -ssp(P.kappa_H);
G0(j,V.eps_cp_H) = -1;
%--------------------------------------------
j = j+1;   % (13) Real marginal cost
%--------------------------------------------
G0(j,V.mc) = 1;
G0(j,V.w) = -(1-para(P.Omega));
G0(j,V.r) = -(1-para(P.Omega))*para(P.nu_n)-para(P.Omega)*para(P.nu_k);
G0(j,V.r_k) = -para(P.Omega);
G0(j,V.tot) = -ssp(P.alpha);
G0(j,V.eps_y) = 1;
%--------------------------------------------
j = j+1;   % (14) Retail firms’ Phillips curve
%--------------------------------------------
G0(j,V.pi_F) = 1+ssp(P.beta)*para(P.delta_F);
G1(j,V.pi_F) = para(P.delta_F);
G0(j,V.E_pi_F) = -ssp(P.beta);
G0(j,V.psi_F) = -ssp(P.kappa_F);
G0(j,V.r) = -ssp(P.kappa_F)*para(P.nu_F);
G0(j,V.eps_cp_F) = -1;
%--------------------------------------------
j = j+1;   % (15) Modified UIP condition
%--------------------------------------------
G0(j,V.r) = 1;
G0(j,V.r_f) = -1;
G0(j,V.E_pi) = -(1-para(P.phi_e));
G0(j,V.E_pi_f) = 1-para(P.phi_e);
G0(j,V.E_rer) = -(1-para(P.phi_e));
G0(j,V.rer) = 1;
G0(j,V.pi) = para(P.phi_e);
G0(j,V.pi_f) = -para(P.phi_e);
G1(j,V.rer) = para(P.phi_e);
G0(j,V.a) = para(P.phi_a);
G0(j,V.eps_rp) = -1;
%--------------------------------------------
j = j+1;   % (16) Net foreign assets
%--------------------------------------------
G0(j,V.a) = 1;
G1(j,V.a) = 1/ssp(P.beta);
G0(j,V.psi_F) = ssp(P.alpha)*ssp(P.c_y);
G0(j,V.tot) = ssp(P.alpha)*ssp(P.c_y);
G0(j,V.y) = -1;
G0(j,V.c) = ssp(P.c_y);
G0(j,V.i) = ssp(P.i_y);
%--------------------------------------------
j = j+1;   % (17) Domestic monetary policy rule
%--------------------------------------------
G0(j,V.r) = 1;
G1(j,V.r) = para(P.rho_R);
G0(j,V.pi) = -(1-para(P.rho_R))*para(P.chi_pi);
G0(j,V.y) = -(1-para(P.rho_R))*(para(P.chi_y)+para(P.chi_dy));
G1(j,V.y) = -(1-para(P.rho_R))*para(P.chi_dy);
G0(j,V.eps_r) = -1;

% Foreign economy
%---------------------------------------------
j = j+1;   % (18) Resource constraint
%---------------------------------------------
G0(j,V.y_f) = 1;
G0(j,V.c_f) = -ssp(P.c_y_f);
G0(j,V.i_f) = -ssp(P.i_y_f);
%---------------------------------------------
j = j+1;   % (19) Production function
%---------------------------------------------
G0(j,V.y_f) = 1;
G0(j,V.k_f) = -para(P.Omega_f);
G0(j,V.n_f) = -(1-para(P.Omega_f));
G0(j,V.eps_y_f) = -1;
%---------------------------------------------
j = j+1;   % (20) Euler equation
%---------------------------------------------
G0(j,V.c_f) = 1+para(P.h_f); 
G1(j,V.c_f) = para(P.h_f);
G0(j,V.E_c_f) = -1;
G0(j,V.r_f) = (1-para(P.h_f))/para(P.sigma_f);
G0(j,V.E_pi_f) = -(1-para(P.h_f))/para(P.sigma_f);
G0(j,V.eps_c_f) = -(1-para(P.h_f))/para(P.sigma_f)*(1-para(P.rho_c_f));
%---------------------------------------------
j = j+1;   % (21) Investment
%---------------------------------------------
G0(j,V.i_f) = 1;
G1(j,V.i_f) = 1/(1+ssp(P.beta_f));
G0(j,V.E_i_f) = -ssp(P.beta_f)/(1+ssp(P.beta_f));
G0(j,V.q_f) = -1/para(P.chi_f)/(1+ssp(P.beta_f));
G0(j,V.eps_i_f) = -1/para(P.chi_f)/(1+ssp(P.beta_f));
%---------------------------------------------
j = j+1;   % (22) Law of motion of capital
%---------------------------------------------
G0(j,V.k_f) = 1;
G1(j,V.k_f) = 1-ssp(P.delta_f);
G1(j,V.i_f) = ssp(P.delta_f);
G1(j,V.eps_i_f) = ssp(P.delta_f);
%---------------------------------------------
j = j+1;   % (23) Capital rental rate
%---------------------------------------------
G0(j,V.r_k_f) = 1; 
G0(j,V.k_f) = 1;
G0(j,V.n_f) = -1; 
G0(j,V.w_f) = -1;
G0(j,V.r_f) = -(para(P.nu_n_f)-para(P.nu_k_f)); 
%---------------------------------------------
j = j+1;   % (24) Real price of capital
%---------------------------------------------
G0(j,V.q_f) = 1;
G0(j,V.E_r_k_f) = -ssp(P.beta_f)*ssp(P.R_k_f);
G0(j,V.E_q_f) = -ssp(P.beta_f)*(1-ssp(P.delta_f));
%--------------------------------------------
j = j+1;   % (25) Real wage
%--------------------------------------------
G0(j,V.w_f) = 1;
G1(j,V.w_f) = 1;
G0(j,V.pi_f) = 1;
%--------------------------------------------
j = j+1;   % (26) Foreign firms’ Phillips curve
%--------------------------------------------
G0(j,V.pi_f) = 1+ssp(P.beta_f)*para(P.delta_p_f);
G1(j,V.pi_f) = para(P.delta_p_f);
G0(j,V.E_pi_f) = -ssp(P.beta_f);
G0(j,V.mc_f) = -(1-para(P.theta_f))*(1-para(P.theta_f)*ssp(P.beta_f))/para(P.theta_f);
G0(j,V.eps_cp_f) = -1;
%--------------------------------------------
j = j+1;   % (27) Real marginal cost
%--------------------------------------------
G0(j,V.mc_f) = 1;
G0(j,V.w_f) = -(1-para(P.Omega_f));
G0(j,V.r_f) = -(1-para(P.Omega_f))*para(P.nu_n_f)-para(P.Omega_f)*para(P.nu_k_f);
G0(j,V.r_k_f) = -para(P.Omega_f);
G0(j,V.eps_y_f) = 1;
%--------------------------------------------
j = j+1;   % (28) Foreign monetary policy rule
%--------------------------------------------
G0(j,V.r_f) = 1;
G1(j,V.r_f) = para(P.rho_R_f);
G0(j,V.pi_f) = -(1-para(P.rho_R_f))*para(P.chi_pi_f);
G0(j,V.y_f) = -(1-para(P.rho_R_f))*(para(P.chi_y_f)+para(P.chi_dy_f));
G1(j,V.y_f) = -(1-para(P.rho_R_f))*para(P.chi_dy_f);
G0(j,V.eps_r_f) = -1;

% Expectations
%--------------------------------------------
j = j+1;   % (1) Define E_c
%--------------------------------------------
G0(j,V.c) = 1; 
G1(j,V.E_c) = 1; 
Pi(j,V.f_c) = 1;
%--------------------------------------------
j = j+1;   % (2) Define E_i
%--------------------------------------------
G0(j,V.i) = 1; 
G1(j,V.E_i) = 1; 
Pi(j,V.f_i) = 1;
%--------------------------------------------
j = j+1;   % (3) Define E_r_k
%--------------------------------------------
G0(j,V.r_k) = 1; 
G1(j,V.E_r_k) = 1; 
Pi(j,V.f_r_k) = 1;
%--------------------------------------------
j = j+1;   % (4) Define E_q
%--------------------------------------------
G0(j,V.q) = 1; 
G1(j,V.E_q) = 1; 
Pi(j,V.f_q) = 1;
%--------------------------------------------
j = j+1;   % (5) Define E_pi
%--------------------------------------------
G0(j,V.pi) = 1; 
G1(j,V.E_pi) = 1; 
Pi(j,V.f_pi) = 1;
%--------------------------------------------
j = j+1;   % (6) Define E_pi_H
%--------------------------------------------
G0(j,V.pi_H) = 1; 
G1(j,V.E_pi_H) = 1; 
Pi(j,V.f_pi_H) = 1;
%--------------------------------------------
j = j+1;   % (7) Define E_pi_F
%--------------------------------------------
G0(j,V.pi_F) = 1; 
G1(j,V.E_pi_F) = 1; 
Pi(j,V.f_pi_F) = 1;
%--------------------------------------------
j = j+1;   % (8) Define E_rer
%--------------------------------------------
G0(j,V.rer) = 1; 
G1(j,V.E_rer) = 1; 
Pi(j,V.f_rer) = 1;
%--------------------------------------------
j = j+1;   % (9) Define E_c_f
%--------------------------------------------
G0(j,V.c_f) = 1; 
G1(j,V.E_c_f) = 1; 
Pi(j,V.f_c_f) = 1;
%--------------------------------------------
j = j+1;   % (10) Define E_i_f
%--------------------------------------------
G0(j,V.i_f) = 1; 
G1(j,V.E_i_f) = 1; 
Pi(j,V.f_i_f) = 1;
%--------------------------------------------
j = j+1;   % (11) Define E_r_k_f
%--------------------------------------------
G0(j,V.r_k_f) = 1; 
G1(j,V.E_r_k_f) = 1; 
Pi(j,V.f_r_k_f) = 1;
%--------------------------------------------
j = j+1;   % (12) Define E_q_f
%--------------------------------------------
G0(j,V.q_f) = 1; 
G1(j,V.E_q_f) = 1; 
Pi(j,V.f_q_f) = 1;
%--------------------------------------------
j = j+1;   % (13) Define E_pi_f
%--------------------------------------------
G0(j,V.pi_f) = 1; 
G1(j,V.E_pi_f) = 1; 
Pi(j,V.f_pi_f) = 1;

% Shocks
%--------------------------------------------
j = j+1;   % (1) Home productivity shock
%--------------------------------------------
G0(j,V.eps_y) = 1; 
G1(j,V.eps_y) = para(P.rho_y); 
Psi(j,V.e_y) = 1;
%--------------------------------------------
j = j+1;   % (2) Home preference shock
%--------------------------------------------
G0(j,V.eps_c) = 1; 
G1(j,V.eps_c) = para(P.rho_c); 
Psi(j,V.e_c) = 1;
%--------------------------------------------
j = j+1;   % (3) Home investment shock
%--------------------------------------------
G0(j,V.eps_i) = 1; 
G1(j,V.eps_i) = para(P.rho_i); 
Psi(j,V.e_i) = 1;
%--------------------------------------------
j = j+1;   % (4) Domestic firms' cost-push shock
%--------------------------------------------
G0(j,V.eps_cp_H) = 1; 
G1(j,V.eps_cp_H) = para(P.rho_cp_H); 
Psi(j,V.e_cp_H) = 1;
%--------------------------------------------
j = j+1;   % (5) Retail firms' cost-push shock
%--------------------------------------------
G0(j,V.eps_cp_F) = 1; 
G1(j,V.eps_cp_F) = para(P.rho_cp_F); 
Psi(j,V.e_cp_F) = 1;
%--------------------------------------------
j = j+1;   % (6) Risk premium shock
%--------------------------------------------
G0(j,V.eps_rp) = 1; 
G1(j,V.eps_rp) = para(P.rho_rp); 
Psi(j,V.e_rp) = 1;
%--------------------------------------------
j = j+1;   % (7) Home interest rate shock
%--------------------------------------------
G0(j,V.eps_r) = 1; 
G1(j,V.eps_r) = para(P.rho_r); 
Psi(j,V.e_r) = 1;
%--------------------------------------------
j = j+1;   % (8) Foreign productivity shock
%--------------------------------------------
G0(j,V.eps_y_f) = 1; 
G1(j,V.eps_y_f) = para(P.rho_y_f); 
Psi(j,V.e_y_f) = 1;
%--------------------------------------------
j = j+1;   % (9) Foreign preference shock
%--------------------------------------------
G0(j,V.eps_c_f) = 1; 
G1(j,V.eps_c_f) = para(P.rho_c_f); 
Psi(j,V.e_c_f) = 1;
%--------------------------------------------
j = j+1;   % (10) Foreign investment shock
%--------------------------------------------
G0(j,V.eps_i_f) = 1; 
G1(j,V.eps_i_f) = para(P.rho_i_f); 
Psi(j,V.e_i_f) = 1;
%--------------------------------------------
j = j+1;   % (11) Foreign firms' cost-push shock
%--------------------------------------------
G0(j,V.eps_cp_f) = 1; 
G1(j,V.eps_cp_f) = para(P.rho_cp_f); 
Psi(j,V.e_cp_f) = 1;
%--------------------------------------------
j = j+1;   % (12) Foreign interest rate shock
%--------------------------------------------
G0(j,V.eps_r_f) = 1; 
G1(j,V.eps_r_f) = para(P.rho_r_f); 
Psi(j,V.e_r_f) = 1;


% Dummy variables
%---------------------------------------------
j = j+1;   % (1) Lagged y
%---------------------------------------------
G0(j,V.d_y) = 1;
G1(j,V.y) = 1;
%---------------------------------------------
j = j+1;   % (2) Lagged c
%---------------------------------------------
G0(j,V.d_c) = 1;
G1(j,V.c) = 1;
%---------------------------------------------
j = j+1;   % (3) Lagged i
%---------------------------------------------
G0(j,V.d_i) = 1;
G1(j,V.i) = 1;
%---------------------------------------------
j = j+1;   % (4) Lagged k
%---------------------------------------------
G0(j,V.d_k) = 1;
G1(j,V.k) = 1;
%---------------------------------------------
j = j+1;   % (5) Lagged rer
%---------------------------------------------
G0(j,V.d_rer) = 1;
G1(j,V.rer) = 1;
%---------------------------------------------
j = j+1;   % (6) Lagged tot
%---------------------------------------------
G0(j,V.d_tot) = 1;
G1(j,V.tot) = 1;
%---------------------------------------------
j = j+1;   % (7) Lagged pi_H
%---------------------------------------------
G0(j,V.d_pi_H) = 1;
G1(j,V.pi_H) = 1;
%---------------------------------------------
j = j+1;   % (8) Lagged pi_F
%---------------------------------------------
G0(j,V.d_pi_F) = 1;
G1(j,V.pi_F) = 1;
%---------------------------------------------
j = j+1;   % (9) Lagged w
%---------------------------------------------
G0(j,V.d_w) = 1;
G1(j,V.w) = 1;
%---------------------------------------------
j = j+1;   % (10) Lagged a
%---------------------------------------------
G0(j,V.d_a) = 1;
G1(j,V.a) = 1;
%---------------------------------------------
j = j+1;   % (11) Lagged r
%---------------------------------------------
G0(j,V.d_r) = 1;
G1(j,V.r) = 1;
%---------------------------------------------
j = j+1;   % (12) Lagged y_f
%---------------------------------------------
G0(j,V.d_y_f) = 1;
G1(j,V.y_f) = 1;
%---------------------------------------------
j = j+1;   % (13) Lagged c_f
%---------------------------------------------
G0(j,V.d_c_f) = 1;
G1(j,V.c_f) = 1;
%---------------------------------------------
j = j+1;   % (14) Lagged i_f
%---------------------------------------------
G0(j,V.d_i_f) = 1;
G1(j,V.i_f) = 1;
%---------------------------------------------
j = j+1;   % (15) Lagged k_f
%---------------------------------------------
G0(j,V.d_k_f) = 1;
G1(j,V.k_f) = 1;
%---------------------------------------------
j = j+1;   % (16) Lagged pi_f
%---------------------------------------------
G0(j,V.d_pi_f) = 1;
G1(j,V.pi_f) = 1;
%---------------------------------------------
j = j+1;   % (17) Lagged w_f
%---------------------------------------------
G0(j,V.d_w_f) = 1;
G1(j,V.w_f) = 1;
%---------------------------------------------
j = j+1;   % (18) Lagged r_f
%---------------------------------------------
G0(j,V.d_r_f) = 1;
G1(j,V.r_f) = 1;

%---------------------------------------------
%           Measurement Equations
%---------------------------------------------
Z(V.GDP,V.y) = 1;
Z(V.GDP,V.d_y) = -1;

Z(V.Inv,V.i) = 1;
Z(V.Inv,V.d_i) = -1;

Z(V.Inf,V.pi) = 1;

Z(V.Fed,V.r) = 1;

Z(V.Tot,V.tot) = 1;
Z(V.Tot,V.d_tot) = -1;

Z(V.Rer,V.rer) = 1;
Z(V.Rer,V.d_rer) = -1;

Z(V.GDP_f,V.y_f) = 1;
Z(V.GDP_f,V.d_y_f) = -1;

Z(V.Inv_f,V.i_f) = 1;
Z(V.Inv_f,V.d_i_f) = -1;

Z(V.Inf_f,V.pi_f) = 1;

Z(V.Fed_f,V.r_f) = 1;


D(V.GDP) = para(P.mu_gdp);
D(V.Inv) = para(P.mu_inv);
D(V.Inf) = para(P.mu_pi);
D(V.Fed) = para(P.til_beta);
D(V.Tot) = 0;
D(V.Rer) = 0;
D(V.GDP_f) = para(P.mu_gdp_f);
D(V.Inv_f) = para(P.mu_inv_f);
D(V.Inf_f) = para(P.mu_pi_f);
D(V.Fed_f) = para(P.til_beta_f);

% Measurement covariance 
Sigma_u(V.GDP) = 0.1106;
Sigma_u(V.Inv) = 2.4897;
Sigma_u(V.Inf) = 0.3434;
Sigma_u(V.Fed) = 0.0026;
Sigma_u(V.Tot) = 0.5167;
Sigma_u(V.Rer) = 0.1086;
Sigma_u(V.GDP_f) = 0.0135;
Sigma_u(V.Inv_f) = 0.3739;
Sigma_u(V.Inf_f) = 0.0020;
Sigma_u(V.Fed_f) = 0.0100;

%Sigma_u = (0.2*std(data.Y)').^2;

%% User Input End Here