This is a static copy of a profile report

Home

Function details for ff_abzr_fibs_vfThis is a static copy of a profile report

Home

ff_abzr_fibs_vf (Calls: 1, Time: 9.357 s)
Generated 14-Jul-2019 14:56:01 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_fibs\m_abzr_solve\ff_abzr_fibs_vf.m
Copy to new window for comparing multiple runs

Parents (calling functions)
No parent
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
281
fl_aprime_nobridge, bl_display...
661506.257 s66.9%
314
fl_beta*mt_z_trans(it_z_i, it_...
9903600.943 s10.1%
295
[fl_b_bridge, fl_inf_borr_nobr...
1653750.685 s7.3%
290
fl_c = f_cons_coh_save(fl_coh,...
1653750.309 s3.3%
341
ar_val_cur(it_ap_k) = f_util_c...
1214850.245 s2.6%
All other lines  0.918 s9.8%
Totals  9.357 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ffs_fibs_min_c_costfunction661506.113 s65.3%
dealfunction1653750.279 s3.0%
ffs_fibs_inf_bridgefunction170100.175 s1.9%
..._save)(coh-ar_for_save./(1+fl_r_fsv))anonymous function1653750.149 s1.6%
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function1214860.125 s1.3%
...h,ar_bprime_in_c)(coh+ar_bprime_in_c)anonymous function661500.088 s0.9%
...tions>@(ar_z,ar_b)(ar_z*fl_w+ar_b)anonymous function66150.009 s0.1%
Self time (built-ins, overhead, etc.)  2.418 s25.8%
Totals  9.357 s100% 
Code Analyzer results
Line numberMessage
147The value assigned here to 'bl_display_defparam' appears to be unused. Consider replacing it by ~.
Coverage results
Show coverage for parent directory
Total lines in function524
Non-code lines (comments, blank lines)305
Code lines (lines that can run)219
Code lines that did run116
Code lines that did not run103
Coverage (did run/can run)52.97 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_abzr_fibs_vf(varargin)
   8 
%% FF_ABZ_FIBS_VF borr + save one asset formal informal + loop
   9 
% This program solves the infinite horizon dynamic single asset and single
  10 
% shock problem with loops. This file contains codes that processes
  11 
% borrowing and handles formal and informal choices. With R Shock.
  12 
%
  13 
% @param param_map container parameter container
  14 
%
  15 
% @param support_map container support container
  16 
%
  17 
% @param armt_map container container with states, choices and shocks
  18 
% grids that are inputs for grid based solution algorithm
  19 
%
  20 
% @param func_map container container with function handles for
  21 
% consumption cash-on-hand etc.
  22 
%
  23 
% @return result_map container contains policy function matrix, value
  24 
% function matrix, iteration results, and policy function, value function
  25 
% and iteration results tables.
  26 
%
  27 
% keys included in result_map:
  28 
%
  29 
% * mt_val matrix states_n by shock_n matrix of converged value function grid
  30 
% * mt_pol_a matrix states_n by shock_n matrix of converged policy function grid
  31 
% * mt_cons matrix states_n by shock_n matrix of optimal consumption
  32 
% levels, unlike modele without formal and informal choices, where we know
  33 
% c from coh and a, here this needed to be stored because it is the results
  34 
% from with joint category maximization problem.
  35 
% * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
  36 
% difference between iteration
  37 
% * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
  38 
% function difference between iterations
  39 
% * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
  40 
% proportion of grid points at which policy function changed between
  41 
% current and last iteration for each element of shock
  42 
%
  43 
% @example
  44 
%
  45 
% @include
  46 
%
  47 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abzr_paramfunc/html/ffs_abzr_fibs_set_default_param.html ffs_abzr_fibs_set_default_param>
  48 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abzr_paramfunc/html/ffs_abzr_fibs_get_funcgrid.html ffs_abzr_fibs_get_funcgrid>
  49 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_min_c_cost_bridge.html ffs_fibs_min_c_cost_bridge>
  50 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_inf_bridge.html ffs_fibs_inf_bridge>
  51 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_min_c_cost.html ffs_fibs_min_c_cost>
  52 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_vf_post.html ff_az_vf_post>
  53 
%
  54 
% @seealso
  55 
%
  56 
% * for/inf + save + borr loop: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abzr_solve/html/ff_abzr_fibs_vf.html ff_abzr_fibs_vf>
  57 
% * for/inf + borr vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abzr_solve/html/ff_abzr_fibs_vf_vec.html ff_abzr_fibs_vf_vec>
  58 
% * for/inf + borr optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abzr_solve/html/ff_abzr_fibs_vf_vecsv.html ff_abzr_fibs_vf_vecsv>
  59 
%
  60 

  61 
%% Default
  62 
% * it_param_set = 1: quick test
  63 
% * it_param_set = 2: benchmark run
  64 
% * it_param_set = 3: benchmark profile
  65 
% * it_param_set = 4: press publish button
  66 

  67 
it_param_set = 3;
  68 
[param_map, support_map] = ffs_abzr_fibs_set_default_param(it_param_set);
  69 

  70 
% Note: param_map and support_map can be adjusted here or outside to override defaults
  71 
% To generate results as if formal informal do not matter
  72 
param_map('it_a_n') = 35;
  73 
param_map('fl_z_r_infbr_n') = 3;
  74 
param_map('it_z_wage_n') = 3;
  75 
param_map('it_z_n') = param_map('it_z_wage_n') * param_map('fl_z_r_infbr_n');
  76 
param_map('it_maxiter_val') = 20;
  77 
% param_map('fl_r_fsv') = 0.025;
  78 
% param_map('fl_r_fbr') = 0.035;
  79 
% param_map('bl_b_is_principle') = false;
  80 
% param_map('st_forbrblk_type') = 'seg3';
  81 
% param_map('fl_forbrblk_brmost') = -19;
  82 
% param_map('fl_forbrblk_brleast') = -1;
  83 
% param_map('fl_forbrblk_gap') = -1.5;
  84 
% param_map('bl_b_is_principle') = false;
  85 

  86 
[armt_map, func_map] = ffs_abzr_fibs_get_funcgrid(param_map, support_map); % 1 for override
  87 
default_params = {param_map support_map armt_map func_map};
  88 

  89 
%% Parse Parameters 1
  90 

  91 
% if varargin only has param_map and support_map,
  92 
params_len = length(varargin);
  93 
[default_params{1:params_len}] = varargin{:};
  94 
param_map = [param_map; default_params{1}];
  95 
support_map = [support_map; default_params{2}];
  96 
if params_len >= 1 && params_len <= 2
  97 
    % If override param_map, re-generate armt and func if they are not
  98 
    % provided
  99 
    bl_input_override = true;
 100 
    [armt_map, func_map] = ffs_abzr_fibs_get_funcgrid(param_map, support_map, bl_input_override);
 101 
else
 102 
    % Override all
 103 
    armt_map = [armt_map; default_params{3}];
 104 
    func_map = [func_map; default_params{4}];
 105 
end
 106 

 107 
% append function name
 108 
st_func_name = 'ff_abzr_fibs_vf';
 109 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 110 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 111 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 112 

 113 
%% Parse Parameters 2
 114 

 115 
% armt_map
 116 
params_group = values(armt_map, {'ar_a', 'mt_z_trans', 'ar_z_r_infbr_mesh_wage', 'ar_z_wage_mesh_r_infbr'});
 117 
[ar_a, mt_z_trans, ar_z_r_infbr_mesh_wage, ar_z_wage_mesh_r_infbr] = params_group{:};
 118 

 119 
% Formal choice Menu/Grid and Interest Rate Menu/Grid
 120 
params_group = values(armt_map, {'ar_forbrblk_r', 'ar_forbrblk'});
 121 
[ar_forbrblk_r, ar_forbrblk] = params_group{:};
 122 

 123 
% func_map
 124 
params_group = values(func_map, {'f_util_log', 'f_util_crra', 'f_coh', 'f_cons_coh_fbis', 'f_cons_coh_save'});
 125 
[f_util_log, f_util_crra, f_coh, f_cons_coh_fbis, f_cons_coh_save] = params_group{:};
 126 

 127 
% param_map
 128 
params_group = values(param_map, {'it_a_n', 'it_z_n', 'fl_crra', 'fl_beta', 'fl_c_min',...
 129 
    'fl_nan_replace', 'bl_default', 'bl_bridge', 'bl_rollover', 'fl_default_aprime'});
 130 
[it_a_n, it_z_n, fl_crra, fl_beta, fl_c_min, ...
 131 
    fl_nan_replace, bl_default, bl_bridge, bl_rollover, fl_default_aprime] = params_group{:};
 132 
params_group = values(param_map, {'it_maxiter_val', 'fl_tol_val', 'fl_tol_pol', 'it_tol_pol_nochange'});
 133 
[it_maxiter_val, fl_tol_val, fl_tol_pol, it_tol_pol_nochange] = params_group{:};
 134 

 135 
% param_map, Formal informal
 136 
params_group = values(param_map, {'fl_r_fsv', 'bl_b_is_principle'});
 137 
[fl_r_fsv, bl_b_is_principle] = params_group{:};
 138 

 139 
% support_map
 140 
params_group = values(support_map, {'bl_profile', 'st_profile_path', ...
 141 
    'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
 142 
    'bl_display_minccost', 'bl_display_infbridge', ...
 143 
    'bl_time', 'bl_display_defparam', 'bl_display', 'it_display_every', 'bl_post'});
 144 
[bl_profile, st_profile_path, ...
 145 
    st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
 146 
    bl_display_minccost, bl_display_infbridge, ...
 147 
    bl_time, bl_display_defparam, bl_display, it_display_every, bl_post] = params_group{:};
 148 

 149 
%% Initialize Output Matrixes
 150 

 151 
mt_val_cur = zeros(it_a_n,it_z_n);
 152 
mt_val = mt_val_cur - 1;
 153 
mt_pol_a = zeros(it_a_n,it_z_n);
 154 
mt_pol_a_cur = mt_pol_a - 1;
 155 
mt_pol_cons = zeros(it_a_n,it_z_n);
 156 

 157 
% collect optimal borrowing formal and informal choices
 158 
mt_pol_b_bridge = zeros(it_a_n,it_z_n);
 159 
mt_pol_inf_borr_nobridge = zeros(it_a_n,it_z_n);
 160 
mt_pol_for_borr = zeros(it_a_n,it_z_n);
 161 
mt_pol_for_save = zeros(it_a_n,it_z_n);
 162 

 163 
%% Initialize Convergence Conditions
 164 

 165 
bl_vfi_continue = true;
 166 
it_iter = 0;
 167 
ar_val_diff_norm = zeros([it_maxiter_val, 1]);
 168 
ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
 169 
mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
 170 

 171 
%% Iterate Value Function
 172 
% Loop solution with 4 nested loops
 173 
%
 174 
% # loop 1: over exogenous states
 175 
% # loop 2: over endogenous states
 176 
% # loop 3: over choices
 177 
% # loop 4: add future utility, integration--loop over future shocks
 178 
%
 179 

 180 
% Start Profile
 181 
if (bl_profile)
 182 
    close all;
 183 
    profile off;
 184 
    profile on;
< 0.001 
      1 
 185
end 
 186 

 187 
% Start Timer
< 0.001 
      1 
 188
if (bl_time) 
< 0.001 
      1 
 189
    tic; 
< 0.001 
      1 
 190
end 
 191 

 192 
% Utility at-Default/at-limiting-case-when-nodefault
< 0.001 
      1 
 193
if (fl_crra == 1) 
 194 
    fl_u_cmin = f_util_log(fl_c_min);
< 0.001 
      1 
 195
else 
< 0.001 
      1 
 196
    fl_u_cmin = f_util_crra(fl_c_min); 
< 0.001 
      1 
 197
end 
 198 

 199 

 200 
% Value Function Iteration
< 0.001 
      1 
 201
while bl_vfi_continue 
< 0.001 
     21 
 202
    it_iter = it_iter + 1; 
 203 

 204 
    %% Iterate over a and z states
 205 
    % handling borrowing and default possibility
 206 

 207 
    % loop 1: over exogenous states
< 0.001 
     21 
 208
    for it_z_i = 1:it_z_n 
 209 
        
< 0.001 
    189 
 210
        fl_z_r_borr = ar_z_r_infbr_mesh_wage(it_z_i); 
< 0.001 
    189 
 211
        fl_z_wage = ar_z_wage_mesh_r_infbr(it_z_i); 
 212 

 213 
        % loop 2: over endogenous states
< 0.001 
    189 
 214
        for it_a_j = 1:it_a_n 
 215 

 216 
            % Get asset state
< 0.001 
   6615 
 217
            fl_a = ar_a(it_a_j); 
 218 

 219 
            % Initialize storage
  0.009 
   6615 
 220
            ar_val_cur = zeros(size(ar_a)); 
  0.004 
   6615 
 221
            ar_c_cur = zeros(size(ar_a)); 
  0.004 
   6615 
 222
            ar_b_bridge = zeros(size(ar_a)); 
  0.004 
   6615 
 223
            ar_inf_borr_nobridge = zeros(size(ar_a)); 
  0.004 
   6615 
 224
            ar_for_borr = zeros(size(ar_a)); 
  0.004 
   6615 
 225
            ar_for_save = zeros(size(ar_a)); 
 226 

 227 
            % calculate cash on hand
  0.021 
   6615 
 228
            fl_coh = f_coh(fl_z_wage, fl_a); 
 229 

 230 
            % loop 3: over choices
< 0.001 
   6615 
 231
            for it_ap_k = 1:it_a_n 
 232 

 233 
                % get next period asset choice
  0.010 
 231525 
 234
                fl_ap = ar_a(it_ap_k); 
 235 

 236 
                %% Compute Consumption given Borrowing and Savings
 237 
                % find the today's consumption maximizing formal and informal
 238 
                % choices given a' and coh. The formal and informal choices need to
 239 
                % generate exactly a', but depending on which formal and informal
 240 
                % joint choice is used, the consumption cost today a' is different.
 241 
                % Note here, a is principle + interests. Three areas:
 242 
                %
 243 
                % * *CASE A* a' > 0: savings, do not need to optimize over formal and
 244 
                % informal choices
 245 
                % * *CASE B* a' < 0 & coh < 0: need bridge loan to pay for unpaid debt, and
 246 
                % borrowing over-all, need to first pick bridge loan to pay for
 247 
                % debt, if bridge loan is insufficient, go into default. After
 248 
                % bridge loan, optimize over formal+informal, borrow+save joint
 249 
                % choices.
 250 
                % * *CASE C* $ a' < 0 & coh > 0: do not need to get informal bridge loans,
 251 
                % optimize over for+inf save, for+save+borr, inf+borr only, for
 252 
                % borrow only.
 253 
                %
 254 

  0.011 
 231525 
 255
                if (fl_ap < 0) 
 256 

 257 
                    % Calculate Bridge Loan Borrowing
  0.004 
  66150 
 258
                    if (bl_bridge && fl_coh < 0) 
 259 

< 0.001 
  17010 
 260
                        bl_input_override = true; 
  0.212 
  17010 
 261
                        [fl_aprime_nobridge, fl_b_bridge, fl_c_bridge] = ffs_fibs_inf_bridge(... 
  17010 
 262
                            bl_b_is_principle, fl_z_r_borr, fl_ap, fl_coh, ... 
  17010 
 263
                            bl_display_infbridge, bl_input_override); 
 264 

  0.002 
  49140 
 265
                    else 
 266 

  0.002 
  49140 
 267
                        fl_aprime_nobridge = fl_ap; 
  0.002 
  49140 
 268
                        fl_b_bridge = 0; 
  0.002 
  49140 
 269
                        fl_c_bridge = 0; 
 270 

  0.003 
  66150 
 271
                    end 
 272 

 273 
                    % Find Optimal Formal Informal Borrow Save Combo
 274 
                    % calculate consumption gain from formal + informal
 275 
                    % borrowing and savings choices.
  0.003 
  66150 
 276
                    bl_input_override = true; 
  6.266 
  66150 
 277
                    [fl_max_c_nobridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = ... 
 278 
                        ffs_fibs_min_c_cost(...
  66150 
 279
                        bl_b_is_principle, fl_z_r_borr, fl_r_fsv, ... 
  66150 
 280
                        ar_forbrblk_r, ar_forbrblk, ... 
  66150 
 281
                        fl_aprime_nobridge, bl_display_minccost, bl_input_override); 
 282 

 283 
                    % Compute Consumption given Formal and Informal joint
 284 
                    % consumption with formal borrow menu + bridge loans.
  0.172 
  66150 
 285
                    fl_c = f_cons_coh_fbis(fl_coh, fl_max_c_nobridge + fl_c_bridge); 
 286 

  0.006 
 165375 
 287
                else 
 288 

 289 
                    % consumption with savings
  0.309 
 165375 
 290
                    fl_c = f_cons_coh_save(fl_coh, fl_ap); 
 291 

 292 
                    % assign values for formal and informal choices
 293 
                    % possible that fl_coh < 0, but if then fl_ap > 0 is
 294 
                    % not valid choice
  0.685 
 165375 
 295
                    [fl_b_bridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = deal(0, 0, 0, fl_ap); 
 296 

  0.011 
 231525 
 297
                end 
 298 

 299 
                %% Compute Utility With Default
 300 
                % if rollover is not allowed and bridge is not allowed,
 301 
                % then as long as coh <= 0, also treat as not allowed
 302 
                % states.
 303 
                % assign u(c)
  0.017 
 231525 
 304
                if (fl_c <= fl_c_min || ... 
 121485 
 305
                    ( ~bl_rollover && ~bl_bridge && fl_coh < fl_c_min)) 
 306 

  0.006 
 110040 
 307
                    if (bl_default) 
 308 
                        % defaults
 309 
                        % current utility: only today u(cmin)
  0.005 
 110040 
 310
                        ar_val_cur(it_ap_k) = fl_u_cmin; 
 311 
                        % transition out next period, debt wiped out
  0.005 
 110040 
 312
                        for it_az_q = 1:it_z_n 
  0.981 
 990360 
 313
                            ar_val_cur(it_ap_k) = ar_val_cur(it_ap_k) + ... 
 990360 
 314
                                fl_beta*mt_z_trans(it_z_i, it_az_q)*mt_val_cur((ar_a == fl_default_aprime), it_az_q); 
  0.040 
 990360 
 315
                        end 
 316 

 317 
                        % Replace Consumption if default cmin
  0.005 
 110040 
 318
                        fl_c = fl_c_min; 
 319 
                    else
 320 
                        % if default is not allowed: v = fl_nan_replace
 321 
                        ar_val_cur(it_ap_k) = fl_nan_replace;
 322 

 323 
                        % Replace Consumption if no default nan
 324 
                        fl_c = 0;
  0.005 
 110040 
 325
                    end 
 326 

 327 
                    % no action, defaulting
  0.005 
 110040 
 328
                    fl_b_bridge = 0; 
  0.005 
 110040 
 329
                    fl_inf_borr_nobridge = 0; 
  0.005 
 110040 
 330
                    fl_for_borr = 0; 
  0.005 
 110040 
 331
                    fl_for_save = 0; 
 332 

  0.005 
 121485 
 333
                else 
 334 
                    % Solve Optimization Problem: max_{a'} u(c(a,a',z)) + beta*EV(a',z')
 335 
                    % borrowed enough to pay debt (and borrowing limit not exceeded)
 336 
                    % saved only the coh available.
 337 
                    % current utility
  0.006 
 121485 
 338
                    if (fl_crra == 1) 
 339 
                        ar_val_cur(it_ap_k) = f_util_log(fl_c);
  0.005 
 121485 
 340
                    else 
  0.245 
 121485 
 341
                        ar_val_cur(it_ap_k) = f_util_crra(fl_c); 
  0.004 
 121485 
 342
                    end 
 343 
                    % loop 4: add future utility, integration--loop over future shocks
  0.006 
 121485 
 344
                    for it_az_q = 1:it_z_n 
  0.081 
1093365 
 345
                        ar_val_cur(it_ap_k) = ar_val_cur(it_ap_k) + ... 
1093365 
 346
                            fl_beta*mt_z_trans(it_z_i, it_az_q)*mt_val_cur(it_ap_k, it_az_q); 
  0.042 
1093365 
 347
                    end 
  0.009 
 231525 
 348
                end 
 349 

 350 
                %% Store Values
 351 

 352 
                % Could get the formal and informal values from
 353 
                % ffs_fibs_min_c_cost_bridge.m
 354 
%                 bl_input_override = true;
 355 
%                 [fl_c, fl_b_bridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = ...
 356 
%                     ffs_fibs_min_c_cost_bridge(fl_ap, fl_coh, ...
 357 
%                     param_map, support_map, armt_map, func_map, bl_input_override);
 358 

 359 
                % Store consumption
  0.010 
 231525 
 360
                ar_c_cur(it_ap_k) = fl_c; 
 361 

 362 
                % Save/Update Borrowing Information
  0.011 
 231525 
 363
                ar_b_bridge(it_ap_k) = fl_b_bridge; 
  0.010 
 231525 
 364
                ar_inf_borr_nobridge(it_ap_k) = fl_inf_borr_nobridge; 
  0.012 
 231525 
 365
                ar_for_borr(it_ap_k) = fl_for_borr; 
  0.010 
 231525 
 366
                ar_for_save(it_ap_k) = fl_for_save; 
 367 

  0.011 
 231525 
 368
            end 
 369 

 370 
            %% Optimize over Next Period Asset Choices
 371 
            % optimal choice value
  0.023 
   6615 
 372
            [fl_opti_val_z, fl_opti_idx_z] = max(ar_val_cur); 
< 0.001 
   6615 
 373
            fl_opti_aprime_z = ar_a(fl_opti_idx_z); 
< 0.001 
   6615 
 374
            fl_opti_c_z = ar_c_cur(fl_opti_idx_z); 
 375 

 376 
            % corresponding optimal borrowing and savings choices
< 0.001 
   6615 
 377
            fl_opti_b_bridge = ar_b_bridge(fl_opti_idx_z); 
< 0.001 
   6615 
 378
            fl_opti_inf_borr_nobridge = ar_inf_borr_nobridge(fl_opti_idx_z); 
< 0.001 
   6615 
 379
            fl_opti_for_borr = ar_for_borr(fl_opti_idx_z); 
< 0.001 
   6615 
 380
            fl_opti_for_save = ar_for_save(fl_opti_idx_z); 
 381 

 382 
            %% Find Optimal Choices for Defaults or Not
 383 
            % Handle Default is optimal or not
< 0.001 
   6615 
 384
            if (fl_opti_c_z <= fl_c_min) 
< 0.001 
    120 
 385
                if (bl_default) 
 386 
                    % if defaulting is optimal choice, at these states, not required
 387 
                    % to default, non-default possible, but default could be optimal
< 0.001 
    120 
 388
                    fl_opti_aprime_z = fl_default_aprime; 
 389 
                else
 390 
                    % if default is not allowed, then next period same state as now
 391 
                    % this is absorbing state, this is the limiting case, single
 392 
                    % state space point, lowest a and lowest shock has this.
 393 
                    fl_opti_aprime_z = min(ar_a);
< 0.001 
    120 
 394
                end 
< 0.001 
    120 
 395
            end 
 396 

 397 
            %% Store Optimal Choices and Value Given(a,z)
 398 

 399 
            % store overal savings, value and consumption
  0.001 
   6615 
 400
            mt_val(it_a_j,it_z_i) = fl_opti_val_z; 
  0.001 
   6615 
 401
            mt_pol_a(it_a_j,it_z_i) = fl_opti_aprime_z; 
  0.001 
   6615 
 402
            mt_pol_cons(it_a_j,it_z_i) = fl_opti_c_z; 
 403 

 404 
            % store savings and borrowing formal and inf optimal choices
< 0.001 
   6615 
 405
            mt_pol_b_bridge(it_a_j,it_z_i) = fl_opti_b_bridge; 
< 0.001 
   6615 
 406
            mt_pol_inf_borr_nobridge(it_a_j,it_z_i) = fl_opti_inf_borr_nobridge; 
  0.001 
   6615 
 407
            mt_pol_for_borr(it_a_j,it_z_i) = fl_opti_for_borr; 
< 0.001 
   6615 
 408
            mt_pol_for_save(it_a_j,it_z_i) = fl_opti_for_save; 
 409 

  0.002 
   6615 
 410
        end 
  0.001 
    189 
 411
    end 
 412 

 413 
    %% Check Tolerance and Continuation
 414 

 415 
    % Difference across iterations
  0.002 
     21 
 416
    ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur); 
  0.002 
     21 
 417
    ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur); 
< 0.001 
     21 
 418
    mt_pol_perc_change(it_iter, :) = sum((mt_pol_a ~= mt_pol_a_cur))/(it_a_n); 
 419 

 420 
    % Update
< 0.001 
     21 
 421
    mt_val_cur = mt_val; 
< 0.001 
     21 
 422
    mt_pol_a_cur = mt_pol_a; 
 423 

 424 
    % Print Iteration Results
< 0.001 
     21 
 425
    if (bl_display && (rem(it_iter, it_display_every)==0)) 
 426 
        fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
 427 
            it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
 428 
        tb_valpol_iter = array2table([mean(mt_val_cur,1); mean(mt_pol_a_cur,1); ...
 429 
            mt_val_cur(it_a_n,:); mt_pol_a_cur(it_a_n,:)]);
 430 
        tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
 431 
        tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'Hval', 'Hap'};
 432 
        disp('mval = mean(mt_val_cur,1), average value over a')
 433 
        disp('map  = mean(mt_pol_a_cur,1), average choice over a')
 434 
        disp('Hval = mt_val_cur(it_a_n,:), highest a state val')
 435 
        disp('Hap = mt_pol_a_cur(it_a_n,:), highest a state choice')
 436 
        disp(tb_valpol_iter);
 437 
    end
 438 

 439 
    % Continuation Conditions:
 440 
    % 1. if value function convergence criteria reached
 441 
    % 2. if policy function variation over iterations is less than
 442 
    % threshold
< 0.001 
     21 
 443
    if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
      1 
 444
        bl_vfi_continue = false; 
  0.001 
     20 
 445
    elseif ((it_iter == it_maxiter_val) || ... 
     19 
 446
            (ar_val_diff_norm(it_iter) < fl_tol_val) || ... 
     19 
 447
            (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol)) 
 448 
        % Fix to max, run again to save results if needed
< 0.001 
      1 
 449
        it_iter_last = it_iter; 
< 0.001 
      1 
 450
        it_iter = it_maxiter_val; 
< 0.001 
      1 
 451
    end 
 452 

< 0.001 
     21 
 453
end 
 454 

 455 
% End Timer
< 0.001 
      1 
 456
if (bl_time) 
< 0.001 
      1 
 457
    toc; 
< 0.001 
      1 
 458
end 
 459 

 460 
% End Profile
< 0.001 
      1 
 461
if (bl_profile) 
  0.004 
      1 
 462
    profile off 
 463 
    profile viewer
 464 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 465 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 466 
end
 467 

 468 
%% Process Optimal Choices
 469 

 470 
result_map = containers.Map('KeyType','char', 'ValueType','any');
 471 
result_map('mt_val') = mt_val;
 472 

 473 
result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
 474 
result_map('cl_mt_coh') = {f_coh(ar_z_r_infbr_mesh_wage, ar_a'), zeros(1)};
 475 

 476 
result_map('cl_mt_pol_c') = {mt_pol_cons, zeros(1)};
 477 
result_map('cl_mt_pol_b_bridge') = {mt_pol_b_bridge, zeros(1)};
 478 
result_map('cl_mt_pol_inf_borr_nobridge') = {mt_pol_inf_borr_nobridge, zeros(1)};
 479 
result_map('cl_mt_pol_for_borr') = {mt_pol_for_borr, zeros(1)};
 480 
result_map('cl_mt_pol_for_save') = {mt_pol_for_save, zeros(1)};
 481 

 482 
result_map('ar_st_pol_names') = ["cl_mt_pol_a", "cl_mt_pol_coh", "cl_mt_pol_c", ...
 483 
    "cl_mt_pol_b_bridge", "cl_mt_pol_inf_borr_nobridge", "cl_mt_pol_for_borr", "cl_mt_pol_for_save"];
 484 

 485 
% Get Discrete Choice Outcomes
 486 
result_map = ffs_fibs_identify_discrete(result_map, bl_input_override);
 487 

 488 
%% Post Solution Graph and Table Generation
 489 
% Note in comparison with *abzr*, results here, even when using identical
 490 
% parameters would differ because in *abzr* solved where choices are
 491 
% principle. Here choices are principle + interests in order to facilitate
 492 
% using the informal choice functions.
 493 
%
 494 
% Note that this means two things are
 495 
% different, on the one hand, the value of asset for to coh is different
 496 
% based on the grid of assets. If the asset grid is negative, now per grid
 497 
% point, there is more coh because that grid point of asset no longer has
 498 
% interest rates. On the other hand, if one has positive asset grid point
 499 
% on arrival, that is worth less to coh. Additionally, when making choices
 500 
% for the next period, now choices aprime includes interests. What these
 501 
% mean is that the a grid no longer has the same meaning. We should expect
 502 
% at higher savings levels, for the same grid points, if optimal grid
 503 
% choices are the same as before, consumption should be lower when b
 504 
% includes interest rates and principle. This is however, not true when
 505 
% arriving in a period with negative a levels, for the same negative a
 506 
% level and same a prime negative choice, could have higher consumption
 507 
% here becasue have to pay less interests on debt. This tends to happen for
 508 
% smaller levels of borrowing choices.
 509 
%
 510 
% Graphically, when using interest + principle, big difference in
 511 
% consumption as a fraction of (coh - aprime) figure. In those figures,
 512 
% when counting in principles only, the gap in coh and aprime is
 513 
% consumption, but now, as more is borrowed only a small fraction of coh
 514 
% and aprime gap is consumption, becuase aprime/(1+r) is put into
 515 
% consumption.
 516 

 517 
if (bl_post)
 518 
    bl_input_override = true;
 519 
    result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
 520 
    result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
 521 
    result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
 522 

 523 
    % Standard AZ graphs
 524 
    result_map = ff_az_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
 525 

 526 
    % Graphs for results_map with FIBS contents
 527 
    result_map = ff_az_fibs_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
 528 
end
 529 

 530 
end

Other subfunctions in this file are not included in this listing.