Debian Codegraph @

tests/heterogeneity/test_simulate_news_shocks.m

raw
1 function [testFailed, testResults] = test_simulate_news_shocks(M_, options_, oo_, steady_state, sim_result, testFailed, testResults)
2 %test_simulate_news_shocks Tests for heterogeneity.simulate with news shock sequences
3 %
4 % Tests news shock sequence mode using M_.det_shocks populated from shocks block
5 % with anticipated shocks (periods/values keywords) known at t=0.
6 % Initializes and solves the model before running simulation tests.
7 %
8 % INPUTS:
9 % M_ [struct] Dynare model structure
10 % options_ [struct] Dynare options structure
11 % oo_ [struct] Dynare results structure
12 % steady_state [struct] Steady state structure
13 % sim_result [struct] Python simulation results (can be empty)
14 % testFailed [scalar] Number of tests failed so far
15 % testResults [array] Array of test result structures
16 %
17 % OUTPUTS:
18 % testFailed [scalar] Updated number of tests failed
19 % testResults [array] Updated array of test results
20
21 fprintf('\n');
22 fprintf('Testing heterogeneity.simulate with news shock sequences\n');
23 fprintf('=========================================================\n');
24
25 % Verify that model was already initialized and solved
26 try
27 assert(isfield(oo_, 'heterogeneity') && isfield(oo_.heterogeneity, 'dr') && isfield(oo_.heterogeneity.dr, 'G'), ...
28 'Solution not available (model not initialized/solved)');
29 fprintf('✔ Using pre-initialized and pre-solved model\n');
30 catch ME
31 fprintf('❌ Model not properly initialized: %s\n', ME.message);
32 testResults = [testResults; struct('name', 'Model verification (news shocks)', 'passed', false, 'message', ME.message)];
33 testFailed = testFailed + 1;
34 return;
35 end
36
37 %% Test 1: News shock simulation with M_.det_shocks
38 try
39 % Verify M_.det_shocks exists (should be populated by shocks block with periods/values)
40 assert(isfield(M_, 'det_shocks') && ~isempty(M_.det_shocks), ...
41 'M_.det_shocks must exist for news shock test');
42
43 % Run news shock simulation (no 'periods' parameter needed - inferred from options_.periods)
44 options_.nograph = true;
45 oo_det = heterogeneity.simulate(M_, options_, oo_, {});
46
47 % Verify outputs
48 assert(isfield(oo_det, 'endo_simul'), 'oo_.endo_simul must exist for news shock simulation');
49 assert(isfield(oo_det, 'exo_simul'), 'oo_.exo_simul must exist (populated by make_ex_)');
50 assert(size(oo_det.endo_simul, 1) == M_.endo_nbr, 'oo_.endo_simul should have n_endo rows');
51
52 fprintf(' News shock simulation completed with %d periods\n', size(oo_det.endo_simul, 2) - 1);
53 testResults = [testResults; struct('name', 'News shock simulation basic', 'passed', true, 'message', '')];
54 fprintf('✔ News shock simulation basic\n');
55 catch ME
56 testResults = [testResults; struct('name', 'News shock simulation basic', 'passed', false, 'message', ME.message)];
57 fprintf('❌ News shock simulation basic: %s\n', ME.message);
58 testFailed = testFailed + 1;
59 return; % Cannot proceed with other tests
60 end
61
62 %% Test 2: Verify shock values in oo_.exo_simul
63 try
64 % Check that oo_.exo_simul contains the shock values from M_.det_shocks
65 for i = 1:length(M_.det_shocks)
66 shock_entry = M_.det_shocks(i);
67 if shock_entry.exo_det
68 continue; % Skip deterministic exogenous variables
69 end
70
71 shock_idx = shock_entry.exo_id;
72 shock_name = M_.exo_names{shock_idx};
73 periods = shock_entry.periods;
74 expected_value = shock_entry.value;
75
76 % Periods in exo_simul are offset by M_.maximum_lag
77 exo_periods = M_.maximum_lag + periods;
78
79 for p_idx = 1:length(exo_periods)
80 p = exo_periods(p_idx);
81 if p <= size(oo_det.exo_simul, 1)
82 actual_value = oo_det.exo_simul(p, shock_idx);
83 if abs(actual_value - expected_value) >= 1e-10
84 error('Shock %s at period %d incorrect: expected %.6f, got %.6f', ...
85 shock_name, periods(p_idx), expected_value, actual_value);
86 end
87 end
88 end
89 end
90
91 fprintf(' Shock values verified in oo_.exo_simul\n');
92 testResults = [testResults; struct('name', 'Shock values verification', 'passed', true, 'message', '')];
93 fprintf('✔ Shock values verification\n');
94 catch ME
95 testResults = [testResults; struct('name', 'Shock values verification', 'passed', false, 'message', ME.message)];
96 fprintf('❌ Shock values verification: %s\n', ME.message);
97 testFailed = testFailed + 1;
98 end
99
100 %% Test 3: Verify endogenous response is non-zero
101
102 % Build the vector of steady-state aggregate endogenous variables
103 steady_state = zeros(M_.endo_nbr, 1);
104 agg_vars = fieldnames(oo_.heterogeneity.steady_state.agg);
105 for i = 1:length(agg_vars)
106 var_name = agg_vars{i};
107 var_idx = find(strcmp(var_name, M_.endo_names));
108 if ~isempty(var_idx)
109 steady_state(var_idx) = oo_.heterogeneity.steady_state.agg.(var_name);
110 end
111 end
112
113 try
114 % Check that at least one endogenous variable responds to news shocks
115 Y_idx = find(strcmp('Y', M_.endo_names));
116 if ~isempty(Y_idx)
117 Y_path = oo_det.endo_simul(Y_idx, :);
118 Y_deviation = Y_path - steady_state(Y_idx);
119 max_deviation = max(abs(Y_deviation));
120
121 assert(max_deviation > 1e-10, ...
122 'Output Y should respond to news shocks');
123
124 fprintf(' Output Y max deviation from steady state: %.6f\n', max_deviation);
125 testResults = [testResults; struct('name', 'Endogenous response verification', 'passed', true, 'message', '')];
126 fprintf('✔ Endogenous response verification\n');
127 else
128 fprintf(' Skipped: Variable Y not found in model\n');
129 testResults = [testResults; struct('name', 'Endogenous response verification', 'passed', true, 'message', 'Skipped (Y not found)')];
130 end
131 catch ME
132 testResults = [testResults; struct('name', 'Endogenous response verification', 'passed', false, 'message', ME.message)];
133 fprintf('❌ Endogenous response verification: %s\n', ME.message);
134 testFailed = testFailed + 1;
135 end
136
137 %% Test 4: Compare MATLAB simulation with Python sim_result
138 try
139 % Check if Python sim_result was passed as input
140 if ~isempty(sim_result) && isstruct(sim_result)
141 fprintf(' Comparing MATLAB simulation with Python sim_result...\n');
142
143 max_diff = 0;
144 max_diff_var = '';
145 n_compared = 0;
146
147 % Compare each variable that exists in both MATLAB and Python results
148 var_names = fieldnames(sim_result);
149 for i = 1:length(var_names)
150 var_name = var_names{i};
151
152 % Find variable index in MATLAB output
153 var_idx = find(strcmp(var_name, M_.endo_names));
154 if isempty(var_idx)
155 continue;
156 end
157
158 % Extract MATLAB simulation (deviations from steady state)
159 matlab_sim = oo_det.endo_simul(var_idx, :)' - steady_state(var_idx);
160
161 % Extract Python simulation
162 python_sim = sim_result.(var_name).';
163
164 % Compare only over common length
165 T_compare = min(length(matlab_sim), length(python_sim));
166 diff = abs(matlab_sim(1:T_compare) - python_sim(1:T_compare));
167 var_max_diff = max(diff);
168
169 if var_max_diff > max_diff
170 max_diff = var_max_diff;
171 max_diff_var = var_name;
172 end
173
174 n_compared = n_compared + 1;
175 end
176
177 % Tolerance for numerical differences
178 tol = 1e-5;
179
180 if n_compared > 0
181 fprintf(' Compared %d variables\n', n_compared);
182 fprintf(' Maximum difference: %.2e (variable: %s)\n', max_diff, max_diff_var);
183
184 assert(max_diff < tol, ...
185 sprintf('MATLAB-Python mismatch exceeds tolerance: %.2e > %.2e for variable %s', ...
186 max_diff, tol, max_diff_var));
187
188 testResults = [testResults; struct('name', 'MATLAB vs Python simulation consistency', 'passed', true, 'message', '')];
189 fprintf('✔ MATLAB vs Python simulation consistency\n');
190 else
191 fprintf(' No variables found for comparison\n');
192 testResults = [testResults; struct('name', 'MATLAB vs Python simulation consistency', 'passed', true, 'message', 'Skipped (no common variables)')];
193 end
194 else
195 fprintf(' Python sim_result not provided, skipping comparison\n');
196 testResults = [testResults; struct('name', 'MATLAB vs Python simulation consistency', 'passed', true, 'message', 'Skipped (no Python data)')];
197 end
198 catch ME
199 testResults = [testResults; struct('name', 'MATLAB vs Python simulation consistency', 'passed', false, 'message', ME.message)];
200 fprintf('❌ MATLAB vs Python simulation consistency: %s\n', ME.message);
201 testFailed = testFailed + 1;
202 end
203
204 end