time | Calls | line |
---|
| | 7 | function result_map = ff_az_vf(varargin)
|
| | 8 | %% FF_AZ_VF solve infinite horizon exo shock + endo asset problem
|
| | 9 | % This program solves the infinite horizon dynamic single asset and single
|
| | 10 | % shock problem with loops. It is useful to have a version of code that is
|
| | 11 | % looped for easy debugging. This is the standard dynamic exogenous
|
| | 12 | % incomplete savings problem.
|
| | 13 | %
|
| | 14 | % See
|
| | 15 | % <https://fanwangecon.github.io/CodeDynaAsset/m_abz/solve/html/ff_abz_vf.html
|
| | 16 | % ff_abz_vf> for the version of the problem that accommodates both borrowing
|
| | 17 | % and savings.
|
| | 18 | %
|
| | 19 | % @param param_map container parameter container
|
| | 20 | %
|
| | 21 | % @param support_map container support container
|
| | 22 | %
|
| | 23 | % @param armt_map container container with states, choices and shocks
|
| | 24 | % grids that are inputs for grid based solution algorithm
|
| | 25 | %
|
| | 26 | % @param func_map container container with function handles for
|
| | 27 | % consumption cash-on-hand etc.
|
| | 28 | %
|
| | 29 | % @return result_map container contains policy function matrix, value
|
| | 30 | % function matrix, iteration results, and policy function, value function
|
| | 31 | % and iteration results tables.
|
| | 32 | %
|
| | 33 | % keys included in result_map:
|
| | 34 | %
|
| | 35 | % * mt_val matrix states_n by shock_n matrix of converged value function grid
|
| | 36 | % * mt_pol_a matrix states_n by shock_n matrix of converged policy function grid
|
| | 37 | % * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
|
| | 38 | % difference between iteration
|
| | 39 | % * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
|
| | 40 | % function difference between iterations
|
| | 41 | % * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
|
| | 42 | % proportion of grid points at which policy function changed between
|
| | 43 | % current and last iteration for each element of shock
|
| | 44 | %
|
| | 45 | % @example
|
| | 46 | %
|
| | 47 | % % Get Default Parameters
|
| | 48 | % it_param_set = 2;
|
| | 49 | % [param_map, support_map] = ffs_abz_set_default_param(it_param_set);
|
| | 50 | % % Change Keys in param_map
|
| | 51 | % param_map('it_a_n') = 50;
|
| | 52 | % param_map('it_z_n') = 5;
|
| | 53 | % param_map('fl_a_max') = 100;
|
| | 54 | % param_map('fl_w') = 1.3;
|
| | 55 | % % Change Keys support_map
|
| | 56 | % support_map('bl_display') = false;
|
| | 57 | % support_map('bl_post') = true;
|
| | 58 | % support_map('bl_display_final') = false;
|
| | 59 | % % Call Program with external parameters that override defaults
|
| | 60 | % % Note this program works very slowly if the grid sizes are too large
|
| | 61 | % ff_az_vf(param_map, support_map);
|
| | 62 | %
|
| | 63 | % @include
|
| | 64 | %
|
| | 65 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_az/paramfunc/html/ffs_az_set_default_param.html ffs_az_set_default_param>
|
| | 66 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_az/paramfunc/html/ffs_az_get_funcgrid.html ffs_az_get_funcgrid>
|
| | 67 | % * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_vf_post.html ff_az_vf_post>
|
| | 68 | %
|
| | 69 | % @seealso
|
| | 70 | %
|
| | 71 | % * save loop: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf.html ff_az_vf>
|
| | 72 | % * save vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vec.html ff_az_vf_vec>
|
| | 73 | % * save optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html ff_az_vf_vecsv>
|
| | 74 | % * save + borr loop: <https://fanwangecon.github.io/CodeDynaAsset/m_abz/solve/html/ff_abz_vf.html ff_abz_vf>
|
| | 75 | % * save + borr vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_abz/solve/html/ff_abz_vf_vec.html ff_abz_vf_vec>
|
| | 76 | % * save + borr optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_abz/solve/html/ff_abz_vf_vecsv.html ff_abz_vf_vecsv>
|
| | 77 | %
|
| | 78 |
|
| | 79 | %% Default
|
| | 80 | % * it_param_set = 1: quick test
|
| | 81 | % * it_param_set = 2: benchmark run
|
| | 82 | % * it_param_set = 3: benchmark profile
|
| | 83 | % * it_param_set = 4: press publish button
|
| | 84 |
|
| | 85 | it_param_set = 3;
|
| | 86 | bl_input_override = true;
|
| | 87 | [param_map, support_map] = ffs_az_set_default_param(it_param_set);
|
| | 88 |
|
| | 89 | % Note: param_map and support_map can be adjusted here or outside to override defaults
|
| | 90 | % param_map('it_a_n') = 750;
|
| | 91 | % param_map('it_z_n') = 15;
|
| | 92 |
|
| | 93 | [armt_map, func_map] = ffs_az_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
|
| | 94 | default_params = {param_map support_map armt_map func_map};
|
| | 95 |
|
| | 96 | %% Parse Parameters 1
|
| | 97 |
|
| | 98 | % if varargin only has param_map and support_map,
|
| | 99 | params_len = length(varargin);
|
| | 100 | [default_params{1:params_len}] = varargin{:};
|
| | 101 | param_map = [param_map; default_params{1}];
|
| | 102 | support_map = [support_map; default_params{2}];
|
| | 103 | if params_len >= 1 && params_len <= 2
|
| | 104 | % If override param_map, re-generate armt and func if they are not
|
| | 105 | % provided
|
| | 106 | bl_input_override = true;
|
| | 107 | [armt_map, func_map] = ffs_az_get_funcgrid(param_map, support_map, bl_input_override);
|
| | 108 | else
|
| | 109 | % Override all
|
| | 110 | armt_map = [armt_map; default_params{3}];
|
| | 111 | func_map = [func_map; default_params{4}];
|
| | 112 | end
|
| | 113 |
|
| | 114 | % append function name
|
| | 115 | st_func_name = 'ff_az_vf';
|
| | 116 | support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
|
| | 117 | support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
|
| | 118 | support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
|
| | 119 |
|
| | 120 | %% Parse Parameters 2
|
| | 121 |
|
| | 122 | % armt_map
|
| | 123 | params_group = values(armt_map, {'ar_a', 'mt_z_trans', 'ar_z'});
|
| | 124 | [ar_a, mt_z_trans, ar_z] = params_group{:};
|
| | 125 | % func_map
|
| | 126 | params_group = values(func_map, {'f_util_log', 'f_util_crra', 'f_cons', 'f_coh'});
|
| | 127 | [f_util_log, f_util_crra, f_cons, f_coh] = params_group{:};
|
| | 128 | % param_map
|
| | 129 | params_group = values(param_map, {'it_a_n', 'it_z_n', 'fl_crra', 'fl_beta', 'fl_nan_replace'});
|
| | 130 | [it_a_n, it_z_n, fl_crra, fl_beta, fl_nan_replace] = params_group{:};
|
| | 131 | params_group = values(param_map, {'it_maxiter_val', 'fl_tol_val', 'fl_tol_pol', 'it_tol_pol_nochange'});
|
| | 132 | [it_maxiter_val, fl_tol_val, fl_tol_pol, it_tol_pol_nochange] = params_group{:};
|
| | 133 | % support_map
|
| | 134 | params_group = values(support_map, {'bl_profile', 'st_profile_path', ...
|
| | 135 | 'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
|
| | 136 | 'bl_time', 'bl_display', 'it_display_every', 'bl_post'});
|
| | 137 | [bl_profile, st_profile_path, ...
|
| | 138 | st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
|
| | 139 | bl_time, bl_display, it_display_every, bl_post] = params_group{:};
|
| | 140 |
|
| | 141 | %% Initialize Output Matrixes
|
| | 142 |
|
| | 143 | mt_val_cur = zeros(length(ar_a),length(ar_z));
|
| | 144 | mt_val = mt_val_cur - 1;
|
| | 145 | mt_pol_a = zeros(length(ar_a),length(ar_z));
|
| | 146 | mt_pol_a_cur = mt_pol_a - 1;
|
| | 147 |
|
| | 148 | %% Initialize Convergence Conditions
|
| | 149 |
|
| | 150 | bl_vfi_continue = true;
|
| | 151 | it_iter = 0;
|
| | 152 | ar_val_diff_norm = zeros([it_maxiter_val, 1]);
|
| | 153 | ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
|
| | 154 | mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
|
| | 155 |
|
| | 156 | %% Iterate Value Function
|
| | 157 | % Loop solution with 4 nested loops
|
| | 158 | %
|
| | 159 | % # loop 1: over exogenous states
|
| | 160 | % # loop 2: over endogenous states
|
| | 161 | % # loop 3: over choices
|
| | 162 | % # loop 4: add future utility, integration--loop over future shocks
|
| | 163 | %
|
| | 164 |
|
| | 165 | % Start Profile
|
| | 166 | if (bl_profile)
|
| | 167 | close all;
|
| | 168 | profile off;
|
| | 169 | profile on;
|
< 0.001 | 1 | 170 | end
|
| | 171 |
|
| | 172 | % Start Timer
|
< 0.001 | 1 | 173 | if (bl_time)
|
< 0.001 | 1 | 174 | tic;
|
< 0.001 | 1 | 175 | end
|
| | 176 |
|
| | 177 | % Value Function Iteration
|
< 0.001 | 1 | 178 | while bl_vfi_continue
|
< 0.001 | 106 | 179 | it_iter = it_iter + 1;
|
| | 180 |
|
| | 181 | %% Solve Optimization Problem Current Iteration
|
| | 182 |
|
| | 183 | % loop 1: over exogenous states
|
< 0.001 | 106 | 184 | for it_z_i = 1:length(ar_z)
|
< 0.001 | 1590 | 185 | fl_z = ar_z(it_z_i);
|
| | 186 |
|
| | 187 | % loop 2: over endogenous states
|
< 0.001 | 1590 | 188 | for it_a_j = 1:length(ar_a)
|
0.045 | 1192500 | 189 | fl_a = ar_a(it_a_j);
|
1.703 | 1192500 | 190 | ar_val_cur = zeros(size(ar_a));
|
| | 191 |
|
| | 192 | % loop 3: over choices
|
0.061 | 1192500 | 193 | for it_ap_k = 1:length(ar_a)
|
32.491 | 894375000 | 194 | fl_ap = ar_a(it_ap_k);
|
2894.067 | 894375000 | 195 | fl_c = f_cons(fl_z, fl_a, fl_ap);
|
| | 196 |
|
| | 197 | % current utility
|
29.605 | 894375000 | 198 | if (fl_crra == 1)
|
| | 199 | ar_val_cur(it_ap_k) = f_util_log(fl_c);
|
29.298 | 894375000 | 200 | else
|
2503.963 | 894375000 | 201 | ar_val_cur(it_ap_k) = f_util_crra(fl_c);
|
27.892 | 894375000 | 202 | end
|
| | 203 |
|
| | 204 | % loop 4: add future utility, integration--loop over future shocks
|
34.482 | 894375000 | 205 | for it_zp_q = 1:length(ar_z)
|
724.781 | 13415625000 | 206 | ar_val_cur(it_ap_k) = ar_val_cur(it_ap_k) + fl_beta*mt_z_trans(it_z_i,it_zp_q)*mt_val_cur(it_ap_k,it_zp_q);
|
447.903 | 13415625000 | 207 | end
|
| | 208 |
|
| | 209 | % Replace if negative consumption
|
27.767 | 894375000 | 210 | if fl_c <= 0
|
1377.009 | 411479492 | 211 | ar_val_cur(it_ap_k) = fl_nan_replace;
|
12.954 | 411479492 | 212 | end
|
| | 213 |
|
35.719 | 894375000 | 214 | end
|
| | 215 |
|
| | 216 | % maximization over loop 3 choices for loop 1+2 states
|
3.622 | 1192500 | 217 | it_max_lin_idx = find(ar_val_cur == max(ar_val_cur));
|
0.063 | 1192500 | 218 | mt_val(it_a_j,it_z_i) = ar_val_cur(it_max_lin_idx(1));
|
0.063 | 1192500 | 219 | mt_pol_a(it_a_j,it_z_i) = ar_a(it_max_lin_idx(1));
|
| | 220 |
|
0.075 | 1192500 | 221 | end
|
0.002 | 1590 | 222 | end
|
| | 223 |
|
| | 224 | %% Check Tolerance and Continuation
|
| | 225 |
|
| | 226 | % Difference across iterations
|
0.056 | 106 | 227 | ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur);
|
0.035 | 106 | 228 | ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur);
|
0.009 | 106 | 229 | mt_pol_perc_change(it_iter, :) = sum((mt_pol_a ~= mt_pol_a_cur))/(it_a_n);
|
| | 230 |
|
| | 231 | % Update
|
0.003 | 106 | 232 | mt_val_cur = mt_val;
|
0.001 | 106 | 233 | mt_pol_a_cur = mt_pol_a;
|
| | 234 |
|
| | 235 | % Print Iteration Results
|
< 0.001 | 106 | 236 | if (bl_display && (rem(it_iter, it_display_every)==0))
|
| | 237 | fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
|
| | 238 | it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
|
| | 239 | tb_valpol_iter = array2table([mean(mt_val_cur,1); mean(mt_pol_a_cur,1); ...
|
| | 240 | mt_val_cur(it_a_n,:); mt_pol_a_cur(it_a_n,:)]);
|
| | 241 | tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
|
| | 242 | tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'Hval', 'Hap'};
|
| | 243 | disp('mval = mean(mt_val_cur,1), average value over a')
|
| | 244 | disp('map = mean(mt_pol_a_cur,1), average choice over a')
|
| | 245 | disp('Hval = mt_val_cur(it_a_n,:), highest a state val')
|
| | 246 | disp('Hap = mt_pol_a_cur(it_a_n,:), highest a state choice')
|
| | 247 | disp(tb_valpol_iter);
|
| | 248 | end
|
| | 249 |
|
| | 250 | % Continuation Conditions:
|
| | 251 | % 1. if value function convergence criteria reached
|
| | 252 | % 2. if policy function variation over iterations is less than
|
| | 253 | % threshold
|
< 0.001 | 106 | 254 | if (it_iter == (it_maxiter_val + 1))
|
< 0.001 | 1 | 255 | bl_vfi_continue = false;
|
0.002 | 105 | 256 | elseif ((it_iter == it_maxiter_val) || ...
|
| 105 | 257 | (ar_val_diff_norm(it_iter) < fl_tol_val) || ...
|
| 105 | 258 | (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol))
|
| | 259 | % Fix to max, run again to save results if needed
|
< 0.001 | 1 | 260 | it_iter_last = it_iter;
|
< 0.001 | 1 | 261 | it_iter = it_maxiter_val;
|
< 0.001 | 1 | 262 | end
|
| | 263 |
|
< 0.001 | 106 | 264 | end
|
| | 265 |
|
| | 266 | % End Timer
|
< 0.001 | 1 | 267 | if (bl_time)
|
< 0.001 | 1 | 268 | toc;
|
< 0.001 | 1 | 269 | end
|
| | 270 |
|
| | 271 | % End Profile
|
< 0.001 | 1 | 272 | if (bl_profile)
|
0.004 | 1 | 273 | profile off
|
| | 274 | profile viewer
|
| | 275 | st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
|
| | 276 | profsave(profile('info'), strcat(st_profile_path, st_file_name));
|
| | 277 | end
|
| | 278 |
|
| | 279 | %% Process Optimal Choices
|
| | 280 | % for choices outcomes, store as cell with two elements, first element is
|
| | 281 | % the y(a,z), outcome given states, the second element will be solved found
|
| | 282 | % in
|
| | 283 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_ds_vf.html
|
| | 284 | % ff_ds_vf> and other distributions files. It stores what are the
|
| | 285 | % probability mass function of y, along with sorted unique values of y.
|
| | 286 |
|
| | 287 | result_map = containers.Map('KeyType','char', 'ValueType','any');
|
| | 288 | result_map('mt_val') = mt_val;
|
| | 289 |
|
| | 290 | result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
|
| | 291 | result_map('cl_mt_pol_coh') = {f_coh(ar_z, ar_a'), zeros(1)};
|
| | 292 | result_map('cl_mt_pol_c') = {f_coh(ar_z, ar_a') - mt_pol_a, zeros(1)};
|
| | 293 | result_map('ar_st_pol_names') = ["cl_mt_pol_a", "cl_mt_pol_coh", "cl_mt_pol_c"];
|
| | 294 |
|
| | 295 | if (bl_post)
|
| | 296 | bl_input_override = true;
|
| | 297 | result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
|
| | 298 | result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
|
| | 299 | result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
|
| | 300 | result_map = ff_az_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
|
| | 301 | end
|
| | 302 |
|
| | 303 | end
|
Other subfunctions in this file are not included in this listing.