Replicating Gali (2008) Chapter 6

Hi, I’m a graduate student and trying to replicate figure 6.3 from chapter 6 of Gali 2008.

I’ve tried multiple scripts the entire afternoon but none seem to work. I now ended up with a mod file and a m file to run it. I get the graphs but they are entirely wrong and there is also an error that doesn’t seem to go away.

The scripts I wrote:

% File: sticky_prices_wages.mod

% Declare variables
var y pi_p pi_w w gap_r;

% Declare shocks
varexo epsilon_mp;

% Parameters
parameters beta sigma phi theta_p theta_w lambda_p lambda_w rho_mp;

% Calibration
beta = 0.99;                % Discount factor
sigma = 1;                  % IS curve slope
phi = 1.5;                  % Taylor rule response to inflation
theta_p = 2/3;              % Fraction of firms not adjusting prices each period
theta_w = 3/4;              % Fraction of workers not adjusting wages each period
lambda_p = (1 - theta_p) * (1 - beta * theta_p) / theta_p; % Price stickiness
lambda_w = (1 - theta_w) * (1 - beta * theta_w) / theta_w; % Wage stickiness
rho_mp = 0.5;               % Monetary policy shock persistence

% Model equations
model;
    % IS curve
    y = beta * y(+1) - sigma * (gap_r - pi_p(+1));
    
    % Price NKPC
    pi_p = beta * pi_p(+1) + lambda_p * y;
    
    % Wage NKPC
    pi_w = beta * pi_w(+1) + lambda_w * (w - pi_p);
    
    % Real wage dynamics
    w = w(-1) + pi_w - pi_p;
    
    % Monetary policy rule
    gap_r = rho_mp * gap_r(-1) + epsilon_mp;
end;

% Shocks
shocks;
    var epsilon_mp; stderr 0.1; % Increase standard deviation to scale IRFs
end;

% Simulation
stoch_simul(order=1, irf=12, nograph);

and the second one:

% File: run_figure6_3.m

% Clear environment and set path
clear; clc;
cd 'C:\Users\benth\Documents\models\Figure 6.3 Gali';

% Define scenarios
scenarios = {
    'StickyPricesAndWages', 0.3, 0.2;  % lambda_p, lambda_w
    'StickyPricesOnly', 0.3, 0;        % lambda_p, lambda_w
    'StickyWagesOnly', 0, 0.2          % lambda_p, lambda_w
};

% Preallocate results
irfs = struct();

% Loop through scenarios
for i = 1:size(scenarios, 1)
    % Extract scenario info
    scenario_name = scenarios{i, 1};
    lambda_p_val = scenarios{i, 2};
    lambda_w_val = scenarios{i, 3};
    
    % Read and update the base .mod file
    dynare_code = fileread('sticky_prices_wages.mod');
    dynare_code = strrep(dynare_code, 'lambda_p = @lambda_p_val;', ['lambda_p = ', num2str(lambda_p_val), ';']);
    dynare_code = strrep(dynare_code, 'lambda_w = @lambda_w_val;', ['lambda_w = ', num2str(lambda_w_val), ';']);
    
    % Write the updated temporary .mod file
    temp_mod_file = 'temp_sticky_prices_wages.mod';
    fid = fopen(temp_mod_file, 'w');
    if fid == -1
        error('Failed to create temporary .mod file.');
    end
    fwrite(fid, dynare_code);
    fclose(fid);
    
    % Run Dynare on the temporary .mod file
    try
        dynare temp_sticky_prices_wages noclearall;
    catch ME
        error('Dynare failed to execute: %s', ME.message);
    end
    
    % Store IRFs
    irfs.(scenario_name).y = oo_.irfs.y_epsilon_mp;
    irfs.(scenario_name).pi_p = oo_.irfs.pi_p_epsilon_mp;
    irfs.(scenario_name).pi_w = oo_.irfs.pi_w_epsilon_mp;
    irfs.(scenario_name).w = oo_.irfs.w_epsilon_mp;
end

% Cleanup Dynare-generated files
delete temp_sticky_prices_wages.mod;
delete temp_sticky_prices_wages.m;

% Plot IRFs
figure;

% Plot Output Gap
subplot(2, 2, 1);
hold on;
plot(1:12, irfs.StickyPricesAndWages.y, 'r', 'LineWidth', 1.5);
plot(1:12, irfs.StickyPricesOnly.y, 'b--', 'LineWidth', 1.5);
plot(1:12, irfs.StickyWagesOnly.y, 'g:', 'LineWidth', 1.5);
title('Output Gap');
legend('Sticky Prices & Wages', 'Sticky Prices Only', 'Sticky Wages Only');
hold off;

% Plot Price Inflation
subplot(2, 2, 2);
hold on;
plot(1:12, irfs.StickyPricesAndWages.pi_p, 'r', 'LineWidth', 1.5);
plot(1:12, irfs.StickyPricesOnly.pi_p, 'b--', 'LineWidth', 1.5);
plot(1:12, irfs.StickyWagesOnly.pi_p, 'g:', 'LineWidth', 1.5);
title('Price Inflation');
legend('Sticky Prices & Wages', 'Sticky Prices Only', 'Sticky Wages Only');
hold off;

% Plot Wage Inflation
subplot(2, 2, 3);
hold on;
plot(1:12, irfs.StickyPricesAndWages.pi_w, 'r', 'LineWidth', 1.5);
plot(1:12, irfs.StickyPricesOnly.pi_w, 'b--', 'LineWidth', 1.5);
plot(1:12, irfs.StickyWagesOnly.pi_w, 'g:', 'LineWidth', 1.5);
title('Wage Inflation');
legend('Sticky Prices & Wages', 'Sticky Prices Only', 'Sticky Wages Only');
hold off;

% Plot Real Wage
subplot(2, 2, 4);
hold on;
plot(1:12, irfs.StickyPricesAndWages.w, 'r', 'LineWidth', 1.5);
plot(1:12, irfs.StickyPricesOnly.w, 'b--', 'LineWidth', 1.5);
plot(1:12, irfs.StickyWagesOnly.w, 'g:', 'LineWidth', 1.5);
title('Real Wage');
legend('Sticky Prices & Wages', 'Sticky Prices Only', 'Sticky Wages Only');
hold off;

Have you tried starting from the files for the second edition at DSGE_mod/Gali_2015 at master · JohannesPfeifer/DSGE_mod · GitHub