Debian Codegraph @

tests/heterogeneity/analytical_rbc.mod

raw
1 heterogeneity_dimension households;
2
3 var(heterogeneity=households) k y c rk;
4
5 varexo(heterogeneity=households) z;
6
7 parameters alpha beta theta rho sig
8 ss_l ss_k ss_c ss_y ss_rk;
9
10 alpha = 1/3;
11 beta = 0.99;
12 rho = 0.95;
13 sig = 0.01;
14 ss_l = 1/3;
15 ss_k = ss_l*(beta*alpha)^(1/(1-alpha));
16 ss_y = ss_k^alpha*ss_l^(1-alpha);
17 ss_c = ss_y-ss_k;
18 ss_rk = alpha*ss_y/ss_k;
19 theta = (1-alpha)*(1-ss_l)*ss_y/(ss_l*ss_c);
20
21 model(heterogeneity=households);
22 1/c - beta*rk(+1)/c(+1) = 0;
23 y = z*k(-1)^alpha*ss_l^(1-alpha);
24 rk = alpha*y/k(-1);
25 y = c+k;
26 end;
27
28 M = 10;
29 N = 30;
30 max_iter = options_.heterogeneity.forward.max_iter;
31 tol = options_.heterogeneity.forward.tol;
32 [z_grid, p, Q] = rouwenhorst(rho, sig, M, tol, max_iter);
33 k_grid = linspace(0.8*ss_k, 1.2*ss_k, N);
34 c_th = zeros(M, N);
35 k_th = zeros(M, N);
36 for i_z = 1:M
37 Z = z_grid(i_z);
38 for i_k = 1:N
39 K = k_grid(i_k);
40 Y = Z * K^alpha * ss_l^(1-alpha);
41 c_th(i_z, i_k) = (1 - alpha*beta) * Y;
42 k_th(i_z, i_k) = alpha * beta * Y;
43 end
44 end
45
46 steady_state = struct;
47 steady_state.shocks = struct;
48 steady_state.shocks.grids.z = z_grid;
49 steady_state.shocks.Pi.z = Q;
50 steady_state.pol.grids.k = k_grid
51 steady_state.pol.values.k = ss_k*ones(M,N);
52 steady_state.pol.values.y = ss_y*ones(M,N);
53 steady_state.pol.values.c = ss_c*ones(M,N);
54 steady_state.pol.values.rk = ss_rk*ones(M,N);
55 steady_state.pol.values.AUX_HET_EXPECT_15 = beta*ss_rk/ss_c;
56 steady_state.pol.order = {'z', 'k'};
57
58 %% ======== TIME ITERATION CHECK ============
59 oo_het = struct;
60 oo_het = heterogeneity.compute_steady_state(M_, options_.heterogeneity, oo_het, steady_state);
61
62 c_ti = oo_het.steady_state.pol.values.c;
63 k_ti = oo_het.steady_state.pol.values.k;
64
65 % Compute errors
66 max_error_c = max(abs(c_th(:) - c_ti(:)) ./ c_th(:));
67 max_error_k = max(abs(k_th(:) - k_ti(:)) ./ k_th(:));
68 fprintf('Max error in c(K,Z): %e\n', max_error_c);
69 fprintf("Max error in k\'(K,Z): %e\n", max_error_k);
70
71 tol = 1e-4;
72 if max_error_c > tol || max_error_k > tol
73 error('Time iteration failed accuracy test: errors exceed tolerance %e', tol);
74 else
75 fprintf('Time iteration matches analytical solution within tolerance %e\n', tol);
76 end
77
78 %% ======== FORWARD ITERATION CHECK ============
79 %% Build sparse transition matrix T
80 % T(dest, src) = probability of going from src to dest
81 % State indexing: linear index = (i_k - 1)*M + i_z for (z_i, k_i)
82 % This matches Fortran column-major with d_mat(1:N_e, 1:N_a) where z varies fastest
83 n_states = M * N;
84 rows = [];
85 cols = [];
86 vals = [];
87
88 for i_z = 1:M
89 for i_k = 1:N
90 src_idx = (i_k - 1)*M + i_z;
91 kp = k_ti(i_z, i_k); % time-iterated policy (same as Fortran uses)
92
93 % Use find_bracket_linear_weight MEX for bracketing
94 % Returns: i_lo (1-based index), w_lo (weight on lower bracket)
95 [i_lo, w_lo] = find_bracket_linear_weight(k_grid, kp);
96
97 % Handle boundary: if kp >= k_grid(end), i_lo = N and w_lo = 1
98 % We need i_hi = i_lo + 1, but cap at N
99 if i_lo >= N
100 i_lo = N - 1;
101 w_lo = 0.0; % all weight on upper (index N)
102 end
103 i_hi = i_lo + 1;
104 w_hi = 1 - w_lo;
105
106 % Distribute mass to destination states for each z' realization
107 for j_z = 1:M
108 prob_z = Q(i_z, j_z); % shock transition prob P(z'|z)
109
110 % Lower bracket contribution
111 if w_lo > 0
112 dest_lo = (i_lo - 1)*M + j_z;
113 rows = [rows; dest_lo];
114 cols = [cols; src_idx];
115 vals = [vals; prob_z * w_lo];
116 end
117
118 % Upper bracket contribution
119 if w_hi > 0
120 dest_hi = (i_hi - 1)*M + j_z;
121 rows = [rows; dest_hi];
122 cols = [cols; src_idx];
123 vals = [vals; prob_z * w_hi];
124 end
125 end
126 end
127 end
128
129 T = sparse(rows, int32(cols), vals, n_states, n_states);
130
131 %% Forward iteration
132 tol = 1e-10;
133 max_iter = 10000;
134
135 % Initialize: uniform over k, stationary over z
136 % D_mat(i_z, i_k) with z varying fastest when flattened
137 D = zeros(M, N);
138 for i_z = 1:M
139 D(i_z, :) = p(i_z) / N;
140 end
141 D = D(:); % flatten to vector (column-major: z varies fastest, then k)
142
143 for iter = 1:max_iter
144 D_next = T * D;
145 diff_norm = max(abs(D_next - D));
146 if diff_norm < tol
147 fprintf('Converged in %d iterations (diff = %e)\n', iter, diff_norm);
148 break;
149 end
150 D = D_next;
151 end
152
153 if iter == max_iter
154 fprintf('Warning: did not converge (diff = %e)\n', diff_norm);
155 end
156
157 %% Reshape and display results
158 D_mat = reshape(D, M, N); % D_mat(i_z, i_k) = distribution mass
159
160 fprintf('\n=== Distribution Summary ===\n');
161 fprintf('Distribution sums to: %f\n', sum(D));
162 fprintf('Marginal over z: %s\n', mat2str(sum(D_mat, 2)', 4));
163 fprintf('Expected (stationary p): %s\n', mat2str(p', 4));
164 % Mean capital: sum over z of (D_mat(i_z, :) * k_grid')
165 mean_k = sum(D_mat * k_grid');
166 fprintf('Mean capital: %f (ss_k = %f)\n', mean_k, ss_k);
167
168 max_error_d = max(abs(oo_het.steady_state.d.hist(:)-D_mat(:)));
169 tol = 1e-9;
170 if (max_error_d > tol)
171 error('Forward iteration failed accuracy test: errors exceed tolerance %e', tol);
172 else
173 fprintf('Forward iteration matches the manual computation within tolerance %e\n', tol);
174 end