time | Calls | line |
---|
| | 7 | function [result_map] = ff_iwkz_ds_vec(varargin)
|
| | 8 | %% FF_IWKZ_DS_VEC finds the stationary asset distributions
|
| | 9 | % Building on the Two Assets Two-Step Interpolated Dynamic Programming
|
| | 10 | % Problem
|
| | 11 | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html
|
| | 12 | % ff_iwkz_vf_vecsv>, here we solve for the asset distribution. This version
|
| | 13 | % of the program is vectorized
|
| | 14 | %
|
| | 15 | % This is the two-stage with interpolation version of
|
| | 16 | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_ds_vec.html
|
| | 17 | % ff_akz_ds_vec>. See that file for additional descriptions and
|
| | 18 | % comparisons. These two functions are nearly identical
|
| | 19 | %
|
| | 20 | % The code here works when we are looking for the distribution of f(a,z),
|
| | 21 | % where a'(a,z,z'), meaning that the a next period is determined by a last
|
| | 22 | % period and some shock last period as well as shock this period. a here is
|
| | 23 | % cash-on-hand. This contrasts with
|
| | 24 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html
|
| | 25 | % ff_az_ds>, which works for a'(a,z), a' can not be a function of z'.
|
| | 26 | %
|
| | 27 | % @example
|
| | 28 | %
|
| | 29 | % % Get Default Parameters
|
| | 30 | % it_param_set = 6;
|
| | 31 | % [param_map, support_map] = ffs_az_set_default_param(it_param_set);
|
| | 32 | % % Change Keys in param_map
|
| | 33 | % param_map('it_w_n') = 750;
|
| | 34 | % param_map('it_ak_n') = param_map('it_w_n');
|
| | 35 | % param_map('it_z_n') = 11;
|
| | 36 | % param_map('fl_a_max') = 100;
|
| | 37 | % param_map('fl_w') = 1.3;
|
| | 38 | % % Change Keys support_map
|
| | 39 | % support_map('bl_display') = false;
|
| | 40 | % support_map('bl_post') = true;
|
| | 41 | % support_map('bl_display_final') = false;
|
| | 42 | % % Call Program with external parameters that override defaults
|
| | 43 | % ff_iwkz_ds_vec(param_map, support_map);
|
| | 44 | %
|
| | 45 | % @include
|
| | 46 | %
|
| | 47 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html ff_wkz_vf_vecsv>
|
| | 48 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html ff_az_ds_post_stats>
|
| | 49 | % * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html fft_disc_rand_var_stats>
|
| | 50 | % * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html fft_disc_rand_var_mass2outcomes>
|
| | 51 | %
|
| | 52 | % @seealso
|
| | 53 | %
|
| | 54 | % * derive distribution f(y'(y,z)) one asset *loop*: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html ff_az_ds>
|
| | 55 | % * derive distribution f(y'({x,y},z)) two assets *loop*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_ds.html ff_akz_ds>
|
| | 56 | % * derive distribution f(y'({x,y},z, *z'*)) two assets *loop*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_ds.html ff_iwkz_ds>
|
| | 57 | % * derive distribution f(y'({y},z)) or f(y'({x,y},z)) *vectorized*: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds_vec.html ff_az_ds_vec>
|
| | 58 | % * derive distribution f(y'({y},z, *z'*)) or f(y'({x,y},z, *z'*)) *vectorized*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_ds_vec.html ff_iwkz_ds_vec>
|
| | 59 | % * derive distribution f(y'({y},z)) or f(y'({x,y},z)) *semi-analytical*: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds_vecsv.html ff_az_ds_vecsv>
|
| | 60 | % * derive distribution f(y'({y},z, *z'*)) or f(y'({x,y},z, *z'*)) *semi-analytical*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_ds_vecsv.html ff_iwkz_ds_vecsv>
|
| | 61 | %
|
| | 62 |
|
| | 63 | %% Default
|
| | 64 | % Program can be externally invoked with _az_, _abz_ or various other
|
| | 65 | % programs. By default, program invokes using _az_ model programs:
|
| | 66 | %
|
| | 67 | % # it_subset = 5 is basic invoke quick test
|
| | 68 | % # it_subset = 6 is invoke full test
|
| | 69 | % # it_subset = 7 is profiling invoke
|
| | 70 | % # it_subset = 8 is matlab publish
|
| | 71 | % # it_subset = 9 is invoke operational (only final stats) and coh graph
|
| | 72 | %
|
| | 73 |
|
| | 74 | if (~isempty(varargin))
|
| | 75 |
|
| | 76 | % if invoked from outside override fully
|
| | 77 | [param_map, support_map, armt_map, func_map, result_map] = varargin{:};
|
| | 78 |
|
| | 79 | else
|
| | 80 |
|
| | 81 | % default invoke
|
| | 82 | close all;
|
| | 83 |
|
| | 84 | it_param_set = 7;
|
| | 85 | st_akz_or_iwkz = 'iwkz';
|
| | 86 |
|
| | 87 | % 1. Generate Parameters
|
| | 88 | [param_map, support_map] = ffs_akz_set_default_param(it_param_set);
|
| | 89 |
|
| | 90 | % Note: param_map and support_map can be adjusted here or outside to override defaults
|
| | 91 | % param_map('it_w_n') = 50;
|
| | 92 | % param_map('it_z_n') = 15;
|
| | 93 |
|
| | 94 | % 2. Generate function and grids
|
| | 95 | [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map); % 1 for override
|
| | 96 |
|
| | 97 | % 3. Solve value and policy function using ff_iwkz_vf_vecsv
|
| | 98 | if (strcmp(st_akz_or_iwkz, 'iwkz'))
|
| | 99 | [result_map] = ff_iwkz_vf_vecsv(param_map, support_map, armt_map, func_map);
|
| | 100 | end
|
| | 101 | end
|
| | 102 |
|
| | 103 | %% Parse Parameters
|
| | 104 |
|
| | 105 | % append function name
|
| | 106 | st_func_name = 'ff_iwkz_ds_vec';
|
| | 107 | support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
|
| | 108 | support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
|
| | 109 | support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
|
| | 110 |
|
| | 111 | % result_map
|
| | 112 | % ar_st_pol_names is from section _Process Optimal Choices_ in the value
|
| | 113 | % function code.
|
| | 114 | params_group = values(result_map, {'cl_mt_pol_a', 'cl_mt_pol_k'});
|
| | 115 | [cl_mt_pol_a, cl_mt_pol_k] = params_group{:};
|
| | 116 | [mt_pol_a, mt_pol_k] = deal(cl_mt_pol_a{1}, cl_mt_pol_k{1});
|
| | 117 |
|
| | 118 | % func_map
|
| | 119 | params_group = values(func_map, {'f_coh'});
|
| | 120 | [f_coh] = params_group{:};
|
| | 121 |
|
| | 122 | % armt_map
|
| | 123 | params_group = values(armt_map, {'mt_z_trans', 'ar_z'});
|
| | 124 | [mt_z_trans, ar_z] = params_group{:};
|
| | 125 | params_group = values(armt_map, {'ar_interp_coh_grid'});
|
| | 126 | [ar_interp_coh_grid] = params_group{:};
|
| | 127 |
|
| | 128 | % param_map
|
| | 129 | params_group = values(param_map, {'it_z_n', 'it_maxiter_dist', 'fl_tol_dist'});
|
| | 130 | [it_z_n, it_maxiter_dist, fl_tol_dist] = params_group{:};
|
| | 131 |
|
| | 132 | % support_map
|
| | 133 | params_group = values(support_map, {'bl_profile_dist', 'st_profile_path', ...
|
| | 134 | 'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
|
| | 135 | 'bl_time', 'bl_display_dist', 'it_display_every'});
|
| | 136 | [bl_profile_dist, st_profile_path, ...
|
| | 137 | st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
|
| | 138 | bl_time, bl_display_dist, it_display_every] = params_group{:};
|
| | 139 |
|
| | 140 | %% Start Profiler and Timer
|
| | 141 |
|
| | 142 | % Start Profile
|
| | 143 | if (bl_profile_dist)
|
| | 144 | close all;
|
| | 145 | profile off;
|
| | 146 | profile on;
|
< 0.001 | 1 | 147 | end
|
| | 148 |
|
| | 149 | % Start Timer
|
< 0.001 | 1 | 150 | if (bl_time)
|
< 0.001 | 1 | 151 | tic;
|
< 0.001 | 1 | 152 | end
|
| | 153 |
|
| | 154 | %% A. Get Size of Endogenous and Exogenous State
|
| | 155 |
|
< 0.001 | 1 | 156 | it_endostates_n = length(ar_interp_coh_grid);
|
< 0.001 | 1 | 157 | it_exostates_n = length(ar_z);
|
| | 158 |
|
| | 159 | %% B. Initialize Output Matrixes
|
| | 160 | % Initialize the distribution to be uniform
|
| | 161 |
|
< 0.001 | 1 | 162 | mt_dist_akz_init = ones(it_endostates_n,it_exostates_n)/it_endostates_n/it_exostates_n;
|
< 0.001 | 1 | 163 | mt_dist_akz_cur = mt_dist_akz_init;
|
< 0.001 | 1 | 164 | mt_dist_akz_zeros = zeros(it_endostates_n,it_exostates_n);
|
| | 165 |
|
| | 166 | %% C. Initialize Convergence Conditions
|
| | 167 |
|
< 0.001 | 1 | 168 | bl_histiter_continue = true;
|
< 0.001 | 1 | 169 | it_iter = 0;
|
< 0.001 | 1 | 170 | ar_dist_diff_norm = zeros([it_maxiter_dist, 1]);
|
< 0.001 | 1 | 171 | mt_dist_perc_change = zeros([it_maxiter_dist, it_z_n]);
|
| | 172 |
|
| | 173 | %% D. Solve for Index
|
| | 174 | % The model is solved by interpolating over cash-on-hand. The optimal
|
| | 175 | % choices do not map to specific points on the cash-on-hand grid. Find the
|
| | 176 | % index of the cash-on-hand vector that is the closest to the
|
| | 177 | % coh'(a'(coh,z),k'(coh,z),z').
|
| | 178 | %
|
| | 179 | % Since we have *z_n* elements of shocks, and *coh_n* elements of the
|
| | 180 | % cash-on-hand grid, there are (coh_n x z_n) possible combinations of
|
| | 181 | % states at period t. In period t+1, there are (coh_n x z_n) by (z_n)
|
| | 182 | % possible/reachable cash-on-hand points. We find the index of all these
|
| | 183 | % reachable coh' points on the interpolation cash-on-hand grid.
|
| | 184 | %
|
| | 185 |
|
| | 186 | % 1. *mt_coh_prime* is (coh_n x z_n) by (z_n)
|
| | 187 | % coh'(z', a'(coh,z), k'(coh,z))
|
0.003 | 1 | 188 | mt_coh_prime = f_coh(ar_z, mt_pol_a(:), mt_pol_k(:));
|
| | 189 |
|
| | 190 | % 2. *mt_coh_prime_on_grid_idx* is (coh_n x z_n) by (z_n):
|
| | 191 | % index for coh'(a,k,z')
|
< 0.001 | 1 | 192 | mt_coh_prime_on_grid_idx = zeros(size(mt_coh_prime));
|
< 0.001 | 1 | 193 | for it_zprime_ctr=1:size(mt_coh_prime, 2)
|
< 0.001 | 15 | 194 | ar_coh_prime = mt_coh_prime(:,it_zprime_ctr);
|
0.183 | 15 | 195 | [~, ar_coh_prime_on_grid_idx] = min(abs(ar_coh_prime(:)' - ar_interp_coh_grid'));
|
< 0.001 | 15 | 196 | mt_coh_prime_on_grid_idx(:,it_zprime_ctr) = ar_coh_prime_on_grid_idx;
|
< 0.001 | 15 | 197 | end
|
| | 198 |
|
| | 199 | %% E. Solve for Unique Index
|
| | 200 | % For each z', there are (coh_n x z_n) possible coh'(z') reachable points,
|
| | 201 | % which have been converted to index: *mt_coh_prime_on_grid_idx* along the
|
| | 202 | % cash-on-hand grid in the previous code segment. Now, we find the number
|
| | 203 | % of unique coh' grid points among the (coh_n x z_n) grid indexes:
|
| | 204 | % *ar_idx_of_unique*. We also find the positions of these unique indexes in
|
| | 205 | % the full (coh_n x z_n) grid: *ar_idx_full*
|
| | 206 | %
|
| | 207 |
|
< 0.001 | 1 | 208 | cl_ar_idx_full = cell([it_exostates_n, 1]);
|
< 0.001 | 1 | 209 | cl_ar_idx_of_unique = cell([it_exostates_n, 1]);
|
| | 210 |
|
< 0.001 | 1 | 211 | for it_z_i = 1:it_exostates_n
|
| | 212 |
|
| | 213 | % 5. Cumulative probability received at state from zi
|
0.016 | 15 | 214 | [ar_idx_full, ~, ar_idx_of_unique] = unique(mt_coh_prime_on_grid_idx(:, it_z_i));
|
| | 215 |
|
< 0.001 | 15 | 216 | cl_ar_idx_full{it_z_i} = ar_idx_full;
|
< 0.001 | 15 | 217 | cl_ar_idx_of_unique{it_z_i} = ar_idx_of_unique;
|
| | 218 |
|
< 0.001 | 15 | 219 | end
|
| | 220 |
|
| | 221 | %% F. Derive Stationary Distribution
|
| | 222 | % Iterate until convergence
|
| | 223 |
|
< 0.001 | 1 | 224 | while (bl_histiter_continue)
|
| | 225 |
|
< 0.001 | 120 | 226 | it_iter = it_iter + 1;
|
| | 227 |
|
| | 228 | %% F1. Iterate over z' Shocks
|
| | 229 | % The code below loops over future states, note that the structure here is
|
| | 230 | % significant different from
|
| | 231 | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_ds.html
|
| | 232 | % ff_akz_ds>, where we looped over current shocks. Here we loop over future
|
| | 233 | % shocks because coh' is a function of z' as well as choices last period.
|
| | 234 | %
|
| | 235 |
|
| | 236 | % 1. initialize empty
|
< 0.001 | 120 | 237 | mt_dist_akz = mt_dist_akz_zeros;
|
| | 238 |
|
| | 239 | % 2. loop over next period exo shocks
|
< 0.001 | 120 | 240 | for it_z_i = 1:it_exostates_n
|
| | 241 |
|
| | 242 | % 3. *ar_zi_prob* is (coh_n x z_n) by (1):
|
| | 243 | % mt_z_trans(:, it_z_i)': for all z today, prob(z'|z) fixing z'
|
| | 244 | % overall: f(coh,z)*f(z'|z) fixing z' for all z
|
0.057 | 1800 | 245 | mt_zi_prob = mt_dist_akz_cur .* mt_z_trans(:,it_z_i)';
|
0.032 | 1800 | 246 | ar_zi_prob = mt_zi_prob(:);
|
| | 247 |
|
| | 248 | % 4. Cumulative probability received at state from zi
|
0.169 | 1800 | 249 | mt_zi_cumu_prob = accumarray(cl_ar_idx_of_unique{it_z_i}, ar_zi_prob);
|
| | 250 |
|
| | 251 | % 5. Adding up
|
0.019 | 1800 | 252 | mt_dist_akz(cl_ar_idx_full{it_z_i}, it_z_i) = mt_zi_cumu_prob + mt_dist_akz(cl_ar_idx_full{it_z_i}, it_z_i);
|
< 0.001 | 1800 | 253 | end
|
| | 254 |
|
| | 255 | %% F2. Check Tolerance and Continuation
|
| | 256 |
|
| | 257 | % Difference across iterations
|
0.031 | 120 | 258 | ar_dist_diff_norm(it_iter) = norm(mt_dist_akz - mt_dist_akz_cur);
|
0.007 | 120 | 259 | mt_dist_perc_change(it_iter, :) = sum((mt_dist_akz ~= mt_dist_akz))/it_endostates_n;
|
| | 260 |
|
| | 261 | % Update
|
0.003 | 120 | 262 | mt_dist_akz_cur = mt_dist_akz;
|
| | 263 |
|
| | 264 | % Print Iteration Results
|
< 0.001 | 120 | 265 | if (bl_display_dist && (rem(it_iter, it_display_every)==0))
|
| | 266 | fprintf('Dist it_iter:%d, fl_dist_diff:%d\n', it_iter, ar_dist_diff_norm(it_iter));
|
| | 267 | tb_hist_iter = array2table([sum(mt_dist_akz_cur,1); std(mt_dist_akz_cur,1); ...
|
| | 268 | mt_dist_akz_cur(1,:); mt_dist_akz_cur(it_endostates_n,:)]);
|
| | 269 | tb_hist_iter.Properties.VariableNames = strcat('z', string((1:size(mt_dist_akz,2))));
|
| | 270 | tb_hist_iter.Properties.RowNames = {'mdist','sddist', 'Ldist', 'Hdist'};
|
| | 271 | disp('mdist = sum(mt_dist_akz_cur,1) = sum_{a,k}(p({a,k})|z)')
|
| | 272 | disp('sddist = std(mt_pol_ak_cur,1) = std_{a,k}(p({a,k})|z)')
|
| | 273 | disp('Ldist = mt_dist_akz_cur(1,:) = p(min({a,k})|z)')
|
| | 274 | disp('Hdist = mt_dist_akz_cur(it_a_n,:) = p(max({a,k})|z)')
|
| | 275 | disp(tb_hist_iter);
|
| | 276 | end
|
| | 277 |
|
| | 278 | % Continuation Conditions:
|
< 0.001 | 120 | 279 | if (it_iter == (it_maxiter_dist + 1))
|
< 0.001 | 1 | 280 | bl_histiter_continue = false;
|
< 0.001 | 119 | 281 | elseif ((it_iter == it_maxiter_dist) || ...
|
| 119 | 282 | (ar_dist_diff_norm(it_iter) < fl_tol_dist))
|
< 0.001 | 1 | 283 | it_iter_last = it_iter;
|
< 0.001 | 1 | 284 | it_iter = it_maxiter_dist;
|
< 0.001 | 1 | 285 | end
|
| | 286 |
|
< 0.001 | 120 | 287 | end
|
| | 288 |
|
| | 289 | %% End Time and Profiler
|
| | 290 |
|
| | 291 | % End Timer
|
< 0.001 | 1 | 292 | if (bl_time)
|
< 0.001 | 1 | 293 | toc;
|
< 0.001 | 1 | 294 | end
|
| | 295 |
|
| | 296 | % End Profile
|
< 0.001 | 1 | 297 | if (bl_profile_dist)
|
0.004 | 1 | 298 | profile off
|
| | 299 | profile viewer
|
| | 300 | st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
|
| | 301 | profsave(profile('info'), strcat(st_profile_path, st_file_name));
|
| | 302 | end
|
| | 303 |
|
| | 304 |
|
| | 305 | %% *f(y), f(c), f(a), f(k)*: Generate Key Distributional Statistics for Each outcome
|
| | 306 | % Having derived f({a,k},z) the probability mass function of the joint discrete
|
| | 307 | % random variables, we now obtain distributional statistics. Note that we
|
| | 308 | % know f({a,k},z), and we also know relevant policy functions a'(a,k,z), k'(a,k,z),
|
| | 309 | % or other policy functions. We can simulate any choices that are a
|
| | 310 | % function of the random variables (coh(a,k),z), using f(coh(a,k),z). We call function
|
| | 311 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html
|
| | 312 | % ff_az_ds_post_stats> which uses
|
| | 313 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html
|
| | 314 | % fft_disc_rand_var_stats> and
|
| | 315 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html
|
| | 316 | % fft_disc_rand_var_mass2outcomes> to compute various statistics of
|
| | 317 | % interest.
|
| | 318 |
|
| | 319 | result_map = ff_az_ds_post_stats(support_map, result_map, mt_dist_akz);
|
| | 320 |
|
| | 321 | end
|
Other subfunctions in this file are not included in this listing.