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