
%Dynare code

%Author: Manuel CG

%-------------------%
%    Description    %
%-------------------%

%   The following code replicates the paper of Carvalho and Ferrero "What                         
%Explains Japan's Persistent Deflation?"(2014). All the variables are expressed
%in efficiency units due to detrending

%------------------------------------------------------------------------------%
%                           Endogenous variables                               % 
%------------------------------------------------------------------------------%

var

%-------------------%
%     Households    %
%-------------------%

    psi         %1  Dependency ratio
    
    c_r         %2  Retirees consumption
    c_w         %3  Workers consumption
    c           %4  Total consumption
    xi_r        %5  Marginal propensity of comsume for retirees
    xi_w        %6  Marginal propensity of comsume for workers
    epsilon     %7  Ratio of marginal propensities to consume (xi_r/xi_w)
    Omega       %8  Adjustment factor

    a_r         %9  Assets holding by the retirees
    a_w         %10 Assets holding by the workers
    a           %11 Total amount of assets
    
    v_r         %12 Retirees welfare
    v_w         %13 Workers welfare
    v           %14 Retirees Welfare
    lambda      %15 Distribution of wealth
    h           %16 Aggregate value of human wealth
    
%-------------------%
%       Firms       %
%-------------------%

    y           %17 Gross Domestic Product           

    i           %18 Investment
    k           %19 Capital

    w           %20 Real wage
    R_k         %21 Rental rate of capital
    mc          %22 Marginal cost

    p_F         %23  Price of intermediate firm shares
    d_F         %24  Profits of intermediate good producers

%-------------------%
%     Government    %
%-------------------%

    pi          %25  Inflation rate

    g_g         %26  Government consumption

    b_g         %27  Government bonds
    R_b         %28  Bond interest rate
    t;          %29  Lump-sum taxes
    

%------------------------------------------------------------------------------%                               
%                           Exogenous variables                                % 
%------------------------------------------------------------------------------%

varexo

    n           %1  Growth rate of labor force
    omega       %2  Probability of remain in the labor force  
    gamma       %3  Probability of survive
    x           %4  Productivity growth rate
    g           %5  Government spending (% of GDP)
    b;          %6  Governemtn debt (% of GDP)

%------------------------------------------------------------------------------%
%                                   Parameters                                 % 
%------------------------------------------------------------------------------%

parameters

    alpha       %1  Labor share
    beta        %2  Individual discount factor
    delta       %3  Depreciation rate of physical capital
    theta       %4  Elasticity of substititution among varieties
    phi_P       %5  Price adjustment cost
    sigma       %6  Elasticity of intertemporal susbstitution
    phi_pi;     %7  Inflation response coefficient

%------------------%
%    Calibration   % 
%------------------%

    alpha = 0.623;       
    beta = 0.9982;        
    delta = 0.025;       
    theta = 12;       
    phi_P = 133;       
    sigma = 0.5;       
    phi_pi = 2;       

%------------------------------------------------------------------------------%                               
%                           Equations of the model                             % 
%------------------------------------------------------------------------------%

model;

    %[1]  Dependency ratio

    (1 + n)*psi = (1 - omega) + gamma*psi(-1);

    %[2]  Agreggate consumption for retirees

    c_r = xi_r*(R_b(-1)/pi)*a_r(-1);

    %[3]  Marginal propensity to consume for retirees

    xi_r = 1 - gamma(+1)*(beta^sigma)*((R_b/pi(+1))^(sigma - 1))*(xi_r/xi_r(+1));

    %[4]  Retirees welfare

    v_r = ((xi_r)^(sigma/(1 - sigma)))*c_r;

    %[5]  Aggregate consumption for workers

    c_w = xi_w*((R_b(-1)/pi)*a_w(-1) + h);

    %[6]  Marginal propensity to consume for workers

    xi_w = 1 - (beta^sigma)*(((Omega(+1)*R_b)/pi(+1))^(sigma - 1))*(xi_w/xi_w(+1));

    %[7]  Workers welfare

    v_w = ((xi_w)^(sigma/(1 - sigma)))*c_w;

    %[8]  Aggregate value of human wealth 

    h = (w - t) + ((1 + x(+1))*omega(+1)*h(+1))/(Omega(+1)*R_b/pi(+1));

    %[9]  Adjustment factor
    
    Omega = omega + (1 - omega)*(epsilon^(1/(1 - sigma)));

    %[10] Ratio of marginal propensities to consume

    epsilon = xi_r/xi_w;

    %[11] Aggregate consumption

    c = xi_w*((1 - lambda(-1))*(R_b(-1)/pi)*a(-1) + h + 
        epsilon*lambda(-1)*(R_b(-1)/pi)*a(-1));
    
    %[12] Distribution of wealth

    (1 + x(+1) + n(+1))*(lambda - (1 - omega))*a = omega*(1 - xi_r)*lambda(-1)*
                                                  (R_b(-1)/pi)*a(-1);  

    %[13] Retirees assests

    a_r/a = lambda;

    %[14] Workers assets 

    a_w/a = 1 - lambda;

    %[15] & [16] No arbitrage conditions

    R_b/(pi(+1)) = R_k(+1) + (1 - delta);
    R_b/(pi(+1)) = (1 + x(+1) + n(+1))*((p_F(+1) + d_F(+1))/p_F);

    %[17] Aggregate welfare

    v = ((xi_w^(sigma/(1 - sigma)))*
        (c_w + psi*(epsilon^(sigma/(1 - sigma)))*c_r))/(1 + psi);

    %[18] Real wage

    w = mc*alpha*y;
    
    %[19] Rental rate of capital

    R_k = mc*(((1 - alpha)*y)/k(-1));

    %[20] Marginal cost

    mc = ((w^alpha)*(R_k^(1 - alpha)))/((alpha^alpha)*((1 - alpha)^(1 - alpha)));

    %[21] Phillips curve

    (pi - 1)*pi = ((theta - 1)/phi_P)*((theta/(theta - 1))*mc - 1) + (1 + x(+1) 
                  + n(+1))*(1/(R_b/pi(+1)))*(y(+1)/y)*(pi(+1) - 1)*pi(+1);

    %[22] Government budget constraint

    (1 + x(+1) + n(+1))*b_g = (R_b/pi)*b_g(-1) + g_g - t;

    %[23] Fiscal rule

    (1 + x(+1) + n(+1))*b_g = b*y;

    %[24] Government consumption

    g_g = g*y;

    %[25] Monetary policy rule

    pi*((y/y(-4))^phi_pi) = 1;

    %[26] Aggregate assets

    a = k + b + p_F;

    %[27] Profits of intermediate good produces

    d_F = (1 - mc - (phi_P/2)*((pi - 1)^2))*y;

    %[28] Capital law of motion

    (1 + x(+1) + n(+1) )*k = (1 - delta)*k(-1) + i;

    %[29] Resource constraint

    (1 - (phi_P/2)*((pi - 1)^2))*y = c + i + g_g;

end;

%------------------------------------------------------------------------------%                               
%                               Steady State Model                             % 
%------------------------------------------------------------------------------%


steady_state_model;


    psi = (1 - omega)/(1 + n - gamma);

    pi = 1^phi_pi;

    mc = (theta - 1)/theta;

end;

%Initial values

initval;

%-------------------%
%     Households    %
%-------------------%

    psi = 0.327;         
    
    c_r = 0.5;         
    c_w = 0.2;         
    c = 0.4;           
    xi_r = 0.7;        
    xi_w = 0.3;        
    epsilon = 2.3;     
    Omega = 1;       

    a_r = 1;         
    a_w = 0.5;         
    a = 1.5;           
    
    v_r = 0.2;         
    v_r = 0.2;         
    v = 2;           
    lambda = 0.8;      
    h = 0.2;           
    
%-------------------%
%       Firms       %
%-------------------%

    y = 1;           

    i = 0.29;           
    k = 3;           

    w = 0.55;           
    R_k = 0.15;         
    mc = 0.9;          

    p_F = 0.2;         
    d_F = 0.1;         

%-------------------%
%     Government    %
%-------------------%

    pi = 1;          

    g_g  = 0.17;
    R_b = 0.012;
    b_g = 3.5;         
    t = 0.2;           

%---------------------------%
%     Exogenous variables   %
%---------------------------%
 
    n = 0.008;           
    omega = 0.9948;       
    gamma = 0.9907;       
    x = 0.0027;           
    g = 0.17;           
    b = 4;          

end;

%Calculating the steady state

steady;

%Check the Blanchard-Khan conditions

check;






