| 1 |
heterogeneity_dimension households; |
| 2 |
|
| 3 |
var(heterogeneity=households) |
| 4 |
c // Consumption |
| 5 |
a // Assets |
| 6 |
Va // Derivative of the value function w.r.t assets |
| 7 |
; |
| 8 |
|
| 9 |
varexo(heterogeneity=households) |
| 10 |
e |
| 11 |
; |
| 12 |
|
| 13 |
var |
| 14 |
Y // Aggregate output |
| 15 |
r // Rate of return on capital net of depreciation |
| 16 |
w // Wage rate |
| 17 |
K // Aggregate capital |
| 18 |
; |
| 19 |
|
| 20 |
varexo Z; // Aggregate productivity shock |
| 21 |
|
| 22 |
parameters |
| 23 |
L // Labor |
| 24 |
alpha // Share of capital in production fuction |
| 25 |
beta // Subjective discount rate of houselholds |
| 26 |
delta // Capital depreciation rate |
| 27 |
eis // Elasticity of intertemporal substitution |
| 28 |
rho_Z // Aggregate TFP shock persistence |
| 29 |
sig_Z // Aggregate TFP shock innovation std err |
| 30 |
Z_ss // Aggregate TFP shock average value |
| 31 |
; |
| 32 |
|
| 33 |
model(heterogeneity=households); |
| 34 |
c^(-1/eis)-beta*Va(+1)=0 ⟂ a>=0; |
| 35 |
(1+r)*a(-1)+w*e-c-a; |
| 36 |
Va = (1+r)*c^(-1/eis); |
| 37 |
end; |
| 38 |
|
| 39 |
model; |
| 40 |
(Z_ss+Z) * K(-1)^alpha * L^(1 - alpha) - Y; |
| 41 |
alpha * (Z_ss+Z) * (K(-1) / L)^(alpha - 1) - delta - r; |
| 42 |
(1 - alpha) * (Z_ss+Z) * (K(-1) / L)^alpha - w; |
| 43 |
K - SUM(a); |
| 44 |
end; |
| 45 |
|
| 46 |
shocks; |
| 47 |
var Z; stderr 0.01; |
| 48 |
end; |
| 49 |
|
| 50 |
load 'ks.mat'; |
| 51 |
|
| 52 |
param_names = fieldnames(steady_state.params); |
| 53 |
for i=1:numel(param_names) |
| 54 |
param = param_names{i}; |
| 55 |
set_param_value(param, steady_state.params.(param)); |
| 56 |
end |
| 57 |
|
| 58 |
verbatim; |
| 59 |
skipline() |
| 60 |
disp('*** TESTING: heterogeneity.check_steady_state_input.m ***'); |
| 61 |
options_.heterogeneity.check.no_warning_redundant = false; |
| 62 |
options_.heterogeneity.check.no_warning_d_grids = false; |
| 63 |
testFailed = 0; |
| 64 |
testResults = []; % Array to collect all test results |
| 65 |
|
| 66 |
% Test heterogeneity.check_steady_state_input function |
| 67 |
[testFailed, testResults] = test_check_steady_state(M_, options_, steady_state, testFailed, testResults); |
| 68 |
end; |
| 69 |
|
| 70 |
%% === FIELD VALIDATION TESTS === |
| 71 |
verbatim; |
| 72 |
% Field validation tests |
| 73 |
verbose = true; |
| 74 |
[testFailed, testResults] = test_field_validation(M_, options_, steady_state, testFailed, testResults); |
| 75 |
end; |
| 76 |
|
| 77 |
%% === FILE LOADING TESTS === |
| 78 |
% Test file loading with various path formats, custom variables, and error handling |
| 79 |
|
| 80 |
% Test 1: Load with explicit .mat extension + validate residuals |
| 81 |
heterogeneity_load_steady_state(filename = 'ks.mat'); |
| 82 |
assert(isfield(oo_, 'heterogeneity'), 'oo_.heterogeneity field missing'); |
| 83 |
assert(isfield(oo_.heterogeneity, 'steady_state'), 'oo_.heterogeneity.ss field missing'); |
| 84 |
assert(isfield(oo_.heterogeneity, 'sizes'), 'oo_.heterogeneity.sizes field missing'); |
| 85 |
assert(isfield(oo_.heterogeneity, 'mat'), 'oo_.heterogeneity.mat field missing'); |
| 86 |
assert(isfield(oo_.heterogeneity, 'indices'), 'oo_.heterogeneity.indices field missing'); |
| 87 |
verbatim; |
| 88 |
% Validate steady-state residuals |
| 89 |
agg_r = max(abs(oo_.heterogeneity.mat.G)); |
| 90 |
assert(agg_r <= 1e-9, sprintf('Aggregate residuals too large: %.2e', agg_r)); |
| 91 |
|
| 92 |
% Check heterogeneous residuals with Euler equation scaling |
| 93 |
het_r = oo_.heterogeneity.mat.F; |
| 94 |
euler_idx = 1; % First equation is Euler equation in ks.mod |
| 95 |
scaling = reshape(steady_state.pol.values.c .^ (-1/eis), 1, []); |
| 96 |
|
| 97 |
if isoctave() |
| 98 |
het_r_euler = max(max(abs(het_r(euler_idx,:) ./ scaling))); |
| 99 |
het_r_other = max(max(abs(het_r(setdiff(1:size(het_r,1), euler_idx),:)))); |
| 100 |
else |
| 101 |
het_r_euler = max(abs(het_r(euler_idx,:) ./ scaling), [], "all"); |
| 102 |
het_r_other = max(abs(het_r(setdiff(1:size(het_r,1), euler_idx),:)), [], "all"); |
| 103 |
end |
| 104 |
|
| 105 |
assert(het_r_euler <= 1e-2, sprintf('Euler residuals (scaled) too large: %.2e', het_r_euler)); |
| 106 |
assert(het_r_other <= 5e-4, sprintf('Other heterogeneous residuals too large: %.2e', het_r_other)); |
| 107 |
|
| 108 |
fprintf('✔ Test 1: Explicit .mat extension + residuals validated\n'); |
| 109 |
testResults = [testResults; struct('name', 'Load: explicit .mat + residuals', 'passed', true, 'message', '')]; |
| 110 |
end; |
| 111 |
|
| 112 |
% Test 2: Load without extension (auto-detect) |
| 113 |
heterogeneity_load_steady_state(filename = ks); |
| 114 |
assert(isfield(oo_, 'heterogeneity'), 'oo_.heterogeneity field missing'); |
| 115 |
assert(isfield(oo_.heterogeneity, 'steady_state'), 'oo_.heterogeneity.ss field missing'); |
| 116 |
assert(isfield(oo_.heterogeneity, 'sizes'), 'oo_.heterogeneity.sizes field missing'); |
| 117 |
assert(isfield(oo_.heterogeneity, 'mat'), 'oo_.heterogeneity.mat field missing'); |
| 118 |
assert(isfield(oo_.heterogeneity, 'indices'), 'oo_.heterogeneity.indices field missing') |
| 119 |
verbatim; |
| 120 |
fprintf('✔ Test 2: Auto-detect .mat extension works\n'); |
| 121 |
testResults = [testResults; struct('name', 'Path: auto-detect extension', 'passed', true, 'message', '')]; |
| 122 |
end; |
| 123 |
|
| 124 |
% Test 3: Load with relative path to current directory |
| 125 |
heterogeneity_load_steady_state(filename = './ks.mat'); |
| 126 |
assert(isfield(oo_, 'heterogeneity'), 'oo_.heterogeneity field missing'); |
| 127 |
assert(isfield(oo_.heterogeneity, 'steady_state'), 'oo_.heterogeneity.ss field missing'); |
| 128 |
assert(isfield(oo_.heterogeneity, 'sizes'), 'oo_.heterogeneity.sizes field missing'); |
| 129 |
assert(isfield(oo_.heterogeneity, 'mat'), 'oo_.heterogeneity.mat field missing'); |
| 130 |
assert(isfield(oo_.heterogeneity, 'indices'), 'oo_.heterogeneity.indices field missing') |
| 131 |
verbatim; |
| 132 |
fprintf('✔ Test 3: Relative path (./) works\n'); |
| 133 |
testResults = [testResults; struct('name', 'Path: relative path ./', 'passed', true, 'message', '')]; |
| 134 |
end; |
| 135 |
|
| 136 |
% Test 4: Load with custom variable name |
| 137 |
verbatim; |
| 138 |
% Create temp file with custom variable name |
| 139 |
temp_file = 'ks_test_temp.mat'; |
| 140 |
steady_state_temp = steady_state; |
| 141 |
save(temp_file, 'steady_state_temp', '-v7'); |
| 142 |
|
| 143 |
% Set options for next command |
| 144 |
options_.heterogeneity.steady_state_file_name = temp_file; |
| 145 |
options_.heterogeneity.steady_state_variable_name = 'steady_state_temp'; |
| 146 |
end; |
| 147 |
|
| 148 |
heterogeneity_load_steady_state; |
| 149 |
|
| 150 |
verbatim; |
| 151 |
assert(isfield(oo_.heterogeneity, 'steady_state'), 'Failed to load custom variable'); |
| 152 |
delete(temp_file); % Clean up |
| 153 |
|
| 154 |
fprintf('✔ Test 4: Custom variable name works\n'); |
| 155 |
testResults = [testResults; struct('name', 'Load: custom variable name', 'passed', true, 'message', '')]; |
| 156 |
|
| 157 |
% Reset to defaults |
| 158 |
options_.heterogeneity.steady_state_file_name = ''; |
| 159 |
options_.heterogeneity.steady_state_variable_name = 'steady_state'; |
| 160 |
end; |
| 161 |
|
| 162 |
% Test 5: Error on missing variable |
| 163 |
verbatim; |
| 164 |
error_caught = false; |
| 165 |
try |
| 166 |
options_.heterogeneity.steady_state_file_name = 'ks.mat'; |
| 167 |
options_.heterogeneity.steady_state_variable_name = 'nonexistent_field'; |
| 168 |
oo_test = heterogeneity.load_steady_state(M_, options_.heterogeneity, oo_.heterogeneity); |
| 169 |
catch ME |
| 170 |
error_caught = true; |
| 171 |
assert(contains(ME.message, 'Variable ''nonexistent_field'' not found'), ... |
| 172 |
sprintf('Wrong error message: %s', ME.message)); |
| 173 |
end |
| 174 |
assert(error_caught, 'Expected error for missing variable not raised'); |
| 175 |
|
| 176 |
fprintf('✔ Test 5: Error on missing variable works\n'); |
| 177 |
testResults = [testResults; struct('name', 'Error: missing variable', 'passed', true, 'message', '')]; |
| 178 |
|
| 179 |
% Reset |
| 180 |
options_.heterogeneity.steady_state_file_name = ''; |
| 181 |
options_.heterogeneity.steady_state_variable_name = 'steady_state'; |
| 182 |
end; |
| 183 |
|
| 184 |
% Test 6: Error on missing file |
| 185 |
verbatim; |
| 186 |
error_caught = false; |
| 187 |
try |
| 188 |
options_.heterogeneity.steady_state_file_name = 'nonexistent_file.mat'; |
| 189 |
oo_test = heterogeneity.load_steady_state(M_, options_.heterogeneity, oo_.heterogeneity); |
| 190 |
catch ME |
| 191 |
error_caught = true; |
| 192 |
assert(contains(ME.message, 'nonexistent_file.mat') || contains(lower(ME.message), 'not found'), ... |
| 193 |
sprintf('Wrong error message: %s', ME.message)); |
| 194 |
end |
| 195 |
assert(error_caught, 'Expected error for missing file not raised'); |
| 196 |
|
| 197 |
fprintf('✔ Test 6: Error on missing file works\n'); |
| 198 |
testResults = [testResults; struct('name', 'Error: missing file', 'passed', true, 'message', '')]; |
| 199 |
|
| 200 |
% Reset |
| 201 |
options_.heterogeneity.steady_state_file_name = ''; |
| 202 |
end; |
| 203 |
|
| 204 |
% Test 7: Error when filename is empty and workspace variable does not exist |
| 205 |
verbatim; |
| 206 |
error_caught = false; |
| 207 |
try |
| 208 |
options_.heterogeneity.steady_state_file_name = ''; |
| 209 |
options_.heterogeneity.steady_state_variable_name = 'nonexistent_variable'; |
| 210 |
oo_test = heterogeneity.load_steady_state(M_, options_.heterogeneity, oo_.heterogeneity); |
| 211 |
catch ME |
| 212 |
error_caught = true; |
| 213 |
assert(contains(ME.message, 'not found in workspace'), ... |
| 214 |
sprintf('Wrong error message: %s', ME.message)); |
| 215 |
end |
| 216 |
assert(error_caught, 'Expected error for missing workspace variable not raised'); |
| 217 |
|
| 218 |
% Reset |
| 219 |
options_.heterogeneity.steady_state_variable_name = 'steady_state'; |
| 220 |
|
| 221 |
fprintf('✔ Test 7: Error on missing workspace variable works\n'); |
| 222 |
testResults = [testResults; struct('name', 'Error: missing workspace variable', 'passed', true, 'message', '')]; |
| 223 |
|
| 224 |
fprintf('\n'); |
| 225 |
end; |
| 226 |
|
| 227 |
% Initialize steady state once for all tests |
| 228 |
heterogeneity_load_steady_state(filename = ks); |
| 229 |
|
| 230 |
% Solve once for all tests |
| 231 |
heterogeneity_solve; |
| 232 |
|
| 233 |
verbatim; |
| 234 |
% Test the solve routine and SSJ comparisons (validation only) |
| 235 |
[testFailed, result] = run_test('heterogeneity.solve functionality and SSJ validation', @() test_solve_functionality(M_, oo_, G_eq, true), testFailed); |
| 236 |
testResults = [testResults; result]; |
| 237 |
|
| 238 |
% Test heterogeneity.simulate function (reuse initialized and solved model) |
| 239 |
[testFailed, testResults] = test_simulate_stochastic(M_, options_, oo_, steady_state, testFailed, testResults); |
| 240 |
|
| 241 |
% Test permutation handling |
| 242 |
[testFailed, testResults] = test_permutation(M_, options_, oo_, steady_state, testFailed, testResults); |
| 243 |
|
| 244 |
% Print test summary |
| 245 |
print_test_summary(testResults); |
| 246 |
|
| 247 |
if testFailed > 0 |
| 248 |
error('Some unit tests failed!'); |
| 249 |
end |
| 250 |
end; |