| 1 |
function test_solve_with_custom_impulse_responses(M_, options_, oo_, x_hat_dash, G, J, F, curlyDs, curlyYs, varargin) |
| 2 |
% Test heterogeneity.solve with custom impulse responses and SSJ validation |
| 3 |
% |
| 4 |
% INPUTS |
| 5 |
% - M_ [structure]: Dynare model structure |
| 6 |
% - options_ [structure]: Dynare options structure |
| 7 |
% - oo_ [structure]: Dynare results structure |
| 8 |
% - x_hat_dash [array]: custom impulse responses |
| 9 |
% - G [structure]: SSJ general equilibrium Jacobians |
| 10 |
% - J [structure]: SSJ heterogeneous-agent Jacobians |
| 11 |
% - F [structure]: SSJ fake news matrices |
| 12 |
% - curlyDs [structure]: SSJ aggregate impulse responses |
| 13 |
% - curlyYs [structure]: SSJ heterogeneous impulse responses |
| 14 |
% - varargin: Model-specific interpolation parameters and optional skip_init flag |
| 15 |
% For one-asset models: a_i, a_pi |
| 16 |
% For two-asset models: a_i, b_i, a_pi, b_pi |
| 17 |
|
| 18 |
% Set up interpolation weights based on model type |
| 19 |
% mat.d.ind and mat.d.w are now [N_om × n_a] matrices where each column |
| 20 |
% corresponds to a state variable (in indices.states order) |
| 21 |
if nargin == 11 |
| 22 |
% One-asset model: column 1 corresponds to 'a' |
| 23 |
interpolation_indices = varargin{1}; |
| 24 |
interpolation_weights = varargin{2}; |
| 25 |
oo_.heterogeneity.mat.d.ind(:, 1) = int32(interpolation_indices(:) + 1); |
| 26 |
oo_.heterogeneity.mat.d.w(:, 1) = interpolation_weights(:); |
| 27 |
elseif nargin == 13 |
| 28 |
% Two-asset model: column 1 = 'b', column 2 = 'a' |
| 29 |
a_i = varargin{1}; |
| 30 |
b_i = varargin{2}; |
| 31 |
a_pi = varargin{3}; |
| 32 |
b_pi = varargin{4}; |
| 33 |
oo_.heterogeneity.mat.d.ind(:, 1) = int32(b_i(:) + 1); |
| 34 |
oo_.heterogeneity.mat.d.ind(:, 2) = int32(a_i(:) + 1); |
| 35 |
oo_.heterogeneity.mat.d.w(:, 1) = b_pi(:); |
| 36 |
oo_.heterogeneity.mat.d.w(:, 2) = a_pi(:); |
| 37 |
end |
| 38 |
|
| 39 |
% Test solve function with custom impulse responses |
| 40 |
options_solve = options_.heterogeneity.solve; |
| 41 |
T = options_solve.truncation_horizon; |
| 42 |
oo_.heterogeneity.dr = heterogeneity.solve(M_, options_solve, oo_.heterogeneity, x_hat_dash); |
| 43 |
|
| 44 |
% Check that oo_.heterogeneity.dr structure contains expected fields |
| 45 |
assert(isfield(oo_.heterogeneity, 'dr'), 'oo_.heterogeneity.dr field missing'); |
| 46 |
dr = oo_.heterogeneity.dr; |
| 47 |
assert(isfield(dr, 'G'), 'dr.G field missing'); |
| 48 |
assert(isfield(dr, 'J_ha'), 'dr.J_ha field missing'); |
| 49 |
assert(isfield(dr, 'F'), 'dr.F field missing'); |
| 50 |
assert(isfield(dr, 'J'), 'dr.J field missing'); |
| 51 |
assert(isfield(dr, 'curlyYs'), 'dr.curlyYs field missing'); |
| 52 |
assert(isfield(dr, 'curlyDs'), 'dr.curlyDs field missing'); |
| 53 |
|
| 54 |
% Test that results with custom impulse responses match SSJ reference |
| 55 |
tol = 1e-12; |
| 56 |
|
| 57 |
% curlyDs comparison - loop over all aggregate variables |
| 58 |
agg = fieldnames(curlyDs); |
| 59 |
for i=1:numel(agg) |
| 60 |
var = agg{i}; |
| 61 |
M = abs(curlyDs.(var) - dr.curlyDs.(var)); |
| 62 |
fprintf('curlyDs.%s vs SSJ reference residual: %.2e\n', var, max(M(:))); |
| 63 |
assert(max(M(:)) <= tol, sprintf('curlyDs.%s differs from SSJ reference with custom impulse responses', var)); |
| 64 |
end |
| 65 |
|
| 66 |
% curlyYs comparison - loop over all variables and heterogeneous states |
| 67 |
het = fieldnames(dr.curlyYs.(agg{1})); |
| 68 |
for i=1:numel(agg) |
| 69 |
var = agg{i}; |
| 70 |
for j=1:numel(het) |
| 71 |
x = het{j}; |
| 72 |
M = abs(curlyYs.(var).(x).' - dr.curlyYs.(var).(x)); |
| 73 |
fprintf('curlyYs.%s.%s vs SSJ reference residual: %.2e\n', var, x, max(M(:))); |
| 74 |
assert(max(M(:)) <= tol, sprintf('curlyYs.%s.%s differs from SSJ reference with custom impulse responses', var, x)); |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
% F matrices comparison - loop over heterogeneous states and aggregate variables |
| 79 |
J_fields = fieldnames(J); |
| 80 |
for j=1:numel(het) |
| 81 |
x = het{j}; |
| 82 |
sum_x = sprintf('SUM_%s', x); |
| 83 |
for i=1:numel(agg) |
| 84 |
var = agg{i}; |
| 85 |
% Map from SSJ naming (capital letters for J) to our naming |
| 86 |
if j <= numel(J_fields) |
| 87 |
M = abs(F.(x).(var) - dr.F.(sum_x).(var)); |
| 88 |
fprintf('F.%s.%s vs SSJ reference residual: %.2e\n', sum_x, var, max(M(:))); |
| 89 |
assert(max(M(:)) <= T*tol, sprintf('F.%s.%s differs from SSJ reference with custom impulse responses', sum_x, var)); |
| 90 |
end |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
% Heterogeneous-agent Jacobians comparison |
| 95 |
for j=1:numel(het) |
| 96 |
x = het{j}; |
| 97 |
sum_x = sprintf('SUM_%s', x); |
| 98 |
for i=1:numel(agg) |
| 99 |
var = agg{i}; |
| 100 |
if j <= numel(J_fields) |
| 101 |
M = abs(J.(x).(var) - dr.J_ha.(sum_x).(var)); |
| 102 |
fprintf('J_ha.%s.%s vs SSJ reference residual: %.2e\n', sum_x, var, max(M(:))); |
| 103 |
assert(max(M(:)) <= T*tol, sprintf('J_ha.%s.%s differs from SSJ reference with custom impulse responses', sum_x, var)); |
| 104 |
end |
| 105 |
end |
| 106 |
end |
| 107 |
|
| 108 |
% General-equilibrium Jacobians comparison |
| 109 |
vars = intersect(fieldnames(G), fieldnames(dr.G)); |
| 110 |
for i=1:numel(vars) |
| 111 |
v = vars{i}; |
| 112 |
for j=1:M_.exo_nbr |
| 113 |
e = M_.exo_names{j}; |
| 114 |
M = abs(dr.G.(v).(e) - G.(v).(e)); |
| 115 |
fprintf('G.%s.%s vs SSJ reference residual: %.2e\n', v, e, max(M(:))); |
| 116 |
assert(max(M(:)) <= 1e-6, sprintf('G.%s.%s differs from SSJ reference', v, e)); |
| 117 |
end |
| 118 |
end |
| 119 |
end |