%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%                                                        %%%
%%%          Stock Market Conditions And Monetary          %%%
%%%           Policy In A DSGE Model For The U.S.          %%%
%%%                                                        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%                                                        %%%
%%%            by E. Castelnuovo / S. Nistico              %%%
%%%                                                        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%        Section 1: PREAMBLE           %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% All variables are expressed in log-linearized form (by using a first order
% Taylor-approximation around the steady state)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Specification of the endogenous variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

var
%c_hat,        % De-trended consumption 
%d_hat,        % De-trended real dividends
Delta_g,      % Change of shocks in the public sector
Delta_nu,     % Change of preference shocks
Delta_w_FE,   % Growth rate of frictionless real wage
Delta_y_FE,   % Growth rate of frictionless output level
mc,           % Marginal costs
mw,           % (Inverse) wage markup
omega,        % Real wage gap
pi,           % Price inflation
pi_w,         % Wage inflation
q_hat,        % De-trended equity share
q_hat_FE,     % De-trended equity share in FE
r,            % Nominal interest rate
rr_FE,        % Frictionless real interest rate
s,            % Real stock price gap
%w,            % real wage
%w_hat,        % De-trended real wage
w_hat_FE,     % Frictionless level of real wage
x,            % Real output-gap
%y_hat,        % De-trended output
y_hat_FE,     % De-trended outpot in FE
endo_p,       % auxiliary endogenous variable
endo_w,       % auxiliary endogenous variable

b,            % AR(1) Shock
Delta_a,      % AR(1) Shock
g,            % AR(1) Shock
mu_p,         % Arma(1,1) Shock
mu_w,         % Arma(1,1) Shock
nu,           % AR(1) Shock
u_r;          % AR(1) Shock


% Specification of the exogenous variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

varexo
epsilon_a,    % White noise error term
epsilon_b,    % White noise error term
epsilon_g,    % White noise error term
epsilon_nu,   % White noise error term
epsilon_p,    % White noise error term
epsilon_r,    % White noise error term
epsilon_w;    % White noise error term

% Specification of the parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

parameters

C_hat_ss,
D_hat_ss,
Omega_hat_ss,
Q_hat_ss,
Y_hat_ss,
N_ss,

alpha,        % Output elasticity of labor
beta,         % "Standard" discount factor
beta_tilde,   % "Extended" discount factor
chi_p,        % MA(1) in mu_p
chi_w,        % MA(1) in mu_w
delta,        % Leisure weight
eta,          % Wage indexation
g_ss,         % Steady-state value of public expenditures over GDP
gamma,        % Quarterly trend-growth rate
Gamma,        % Steady-state gross rate of productivity growth
h,            % Parameter strictly connected to hbar
hbar,         % Degree to which consumers would like to smooth their consumption with respect to average past level
kappa_p,      % Auxiliary Parameter
kappa_w,      % Auxiliary Parameter
lambda_p,     % Auxiliary Parameter
lambda_w,     % Auxiliary Parameter
mu_p_const,   % Constant price markup in steady state
mu_w_const,   % Constant wage markup in steady state
pi_ss         % Steady state level of inflation
phi_pi,       % MP response to inflation
phi_r,        % MP intertia 
phi_s,        % MP response to stock price gap
phi_x,        % MP response to output gap
Phi_omega,    % Auxiliary Parameter
Phi_x,        % Auxiliary Parameter
psi,          % Auxiliary Parameter
psi_nu,       % Auxiliary Parameter
r_ss,         % Net steady state short-term policy rate    
rho_a,        % Persistance in Delta_a
rho_b,        % Persistance in b
rho_g,        % Persistance in g
rho_nu,       % Persistance in nu
rho_p,        % Persistance in p
rho_r,        % Persistance in r
rho_w,        % Persistance in w
rho_tilde,    % Steady State of gross interest rate
sigma_a,      % Standard deviation of Delta_a
sigma_b,      % Standard deviation of b
sigma_ceta,   % Standard deviation of ceta
sigma_g,      % Standard deviation of g
sigma_nu,     % Standard deviation of nu
sigma_p,      % Standard deviation of p
sigma_r,      % Standard deviation of r
sigma_w,      % Standard deviation of w
theta_p,      % Price rigidity
theta_w,      % Wage rigidity
Theta,        % Auxiliary Parameter
varphi,       % Inverse steady-state Frisch elasticity of labor supply
varpi,        % Price indexation
xi;           % Turnover rate

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Section 2: Calibration of parameters %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    alpha = 0.3600;
    chi_p = 0.6598;
    chi_w = 0.8572;
    delta = 1.4456;
    eta = 0.3543;
    g_ss = 0.1800;
    gamma = 0.0047;
    r_ss = 1.4300;
    hbar = 0.8269;
    mu_p_const = 0.2010;
    mu_w_const = 0.2551;
    phi_pi = 1.6745;
    phi_r = 0.7533;
    phi_s = 0.1181;
    phi_x = 0.0232;
    pi_ss = 0.8100;
    rho_a = 0.3111;
    rho_b = 0.9071;
    rho_g = 0.9692;
    rho_nu = 0.0772;
    rho_p = 0.8445;
    rho_w = 0.9600;
    sigma_a = 0.0102;
    sigma_b = 0.0059;
    sigma_ceta = 0.0673;
    sigma_g = 0.0106;
    sigma_nu = 0.0314;
    sigma_p = 0.1120;
    sigma_r = 0.0031;
    sigma_w = 0.4704;
    theta_p = 0.7404;
    theta_w = 0.6325;
    varpi = 0.1516;
    xi = 0.1292;
    beta = 1/(1+r_ss);
    Gamma = log(gamma);
    h = (hbar)/(Gamma);
    D_hat_ss = ((alpha+mu_p_const)/(1+mu_p_const))*(((1+g)*(1-alpha))/((1+g)*(1-alpha)+delta*(1-h)*(1+mu_p_const)*(1+mu_w_const)))^(1-alpha);
    Q_hat_ss = (D_hat_ss)*(((1+pi_ss)*(1+gamma))/((1+r)-(1+pi_ss)*(1+gamma)));
    Omega_hat_ss =  Q_hat_ss+D_hat_ss;
    N_ss = ((1-alpha)*(1+g_ss))/((1-alpha)*(1+g_ss)+delta*(1-h)*(1+mu_p_const)*(1+mu_w_const));
    varphi = (N_ss)/(1-N_ss);
    Y_hat_ss = (N_ss)^(1-alpha);
    C_hat_ss = (Y_hat_ss)/(1+g_ss);
    psi = xi*(1-beta*(1-xi))/(1-xi)*(Omega_hat_ss/C_hat_ss);
    beta_tilde = (beta*(1-h))/(1-h+psi);
    lambda_p = mu_p_const*((1-theta_p)*(1-beta_tilde*theta_p)*(1-alpha))/(theta_p*(mu_p_const+alpha));
    lambda_w = mu_w_const*((1-theta_w)*(1-beta_tilde*theta_w))/(theta_w*(mu_p_const+varphi*(1+mu_w_const)));
    kappa_p = lambda_p*alpha/(1-alpha);
    kappa_w = lambda_w*(1/(1-h)+varphi/(1-alpha));
    Phi_omega = (1-beta_tilde)*(1-alpha)/(alpha+mu_p_const);
    Phi_x = (1-beta_tilde)*mu_p_const/(alpha+mu_p_const);
    psi_nu = (psi*(beta*(1-xi)*rho_nu))/((1+psi-h)*(1-beta*(1-xi)*rho_nu));
    rho_tilde = log(1+r_ss);
    Theta = (1-h)/(1+psi-h);
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%     Section 3: Model Equations       %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Specification of the model relations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

model (linear);
    % Central equations of the economy
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        x-h*x(-1) = Theta*(x(+1)-h*x)+psi*Theta*s-(1-h)*Theta*(r-pi(+1)-rr_FE);
            % Equation outputgap
        s = beta_tilde*s(+1)+Phi_x*x(+1)-Phi_omega*omega(+1)-(r-pi(+1)-rr_FE)+b;
            % Stockprice-gap
        pi-varpi*pi(-1) = beta_tilde*(pi(+1)-varpi*pi)+lambda_p*omega+kappa_p*x+lambda_p*(mu_p-mu_p_const);
            % New Keynesian Philips Curve for price inflation dynamics
        pi_w-eta*pi(-1)-eta*Delta_a(-1) = beta_tilde*(pi_w(+1)-eta*pi-eta*Delta_a)+kappa_w*x-h/(1-h)*lambda_w*x(-1)+lambda_w*omega+lambda_w*(mu_w-mu_w_const);
            % New Keynesian Philips Curve for wage inflation dynamics
        omega = omega(-1)+pi_w-pi-Delta_w_FE;
            % Equation for real wage gap
        r = (1-phi_r)*(phi_pi*pi+phi_x*x+phi_s*s)+phi_r*r(-1)+u_r;
            % Taylor Rule for short-term nominal interest rates

    % Frictionless equilibrium
    %%%%%%%%%%%%%%%%%%%%%%%%%%

        y_hat_FE = h*(1-alpha)/(1-h*alpha+(1-alpha)*varphi)*y_hat_FE(-1)+(1-alpha)/(1-h*alpha+(1-h)*varphi)*(g-h*g(-1)-h*Delta_a);
            % Frictionless level of real output
        w_hat_FE = h*(1-alpha)/(1-h*alpha+(1-alpha)*varphi)*w_hat_FE(-1)-alpha/(1-h*alpha+(1-h)*varphi)*(g-h*g(-1)-h*Delta_a);
            % Frictionless level of real wage
        rr_FE = rho_tilde+1/(1-h)*(Delta_y_FE(+1)-Delta_g(+1)+Delta_a(+1)-Delta_nu(+1))+h/(1-h)*(Delta_nu(+1)-Delta_y_FE+Delta_g-Delta_a)+psi/(1-h)*q_hat_FE-psi/(1-h)^2*(y_hat_FE-g-h*y_hat_FE(-1)+h*g(-1)+h*Delta_a)-(psi_nu*(1+psi-h)+psi)/(1-h)*Delta_nu(+1);
            % Equation for frictionless real interest rate
        q_hat_FE = beta_tilde*q_hat_FE(+1)+(1-beta_tilde)*y_hat_FE(+1)-rr_FE+Delta_a(+1);
            % Equation for stock price level
        Delta_w_FE = w_hat_FE-w_hat_FE(-1);
            % Identity for frictionless wage growth

    % Demand side of the economy
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

        %y_hat = c_hat+g;
            % A.43 good market clearing
        %(c_hat-h*c_hat(-1)+h*Delta_a) = (1-h)/(1+psi-h)*(c_hat(+1)-h*c_hat+Delta_a(+1))+psi*(1-h)/(1+psi-h)*q_hat-(1-h)^2/(1+psi-h)*(r-pi(+1)-rho_tilde-Delta_a(+1))-(1-h)*(1+psi_nu)*Delta_nu(+1);
            % A.44 dynamic consumption path
        %q_hat-b = beta_tilde*(q_hat(+1)-b(+1))+(1-beta_tilde)*d_hat(+1)-(r-pi(+1)-rho_tilde)+Delta_a(+1);
            % A.45 share pricing
        %d_hat = y_hat-(1-alpha)/(alpha+mu_p_const)*mc;
            % A.46 real dividends

    % Supply side of the economy
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

        %pi-varpi*pi(-1) = beta_tilde*(pi(+1)-varpi*pi)+lambda_p*mc+lambda_p*(mu_p-mu_p_const);
            % A.47 New Keynesian Philips Curve for price inflation
        %mc = w_hat+alpha/(1-alpha)*y_hat;
            % A.48 marginal costs
        %pi_w-eta*pi(-1)-eta*Delta_a(-1) = beta_tilde*(pi_w(+1)-eta*pi-eta*Delta_a)+lambda_w*mw+lambda_w*(mu_w-mu_w_const);
            % A.49 New Keynesian Philips Curve for wage inflation
        %mw = (h/(1-h)+varpi/(1-alpha))*y_hat-h/(1-h)*(y_hat(-1)-Delta_a-g(-1))-1/(1-h)*g-w_hat;
            % A.50 (inverse) wage markup

    % Stochastic structure of the model
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Delta_a = rho_a*Delta_a(-1)+epsilon_a;
        g = rho_g*g(-1)+epsilon_g;
        nu = rho_nu*nu(-1)+epsilon_nu;
        u_r = rho_r*u_r(-1)+epsilon_r;
        b = rho_b*b(-1)+epsilon_b;
        mu_p = (1-rho_p)*mu_p_const+rho_p*mu_p(-1)+epsilon_p-chi_p*endo_p(-1);
            % ARMA (1,1) price markup 
        mu_w = (1-rho_w)*mu_w_const+rho_w*mu_w(-1)+epsilon_w-chi_w*endo_w(-1);
            % ARMA (1,1) wage markup 

    % Additional auxiliary equations and identities
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        endo_p = epsilon_p;
        endo_w = epsilon_w;
        Delta_g = g-g(-1);
        Delta_nu = nu-nu(-1);
        Delta_y_FE = y_hat_FE-y_hat_FE(-1);
        s = q_hat-q_hat_FE;
        
        mc = omega+(alpha)/(1-alpha)*x;
        mw = (1/(1-h)+(varphi)/(1-alpha))*x-(h)/(1-h)*x(-1)-omega;

end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%        Section 4: Computation        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% compute and display Eigenvalues and check the Blanchard-Kahn conditions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
resid;
%steady;
check;
%resid;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%         Section 5: Shocks            %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % All shocks are white noise and crossequation uncorrelated
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

shocks;
        var epsilon_a = sigma_a^2;
        var epsilon_g = sigma_g^2;
        var epsilon_nu = sigma_nu^2;
        var epsilon_r = sigma_r^2;
        var epsilon_b = sigma_b^2;
        var epsilon_p = sigma_p^2;
        var epsilon_w = sigma_w^2;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%        Section 6: Simulation         %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


stoch_simul(irf=40, order=1) r rr_FE s pi pi_w x omega;