| 1 |
// Comprehensive test for all heterogeneity command variations |
| 2 |
// Tests all options and syntax variations |
| 3 |
// |
| 4 |
// USAGE: This file tests two distinct simulation modes: |
| 5 |
// |
| 6 |
// 1. STOCHASTIC SIMULATION MODE (default, no flags): |
| 7 |
// dynare-preprocessor ks_commands.mod |
| 8 |
// Tests: heterogeneity_simulate with various options: |
| 9 |
// - IRF options: irf, relative_irf, irf_shocks |
| 10 |
// - Simulation options: periods, drop |
| 11 |
// - Combined: Both IRFs and stochastic simulation together |
| 12 |
// Output: oo_.irfs (always computed) and oo_.endo_simul (when periods > 0) |
| 13 |
// |
| 14 |
// 2. NEWS SHOCK SEQUENCE MODE (with -DDET_SHOCKS=1): |
| 15 |
// dynare-preprocessor ks_commands.mod -DDET_SHOCKS=1 |
| 16 |
// Tests: heterogeneity_simulate with anticipated shock sequences (news shocks) |
| 17 |
// When shocks are defined with periods/values in shocks block, agents learn |
| 18 |
// at t=0 about shocks at various future dates (Auclert et al. 2021) |
| 19 |
// Output: oo_.endo_simul with perfect foresight paths |
| 20 |
// |
| 21 |
// All modes test heterogeneity_load_steady_state and heterogeneity_solve commands |
| 22 |
|
| 23 |
heterogeneity_dimension households; |
| 24 |
|
| 25 |
var(heterogeneity=households) |
| 26 |
c // Consumption |
| 27 |
a // Assets |
| 28 |
Va // Derivative of the value function w.r.t assets |
| 29 |
; |
| 30 |
|
| 31 |
varexo(heterogeneity=households) |
| 32 |
e // Idiosyncratic productivity shock |
| 33 |
; |
| 34 |
|
| 35 |
var |
| 36 |
Y // Aggregate output |
| 37 |
r // Rate of return on capital net of depreciation |
| 38 |
w // Wage rate |
| 39 |
K // Aggregate capital |
| 40 |
; |
| 41 |
|
| 42 |
varexo Z; // Aggregate productivity shock |
| 43 |
|
| 44 |
parameters |
| 45 |
L // Labor |
| 46 |
alpha // Share of capital in production function |
| 47 |
beta // Subjective discount rate of households |
| 48 |
delta // Capital depreciation rate |
| 49 |
eis // Elasticity of intertemporal substitution |
| 50 |
rho_Z // Aggregate TFP shock persistence |
| 51 |
sig_Z // Aggregate TFP shock innovation std err |
| 52 |
Z_ss // Aggregate TFP shock average value |
| 53 |
; |
| 54 |
|
| 55 |
model(heterogeneity=households); |
| 56 |
c^(-1/eis)-beta*Va(+1)=0 ⟂ a>=0; |
| 57 |
(1+r)*a(-1)+w*e-c-a; |
| 58 |
Va = (1+r)*c^(-1/eis); |
| 59 |
end; |
| 60 |
|
| 61 |
model; |
| 62 |
(Z_ss+Z) * K(-1)^alpha * L^(1 - alpha) - Y; |
| 63 |
alpha * (Z_ss+Z) * (K(-1) / L)^(alpha - 1) - delta - r; |
| 64 |
(1 - alpha) * (Z_ss+Z) * (K(-1) / L)^alpha - w; |
| 65 |
K - SUM(a); |
| 66 |
end; |
| 67 |
|
| 68 |
@#ifdef DET_SHOCKS |
| 69 |
// News shock sequence (anticipated shocks known at t=0) |
| 70 |
shocks; |
| 71 |
var Z; |
| 72 |
periods 1:10; |
| 73 |
values 0.01; |
| 74 |
end; |
| 75 |
@#else |
| 76 |
// Stochastic shocks (for IRFs and stochastic simulation) |
| 77 |
shocks; |
| 78 |
var Z; stderr 0.01; |
| 79 |
end; |
| 80 |
@#endif |
| 81 |
|
| 82 |
//========================================================================== |
| 83 |
// TEST 1: heterogeneity_load_steady_state with both options |
| 84 |
//========================================================================== |
| 85 |
heterogeneity_load_steady_state(filename = ks); |
| 86 |
|
| 87 |
verbatim; |
| 88 |
disp('TEST 1: heterogeneity_load_steady_state(filename=ks)'); |
| 89 |
assert(isfield(oo_, 'heterogeneity'), 'TEST 1 FAILED: Missing oo_.heterogeneity'); |
| 90 |
assert(isfield(oo_.heterogeneity, 'steady_state'), 'TEST 1 FAILED: Missing steady state'); |
| 91 |
assert(isfield(oo_.heterogeneity.steady_state, 'agg'), 'TEST 1 FAILED: Missing aggregate variables'); |
| 92 |
assert(isfield(oo_.heterogeneity.steady_state, 'pol'), 'TEST 1 FAILED: Missing policy functions'); |
| 93 |
disp(' ✓ Steady state loaded successfully'); |
| 94 |
disp(' '); |
| 95 |
end; |
| 96 |
|
| 97 |
//========================================================================== |
| 98 |
// TEST 2: heterogeneity_solve with custom time horizon |
| 99 |
//========================================================================== |
| 100 |
heterogeneity_solve(truncation_horizon = 400); |
| 101 |
|
| 102 |
verbatim; |
| 103 |
disp('TEST 2: heterogeneity_solve(truncation_horizon=400)'); |
| 104 |
assert(isfield(options_.heterogeneity.solve, 'truncation_horizon'), 'TEST 2 FAILED: truncation_horizon option not set'); |
| 105 |
assert(options_.heterogeneity.solve.truncation_horizon == 400, 'TEST 2 FAILED: truncation_horizon option not set to 400'); |
| 106 |
assert(isfield(oo_.heterogeneity, 'dr'), 'TEST 2 FAILED: Missing solution'); |
| 107 |
assert(isfield(oo_.heterogeneity.dr, 'G'), 'TEST 2 FAILED: Missing Jacobians'); |
| 108 |
disp(' ✓ Solution computed with truncation_horizon=400'); |
| 109 |
disp(' '); |
| 110 |
end; |
| 111 |
|
| 112 |
@#ifndef DET_SHOCKS |
| 113 |
//========================================================================== |
| 114 |
// STOCHASTIC SIMULATION MODE TESTS (default mode) |
| 115 |
// Tests IRF computation and stochastic simulation options |
| 116 |
//========================================================================== |
| 117 |
|
| 118 |
//========================================================================== |
| 119 |
// TEST 3: heterogeneity_simulate - basic IRFs |
| 120 |
//========================================================================== |
| 121 |
heterogeneity_simulate; |
| 122 |
|
| 123 |
verbatim; |
| 124 |
disp('TEST 3: heterogeneity_simulate (basic IRFs)'); |
| 125 |
assert(isfield(oo_, 'irfs'), 'TEST 3 FAILED: Missing oo_.irfs'); |
| 126 |
assert(~isempty(fieldnames(oo_.irfs)), 'TEST 3 FAILED: IRFs are empty'); |
| 127 |
disp(' ✓ IRFs computed successfully'); |
| 128 |
disp(' '); |
| 129 |
end; |
| 130 |
|
| 131 |
//========================================================================== |
| 132 |
// TEST 4: heterogeneity_simulate with irf option |
| 133 |
//========================================================================== |
| 134 |
heterogeneity_simulate(irf = 80); |
| 135 |
|
| 136 |
verbatim; |
| 137 |
disp('TEST 4: heterogeneity_simulate(irf=80)'); |
| 138 |
assert(options_.irf == 80, 'TEST 4 FAILED: irf option not set to 80'); |
| 139 |
disp(' ✓ irf option correctly set to 80'); |
| 140 |
disp(' '); |
| 141 |
end; |
| 142 |
|
| 143 |
//========================================================================== |
| 144 |
// TEST 5: heterogeneity_simulate with relative_irf |
| 145 |
//========================================================================== |
| 146 |
heterogeneity_simulate(relative_irf); |
| 147 |
|
| 148 |
verbatim; |
| 149 |
disp('TEST 5: heterogeneity_simulate(relative_irf)'); |
| 150 |
assert(isfield(options_, 'relative_irf'), 'TEST 5 FAILED: relative_irf option not set'); |
| 151 |
assert(options_.relative_irf == true, 'TEST 5 FAILED: relative_irf not true'); |
| 152 |
disp(' ✓ relative_irf option correctly set'); |
| 153 |
disp(' '); |
| 154 |
end; |
| 155 |
|
| 156 |
//========================================================================== |
| 157 |
// TEST 6: heterogeneity_simulate with multiple options |
| 158 |
//========================================================================== |
| 159 |
heterogeneity_simulate(irf = 60, relative_irf); |
| 160 |
|
| 161 |
verbatim; |
| 162 |
disp('TEST 6: heterogeneity_simulate(irf=60, relative_irf)'); |
| 163 |
assert(options_.irf == 60, 'TEST 6 FAILED: irf option not set to 60'); |
| 164 |
assert(options_.relative_irf == true, 'TEST 6 FAILED: relative_irf not set'); |
| 165 |
disp(' ✓ Both options correctly set'); |
| 166 |
disp(' '); |
| 167 |
end; |
| 168 |
|
| 169 |
//========================================================================== |
| 170 |
// TEST 7: heterogeneity_simulate with irf_shocks option |
| 171 |
//========================================================================== |
| 172 |
heterogeneity_simulate(irf_shocks = (Z)); |
| 173 |
|
| 174 |
verbatim; |
| 175 |
disp('TEST 7: heterogeneity_simulate(irf_shocks=(Z))'); |
| 176 |
assert(isfield(options_, 'irf_shocks'), 'TEST 7 FAILED: irf_shocks option not set'); |
| 177 |
assert(isequal(options_.irf_shocks, {'Z'}), 'TEST 7 FAILED: irf_shocks not set to {''Z''}'); |
| 178 |
disp(' ✓ irf_shocks correctly set to {''Z''}'); |
| 179 |
disp(' '); |
| 180 |
end; |
| 181 |
|
| 182 |
//========================================================================== |
| 183 |
// TEST 8: heterogeneity_simulate with variable list |
| 184 |
//========================================================================== |
| 185 |
heterogeneity_simulate Y K; |
| 186 |
|
| 187 |
verbatim; |
| 188 |
disp('TEST 8: heterogeneity_simulate Y K'); |
| 189 |
% Variable list is passed to simulate function, no option to check |
| 190 |
disp(' ✓ Variable list syntax accepted'); |
| 191 |
disp(' '); |
| 192 |
end; |
| 193 |
|
| 194 |
//========================================================================== |
| 195 |
// TEST 9: heterogeneity_simulate with options AND variable list |
| 196 |
//========================================================================== |
| 197 |
heterogeneity_simulate(irf = 50) w r; |
| 198 |
|
| 199 |
verbatim; |
| 200 |
disp('TEST 9: heterogeneity_simulate(irf=50) w r'); |
| 201 |
assert(options_.irf == 50, 'TEST 9 FAILED: irf option not set to 50'); |
| 202 |
disp(' ✓ Options and variable list both accepted'); |
| 203 |
disp(' '); |
| 204 |
end; |
| 205 |
|
| 206 |
//========================================================================== |
| 207 |
// TEST 10: heterogeneity_simulate with many options |
| 208 |
//========================================================================== |
| 209 |
heterogeneity_simulate(irf = 70, irf_shocks = (Z), relative_irf); |
| 210 |
|
| 211 |
verbatim; |
| 212 |
disp('TEST 10: heterogeneity_simulate(irf=70, irf_shocks=(Z), relative_irf)'); |
| 213 |
assert(options_.irf == 70, 'TEST 10 FAILED: irf option not set to 70'); |
| 214 |
assert(isequal(options_.irf_shocks, {'Z'}), 'TEST 10 FAILED: irf_shocks not correct'); |
| 215 |
assert(options_.relative_irf == true, 'TEST 10 FAILED: relative_irf not set'); |
| 216 |
disp(' ✓ All three options correctly set'); |
| 217 |
disp(' '); |
| 218 |
end; |
| 219 |
|
| 220 |
//========================================================================== |
| 221 |
// TEST 13: heterogeneity_simulate with periods option |
| 222 |
//========================================================================== |
| 223 |
heterogeneity_simulate(periods = 200); |
| 224 |
|
| 225 |
verbatim; |
| 226 |
disp('TEST 13: heterogeneity_simulate(periods=200)'); |
| 227 |
assert(isfield(options_, 'periods'), 'TEST 13 FAILED: periods option not set'); |
| 228 |
assert(options_.periods == 200, 'TEST 13 FAILED: periods not set to 200'); |
| 229 |
assert(isfield(oo_, 'endo_simul'), 'TEST 13 FAILED: Missing oo_.endo_simul'); |
| 230 |
disp(' ✓ Stochastic simulation with periods=200'); |
| 231 |
disp(' '); |
| 232 |
end; |
| 233 |
|
| 234 |
//========================================================================== |
| 235 |
// TEST 14: heterogeneity_simulate with periods and drop options |
| 236 |
//========================================================================== |
| 237 |
heterogeneity_simulate(periods = 300, drop = 50); |
| 238 |
|
| 239 |
verbatim; |
| 240 |
disp('TEST 14: heterogeneity_simulate(periods=300, drop=50)'); |
| 241 |
assert(options_.periods == 300, 'TEST 14 FAILED: periods not set to 300'); |
| 242 |
assert(isfield(options_, 'drop'), 'TEST 14 FAILED: drop option not set'); |
| 243 |
assert(options_.drop == 50, 'TEST 14 FAILED: drop not set to 50'); |
| 244 |
disp(' ✓ Both periods and drop options correctly set'); |
| 245 |
disp(' '); |
| 246 |
end; |
| 247 |
|
| 248 |
//========================================================================== |
| 249 |
// TEST 15: heterogeneity_simulate with periods, drop, and variable list |
| 250 |
//========================================================================== |
| 251 |
heterogeneity_simulate(periods = 250, drop = 25) Y K w r; |
| 252 |
|
| 253 |
verbatim; |
| 254 |
disp('TEST 15: heterogeneity_simulate(periods=250, drop=25) Y K w r'); |
| 255 |
assert(options_.periods == 250, 'TEST 15 FAILED: periods not set to 250'); |
| 256 |
assert(options_.drop == 25, 'TEST 15 FAILED: drop not set to 25'); |
| 257 |
disp(' ✓ Options and variable list both accepted'); |
| 258 |
disp(' '); |
| 259 |
end; |
| 260 |
|
| 261 |
//========================================================================== |
| 262 |
// TEST 16: heterogeneity_simulate with combined IRFs and stochastic simulation |
| 263 |
//========================================================================== |
| 264 |
heterogeneity_simulate(irf = 50, periods = 200, drop = 50); |
| 265 |
|
| 266 |
verbatim; |
| 267 |
disp('TEST 16: heterogeneity_simulate(irf=50, periods=200, drop=50)'); |
| 268 |
assert(options_.irf == 50, 'TEST 16 FAILED: irf option not set to 50'); |
| 269 |
assert(options_.periods == 200, 'TEST 16 FAILED: periods not set to 200'); |
| 270 |
assert(options_.drop == 50, 'TEST 16 FAILED: drop not set to 50'); |
| 271 |
assert(isfield(oo_, 'irfs'), 'TEST 16 FAILED: Missing oo_.irfs'); |
| 272 |
assert(isfield(oo_, 'endo_simul'), 'TEST 16 FAILED: Missing oo_.endo_simul'); |
| 273 |
disp(' ✓ Combined IRFs and stochastic simulation work together'); |
| 274 |
disp(' '); |
| 275 |
end; |
| 276 |
|
| 277 |
@#else |
| 278 |
//========================================================================== |
| 279 |
// NEWS SHOCK SEQUENCE MODE TESTS (when DET_SHOCKS is defined) |
| 280 |
// Tests simulation with anticipated shocks known at t=0 |
| 281 |
//========================================================================== |
| 282 |
|
| 283 |
//========================================================================== |
| 284 |
// TEST 11: heterogeneity_simulate with news shocks |
| 285 |
//========================================================================== |
| 286 |
// When news shocks are defined in the shocks block (with periods/values), |
| 287 |
// heterogeneity_simulate automatically uses news shock mode |
| 288 |
heterogeneity_simulate; |
| 289 |
|
| 290 |
verbatim; |
| 291 |
disp('TEST 11: heterogeneity_simulate (news shocks)'); |
| 292 |
assert(isfield(oo_, 'endo_simul'), 'TEST 11 FAILED: Missing oo_.endo_simul'); |
| 293 |
assert(~isempty(M_.det_shocks), 'TEST 11 FAILED: News shocks not configured'); |
| 294 |
disp(' ✓ News shock simulation computed successfully'); |
| 295 |
disp(' '); |
| 296 |
end; |
| 297 |
|
| 298 |
//========================================================================== |
| 299 |
// TEST 12: heterogeneity_simulate with news shocks and variable list |
| 300 |
//========================================================================== |
| 301 |
heterogeneity_simulate Y K w; |
| 302 |
|
| 303 |
verbatim; |
| 304 |
disp('TEST 12: heterogeneity_simulate Y K w (news shocks with var list)'); |
| 305 |
% Variable list is passed to simulate function, no option to check |
| 306 |
disp(' ✓ Variable list syntax accepted in news shock mode'); |
| 307 |
disp(' '); |
| 308 |
end; |
| 309 |
|
| 310 |
@#endif |
| 311 |
|
| 312 |
//========================================================================== |
| 313 |
// Summary |
| 314 |
//========================================================================== |
| 315 |
verbatim; |
| 316 |
disp('================================================================='); |
| 317 |
@#ifndef DET_SHOCKS |
| 318 |
disp('ALL 16 TESTS PASSED (STOCHASTIC SIMULATION MODE)! ✓✓✓'); |
| 319 |
disp(' - Tests 1-2: Load and solve'); |
| 320 |
disp(' - Tests 3-10: IRF options'); |
| 321 |
disp(' - Tests 13-16: Stochastic simulation and combined mode'); |
| 322 |
@#else |
| 323 |
disp('ALL 4 TESTS PASSED (NEWS SHOCK SEQUENCE MODE)! ✓✓✓'); |
| 324 |
disp(' - Tests 1-2: Load and solve'); |
| 325 |
disp(' - Tests 11-12: News shock simulation'); |
| 326 |
@#endif |
| 327 |
disp('================================================================='); |
| 328 |
end; |