time | Calls | line |
---|
| | 7 | function result_map = ff_ipwkz_vf_vecsv(varargin)
|
| | 8 | %% FF_IPWKZ_VF_VECSV solve infinite horizon exo shock + endo asset problem
|
| | 9 | % This program solves the infinite horizon dynamic savings and risky
|
| | 10 | % capital asset problem with some ar1 shock. This is the two step solution
|
| | 11 | % with interpolation and with percentage asset grids version of
|
| | 12 | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html
|
| | 13 | % ff_iwkz_vf_vecsv>. See that file for more descriptions. This is the
|
| | 14 | % optimized-vectorized version of the program.
|
| | 15 | %
|
| | 16 | % @param param_map container parameter container
|
| | 17 | %
|
| | 18 | % @param support_map container support container
|
| | 19 | %
|
| | 20 | % @param armt_map container container with states, choices and shocks
|
| | 21 | % grids that are inputs for grid based solution algorithm
|
| | 22 | %
|
| | 23 | % @param func_map container container with function handles for
|
| | 24 | % consumption cash-on-hand etc.
|
| | 25 | %
|
| | 26 | % @return result_map container contains policy function matrix, value
|
| | 27 | % function matrix, iteration results, and policy function, value function
|
| | 28 | % and iteration results tables.
|
| | 29 | %
|
| | 30 | % keys included in result_map:
|
| | 31 | %
|
| | 32 | % * mt_val matrix states_n by shock_n matrix of converged value function grid
|
| | 33 | % * mt_pol_a matrix states_n by shock_n matrix of converged policy function grid
|
| | 34 | % * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
|
| | 35 | % difference between iteration
|
| | 36 | % * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
|
| | 37 | % function difference between iterations
|
| | 38 | % * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
|
| | 39 | % proportion of grid points at which policy function changed between
|
| | 40 | % current and last iteration for each element of shock
|
| | 41 | %
|
| | 42 | % @example
|
| | 43 | %
|
| | 44 | % @include
|
| | 45 | %
|
| | 46 | % * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_ipwkz/paramfunc/ff_ipwkz_evf.m ff_ipwkz_evf>
|
| | 47 | % * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_ipwkz/paramfunc/ffs_ipwkz_set_default_param.m ffs_ipwkz_set_default_param>
|
| | 48 | % * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_ipwkz/paramfunc/ffs_ipwkz_get_funcgrid.m ffs_ipwkz_get_funcgrid>
|
| | 49 | % * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_akz/solvepost/ff_akz_vf_post.m ff_akz_vf_post>
|
| | 50 | %
|
| | 51 |
|
| | 52 | %% Default
|
| | 53 | % * it_param_set = 1: quick test
|
| | 54 | % * it_param_set = 2: benchmark run
|
| | 55 | % * it_param_set = 3: benchmark profile
|
| | 56 | % * it_param_set = 4: press publish button
|
| | 57 |
|
| | 58 | it_param_set = 3;
|
| | 59 | bl_input_override = true;
|
| | 60 | [param_map, support_map] = ffs_ipwkz_set_default_param(it_param_set);
|
| | 61 |
|
| | 62 | % parameters can be set inside ffs_ipwkz_set_default_param or updated here
|
| | 63 | % param_map('it_w_perc_n') = 50;
|
| | 64 | % param_map('it_ak_perc_n') = param_map('it_w_perc_n');
|
| | 65 | % param_map('it_z_n') = 15;
|
| | 66 | % param_map('fl_coh_interp_grid_gap') = 0.025;
|
| | 67 | % param_map('it_c_interp_grid_gap') = 0.001;
|
| | 68 | % param_map('fl_w_interp_grid_gap') = 0.25;
|
| | 69 | % param_map('it_w_perc_n') = 100;
|
| | 70 | % param_map('it_ak_perc_n') = param_map('it_w_perc_n');
|
| | 71 | % param_map('it_z_n') = 11;
|
| | 72 | % param_map('fl_coh_interp_grid_gap') = 0.1;
|
| | 73 | % param_map('it_c_interp_grid_gap') = 10^-4;
|
| | 74 | % param_map('fl_w_interp_grid_gap') = 0.1;
|
| | 75 |
|
| | 76 | % get armt and func map
|
| | 77 | [armt_map, func_map] = ffs_ipwkz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
|
| | 78 | default_params = {param_map support_map armt_map func_map};
|
| | 79 |
|
| | 80 | %% Parse Parameters 1
|
| | 81 |
|
| | 82 | % if varargin only has param_map and support_map,
|
| | 83 | params_len = length(varargin);
|
| | 84 | [default_params{1:params_len}] = varargin{:};
|
| | 85 | param_map = [param_map; default_params{1}];
|
| | 86 | support_map = [support_map; default_params{2}];
|
| | 87 | if params_len >= 1 && params_len <= 2
|
| | 88 | % If override param_map, re-generate armt and func if they are not
|
| | 89 | % provided
|
| | 90 | bl_input_override = true;
|
| | 91 | [armt_map, func_map] = ffs_ipwkz_get_funcgrid(param_map, support_map, bl_input_override);
|
| | 92 | else
|
| | 93 | % Override all
|
| | 94 | armt_map = [armt_map; default_params{3}];
|
| | 95 | func_map = [func_map; default_params{4}];
|
| | 96 | end
|
| | 97 |
|
| | 98 | % append function name
|
| | 99 | st_func_name = 'ff_ipwkz_vf_vecsv';
|
| | 100 | support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
|
| | 101 | support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
|
| | 102 | support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
|
| | 103 |
|
| | 104 | %% Parse Parameters 2
|
| | 105 |
|
| | 106 | % armt_map
|
| | 107 | params_group = values(armt_map, {'ar_w_perc', 'ar_w_level', 'ar_z'});
|
| | 108 | [ar_w_perc, ar_w_level, ar_z] = params_group{:};
|
| | 109 | params_group = values(armt_map, {'ar_interp_c_grid', 'ar_interp_coh_grid', ...
|
| | 110 | 'mt_interp_coh_grid_mesh_z', 'mt_z_mesh_coh_interp_grid',...
|
| | 111 | 'mt_interp_coh_grid_mesh_w_perc', ...
|
| | 112 | 'mt_w_by_interp_coh_interp_grid'});
|
| | 113 | [ar_interp_c_grid, ar_interp_coh_grid, ...
|
| | 114 | mt_interp_coh_grid_mesh_z, mt_z_mesh_coh_interp_grid, ...
|
| | 115 | mt_interp_coh_grid_mesh_w_perc,...
|
| | 116 | mt_w_by_interp_coh_interp_grid] = params_group{:};
|
| | 117 | params_group = values(armt_map, {'mt_coh_wkb', 'mt_z_mesh_coh_wkb'});
|
| | 118 | [mt_coh_wkb, mt_z_mesh_coh_wkb] = params_group{:};
|
| | 119 |
|
| | 120 | % func_map
|
| | 121 | params_group = values(func_map, {'f_util_log', 'f_util_crra', 'f_cons'});
|
| | 122 | [f_util_log, f_util_crra, f_cons] = params_group{:};
|
| | 123 |
|
| | 124 | % param_map
|
| | 125 | params_group = values(param_map, {'it_z_n', 'fl_crra', 'fl_beta', 'fl_c_min'});
|
| | 126 | [it_z_n, fl_crra, fl_beta, fl_c_min] = params_group{:};
|
| | 127 | params_group = values(param_map, {'it_maxiter_val', 'fl_tol_val', 'fl_tol_pol', 'it_tol_pol_nochange'});
|
| | 128 | [it_maxiter_val, fl_tol_val, fl_tol_pol, it_tol_pol_nochange] = params_group{:};
|
| | 129 |
|
| | 130 | % support_map
|
| | 131 | params_group = values(support_map, {'bl_profile', 'st_profile_path', ...
|
| | 132 | 'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
|
| | 133 | 'bl_time', 'bl_display_defparam', 'bl_graph_evf', 'bl_display', 'it_display_every', 'bl_post'});
|
| | 134 | [bl_profile, st_profile_path, ...
|
| | 135 | st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
|
| | 136 | bl_time, bl_display_defparam, bl_graph_evf, bl_display, it_display_every, bl_post] = params_group{:};
|
| | 137 | params_group = values(support_map, {'it_display_summmat_rowmax', 'it_display_summmat_colmax'});
|
| | 138 | [it_display_summmat_rowmax, it_display_summmat_colmax] = params_group{:};
|
| | 139 |
|
| | 140 | %% Initialize Output Matrixes
|
| | 141 |
|
| | 142 | mt_val_cur = zeros(length(ar_interp_coh_grid),length(ar_z));
|
| | 143 | mt_val = mt_val_cur - 1;
|
| | 144 | mt_pol_a = zeros(length(ar_interp_coh_grid),length(ar_z));
|
| | 145 | mt_pol_a_cur = mt_pol_a - 1;
|
| | 146 | mt_pol_k = zeros(length(ar_interp_coh_grid),length(ar_z));
|
| | 147 | mt_pol_k_cur = mt_pol_k - 1;
|
| | 148 | mt_pol_idx = zeros(length(ar_interp_coh_grid),length(ar_z));
|
| | 149 |
|
| | 150 | % We did not need these in ff_oz_vf or ff_oz_vf_vec
|
| | 151 | % see
|
| | 152 | % <https://fanwangecon.github.io/M4Econ/support/speed/partupdate/fs_u_c_partrepeat_main.html
|
| | 153 | % fs_u_c_partrepeat_main> for why store using cells.
|
| | 154 | cl_u_c_store = cell([it_z_n, 1]);
|
| | 155 | cl_w_kstar_interp_z = cell([it_z_n, 1]);
|
| | 156 | for it_z_i = 1:length(ar_z)
|
| | 157 | cl_w_kstar_interp_z{it_z_i} = zeros([length(ar_w_perc), length(ar_interp_coh_grid)]) - 1;
|
| | 158 | end
|
| | 159 |
|
| | 160 | %% Initialize Convergence Conditions
|
| | 161 |
|
| | 162 | bl_vfi_continue = true;
|
| | 163 | it_iter = 0;
|
| | 164 | ar_val_diff_norm = zeros([it_maxiter_val, 1]);
|
| | 165 | ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
|
| | 166 | mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
|
| | 167 |
|
| | 168 | %% Pre-calculate u(c)
|
| | 169 | % Interpolation, see
|
| | 170 | % <https://fanwangecon.github.io/M4Econ/support/speed/partupdate/fs_u_c_partrepeat_main.html
|
| | 171 | % fs_u_c_partrepeat_main> for why interpolate over u(c)
|
| | 172 |
|
| | 173 | % Evaluate
|
| | 174 | if (fl_crra == 1)
|
| | 175 | ar_interp_u_of_c_grid = f_util_log(ar_interp_c_grid);
|
| | 176 | fl_u_neg_c = f_util_log(fl_c_min);
|
| | 177 | else
|
| | 178 | ar_interp_u_of_c_grid = f_util_crra(ar_interp_c_grid);
|
| | 179 | fl_u_neg_c = f_util_crra(fl_c_min);
|
| | 180 | end
|
| | 181 | ar_interp_u_of_c_grid(ar_interp_c_grid <= fl_c_min) = fl_u_neg_c;
|
| | 182 |
|
| | 183 | % Get Interpolant
|
| | 184 | f_grid_interpolant_spln = griddedInterpolant(ar_interp_c_grid, ar_interp_u_of_c_grid, 'spline', 'nearest');
|
| | 185 |
|
| | 186 | %% Iterate Value Function
|
| | 187 | % Loop solution with 4 nested loops
|
| | 188 | %
|
| | 189 | % # loop 1: over exogenous states
|
| | 190 | % # loop 2: over endogenous states
|
| | 191 | % # loop 3: over choices
|
| | 192 | % # loop 4: add future utility, integration--loop over future shocks
|
| | 193 | %
|
| | 194 |
|
| | 195 | % Start Profile
|
| | 196 | if (bl_profile)
|
| | 197 | close all;
|
| | 198 | profile off;
|
| | 199 | profile on;
|
< 0.001 | 1 | 200 | end
|
| | 201 |
|
| | 202 | % Start Timer
|
< 0.001 | 1 | 203 | if (bl_time)
|
< 0.001 | 1 | 204 | tic;
|
< 0.001 | 1 | 205 | end
|
| | 206 |
|
| | 207 | % Value Function Iteration
|
< 0.001 | 1 | 208 | while bl_vfi_continue
|
< 0.001 | 105 | 209 | it_iter = it_iter + 1;
|
| | 210 |
|
| | 211 | %% Interpolate (1) reacahble v(coh(k(w,z),b(w,z),z),z) given v(coh, z)
|
| | 212 | % v(coh,z) solved on ar_interp_coh_grid, ar_z grids, see
|
| | 213 | % ffs_ipwkz_get_funcgrid.m. Generate interpolant based on that, Then
|
| | 214 | % interpolate for the coh reachable levels given the k(w,z) percentage
|
| | 215 | % choice grids in the second stage of the problem
|
| | 216 |
|
| | 217 | % Generate Interpolant for v(coh,z)
|
0.025 | 105 | 218 | f_grid_interpolant_value = griddedInterpolant(...
|
| 105 | 219 | mt_z_mesh_coh_interp_grid', mt_interp_coh_grid_mesh_z', mt_val_cur', 'linear', 'nearest');
|
| | 220 |
|
| | 221 | % Interpolate for v(coh(k(w,z),b(w,z),z),z)
|
0.326 | 105 | 222 | mt_val_wkb_interpolated = f_grid_interpolant_value(mt_z_mesh_coh_wkb, mt_coh_wkb);
|
| | 223 |
|
| | 224 | %% Solve Second Stage Problem k*(w,z)
|
| | 225 | % This is the key difference between this function and
|
| | 226 | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_functions.html
|
| | 227 | % ffs_akz_set_functions> which solves the two stages jointly
|
| | 228 | % Interpolation first, because solution coh grid is not the same as all
|
| | 229 | % points reachable by k and b choices given w.
|
| | 230 |
|
0.009 | 105 | 231 | support_map('bl_graph_evf') = false;
|
< 0.001 | 105 | 232 | if (it_iter == (it_maxiter_val + 1))
|
< 0.001 | 1 | 233 | support_map('bl_graph_evf') = bl_graph_evf;
|
< 0.001 | 1 | 234 | end
|
| | 235 |
|
< 0.001 | 105 | 236 | bl_input_override = true;
|
0.220 | 105 | 237 | [mt_ev_condi_z_max, ~, mt_ev_condi_z_max_kp, ~] = ...
|
| 105 | 238 | ff_ipwkz_evf(mt_val_wkb_interpolated, param_map, support_map, armt_map, bl_input_override);
|
| | 239 |
|
| | 240 | %% Solve First Stage Problem w*(z) given k*(w,z)
|
| | 241 |
|
| | 242 | % loop 1: over exogenous states
|
< 0.001 | 105 | 243 | for it_z_i = 1:length(ar_z)
|
| | 244 |
|
| | 245 | %% A. Interpolate FULL to get k*(w_perc, z), b*(k,w) based on k*(w_level, z)
|
| | 246 | % Generate interpolant for (2) k*(ar_w_perc) from k*(ar_w_level,z)
|
| | 247 | % There are two w=k'+b' arrays. ar_w_level is the level even grid based
|
| | 248 | % on which we solve the 2nd stage problem in ff_ipwkz_evf.m. Here for
|
| | 249 | % each coh level, we have a different vector of w levels, but the same
|
| | 250 | % vector of percentage ws. So we need to interpolate to get the optimal
|
| | 251 | % k* and b* choices at each percentage level of w.
|
0.057 | 1575 | 252 | f_interpolante_w_level_kstar_z = griddedInterpolant(ar_w_level, mt_ev_condi_z_max_kp(:, it_z_i)', 'linear', 'nearest');
|
| | 253 |
|
| | 254 | % Interpolate (2), shift from w_level to w_perc
|
0.161 | 1575 | 255 | mt_w_kstar_interp_z = f_interpolante_w_level_kstar_z(mt_w_by_interp_coh_interp_grid);
|
0.019 | 1575 | 256 | mt_w_astar_interp_z = mt_w_by_interp_coh_interp_grid - mt_w_kstar_interp_z;
|
| | 257 |
|
| | 258 | % changes in w_perc kstar choices
|
0.022 | 1575 | 259 | mt_w_kstar_diff_idx = (cl_w_kstar_interp_z{it_z_i} ~= mt_w_kstar_interp_z);
|
| | 260 |
|
| | 261 | %% B. Calculate UPDATE u(c): u(c(coh_level, w_perc)) given k*_interp, b*_interp
|
| | 262 | % Note that compared to
|
| | 263 | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_functions.html
|
| | 264 | % ffs_akz_set_functions> the mt_c here is much smaller the same
|
| | 265 | % number of columns (states) as in the ffs_akz_set_functions file,
|
| | 266 | % but the number of rows equal to ar_w length.
|
0.121 | 1575 | 267 | ar_c = f_cons(mt_interp_coh_grid_mesh_w_perc(mt_w_kstar_diff_idx), ...
|
| 1575 | 268 | mt_w_astar_interp_z(mt_w_kstar_diff_idx), ...
|
| 1575 | 269 | mt_w_kstar_interp_z(mt_w_kstar_diff_idx));
|
| | 270 |
|
| | 271 | % EVAL current utility: N by N, f_util defined earlier
|
0.073 | 1575 | 272 | ar_utility_update = f_grid_interpolant_spln(ar_c);
|
| | 273 |
|
| | 274 | % Update Storage
|
< 0.001 | 1575 | 275 | if (it_iter == 1)
|
< 0.001 | 15 | 276 | cl_u_c_store{it_z_i} = reshape(ar_utility_update, [length(ar_w_perc), length(ar_interp_coh_grid)]);
|
< 0.001 | 1560 | 277 | else
|
0.034 | 1560 | 278 | cl_u_c_store{it_z_i}(mt_w_kstar_diff_idx) = ar_utility_update;
|
< 0.001 | 1575 | 279 | end
|
0.040 | 1575 | 280 | cl_w_kstar_interp_z{it_z_i} = mt_w_kstar_interp_z;
|
| | 281 |
|
| | 282 | %% C. Interpolate FULL EV(k*(coh_level, w_perc, z), w - b*|z) based on EV(k*(w_level, z))
|
| | 283 | % Generate Interpolant for (3) EV(k*(ar_w_perc),Z)
|
0.048 | 1575 | 284 | f_interpolante_ev_condi_z_max_z = griddedInterpolant(ar_w_level, mt_ev_condi_z_max(:, it_z_i)', 'linear', 'nearest');
|
| | 285 | % Interpolate (3), EVAL add on future utility, N by N + N by N
|
0.157 | 1575 | 286 | mt_ev_condi_z_max_interp_z = f_interpolante_ev_condi_z_max_z(mt_w_by_interp_coh_interp_grid);
|
| | 287 |
|
| | 288 | %% D. Compute FULL U(coh_level, w_perc, z) over all w_perc
|
0.025 | 1575 | 289 | mt_utility = cl_u_c_store{it_z_i} + fl_beta*mt_ev_condi_z_max_interp_z;
|
| | 290 |
|
| | 291 | % percentage algorithm does not have invalid (check to make sure
|
| | 292 | % min percent is not 0 in ffs_ipwkz_get_funcgrid.m)
|
| | 293 | % mt_utility = mt_utility.*(~mt_it_c_valid_idx) + fl_u_neg_c*(mt_it_c_valid_idx);
|
| | 294 |
|
| | 295 | %% E. Optimize Over Choices: max_{w_perc} U(coh_level, w_perc, z)
|
| | 296 | % Optimization: remember matlab is column major, rows must be
|
| | 297 | % choices, columns must be states
|
| | 298 | % <https://en.wikipedia.org/wiki/Row-_and_column-major_order COLUMN-MAJOR>
|
0.069 | 1575 | 299 | [ar_opti_val1_z, ar_opti_idx_z] = max(mt_utility);
|
| | 300 |
|
| | 301 | % Generate Linear Opti Index
|
0.005 | 1575 | 302 | [it_choies_n, it_states_n] = size(mt_utility);
|
0.021 | 1575 | 303 | ar_add_grid = linspace(0, it_choies_n*(it_states_n-1), it_states_n);
|
0.001 | 1575 | 304 | ar_opti_linear_idx_z = ar_opti_idx_z + ar_add_grid;
|
| | 305 |
|
| | 306 | %% F. Store Results
|
0.007 | 1575 | 307 | mt_val(:,it_z_i) = ar_opti_val1_z;
|
0.014 | 1575 | 308 | mt_pol_a(:,it_z_i) = mt_w_astar_interp_z(ar_opti_linear_idx_z);
|
0.009 | 1575 | 309 | mt_pol_k(:,it_z_i) = mt_w_kstar_interp_z(ar_opti_linear_idx_z);
|
< 0.001 | 1575 | 310 | if (it_iter == (it_maxiter_val + 1))
|
< 0.001 | 15 | 311 | mt_pol_idx(:,it_z_i) = ar_opti_linear_idx_z;
|
< 0.001 | 15 | 312 | end
|
| | 313 |
|
0.001 | 1575 | 314 | end
|
| | 315 |
|
| | 316 | %% Check Tolerance and Continuation
|
| | 317 |
|
| | 318 | % Difference across iterations
|
0.027 | 105 | 319 | ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur);
|
0.042 | 105 | 320 | ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur) + norm(mt_pol_k - mt_pol_k_cur);
|
0.006 | 105 | 321 | ar_pol_a_perc_change = sum((mt_pol_a ~= mt_pol_a_cur))/(length(ar_interp_coh_grid));
|
0.005 | 105 | 322 | ar_pol_k_perc_change = sum((mt_pol_k ~= mt_pol_k_cur))/(length(ar_interp_coh_grid));
|
0.014 | 105 | 323 | mt_pol_perc_change(it_iter, :) = mean([ar_pol_a_perc_change;ar_pol_k_perc_change]);
|
| | 324 |
|
| | 325 | % Update
|
0.002 | 105 | 326 | mt_val_cur = mt_val;
|
0.001 | 105 | 327 | mt_pol_a_cur = mt_pol_a;
|
< 0.001 | 105 | 328 | mt_pol_k_cur = mt_pol_k;
|
| | 329 |
|
| | 330 | % Print Iteration Results
|
< 0.001 | 105 | 331 | if (bl_display && (rem(it_iter, it_display_every)==0))
|
| | 332 | fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
|
| | 333 | it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
|
| | 334 | tb_valpol_iter = array2table([mean(mt_val_cur,1);...
|
| | 335 | mean(mt_pol_a_cur,1); ...
|
| | 336 | mean(mt_pol_k_cur,1); ...
|
| | 337 | mt_val_cur(length(ar_interp_coh_grid),:); ...
|
| | 338 | mt_pol_a_cur(length(ar_interp_coh_grid),:); ...
|
| | 339 | mt_pol_k_cur(length(ar_interp_coh_grid),:)]);
|
| | 340 | tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
|
| | 341 | tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'mak', 'Hval', 'Hap', 'Hak'};
|
| | 342 | disp('mval = mean(mt_val_cur,1), average value over a')
|
| | 343 | disp('map = mean(mt_pol_a_cur,1), average choice over a')
|
| | 344 | disp('mkp = mean(mt_pol_k_cur,1), average choice over k')
|
| | 345 | disp('Hval = mt_val_cur(it_ameshk_n,:), highest a state val')
|
| | 346 | disp('Hap = mt_pol_a_cur(it_ameshk_n,:), highest a state choice')
|
| | 347 | disp('mak = mt_pol_k_cur(it_ameshk_n,:), highest k state choice')
|
| | 348 | disp(tb_valpol_iter);
|
| | 349 | end
|
| | 350 |
|
| | 351 | % Continuation Conditions:
|
| | 352 | % 1. if value function convergence criteria reached
|
| | 353 | % 2. if policy function variation over iterations is less than
|
| | 354 | % threshold
|
< 0.001 | 105 | 355 | if (it_iter == (it_maxiter_val + 1))
|
< 0.001 | 1 | 356 | bl_vfi_continue = false;
|
0.001 | 104 | 357 | elseif ((it_iter == it_maxiter_val) || ...
|
| 104 | 358 | (ar_val_diff_norm(it_iter) < fl_tol_val) || ...
|
| 104 | 359 | (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol))
|
| | 360 | % Fix to max, run again to save results if needed
|
< 0.001 | 1 | 361 | it_iter_last = it_iter;
|
< 0.001 | 1 | 362 | it_iter = it_maxiter_val;
|
< 0.001 | 1 | 363 | end
|
| | 364 |
|
0.001 | 105 | 365 | end
|
| | 366 |
|
| | 367 | % End Timer
|
< 0.001 | 1 | 368 | if (bl_time)
|
< 0.001 | 1 | 369 | toc;
|
< 0.001 | 1 | 370 | end
|
| | 371 |
|
| | 372 | % End Profile
|
< 0.001 | 1 | 373 | if (bl_profile)
|
0.001 | 1 | 374 | profile off
|
| | 375 | profile viewer
|
| | 376 | st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
|
| | 377 | profsave(profile('info'), strcat(st_profile_path, st_file_name));
|
| | 378 | end
|
| | 379 |
|
| | 380 | %% Process Optimal Choices
|
| | 381 |
|
| | 382 | result_map = containers.Map('KeyType','char', 'ValueType','any');
|
| | 383 | result_map('mt_val') = mt_val;
|
| | 384 | result_map('mt_pol_idx') = mt_pol_idx;
|
| | 385 |
|
| | 386 | result_map('cl_mt_coh') = {mt_interp_coh_grid_mesh_z, zeros(1)};
|
| | 387 | result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
|
| | 388 | result_map('cl_mt_pol_k') = {mt_pol_k, zeros(1)};
|
| | 389 | result_map('cl_mt_pol_c') = {f_cons(mt_interp_coh_grid_mesh_z, mt_pol_a, mt_pol_k), zeros(1)};
|
| | 390 | result_map('ar_st_pol_names') = ["cl_mt_coh", "cl_mt_pol_a", "cl_mt_pol_k", "cl_mt_pol_c"];
|
| | 391 |
|
| | 392 | if (bl_post)
|
| | 393 | bl_input_override = true;
|
| | 394 | result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
|
| | 395 | result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
|
| | 396 | result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
|
| | 397 |
|
| | 398 | % graphing based on coh_wkb, but that does not match optimal choice
|
| | 399 | % matrixes for graphs.
|
| | 400 | armt_map('mt_coh_wkb') = mt_interp_coh_grid_mesh_z;
|
| | 401 | armt_map('it_ameshk_n') = length(ar_interp_coh_grid);
|
| | 402 | armt_map('ar_a_meshk') = mt_interp_coh_grid_mesh_z(:,1);
|
| | 403 | armt_map('ar_k_mesha') = zeros(size(mt_interp_coh_grid_mesh_z(:,1)) + 0);
|
| | 404 |
|
| | 405 | result_map = ff_akz_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
|
| | 406 | end
|
| | 407 |
|
| | 408 | %% Display Various Containers
|
| | 409 |
|
| | 410 | if (bl_display_defparam)
|
| | 411 |
|
| | 412 | %% Display 1 support_map
|
| | 413 | fft_container_map_display(support_map, it_display_summmat_rowmax, it_display_summmat_colmax);
|
| | 414 |
|
| | 415 | %% Display 2 armt_map
|
| | 416 | fft_container_map_display(armt_map, it_display_summmat_rowmax, it_display_summmat_colmax);
|
| | 417 |
|
| | 418 | %% Display 3 param_map
|
| | 419 | fft_container_map_display(param_map, it_display_summmat_rowmax, it_display_summmat_colmax);
|
| | 420 |
|
| | 421 | %% Display 4 func_map
|
| | 422 | fft_container_map_display(func_map, it_display_summmat_rowmax, it_display_summmat_colmax);
|
| | 423 |
|
| | 424 | %% Display 5 result_map
|
| | 425 | fft_container_map_display(result_map, it_display_summmat_rowmax, it_display_summmat_colmax);
|
| | 426 |
|
| | 427 | end
|
| | 428 |
|
| | 429 | end
|
Other subfunctions in this file are not included in this listing.