This is a static copy of a profile report

Home

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

Home

ff_ipwkbzr_fibs_evf (Calls: 131, Time: 12.528 s)
Generated 15-Jul-2019 20:58:06 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_fibs\m_ipwkbzr_solve\ff_ipwkbzr_fibs_evf.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ff_ipwkbzr_fibs_vf_vecsvfunction131
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
263
clmt_val_wkb_interpolated{it_z...
65510.002 s79.8%
663
end
1311.254 s10.0%
288
[ar_ev_condi_z_max, ar_ev_cond...
1310.357 s2.8%
335
mt_ev_condi_z_max_kp = reshape...
1310.334 s2.7%
336
mt_ev_condi_z_max_bp = reshape...
1310.320 s2.6%
All other lines  0.261 s2.1%
Totals  12.528 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ismemberfunction6550.096 s0.8%
linspacefunction1310.014 s0.1%
Self time (built-ins, overhead, etc.)  12.417 s99.1%
Totals  12.528 s100% 
Code Analyzer results
Line numberMessage
164The value assigned here to 'ar_ameshk_tnext_with_r' appears to be unused. Consider replacing it by ~.
164The value assigned here to 'ar_k_mesha' appears to be unused. Consider replacing it by ~.
209The value assigned here to 'ar_w_level' appears to be unused. Consider replacing it by ~.
209The value assigned here to 'ar_aplusk_mesh' appears to be unused. Consider replacing it by ~.
217The value assigned here to 'fl_nan_replace' appears to be unused. Consider replacing it by ~.
383The variable 'hAxis' appears to change size on every loop iteration. Consider preallocating for speed.
462Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Coverage results
Show coverage for parent directory
Total lines in function657
Non-code lines (comments, blank lines)271
Code lines (lines that can run)386
Code lines that did run49
Code lines that did not run337
Coverage (did run/can run)12.69 %
Function listing
time 
Calls 
 line
   7 
function [mt_ev_condi_z_max, mt_ev_condi_z_max_idx, mt_ev_condi_z_max_kp, mt_ev_condi_z_max_bp] = ff_ipwkbzr_fibs_evf(varargin)
   8 
%% FF_IPWKBZ_FIBS_EVF solves the k' vs b' problem given aggregate savings
   9 
% This file is based on
  10 
% <https://fanwangecon.github.io/CodeDynaAsset/m_ipwkbzr/solve/html/ff_ipwkbzr_evf.html
  11 
% ff_ipwkbzr_evf>, see that file for more comments. Compare graphs side by
  12 
% side from this file and
  13 
% <https://fanwangecon.github.io/CodeDynaAsset/m_ipwkbzr/solve/html/ff_ipwkbzr_evf.html
  14 
% ff_ipwkbzr_evf> to see visually the effect of introducing formal and
  15 
% informal choices with bridge loan.
  16 
%
  17 
% In contrast to ff_ipwkbzr_evf.m, here, we need to deal with borrowing and
  18 
% savings formal and informal. These will change how the testing matrix is
  19 
% constructed. When bridge loan is allowed, we also need to construct the
  20 
% output matrixes differently. In ff_ipwkbzr_evf.m, the assumption is that
  21 
% coh today does not matter, so to find optimal k* choice, we only need to
  22 
% know the aggregate savings level. But now, we need to know the coh level
  23 
% as well.
  24 
%
  25 
% Below two reachable coh matrixes are constructed, one for when aggregate
  26 
% savings choice w >= 0, and another for when aggregate savings <= 0. Then
  27 
% they are stacked together. And we still have the same outputs as
  28 
% ff_ipwkbzr_evf.m. The difference is that while for savings where w >=0,
  29 
% each row are w levels for the output matrixes, but for w <=0, each row is
  30 
% for w level + coh percentage combinations.
  31 
%
  32 
% @param mt_val matrix state_n I^2 by shock_n. This is the value
  33 
% matrix each row is a feasible reachable state given the choice
  34 
% vectors/matrix and each column is a shock state.
  35 
%
  36 
% @param param_map container parameter container
  37 
%
  38 
% @param support_map container support container
  39 
%
  40 
% @param armt_map container container with states, choices and shocks
  41 
% grids that are inputs for grid based solution algorithm
  42 
%
  43 
% @return mt_ev_condi_z_max matrix *(choice_w_pos_n + choice_w_neg_n x
  44 
% coh_perc_n)* by *shock_n* max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)) conditional
  45 
% on z and w, at the optimal k' choice (w=k'+b') what is the expected
  46 
% utility? This is the value result from the 2nd stage problem. Note the
  47 
% result integrates over z'.
  48 
%
  49 
% @return mt_ev_condi_z_max_idx matrix *(choice_w_pos_n + choice_w_neg_n x
  50 
% coh_perc_n)* by *(shock_n)* this is the argmax from
  51 
% max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)). Given the vector of k' choices,
  52 
% which index maximized conditional on z and w integrating over z'.
  53 
%
  54 
% @return mt_ev_condi_z_max_kp matrix  *(choice_w_pos_n + choice_w_neg_n x
  55 
% coh_perc_n)* by *(shock_n)* the k' choice at
  56 
% max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))
  57 
%
  58 
% @return mt_ev_condi_z_max_bp matrix  *(choice_w_pos_n + choice_w_neg_n x
  59 
% coh_perc_n)* by *(shock_n)* the b'=w-k' choice at
  60 
% max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))
  61 
%
  62 
% @example
  63 
%
  64 
% @include
  65 
%
  66 
% * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_fibs/m_ipwkbzr_paramfunc/ffs_ipwkbzr_fibs_set_default_param.m ffs_ipwkbzr_fibs_set_default_param>
  67 
% * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_fibs/m_ipwkbzr_paramfunc/ffs_ipwkbzr_fibs_get_funcgrid.m ffs_ipwkbzr_fibs_get_funcgrid>
  68 
%
  69 

  70 
%% Default
  71 
% If comparing with
  72 
% <https://fanwangecon.github.io/CodeDynaAsset/m_ipwkbzr/solve/html/ff_ipwkbzr_evf.html
  73 
% ff_ipwkbzr_evf>, note that the borrowing and savings interest rates are
  74 
% the same there. Run st_param_which = 'default' to replicate identical
  75 
% result as ff_ipwkbzr_evf.m.
  76 
%
  77 

< 0.001 
    131 
  78
if (~isempty(varargin)) 
  79 
    
  80 
    % override when called from outside
  0.005 
    131 
  81
    [clmt_val_wkb_interpolated, param_map, support_map, armt_map] = varargin{:}; 
  82 
    
  83 
else
  84 
    
  85 
    close all;
  86 
    % Not default parameters, but parameters that generate defaults
  87 
    it_param_set = 4;
  88 
    [param_map, support_map] = ffs_ipwkbzr_fibs_set_default_param(it_param_set);
  89 

  90 
    support_map('bl_graph_evf') = true;
  91 
    bl_display_evf = true;    
  92 
    support_map('bl_display_evf') = bl_display_evf;
  93 

  94 
    st_param_which = 'default';
  95 

  96 
    if (strcmp(st_param_which, 'default'))
  97 

  98 
        param_map('it_ak_perc_n') = 250;
  99 
        param_map('bl_bridge') = true;
 100 
        
 101 
    elseif (strcmp(st_param_which, 'small'))
 102 

 103 
        param_map('fl_z_r_infbr_n') = 2;
 104 
        param_map('it_z_wage_n') = 3;
 105 
        param_map('it_z_n') = param_map('it_z_wage_n') * param_map('fl_z_r_infbr_n');
 106 
        
 107 
        param_map('fl_b_bd') = -20; % borrow bound, = 0 if save only
 108 
        param_map('fl_default_aprime') = 0;
 109 
        param_map('bl_default') = 0; % if borrowing is default allowed
 110 

 111 
        param_map('fl_w_min') = param_map('fl_b_bd');
 112 
        param_map('it_w_perc_n') = 7;
 113 
        param_map('it_ak_perc_n') = 7;
 114 
        param_map('it_coh_bridge_perc_n') = 3;
 115 
        
 116 
        param_map('fl_w_interp_grid_gap') = 2;
 117 
        param_map('fl_coh_interp_grid_gap') = 2;
 118 
                
 119 
        param_map('fl_z_r_infbr_min') = 0.025;
 120 
        param_map('fl_z_r_infbr_max') = 0.95;
 121 
        param_map('fl_z_r_infbr_n') = 3;
 122 
        
 123 
        param_map('bl_bridge') = true;
 124 
        
 125 
    elseif (strcmp(st_param_which, 'ff_ipwkbzrr_evf'))
 126 
        
 127 
        % ff_ipwkbzrr_evf default
 128 
        param_map('fl_r_fsv') = 0.0;
 129 
        param_map('fl_r_fbr') = 1.000;
 130 
        param_map('it_ak_perc_n') = 250;
 131 
        param_map('bl_bridge') = false;
 132 
        param_map('it_coh_bridge_perc_n') = 1;
 133 
        
 134 
    elseif (strcmp(st_param_which, 'ff_ipwkbzr_evf'))
 135 
        
 136 
        % ff_ipwkbzr_evf default
 137 
        param_map('fl_r_fsv') = 0.025;
 138 
        param_map('fl_z_r_infbr_min') = 0.025;
 139 
        param_map('fl_z_r_infbr_max') = 0.025;
 140 
        param_map('fl_z_r_infbr_n') = 1;
 141 
        param_map('fl_r_fbr') = 0.025;
 142 
        param_map('it_ak_perc_n') = 250;
 143 

 144 
        param_map('bl_bridge') = false;
 145 
        param_map('it_coh_bridge_perc_n') = 1;
 146 
        
 147 
    end
 148 
    
 149 
    % Dimension Adjustments
 150 
    param_map('it_z_n') = param_map('it_z_wage_n') * param_map('fl_z_r_infbr_n');       
 151 
    param_map('fl_w_interp_grid_gap') = (param_map('fl_w_max')-param_map('fl_b_bd'))/param_map('it_ak_perc_n');    
 152 
    
 153 
    % Generate Grids
 154 
    [armt_map, func_map] = ffs_ipwkbzr_fibs_get_funcgrid(param_map, support_map);
 155 

 156 
    % Get Defaults    
 157 
    params_group = values(param_map, {'it_z_n', 'fl_z_r_infbr_n'});
 158 
    [it_z_n, fl_z_r_infbr_n] = params_group{:};
 159 
    params_group = values(param_map, {'st_v_coh_z_interp_method'});
 160 
    [st_v_coh_z_interp_method] = params_group{:};        
 161 
    params_group = values(armt_map, {'mt_coh_wkb', 'ar_z_r_infbr', 'ar_ak_perc', 'ar_w_level_full'});
 162 
    [mt_coh_wkb, ar_z_r_infbr, ar_ak_perc, ar_w_level_full] = params_group{:};
 163 
    params_group = values(armt_map, {'ar_ameshk_tnext_with_r', 'ar_k_mesha'});
 164 
    [ar_ameshk_tnext_with_r, ar_k_mesha] = params_group{:};
 165 
    params_group = values(func_map, {'f_util_standin_coh'});
 166 
    [f_util_standin_coh] = params_group{:};
 167 

 168 
    % mt_coh_wkb is: ((P^{k}_{a>=0} + P^{k}_{a<0} x P^{w frac bridge} ) x I^w x M^r) by (M^z) matrix
 169 
    % mt_coh_wkb(:): ((P^{k}_{a>=0} + P^{k}_{a<0} x P^{w frac bridge} ) x I^w x M^r x M^z) by 1
 170 
    % ar_z_r_infbr is: (1 x M^r)
 171 
    % mt_val: ((P^{k}_{a>=0} + P^{k}_{a<0} x P^{w frac bridge} ) x I^w x M^r x M^z) by (M^r)
 172 
    mt_val = f_util_standin_coh(mt_coh_wkb(:), ar_z_r_infbr);
 173 
    % mt_val is: (I^k x I^w x M^r) by (M^z x M^r)
 174 
    mt_val = reshape(mt_val, [size(mt_coh_wkb, 1), it_z_n]);
 175 

 176 
    if (ismember(st_v_coh_z_interp_method, ["method_idx_a", "method_idx_b", "method_cell"]))
 177 
    
 178 
        it_ak_perc_n = length(ar_ak_perc);
 179 
        it_w_interp_n = length(ar_w_level_full);
 180 
        it_wak_n = it_w_interp_n*it_ak_perc_n;
 181 
        clmt_val_wkb_interpolated = cell([fl_z_r_infbr_n, 1]);
 182 
        for it_z_r_infbr_ctr = 1:1:fl_z_r_infbr_n
 183 
            it_mt_val_row_start = it_wak_n*(it_z_r_infbr_ctr-1) + 1;
 184 
            it_mt_val_row_end = it_mt_val_row_start + it_wak_n - 1;
 185 
            clmt_val_wkb_interpolated{it_z_r_infbr_ctr} = ...
 186 
                mt_val(it_mt_val_row_start:it_mt_val_row_end, :);
 187 
        end
 188 
    
 189 
    elseif (ismember(st_v_coh_z_interp_method, ["method_matrix", "method_mat_seg"]))
 190 
        
 191 
        clmt_val_wkb_interpolated = mt_val;
 192 
        
 193 
    end
 194 
    
 195 
    % Display Parameters
 196 
    if (bl_display_evf)
 197 
        fft_container_map_display(param_map);
 198 
        fft_container_map_display(support_map);
 199 
    end
 200 
    
< 0.001 
    131 
 201
end 
 202 

 203 
%% Parse Parameters
 204 

 205 
% armt_map
  0.003 
    131 
 206
params_group = values(armt_map, {'ar_z_r_infbr_mesh_wage_w1r2', 'ar_z_wage_mesh_r_infbr_w1r2'}); 
  0.001 
    131 
 207
[ar_z_r_infbr_mesh_wage_w1r2, ar_z_wage_mesh_r_infbr_w1r2] = params_group{:}; 
  0.002 
    131 
 208
params_group = values(armt_map, {'mt_z_trans', 'ar_ak_perc', 'ar_w_level', 'ar_k_mesha', 'ar_a_meshk', 'ar_aplusk_mesh'}); 
  0.002 
    131 
 209
[mt_z_trans, ar_ak_perc, ar_w_level, ar_k_mesha, ar_a_meshk, ar_aplusk_mesh] = params_group{:}; 
  0.002 
    131 
 210
params_group = values(armt_map, {'ar_w_level_full'}); 
< 0.001 
    131 
 211
[ar_w_level_full] = params_group{:}; 
 212 

 213 
% param_map
  0.002 
    131 
 214
params_group = values(param_map, {'it_z_n', 'fl_z_r_infbr_n', 'it_z_wage_n'}); 
  0.002 
    131 
 215
[it_z_n, fl_z_r_infbr_n, it_z_wage_n] = params_group{:}; 
  0.002 
    131 
 216
params_group = values(param_map, {'fl_nan_replace', 'fl_b_bd'}); 
  0.001 
    131 
 217
[fl_nan_replace, fl_b_bd] = params_group{:}; 
  0.001 
    131 
 218
params_group = values(param_map, {'st_v_coh_z_interp_method'}); 
< 0.001 
    131 
 219
[st_v_coh_z_interp_method] = params_group{:}; 
 220 

 221 
% support_map
  0.002 
    131 
 222
params_group = values(support_map, {'bl_graph_onebyones','bl_display_evf', 'bl_graph_evf'}); 
  0.001 
    131 
 223
[bl_graph_onebyones, bl_display_evf, bl_graph_evf] = params_group{:}; 
  0.002 
    131 
 224
params_group = values(support_map, {'bl_img_save', 'st_img_path', 'st_img_prefix', 'st_img_name_main', 'st_img_suffix'}); 
  0.002 
    131 
 225
[bl_img_save, st_img_path, st_img_prefix, st_img_name_main, st_img_suffix] = params_group{:}; 
  0.002 
    131 
 226
params_group = values(support_map, {'it_display_summmat_rowmax', 'it_display_summmat_colmax'}); 
  0.001 
    131 
 227
[it_display_summmat_rowmax, it_display_summmat_colmax] = params_group{:}; 
 228 

 229 
% append function name
< 0.001 
    131 
 230
st_func_name = 'ff_ipwkbzr_fibs_evf'; 
  0.001 
    131 
 231
st_img_name_main = [st_func_name st_img_name_main]; 
 232 

 233 
%% Integrate *E(V(coh(k',forinf(b',zr)),zw',zr')|zw,zr)*
 234 
% Start with E(V(coh(k',forinf(b',zr)),zw',zr')|zw,zr), integrate to find
 235 
% EV(k',b';zw,zr).
 236 
% 
 237 
% Note that mt_ev_condi_z rows are less by length of r rate shock times.
 238 
%
 239 
% # mt_val = (it_w_interp_n*it_ak_perc_n*length(fl_z_r_infbr_n)) by (it_z_wage_n*length(fl_z_r_infbr_n))
 240 
% # mt_ev_condi_z = (it_w_interp_n*it_ak_perc_n) by (it_z_wage_n*length(fl_z_r_infbr_n))
 241 
%
 242 

 243 
% 1. Number of W/B/K Choice Combinations
< 0.001 
    131 
 244
it_ak_perc_n = length(ar_ak_perc); 
< 0.001 
    131 
 245
it_w_interp_n = length(ar_w_level_full); 
< 0.001 
    131 
 246
it_wak_n = it_w_interp_n*it_ak_perc_n; 
 247 

 248 
% 2. Initialize mt_ev_condi_z = E(V(coh(k',b',zr'),zw',zr')|zw,zr)
 249 
% rows = it_wak_n
 250 
% cols = it_z_n
  0.019 
    131 
 251
mt_ev_condi_z = zeros([it_wak_n, it_z_n]); 
 252 

< 0.001 
    131 
 253
for it_z_r_infbr_ctr = 1:1:fl_z_r_infbr_n 
 254 
    
 255 
    % Transition Row Subset: ((M^z) by (M^z x M^r))' for one m^r
< 0.001 
    655 
 256
    it_mt_z_trans_row_start = it_z_wage_n*(it_z_r_infbr_ctr-1) + 1; 
< 0.001 
    655 
 257
    it_mt_z_trans_row_end = it_mt_z_trans_row_start + it_z_wage_n - 1;     
  0.015 
    655 
 258
    mt_z_trans_cur_z_r_borr = mt_z_trans(it_mt_z_trans_row_start:it_mt_z_trans_row_end, :); 
 259 
    
  0.105 
    655 
 260
    if (ismember(st_v_coh_z_interp_method, ["method_idx_a", "method_idx_b", "method_cell"])) 
 261 
        
 10.002 
    655 
 262
        mt_ev_condi_z(:, it_mt_z_trans_row_start:it_mt_z_trans_row_end) = ... 
    655 
 263
            clmt_val_wkb_interpolated{it_z_r_infbr_ctr}*mt_z_trans_cur_z_r_borr'; 
 264 
        
 265 
    elseif (ismember(st_v_coh_z_interp_method, ["method_matrix", "method_mat_seg"]))
 266 
        
 267 
        % Val Segment : ((M^z) by (M^z x M^r))' for one m^r
 268 
        it_mt_val_row_start = it_wak_n*(it_z_r_infbr_ctr-1) + 1;
 269 
        it_mt_val_row_end = it_mt_val_row_start + it_wak_n - 1;
 270 
        mt_val_cur_z_r_borr = clmt_val_wkb_interpolated(it_mt_val_row_start:it_mt_val_row_end, :);
 271 

 272 
        % EV(k',b';zw,zr) = E(V(coh(k',b',zr),zw',zr')|zw,zr) for one zr and all zw
 273 
        mt_ev_condi_z(:, it_mt_z_trans_row_start:it_mt_z_trans_row_end) = ...
 274 
            mt_val_cur_z_r_borr*mt_z_trans_cur_z_r_borr';
 275 
        
 276 
    end
 277 
    
  0.002 
    655 
 278
end 
 279 

 280 
%% Reshape *E(V(coh,z'|z,w))* to allow for maxing
 281 
% dim(mt_ev_condi_z): *IxJ by M*
 282 

  0.002 
    131 
 283
mt_ev_condi_z_full = reshape(mt_ev_condi_z, [it_ak_perc_n, it_w_interp_n*it_z_n]); 
 284 

 285 
%% Maximize *max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))* optimal value and index
 286 
% Maximization, find optimal k'/b' combination given z and w=k'+b'
 287 

  0.357 
    131 
 288
[ar_ev_condi_z_max, ar_ev_condi_z_max_idx] = max(mt_ev_condi_z_full); 
  0.002 
    131 
 289
mt_ev_condi_z_max = reshape(ar_ev_condi_z_max, [it_w_interp_n, it_z_n]); 
< 0.001 
    131 
 290
mt_ev_condi_z_max_idx = reshape(ar_ev_condi_z_max_idx, [it_w_interp_n, it_z_n]); 
< 0.001 
    131 
 291
if(bl_display_evf) 
 292 

 293 
    disp('----------------------------------------');
 294 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 295 
    disp('mt_ev_condi_z_full: J by IxM');
 296 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 297 
    disp(size(mt_ev_condi_z_full));
 298 
%     disp(head(array2table(mt_ev_condi_z_full), 20));
 299 
%     disp(tail(array2table(mt_ev_condi_z_full), 20));
 300 

 301 
    disp('----------------------------------------');
 302 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 303 
    disp('mt_ev_condi_z_max: I by M');
 304 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 305 
    disp(size(mt_ev_condi_z_max));
 306 
    disp(head(array2table(mt_ev_condi_z_max), 20));
 307 
    disp(tail(array2table(mt_ev_condi_z_max), 20));
 308 

 309 
    disp('----------------------------------------');
 310 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 311 
    disp('mt_ev_condi_z_max_idx: I by M');
 312 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 313 
    disp(size(mt_ev_condi_z_max_idx));
 314 
    disp(head(array2table(mt_ev_condi_z_max_idx), 20));
 315 
    disp(tail(array2table(mt_ev_condi_z_max_idx), 20));
 316 

 317 
end
 318 

 319 
%% Reindex K' and B' Choices for each State at the Optimal *w'=k'+b'* choice
 320 
% The K' and B' Optimal Choices Associated with EV opti
 321 
% dim(mt_ev_condi_z_max_kp): *I by M*
  0.017 
    131 
 322
ar_add_grid = linspace(0, it_ak_perc_n*(it_w_interp_n-1), it_w_interp_n); 
  0.058 
    131 
 323
mt_ev_condi_z_max_idx = mt_ev_condi_z_max_idx + ar_add_grid'; 
 324 

< 0.001 
    131 
 325
if(bl_display_evf) 
 326 
    disp('----------------------------------------');
 327 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 328 
    disp('mt_ev_condi_z_max_idx: I by M');
 329 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 330 
    disp(size(mt_ev_condi_z_max_idx));
 331 
    disp(head(array2table(mt_ev_condi_z_max_idx), 20));
 332 
    disp(tail(array2table(mt_ev_condi_z_max_idx), 20));
 333 
end
 334 

  0.334 
    131 
 335
mt_ev_condi_z_max_kp = reshape(ar_k_mesha(mt_ev_condi_z_max_idx), [it_w_interp_n, it_z_n]); 
  0.320 
    131 
 336
mt_ev_condi_z_max_bp = reshape(ar_a_meshk(mt_ev_condi_z_max_idx), [it_w_interp_n, it_z_n]); 
 337 

< 0.001 
    131 
 338
if(bl_display_evf) 
 339 
    disp('----------------------------------------');
 340 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 341 
    disp('mt_ev_condi_z_max_kp: I by M');
 342 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 343 
    disp(size(mt_ev_condi_z_max_kp));
 344 
    disp(head(array2table(mt_ev_condi_z_max_kp), 20));
 345 
    disp(tail(array2table(mt_ev_condi_z_max_kp), 20));
 346 

 347 
    disp('----------------------------------------');
 348 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 349 
    disp('mt_ev_condi_z_max_bp: I by M');
 350 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 351 
    disp(size(mt_ev_condi_z_max_bp));
 352 
    disp(head(array2table(mt_ev_condi_z_max_bp), 20));
 353 
    disp(tail(array2table(mt_ev_condi_z_max_bp), 20));
 354 
end
 355 

 356 
%% Graph
 357 

< 0.001 
    131 
 358
if (bl_graph_evf) 
 359 

 360 
    %% Generate Limited Legends
 361 
    % 8 graph points, 2 levels of borrow rates, and 4 levels of rbr rates
 362 
    ar_it_z_r_infbr = ([1 round((fl_z_r_infbr_n)/2) (fl_z_r_infbr_n)]);
 363 
    ar_it_z_wage = ([1 round((it_z_wage_n)/2) (it_z_wage_n)]);
 364 

 365 
    % combine by index
 366 
    mt_it_z_graph = ar_it_z_wage' + it_z_wage_n*(ar_it_z_r_infbr-1);
 367 
    ar_it_z_graph = mt_it_z_graph(:)';
 368 

 369 
    % legends index final
 370 
    cl_st_legendCell = cellstr([num2str(ar_z_r_infbr_mesh_wage_w1r2', 'zr=%3.2f;'), ...
 371 
                                num2str(ar_z_wage_mesh_r_infbr_w1r2', 'zw=%3.2f')]);
 372 
    
 373 
    %% Graph 1, V and EV
 374 
    if (~bl_graph_onebyones)
 375 
        figure('PaperPosition', [0 0 14 4]);
 376 
        hold on;
 377 
    end
 378 

 379 

 380 
    for subplot_j=1:1:2
 381 

 382 
        if (~bl_graph_onebyones)
 383 
            hAxis(subplot_j) = subplot(1,2,subplot_j);
 384 
        else
 385 
            figure('PaperPosition', [0 0 7 4]);
 386 
        end
 387 

 388 
        if (subplot_j==1)
 389 
            chart = plot(mt_val);
 390 
        end
 391 
        if (subplot_j==2)
 392 
            chart = plot(mt_ev_condi_z);
 393 
        end
 394 

 395 
        clr = jet(numel(chart));
 396 
        for m = 1:numel(chart)
 397 
            set(chart(m),'Color',clr(m,:))
 398 
        end
 399 

 400 
        legend(chart(ar_it_z_graph), cl_st_legendCell(ar_it_z_graph), 'Location','southeast');
 401 
        
 402 
        if (subplot_j==1)
 403 
            title('V(coh,zp); w(k+b),k,z');
 404 
        end
 405 
        if (subplot_j==2)
 406 
            title('E_z(V(coh,zp|z))');
 407 
        end
 408 

 409 
        ylabel('Next Period Value');
 410 
        xlabel({'Index of Cash-on-Hand Discrete Point'...
 411 
            'Each Segment is a w=k+b; within segment increasing k'...
 412 
            'EV and V identical if shock is fully persistent'});
 413 
        grid on;
 414 
        grid minor;
 415 
    end
 416 

 417 
    % Share y axis
 418 
    if (~bl_graph_onebyones)
 419 
        linkaxes(hAxis,'y');
 420 
    end
 421 

 422 
    % save file
 423 
    if (bl_img_save)
 424 
        mkdir(support_map('st_img_path'));
 425 
        st_file_name = [st_img_prefix st_img_name_main '_vev' st_img_suffix];
 426 
        saveas(gcf, strcat(st_img_path, st_file_name));
 427 
    end
 428 

 429 
    %% Graph 2, max(EV)
 430 

 431 
    if(~bl_graph_onebyones)
 432 
        figure('PaperPosition', [0 0 7 4]);
 433 
    end
 434 

 435 
    for sub_j=1:1:1
 436 

 437 
        if(sub_j==1)
 438 
            mt_outcome = mt_ev_condi_z_max;
 439 
            st_y_label = 'max_{k''}(E(V(coh(k'',b''=w-k''),z''|z,w))';
 440 
        end
 441 

 442 
        if(~bl_graph_onebyones)
 443 
            subplot(1,1,sub_j)
 444 
        else
 445 
            figure('PaperPosition', [0 0 7 4]);
 446 
        end
 447 
        hold on;
 448 

 449 
        clr = jet(length(ar_it_z_graph));
 450 
        i_ctr = 0;
 451 
        for i = ar_it_z_graph
 452 
            i_ctr = i_ctr + 1;
 453 
            ar_x = ar_w_level_full;
 454 
            ar_y = mt_outcome(:, i);
 455 
            scatter(ar_x, ar_y, 5, ...
 456 
                'MarkerEdgeColor', clr(i_ctr,:), ...
 457 
                'MarkerFaceColor', clr(i_ctr,:));
 458 
        end
 459 

 460 
        grid on;
 461 
        grid minor;
 462 
        title(['2nd Stage Exp Value at Optimal K given W=K''+B'''])
 463 
        ylabel(st_y_label)
 464 
        xlabel({'Aggregate Savings'})
 465 

 466 
        legendCell_here = cl_st_legendCell;
 467 
        legendCell_here{length(legendCell_here) + 1} = 'max-agg-save';
 468 
        legend(legendCell_here([ar_it_z_graph length(legendCell_here)]), 'Location','southeast');
 469 
        
 470 
        xline0 = xline(0);
 471 
        xline0.HandleVisibility = 'off';
 472 
        yline0 = yline(0);
 473 
        yline0.HandleVisibility = 'off';
 474 

 475 
    end
 476 

 477 
    % save file
 478 
    if (bl_img_save)
 479 
        mkdir(support_map('st_img_path'));
 480 
        st_file_name = [st_img_prefix st_img_name_main '_maxev' st_img_suffix];
 481 
        saveas(gcf, strcat(st_img_path, st_file_name));
 482 
    end
 483 

 484 
    %% Graph 3, at max(EV) optimal choice category, color regions, borrow save
 485 

 486 
    % Borrow Vs Save
 487 
    [ar_z_mw, ar_w_mz] = meshgrid(ar_z_wage_mesh_r_infbr_w1r2, ar_w_level_full);
 488 
    mt_it_borr_idx = (mt_ev_condi_z_max_bp < 0);
 489 
    mt_it_riskyhalf_idx = ((mt_ev_condi_z_max_kp./mt_ev_condi_z_max_bp) > 0.5);
 490 
    mt_it_kzero_idx = (mt_ev_condi_z_max_kp == 0);
 491 
    mt_it_isnan_idx = (isnan(mt_ev_condi_z_max_kp));
 492 

 493 
    figure('PaperPosition', [0 0 7 4]);
 494 
    % States: ar_w, ar_z
 495 
    % Choices: mt_ev_condi_z_max_kp, mt_ev_condi_z_max_bp
 496 
    hold on;
 497 
    it_sca_size = 10;
 498 
    chart_br = scatter(ar_w_mz(mt_it_borr_idx),...
 499 
        ar_z_mw(mt_it_borr_idx),...
 500 
        it_sca_size, 'blue', 'filled');
 501 
    %     legend([chart_br], {'Borrow'}, 'Location','northeast');
 502 
    chart_khalf = scatter(ar_w_mz(~mt_it_borr_idx & mt_it_riskyhalf_idx),...
 503 
        ar_z_mw(~mt_it_borr_idx & mt_it_riskyhalf_idx),...
 504 
        it_sca_size, 'black', 'filled');
 505 
    %     legend([chart_khalf], {'Save >0.5 K'}, 'Location','northeast');
 506 
    chart_sv = scatter(ar_w_mz(~mt_it_borr_idx & ~mt_it_riskyhalf_idx),...
 507 
        ar_z_mw(~mt_it_borr_idx & ~mt_it_riskyhalf_idx),...
 508 
        it_sca_size, 'red', 'filled');
 509 
    %     legend([chart_sv], {'Save <0.5 K'}, 'Location','northeast');
 510 
    chart_invalid = scatter(ar_w_mz(mt_it_kzero_idx | mt_it_isnan_idx),...
 511 
        ar_z_mw(mt_it_kzero_idx | mt_it_isnan_idx),...
 512 
        it_sca_size, 'yellow', 'filled');
 513 
    legend([chart_br, chart_khalf, chart_sv, chart_invalid], ...
 514 
        {'Borrow','Save >0.5 K','Save <0.5 K', 'k=0 or k=nan'}, 'Location','northeast');
 515 
    title('Borrow and Save Regions')
 516 
    ylabel('Shocks')
 517 
    xlabel({'Total Savings w=k+b'})
 518 
    grid on;
 519 

 520 
    % save file
 521 
    if (bl_img_save)
 522 
        mkdir(support_map('st_img_path'));
 523 
        st_file_name = [st_img_prefix st_img_name_main '_maxbrsv' st_img_suffix];
 524 
        saveas(gcf, strcat(st_img_path, st_file_name));
 525 
    end
 526 

 527 
    %% Graph 4, Optimal K' and B' Levels
 528 
    % compare results here to results from <https://fanwangecon.github.io/CodeDynaAsset/m_ipwkbzr/solve/html/ff_ipwkbzr_evf.html
 529 
    % ff_ipwkbzr_evf>. Several key differences:
 530 
    %
 531 
    % # Each color line is thicker here, because there is in effect another
 532 
    % state that is relevant now in the 2nd stage, which is the
 533 
    % cash-on-hand percentage, which is implemented as a percentage of the
 534 
    % w = k' + b' choice that needs to go cover bridge loan. So different
 535 
    % percentages have the same color, hence thicker lines fore each color
 536 
    % # Jump between saving and borrowing, here, the borrowing and savings
 537 
    % interest rates differ
 538 
    % # Finally, the discontinuities in choices, they occur here because of
 539 
    % the formal menu of choices, the little squiggly up and downs are due
 540 
    % to households using informal choices to complement formal choices.
 541 
    %
 542 

 543 
    [~, ar_w_mz] = meshgrid(ar_z_wage_mesh_r_infbr_w1r2, ar_w_level_full);
 544 
    for sub_j=1:1:4
 545 

 546 
        if (bl_graph_onebyones)
 547 
            figure('PaperPosition', [0 0 7 4]);
 548 
        end
 549 

 550 
        if (sub_j==1)
 551 
            if(~bl_graph_onebyones)
 552 
                figure('PaperPosition', [0 0 14 4]);
 553 
                subplot(1,2,sub_j);
 554 
            end
 555 
            mt_y = mt_ev_condi_z_max_bp;
 556 
        end
 557 
        if (sub_j==2)
 558 
            if(~bl_graph_onebyones)
 559 
                subplot(1,2,sub_j);
 560 
            end
 561 

 562 
            mt_y = mt_ev_condi_z_max_kp;
 563 
        end
 564 
        if (sub_j==3)
 565 
            if(~bl_graph_onebyones)
 566 
                figure('PaperPosition', [0 0 14 4]);
 567 
                subplot(1,2,sub_j-2);
 568 
            end
 569 
            mt_y = zeros(size(mt_ev_condi_z_max_bp));
 570 
            mt_it_borr_idx = (mt_ev_condi_z_max_bp < 0);
 571 
            mt_y(mt_it_borr_idx) = -mt_ev_condi_z_max_bp(mt_it_borr_idx)/fl_b_bd;
 572 
            mt_y(~mt_it_borr_idx) = mt_ev_condi_z_max_bp(~mt_it_borr_idx)./ar_w_mz(~mt_it_borr_idx);
 573 
        end
 574 
        if (sub_j==4)
 575 
            if(~bl_graph_onebyones)
 576 
                subplot(1,2,sub_j-2);
 577 
            end
 578 
            mt_y = mt_ev_condi_z_max_kp./(ar_w_level_full'-fl_b_bd);
 579 
        end
 580 

 581 
        hold on;
 582 
        clr = jet(it_z_n);
 583 
        for m = 1:it_z_n
 584 
            chart(m) = scatter(ar_w_level_full, mt_y(:, m), 3, ...
 585 
                'Marker', 'O', ...
 586 
                'MarkerEdgeColor', clr(m,:), 'MarkerFaceAlpha', 0.75, ...
 587 
                'MarkerFaceColor', clr(m,:), 'MarkerEdgeAlpha', 0.75);
 588 
        end
 589 

 590 
        legend2plot = fliplr(ar_it_z_graph);
 591 
        legendCell = cl_st_legendCell;
 592 

 593 
        xline0 = xline(0);
 594 
        xline0.HandleVisibility = 'off';
 595 
        yline0 = yline(0);
 596 
        yline0.HandleVisibility = 'off';
 597 
        grid on;
 598 
        if (sub_j<=2)
 599 
            hline = refline([1 0]);
 600 
            hline.Color = 'k';
 601 
            hline.LineStyle = ':';
 602 
            hline.HandleVisibility = 'off';
 603 
        end
 604 

 605 
        if (sub_j==1)
 606 
            title('B Choices of W');
 607 
            ylabel('B Choices');
 608 
            xlabel({'Total Savings w=k+b'});
 609 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
 610 
        end
 611 
        if (sub_j==2)
 612 
            title('K Choices of W');
 613 
            ylabel('K Choices');
 614 
            xlabel({'Total Savings w=k+b'});
 615 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
 616 
        end
 617 

 618 
        if (sub_j==3)
 619 
            title('B Fraction of Borrow Max and Save');
 620 
            ylabel('B/bar(B) if br or B/W if sv');
 621 
            xlabel({'Total Savings w=k+b'});
 622 
            %             set(gca, 'YScale', 'log');
 623 
            ylim([-1.1 1.1]);
 624 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
 625 
        end
 626 
        if (sub_j==4)
 627 
            title('K Fraction Choices of Total K Possible');
 628 
            ylabel('K/(W-bar(b)) ');
 629 
            xlabel({'Total Savings w=k+b'});
 630 
            %             set(gca, 'YScale', 'log');
 631 
            ylim([0 1.1]);
 632 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northeast');
 633 
        end
 634 

 635 
    end
 636 

 637 
    % save file
 638 
    if (bl_img_save)
 639 
        mkdir(support_map('st_img_path'));
 640 
        st_file_name = [st_img_prefix st_img_name_main '_wkbopti' st_img_suffix];
 641 
        saveas(gcf, strcat(st_img_path, st_file_name));
 642 
    end
 643 

 644 
end
 645 

 646 
%% Display Various Containers
 647 

< 0.001 
    131 
 648
if (bl_display_evf) 
 649 

 650 
    %% Display 1 support_map
 651 
    fft_container_map_display(support_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 652 
        
 653 
    %% Display 2 armt_map
 654 
    fft_container_map_display(armt_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 655 

 656 
    %% Display 3 param_map
 657 
    fft_container_map_display(param_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 658 
    
 659 
    %% Display 4 func_map
 660 
    fft_container_map_display(func_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 661 
end
 662 

  1.254 
    131 
 663
end 

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