| 1 |
% Tests for preprocessor capabilities of skew in (stochastic) shocks block |
| 2 |
% Note: manual tests for error messages by uncommenting specific lines |
| 3 |
% ------------------------------------------------------------------------- |
| 4 |
|
| 5 |
% Copyright © 2025 Dynare Team |
| 6 |
% |
| 7 |
% This file is part of Dynare. |
| 8 |
% |
| 9 |
% Dynare is free software: you can redistribute it and/or modify |
| 10 |
% it under the terms of the GNU General Public License as published by |
| 11 |
% the Free Software Foundation, either version 3 of the License, or |
| 12 |
% (at your option) any later version. |
| 13 |
% |
| 14 |
% Dynare is distributed in the hope that it will be useful, |
| 15 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
% GNU General Public License for more details. |
| 18 |
% |
| 19 |
% You should have received a copy of the GNU General Public License |
| 20 |
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
@#include "ireland2004_model.inc" |
| 23 |
|
| 24 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 25 |
% TEST 0: check different ways to specify skewness and co-skewness % |
| 26 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 27 |
shocks; |
| 28 |
% Single shock notation |
| 29 |
skew eta_a, eta_a, eta_a = 1; |
| 30 |
% skew eta_a = 1; % same as above |
| 31 |
skew eta_e = 2; |
| 32 |
skew eta_r = 3; |
| 33 |
skew eta_z = 4; |
| 34 |
%skew eta_u = 5; % triggers error because of undeclared variable (eta_u) |
| 35 |
%skew yhat = 6; % triggers error because skewness only allowed for exogenous |
| 36 |
%skew ALPHA_PI = 7; % triggers error because skewness only allowed for exogenous |
| 37 |
|
| 38 |
% Two same shocks, one different |
| 39 |
skew eta_a, eta_e, eta_a = 10; |
| 40 |
skew eta_e, eta_r, eta_e = 11; |
| 41 |
skew eta_z, eta_a, eta_z = 12; |
| 42 |
skew eta_r, eta_r, eta_z = 13; |
| 43 |
%skew eta_r, eta_r, eta_u = 14; % triggers error because of undeclared variable (eta_u) [nostrict works] |
| 44 |
%skew eta_r, eta_r, yhat = 15; % triggers error because skewness only allowed for exogenous |
| 45 |
%skew eta_r, ALPHA_PI, eta_e = 16; % triggers error because skewness only allowed for exogenous |
| 46 |
|
| 47 |
% All three shocks different |
| 48 |
skew eta_a, eta_e, eta_r = 20; |
| 49 |
%skew eta_e, eta_a, eta_r = 20; % Same as above, just different order |
| 50 |
%skew eta_r, eta_a, eta_e = 20; % Same as above, just different order |
| 51 |
skew eta_a, eta_e, eta_z = 21; |
| 52 |
skew eta_a, eta_r, eta_z = 22; |
| 53 |
skew eta_e, eta_r, eta_z = 23; |
| 54 |
|
| 55 |
% Fill remaining two-same-one-different families |
| 56 |
skew eta_a, eta_a, eta_z = 31; |
| 57 |
skew eta_a, eta_a, eta_r = 32; |
| 58 |
skew eta_e, eta_e, eta_a = 33; |
| 59 |
skew eta_e, eta_e, eta_z = 34; |
| 60 |
skew eta_z, eta_z, eta_e = 35; |
| 61 |
skew eta_z, eta_z, eta_r = 36; |
| 62 |
skew eta_r, eta_r, eta_a = 37; |
| 63 |
% Intentionally leave one family unset so its permutations remain zero |
| 64 |
% skew eta_r, eta_r, eta_e = 38 |
| 65 |
end; |
| 66 |
|
| 67 |
% Helper to look up skewness value from sparse Skew_e (sorts indices, returns 0 if not found) |
| 68 |
skew_lookup = @(S,i,j,k) sum(S(S(:,1)==min([i j k]) & S(:,2)==median([i j k]) & S(:,3)==max([i j k]), 4)); |
| 69 |
|
| 70 |
fprintf('\n======= M_.Skew_e (sparse 4-column format, sorted indices) =======\n'); |
| 71 |
disp(M_.Skew_e); |
| 72 |
fprintf('\n=========================\n'); |
| 73 |
|
| 74 |
fprintf('\n=== Testing Co-Skewness Sparse Matrix ===\n\n'); |
| 75 |
fprintf('Number of stored entries: %d\n\n', size(M_.Skew_e, 1)); |
| 76 |
|
| 77 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 78 |
% TEST 1: Check that indices are stored in increasing order % |
| 79 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 80 |
fprintf('Test 1: Checking sorted index order...\n'); |
| 81 |
errors = 0; |
| 82 |
|
| 83 |
for row = 1:size(M_.Skew_e, 1) |
| 84 |
i = M_.Skew_e(row, 1); |
| 85 |
j = M_.Skew_e(row, 2); |
| 86 |
k = M_.Skew_e(row, 3); |
| 87 |
|
| 88 |
if ~(i <= j && j <= k) |
| 89 |
errors = errors + 1; |
| 90 |
fprintf(' ERROR: Row %d has unsorted indices (%d, %d, %d)\n', row, i, j, k); |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
if errors == 0 |
| 95 |
fprintf(' PASSED: All %d entries have sorted indices (i <= j <= k)!\n\n', size(M_.Skew_e, 1)); |
| 96 |
else |
| 97 |
error(' FAILED: Found %d rows with unsorted indices\n\n', errors); |
| 98 |
end |
| 99 |
|
| 100 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 101 |
% TEST 2: Check that any permutation lookup returns correct % |
| 102 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 103 |
fprintf('Test 2: Checking that any-permutation lookup works...\n'); |
| 104 |
errors = 0; |
| 105 |
total_checks = 0; |
| 106 |
|
| 107 |
for row = 1:size(M_.Skew_e, 1) |
| 108 |
i = M_.Skew_e(row, 1); |
| 109 |
j = M_.Skew_e(row, 2); |
| 110 |
k = M_.Skew_e(row, 3); |
| 111 |
v = M_.Skew_e(row, 4); |
| 112 |
|
| 113 |
% Look up all 6 permutations via skew_lookup (which sorts internally) |
| 114 |
perm_vals = [skew_lookup(M_.Skew_e,i,j,k), skew_lookup(M_.Skew_e,i,k,j), ... |
| 115 |
skew_lookup(M_.Skew_e,j,i,k), skew_lookup(M_.Skew_e,j,k,i), ... |
| 116 |
skew_lookup(M_.Skew_e,k,i,j), skew_lookup(M_.Skew_e,k,j,i)]; |
| 117 |
|
| 118 |
if ~iszero(perm_vals - v) |
| 119 |
errors = errors + 1; |
| 120 |
fprintf(' ERROR: Lookup mismatch for (%s, %s, %s)\n', ... |
| 121 |
M_.exo_names{i}, M_.exo_names{j}, M_.exo_names{k}); |
| 122 |
end |
| 123 |
total_checks = total_checks + 1; |
| 124 |
end |
| 125 |
|
| 126 |
if errors == 0 |
| 127 |
fprintf(' PASSED: All %d entries return consistent values for any permutation!\n\n', total_checks); |
| 128 |
else |
| 129 |
error(' FAILED: Found %d inconsistencies out of %d checks\n\n', errors, total_checks); |
| 130 |
end |
| 131 |
|
| 132 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 133 |
% TEST 3: Display non-zero unique elements % |
| 134 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 135 |
fprintf('Test 3: Summary of non-zero skewness values...\n'); |
| 136 |
|
| 137 |
for count = 1:size(M_.Skew_e, 1) |
| 138 |
i = M_.Skew_e(count, 1); |
| 139 |
j = M_.Skew_e(count, 2); |
| 140 |
k = M_.Skew_e(count, 3); |
| 141 |
fprintf(' [%d] Skew(%s, %s, %s) = %.6f\n', ... |
| 142 |
count, M_.exo_names{i}, M_.exo_names{j}, M_.exo_names{k}, M_.Skew_e(count, 4)); |
| 143 |
end |
| 144 |
|
| 145 |
if size(M_.Skew_e, 1) == 0 |
| 146 |
fprintf(' No non-zero elements found.\n'); |
| 147 |
end |
| 148 |
fprintf('\n'); |
| 149 |
|
| 150 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 151 |
% Test 4: Check diagonal elements (own-shock skewness) % |
| 152 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 153 |
fprintf('Test 4: Own-shock skewness (diagonal elements)...\n'); |
| 154 |
for i = 1:M_.exo_nbr |
| 155 |
val = skew_lookup(M_.Skew_e, i, i, i); |
| 156 |
if abs(val) > 0 |
| 157 |
fprintf(' Skew(%s, %s, %s) = %.6f\n', ... |
| 158 |
M_.exo_names{i}, M_.exo_names{i}, M_.exo_names{i}, val); |
| 159 |
end |
| 160 |
end |
| 161 |
fprintf('\n'); |
| 162 |
|
| 163 |
fprintf('=== Tests Completed ===\n\n'); |