| 1 |
function [testFailed, testResults] = test_permutation(M_, options_, oo_, steady_state, testFailed, testResults) |
| 2 |
%test_permutation Tests that permutation logic works correctly for pol.order and d.order |
| 3 |
% |
| 4 |
% Verifies that: |
| 5 |
% 1. Valid alternative orderings are accepted by check_steady_state_input |
| 6 |
% 2. Output arrays are correctly permuted to canonical order |
| 7 |
% 3. heterogeneity_solve produces identical G matrices regardless of input order |
| 8 |
% |
| 9 |
% INPUTS: |
| 10 |
% M_ [struct] Dynare model structure |
| 11 |
% options_ [struct] Dynare options structure |
| 12 |
% oo_ [struct] Dynare output structure (must have heterogeneity.mat.G) |
| 13 |
% steady_state [struct] Steady state structure with canonical order |
| 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 |
n_dims = numel(steady_state.pol.order); |
| 22 |
if n_dims == 2 |
| 23 |
fprintf('\nTesting permutation handling (2D)\n'); |
| 24 |
fprintf('=================================\n'); |
| 25 |
else |
| 26 |
fprintf('\nTesting permutation handling (%dD)\n', n_dims); |
| 27 |
fprintf('==================================\n'); |
| 28 |
end |
| 29 |
|
| 30 |
% Store baseline G from canonical order (already solved) |
| 31 |
G_baseline = oo_.heterogeneity.mat.G; |
| 32 |
|
| 33 |
% Get canonical order and sizes |
| 34 |
canonical_order = steady_state.pol.order; |
| 35 |
sizes = zeros(1, n_dims); |
| 36 |
for i = 1:n_dims |
| 37 |
var = canonical_order{i}; |
| 38 |
if isfield(steady_state.shocks.grids, var) |
| 39 |
sizes(i) = numel(steady_state.shocks.grids.(var)); |
| 40 |
else |
| 41 |
sizes(i) = numel(steady_state.pol.grids.(var)); |
| 42 |
end |
| 43 |
end |
| 44 |
|
| 45 |
% Create a non-trivial permutation (circular shift: [2,3,...,n,1]) |
| 46 |
perm_vec = [2:n_dims, 1]; |
| 47 |
new_order = canonical_order(perm_vec); |
| 48 |
|
| 49 |
% Create permuted steady state |
| 50 |
ss_perm = steady_state; |
| 51 |
ss_perm.pol.order = new_order; |
| 52 |
pol_fields = fieldnames(ss_perm.pol.values); |
| 53 |
for i = 1:numel(pol_fields) |
| 54 |
ss_perm.pol.values.(pol_fields{i}) = permute(steady_state.pol.values.(pol_fields{i}), perm_vec); |
| 55 |
end |
| 56 |
ss_perm.d.order = new_order; |
| 57 |
ss_perm.d.hist = permute(steady_state.d.hist, perm_vec); |
| 58 |
|
| 59 |
%% Unit test: check_steady_state_input restores canonical order |
| 60 |
try |
| 61 |
[out_ss, ~, ~] = heterogeneity.check_steady_state_input(M_, options_.heterogeneity.check, ss_perm); |
| 62 |
|
| 63 |
% Verify dimensions are canonical |
| 64 |
assert(isequal(size(out_ss.pol.values.(canonical_order{end})), sizes), ... |
| 65 |
sprintf('pol.values.%s not permuted to canonical dimensions', canonical_order{end})); |
| 66 |
assert(isequal(size(out_ss.d.hist), sizes), 'd.hist not permuted to canonical dimensions'); |
| 67 |
|
| 68 |
% Verify values match original (numerical correctness) |
| 69 |
ref_var = canonical_order{end}; % Use last state variable for comparison |
| 70 |
max_diff_pol = max(abs(out_ss.pol.values.(ref_var)(:) - steady_state.pol.values.(ref_var)(:))); |
| 71 |
assert(max_diff_pol < 1e-14, sprintf('pol.values.%s values differ after permutation: %.2e', ref_var, max_diff_pol)); |
| 72 |
max_diff_hist = max(abs(out_ss.d.hist(:) - steady_state.d.hist(:))); |
| 73 |
assert(max_diff_hist < 1e-14, sprintf('d.hist values differ after permutation: %.2e', max_diff_hist)); |
| 74 |
|
| 75 |
fprintf('✔ check_steady_state_input correctly permutes arrays to canonical order\n'); |
| 76 |
testResults = [testResults; struct('name', sprintf('Permutation %dD: unit test', n_dims), 'passed', true, 'message', '')]; |
| 77 |
catch ME |
| 78 |
testFailed = testFailed + 1; |
| 79 |
fprintf('✘ check_steady_state_input permutation test failed: %s\n', ME.message); |
| 80 |
testResults = [testResults; struct('name', sprintf('Permutation %dD: unit test', n_dims), 'passed', false, 'message', ME.message)]; |
| 81 |
return; % Skip integration test if unit test fails |
| 82 |
end |
| 83 |
|
| 84 |
%% Integration test: solve with permuted input, compare G matrices |
| 85 |
try |
| 86 |
% Load and solve with permuted data directly (no temp file needed) |
| 87 |
oo_.heterogeneity = heterogeneity.load_steady_state(M_, options_.heterogeneity, oo_.heterogeneity, ss_perm); |
| 88 |
oo_.heterogeneity.dr = heterogeneity.solve(M_, options_.heterogeneity.solve, oo_.heterogeneity); |
| 89 |
|
| 90 |
G_permuted = oo_.heterogeneity.mat.G; |
| 91 |
|
| 92 |
% Compare G matrices - should be identical |
| 93 |
max_diff_G = max(abs(G_baseline(:) - G_permuted(:))); |
| 94 |
assert(max_diff_G < 1e-10, sprintf('G matrices differ: max diff = %.2e', max_diff_G)); |
| 95 |
|
| 96 |
fprintf('✔ heterogeneity_solve produces identical G with permuted input (diff: %.2e)\n', max_diff_G); |
| 97 |
testResults = [testResults; struct('name', sprintf('Permutation %dD: integration test', n_dims), 'passed', true, 'message', '')]; |
| 98 |
catch ME |
| 99 |
testFailed = testFailed + 1; |
| 100 |
fprintf('✘ heterogeneity_solve permutation test failed: %s\n', ME.message); |
| 101 |
testResults = [testResults; struct('name', sprintf('Permutation %dD: integration test', n_dims), 'passed', false, 'message', ME.message)]; |
| 102 |
end |
| 103 |
|
| 104 |
end |