| 1 |
function [testFailed, testResults] = test_simulate_stochastic(M_, options_, oo_, steady_state, testFailed, testResults) |
| 2 |
%test_simulate_stochastic Tests for heterogeneity.simulate in stochastic mode |
| 3 |
% |
| 4 |
% Tests core functionality for IRF computation and stochastic simulations |
| 5 |
% including combined IRF + simulation mode |
| 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 |
% testFailed [scalar] Number of tests failed so far |
| 14 |
% testResults [array] Array of test result structures |
| 15 |
% |
| 16 |
% OUTPUTS: |
| 17 |
% testFailed [scalar] Updated number of tests failed |
| 18 |
% testResults [array] Updated array of test results |
| 19 |
|
| 20 |
fprintf('\n'); |
| 21 |
fprintf('Testing heterogeneity.simulate function\n'); |
| 22 |
fprintf('===============================\n'); |
| 23 |
|
| 24 |
% Verify that model was already initialized and solved |
| 25 |
try |
| 26 |
assert(isfield(oo_, 'heterogeneity') && isfield(oo_.heterogeneity, 'dr') && isfield(oo_.heterogeneity.dr, 'G'), ... |
| 27 |
'Solution not available (model not initialized/solved)'); |
| 28 |
fprintf('✔ Using pre-initialized and pre-solved model\n'); |
| 29 |
catch ME |
| 30 |
fprintf('❌ Model not properly initialized: %s\n', ME.message); |
| 31 |
testResults = [testResults; struct('name', 'Model verification', 'passed', false, 'message', ME.message)]; |
| 32 |
testFailed = testFailed + 1; |
| 33 |
return; |
| 34 |
end |
| 35 |
|
| 36 |
%% Test 1: Default IRFs for all shocks |
| 37 |
try |
| 38 |
options_.nograph = true; |
| 39 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 40 |
assert(isfield(oo_, 'irfs'), 'oo_.irfs must exist'); |
| 41 |
|
| 42 |
% Check that IRFs exist for all shock-variable pairs |
| 43 |
n_irfs = 0; |
| 44 |
for i_shock = 1:M_.exo_nbr |
| 45 |
shock_name = M_.exo_names{i_shock}; |
| 46 |
for i_var = 1:M_.endo_nbr |
| 47 |
var_name = M_.endo_names{i_var}; |
| 48 |
field_name = [var_name '_' shock_name]; |
| 49 |
if isfield(oo_.irfs, field_name) |
| 50 |
n_irfs = n_irfs + 1; |
| 51 |
assert(isvector(oo_.irfs.(field_name)), sprintf('IRF for %s must be a vector', field_name)); |
| 52 |
end |
| 53 |
end |
| 54 |
end |
| 55 |
fprintf(' oo_.irfs contains %d IRFs for %d shocks\n', n_irfs, M_.exo_nbr); |
| 56 |
testResults = [testResults; struct('name', 'Default IRFs for all shocks', 'passed', true, 'message', '')]; |
| 57 |
fprintf('✔ Default IRFs for all shocks\n'); |
| 58 |
catch ME |
| 59 |
testResults = [testResults; struct('name', 'Default IRFs for all shocks', 'passed', false, 'message', ME.message)]; |
| 60 |
fprintf('❌ Default IRFs for all shocks: %s\n', ME.message); |
| 61 |
testFailed = testFailed + 1; |
| 62 |
end |
| 63 |
|
| 64 |
%% Test 2: IRF for subset of shocks using irf_shocks option |
| 65 |
try |
| 66 |
shock_name = M_.exo_names{1}; |
| 67 |
options_.irf_shocks = {shock_name}; |
| 68 |
options_.nograph = true; |
| 69 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 70 |
assert(isfield(oo_, 'irfs'), 'oo_.irfs must exist'); |
| 71 |
|
| 72 |
% Check that IRFs exist only for the specified shock |
| 73 |
for i_var = 1:M_.endo_nbr |
| 74 |
var_name = M_.endo_names{i_var}; |
| 75 |
field_name = [var_name '_' shock_name]; |
| 76 |
if isfield(oo_.heterogeneity.dr.G, var_name) && isfield(oo_.heterogeneity.dr.G.(var_name), shock_name) |
| 77 |
assert(isfield(oo_.irfs, field_name), sprintf('Variable %s_%s missing from oo_.irfs', var_name, shock_name)); |
| 78 |
end |
| 79 |
end |
| 80 |
fprintf(' IRFs computed for shock "%s" only\n', shock_name); |
| 81 |
testResults = [testResults; struct('name', 'IRF for subset of shocks', 'passed', true, 'message', '')]; |
| 82 |
fprintf('✔ IRF for subset of shocks\n'); |
| 83 |
catch ME |
| 84 |
testResults = [testResults; struct('name', 'IRF for subset of shocks', 'passed', false, 'message', ME.message)]; |
| 85 |
fprintf('❌ IRF for subset of shocks: %s\n', ME.message); |
| 86 |
testFailed = testFailed + 1; |
| 87 |
end |
| 88 |
|
| 89 |
%% Test 3: Variable subset using positional arguments |
| 90 |
try |
| 91 |
shock_name = M_.exo_names{1}; |
| 92 |
if M_.endo_nbr >= 3 |
| 93 |
var_subset = M_.endo_names(1:3); |
| 94 |
else |
| 95 |
var_subset = M_.endo_names; |
| 96 |
end |
| 97 |
options_.irf_shocks = {shock_name}; |
| 98 |
options_.nograph = true; |
| 99 |
oo_ = heterogeneity.simulate(M_, options_, oo_, var_subset); |
| 100 |
|
| 101 |
% Only the requested variables should have IRFs stored |
| 102 |
for i = 1:length(var_subset) |
| 103 |
field_name = [var_subset{i} '_' shock_name]; |
| 104 |
if isfield(oo_.heterogeneity.dr.G, var_subset{i}) && isfield(oo_.heterogeneity.dr.G.(var_subset{i}), shock_name) |
| 105 |
assert(isfield(oo_.irfs, field_name), ... |
| 106 |
sprintf('Variable %s_%s missing from oo_.irfs', var_subset{i}, shock_name)); |
| 107 |
end |
| 108 |
end |
| 109 |
fprintf(' Subset of %d variables computed correctly\n', length(var_subset)); |
| 110 |
testResults = [testResults; struct('name', 'Variable subset', 'passed', true, 'message', '')]; |
| 111 |
fprintf('✔ Variable subset\n'); |
| 112 |
catch ME |
| 113 |
testResults = [testResults; struct('name', 'Variable subset', 'passed', false, 'message', ME.message)]; |
| 114 |
fprintf('❌ Variable subset: %s\n', ME.message); |
| 115 |
testFailed = testFailed + 1; |
| 116 |
end |
| 117 |
|
| 118 |
%% Test 4: Custom IRF horizon |
| 119 |
try |
| 120 |
shock_name = M_.exo_names{1}; |
| 121 |
T_custom = 50; |
| 122 |
options_.irf = T_custom; |
| 123 |
options_.irf_shocks = {shock_name}; |
| 124 |
options_.nograph = true; |
| 125 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 126 |
var_name = M_.endo_names{1}; |
| 127 |
field_name = [var_name '_' shock_name]; |
| 128 |
assert(length(oo_.irfs.(field_name)) == T_custom, ... |
| 129 |
sprintf('Expected horizon %d, got %d', T_custom, length(oo_.irfs.(field_name)))); |
| 130 |
fprintf(' Custom horizon irf=%d verified\n', T_custom); |
| 131 |
testResults = [testResults; struct('name', 'Custom IRF horizon', 'passed', true, 'message', '')]; |
| 132 |
fprintf('✔ Custom IRF horizon\n'); |
| 133 |
catch ME |
| 134 |
testResults = [testResults; struct('name', 'Custom IRF horizon', 'passed', false, 'message', ME.message)]; |
| 135 |
fprintf('❌ Custom IRF horizon: %s\n', ME.message); |
| 136 |
testFailed = testFailed + 1; |
| 137 |
end |
| 138 |
|
| 139 |
%% Test 5: Stochastic simulation with periods option |
| 140 |
try |
| 141 |
T_sim = 100; |
| 142 |
T_drop = 20; |
| 143 |
|
| 144 |
% Check if all shocks have non-zero variance |
| 145 |
if isfield(M_, 'Sigma_e') && ~isempty(M_.Sigma_e) && all(diag(M_.Sigma_e) > 0) |
| 146 |
options_.periods = T_sim; |
| 147 |
options_.drop = T_drop; |
| 148 |
options_.nograph = true; |
| 149 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 150 |
assert(isfield(oo_, 'endo_simul'), 'oo_.endo_simul must exist for stochastic simulation'); |
| 151 |
assert(size(oo_.endo_simul, 1) == M_.endo_nbr, 'oo_.endo_simul should have n_endo rows'); |
| 152 |
assert(size(oo_.endo_simul, 2) == T_sim, sprintf('Expected %d periods, got %d', T_sim, size(oo_.endo_simul, 2))); |
| 153 |
fprintf(' Stochastic simulation: %d periods, %d drop\n', T_sim, T_drop); |
| 154 |
testResults = [testResults; struct('name', 'Stochastic simulation with periods', 'passed', true, 'message', '')]; |
| 155 |
fprintf('✔ Stochastic simulation with periods\n'); |
| 156 |
else |
| 157 |
fprintf(' Skipped: Some shocks have zero variance\n'); |
| 158 |
testResults = [testResults; struct('name', 'Stochastic simulation with periods', 'passed', true, 'message', 'Skipped (zero variance shocks)')]; |
| 159 |
fprintf('✔ Stochastic simulation with periods (skipped)\n'); |
| 160 |
end |
| 161 |
catch ME |
| 162 |
testResults = [testResults; struct('name', 'Stochastic simulation with periods', 'passed', false, 'message', ME.message)]; |
| 163 |
fprintf('❌ Stochastic simulation with periods: %s\n', ME.message); |
| 164 |
testFailed = testFailed + 1; |
| 165 |
end |
| 166 |
|
| 167 |
%% Test 6: Error handling - unknown shock in irf_shocks |
| 168 |
try |
| 169 |
try |
| 170 |
options_.irf_shocks = {'nonexistent_shock'}; |
| 171 |
options_.nograph = true; |
| 172 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 173 |
error('Should have thrown an error for unknown shock'); |
| 174 |
catch ME |
| 175 |
assert(strcmp(ME.identifier, 'heterogeneity:simulate:UnknownShock'), ... |
| 176 |
'Wrong error identifier'); |
| 177 |
end |
| 178 |
fprintf(' Unknown shock correctly rejected\n'); |
| 179 |
testResults = [testResults; struct('name', 'Error handling: unknown shock', 'passed', true, 'message', '')]; |
| 180 |
fprintf('✔ Error handling: unknown shock\n'); |
| 181 |
catch ME |
| 182 |
testResults = [testResults; struct('name', 'Error handling: unknown shock', 'passed', false, 'message', ME.message)]; |
| 183 |
fprintf('❌ Error handling: unknown shock: %s\n', ME.message); |
| 184 |
testFailed = testFailed + 1; |
| 185 |
end |
| 186 |
|
| 187 |
%% Test 7: Plot generation (conditional - only if graphics enabled) |
| 188 |
if ~options_.nograph |
| 189 |
try |
| 190 |
shock_name = M_.exo_names{1}; |
| 191 |
options_.irf_shocks = {shock_name}; |
| 192 |
options_.nograph = false; |
| 193 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 194 |
assert(isfield(oo_, 'irfs'), 'oo_.irfs must exist'); |
| 195 |
assert(~isempty(fieldnames(oo_.irfs)), 'oo_.irfs should not be empty'); |
| 196 |
|
| 197 |
% Check if any IRFs exceed threshold (only then should plots be created) |
| 198 |
threshold = 1e-10; |
| 199 |
if isfield(options_, 'impulse_responses') && isfield(options_.impulse_responses, 'plot_threshold') |
| 200 |
threshold = options_.impulse_responses.plot_threshold; |
| 201 |
end |
| 202 |
|
| 203 |
irf_fields = fieldnames(oo_.irfs); |
| 204 |
max_irf = 0; |
| 205 |
for i = 1:length(irf_fields) |
| 206 |
max_irf = max(max_irf, max(abs(oo_.irfs.(irf_fields{i})))); |
| 207 |
end |
| 208 |
|
| 209 |
if max_irf >= threshold |
| 210 |
% IRFs exceed threshold, plots should be created |
| 211 |
assert(isfolder([M_.dname '/graphs']), 'Graphs directory should exist'); |
| 212 |
graph_files = dir([M_.dname '/graphs/' M_.fname '_IRF_' shock_name '*']); |
| 213 |
assert(~isempty(graph_files), 'Plot file should have been created'); |
| 214 |
fprintf(' Created %d graph file(s)\n', length(graph_files)); |
| 215 |
else |
| 216 |
% IRFs below threshold, no plots should be created |
| 217 |
fprintf(' IRFs below threshold, no plots created (as expected)\n'); |
| 218 |
end |
| 219 |
testResults = [testResults; struct('name', 'Plot generation (default)', 'passed', true, 'message', '')]; |
| 220 |
fprintf('✔ Plot generation (default)\n'); |
| 221 |
catch ME |
| 222 |
testResults = [testResults; struct('name', 'Plot generation (default)', 'passed', false, 'message', ME.message)]; |
| 223 |
fprintf('❌ Plot generation (default): %s\n', ME.message); |
| 224 |
testFailed = testFailed + 1; |
| 225 |
end |
| 226 |
|
| 227 |
%% Test 8: No plot with nograph option |
| 228 |
try |
| 229 |
% Use second shock if available |
| 230 |
shock_idx = min(2, M_.exo_nbr); |
| 231 |
shock_name = M_.exo_names{shock_idx}; |
| 232 |
if isfolder([M_.dname '/graphs']) |
| 233 |
existing = dir([M_.dname '/graphs/' M_.fname '_IRF_' shock_name '*']); |
| 234 |
for i = 1:length(existing) |
| 235 |
delete([M_.dname '/graphs/' existing(i).name]); |
| 236 |
end |
| 237 |
end |
| 238 |
options_.irf_shocks = {shock_name}; |
| 239 |
options_.nograph = true; |
| 240 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 241 |
assert(isfield(oo_, 'irfs'), 'oo_.irfs must exist'); |
| 242 |
graph_files = dir([M_.dname '/graphs/' M_.fname '_IRF_' shock_name '*']); |
| 243 |
assert(isempty(graph_files), 'No plot files should have been created'); |
| 244 |
fprintf(' Correctly skipped plotting\n'); |
| 245 |
testResults = [testResults; struct('name', 'No plot with nograph', 'passed', true, 'message', '')]; |
| 246 |
fprintf('✔ No plot with nograph\n'); |
| 247 |
catch ME |
| 248 |
testResults = [testResults; struct('name', 'No plot with nograph', 'passed', false, 'message', ME.message)]; |
| 249 |
fprintf('❌ No plot with nograph: %s\n', ME.message); |
| 250 |
testFailed = testFailed + 1; |
| 251 |
end |
| 252 |
end |
| 253 |
|
| 254 |
%% Test 9: relative_irf option |
| 255 |
try |
| 256 |
% Find a shock with non-zero variance |
| 257 |
shock_idx = 1; |
| 258 |
for i = 1:M_.exo_nbr |
| 259 |
if isfield(M_, 'Sigma_e') && ~isempty(M_.Sigma_e) && size(M_.Sigma_e, 1) >= i |
| 260 |
if M_.Sigma_e(i, i) > 0 |
| 261 |
shock_idx = i; |
| 262 |
break; |
| 263 |
end |
| 264 |
end |
| 265 |
end |
| 266 |
shock_name = M_.exo_names{shock_idx}; |
| 267 |
|
| 268 |
% Compute IRFs with and without relative_irf |
| 269 |
options_.periods = 0; % Reset to default (IRFs only, no simulation paths) |
| 270 |
options_.irf_shocks = {shock_name}; |
| 271 |
options_.relative_irf = false; |
| 272 |
options_.nograph = true; |
| 273 |
oo_std = heterogeneity.simulate(M_, options_, oo_, {}); |
| 274 |
|
| 275 |
options_.relative_irf = true; |
| 276 |
oo_rel = heterogeneity.simulate(M_, options_, oo_, {}); |
| 277 |
|
| 278 |
% Get shock standard deviation |
| 279 |
shock_std = sqrt(M_.Sigma_e(shock_idx, shock_idx)); |
| 280 |
|
| 281 |
% Check relationship: relative_irf should give (1/shock_std) * 100 times the standard IRF |
| 282 |
var_name = M_.endo_names{1}; |
| 283 |
field_name = [var_name '_' shock_name]; |
| 284 |
|
| 285 |
if isfield(oo_std.irfs, field_name) && isfield(oo_rel.irfs, field_name) |
| 286 |
% Check if IRF values are non-zero (can be zero if shock has zero variance) |
| 287 |
irf_std_val = oo_std.irfs.(field_name)(1); |
| 288 |
irf_rel_val = oo_rel.irfs.(field_name)(1); |
| 289 |
|
| 290 |
if abs(irf_std_val) > 1e-14 && abs(irf_rel_val) > 1e-14 |
| 291 |
% relative_irf uses unit shock * 100, standard uses shock_std |
| 292 |
% So: oo_rel = (1/shock_std) * 100 * oo_std |
| 293 |
expected_ratio = 100 / shock_std; |
| 294 |
actual_ratio = irf_rel_val / irf_std_val; |
| 295 |
|
| 296 |
assert(abs(actual_ratio - expected_ratio) < 1e-10, ... |
| 297 |
sprintf('relative_irf ratio incorrect: expected %.6f, got %.6f', expected_ratio, actual_ratio)); |
| 298 |
|
| 299 |
fprintf(' relative_irf scaling verified (ratio: %.2f)\n', actual_ratio); |
| 300 |
testResults = [testResults; struct('name', 'relative_irf option', 'passed', true, 'message', '')]; |
| 301 |
fprintf('✔ relative_irf option\n'); |
| 302 |
else |
| 303 |
fprintf(' Skipped: IRF values too small (shock has zero/near-zero variance)\n'); |
| 304 |
testResults = [testResults; struct('name', 'relative_irf option', 'passed', true, 'message', 'Skipped (zero variance)')]; |
| 305 |
end |
| 306 |
else |
| 307 |
fprintf(' Skipped: IRF field not found (likely below threshold)\n'); |
| 308 |
testResults = [testResults; struct('name', 'relative_irf option', 'passed', true, 'message', 'Skipped (IRF below threshold)')]; |
| 309 |
end |
| 310 |
catch ME |
| 311 |
testResults = [testResults; struct('name', 'relative_irf option', 'passed', false, 'message', ME.message)]; |
| 312 |
fprintf('❌ relative_irf option: %s\n', ME.message); |
| 313 |
testFailed = testFailed + 1; |
| 314 |
end |
| 315 |
|
| 316 |
%% Test: Combined IRFs and stochastic simulation |
| 317 |
fprintf('\n--- Testing combined IRFs + stochastic simulation ---\n'); |
| 318 |
try |
| 319 |
options_.periods = 100; |
| 320 |
options_.drop = 50; |
| 321 |
options_.irf = 50; |
| 322 |
options_.irf_shocks = {}; % All shocks |
| 323 |
options_.nograph = true; |
| 324 |
|
| 325 |
oo_ = heterogeneity.simulate(M_, options_, oo_, {}); |
| 326 |
|
| 327 |
% Verify both outputs exist |
| 328 |
assert(isfield(oo_, 'irfs'), 'IRFs should be computed when periods > 0'); |
| 329 |
assert(isfield(oo_, 'endo_simul'), 'Simulation should be computed when periods > 0'); |
| 330 |
|
| 331 |
% Check simulation dimensions |
| 332 |
expected_cols = options_.periods; % No initial state column (matches stoch_simul) |
| 333 |
actual_cols = size(oo_.endo_simul, 2); |
| 334 |
assert(actual_cols == expected_cols, ... |
| 335 |
sprintf('Simulation length mismatch: expected %d, got %d', expected_cols, actual_cols)); |
| 336 |
|
| 337 |
% Check IRFs exist for at least one variable-shock pair |
| 338 |
irf_fields = fieldnames(oo_.irfs); |
| 339 |
assert(~isempty(irf_fields), 'At least one IRF should be computed'); |
| 340 |
|
| 341 |
% Check IRF length |
| 342 |
first_irf = oo_.irfs.(irf_fields{1}); |
| 343 |
assert(length(first_irf) == options_.irf, ... |
| 344 |
sprintf('IRF length mismatch: expected %d, got %d', options_.irf, length(first_irf))); |
| 345 |
|
| 346 |
fprintf('✓ Combined IRFs + simulation mode works correctly\n'); |
| 347 |
testResults = [testResults; struct('name', 'combined IRFs + simulation', 'passed', true, 'message', '')]; |
| 348 |
catch ME |
| 349 |
testResults = [testResults; struct('name', 'combined IRFs + simulation', 'passed', false, 'message', ME.message)]; |
| 350 |
fprintf('❌ Combined IRFs + simulation: %s\n', ME.message); |
| 351 |
testFailed = testFailed + 1; |
| 352 |
end |
| 353 |
|
| 354 |
end |