This is a static copy of a profile report

Home

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

Home

ff_ipwkbzr_evf (Calls: 152, Time: 4.432 s)
Generated 28-Jul-2019 18:14:01 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_ipwkbzr\solve\ff_ipwkbzr_evf.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ff_ipwkbzr_vf_vecsvfunction152
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
255
clmt_val_wkb_interpolated{it_z...
7603.338 s75.3%
690
end
1520.399 s9.0%
309
[ar_ev_condi_z_max, ar_ev_cond...
1520.168 s3.8%
252
if (ismember(st_v_coh_z_interp...
7600.149 s3.4%
362
mt_ev_condi_z_max_kp = reshape...
1520.107 s2.4%
All other lines  0.270 s6.1%
Totals  4.432 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ismemberfunction7600.137 s3.1%
linspacefunction1520.007 s0.1%
Self time (built-ins, overhead, etc.)  4.288 s96.8%
Totals  4.432 s100% 
Code Analyzer results
Line numberMessage
75Use of brackets [] is unnecessary. Use parentheses to group, if needed.
79Use of brackets [] is unnecessary. Use parentheses to group, if needed.
100Use of brackets [] is unnecessary. Use parentheses to group, if needed.
185The value assigned here to 'fl_nan_replace' appears to be unused. Consider replacing it by ~.
333Use of brackets [] is unnecessary. Use parentheses to group, if needed.
500Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Coverage results
Show coverage for parent directory
Total lines in function684
Non-code lines (comments, blank lines)316
Code lines (lines that can run)368
Code lines that did run47
Code lines that did not run321
Coverage (did run/can run)12.77 %
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_evf(varargin)
   8 
%% FF_IPWKBZR_EVF solves the k' vs b' problem given aggregate savings
   9 
% This function follows the structure set up here:
  10 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_evf.html
  11 
% ff_wkz_evf> but now we solve the second stage with percentage choice grid
  12 
%
  13 
% We solve along a vector of w_n vector, that is an interpolation vector,
  14 
% not a vector of actual w choices picked in the first stage. k' choices
  15 
% are in terms of percentages. Compared to ff_wkz_evf where we only had an
  16 
% upper triangle of choices, now we have a full matrix of percentage
  17 
% choices.
  18 
%
  19 
% @param mt_val matrix state_n I^2 by shock_n. This is the value
  20 
% matrix each row is a feasible reachable state given the choice
  21 
% vectors/matrix and each column is a shock state.
  22 
%
  23 
% @param param_map container parameter container
  24 
%
  25 
% @param support_map container support container
  26 
%
  27 
% @param armt_map container container with states, choices and shocks
  28 
% grids that are inputs for grid based solution algorithm
  29 
%
  30 
% @return mt_ev_condi_z_max matrix choice_w_n by shock_n
  31 
% max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)) conditional on z and w, at the
  32 
% optimal k' choice (w=k'+b') what is the expected utility? This is the
  33 
% value result from the 2nd stage problem. Note the result integrates over
  34 
% z'.
  35 
%
  36 
% @return mt_ev_condi_z_max_idx matrix choice_w_n by shock_n this is the
  37 
% argmax from max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)). Given the vector of k'
  38 
% choices, which index maximized conditional on z and w integrating over
  39 
% z'/
  40 
%
  41 
% @return mt_ev_condi_z_max_kp matrix choice_w_level_n by shock_n the k'
  42 
% choice at max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))
  43 
%
  44 
% @return mt_ev_condi_z_max_bp matrix choice_w_n by shock_n the b'=w-k'
  45 
% choice at max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))
  46 
%
  47 
% @example
  48 
%
  49 
% @include
  50 
%
  51 
% * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_ipwkbzr/paramfunc/ffs_ipwkbzr_set_default_param.m ffs_ipwkbzr_set_default_param>
  52 
% * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_ipwkbzr/paramfunc/ffs_ipwkbzr_get_funcgrid.m ffs_ipwkbzr_get_funcgrid>
  53 
%
  54 

  55 
%% Default
  56 

< 0.001 
    152 
  57
if (~isempty(varargin)) 
  58 
    
  59 
    % override when called from outside
  0.007 
    152 
  60
    [clmt_val_wkb_interpolated, param_map, support_map, armt_map] = varargin{:}; 
  61 
    
  62 
else
  63 
    
  64 
    close all;
  65 
    
  66 
    % Not default parameters, but parameters that generate defaults
  67 
    [param_map, support_map] = ffs_ipwkbzr_set_default_param();
  68 
    
  69 
    support_map('bl_graph_evf') = true;
  70 
    bl_display_evf = true;
  71 
    support_map('bl_display_evf') = bl_display_evf;        
  72 
    
  73 
    st_param_which = 'default';
  74 

  75 
    if (ismember(st_param_which, ['default']))
  76 

  77 
        param_map('it_ak_perc_n') = 250;        
  78 
        
  79 
    elseif ismember(st_param_which, ['small'])
  80 

  81 
        param_map('fl_z_r_borr_n') = 2;
  82 
        param_map('it_z_wage_n') = 3;
  83 
        param_map('it_z_n') = param_map('it_z_wage_n') * param_map('fl_z_r_borr_n');
  84 

  85 
        param_map('fl_b_bd') = -20; % borrow bound, = 0 if save only
  86 
        param_map('fl_default_aprime') = 0;
  87 
        param_map('bl_default') = 0; % if borrowing is default allowed
  88 

  89 
        param_map('fl_w_min') = param_map('fl_b_bd');
  90 
        param_map('it_w_perc_n') = 7;
  91 
        param_map('it_ak_perc_n') = 7;
  92 

  93 
        param_map('fl_w_interp_grid_gap') = 2;
  94 
        param_map('fl_coh_interp_grid_gap') = 2;
  95 

  96 
        param_map('fl_z_r_borr_min') = 0.025;
  97 
        param_map('fl_z_r_borr_max') = 0.95;
  98 
        param_map('fl_z_r_borr_n') = 3;
  99 
        
 100 
    elseif ismember(st_param_which, ['ff_ipwkbzr_evf'])
 101 
        
 102 
        % ff_ipwkbzr_evf default
 103 
        param_map('fl_z_r_borr_min') = 0.025;
 104 
        param_map('fl_z_r_borr_max') = 0.025;
 105 
        param_map('fl_z_r_borr_n') = 1;
 106 
        param_map('it_ak_perc_n') = 250;
 107 
        
 108 
        param_map('fl_r_save') = 0.025;
 109 
        
 110 
    end
 111 
    
 112 
    % Dimension Adjustments
 113 
    param_map('it_z_n') = param_map('it_z_wage_n') * param_map('fl_z_r_borr_n');       
 114 
    param_map('fl_w_interp_grid_gap') = (param_map('fl_w_max')-param_map('fl_b_bd'))/param_map('it_ak_perc_n');
 115 

 116 
    % Generate Grids
 117 
    [armt_map, func_map] = ffs_ipwkbzr_get_funcgrid(param_map, support_map);
 118 
    
 119 
    % Get Defaults
 120 
    params_group = values(param_map, {'it_z_n', 'fl_z_r_borr_n'});
 121 
    [it_z_n, fl_z_r_borr_n] = params_group{:};    
 122 
    params_group = values(param_map, {'st_v_coh_z_interp_method'});
 123 
    [st_v_coh_z_interp_method] = params_group{:};    
 124 
    params_group = values(armt_map, {'mt_coh_wkb', 'ar_z_r_borr', 'ar_ak_perc', 'ar_w_level'});
 125 
    [mt_coh_wkb, ar_z_r_borr, ar_ak_perc, ar_w_level] = params_group{:};
 126 
    params_group = values(func_map, {'f_util_standin_coh'});
 127 
    [f_util_standin_coh] = params_group{:};
 128 
    
 129 
    % Note that for the testing function below, ar_z_r_borr does not need
 130 
    % to matter for testing, meaning V(coh, zw, zr_j) = V(coh, zw, zr_i).
 131 
    % With integration it matters. This is an important point, for just
 132 
    % last period debt, if no new borrowing choices are made, it does not
 133 
    % matter what new zr shocks are, just what last period rates are. But
 134 
    % once the problem is dynamic. But the object of interest here is:
 135 
    % EV(k', b', zw, zr), conditionally on the same k'/b', will zr have an
 136 
    % impact? yes it will, even just through interest rate on b'.
 137 
    
 138 
    % mt_coh_wkb is: (I^k x I^w x M^r) by (M^z)
 139 
    % mt_coh_wkb(:) is: (I^k x I^w x M^r x M^z) by 1
 140 
    % ar_z_r_borr is: 1 by M^r
 141 
    % mt_val is: (I^k x I^w x M^r x M^z) by (M^r)
 142 
    mt_val = f_util_standin_coh(mt_coh_wkb(:), ar_z_r_borr);
 143 
    % mt_val is: (I^k x I^w x M^r) by (M^z x M^r)
 144 
    mt_val = reshape(mt_val, [size(mt_coh_wkb, 1), it_z_n]);
 145 

 146 
    if (ismember(st_v_coh_z_interp_method, ["method_idx_a", "method_idx_b", "method_cell"]))
 147 
    
 148 
        it_ak_perc_n = length(ar_ak_perc);
 149 
        it_w_interp_n = length(ar_w_level);
 150 
        it_wak_n = it_w_interp_n*it_ak_perc_n;
 151 
        clmt_val_wkb_interpolated = cell([fl_z_r_borr_n, 1]);
 152 
        for it_z_r_borr_ctr = 1:1:fl_z_r_borr_n
 153 
            it_mt_val_row_start = it_wak_n*(it_z_r_borr_ctr-1) + 1;
 154 
            it_mt_val_row_end = it_mt_val_row_start + it_wak_n - 1;
 155 
            clmt_val_wkb_interpolated{it_z_r_borr_ctr} = ...
 156 
                mt_val(it_mt_val_row_start:it_mt_val_row_end, :);
 157 
        end
 158 
    
 159 
    elseif (ismember(st_v_coh_z_interp_method, ["method_matrix", "method_mat_seg"]))
 160 
        
 161 
        clmt_val_wkb_interpolated = mt_val;
 162 
        
 163 
    end
 164 
        
 165 
    % Display Parameters
 166 
    if (bl_display_evf)
 167 
        fft_container_map_display(param_map);
 168 
        fft_container_map_display(support_map);
 169 
    end
 170 
    
< 0.001 
    152 
 171
end 
 172 

 173 
%% Parse Parameters
 174 

 175 
% armt_map
  0.005 
    152 
 176
params_group = values(armt_map, {'ar_z_r_borr_mesh_wage_w1r2', 'ar_z_wage_mesh_r_borr_w1r2'}); 
  0.002 
    152 
 177
[ar_z_r_borr_mesh_wage_w1r2, ar_z_wage_mesh_r_borr_w1r2] = params_group{:}; 
  0.004 
    152 
 178
params_group = values(armt_map, {'mt_z_trans', 'ar_ak_perc', 'ar_w_level', 'ar_k_mesha', 'ar_a_meshk', 'ar_aplusk_mesh'}); 
  0.004 
    152 
 179
[mt_z_trans, ar_ak_perc, ar_w_level, ar_k_mesha, ar_a_meshk, ar_aplusk_mesh] = params_group{:}; 
 180 

 181 
% param_map
  0.003 
    152 
 182
params_group = values(param_map, {'it_z_n', 'fl_z_r_borr_n', 'it_z_wage_n'}); 
  0.002 
    152 
 183
[it_z_n, fl_z_r_borr_n, it_z_wage_n] = params_group{:}; 
  0.003 
    152 
 184
params_group = values(param_map, {'fl_nan_replace', 'fl_b_bd'}); 
  0.002 
    152 
 185
[fl_nan_replace, fl_b_bd] = params_group{:}; 
  0.003 
    152 
 186
params_group = values(param_map, {'st_v_coh_z_interp_method'}); 
< 0.001 
    152 
 187
[st_v_coh_z_interp_method] = params_group{:}; 
 188 

 189 
% support_map
  0.003 
    152 
 190
params_group = values(support_map, {'bl_graph_onebyones','bl_display_evf', 'bl_graph_evf'}); 
  0.002 
    152 
 191
[bl_graph_onebyones, bl_display_evf, bl_graph_evf] = params_group{:}; 
  0.004 
    152 
 192
params_group = values(support_map, {'bl_img_save', 'st_img_path', 'st_img_prefix', 'st_img_name_main', 'st_img_suffix'}); 
  0.003 
    152 
 193
[bl_img_save, st_img_path, st_img_prefix, st_img_name_main, st_img_suffix] = params_group{:}; 
  0.003 
    152 
 194
params_group = values(support_map, {'it_display_summmat_rowmax', 'it_display_summmat_colmax'}); 
  0.002 
    152 
 195
[it_display_summmat_rowmax, it_display_summmat_colmax] = params_group{:}; 
 196 

 197 
% append function name
< 0.001 
    152 
 198
st_func_name = 'ff_ipwkbzr_evf'; 
  0.002 
    152 
 199
st_img_name_main = [st_func_name st_img_name_main]; 
 200 

 201 
%% Integrate *E(V(coh(k',b',zr),zw',zr')|zw,zr)*
 202 
% start with E(V(coh(k',b',zr),zw',zr')|zw,zr), integrate to find
 203 
% EV(k',b';zw,zr).
 204 
%
 205 
% Each column for a different state z, to integrate:
 206 
% *E(V(coh(k',b',zr),zw',zr')|zw,zr)*. Each column is a different shock,
 207 
% from the combinations of zw and zr shocks. Each row is a different unique
 208 
% level of reacheable cash-on-hand level, which is determined by the choice
 209 
% grid for w = k' + b', k' and b', as well as the borrowing shock vector
 210 
% zr. 
 211 
%
 212 
% The issue here is, unlike the productivity shock, where only the z'
 213 
% matters tomorrow, and z matters via conditional probability of p(z'|z),
 214 
% for the interest rate shock, both r and r' matter. For the z case, z'
 215 
% impacts the cash-on-hand, and z''. For r case, r impacts cash-on-hand
 216 
% tomorrow, since interest is known at the time when the loan is taken out,
 217 
% and r' also matters because it is the rate that decision maker next
 218 
% period faces when making b'' borrowing choices. 
 219 
%
 220 
% With the structure below, the interest rate r draw that households face
 221 
% today will impact the cash-on-hand tomorrow; the r' draw will impact
 222 
% tomorrow's value function through its effect on b'' choice; r impacts r'
 223 
% through conditional probability.
 224 
%
 225 
% Note that: mt_ev_condi_z = mt_val*mt_z_trans' work if we did not have the
 226 
% interest rate shock. With the interest rate shock, we have to proceed
 227 
% differently. 
 228 
%
 229 
% Note that mt_ev_condi_z rows are less by length of r rate shock times.
 230 
%
 231 
% # mt_val = (it_w_interp_n*it_ak_perc_n*length(fl_z_r_borr_n)) by (it_z_wage_n*length(fl_z_r_borr_n))
 232 
% # mt_ev_condi_z = (it_w_interp_n*it_ak_perc_n) by (it_z_wage_n*length(fl_z_r_borr_n))
 233 
%
 234 

 235 
% 1. Number of W/B/K Choice Combinations
< 0.001 
    152 
 236
it_ak_perc_n = length(ar_ak_perc); 
< 0.001 
    152 
 237
it_w_interp_n = length(ar_w_level); 
< 0.001 
    152 
 238
it_wak_n = it_w_interp_n*it_ak_perc_n; 
 239 

 240 
% 2. Initialize mt_ev_condi_z = E(V(coh(k',b',zr'),zw',zr')|zw,zr)
 241 
% rows = it_wak_n
 242 
% cols = it_z_n
  0.010 
    152 
 243
mt_ev_condi_z = zeros([it_wak_n, it_z_n]); 
 244 

< 0.001 
    152 
 245
for it_z_r_borr_ctr = 1:1:fl_z_r_borr_n 
 246 
    
 247 
    % Transition Row Subset: ((M^z) by (M^z x M^r))' for one m^r
< 0.001 
    760 
 248
    it_mt_z_trans_row_start = it_z_wage_n*(it_z_r_borr_ctr-1) + 1; 
< 0.001 
    760 
 249
    it_mt_z_trans_row_end = it_mt_z_trans_row_start + it_z_wage_n - 1;     
  0.016 
    760 
 250
    mt_z_trans_cur_z_r_borr = mt_z_trans(it_mt_z_trans_row_start:it_mt_z_trans_row_end, :); 
 251 
    
  0.149 
    760 
 252
    if (ismember(st_v_coh_z_interp_method, ["method_idx_a", "method_idx_b", "method_cell"])) 
 253 
        
  3.339 
    760 
 254
        mt_ev_condi_z(:, it_mt_z_trans_row_start:it_mt_z_trans_row_end) = ... 
    760 
 255
            clmt_val_wkb_interpolated{it_z_r_borr_ctr}*mt_z_trans_cur_z_r_borr'; 
 256 
        
 257 
    elseif (ismember(st_v_coh_z_interp_method, ["method_matrix", "method_mat_seg"]))
 258 
        
 259 
        % Val Segment : ((M^z) by (M^z x M^r))' for one m^r
 260 
        it_mt_val_row_start = it_wak_n*(it_z_r_borr_ctr-1) + 1;
 261 
        it_mt_val_row_end = it_mt_val_row_start + it_wak_n - 1;
 262 
        mt_val_cur_z_r_borr = clmt_val_wkb_interpolated(it_mt_val_row_start:it_mt_val_row_end, :);
 263 

 264 
        % EV(k',b';zw,zr) = E(V(coh(k',b',zr),zw',zr')|zw,zr) for one zr and all zw
 265 
        mt_ev_condi_z(:, it_mt_z_trans_row_start:it_mt_z_trans_row_end) = ...
 266 
            mt_val_cur_z_r_borr*mt_z_trans_cur_z_r_borr';
 267 
        
 268 
    end
 269 
    
  0.003 
    760 
 270
end 
 271 

< 0.001 
    152 
 272
if(bl_display_evf) 
 273 
    
 274 
    disp('----------------------------------------');
 275 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 276 
    disp('Expected Value: mt_ev_condi_z');
 277 
    disp("EV(k', b', zw, zr) = (V(coh(k',b',zr'),zw',zr')|zw,zr)");       
 278 
    disp("rows = k'/b' combos, cols = zw/zr combos");
 279 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 280 
    it_col_n_keep = it_z_wage_n*2;
 281 
    it_row_n_keep = it_ak_perc_n*3;
 282 
    [it_row_n, it_col_n] = size(mt_ev_condi_z);
 283 
    [ar_it_cols, ar_it_rows] = fft_row_col_subset(it_col_n, it_col_n_keep, it_row_n, it_row_n_keep);    
 284 
    cl_st_full_cols = cellstr([num2str(ar_z_r_borr_mesh_wage_w1r2', 'r%3.2f;'), ...
 285 
                               num2str(ar_z_wage_mesh_r_borr_w1r2', 'w%3.2f')]);
 286 
    cl_st_full_rows = cellstr([num2str(ar_aplusk_mesh, 'w%3.2f'), ...
 287 
                               num2str(ar_k_mesha, 'k%3.2f'),...
 288 
                               num2str(ar_a_meshk, 'a%3.2f')]);
 289 
    tb_mt_exp_val = array2table(round(mt_ev_condi_z(ar_it_rows, ar_it_cols),6));
 290 
    cl_col_names = strcat('i', num2str(ar_it_cols'), ':', cl_st_full_cols(ar_it_cols));
 291 
    cl_row_names = strcat('i', num2str(ar_it_rows'), ':', cl_st_full_rows(ar_it_rows));
 292 
    tb_mt_exp_val.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
 293 
    tb_mt_exp_val.Properties.RowNames = matlab.lang.makeValidName(cl_row_names);
 294 
       
 295 
    disp(size(mt_ev_condi_z));
 296 
    disp(tb_mt_exp_val(1:round(it_row_n_keep/2), :));
 297 
    disp(tb_mt_exp_val((round(it_row_n_keep/2)+1):it_row_n_keep, :));
 298 

 299 
end
 300 

 301 
%% Reshape *E(V(coh,z'|z,w))* to allow for maxing
 302 
% dim(mt_ev_condi_z): *IxJ by M*
 303 

  0.002 
    152 
 304
mt_ev_condi_z_full = reshape(mt_ev_condi_z, [it_ak_perc_n, it_w_interp_n*it_z_n]); 
 305 

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

  0.168 
    152 
 309
[ar_ev_condi_z_max, ar_ev_condi_z_max_idx] = max(mt_ev_condi_z_full); 
  0.002 
    152 
 310
mt_ev_condi_z_max = reshape(ar_ev_condi_z_max, [it_w_interp_n, it_z_n]); 
< 0.001 
    152 
 311
mt_ev_condi_z_max_idx = reshape(ar_ev_condi_z_max_idx, [it_w_interp_n, it_z_n]); 
 312 

< 0.001 
    152 
 313
if(bl_display_evf) 
 314 

 315 
    disp('----------------------------------------');
 316 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 317 
    disp('mt_ev_condi_z_full: J by IxM');
 318 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 319 
    disp(size(mt_ev_condi_z_full));
 320 
%     disp(head(array2table(mt_ev_condi_z_full), 20));
 321 
%     disp(tail(array2table(mt_ev_condi_z_full), 20));
 322 
        
 323 
    disp('----------------------------------------');
 324 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 325 
    disp('mt_ev_condi_z_max: I by M');
 326 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 327 
    it_col_n_keep = it_z_wage_n*2;    
 328 
    it_row_n_keep = round(it_w_interp_n);
 329 
    [it_row_n, it_col_n] = size(mt_ev_condi_z_max);
 330 
    [ar_it_cols, ar_it_rows] = fft_row_col_subset(it_col_n, it_col_n_keep, it_row_n, it_row_n_keep);    
 331 
    cl_st_full_cols = cellstr([num2str(ar_z_r_borr_mesh_wage_w1r2', 'r%3.2f;'), ...
 332 
                               num2str(ar_z_wage_mesh_r_borr_w1r2', 'w%3.2f')]);
 333 
    cl_st_full_rows = cellstr([num2str(ar_w_level', 'w%3.2f')]);
 334 
    tb_mt_ev_condi_z_max = array2table(round(mt_ev_condi_z_max(ar_it_rows, ar_it_cols), 6));
 335 
    cl_col_names = strcat('i', num2str(ar_it_cols'), ':', cl_st_full_cols(ar_it_cols));
 336 
    cl_row_names = strcat('i', num2str(ar_it_rows'), ':', cl_st_full_rows(ar_it_rows));
 337 
    tb_mt_ev_condi_z_max.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
 338 
    tb_mt_ev_condi_z_max.Properties.RowNames = matlab.lang.makeValidName(cl_row_names);    
 339 
    disp(size(mt_ev_condi_z_max));
 340 
    disp(tb_mt_ev_condi_z_max(1:round(it_row_n_keep/2), :));
 341 
    disp(tb_mt_ev_condi_z_max((round(it_row_n_keep/2)+1):it_row_n_keep, :));
 342 

 343 
    disp('----------------------------------------');
 344 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 345 
    disp('mt_ev_condi_z_max_idx: I by M');
 346 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 347 
    tb_mt_ev_condi_z_max_idx = array2table(mt_ev_condi_z_max_idx(ar_it_rows, ar_it_cols));
 348 
    tb_mt_ev_condi_z_max_idx.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
 349 
    tb_mt_ev_condi_z_max_idx.Properties.RowNames = matlab.lang.makeValidName(cl_row_names);    
 350 
    disp(size(mt_ev_condi_z_max_idx));
 351 
    disp(tb_mt_ev_condi_z_max_idx(1:round(it_row_n_keep/2), :));
 352 
    disp(tb_mt_ev_condi_z_max_idx((round(it_row_n_keep/2)+1):it_row_n_keep, :));
 353 
    
 354 
end
 355 

 356 
%% Reindex K' and B' Choices for each State at the Optimal *w'=k'+b'* choice
 357 
% The K' and B' Optimal Choices Associated with EV opti
 358 
% dim(mt_ev_condi_z_max_kp): *I by M*
  0.010 
    152 
 359
ar_add_grid = linspace(0, it_ak_perc_n*(it_w_interp_n-1), it_w_interp_n); 
  0.060 
    152 
 360
mt_ev_condi_z_max_idx = mt_ev_condi_z_max_idx + ar_add_grid'; 
 361 

  0.107 
    152 
 362
mt_ev_condi_z_max_kp = reshape(ar_k_mesha(mt_ev_condi_z_max_idx), [it_w_interp_n, it_z_n]); 
  0.102 
    152 
 363
mt_ev_condi_z_max_bp = reshape(ar_a_meshk(mt_ev_condi_z_max_idx), [it_w_interp_n, it_z_n]); 
 364 

< 0.001 
    152 
 365
if(bl_display_evf) 
 366 
    
 367 
    disp('----------------------------------------');
 368 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 369 
    disp('mt_ev_condi_z_max_kp: I by M');
 370 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 371 
    tb_ev_condi_z_max_kp = array2table(mt_ev_condi_z_max_kp(ar_it_rows, ar_it_cols));
 372 
    tb_ev_condi_z_max_kp.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
 373 
    tb_ev_condi_z_max_kp.Properties.RowNames = matlab.lang.makeValidName(cl_row_names);    
 374 
    disp(size(mt_ev_condi_z_max_kp));
 375 
    disp(tb_ev_condi_z_max_kp(1:round(it_row_n_keep/2), :));
 376 
    disp(tb_ev_condi_z_max_kp((round(it_row_n_keep/2)+1):it_row_n_keep, :));
 377 
    
 378 
    disp('----------------------------------------');
 379 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 380 
    disp('mt_ev_condi_z_max_bp: I by M');
 381 
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
 382 
    tb_ev_condi_z_max_bp = array2table(mt_ev_condi_z_max_bp(ar_it_rows, ar_it_cols));
 383 
    tb_ev_condi_z_max_bp.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
 384 
    tb_ev_condi_z_max_bp.Properties.RowNames = matlab.lang.makeValidName(cl_row_names);    
 385 
    disp(size(mt_ev_condi_z_max_kp));
 386 
    disp(tb_ev_condi_z_max_bp(1:round(it_row_n_keep/2), :));
 387 
    disp(tb_ev_condi_z_max_bp((round(it_row_n_keep/2)+1):it_row_n_keep, :));
 388 

 389 
    
 390 
end
 391 

 392 
%% Graph
 393 

< 0.001 
    152 
 394
if (bl_graph_evf) 
 395 
    
 396 
    %% Generate Limited Legends
 397 
    % 8 graph points, 2 levels of borrow rates, and 4 levels of rbr rates
 398 
    ar_it_z_r_borr = ([1 round((fl_z_r_borr_n)/2) (fl_z_r_borr_n)]);
 399 
    ar_it_z_wage = ([1 round((it_z_wage_n)/2) (it_z_wage_n)]);
 400 

 401 
    % combine by index
 402 
    mt_it_z_graph = ar_it_z_wage' + it_z_wage_n*(ar_it_z_r_borr-1);
 403 
    ar_it_z_graph = mt_it_z_graph(:)';
 404 

 405 
    % legends index final
 406 
    cl_st_legendCell = cellstr([num2str(ar_z_r_borr_mesh_wage_w1r2', 'zr=%3.2f;'), ...
 407 
                                num2str(ar_z_wage_mesh_r_borr_w1r2', 'zw=%3.2f')]);
 408 

 409 
    
 410 
    %% Graph 1, V and EV
 411 
%     if (~bl_graph_onebyones)
 412 
%         figure('PaperPosition', [0 0 14 4]);
 413 
%         hold on;
 414 
%     end
 415 
%     
 416 
%     
 417 
%     for subplot_j=1:1:2
 418 
%         
 419 
%         if (~bl_graph_onebyones)
 420 
%             hAxis(subplot_j) = subplot(1,2,subplot_j);
 421 
%         else
 422 
%             figure('PaperPosition', [0 0 7 4]);
 423 
%         end
 424 
%         
 425 
% %         cl_val_wkb_interpolated
 426 
%         if (subplot_j==1)
 427 
%             chart = plot(mt_val);
 428 
%         end
 429 
%         if (subplot_j==2)
 430 
%             chart = plot(mt_ev_condi_z);
 431 
%         end
 432 
%         
 433 
%         clr = jet(numel(chart));
 434 
%         for m = 1:numel(chart)
 435 
%             set(chart(m),'Color',clr(m,:))
 436 
%         end
 437 
%         
 438 
%         legend(chart(ar_it_z_graph), cl_st_legendCell(ar_it_z_graph), 'Location','southeast');
 439 
%         
 440 
%         if (subplot_j==1)
 441 
%             title('V(coh,zp); w(k+b),k,z');
 442 
%         end
 443 
%         if (subplot_j==2)
 444 
%             title('E_z(V(coh,zp|z))');
 445 
%         end
 446 
%         
 447 
%         ylabel('Next Period Value');
 448 
%         xlabel({'Index of Cash-on-Hand Discrete Point'...
 449 
%             'Each Segment is a w=k+b; within segment increasing k'...
 450 
%             'EV and V identical if shock is fully persistent'});
 451 
%         grid on;
 452 
%         grid minor;
 453 
%     end
 454 
%     
 455 
%     % Share y axis
 456 
%     if (~bl_graph_onebyones)
 457 
%         linkaxes(hAxis,'y');
 458 
%     end
 459 
%     
 460 
%     % save file
 461 
%     if (bl_img_save)
 462 
%         mkdir(support_map('st_img_path'));
 463 
%         st_file_name = [st_img_prefix st_img_name_main '_vev' st_img_suffix];
 464 
%         saveas(gcf, strcat(st_img_path, st_file_name));
 465 
%     end
 466 
    
 467 
    %% Graph 2, max(EV)
 468 
    
 469 
    if(~bl_graph_onebyones)
 470 
        figure('PaperPosition', [0 0 7 4]);
 471 
    end
 472 
    
 473 
    for sub_j=1:1:1
 474 
        
 475 
        if(sub_j==1)
 476 
            mt_outcome = mt_ev_condi_z_max;
 477 
            st_y_label = 'max_{k''}(E(V(coh(k'',b''=w-k''),z''|z,w))';
 478 
        end
 479 
        
 480 
        if(~bl_graph_onebyones)
 481 
            subplot(1,1,sub_j)
 482 
        else
 483 
            figure('PaperPosition', [0 0 7 4]);
 484 
        end
 485 
        hold on;
 486 
        
 487 
        clr = jet(length(ar_it_z_graph));
 488 
        i_ctr = 0;
 489 
        for i = ar_it_z_graph
 490 
            i_ctr = i_ctr + 1;
 491 
            ar_x = ar_w_level;
 492 
            ar_y = mt_outcome(:, i);
 493 
            scatter(ar_x, ar_y, 5, ...
 494 
                'MarkerEdgeColor', clr(i_ctr,:), ...
 495 
                'MarkerFaceColor', clr(i_ctr,:));
 496 
        end
 497 
        
 498 
        grid on;
 499 
        grid minor;
 500 
        title(['2nd Stage Exp Value at Optimal K given W=K''+B'''])
 501 
        ylabel(st_y_label)
 502 
        xlabel({'Aggregate Savings'})
 503 
                
 504 
        legendCell_here = cl_st_legendCell;
 505 
        legendCell_here{length(legendCell_here) + 1} = 'max-agg-save';
 506 
        legend(legendCell_here([ar_it_z_graph length(legendCell_here)]), 'Location','southeast');
 507 
        
 508 
        xline0 = xline(0);
 509 
        xline0.HandleVisibility = 'off';
 510 
        yline0 = yline(0);
 511 
        yline0.HandleVisibility = 'off';
 512 
        
 513 
    end
 514 
    
 515 
    % save file
 516 
    if (bl_img_save)
 517 
        mkdir(support_map('st_img_path'));
 518 
        st_file_name = [st_img_prefix st_img_name_main '_maxev' st_img_suffix];
 519 
        saveas(gcf, strcat(st_img_path, st_file_name));
 520 
    end
 521 
    
 522 
    %% Graph 3, at max(EV) optimal choice category, color regions, borrow save
 523 
    
 524 
    % Borrow Vs Save
 525 
    [ar_z_mw, ar_w_mz] = meshgrid(ar_z_wage_mesh_r_borr_w1r2, ar_w_level);
 526 
    mt_it_borr_idx = (mt_ev_condi_z_max_bp < 0);
 527 
    mt_it_riskyhalf_idx = ((mt_ev_condi_z_max_kp./mt_ev_condi_z_max_bp) > 0.5);
 528 
    mt_it_kzero_idx = (mt_ev_condi_z_max_kp == 0);
 529 
    mt_it_isnan_idx = (isnan(mt_ev_condi_z_max_kp));
 530 
    
 531 
    figure('PaperPosition', [0 0 7 4]);
 532 
    % States: ar_w, ar_z
 533 
    % Choices: mt_ev_condi_z_max_kp, mt_ev_condi_z_max_bp
 534 
    hold on;
 535 
    it_sca_size = 10;
 536 
    chart_br = scatter(ar_w_mz(mt_it_borr_idx),...
 537 
        ar_z_mw(mt_it_borr_idx),...
 538 
        it_sca_size, 'blue', 'filled');
 539 
    %     legend([chart_br], {'Borrow'}, 'Location','northeast');
 540 
    chart_khalf = scatter(ar_w_mz(~mt_it_borr_idx & mt_it_riskyhalf_idx),...
 541 
        ar_z_mw(~mt_it_borr_idx & mt_it_riskyhalf_idx),...
 542 
        it_sca_size, 'black', 'filled');
 543 
    %     legend([chart_khalf], {'Save >0.5 K'}, 'Location','northeast');
 544 
    chart_sv = scatter(ar_w_mz(~mt_it_borr_idx & ~mt_it_riskyhalf_idx),...
 545 
        ar_z_mw(~mt_it_borr_idx & ~mt_it_riskyhalf_idx),...
 546 
        it_sca_size, 'red', 'filled');
 547 
    %     legend([chart_sv], {'Save <0.5 K'}, 'Location','northeast');
 548 
    chart_invalid = scatter(ar_w_mz(mt_it_kzero_idx | mt_it_isnan_idx),...
 549 
        ar_z_mw(mt_it_kzero_idx | mt_it_isnan_idx),...
 550 
        it_sca_size, 'yellow', 'filled');
 551 
    legend([chart_br, chart_khalf, chart_sv, chart_invalid], ...
 552 
        {'Borrow','Save >0.5 K','Save <0.5 K', 'k=0 or k=nan'}, 'Location','northeast');
 553 
    title('Borrow and Save Regions')
 554 
    ylabel('Shocks')
 555 
    xlabel({'Total Savings w=k+b'})
 556 
    grid on;
 557 
    
 558 
    % save file
 559 
    if (bl_img_save)
 560 
        mkdir(support_map('st_img_path'));
 561 
        st_file_name = [st_img_prefix st_img_name_main '_maxbrsv' st_img_suffix];
 562 
        saveas(gcf, strcat(st_img_path, st_file_name));
 563 
    end
 564 
    
 565 
    %% Graph 4, Optimal K' and B' Levels
 566 
    
 567 
    [~, ar_w_mz] = meshgrid(ar_z_wage_mesh_r_borr_w1r2, ar_w_level);
 568 
    for sub_j=1:1:4
 569 
        
 570 
        if (bl_graph_onebyones)
 571 
            figure('PaperPosition', [0 0 7 4]);
 572 
        end
 573 
        
 574 
        if (sub_j==1)
 575 
            if(~bl_graph_onebyones)
 576 
                figure('PaperPosition', [0 0 14 4]);
 577 
                subplot(1,2,sub_j);
 578 
            end
 579 
            mt_y = mt_ev_condi_z_max_bp;
 580 
        end
 581 
        if (sub_j==2)
 582 
            if(~bl_graph_onebyones)
 583 
                subplot(1,2,sub_j);
 584 
            end
 585 
            
 586 
            mt_y = mt_ev_condi_z_max_kp;
 587 
        end
 588 
        if (sub_j==3)
 589 
            if(~bl_graph_onebyones)
 590 
                figure('PaperPosition', [0 0 14 4]);
 591 
                subplot(1,2,sub_j-2);
 592 
            end
 593 
            mt_y = zeros(size(mt_ev_condi_z_max_bp));
 594 
            mt_it_borr_idx = (mt_ev_condi_z_max_bp < 0);
 595 
            mt_y(mt_it_borr_idx) = -mt_ev_condi_z_max_bp(mt_it_borr_idx)/fl_b_bd;
 596 
            mt_y(~mt_it_borr_idx) = mt_ev_condi_z_max_bp(~mt_it_borr_idx)./ar_w_mz(~mt_it_borr_idx);
 597 
        end
 598 
        if (sub_j==4)
 599 
            if(~bl_graph_onebyones)
 600 
                subplot(1,2,sub_j-2);
 601 
            end
 602 
            mt_y = mt_ev_condi_z_max_kp./(ar_w_level'-fl_b_bd);
 603 
        end
 604 
        
 605 
        hold on;
 606 
        chart = plot(ar_w_level, mt_y);
 607 
        clr = jet(numel(chart));
 608 
        
 609 
        if (length(ar_w_level) <= 100)
 610 
            scatter(ar_w_mz(:), mt_y(:), 3, 'filled', 'MarkerEdgeColor', 'b', 'MarkerFaceColor', 'b');
 611 
        end
 612 
        
 613 
        for m = 1:numel(chart)
 614 
            set(chart(m),'Color',clr(m,:))
 615 
        end
 616 
        
 617 
        legend2plot = fliplr(ar_it_z_graph);
 618 
        legendCell = cl_st_legendCell;
 619 
        
 620 
        xline0 = xline(0);
 621 
        xline0.HandleVisibility = 'off';
 622 
        yline0 = yline(0);
 623 
        yline0.HandleVisibility = 'off';
 624 
        grid on;
 625 
        if (sub_j<=2)
 626 
            hline = refline([1 0]);
 627 
            hline.Color = 'k';
 628 
            hline.LineStyle = ':';
 629 
            hline.HandleVisibility = 'off';
 630 
        end
 631 
        
 632 
        if (sub_j==1)
 633 
            title('B Choices of W');
 634 
            ylabel('B Choices');
 635 
            xlabel({'Total Savings w=k+b'});
 636 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
 637 
        end
 638 
        if (sub_j==2)
 639 
            title('K Choices of W');
 640 
            ylabel('K Choices');
 641 
            xlabel({'Total Savings w=k+b'});
 642 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
 643 
        end
 644 
        
 645 
        if (sub_j==3)
 646 
            title('B Fraction of Borrow Max and Save');
 647 
            ylabel('B/bar(B) if br or B/W if sv');
 648 
            xlabel({'Total Savings w=k+b'});
 649 
            %             set(gca, 'YScale', 'log');
 650 
            ylim([-1.1 1.1]);
 651 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
 652 
        end
 653 
        if (sub_j==4)
 654 
            title('K Fraction Choices of Total K Possible');
 655 
            ylabel('K/(W-bar(b)) ');
 656 
            xlabel({'Total Savings w=k+b'});
 657 
            %             set(gca, 'YScale', 'log');
 658 
            ylim([0 1.1]);
 659 
            legend(chart(legend2plot), legendCell(legend2plot), 'Location','northeast');
 660 
        end
 661 
        
 662 
    end
 663 
    
 664 
    % save file
 665 
    if (bl_img_save)
 666 
        mkdir(support_map('st_img_path'));
 667 
        st_file_name = [st_img_prefix st_img_name_main '_wkbopti' st_img_suffix];
 668 
        saveas(gcf, strcat(st_img_path, st_file_name));
 669 
    end
 670 
    
 671 
end
 672 

 673 
%% Display Various Containers
 674 

< 0.001 
    152 
 675
if (bl_display_evf) 
 676 

 677 
    %% Display 1 support_map
 678 
    fft_container_map_display(support_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 679 
        
 680 
    %% Display 2 armt_map
 681 
    fft_container_map_display(armt_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 682 

 683 
    %% Display 3 param_map
 684 
    fft_container_map_display(param_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 685 
    
 686 
    %% Display 4 func_map
 687 
    fft_container_map_display(func_map, it_display_summmat_rowmax, it_display_summmat_colmax);
 688 
end
 689 

  0.399 
    152 
 690
end 

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