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_wkz_evf(varargin)
   | 
 |  |    8   | %% FF_WKZ_EVF solves the k' vs b' problem given aggregate savings
   | 
 |  |    9   | % Rather than solving the two asset problem in one shot. We can separate
   | 
 |  |   10   | % the problem into two stages. This is the second stage of the problem.
   | 
 |  |   11   | % Here we find the optimal k' choice given w=k'+b'.
   | 
 |  |   12   | % max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)) is the maximization problem.
  | 
 |  |   13   | % Conditional on shock and w. Note that this problem is not conditional on
   | 
 |  |   14   | % cash-on-hand today, but conditional on w. We do not need to know the
   | 
 |  |   15   | % cash-on-hand in the current period to find the optimal k' choice
   | 
 |  |   16   | % conditional on w.
   | 
 |  |   17   | %
   | 
 |  |   18   | % This means we can solve for the optimal relative allocation of risky and
   | 
 |  |   19   | % safe asset conditional on total savings. Note that we still need the
   | 
 |  |   20   | % value function *mt_val*, which is required to find EV.
   | 
 |  |   21   | %
   | 
 |  |   22   | % @param mt_val matrix state_n ((I-1)*J/2+I) by shock_n. This is the value
   | 
 |  |   23   | % matrix each row is a feasible reachable state given the choice
   | 
 |  |   24   | % vectors/matrix and each column is a shock state.
   | 
 |  |   25   | %
   | 
 |  |   26   | % @param param_map container parameter container
   | 
 |  |   27   | %
   | 
 |  |   28   | % @param support_map container support container
   | 
 |  |   29   | %
   | 
 |  |   30   | % @param armt_map container container with states, choices and shocks
   | 
 |  |   31   | % grids that are inputs for grid based solution algorithm
   | 
 |  |   32   | %
   | 
 |  |   33   | % @return mt_ev_condi_z_max matrix choice_w_n by shock_n
   | 
 |  |   34   | % max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)) conditional on z and w, at the
  | 
 |  |   35   | % optimal k' choice (w=k'+b') what is the expected utility? This is the
   | 
 |  |   36   | % value result from the 2nd stage problem. Note the result integrates over
   | 
 |  |   37   | % z'.
   | 
 |  |   38   | %
   | 
 |  |   39   | % @return mt_ev_condi_z_max_idx matrix choice_w_n by shock_n this is the
   | 
 |  |   40   | % argmax from max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w)). Given the vector of k'
  | 
 |  |   41   | % choices, which index maximized conditional on z and w integrating over
   | 
 |  |   42   | % z'/
   | 
 |  |   43   | %
   | 
 |  |   44   | % @return mt_ev_condi_z_max_kp matrix choice_w_n by shock_n the k' choice
   | 
 |  |   45   | % at max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))
  | 
 |  |   46   | %
   | 
 |  |   47   | % @return mt_ev_condi_z_max_bp matrix choice_w_n by shock_n the b'=w-k'
   | 
 |  |   48   | % choice at max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))
  | 
 |  |   49   | %
   | 
 |  |   50   | % @example
   | 
 |  |   51   | %
   | 
 |  |   52   | % @include
   | 
 |  |   53   | %
   | 
 |  |   54   | % * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_akz/paramfunc/ffs_akz_set_default_param.m ffs_akz_set_default_param>
   | 
 |  |   55   | % * <https://github.com/FanWangEcon/CodeDynaAsset/blob/master/m_akz/paramfunc/ffs_akz_get_funcgrid.m ffs_akz_get_funcgrid>
   | 
 |  |   56   | %
   | 
 |  |   57   | 
 
  | 
 |  |   58   | %% Default
   | 
 |  |   59   | 
 
  | 
< 0.001   |     101   |   60  | params_len = length(varargin); 
   | 
< 0.001   |     101   |   61  | bl_input_override = 0; 
   | 
< 0.001   |     101   |   62  | if (params_len == 5) 
   | 
< 0.001   |     101   |   63  |     bl_input_override = varargin{5}; 
  | 
< 0.001   |     101   |   64  | end 
   | 
< 0.001   |     101   |   65  | if (bl_input_override) 
   | 
 |  |   66   |     % override when called from outside
   | 
  0.004   |     101   |   67  |     [mt_val, param_map, support_map, armt_map, ~] = varargin{:}; 
  | 
 |  |   68   | else
   | 
 |  |   69   |     clear all;
   | 
 |  |   70   |     close all;
   | 
 |  |   71   | 
 
  | 
 |  |   72   |     % Not default parameters, but parameters that generate defaults
   | 
 |  |   73   |     it_param_set = 4;
   | 
 |  |   74   |     bl_input_override = true;
   | 
 |  |   75   |     [param_map, support_map] = ffs_akz_set_default_param(it_param_set);
   | 
 |  |   76   |     support_map('bl_graph_evf') = true;
  | 
 |  |   77   |     support_map('bl_display_evf') = true;
  | 
 |  |   78   | 
 
  | 
 |  |   79   |     [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
   | 
 |  |   80   | 
 
  | 
 |  |   81   |     % Generating Defaults
   | 
 |  |   82   |     params_group = values(armt_map, {'ar_a_meshk', 'ar_k_mesha', 'ar_z'});
  | 
 |  |   83   |     [ar_a_meshk, ar_k_mesha, ar_z] = params_group{:};
  | 
 |  |   84   |     params_group = values(func_map, {'f_util_standin'});
  | 
 |  |   85   |     [f_util_standin] = params_group{:};
  | 
 |  |   86   |     mt_val = f_util_standin(ar_z, ar_a_meshk, ar_k_mesha);
   | 
 |  |   87   |     
   | 
< 0.001   |     101   |   88  | end 
   | 
 |  |   89   | 
 
  | 
 |  |   90   | %% Parse Parameters
   | 
 |  |   91   | 
 
  | 
  0.006   |     101   |   92  | params_group = values(armt_map, {'mt_z_trans', 'ar_z',... 
  | 
 |  |   93   |     'ar_w', 'mt_k_wth_na', 'ar_k_mw_wth_na', 'ar_a_mw_wth_na'});
   | 
  0.002   |     101   |   94  | [mt_z_trans, ar_z, ar_w, mt_k_wth_na, ar_k_mw_wth_na, ar_a_mw_wth_na] = params_group{:}; 
  | 
  0.002   |     101   |   95  | params_group = values(param_map, {'it_z_n', 'fl_nan_replace', 'fl_b_bd'}); 
  | 
  0.001   |     101   |   96  | [it_z_n, fl_nan_replace, fl_b_bd] = params_group{:}; 
  | 
  0.002   |     101   |   97  | params_group = values(support_map, {'bl_graph_onebyones','bl_display_evf', 'bl_graph_evf'}); 
  | 
  0.001   |     101   |   98  | [bl_graph_onebyones, bl_display_evf, bl_graph_evf] = params_group{:}; 
  | 
  0.002   |     101   |   99  | params_group = values(support_map, {'bl_img_save', 'st_img_path', 'st_img_prefix', 'st_img_name_main', 'st_img_suffix'}); 
  | 
  0.002   |     101   |  100  | [bl_img_save, st_img_path, st_img_prefix, st_img_name_main, st_img_suffix] = params_group{:}; 
  | 
 |  |  101   | 
 
  | 
 |  |  102   | % append function name
   | 
< 0.001   |     101   |  103  | st_func_name = 'ff_wkz_evf'; 
   | 
< 0.001   |     101   |  104  | st_img_name_main = [st_func_name st_img_name_main]; 
   | 
 |  |  105   | 
 
  | 
 |  |  106   | %% Integrate *E(V(coh(k',b'), z')|z, w)*
   | 
 |  |  107   | % Each column for a different state z, each value *E(V(coh,z')|z)* integrated already
   | 
 |  |  108   | % Here, each column is a current z, more to right higher EV
   | 
 |  |  109   | % dim(mt_ev_condi_z): *Q by M*
   | 
 |  |  110   | % Note that: mt_ev_condi_z = mt_val*mt_z_trans' is a mistake, that would be
   | 
 |  |  111   | % what we do in the
   | 
 |  |  112   | % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_functions.html
   | 
 |  |  113   | % ffs_akz_set_functions> code where we loop over current z, and for each
   | 
 |  |  114   | % current z, grab out a particular row from the mt_z_trans that corresponds
   | 
 |  |  115   | % to a current shock's transition into all future states.
   | 
 |  |  116   | %
   | 
 |  |  117   | % here, each column of mt_val corresponds to a state z, think of that as
   | 
 |  |  118   | % future state z. The input mt_val is *V(coh, z)*, we need to integrate to
   | 
 |  |  119   | % get *E(V(coh,z')|z)*.
   | 
 |  |  120   | %
   | 
 |  |  121   | 
 
  | 
  0.016   |     101   |  122  | mt_ev_condi_z = mt_val*mt_z_trans'; 
   | 
< 0.001   |     101   |  123  | if (bl_display_evf) 
   | 
 |  |  124   |     disp('mt_ev_condi_z: Q by M');
  | 
 |  |  125   |     disp(size(mt_ev_condi_z));
   | 
 |  |  126   |     summary(array2table(mt_ev_condi_z));
   | 
 |  |  127   | end
   | 
 |  |  128   | 
 
  | 
 |  |  129   | %% Reshape *E(V(coh,z'|z,w))* to allow for maxing
   | 
 |  |  130   | % dim(mt_ev_condi_z): *IxJ by M*
   | 
 |  |  131   | 
 
  | 
  0.002   |     101   |  132  | [it_mt_bp_rown, it_mt_bp_coln] = size(mt_k_wth_na); 
   | 
  0.009   |     101   |  133  | mt_ev_condi_z_full = zeros(it_mt_bp_rown*it_mt_bp_coln, it_z_n); 
   | 
  0.004   |     101   |  134  | mt_ev_condi_z_full(~isnan(ar_k_mw_wth_na),:) = mt_ev_condi_z; 
   | 
  0.003   |     101   |  135  | mt_ev_condi_z_full(isnan(ar_k_mw_wth_na),:) = fl_nan_replace; 
   | 
< 0.001   |     101   |  136  | mt_ev_condi_z_full = reshape(mt_ev_condi_z_full, [it_mt_bp_rown, it_mt_bp_coln*it_z_n]); 
   | 
 |  |  137   | 
 
  | 
 |  |  138   | %% Maximize *max_{k'}(E(V(coh(k',b'=w-k'),z'|z,w))* optimal value and index
  | 
 |  |  139   | % Maximization, find optimal k'/b' combination given z and w=k'+b'
   | 
 |  |  140   | 
 
  | 
  0.006   |     101   |  141  | [ar_ev_condi_z_max, ar_ev_condi_z_max_idx] = max(mt_ev_condi_z_full); 
   | 
< 0.001   |     101   |  142  | mt_ev_condi_z_max = reshape(ar_ev_condi_z_max, [it_mt_bp_coln, it_z_n]); 
   | 
< 0.001   |     101   |  143  | mt_ev_condi_z_max_idx = reshape(ar_ev_condi_z_max_idx, [it_mt_bp_coln, it_z_n]); 
   | 
< 0.001   |     101   |  144  | if (bl_display_evf) 
   | 
 |  |  145   | 
 
  | 
 |  |  146   |     disp('----------------------------------------');
  | 
 |  |  147   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  148   |     disp('mt_ev_condi_z_full: J by IxM');
  | 
 |  |  149   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  150   |     disp(size(mt_ev_condi_z_full));
   | 
 |  |  151   | %     disp(head(array2table(mt_ev_condi_z_full), 20));
   | 
 |  |  152   | %     disp(tail(array2table(mt_ev_condi_z_full), 20));
   | 
 |  |  153   |         
   | 
 |  |  154   |     disp('----------------------------------------');
  | 
 |  |  155   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  156   |     disp('mt_ev_condi_z_max: I by M');
  | 
 |  |  157   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  158   |     disp(size(mt_ev_condi_z_max));
   | 
 |  |  159   |     disp(head(array2table(mt_ev_condi_z_max), 20));
   | 
 |  |  160   |     disp(tail(array2table(mt_ev_condi_z_max), 20));
   | 
 |  |  161   |     
   | 
 |  |  162   | 
 
  | 
 |  |  163   |     disp('----------------------------------------');
  | 
 |  |  164   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  165   |     disp('mt_ev_condi_z_max_idx: I by M');
  | 
 |  |  166   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  167   |     disp(size(mt_ev_condi_z_max_idx));
   | 
 |  |  168   |     disp(head(array2table(mt_ev_condi_z_max_idx), 20));
   | 
 |  |  169   |     disp(tail(array2table(mt_ev_condi_z_max_idx), 20));
   | 
 |  |  170   |     
   | 
 |  |  171   | end
   | 
 |  |  172   | 
 
  | 
 |  |  173   | %% Reindex K' and B' Choices for each State at the Optimal *w'=k'+b'* choice
   | 
 |  |  174   | % The K' and B' Optimal Choices Associated with EV opti
   | 
 |  |  175   | % dim(mt_ev_condi_z_max_kp): *I by M*
   | 
 |  |  176   | 
 
  | 
  0.005   |     101   |  177  | ar_add_grid = linspace(0, it_mt_bp_rown*(it_mt_bp_coln-1), it_mt_bp_coln); 
   | 
  0.001   |     101   |  178  | mt_ev_condi_z_max_idx = mt_ev_condi_z_max_idx + ar_add_grid'; 
   | 
< 0.001   |     101   |  179  | if (bl_display_evf) 
   | 
 |  |  180   |     disp('----------------------------------------');
  | 
 |  |  181   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  182   |     disp('mt_ev_condi_z_max_idx: I by M');
  | 
 |  |  183   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  184   |     disp(size(mt_ev_condi_z_max_idx));
   | 
 |  |  185   |     disp(head(array2table(mt_ev_condi_z_max_idx), 20));
   | 
 |  |  186   |     disp(tail(array2table(mt_ev_condi_z_max_idx), 20));    
   | 
 |  |  187   | end
   | 
  0.002   |     101   |  188  | mt_ev_condi_z_max_kp = reshape(ar_k_mw_wth_na(mt_ev_condi_z_max_idx(:)), [it_mt_bp_coln, it_z_n]); 
   | 
  0.002   |     101   |  189  | mt_ev_condi_z_max_bp = reshape(ar_a_mw_wth_na(mt_ev_condi_z_max_idx(:)), [it_mt_bp_coln, it_z_n]); 
   | 
< 0.001   |     101   |  190  | if (bl_display_evf) 
   | 
 |  |  191   |     disp('----------------------------------------');
  | 
 |  |  192   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  193   |     disp('mt_ev_condi_z_max_kp: I by M');
  | 
 |  |  194   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  195   |     disp(size(mt_ev_condi_z_max_kp));
   | 
 |  |  196   |     disp(head(array2table(mt_ev_condi_z_max_kp), 20));
   | 
 |  |  197   |     disp(tail(array2table(mt_ev_condi_z_max_kp), 20));        
   | 
 |  |  198   |     
   | 
 |  |  199   |     disp('----------------------------------------');
  | 
 |  |  200   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  201   |     disp('mt_ev_condi_z_max_bp: I by M');
  | 
 |  |  202   |     disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  | 
 |  |  203   |     disp(size(mt_ev_condi_z_max_bp));
   | 
 |  |  204   |     disp(head(array2table(mt_ev_condi_z_max_bp), 20));
   | 
 |  |  205   |     disp(tail(array2table(mt_ev_condi_z_max_bp), 20));    
   | 
 |  |  206   | end
   | 
 |  |  207   | 
 
  | 
 |  |  208   | %% Generate Optimal K' and B' on w(coh) percentage grid from w level grid
   | 
 |  |  209   | 
 
  | 
 |  |  210   | %% Graph
   | 
 |  |  211   | 
 
  | 
< 0.001   |     101   |  212  | if (bl_graph_evf) 
   | 
 |  |  213   | 
 
  | 
 |  |  214   |     %% Graph 1, V and EV
   | 
 |  |  215   |     if (~bl_graph_onebyones)
   | 
 |  |  216   |         figure('PaperPosition', [0 0 14 4]);
  | 
 |  |  217   |         hold on;
   | 
 |  |  218   |     end
   | 
 |  |  219   | 
 
  | 
 |  |  220   | 
 
  | 
 |  |  221   |     for subplot_j=1:1:2
   | 
 |  |  222   | 
 
  | 
 |  |  223   |         if (~bl_graph_onebyones)
   | 
 |  |  224   |             hAxis(subplot_j) = subplot(1,2,subplot_j);
   | 
 |  |  225   |         else
   | 
 |  |  226   |             figure('PaperPosition', [0 0 7 4]);
  | 
 |  |  227   |         end
   | 
 |  |  228   | 
 
  | 
 |  |  229   |         if (subplot_j==1)
   | 
 |  |  230   |             chart = plot(mt_val);
   | 
 |  |  231   |         end
   | 
 |  |  232   |         if (subplot_j==2)
   | 
 |  |  233   |             chart = plot(mt_ev_condi_z);
   | 
 |  |  234   |         end
   | 
 |  |  235   | 
 
  | 
 |  |  236   |         clr = jet(numel(chart));
   | 
 |  |  237   |         for m = 1:numel(chart)
   | 
 |  |  238   |             set(chart(m),'Color',clr(m,:))
   | 
 |  |  239   |         end
   | 
 |  |  240   | 
 
  | 
 |  |  241   |         legend2plot = fliplr([1 round(numel(chart)/3) round((2*numel(chart))/3)  numel(chart)]);
   | 
 |  |  242   |         legendCell = cellstr(num2str(ar_z', 'shock=%3.2f'));
   | 
 |  |  243   |         legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
   | 
 |  |  244   | 
 
  | 
 |  |  245   |         if (subplot_j==1)
   | 
 |  |  246   |             title('V(coh,zp); w(k+b),k,z');
  | 
 |  |  247   |         end
   | 
 |  |  248   |         if (subplot_j==2)
   | 
 |  |  249   |             title('E_z(V(coh,zp|z))');
  | 
 |  |  250   |         end
   | 
 |  |  251   | 
 
  | 
 |  |  252   |         ylabel('Next Period Value');
  | 
 |  |  253   |         xlabel({'Index of Cash-on-Hand Discrete Point'...
  | 
 |  |  254   |                 'Each Segment is a w=k+b; within segment increasing k'...
   | 
 |  |  255   |                 'EV and V identical if shock is fully persistent'});
   | 
 |  |  256   |         grid on;
   | 
 |  |  257   |         grid minor;
   | 
 |  |  258   |     end
   | 
 |  |  259   | 
 
  | 
 |  |  260   |     % Share y axis
   | 
 |  |  261   |     if (~bl_graph_onebyones)
   | 
 |  |  262   |         linkaxes(hAxis,'y');
   | 
 |  |  263   |     end
   | 
 |  |  264   | 
 
  | 
 |  |  265   |     % save file
   | 
 |  |  266   |     if (bl_img_save)
   | 
 |  |  267   |         mkdir(support_map('st_img_path'));
  | 
 |  |  268   |         st_file_name = [st_img_prefix st_img_name_main '_vev' st_img_suffix];
   | 
 |  |  269   |         saveas(gcf, strcat(st_img_path, st_file_name));
   | 
 |  |  270   |     end
   | 
 |  |  271   | 
 
  | 
 |  |  272   |     %% Graph 2, max(EV)
   | 
 |  |  273   | 
 
  | 
 |  |  274   |     if (~bl_graph_onebyones)
   | 
 |  |  275   |         figure('PaperPosition', [0 0 7 4]);
  | 
 |  |  276   |     end
   | 
 |  |  277   | 
 
  | 
 |  |  278   |     for sub_j=1:1:1
   | 
 |  |  279   | 
 
  | 
 |  |  280   |         if (sub_j==1)
   | 
 |  |  281   |             mt_outcome = mt_ev_condi_z_max;
   | 
 |  |  282   |             st_y_label = 'max_{k''}(E(V(coh(k'',b''=w-k''),z''|z,w))';
  | 
 |  |  283   |         end
   | 
 |  |  284   | 
 
  | 
 |  |  285   |         if (~bl_graph_onebyones)
   | 
 |  |  286   |             subplot(1,1,sub_j)
   | 
 |  |  287   |         else
   | 
 |  |  288   |             figure('PaperPosition', [0 0 7 4]);
  | 
 |  |  289   |         end
   | 
 |  |  290   |         hold on;
   | 
 |  |  291   | 
 
  | 
 |  |  292   |         ar_it_z_graph = ([1 round((it_z_n)/4) round(2*((it_z_n)/4)) round(3*((it_z_n)/4)) (it_z_n)]);
   | 
 |  |  293   |         clr = jet(length(ar_it_z_graph));
   | 
 |  |  294   |         i_ctr = 0;
   | 
 |  |  295   |         for i = ar_it_z_graph
   | 
 |  |  296   |             i_ctr = i_ctr + 1;
   | 
 |  |  297   |             ar_x = ar_w;
   | 
 |  |  298   |             ar_y = mt_outcome(:, i);
   | 
 |  |  299   |             scatter(ar_x, ar_y, 5, ...
   | 
 |  |  300   |                 'MarkerEdgeColor', clr(i_ctr,:), ...
   | 
 |  |  301   |                 'MarkerFaceColor', clr(i_ctr,:));
   | 
 |  |  302   |         end
   | 
 |  |  303   | 
 
  | 
 |  |  304   |         grid on;
   | 
 |  |  305   |         grid minor;
   | 
 |  |  306   |         title(['2nd Stage Exp Value at Optimal K given W=K''+B'''])
   | 
 |  |  307   |         ylabel(st_y_label)
   | 
 |  |  308   |         xlabel({'Aggregate Savings'})
  | 
 |  |  309   | 
 
  | 
 |  |  310   |         legendCell = cellstr(num2str(ar_z', 'shock=%3.2f'));
   | 
 |  |  311   |         legendCell{length(legendCell) + 1} = 'max-agg-save';
  | 
 |  |  312   |         legend(legendCell([ar_it_z_graph length(legendCell)]), 'Location','southeast');
   | 
 |  |  313   | 
 
  | 
 |  |  314   |         xline0 = xline(0);
   | 
 |  |  315   |         xline0.HandleVisibility = 'off';
   | 
 |  |  316   |         yline0 = yline(0);
   | 
 |  |  317   |         yline0.HandleVisibility = 'off';
   | 
 |  |  318   | 
 
  | 
 |  |  319   |     end
   | 
 |  |  320   | 
 
  | 
 |  |  321   |     % save file
   | 
 |  |  322   |     if (bl_img_save)
   | 
 |  |  323   |         mkdir(support_map('st_img_path'));
  | 
 |  |  324   |         st_file_name = [st_img_prefix st_img_name_main '_maxev' st_img_suffix];
   | 
 |  |  325   |         saveas(gcf, strcat(st_img_path, st_file_name));
   | 
 |  |  326   |     end
   | 
 |  |  327   | 
 
  | 
 |  |  328   |     %% Graph 3, max(EV), color regions, borrow save
   | 
 |  |  329   | 
 
  | 
 |  |  330   |     % Borrow Vs Save
   | 
 |  |  331   |     [ar_z_mw, ar_w_mz] = meshgrid(ar_z, ar_w);
   | 
 |  |  332   |     mt_it_borr_idx = (mt_ev_condi_z_max_bp < 0);
   | 
 |  |  333   |     mt_it_riskyhalf_idx = ((mt_ev_condi_z_max_kp./mt_ev_condi_z_max_bp) > 0.5);
   | 
 |  |  334   |     mt_it_kzero_idx = (mt_ev_condi_z_max_kp == 0);
   | 
 |  |  335   |     mt_it_isnan_idx = (isnan(mt_ev_condi_z_max_kp));
   | 
 |  |  336   | 
 
  | 
 |  |  337   |     figure('PaperPosition', [0 0 7 4]);
  | 
 |  |  338   |     % States: ar_w, ar_z
   | 
 |  |  339   |     % Choices: mt_ev_condi_z_max_kp, mt_ev_condi_z_max_bp
   | 
 |  |  340   |     hold on;
   | 
 |  |  341   |     it_sca_size = 10;
   | 
 |  |  342   |     chart_br = scatter(ar_w_mz(mt_it_borr_idx),...
   | 
 |  |  343   |         ar_z_mw(mt_it_borr_idx),...
   | 
 |  |  344   |         it_sca_size, 'blue', 'filled');
   | 
 |  |  345   |     %     legend([chart_br], {'Borrow'}, 'Location','northeast');
  | 
 |  |  346   |     chart_khalf = scatter(ar_w_mz(~mt_it_borr_idx & mt_it_riskyhalf_idx),...
   | 
 |  |  347   |         ar_z_mw(~mt_it_borr_idx & mt_it_riskyhalf_idx),...
   | 
 |  |  348   |         it_sca_size, 'black', 'filled');
   | 
 |  |  349   |     %     legend([chart_khalf], {'Save >0.5 K'}, 'Location','northeast');
  | 
 |  |  350   |     chart_sv = scatter(ar_w_mz(~mt_it_borr_idx & ~mt_it_riskyhalf_idx),...
   | 
 |  |  351   |         ar_z_mw(~mt_it_borr_idx & ~mt_it_riskyhalf_idx),...
   | 
 |  |  352   |         it_sca_size, 'red', 'filled');
   | 
 |  |  353   |     %     legend([chart_sv], {'Save <0.5 K'}, 'Location','northeast');
  | 
 |  |  354   |     chart_invalid = scatter(ar_w_mz(mt_it_kzero_idx | mt_it_isnan_idx),...
   | 
 |  |  355   |         ar_z_mw(mt_it_kzero_idx | mt_it_isnan_idx),...
   | 
 |  |  356   |         it_sca_size, 'yellow', 'filled');
   | 
 |  |  357   |     legend([chart_br, chart_khalf, chart_sv, chart_invalid], ...
   | 
 |  |  358   |         {'Borrow','Save >0.5 K','Save <0.5 K', 'k=0 or k=nan'}, 'Location','northeast');
  | 
 |  |  359   |     title('Borrow and Save Regions')
  | 
 |  |  360   |     ylabel('Shocks')
  | 
 |  |  361   |     xlabel({'Total Savings w=k+b'})
  | 
 |  |  362   |     grid on;
   | 
 |  |  363   | 
 
  | 
 |  |  364   |     % save file
   | 
 |  |  365   |     if (bl_img_save)
   | 
 |  |  366   |         mkdir(support_map('st_img_path'));
  | 
 |  |  367   |         st_file_name = [st_img_prefix st_img_name_main '_maxbrsv' st_img_suffix];
   | 
 |  |  368   |         saveas(gcf, strcat(st_img_path, st_file_name));
   | 
 |  |  369   |     end
   | 
 |  |  370   | 
 
  | 
 |  |  371   |     %% Graph 4, Optimal K' and B' Levels
   | 
 |  |  372   | 
 
  | 
 |  |  373   |     [~, ar_w_mz] = meshgrid(ar_z, ar_w);
   | 
 |  |  374   |     for sub_j=1:1:4
   | 
 |  |  375   | 
 
  | 
 |  |  376   |         if (bl_graph_onebyones)
   | 
 |  |  377   |             figure('PaperPosition', [0 0 7 4]);
  | 
 |  |  378   |         end
   | 
 |  |  379   | 
 
  | 
 |  |  380   |         if (sub_j==1)
   | 
 |  |  381   |             if (~bl_graph_onebyones)
   | 
 |  |  382   |                 figure('PaperPosition', [0 0 14 4]);
  | 
 |  |  383   |                 subplot(1,2,sub_j);
   | 
 |  |  384   |             end
   | 
 |  |  385   |             mt_y = mt_ev_condi_z_max_bp;
   | 
 |  |  386   |         end
   | 
 |  |  387   |         if (sub_j==2)
   | 
 |  |  388   |             if (~bl_graph_onebyones)
   | 
 |  |  389   |                 subplot(1,2,sub_j);
   | 
 |  |  390   |             end
   | 
 |  |  391   | 
 
  | 
 |  |  392   |             mt_y = mt_ev_condi_z_max_kp;
   | 
 |  |  393   |         end
   | 
 |  |  394   |         if (sub_j==3)
   | 
 |  |  395   |             if (~bl_graph_onebyones)
   | 
 |  |  396   |                 figure('PaperPosition', [0 0 14 4]);
  | 
 |  |  397   |                 subplot(1,2,sub_j-2);
   | 
 |  |  398   |             end
   | 
 |  |  399   |             mt_y = zeros(size(mt_ev_condi_z_max_bp));
   | 
 |  |  400   |             mt_it_borr_idx = (mt_ev_condi_z_max_bp < 0);
   | 
 |  |  401   |             mt_y(mt_it_borr_idx) = -mt_ev_condi_z_max_bp(mt_it_borr_idx)/fl_b_bd;
   | 
 |  |  402   |             mt_y(~mt_it_borr_idx) = mt_ev_condi_z_max_bp(~mt_it_borr_idx)./ar_w_mz(~mt_it_borr_idx);
   | 
 |  |  403   |         end
   | 
 |  |  404   |         if (sub_j==4)
   | 
 |  |  405   |             if (~bl_graph_onebyones)
   | 
 |  |  406   |                 subplot(1,2,sub_j-2);
   | 
 |  |  407   |             end
   | 
 |  |  408   |             mt_y = mt_ev_condi_z_max_kp./(ar_w'-fl_b_bd);
   | 
 |  |  409   |         end
   | 
 |  |  410   | 
 
  | 
 |  |  411   |         hold on;
   | 
 |  |  412   |         chart = plot(ar_w, mt_y);
   | 
 |  |  413   |         clr = jet(numel(chart));
   | 
 |  |  414   | 
 
  | 
 |  |  415   |         if (length(ar_w) <= 50)
   | 
 |  |  416   |             scatter(ar_w_mz(:), mt_y(:), 5, 'filled');
   | 
 |  |  417   |         end
   | 
 |  |  418   | 
 
  | 
 |  |  419   |         for m = 1:numel(chart)
   | 
 |  |  420   |             set(chart(m),'Color',clr(m,:))
   | 
 |  |  421   |         end
   | 
 |  |  422   |         legend2plot = fliplr([1 round(numel(chart)/3) round((2*numel(chart))/3)  numel(chart)]);
   | 
 |  |  423   |         legendCell = cellstr(num2str(ar_z', 'shock=%3.2f'));
   | 
 |  |  424   | 
 
  | 
 |  |  425   |         xline0 = xline(0);
   | 
 |  |  426   |         xline0.HandleVisibility = 'off';
   | 
 |  |  427   |         yline0 = yline(0);
   | 
 |  |  428   |         yline0.HandleVisibility = 'off';
   | 
 |  |  429   |         grid on;
   | 
 |  |  430   |         if (sub_j<=2)
   | 
 |  |  431   |             hline = refline([1 0]);
   | 
 |  |  432   |             hline.Color = 'k';
   | 
 |  |  433   |             hline.LineStyle = ':';
   | 
 |  |  434   |             hline.HandleVisibility = 'off';
   | 
 |  |  435   |         end
   | 
 |  |  436   | 
 
  | 
 |  |  437   |         if (sub_j==1)
   | 
 |  |  438   |             title('B Choices of W');
  | 
 |  |  439   |             ylabel('B Choices');
  | 
 |  |  440   |             xlabel({'Total Savings w=k+b'});
  | 
 |  |  441   |             legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
   | 
 |  |  442   |         end
   | 
 |  |  443   |         if (sub_j==2)
   | 
 |  |  444   |             title('K Choices of W');
  | 
 |  |  445   |             ylabel('K Choices');
  | 
 |  |  446   |             xlabel({'Total Savings w=k+b'});
  | 
 |  |  447   |             legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
   | 
 |  |  448   |         end
   | 
 |  |  449   | 
 
  | 
 |  |  450   |         if (sub_j==3)
   | 
 |  |  451   |             title('B Fraction of Borrow Max and Save');
  | 
 |  |  452   |             ylabel('B/bar(B) if br or B/W if sv');
  | 
 |  |  453   |             xlabel({'Total Savings w=k+b'});
  | 
 |  |  454   |             %             set(gca, 'YScale', 'log');
   | 
 |  |  455   |             ylim([-1.1 1.1]);
   | 
 |  |  456   |             legend(chart(legend2plot), legendCell(legend2plot), 'Location','northwest');
   | 
 |  |  457   |         end
   | 
 |  |  458   |         if (sub_j==4)
   | 
 |  |  459   |             title('K Fraction Choices of Total K Possible');
  | 
 |  |  460   |             ylabel('K/(W-bar(b)) ');
  | 
 |  |  461   |             xlabel({'Total Savings w=k+b'});
  | 
 |  |  462   |             %             set(gca, 'YScale', 'log');
   | 
 |  |  463   |             ylim([0 1.1]);
   | 
 |  |  464   |             legend(chart(legend2plot), legendCell(legend2plot), 'Location','northeast');
   | 
 |  |  465   |         end
   | 
 |  |  466   | 
 
  | 
 |  |  467   |     end
   | 
 |  |  468   | 
 
  | 
 |  |  469   |     % save file
   | 
 |  |  470   |     if (bl_img_save)
   | 
 |  |  471   |         mkdir(support_map('st_img_path'));
  | 
 |  |  472   |         st_file_name = [st_img_prefix st_img_name_main '_wkbopti' st_img_suffix];
   | 
 |  |  473   |         saveas(gcf, strcat(st_img_path, st_file_name));
   | 
 |  |  474   |     end
   | 
 |  |  475   | 
 
  | 
 |  |  476   | end
   | 
 |  |  477   | 
 
  | 
  0.009   |     101   |  478  | end 
   | 
Other subfunctions in this file are not included in this listing.