time | Calls | line |
---|
| | 7 | function [result_map] = ff_iwkz_ds(varargin)
|
| | 8 | %% FF_IWKZ_DS 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 uses loops.
|
| | 14 | %
|
| | 15 | % This is the two-stage with interpolation version of
|
| | 16 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_akz_ds.html
|
| | 17 | % ff_akz_ds>. See that file, as well as
|
| | 18 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html
|
| | 19 | % ff_az_ds> for additional descriptions and comparisons. These two
|
| | 20 | % functions are different. Specifically, the code here works when we are
|
| | 21 | % looking for the distribution of f(a,z), where a'(a,z,z'), meaning that
|
| | 22 | % the _a_ next period is determined by choices last period and some _z_
|
| | 23 | % last period, but also _z'_ this period. _a_ here specifically
|
| | 24 | % cash-on-hand. This contrasts with
|
| | 25 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html
|
| | 26 | % ff_az_ds>, which works for a'(a,z), _a'_ can not be a function of _z'_.
|
| | 27 | %
|
| | 28 | % @example
|
| | 29 | %
|
| | 30 | % % Get Default Parameters
|
| | 31 | % it_param_set = 6;
|
| | 32 | % [param_map, support_map] = ffs_az_set_default_param(it_param_set);
|
| | 33 | % % Change Keys in param_map
|
| | 34 | % param_map('it_w_n') = 750;
|
| | 35 | % param_map('it_ak_n') = param_map('it_w_n');
|
| | 36 | % param_map('it_z_n') = 11;
|
| | 37 | % param_map('fl_a_max') = 100;
|
| | 38 | % param_map('fl_w') = 1.3;
|
| | 39 | % % Change Keys support_map
|
| | 40 | % support_map('bl_display') = false;
|
| | 41 | % support_map('bl_post') = true;
|
| | 42 | % support_map('bl_display_final') = false;
|
| | 43 | % % Call Program with external parameters that override defaults
|
| | 44 | % ff_iwkz_ds(param_map, support_map);
|
| | 45 | %
|
| | 46 | % @include
|
| | 47 | %
|
| | 48 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html ff_wkz_vf_vecsv>
|
| | 49 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html ff_az_ds_post_stats>
|
| | 50 | % * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html fft_disc_rand_var_stats>
|
| | 51 | % * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html fft_disc_rand_var_mass2outcomes>
|
| | 52 | %
|
| | 53 | % @seealso
|
| | 54 | %
|
| | 55 | % * 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>
|
| | 56 | % * 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>
|
| | 57 | % * 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>
|
| | 58 | % * 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>
|
| | 59 | % * 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>
|
| | 60 | % * 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>
|
| | 61 | % * 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>
|
| | 62 | %
|
| | 63 |
|
| | 64 | %% Default
|
| | 65 | % Program can be externally invoked with _az_, _abz_ or various other
|
| | 66 | % programs. By default, program invokes using _az_ model programs:
|
| | 67 | %
|
| | 68 | % # it_subset = 5 is basic invoke quick test
|
| | 69 | % # it_subset = 6 is invoke full test
|
| | 70 | % # it_subset = 7 is profiling invoke
|
| | 71 | % # it_subset = 8 is matlab publish
|
| | 72 | % # it_subset = 9 is invoke operational (only final stats) and coh graph
|
| | 73 | %
|
| | 74 |
|
| | 75 | params_len = length(varargin);
|
| | 76 | bl_input_override = 0;
|
| | 77 | if (params_len == 6)
|
| | 78 | bl_input_override = varargin{6};
|
| | 79 | end
|
| | 80 |
|
| | 81 | if (bl_input_override)
|
| | 82 | % if invoked from outside override fully
|
| | 83 | [param_map, support_map, armt_map, func_map, result_map, ~] = varargin{:};
|
| | 84 |
|
| | 85 | else
|
| | 86 | % default invoke
|
| | 87 | close all;
|
| | 88 |
|
| | 89 | it_param_set = 7;
|
| | 90 | st_akz_or_iwkz = 'iwkz';
|
| | 91 |
|
| | 92 | bl_input_override = true;
|
| | 93 |
|
| | 94 | % 1. Generate Parameters
|
| | 95 | [param_map, support_map] = ffs_akz_set_default_param(it_param_set);
|
| | 96 |
|
| | 97 | % Note: param_map and support_map can be adjusted here or outside to override defaults
|
| | 98 | % param_map('it_w_n') = 50;
|
| | 99 | % param_map('it_z_n') = 15;
|
| | 100 |
|
| | 101 | % 2. Generate function and grids
|
| | 102 | [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
|
| | 103 |
|
| | 104 | % 3. Solve value and policy function using ff_iwkz_vf_vecsv
|
| | 105 | if (strcmp(st_akz_or_iwkz, 'iwkz'))
|
| | 106 | [result_map] = ff_iwkz_vf_vecsv(param_map, support_map, armt_map, func_map);
|
| | 107 | end
|
| | 108 | end
|
| | 109 |
|
| | 110 | %% Parse Parameters
|
| | 111 |
|
| | 112 | % append function name
|
| | 113 | st_func_name = 'ff_iwkz_ds';
|
| | 114 | support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
|
| | 115 | support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
|
| | 116 | support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
|
| | 117 |
|
| | 118 | % result_map
|
| | 119 | % ar_st_pol_names is from section _Process Optimal Choices_ in the value
|
| | 120 | % function code.
|
| | 121 | params_group = values(result_map, {'cl_mt_pol_a', 'cl_mt_pol_k'});
|
| | 122 | [cl_mt_pol_a, cl_mt_pol_k] = params_group{:};
|
| | 123 | [mt_pol_a, mt_pol_k] = deal(cl_mt_pol_a{1}, cl_mt_pol_k{1});
|
| | 124 |
|
| | 125 | % func_map
|
| | 126 | params_group = values(func_map, {'f_coh'});
|
| | 127 | [f_coh] = params_group{:};
|
| | 128 |
|
| | 129 | % armt_map
|
| | 130 | params_group = values(armt_map, {'mt_z_trans', 'ar_z'});
|
| | 131 | [mt_z_trans, ar_z] = params_group{:};
|
| | 132 | params_group = values(armt_map, {'ar_interp_coh_grid'});
|
| | 133 | [ar_interp_coh_grid] = params_group{:};
|
| | 134 |
|
| | 135 | % param_map
|
| | 136 | params_group = values(param_map, {'it_z_n', 'it_maxiter_dist', 'fl_tol_dist'});
|
| | 137 | [it_z_n, it_maxiter_dist, fl_tol_dist] = params_group{:};
|
| | 138 |
|
| | 139 | % support_map
|
| | 140 | params_group = values(support_map, {'bl_profile_dist', 'st_profile_path', ...
|
| | 141 | 'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
|
| | 142 | 'bl_time', 'bl_display_dist', 'it_display_every'});
|
| | 143 | [bl_profile_dist, st_profile_path, ...
|
| | 144 | st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
|
| | 145 | bl_time, bl_display_dist, it_display_every] = params_group{:};
|
| | 146 |
|
| | 147 | %% Start Profiler and Timer
|
| | 148 |
|
| | 149 | % Start Profile
|
| | 150 | if (bl_profile_dist)
|
| | 151 | close all;
|
| | 152 | profile off;
|
| | 153 | profile on;
|
< 0.001 | 1 | 154 | end
|
| | 155 |
|
| | 156 | % Start Timer
|
< 0.001 | 1 | 157 | if (bl_time)
|
< 0.001 | 1 | 158 | tic;
|
< 0.001 | 1 | 159 | end
|
| | 160 |
|
| | 161 | %% A. Get Size of Endogenous and Exogenous State
|
| | 162 |
|
< 0.001 | 1 | 163 | it_endostates_n = length(ar_interp_coh_grid);
|
< 0.001 | 1 | 164 | it_exostates_n = length(ar_z);
|
| | 165 |
|
| | 166 | %% B. *f({a,k},z)*: Initialize Output Matrixes
|
| | 167 | % Initialize the distribution to be uniform
|
| | 168 |
|
< 0.001 | 1 | 169 | mt_dist_akz_init = ones(it_endostates_n,it_exostates_n)/it_endostates_n/it_exostates_n;
|
< 0.001 | 1 | 170 | mt_dist_akz_cur = mt_dist_akz_init;
|
< 0.001 | 1 | 171 | mt_dist_akz_zeros = zeros(it_endostates_n,it_exostates_n);
|
| | 172 |
|
| | 173 | %% C. *f({a,k},z)*: Initialize Convergence Conditions
|
| | 174 |
|
< 0.001 | 1 | 175 | bl_histiter_continue = true;
|
< 0.001 | 1 | 176 | it_iter = 0;
|
< 0.001 | 1 | 177 | ar_dist_diff_norm = zeros([it_maxiter_dist, 1]);
|
< 0.001 | 1 | 178 | mt_dist_perc_change = zeros([it_maxiter_dist, it_z_n]);
|
| | 179 |
|
| | 180 | %% D. *f({a,k},z)*: Derive Stationary Distribution
|
| | 181 | % Iterate over the discrete joint random variable variables ({a,k},z)
|
| | 182 | %
|
| | 183 | % We are looking for the distribution of: $p({a,k},z)$ where $a'({a,k},z)$
|
| | 184 | % and $k'({a,k},z)$, meaning that $a'$ and $k'$ are determined by $a$ and
|
| | 185 | % $k$ and $z$, but not $z'$.
|
| | 186 | %
|
| | 187 |
|
< 0.001 | 1 | 188 | while (bl_histiter_continue)
|
| | 189 |
|
< 0.001 | 120 | 190 | it_iter = it_iter + 1;
|
| | 191 |
|
| | 192 | %% *f({a,k},z)*: Iterate over Probability mass for Discrete Random Variable
|
| | 193 | % compared to
|
| | 194 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_akz_vf.html
|
| | 195 | % ff_akz_vf>, we basically have the same set of loops. There, there were
|
| | 196 | % four loops, here there are three loops. We eliminated the loop over
|
| | 197 | % next period choices, because here we already know optimal choices
|
| | 198 |
|
| | 199 | % initialize empty
|
< 0.001 | 120 | 200 | mt_dist_akz = mt_dist_akz_zeros;
|
| | 201 |
|
| | 202 | % loop 1: over exogenous states
|
< 0.001 | 120 | 203 | for it_z_i = 1:it_exostates_n
|
| | 204 |
|
| | 205 | % loop 2: over endogenous states
|
< 0.001 | 1800 | 206 | for it_coh_grid_j = 1:it_endostates_n
|
| | 207 |
|
| | 208 | % f(a'|a) = 1 for only one a'
|
| | 209 | % in dynamic programming problem, had a loop over choices, now
|
| | 210 | % already have optimal choices, do not need to loop
|
0.047 | 1022400 | 211 | fl_aprime = mt_pol_a(it_coh_grid_j, it_z_i);
|
0.048 | 1022400 | 212 | fl_kprime = mt_pol_k(it_coh_grid_j, it_z_i);
|
| | 213 |
|
| | 214 | % loop 3: loop over future shocks
|
| | 215 | % E_{coh,z}(f(coh'(a',k',z'),z'|coh,z)*f(coh,z))
|
0.053 | 1022400 | 216 | for it_zp_q = 1:it_exostates_n
|
| | 217 |
|
| | 218 | % A. Get the index that the index for coh' based on coh,z,z'
|
| | 219 | % current shock
|
0.748 | 15336000 | 220 | fl_zprime = ar_z(it_zp_q);
|
| | 221 | % cash-on-hand next period which is a function also of z'
|
62.332 | 15336000 | 222 | fl_coh_prime = f_coh(fl_zprime, fl_aprime, fl_kprime);
|
| | 223 | % next period index
|
36.868 | 15336000 | 224 | [~, it_coh_prime_on_grid_idx] = min(abs(fl_coh_prime - ar_interp_coh_grid));
|
| | 225 |
|
| | 226 | % B. prob of going to coh'(coh,z,z')
|
| | 227 | % current probablity at (a,z)
|
0.669 | 15336000 | 228 | fl_cur_zak_prob = mt_dist_akz_cur(it_coh_grid_j, it_z_i);
|
| | 229 | % f(z'|z) transition
|
0.674 | 15336000 | 230 | fl_ztoz_trans = mt_z_trans(it_z_i, it_zp_q);
|
| | 231 | % f(a',z'|a,z)*f({a,k},z)
|
0.652 | 15336000 | 232 | fl_zfromzak = fl_cur_zak_prob*fl_ztoz_trans;
|
| | 233 |
|
| | 234 | % cumulating
|
1.403 | 15336000 | 235 | mt_dist_akz(it_coh_prime_on_grid_idx, it_zp_q) = ...
|
| 15336000 | 236 | mt_dist_akz(it_coh_prime_on_grid_idx, it_zp_q) + fl_zfromzak;
|
0.737 | 15336000 | 237 | end
|
| | 238 |
|
0.045 | 1022400 | 239 | end
|
| | 240 |
|
0.001 | 1800 | 241 | end
|
| | 242 |
|
| | 243 | %% *f({a,k},z)*: Check Tolerance and Continuation
|
| | 244 |
|
| | 245 | % Difference across iterations
|
0.102 | 120 | 246 | ar_dist_diff_norm(it_iter) = norm(mt_dist_akz - mt_dist_akz_cur);
|
0.010 | 120 | 247 | mt_dist_perc_change(it_iter, :) = sum((mt_dist_akz ~= mt_dist_akz))/it_endostates_n;
|
| | 248 |
|
| | 249 | % Update
|
0.012 | 120 | 250 | mt_dist_akz_cur = mt_dist_akz;
|
| | 251 |
|
| | 252 | % Print Iteration Results
|
< 0.001 | 120 | 253 | if (bl_display_dist && (rem(it_iter, it_display_every)==0))
|
| | 254 | fprintf('Dist it_iter:%d, fl_dist_diff:%d\n', it_iter, ar_dist_diff_norm(it_iter));
|
| | 255 | tb_hist_iter = array2table([sum(mt_dist_akz_cur,1); std(mt_dist_akz_cur,1); ...
|
| | 256 | mt_dist_akz_cur(1,:); mt_dist_akz_cur(it_endostates_n,:)]);
|
| | 257 | tb_hist_iter.Properties.VariableNames = strcat('z', string((1:size(mt_dist_akz,2))));
|
| | 258 | tb_hist_iter.Properties.RowNames = {'mdist','sddist', 'Ldist', 'Hdist'};
|
| | 259 | disp('mdist = sum(mt_dist_akz_cur,1) = sum_{a,k}(p({a,k})|z)')
|
| | 260 | disp('sddist = std(mt_pol_ak_cur,1) = std_{a,k}(p({a,k})|z)')
|
| | 261 | disp('Ldist = mt_dist_akz_cur(1,:) = p(min({a,k})|z)')
|
| | 262 | disp('Hdist = mt_dist_akz_cur(it_a_n,:) = p(max({a,k})|z)')
|
| | 263 | disp(tb_hist_iter);
|
| | 264 | end
|
| | 265 |
|
| | 266 | % Continuation Conditions:
|
< 0.001 | 120 | 267 | if (it_iter == (it_maxiter_dist + 1))
|
< 0.001 | 1 | 268 | bl_histiter_continue = false;
|
< 0.001 | 119 | 269 | elseif ((it_iter == it_maxiter_dist) || ...
|
| 119 | 270 | (ar_dist_diff_norm(it_iter) < fl_tol_dist))
|
< 0.001 | 1 | 271 | it_iter_last = it_iter;
|
< 0.001 | 1 | 272 | it_iter = it_maxiter_dist;
|
< 0.001 | 1 | 273 | end
|
| | 274 |
|
< 0.001 | 120 | 275 | end
|
| | 276 |
|
| | 277 | %% End Time and Profiler
|
| | 278 |
|
| | 279 | % End Timer
|
< 0.001 | 1 | 280 | if (bl_time)
|
< 0.001 | 1 | 281 | toc;
|
< 0.001 | 1 | 282 | end
|
| | 283 |
|
| | 284 | % End Profile
|
< 0.001 | 1 | 285 | if (bl_profile_dist)
|
0.005 | 1 | 286 | profile off
|
| | 287 | profile viewer
|
| | 288 | st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
|
| | 289 | profsave(profile('info'), strcat(st_profile_path, st_file_name));
|
| | 290 | end
|
| | 291 |
|
| | 292 |
|
| | 293 | %% *f(y), f(c), f(a), f(k)*: Generate Key Distributional Statistics for Each outcome
|
| | 294 | % Having derived f({a,k},z) the probability mass function of the joint discrete
|
| | 295 | % random variables, we now obtain distributional statistics. Note that we
|
| | 296 | % know f({a,k},z), and we also know relevant policy functions a'(a,z), c(a,z),
|
| | 297 | % or other policy functions. We can simulate any choices that are a
|
| | 298 | % function of the random variables (a,z), using f({a,k},z). We call function
|
| | 299 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html
|
| | 300 | % ff_az_ds_post_stats> which uses
|
| | 301 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html
|
| | 302 | % fft_disc_rand_var_stats> and
|
| | 303 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html
|
| | 304 | % fft_disc_rand_var_mass2outcomes> to compute various statistics of
|
| | 305 | % interest.
|
| | 306 |
|
| | 307 | bl_input_override = true;
|
| | 308 | result_map = ff_az_ds_post_stats(support_map, result_map, mt_dist_akz, bl_input_override);
|
| | 309 |
|
| | 310 | end
|
Other subfunctions in this file are not included in this listing.