This is a static copy of a profile report

Home

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

Home

ffs_fibs_min_c_cost_bridge (Calls: 11580, Time: 1.239 s)
Generated 15-Jul-2019 20:47:30 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_fibs\paramfunc_fibs\ffs_fibs_min_c_cost_bridge.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ff_ipwkbz_fibs_vf_vecsvfunction11580
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
206
fl_aprime_nobridge, bl_display...
41740.452 s36.5%
139
params_group = values(armt_map...
115800.096 s7.8%
75
[fl_ap, fl_coh, param_map, sup...
115800.091 s7.4%
144
params_group = values(param_ma...
115800.087 s7.0%
150
params_group = values(func_map...
115800.080 s6.5%
All other lines  0.432 s34.8%
Totals  1.239 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ffs_fibs_min_c_costfunction41740.442 s35.7%
ffs_fibs_inf_bridgefunction30000.036 s2.9%
dealfunction74060.015 s1.2%
...et_functions>@(b)(b.*(1+fl_r_fsv))anonymous function74060.010 s0.8%
...+fl_r_inf)+ar_for_save.*(1+fl_r_fsv))anonymous function41740.006 s0.5%
Self time (built-ins, overhead, etc.)  0.730 s58.9%
Totals  1.239 s100% 
Code Analyzer results
Line numberMessage
84The value assigned to variable 'bl_input_override' might be unused.
118The value assigned to variable 'fl_ap' might be unused.
119The value assigned to variable 'fl_coh' might be unused.
122The value assigned to variable 'fl_ap' might be unused.
123The value assigned to variable 'fl_coh' might be unused.
Coverage results
Show coverage for parent directory
Total lines in function263
Non-code lines (comments, blank lines)161
Code lines (lines that can run)102
Code lines that did run45
Code lines that did not run57
Coverage (did run/can run)44.12 %
Function listing
time 
Calls 
 line
   7 
function [fl_max_c, fl_b_bridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = ...
   8 
            ffs_fibs_min_c_cost_bridge(varargin)
   9 
%% FFS_FIBS_MIN_C_COST_INF_BRIDGE combine ffs_fibs_min_c_cost + inf_bridge
  10 
% Given coh and aprime choice, what are the optimal formal and informal
  11 
% joint choices including bridge choices. This function is invoked after
  12 
% optimal a-prime choices have been found. This is invoked so that during
  13 
% solution, do not have to store these choices. This is the same material
  14 
% as what is in ff_abz_fibs_vf.m
  15 
%
  16 
% @param fl_ap float aprime choice, for example optimal aprime solved
  17 
%
  18 
% @param fl_coh float cash-on-hand for the aprime choice
  19 
%
  20 
% @param ar_aprime array N by 1 level of aggregate borrowing excluding
  21 
% bridge loan. Note that bridge loan is needed if coh is negative and
  22 
% households can not pay back principle and interests. This must be
  23 
% negative.
  24 
%
  25 
% @return fl_max_c float next period consumption cost
  26 
% (_bl_b_is_principle_ == true), or this period consumption gain
  27 
% (_bl_b_is_principle_ == false) based on choosing optimally between formal
  28 
% and informal, borrowing and savings joint categories, given either total
  29 
% borrowing in principles or principles + interest rate from ar_aprime.
  30 
%
  31 
% @return fl_b_bridge float bridge loan debt to pay for unpaid uncovered
  32 
% cash-on-hand
  33 
%
  34 
% @return fl_inf_borr_nobridge float informal borrowing choices
  35 
% (Excluding Informal Bridge loans, calculated elsewhere) which could come
  36 
% from informal borrowing only if that minimizes consumption cost, or joint
  37 
% formal and informal borrowing if that is the cost minimizing choice. if
  38 
% _bl_b_is_principle_ == true, then this includes just the principles,  no
  39 
% intrest rates. if _bl_b_is_principle_ == false, that means this includes
  40 
% interest rates costs as well as principles costs.
  41 
%
  42 
% @return fl_for_borr float formal borrowing choice that minimizes
  43 
% consumption costs given fixed _ar_aprime_. Could come from formal
  44 
% borrowing alone (which shows up as joint formal and something else where
  45 
% the other choice is 0), or formal + informal joint borrow, or formal
  46 
% borrowing and formal savings.
  47 
%
  48 
% @return fl_for_save float this is the formal savings choice when
  49 
% households are borrowing. Households coulds save just for savings, no
  50 
% borrowing as well, that is not captured here.
  51 
%
  52 
% @example
  53 
%
  54 
%   bl_input_override = true;
  55 
%   [fl_max_c, fl_b_bridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = ...
  56 
%        ffs_fibs_min_c_cost_bridge(fl_ap, fl_coh, ...
  57 
%            param_map, support_map, armt_map, func_map, bl_input_override);
  58 
%
  59 
% @seealso
  60 
%
  61 
% * Formal Borrowing Grid: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_for_br_block_gen.html ffs_for_br_block_gen>
  62 
% * Informal Interest Rates: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_r_inf.html ffs_r_inf>
  63 
% * Match Borrowing to Formal Grid: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_for_br_block_match.html ffs_for_br_block_match>
  64 
% * Optimize Formal and Informal, Borrowing and Savings Joint Choices: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_min_c_cost.html ffs_fibs_min_c_cost>
  65 
% * Bridge Loan: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_inf_bridge.html ffs_fibs_inf_bridge>
  66 
% * Overall Optimization: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_min_c_cost_bridge.html ffs_fibs_min_c_cost_bridge>
  67 
% * Discrete Choices: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_identify_discrete.html ffs_fibs_identify_discrete>
  68 
%
  69 

  70 
%% Default 
  71 

  0.001 
  11580 
  72
if (~isempty(varargin)) 
  73 
    
  74 
    % override when called from outside
  0.091 
  11580 
  75
    [fl_ap, fl_coh, param_map, support_map, armt_map, func_map] = varargin{:}; 
< 0.001 
  11580 
  76
    bl_display_minccost_bridge = false; 
  77 
    
  78 
else
  79 
    
  80 
    close all
  81 
    
  82 
    % Default
  83 
    it_param_set = 4;
  84 
    bl_input_override = true;
  85 
    [param_map, support_map] = ffs_ipwkbz_fibs_set_default_param(it_param_set);
  86 
    
  87 
    % principle or p+r
  88 
    param_map('bl_bridge') = true;
  89 
    param_map('fl_r_inf') = 0.065;
  90 
    
  91 
    % Gather Inputs from armt_map
  92 
    params_group = values(param_map, ...
  93 
        {'fl_r_fbr', 'st_forbrblk_type', 'fl_forbrblk_brmost', 'fl_forbrblk_brleast', 'fl_forbrblk_gap'});
  94 
    [fl_r_fbr, st_forbrblk_type, fl_forbrblk_brmost, fl_forbrblk_brleast, fl_forbrblk_gap] = params_group{:};
  95 
    [ar_forbrblk, ar_forbrblk_r] = ...
  96 
        ffs_for_br_block_gen(fl_r_fbr, st_forbrblk_type, fl_forbrblk_brmost, fl_forbrblk_brleast, fl_forbrblk_gap);
  97 
    
  98 
    armt_map = containers.Map('KeyType','char', 'ValueType','any');
  99 
    armt_map('ar_forbrblk') = ar_forbrblk;
 100 
    armt_map('ar_forbrblk_r') = ar_forbrblk_r;
 101 
    
 102 
    % Get Functions
 103 
    params_group = values(param_map, {'fl_crra', 'fl_c_min', 'fl_b_bd'});
 104 
    [fl_crra, fl_c_min, fl_b_bd] = params_group{:};
 105 
    params_group = values(param_map, {'fl_Amean', 'fl_alpha', 'fl_delta'});
 106 
    [fl_Amean, fl_alpha, fl_delta] = params_group{:};
 107 
    params_group = values(param_map, {'fl_r_fsv', 'fl_w'});
 108 
    [fl_r_fsv, fl_w] = params_group{:};
 109 
    [~, ~, ~, ~, ~, ~, f_coh_fbis, f_coh_save, ~] = ...
 110 
        ffs_ipwkbz_fibs_set_functions(...
 111 
        fl_crra, fl_c_min, fl_b_bd, fl_Amean, fl_alpha, fl_delta, fl_w, fl_r_fbr, fl_r_fsv);
 112 
    
 113 
    func_map = containers.Map('KeyType','char', 'ValueType','any');
 114 
    func_map('f_coh_fbis') = f_coh_fbis;
 115 
    func_map('f_coh_save') = f_coh_save;
 116 
    
 117 
    % Testing COH and Aprime Vectors
 118 
    fl_ap = -10;
 119 
    fl_coh = 5;
 120 

 121 
    % Testing COH and Aprime Vectors
 122 
    fl_ap = -10;
 123 
    fl_coh = -7;
 124 
    
 125 
    % Example where aprime choice can not repay debt. 
 126 
    fl_ap = -5;
 127 
    fl_coh = -10;
 128 

 129 
    % Set Display Control
 130 
    support_map('bl_display_infbridge') = true;
 131 
    support_map('bl_display_minccost') = true;
 132 
    
 133 
    bl_display_minccost_bridge = true;
< 0.001 
  11580 
 134
end 
 135 

 136 
%% Parse Parameters
 137 

 138 
% Gather Inputs from armt_map
  0.096 
  11580 
 139
params_group = values(armt_map, {'ar_forbrblk', 'ar_forbrblk_r'}); 
  0.032 
  11580 
 140
[ar_forbrblk, ar_forbrblk_r] = params_group{:}; 
 141 

 142 
% Gather Inputs from param_map
 143 
% fl_r_inf is a shock, need to be inserted in
  0.087 
  11580 
 144
params_group = values(param_map, {'bl_rollover', 'bl_default', 'bl_bridge', 'bl_b_is_principle', 'fl_r_inf', 'fl_r_fsv', 'fl_c_min'}); 
  0.042 
  11580 
 145
[bl_rollover, bl_default, bl_bridge, bl_b_is_principle, fl_r_inf, fl_r_fsv, fl_c_min] = params_group{:}; 
 146 

 147 
% func_map
< 0.001 
  11580 
 148
if (bl_b_is_principle) 
 149 
    % when savings is principle: mimizing cost cost tomorrow
  0.080 
  11580 
 150
    params_group = values(func_map, {'f_coh_fbis', 'f_coh_save'}); 
  0.055 
  11580 
 151
    [f_coh_fbis, f_coh_save] = params_group{:}; 
 152 
else
 153 
    % when not principle, but principle + interest: maximize c gain today
 154 
    params_group = values(func_map, {'f_cons_coh_fbis', 'f_cons_coh_save'});
 155 
    [f_cons_coh_fbis, f_cons_coh_save] = params_group{:};
< 0.001 
  11580 
 156
end 
 157 

 158 
% support_map
  0.075 
  11580 
 159
params_group = values(support_map, {'bl_display_minccost', 'bl_display_infbridge'}); 
  0.029 
  11580 
 160
[bl_display_minccost, bl_display_infbridge] = params_group{:}; 
 161 

 162 
%% Compute Consumption given Borrowing and Savings
 163 
% find the today's consumption maximizing formal and informal
 164 
% choices given a' and coh. The formal and informal choices need to
 165 
% generate exactly a', but depending on which formal and informal
 166 
% joint choice is used, the consumption cost today a' is different.
 167 
% Note here, a is principle + interests. Three areas:
 168 
%
 169 
% * *CASE A* a' > 0: savings, do not need to optimize over formal and
 170 
% informal choices
 171 
% * *CASE B* a' < 0 & coh < 0: need bridge loan to pay for unpaid debt, and
 172 
% borrowing over-all, need to first pick bridge loan to pay for
 173 
% debt, if bridge loan is insufficient, go into default. After
 174 
% bridge loan, optimize over formal+informal, borrow+save joint
 175 
% choices.
 176 
% * *CASE C* $ a' < 0 & coh > 0: do not need to get informal bridge loans,
 177 
% optimize over for+inf save, for+save+borr, inf+borr only, for
 178 
% borrow only.
 179 
%
 180 

< 0.001 
  11580 
 181
if (fl_ap < 0) 
 182 
    
 183 
    % Calculate Bridge Loan Borrowing
< 0.001 
   4174 
 184
    if (fl_coh < 0 && bl_bridge) 
< 0.001 
   3000 
 185
        bl_input_override = true; 
  0.045 
   3000 
 186
        [fl_aprime_nobridge, fl_b_bridge, fl_c_bridge] = ffs_fibs_inf_bridge(... 
   3000 
 187
            bl_b_is_principle, fl_r_inf, fl_ap, fl_coh, ... 
   3000 
 188
            bl_display_infbridge, bl_input_override); 
 189 
        
< 0.001 
   1174 
 190
    else 
 191 
        
< 0.001 
   1174 
 192
        fl_aprime_nobridge = fl_ap; 
< 0.001 
   1174 
 193
        fl_b_bridge = 0; 
< 0.001 
   1174 
 194
        fl_c_bridge = 0; 
 195 
        
< 0.001 
   4174 
 196
    end 
 197 
    
 198 
    % Find Optimal Formal Informal Borrow Save Combo
 199 
    % calculate consumption gain from formal + informal
 200 
    % borrowing and savings choices.
< 0.001 
   4174 
 201
    bl_input_override = true; 
  0.453 
   4174 
 202
    [fl_max_c_nobridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = ... 
 203 
        ffs_fibs_min_c_cost(...
   4174 
 204
        bl_b_is_principle, fl_r_inf, fl_r_fsv, ... 
   4174 
 205
        ar_forbrblk_r, ar_forbrblk, ... 
   4174 
 206
        fl_aprime_nobridge, bl_display_minccost, bl_input_override); 
 207 
    
 208 
    % Compute Consumption given Formal and Informal joint
 209 
    % consumption with formal borrow menu + bridge loans.
< 0.001 
   4174 
 210
    if (bl_b_is_principle) 
  0.014 
   4174 
 211
        fl_max_c_or_coh_raw = f_coh_fbis(fl_r_inf, fl_for_borr, fl_b_bridge + fl_inf_borr_nobridge, fl_for_save); 
 212 
    else
 213 
        fl_max_c_or_coh_raw = f_cons_coh_fbis(fl_coh, fl_max_c_nobridge + fl_c_bridge);
< 0.001 
   4174 
 214
    end       
 215 
    
< 0.001 
   7406 
 216
else 
 217 
    
 218 
    % consumption with savings
< 0.001 
   7406 
 219
    if (bl_b_is_principle) 
  0.021 
   7406 
 220
        fl_max_c_or_coh_raw = f_coh_save(fl_ap); 
 221 
    else
 222 
        fl_max_c_or_coh_raw = f_cons_coh_save(fl_coh, fl_ap);
< 0.001 
   7406 
 223
    end 
 224 
    
 225 
    % assign values for formal and informal choices
 226 
    % possible that fl_coh < 0, but if then fl_ap > 0 is
 227 
    % not valid choice
  0.043 
   7406 
 228
    [fl_b_bridge, fl_inf_borr_nobridge, fl_for_borr, fl_for_save] = deal(0, 0, 0, fl_ap); 
 229 
    
< 0.001 
  11580 
 230
end 
 231 

 232 
%% Compute Utility With Default
 233 
% assign u(c)
 234 

< 0.001 
  11580 
 235
if (~bl_b_is_principle) && ... 
 236 
        ((fl_max_c_or_coh_raw <= fl_c_min || ( ~bl_rollover && ~bl_bridge && fl_coh < fl_c_min)))
 237 
    
 238 
    if (bl_default)
 239 
        % Replace Consumption if default cmin
 240 
        fl_max_c = fl_c_min;
 241 
    else
 242 
        % Replace Consumption if no default nan
 243 
        fl_max_c = 0;
 244 
    end
 245 
    % if coh < 0 but aprime > coh, choice not sufficient to pay debt, then
 246 
    % fl_for_save ends up > 0, but that is not possible, not a real choice.
 247 
    if (fl_for_save > 0)
 248 
        fl_for_save = 0;
 249 
    end
< 0.001 
  11580 
 250
else 
< 0.001 
  11580 
 251
    fl_max_c = fl_max_c_or_coh_raw; 
< 0.001 
  11580 
 252
end 
 253 

 254 
%% Display
 255 

< 0.001 
  11580 
 256
if (bl_display_minccost_bridge) 
 257 

 258 
    disp('fl_max_c_raw');
 259 
    disp(fl_max_c_or_coh_raw);    
 260 
    
 261 
    disp('fl_max_c');
 262 
    disp(fl_max_c);
 263 
    
 264 
    disp('fl_for_save');
 265 
    disp(fl_for_save);
 266 
    
 267 
end
 268 

  0.050 
  11580 
 269
end 

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