This is a static copy of a profile report

Home

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

Home

ffs_for_br_block_match (Calls: 44478, Time: 3.509 s)
Generated 14-Jul-2019 14:55:57 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_fibs\paramfunc_fibs\ffs_for_br_block_match.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ffs_fibs_min_c_costfunction44478
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
88
[ar_forbrblk, ar_forbrblk_r] =...
444782.468 s70.3%
103
[default_params{1:length(varar...
444780.337 s9.6%
164
[~, ar_max_a_on_grid_idx] = ma...
444780.181 s5.2%
104
[ar_a, ar_forbrblk, ar_forbrbl...
444780.154 s4.4%
85
ar_a = -sort(rand([10,1])*20);
444780.116 s3.3%
All other lines  0.253 s7.2%
Totals  3.509 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ffs_for_br_block_genfunction444782.393 s68.2%
Self time (built-ins, overhead, etc.)  1.117 s31.8%
Totals  3.509 s100% 
Code Analyzer results
Line numberMessage
136Terminate statement with semicolon to suppress output (in functions).
141Terminate statement with semicolon to suppress output (in functions).
145Terminate statement with semicolon to suppress output (in functions).
150Terminate statement with semicolon to suppress output (in functions).
Coverage results
Show coverage for parent directory
Total lines in function203
Non-code lines (comments, blank lines)145
Code lines (lines that can run)58
Code lines that did run22
Code lines that did not run36
Coverage (did run/can run)37.93 %
Function listing
time 
Calls 
 line
   7 
function [ar_a_grid_ceil_principle, ar_a_grid_ceil_wthr, ...
   8 
    ar_a_grid_floor_principle, ar_a_grid_floor_wthr] = ffs_for_br_block_match(varargin)
   9 
%% FFS_FOR_BR_BLOCK_MATCH formal borrowing blocks
  10 
% Find Value just below or above each element of *ar_a* from *ar_forbrblk*.
  11 
% a vector of grid points, find for each element of ar_a the element of
  12 
% ar_forbrblk that is just above or just below.
  13 
%
  14 
% # _ar_a_: $a'_i$ where $i \in (1,...,N)$
  15 
% # _ar_forbrblk_: $grid_j$ where $j\in (1,...,M)$
  16 
%
  17 
% $$ForCEIL_i = argmin_{j}(ForGrid_j - a'_i | grid_j > a'_i)$$
  18 
%
  19 
% $ForCEIL_i$ is the level of formal borrowing if joint formal + informal
  20 
% borrowing is chosen.
  21 
%
  22 
% $$ForFLOOR_i = argmax_{j}(ForGrid_j - a'_i | grid_j <= a'_i)$$
  23 
%
  24 
% $ForFLOOR_i$ is the level of formal borrowing if joint formal borrowing +
  25 
% informal savings is chosen.
  26 
%
  27 
% @param ar_a boolean N by 1 single formal borrowing levels, could include
  28 
% interest rate or principle only depending on _bl_b_is_principle_
  29 
%
  30 
% @param ar_forbrblk 1 by M array array of formal borrowing grid points.
  31 
% This always is just the formal borrowing levels principles only without
  32 
% interest rate.
  33 
%
  34 
% @param ar_forbrblk_r array interest rates associated with equal-length
  35 
% _ar_forbrblk_.
  36 
%
  37 
% @param bl_b_is_principle boolean solving with aggregate savings as
  38 
% savings + debt principles + interests, or just principles no interests.
  39 
% if true, principels only, no interests. Specifically: 
  40 
%
  41 
% * bl_b_is_principle = false: this means that the asset choices include
  42 
% both principle and interest rate. For here, that means _ar_a_ vector
  43 
% elements include both principle and interest rate, but the _ar_forbrblk_
  44 
% vector always only include principles. So when matching, need to
  45 
% translate _ar_forbrblk_ by appending interest rates on. 
  46 
% * bl_b_is_principle = false for *abz*, bl_b_is_principle = true for
  47 
% *ipwkbz*.
  48 
%
  49 
% @return ar_a_grid_ceil_principle array N by 1 Solution to:
  50 
%
  51 
% $$min_{j}(ForGrid_j - a'_i | grid_j > a'_i)$$
  52 
%
  53 
% @return ar_a_grid_ceil_wthr array _ar_a_grid_ceil_principle_ with
  54 
% interest rates specified to each borrowing formal level added
  55 
%
  56 
% @return ar_a_grid_floor_principle array N by 1 element of the *ar_forbrblk* vector that are
  57 
% the elements right above each eelemnt of ar_a. Solution to:
  58 
%
  59 
% $$max_{j}(ForGrid_j - a'_i | grid_j <= a'_i)$$
  60 
%
  61 
% @return ar_a_grid_floor_principle array _ar_a_grid_floor_principle_ with
  62 
% interest rates specified to each borrowing formal level added
  63 
%
  64 
% @example
  65 
%
  66 
%   [ar_a_grid_ceil, ar_a_grid_floor] = ...
  67 
%        ffs_for_br_block_match(ar_a, ar_forbrblk, ar_forbrblk_r, bl_b_is_principle);
  68 
%
  69 
% @seealso
  70 
%
  71 
% * Formal Borrowing Grid: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_for_br_block_gen.html ffs_for_br_block_gen>
  72 
% * Informal Interest Rates: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_r_inf.html ffs_r_inf>
  73 
% * 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>
  74 
% * 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>
  75 
% * Bridge Loan: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_inf_bridge.html ffs_fibs_inf_bridge>
  76 
% * 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>
  77 
% * Discrete Choices: <https://fanwangecon.github.io/CodeDynaAsset/m_fibs/paramfunc_fibs/html/ffs_fibs_identify_discrete.html ffs_fibs_identify_discrete>
  78 
%
  79 

  80 

  81 
%% Default
  82 

  83 
% array of a choices
  84 
% ar_a could be principles + interests, or principles only
  0.116 
  44478 
  85
ar_a = -sort(rand([10,1])*20); 
  86 

  87 
% use defaults from block gen
  2.468 
  44478 
  88
[ar_forbrblk, ar_forbrblk_r] = ffs_for_br_block_gen(); 
  89 

  90 
% if bl_b_is_principle is true, b is principles only, no interests.
  91 
% bl_b_is_principle = false is the case for models like *abz* without
  92 
% interpolation over cash-on-hand
  0.003 
  44478 
  93
bl_b_is_principle = true; 
  94 

  95 
% Display
  0.002 
  44478 
  96
bl_display_brblockmatch = false; 
  97 

  0.061 
  44478 
  98
default_params = {ar_a ar_forbrblk ar_forbrblk_r bl_b_is_principle bl_display_brblockmatch}; 
  99 

 100 
%% Parse Parameters
 101 

 102 
% numvarargs is the number of varagin inputted
  0.337 
  44478 
 103
[default_params{1:length(varargin)}] = varargin{:}; 
  0.154 
  44478 
 104
[ar_a, ar_forbrblk, ar_forbrblk_r, bl_b_is_principle, bl_display_brblockmatch] = default_params{:}; 
 105 

 106 
%% Adjust Inputs t
 107 
% if bl_b_is_principle, then principle, with the assumption that
 108 
% ar_forbrblk. If bl_b_is_principle is false, that means the ar_a vector is
 109 
% principle and interest rates. Hence, need to convert ar_forbrblk which
 110 
% are principles to interests plus principles to be on the same scale as
 111 
% ar_a.
 112 

  0.003 
  44478 
 113
if (bl_b_is_principle) 
 114 
    ar_forbrblk_use = ar_forbrblk;
  0.002 
  44478 
 115
else 
  0.020 
  44478 
 116
    ar_forbrblk_use = ar_forbrblk.*(1+ar_forbrblk_r); 
  0.003 
  44478 
 117
end 
 118 

 119 
%% Show Details Step by Step
  0.003 
  44478 
 120
if (bl_display_brblockmatch) 
 121 

 122 
    % show borrowing array
 123 
    disp('ar_a')
 124 
    disp(ar_a)
 125 

 126 
    % show borrowing formal blocks/grids
 127 
    disp('ar_forbrblk_use and ar_forbrblk');
 128 
    disp([ar_forbrblk_use;ar_forbrblk]');
 129 

 130 
    % all combination division
 131 
    disp('mt_a_dvd_grid = (ar_a./ar_forbrblk_use)');
 132 
    mt_a_dvd_grid = (ar_a./ar_forbrblk_use);
 133 

 134 
    % ceiling for each
 135 
    disp('(mt_a_dvd_grid >= 1)');
 136 
    (mt_a_dvd_grid >= 1)
 137 

 138 
    % If ceiling exists and cloest ceiling index
 139 
    % min_{j}( ar_forbrblk[j] - ar_a[i] | ar_forbrblk[j] > ar_a[i])
 140 
    disp('[~, ar_max_a_on_grid_idx] = max((mt_a_dvd_grid >= 1),[], 2)');
 141 
    [~, ar_max_a_on_grid_idx] = max((mt_a_dvd_grid >= 1),[], 2)
 142 

 143 
    % ar_forbrblk[argmin_{j}( ar_forbrblk[j] - ar_a[i] | ar_forbrblk[j] > ar_a[i])]
 144 
    disp('ar_a_grid_ceil = ar_forbrblk_use(ar_max_a_on_grid_idx)');
 145 
    ar_a_grid_ceil = ar_forbrblk_use(ar_max_a_on_grid_idx)
 146 
    % ar_a_grid_ceil(ar_max_a_on_grid_idx == 1) = ar_forbrblk(0)
 147 

 148 
    % now floor, just one index less
 149 
    disp('ar_a_grid_floor = ar_forbrblk_use(max(ar_max_a_on_grid_idx - 1, 1))');
 150 
    ar_a_grid_floor = ar_forbrblk_use(max(ar_max_a_on_grid_idx - 1, 1))
 151 
    % ar_a_grid_floor(ar_max_a_on_grid_idx == 1) =
 152 

 153 
    % Dispaly
 154 
    tab_matched_grid = table(ar_a, ar_a_grid_floor', ar_a_grid_ceil');
 155 
    tab_matched_grid.Properties.VariableNames = {'ar_a','ar_a_grid_floor','ar_a_grid_ceil'};
 156 
    disp('ar_a_grid_floor: for borrow + for save');
 157 
    disp('ar_a_grid_ceil: for + inf borrow');
 158 
    disp(tab_matched_grid);
 159 
end
 160 

 161 
%% Standard Quicker Solve
 162 

 163 
% Get Index
  0.181 
  44478 
 164
[~, ar_max_a_on_grid_idx] = max(((ar_a./ar_forbrblk_use) >= 1),[], 2); 
 165 

 166 
% Get Values
  0.003 
  44478 
 167
if (bl_b_is_principle) 
 168 

 169 
    % Borrowing borrowing points, following formal grids, but add interests
 170 
    ar_a_grid_ceil_wthr = ...
 171 
        (ar_forbrblk_use(ar_max_a_on_grid_idx).*(1+ar_forbrblk_r(ar_max_a_on_grid_idx)))';
 172 
    ar_a_grid_floor_wthr = ...
 173 
        (ar_forbrblk_use(max(ar_max_a_on_grid_idx - 1, 1)).*(1+ar_forbrblk_r(max(ar_max_a_on_grid_idx - 1, 1))))';
 174 

 175 
    % Principles only, note ar_forbrblk_use = ar_forbrblk
 176 
    ar_a_grid_ceil_principle = ar_forbrblk_use(ar_max_a_on_grid_idx)';
 177 
    ar_a_grid_floor_principle = ar_forbrblk_use(max(ar_max_a_on_grid_idx - 1, 1))';
 178 

  0.002 
  44478 
 179
else 
 180 
    
 181 
    % Borrowing borrowing points, following formal grids, but add interests
  0.002 
  44478 
 182
    ar_a_grid_ceil_wthr = ar_forbrblk_use(ar_max_a_on_grid_idx)'; 
  0.002 
  44478 
 183
    ar_a_grid_floor_wthr = ar_forbrblk_use(max(ar_max_a_on_grid_idx - 1, 1))'; 
 184 

 185 
    % Principles only
  0.002 
  44478 
 186
    ar_a_grid_ceil_principle = ar_forbrblk(ar_max_a_on_grid_idx)'; 
  0.002 
  44478 
 187
    ar_a_grid_floor_principle = ar_forbrblk(max(ar_max_a_on_grid_idx - 1, 1))'; 
 188 

  0.002 
  44478 
 189
end 
 190 

 191 
%% Display
 192 

  0.003 
  44478 
 193
if (bl_display_brblockmatch) 
 194 
    
 195 
    disp('ar_a_grid_ceil_principle');
 196 
    disp(ar_a_grid_ceil_principle);
 197 
    
 198 
    disp('ar_a_grid_ceil_wthr');
 199 
    disp(ar_a_grid_ceil_wthr);
 200 
    
 201 
    disp('ar_a_grid_floor_principle');
 202 
    disp(ar_a_grid_floor_principle);
 203 
    
 204 
    disp('ar_a_grid_floor_wthr');
 205 
    disp(ar_a_grid_floor_wthr);
 206 
    
 207 
end
 208 

  0.086 
  44478 
 209
end