Debian Codegraph @

tests/heterogeneity/analytical_rbc_2capital.mod

raw
1 // Two-sector analytical RBC model with 2D endogenous state space
2 // This model validates time iteration and forward iteration algorithms
3 // for heterogeneous-agent models with multiple endogenous state variables.
4 //
5 // Key features:
6 // - Two independent sectors with separate consumption goods
7 // - Full depreciation (delta = 1) allows closed-form policy functions
8 // - 2D endogenous state space: (k1, k2)
9 // - 1D exogenous state space: productivity shock z
10
11 heterogeneity_dimension households;
12
13 var(heterogeneity=households) k1 k2 y1 y2 c1 c2 rk1 rk2;
14
15 varexo(heterogeneity=households) z;
16
17 parameters alpha1 alpha2 beta l1 l2 rho sig
18 ss_k1 ss_k2 ss_y1 ss_y2 ss_c1 ss_c2 ss_rk1 ss_rk2;
19
20 // Calibration
21 alpha1 = 0.33; // Capital share sector 1
22 alpha2 = 0.30; // Capital share sector 2
23 beta = 0.96; // Discount factor
24 l1 = 0.33; // Labor supply sector 1 (fixed)
25 l2 = 0.33; // Labor supply sector 2 (fixed)
26 rho = 0.95; // TFP persistence
27 sig = 0.01; // TFP shock std dev
28
29 // Analytical steady-state values (at z=1)
30 ss_k1 = (beta * alpha1)^(1/(1-alpha1)) * l1;
31 ss_k2 = (beta * alpha2)^(1/(1-alpha2)) * l2;
32 ss_y1 = ss_k1^alpha1 * l1^(1-alpha1);
33 ss_y2 = ss_k2^alpha2 * l2^(1-alpha2);
34 ss_c1 = (1 - beta * alpha1) * ss_y1;
35 ss_c2 = (1 - beta * alpha2) * ss_y2;
36 ss_rk1 = alpha1 * ss_y1 / ss_k1;
37 ss_rk2 = alpha2 * ss_y2 / ss_k2;
38
39 model(heterogeneity=households);
40 // Sector 1
41 y1 = z*k1(-1)^alpha1*l1^(1-alpha1);
42 rk1 = alpha1*y1/k1(-1);
43 y1 = c1 + k1;
44 1/c1 - beta*rk1(+1)/c1(+1) = 0;
45
46 // Sector 2
47 y2 = z*k2(-1)^alpha2*l2^(1-alpha2);
48 rk2 = alpha2*y2/k2(-1);
49 y2 = c2 + k2;
50 1/c2 - beta*rk2(+1)/c2(+1) = 0;
51 end;
52
53 %% ======== SHOCK DISCRETIZATION ============
54 M = 5; // Number of TFP states
55 max_iter = options_.heterogeneity.forward.max_iter;
56 tol = options_.heterogeneity.forward.tol;
57 [z_grid, p, Q] = rouwenhorst(rho, sig, M, tol, max_iter);
58
59 %% ======== CAPITAL GRIDS ============
60 N1 = 20; // Grid points for k1
61 N2 = 20; // Grid points for k2
62 k1_grid = linspace(0.8*ss_k1, 1.2*ss_k1, N1);
63 k2_grid = linspace(0.8*ss_k2, 1.2*ss_k2, N2);
64
65 %% ======== ANALYTICAL SOLUTION ============
66 % Compute closed-form policy functions on 3D state space
67 y1_th = zeros(M, N1, N2);
68 y2_th = zeros(M, N1, N2);
69 c1_th = zeros(M, N1, N2);
70 c2_th = zeros(M, N1, N2);
71 k1_th = zeros(M, N1, N2);
72 k2_th = zeros(M, N1, N2);
73 rk1_th = zeros(M, N1, N2);
74 rk2_th = zeros(M, N1, N2);
75
76 for i_z = 1:M
77 Z = z_grid(i_z);
78 for i_k1 = 1:N1
79 K1_lag = k1_grid(i_k1); % k1(-1) is the state variable
80 for i_k2 = 1:N2
81 K2_lag = k2_grid(i_k2); % k2(-1) is the state variable
82
83 % Sector 1 (independent of sector 2)
84 Y1 = Z * K1_lag^alpha1 * l1^(1-alpha1);
85 k1_th(i_z, i_k1, i_k2) = beta * alpha1 * Y1;
86 c1_th(i_z, i_k1, i_k2) = (1 - beta * alpha1) * Y1;
87 y1_th(i_z, i_k1, i_k2) = Y1;
88 rk1_th(i_z, i_k1, i_k2) = alpha1 * Y1 / K1_lag;
89
90 % Sector 2 (independent of sector 1)
91 Y2 = Z * K2_lag^alpha2 * l2^(1-alpha2);
92 k2_th(i_z, i_k1, i_k2) = beta * alpha2 * Y2;
93 c2_th(i_z, i_k1, i_k2) = (1 - beta * alpha2) * Y2;
94 y2_th(i_z, i_k1, i_k2) = Y2;
95 rk2_th(i_z, i_k1, i_k2) = alpha2 * Y2 / K2_lag;
96 end
97 end
98 end
99
100 %% ======== STEADY-STATE STRUCTURE ============
101 steady_state = struct;
102 steady_state.shocks = struct;
103 steady_state.shocks.grids.z = z_grid;
104 steady_state.shocks.Pi.z = Q;
105
106 steady_state.pol = struct;
107 steady_state.pol.grids.k1 = k1_grid;
108 steady_state.pol.grids.k2 = k2_grid;
109 steady_state.pol.values.k1 = ss_k1 * ones(M, N1, N2);
110 steady_state.pol.values.k2 = ss_k2 * ones(M, N1, N2);
111 steady_state.pol.values.y1 = ss_y1 * ones(M, N1, N2);
112 steady_state.pol.values.y2 = ss_y2 * ones(M, N1, N2);
113 steady_state.pol.values.c1 = ss_c1 * ones(M, N1, N2);
114 steady_state.pol.values.c2 = ss_c2 * ones(M, N1, N2);
115 steady_state.pol.values.rk1 = ss_rk1 * ones(M, N1, N2);
116 steady_state.pol.values.rk2 = ss_rk2 * ones(M, N1, N2);
117 steady_state.pol.values.AUX_HET_EXPECT_15 = beta*ss_rk1/ss_c1; % Auxiliary for sector 1 Euler
118 steady_state.pol.values.AUX_HET_EXPECT_19 = beta*ss_rk2/ss_c2; % Auxiliary for sector 2 Euler
119 steady_state.pol.order = {'z', 'k1', 'k2'};
120
121 %% ======== TIME ITERATION CHECK ============
122 fprintf('\n=== TIME ITERATION VALIDATION ===\n');
123 oo_het = struct;
124 oo_het = heterogeneity.compute_steady_state(M_, options_.heterogeneity, oo_het, steady_state);
125
126 % Extract time-iterated policies
127 k1_ti = oo_het.steady_state.pol.values.k1;
128 k2_ti = oo_het.steady_state.pol.values.k2;
129 c1_ti = oo_het.steady_state.pol.values.c1;
130 c2_ti = oo_het.steady_state.pol.values.c2;
131 y1_ti = oo_het.steady_state.pol.values.y1;
132 y2_ti = oo_het.steady_state.pol.values.y2;
133 rk1_ti = oo_het.steady_state.pol.values.rk1;
134 rk2_ti = oo_het.steady_state.pol.values.rk2;
135
136 % Compute relative errors
137 max_error_k1 = max(abs(k1_th(:) - k1_ti(:)) ./ k1_th(:));
138 max_error_k2 = max(abs(k2_th(:) - k2_ti(:)) ./ k2_th(:));
139 max_error_c1 = max(abs(c1_th(:) - c1_ti(:)) ./ c1_th(:));
140 max_error_c2 = max(abs(c2_th(:) - c2_ti(:)) ./ c2_th(:));
141
142 fprintf('Max error in k1''(Z,K1,K2): %e\n', max_error_k1);
143 fprintf('Max error in k2''(Z,K1,K2): %e\n', max_error_k2);
144 fprintf('Max error in c1(Z,K1,K2): %e\n', max_error_c1);
145 fprintf('Max error in c2(Z,K1,K2): %e\n', max_error_c2);
146
147 % This is expected for coarse grids and validates the algorithm correctly
148 tol = 1e-3;
149 if max_error_k1 > tol || max_error_k2 > tol || max_error_c1 > tol || max_error_c2 > tol
150 error('Time iteration failed accuracy test: errors exceed tolerance %e', tol);
151 else
152 fprintf('✓ Time iteration matches analytical solution within tolerance %e\n', tol);
153 end
154
155 %% ======== FORWARD ITERATION CHECK ============
156 fprintf('\n=== FORWARD ITERATION VALIDATION ===\n');
157
158 % Build sparse transition matrix T
159 % T(dest, src) = probability of going from src to dest
160 % State indexing: linear index = ((i_k2 - 1)*N1 + (i_k1 - 1))*M + i_z
161 n_states = M * N1 * N2;
162 rows = [];
163 cols = [];
164 vals = [];
165
166 for i_z = 1:M
167 for i_k1 = 1:N1
168 for i_k2 = 1:N2
169 src_idx = ((i_k2 - 1)*N1 + (i_k1 - 1))*M + i_z;
170
171 % Next-period capitals from time-iterated policy
172 k1p = k1_ti(i_z, i_k1, i_k2);
173 k2p = k2_ti(i_z, i_k1, i_k2);
174
175 % Bilinear interpolation: find brackets and weights
176 [i1_lo, w1_lo] = find_bracket_linear_weight(k1_grid, k1p);
177 [i2_lo, w2_lo] = find_bracket_linear_weight(k2_grid, k2p);
178
179 i1_hi = i1_lo + 1;
180 i2_hi = i2_lo + 1;
181 w1_hi = 1 - w1_lo;
182 w2_hi = 1 - w2_lo;
183
184 % Distribute mass over shock transitions and 4 spatial corners
185 for j_z = 1:M
186 prob_z = Q(i_z, j_z);
187
188 % Corner 1: (i1_lo, i2_lo)
189 dest_idx = ((i2_lo - 1)*N1 + (i1_lo - 1))*M + j_z;
190 rows = [rows; dest_idx];
191 cols = [cols; src_idx];
192 vals = [vals; prob_z * w1_lo * w2_lo];
193
194 % Corner 2: (i1_hi, i2_lo)
195 dest_idx = ((i2_lo - 1)*N1 + (i1_hi - 1))*M + j_z;
196 rows = [rows; dest_idx];
197 cols = [cols; src_idx];
198 vals = [vals; prob_z * w1_hi * w2_lo];
199
200 % Corner 3: (i1_lo, i2_hi)
201 dest_idx = ((i2_hi - 1)*N1 + (i1_lo - 1))*M + j_z;
202 rows = [rows; dest_idx];
203 cols = [cols; src_idx];
204 vals = [vals; prob_z * w1_lo * w2_hi];
205
206 % Corner 4: (i1_hi, i2_hi)
207 dest_idx = ((i2_hi - 1)*N1 + (i1_hi - 1))*M + j_z;
208 rows = [rows; dest_idx];
209 cols = [cols; src_idx];
210 vals = [vals; prob_z * w1_hi * w2_hi];
211 end
212 end
213 end
214 end
215
216 T = sparse(rows, int32(cols), vals, n_states, n_states);
217
218 % Forward iteration
219 tol = 1e-10;
220 max_iter = 10000;
221
222 % Initialize: uniform over (k1,k2), stationary over z
223 D = zeros(M, N1, N2);
224 for i_z = 1:M
225 D(i_z, :, :) = p(i_z) / (N1 * N2);
226 end
227 D = D(:); % Flatten to vector (z varies fastest, then k1, then k2)
228
229 for iter = 1:max_iter
230 D_next = T * D;
231 diff_norm = max(abs(D_next - D));
232 if diff_norm < tol
233 fprintf('Converged in %d iterations (diff = %e)\n', iter, diff_norm);
234 break;
235 end
236 D = D_next;
237 end
238
239 if iter == max_iter
240 fprintf('Warning: did not converge (diff = %e)\n', diff_norm);
241 end
242
243 % Reshape and display results
244 D_mat = reshape(D, M, N1, N2); % D_mat(i_z, i_k1, i_k2) = distribution mass
245
246 fprintf('\n=== Distribution Summary ===\n');
247 fprintf('Distribution sums to: %f\n', sum(D));
248 fprintf('Marginal over z: %s\n', mat2str(squeeze(sum(sum(D_mat, 2), 3))', 4));
249 fprintf('Expected (stationary p): %s\n', mat2str(p', 4));
250
251 % Mean capitals
252 mean_k1 = 0;
253 mean_k2 = 0;
254 for i_z = 1:M
255 for i_k1 = 1:N1
256 for i_k2 = 1:N2
257 mean_k1 = mean_k1 + D_mat(i_z, i_k1, i_k2) * k1_grid(i_k1);
258 mean_k2 = mean_k2 + D_mat(i_z, i_k1, i_k2) * k2_grid(i_k2);
259 end
260 end
261 end
262 fprintf('Mean k1: %f (ss_k1 = %f)\n', mean_k1, ss_k1);
263 fprintf('Mean k2: %f (ss_k2 = %f)\n', mean_k2, ss_k2);
264
265 % Compare to MEX implementation
266 max_error_d = max(abs(oo_het.steady_state.d.hist(:) - D_mat(:)));
267 tol = 1e-9;
268 if max_error_d > tol
269 error('Forward iteration failed accuracy test: errors exceed tolerance %e', tol);
270 else
271 fprintf('✓ Forward iteration matches MEX computation within tolerance %e\n', tol);
272 end
273
274 fprintf('\n=== ALL VALIDATION CHECKS PASSED ===\n');