This is a static copy of a profile report

Home

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

Home

ff_abz_fibs_vf (Calls: 1, Time: 7.491 s)
Generated 14-Jul-2019 14:55:56 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_fibs\m_abz_solve\ff_abz_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
280
fl_aprime_nobridge, bl_display...
514505.117 s68.3%
313
fl_beta*mt_z_trans(it_z_i, it_...
6028470.631 s8.4%
294
[fl_b_bridge, fl_inf_borr_nobr...
1286250.565 s7.5%
289
fl_c = f_cons_coh_save(fl_coh,...
1286250.249 s3.3%
340
ar_val_cur(it_ap_k) = f_util_c...
939540.201 s2.7%
All other lines  0.730 s9.7%
Totals  7.491 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ffs_fibs_min_c_costfunction514504.997 s66.7%
dealfunction1286250.224 s3.0%
ffs_fibs_inf_bridgefunction136500.159 s2.1%
..._save)(coh-ar_for_save./(1+fl_r_fsv))anonymous function1286250.122 s1.6%
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function939550.102 s1.4%
...h,ar_bprime_in_c)(coh+ar_bprime_in_c)anonymous function514500.072 s1.0%
...tions>@(ar_z,ar_b)(ar_z*fl_w+ar_b)anonymous function51450.008 s0.1%
Self time (built-ins, overhead, etc.)  1.808 s24.1%
Totals  7.491 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function523
Non-code lines (comments, blank lines)306
Code lines (lines that can run)217
Code lines that did run115
Code lines that did not run102
Coverage (did run/can run)53.00 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_abz_fibs_vf(varargin)
   8 
%% FF_ABZR_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. 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_abz_paramfunc/html/ffs_abz_fibs_set_default_param.html ffs_abz_fibs_set_default_param>
  48 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abz_paramfunc/html/ffs_abz_fibs_get_funcgrid.html ffs_abz_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_abz_solve/html/ff_abz_fibs_vf.html ff_abz_fibs_vf>
  57 
% * for/inf + borr vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abz_solve/html/ff_abz_fibs_vf_vec.html ff_abz_fibs_vf_vec>
  58 
% * for/inf + borr optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/m_abz_solve/html/ff_abz_fibs_vf_vecsv.html ff_abz_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 
bl_input_override = true;
  69 
[param_map, support_map] = ffs_abz_fibs_set_default_param(it_param_set);
  70 

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

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

  90 
%% Parse Parameters 1
  91 

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

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

 114 
%% Parse Parameters 2
 115 

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

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

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

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

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

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

 150 
%% Initialize Output Matrixes
 151 

 152 
mt_val_cur = zeros(length(ar_a),length(ar_z));
 153 
mt_val = mt_val_cur - 1;
 154 
mt_pol_a = zeros(length(ar_a),length(ar_z));
 155 
mt_pol_a_cur = mt_pol_a - 1;
 156 
mt_pol_cons = zeros(length(ar_a),length(ar_z));
 157 

 158 
% collect optimal borrowing formal and informal choices
 159 
mt_pol_b_bridge = zeros(length(ar_a),length(ar_z));
 160 
mt_pol_inf_borr_nobridge = zeros(length(ar_a),length(ar_z));
 161 
mt_pol_for_borr = zeros(length(ar_a),length(ar_z));
 162 
mt_pol_for_save = zeros(length(ar_a),length(ar_z));
 163 

 164 
%% Initialize Convergence Conditions
 165 

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

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

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

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

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

 200 

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

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

 208 
    % loop 1: over exogenous states
< 0.001 
     21 
 209
    for it_z_i = 1:length(ar_z) 
< 0.001 
    147 
 210
        fl_z = ar_z(it_z_i); 
 211 

 212 
        % loop 2: over endogenous states
< 0.001 
    147 
 213
        for it_a_j = 1:length(ar_a) 
 214 

 215 
            % Get asset state
< 0.001 
   5145 
 216
            fl_a = ar_a(it_a_j); 
 217 

 218 
            % Initialize storage
  0.008 
   5145 
 219
            ar_val_cur = zeros(size(ar_a)); 
  0.004 
   5145 
 220
            ar_c_cur = zeros(size(ar_a)); 
  0.003 
   5145 
 221
            ar_b_bridge = zeros(size(ar_a)); 
  0.003 
   5145 
 222
            ar_inf_borr_nobridge = zeros(size(ar_a)); 
  0.003 
   5145 
 223
            ar_for_borr = zeros(size(ar_a)); 
  0.003 
   5145 
 224
            ar_for_save = zeros(size(ar_a)); 
 225 

 226 
            % calculate cash on hand
  0.018 
   5145 
 227
            fl_coh = f_coh(fl_z, fl_a); 
 228 

 229 
            % loop 3: over choices
< 0.001 
   5145 
 230
            for it_ap_k = 1:length(ar_a) 
 231 

 232 
                % get next period asset choice
  0.008 
 180075 
 233
                fl_ap = ar_a(it_ap_k); 
 234 

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

  0.009 
 180075 
 254
                if (fl_ap < 0) 
 255 

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

< 0.001 
  13650 
 259
                        bl_input_override = true; 
  0.192 
  13650 
 260
                        [fl_aprime_nobridge, fl_b_bridge, fl_c_bridge] = ffs_fibs_inf_bridge(... 
  13650 
 261
                            bl_b_is_principle, fl_r_inf, fl_ap, fl_coh, ... 
  13650 
 262
                            bl_display_infbridge, bl_input_override); 
 263 

  0.002 
  37800 
 264
                    else 
 265 

  0.002 
  37800 
 266
                        fl_aprime_nobridge = fl_ap; 
  0.002 
  37800 
 267
                        fl_b_bridge = 0; 
  0.002 
  37800 
 268
                        fl_c_bridge = 0; 
 269 

  0.002 
  51450 
 270
                    end 
 271 

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

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

  0.004 
 128625 
 286
                else 
 287 

 288 
                    % consumption with savings
  0.249 
 128625 
 289
                    fl_c = f_cons_coh_save(fl_coh, fl_ap); 
 290 

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

  0.008 
 180075 
 296
                end 
 297 

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

  0.004 
  86121 
 306
                    if (bl_default) 
 307 
                        % defaults
 308 
                        % current utility: only today u(cmin)
  0.004 
  86121 
 309
                        ar_val_cur(it_ap_k) = fl_u_cmin; 
 310 
                        % transition out next period, debt wiped out
  0.004 
  86121 
 311
                        for it_az_q = 1:length(ar_z) 
  0.652 
 602847 
 312
                            ar_val_cur(it_ap_k) = ar_val_cur(it_ap_k) + ... 
 602847 
 313
                                fl_beta*mt_z_trans(it_z_i, it_az_q)*mt_val_cur((ar_a == fl_default_aprime), it_az_q); 
  0.025 
 602847 
 314
                        end 
 315 

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

 322 
                        % Replace Consumption if no default nan
 323 
                        fl_c = 0;
  0.004 
  86121 
 324
                    end 
 325 

 326 
                    % no action, defaulting
  0.004 
  86121 
 327
                    fl_b_bridge = 0; 
  0.004 
  86121 
 328
                    fl_inf_borr_nobridge = 0; 
  0.004 
  86121 
 329
                    fl_for_borr = 0; 
  0.004 
  86121 
 330
                    fl_for_save = 0; 
 331 

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

 349 
                %% Store Values
 350 

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

 358 
                % Store consumption
  0.008 
 180075 
 359
                ar_c_cur(it_ap_k) = fl_c; 
 360 

 361 
                % Save/Update Borrowing Information
  0.008 
 180075 
 362
                ar_b_bridge(it_ap_k) = fl_b_bridge; 
  0.008 
 180075 
 363
                ar_inf_borr_nobridge(it_ap_k) = fl_inf_borr_nobridge; 
  0.008 
 180075 
 364
                ar_for_borr(it_ap_k) = fl_for_borr; 
  0.009 
 180075 
 365
                ar_for_save(it_ap_k) = fl_for_save; 
 366 

  0.009 
 180075 
 367
            end 
 368 

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

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

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

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

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

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

  0.002 
   5145 
 409
        end 
  0.001 
    147 
 410
    end 
 411 

 412 
    %% Check Tolerance and Continuation
 413 

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

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

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

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

< 0.001 
     21 
 452
end 
 453 

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

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

 467 
%% Process Optimal Choices
 468 

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

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

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

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

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

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

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

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

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

 529 
end

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