clc;
clear all;
 
% Definition of all variables
% --> real interest rate?
% l labour services
% k 
% h real money balances???
% --> c consumption -endo
% v taste shock?
% --> m real money balances -endo?
% --> y output?/final consumption good
% --> pi inflation -endo
% w real wage rate
% z technology  --> transaction cost technology with mean 1
% a productivity shock
% mu 
% g government expenditures - exo
% --> R gross nominal interest rate on government bonds
% sigma_c
% n_cc
% phi(func) captures real resource costs of transaction
% phi_cc real marginal transaction costs? non-decreasing
% phi_c real marginal transaction costs (in consumption) increasing
% u_cc
% u_c (u_c=u_cv)
% n_c
% phi_h transaction costs - real money balances decrease strictly
% v_ll
% v_l
% n_h
% sigma_h
% phi_hh transaction costs - real money balacnes strictly decreasing
% n_R
% phi_ch transaction costs - non increasing
% phi_hc
% beta discount factor
% omega 
% sigma_tilda coefficient of relative risk aversion
% rho_pi interest rate policy?
% n_ch transaction frictions
% sigma_h
% n_hc The prior distribution for n_hc is chosen to generate a unit output elasticity of money demand at the mean
% alpha
 
%search for variance and cov for shocks
 
 
% Endogenous variables:
var y pi h c R l g v m r_m a mu z c_hat v_hat m_hat y_hat pi_hat w_hat z_hat a_hat mu_hat g_hat R_hat;
% Exogenous variables:
varexo epsilon e_a e_g e_mu e_v e_z n_cc phi_cc e_rm phi_c u_c n_c phi_h v_ll v_l n_h phi_hh n_R phi_ch phi_hc;
 
% Parameters:
parameters s_c kappa phi sigma_m sigma_a sigma_g sigma_mu sigma_v sigma_z rho_a rho_g rho_mu rho_v rho_z beta omega sigma_tilda R rho_pi n_ch sigma_c sigma_h n_hc alpha;
 
beta = 0.99; 
s_c=0.8; %for ss
rho_pi = 0.9692;
alpha = 0.5;
sigma_tilda = 1;
omega = 3.5;
sigma_c = 0.8;
% R = 1.0146; could be R_bar
rho_pi = 0.9692; %mean of 0.85 in R3
n_ch = 0.3; % mean of 0.3 in R3
sigma_h = 0.29; %at the mean //frisch elasticity of labour ss is also 0.29 
n_hc = 0.232; %0.8 of sigma_h
rho_a = 0.9221;
rho_g = 0.8535;
rho_mu = 0.8501;
rho_v = 0.6337;
rho_z = 0.8943;
sigma_a = 1;
sigma_g = 1; 
sigma_mu = 1; 
sigma_v = 1; 
sigma_z = 1;
sigma_m = 1; 
phi = 1;
 
% The Model:
model(linear);

    a = rho_a*a(-1) + e_a;
    g = rho_g*g(-1) + e_g;
    mu = rho_mu*mu(-1) + e_mu;
    v = rho_v*v(-1) + e_v;
    z = rho_z*z(-1) + e_z;
    log(r_m) = e_rm;
    
    
     % sigma_c=-(u_cc*c)/u_c;
     % omega=(v_ll*l)/v_l;
    kappa=((1-alpha)*(1-alpha*beta))/alpha;
     % n_ch=-(phi_ch*h)/(1+phi_c);
     % n_hc=(phi_hc*c)/phi_h;
    n_R=R/(sigma_h*(R-1));
     % sigma_h=-(phi_hh*h)/phi_h;
     % sigma_tilda=sigma_c+n_cc;
    n_cc=(phi_cc*c)/(1+phi_c);
    n_c = (phi_c*c)/phi;
    n_h = -(phi_h*h)/phi;
    
   % rho_pi=(f_pi(pi,1)*pi)/R;
    
    
    sigma_tilda*c_hat(+1)-v_hat(+1)-n_ch*(m_hat-pi_hat(+1)+z_hat(+1)) = sigma_tilda*c_hat-v_hat-n_ch*(m_hat(-1)-pi_hat+z_hat)+R_hat-pi_hat(+1);
    omega*(y_hat-a_hat)+mu_hat-w_hat = -sigma_tilda*c_hat+n_ch*(m_hat(-1)-pi_hat+z_hat)+v_hat;
    pi_hat = beta*pi_hat(+1)+kappa*(w_hat-a_hat);
    y_hat = s_c*c_hat + g_hat;     % y_hat=(1-(g/y)-(phi/y)+n_c)*c_hat+g_hat-n_h*(m_hat(-1)-pi_hat+z_hat);
    m_hat = -n_R*R_hat+(n_hc/sigma_h)*c_hat(+1)+pi_hat(+1)+((1-sigma_h)/sigma_h)*z_hat(+1);
    
    R_hat = rho_pi*pi_hat+epsilon^m;
    
end;
 
shocks;
var e_a = sigma_a^2;
var e_g = sigma_g^2;
var e_mu = sigma_mu^2;
var e_v = sigma_v^2;
var e_z = sigma_z^2;
var e_rm = sigma_r_m^2;
var epsilon = sigma_m^2;
 
end;

initval;
a = 0;
g = 0;
mu = 0;
v = 0;
z = 0;
 
stoch_simul c y pi m R; 
check;
