This is a static copy of a profile report

Home

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

Home

ff_iwkz_ds_vecsv (Calls: 1, Time: 0.371 s)
Generated 28-Jul-2019 11:53:06 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_akz\solve\ff_iwkz_ds_vecsv.m
Copy to new window for comparing multiple runs

Parents (calling functions)
No parent
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
212
[~, ar_coh_prime_on_grid_idx] ...
150.184 s49.6%
257
[V, ~] = eigs(mt_full_trans_ma...
10.159 s42.9%
245
mt_full_trans_mat = sparse(i, ...
10.011 s3.0%
195
if (ismember(st_model, ["ipwkb...
10.006 s1.5%
275
profile off
10.004 s1.0%
All other lines  0.007 s2.0%
Totals  0.371 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
eigsfunction10.159 s42.8%
ismemberfunction20.006 s1.5%
...(b>0)+b.*(1+fl_r_borr).*(b<=0))anonymous function10.002 s0.7%
Self time (built-ins, overhead, etc.)  0.204 s55.0%
Totals  0.371 s100% 
Code Analyzer results
Line numberMessage
134Use of brackets [] is unnecessary. Use parentheses to group, if needed.
138The value assigned here to 'it_z_wage_n' appears to be unused. Consider replacing it by ~.
138The value assigned here to 'fl_z_r_borr_n' appears to be unused. Consider replacing it by ~.
139Use of brackets [] is unnecessary. Use parentheses to group, if needed.
143The value assigned here to 'it_z_wage_n' appears to be unused. Consider replacing it by ~.
143The value assigned here to 'fl_z_r_borr_n' appears to be unused. Consider replacing it by ~.
195Use of brackets [] is unnecessary. Use parentheses to group, if needed.
198Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Coverage results
Show coverage for parent directory
Total lines in function292
Non-code lines (comments, blank lines)195
Code lines (lines that can run)97
Code lines that did run36
Code lines that did not run61
Coverage (did run/can run)37.11 %
Function listing
time 
Calls 
 line
   7 
function [result_map] = ff_iwkz_ds_vecsv(varargin)
   8 
%% FF_IWKZ_DS finds the stationary asset distributions
   9 
% Building on the Two Assets Two-Step Interpolated Dynamic Programming
  10 
% Problem
  11 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html
  12 
% ff_iwkz_vf_vecsv>, here we solve for the asset distribution. This version
  13 
% of the program is semi-analytical.
  14 
%
  15 
% This is the two-stage with interpolation version of
  16 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_ds_vecsv.html
  17 
% ff_akz_ds_vecsv>. See that file for additional descriptions and
  18 
% comparisons. These two functions are nearly identical
  19 
%
  20 
% The code here works when we are looking for the distribution of f(a,z),
  21 
% where a'(a,z,z'), meaning that the a next period is determined by a last
  22 
% period and some shock last period as well as shock this period. a here is
  23 
% cash-on-hand. This contrasts with
  24 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds_vecsv.html
  25 
% ff_az_ds_vecsv>, which works for a'(a,z), a' can not be a function of z'.
  26 
%
  27 
% @example
  28 
%
  29 
%    % Get Default Parameters
  30 
%    it_param_set = 6;
  31 
%    [param_map, support_map] = ffs_az_set_default_param(it_param_set);
  32 
%    % Change Keys in param_map
  33 
%    param_map('it_w_n') = 750;
  34 
%    param_map('it_ak_n') = param_map('it_w_n');
  35 
%    param_map('it_z_n') = 11;
  36 
%    param_map('fl_a_max') = 100;
  37 
%    param_map('fl_w') = 1.3;
  38 
%    % Change Keys support_map
  39 
%    support_map('bl_display') = false;
  40 
%    support_map('bl_post') = true;
  41 
%    support_map('bl_display_final') = false;
  42 
%    % Call Program with external parameters that override defaults
  43 
%    ff_iwkz_ds_vecsv(param_map, support_map);
  44 
%
  45 
% @include
  46 
%
  47 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html ff_wkz_vf_vecsv>
  48 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html ff_az_ds_post_stats>
  49 
% * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html fft_disc_rand_var_stats>
  50 
% * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html fft_disc_rand_var_mass2outcomes>
  51 
%
  52 
% @seealso
  53 
%
  54 
% * derive distribution f(y'(y,z)) one asset *loop*: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html ff_az_ds>
  55 
% * derive distribution f(y'({x,y},z)) two assets *loop*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_ds.html ff_akz_ds>
  56 
% * derive distribution f(y'({x,y},z, *z'*)) two assets *loop*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_ds.html ff_iwkz_ds>
  57 
% * derive distribution f(y'({y},z)) or f(y'({x,y},z)) *vectorized*: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds_vec.html ff_az_ds_vec>
  58 
% * derive distribution f(y'({y},z, *z'*)) or f(y'({x,y},z, *z'*)) *vectorized*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_ds_vec.html ff_iwkz_ds_vec>
  59 
% * derive distribution f(y'({y},z)) or f(y'({x,y},z)) *semi-analytical*: <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds_vecsv.html ff_az_ds_vecsv>
  60 
% * derive distribution f(y'({y},z, *z'*)) or f(y'({x,y},z, *z'*)) *semi-analytical*: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_ds_vecsv.html ff_iwkz_ds_vecsv>
  61 
%
  62 

  63 
%% Default
  64 
% Program can be externally invoked with _az_, _abz_ or various other
  65 
% programs. By default, program invokes using _az_ model programs:
  66 
%
  67 
% # it_subset = 5 is basic invoke quick test
  68 
% # it_subset = 6 is invoke full test
  69 
% # it_subset = 7 is profiling invoke
  70 
% # it_subset = 8 is matlab publish
  71 
% # it_subset = 9 is invoke operational (only final stats) and coh graph
  72 
%
  73 

  74 
if (~isempty(varargin))
  75 
    
  76 
    % if invoked from outside override fully
  77 
    [param_map, support_map, armt_map, func_map, result_map] = varargin{:};
  78 

  79 
else
  80 
    
  81 
    % default invoke
  82 
    close all;
  83 

  84 
    it_param_set = 7;
  85 
    st_akz_or_iwkz = 'iwkz';
  86 

  87 
    % 1. Generate Parameters
  88 
    [param_map, support_map] = ffs_akz_set_default_param(it_param_set);
  89 

  90 
    % Note: param_map and support_map can be adjusted here or outside to override defaults
  91 
    % param_map('it_w_n') = 50;
  92 
    % param_map('it_z_n') = 15;
  93 

  94 
    param_map('st_analytical_stationary_type') = 'eigenvector';
  95 

  96 
    % 2. Generate function and grids
  97 
    [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map); % 1 for override
  98 

  99 
    % 3. Solve value and policy function using ff_iwkz_vf_vecsv
 100 
    if (strcmp(st_akz_or_iwkz, 'iwkz'))
 101 
        [result_map] = ff_iwkz_vf_vecsv(param_map, support_map, armt_map, func_map);
 102 
    end
 103 
end
 104 

 105 
%% Parse Parameters
 106 

 107 
% append function name
 108 
st_func_name = 'ff_iwkz_ds_vecsv';
 109 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 110 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 111 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 112 

 113 
% result_map
 114 
% ar_st_pol_names is from section _Process Optimal Choices_ in the value
 115 
% function code.
 116 
params_group = values(result_map, {'cl_mt_pol_a', 'cl_mt_pol_k'});
 117 
[cl_mt_pol_a, cl_mt_pol_k] = params_group{:};
 118 
[mt_pol_a, mt_pol_k] = deal(cl_mt_pol_a{1}, cl_mt_pol_k{1});
 119 

 120 
% Get Model Name
 121 
params_group = values(param_map, {'st_model'});
 122 
[st_model] = params_group{:};
 123 
% param_map
 124 
params_group = values(param_map, {'it_z_n'});
 125 
[it_z_n] = params_group{:};
 126 

 127 
% func_map
 128 
params_group = values(func_map, {'f_coh'});
 129 
[f_coh] = params_group{:};
 130 

 131 
% armt_map
 132 
params_group = values(armt_map, {'mt_z_trans', 'ar_interp_coh_grid'});
 133 
[mt_z_trans, ar_interp_coh_grid] = params_group{:};
 134 
if (ismember(st_model, ["ipwkbzr"]))
 135 
    params_group = values(armt_map, {'ar_z_r_borr_mesh_wage_w1r2', 'ar_z_wage_mesh_r_borr_w1r2'});
 136 
    [ar_z_r_borr_mesh_wage_w1r2, ar_z_wage_mesh_r_borr_w1r2] = params_group{:};
 137 
    params_group = values(param_map, {'it_z_wage_n', 'fl_z_r_borr_n'});
 138 
    [it_z_wage_n, fl_z_r_borr_n] = params_group{:};
 139 
elseif (ismember(st_model, ["ipwkbzr_fibs"]))
 140 
    params_group = values(armt_map, {'ar_z_r_infbr_mesh_wage_w1r2', 'ar_z_wage_mesh_r_infbr_w1r2'});
 141 
    [ar_z_r_borr_mesh_wage_w1r2, ar_z_wage_mesh_r_borr_w1r2] = params_group{:};
 142 
    params_group = values(param_map, {'it_z_wage_n', 'fl_z_r_infbr_n'});
 143 
    [it_z_wage_n, fl_z_r_borr_n] = params_group{:};
 144 
else
 145 
    params_group = values(armt_map, {'ar_z'});
 146 
    [ar_z] = params_group{:};
 147 
end
 148 

 149 
% param_map
 150 
params_group = values(param_map, {'st_analytical_stationary_type'});
 151 
[st_analytical_stationary_type] = params_group{:};
 152 

 153 
% support_map
 154 
params_group = values(support_map, {'bl_profile_dist', 'st_profile_path', ...
 155 
    'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
 156 
    'bl_time'});
 157 
[bl_profile_dist, st_profile_path, ...
 158 
    st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
 159 
    bl_time] = params_group{:};
 160 

 161 
%% Start Profiler and Timer
 162 

 163 
% Start Profile
 164 
if (bl_profile_dist)
 165 
    close all;
 166 
    profile off;
 167 
    profile on;
< 0.001 
      1 
 168
end 
 169 

 170 
% Start Timer
< 0.001 
      1 
 171
if (bl_time) 
< 0.001 
      1 
 172
    tic; 
< 0.001 
      1 
 173
end 
 174 

 175 
%% A. Get Size of Endogenous and Exogenous State
 176 

< 0.001 
      1 
 177
it_endostates_n = length(ar_interp_coh_grid); 
< 0.001 
      1 
 178
it_exostates_n = it_z_n; 
 179 

 180 
%% B. Solve for Index
 181 
% The model is solved by interpolating over cash-on-hand. The optimal
 182 
% choices do not map to specific points on the cash-on-hand grid. Find the
 183 
% index of the cash-on-hand vector that is the closest to the
 184 
% coh'(a'(coh,z),k'(coh,z),z').
 185 
%
 186 
% Since we have *z_n* elements of shocks, and *coh_n* elements of the
 187 
% cash-on-hand grid, there are (coh_n x z_n) possible combinations of
 188 
% states at period t. In period t+1, there are (coh_n x z_n) by (z_n)
 189 
% possible/reachable cash-on-hand points. We find the index of all these
 190 
% reachable coh' points on the interpolation cash-on-hand grid.
 191 
%
 192 

 193 
% 1. *mt_coh_prime* is (coh_n x z_n) by (z_n)
 194 
% coh'(z', a'(coh,z), k'(coh,z))
  0.006 
      1 
 195
if (ismember(st_model, ["ipwkbzr"])) 
 196 
    mt_coh_prime = f_coh(ar_z_r_borr_mesh_wage_w1r2, ar_z_wage_mesh_r_borr_w1r2, ...
 197 
                        mt_pol_a(:), mt_pol_k(:));
< 0.001 
      1 
 198
elseif (ismember(st_model, ["ipwkbzr_fibs"])) 
 199 
    % mt_pol_a includes interest rates
 200 
    mt_coh_prime = f_coh(ar_z_wage_mesh_r_borr_w1r2, mt_pol_a(:), mt_pol_k(:));
< 0.001 
      1 
 201
else 
  0.003 
      1 
 202
    mt_coh_prime = f_coh(ar_z, mt_pol_a(:), mt_pol_k(:)); 
< 0.001 
      1 
 203
end 
 204 

 205 

 206 
% 2. *mt_coh_prime_on_grid_idx* is (coh_n x z_n) by (z_n):
 207 
% index for coh'(a,k,z')
 208 
% to reduce potential size, loop over future states
< 0.001 
      1 
 209
mt_coh_prime_on_grid_idx = zeros(size(mt_coh_prime)); 
< 0.001 
      1 
 210
for it_zprime_ctr=1:size(mt_coh_prime, 2) 
< 0.001 
     15 
 211
    ar_coh_prime = mt_coh_prime(:,it_zprime_ctr); 
  0.184 
     15 
 212
    [~, ar_coh_prime_on_grid_idx] = min(abs(ar_coh_prime(:)' - ar_interp_coh_grid')); 
< 0.001 
     15 
 213
    mt_coh_prime_on_grid_idx(:,it_zprime_ctr) = ar_coh_prime_on_grid_idx; 
< 0.001 
     15 
 214
end 
 215 

 216 
%% C. Expand Index so Matches Full States Index Dimension
 217 
% The index above matches the index in the cash-on-hand grid, but now, the
 218 
% state space is cash-on-hand jointly with shocks, that is the full states
 219 
% markov's states. So if there are two shocks and two cash-on-hand grid
 220 
% points, the cash-on-hand grid points would have been [1,2] and [1,2], but
 221 
% depending on which z' they match up to, they would now be [1,2] if
 222 
% matching to the first z', and [3,4] if matching to the second z'.
 223 

 224 
% mt_pol_idx_mesh_max is (NxM) by M, mt_pol_idx is N by M
< 0.001 
      1 
 225
mt_pol_idx_mesh_max = mt_coh_prime_on_grid_idx + (0:1:(it_exostates_n-1))*it_endostates_n; 
 226 

 227 
%% D. Transition Probabilities from (M by M) to (NxM) by M
 228 
% Probability comes from the shock transition matrix, which is now
 229 
% duplicated for all cash-on-hand grid elements
 230 

< 0.001 
      1 
 231
mt_trans_prob = reshape(repmat(mt_z_trans(:)', ... 
      1 
 232
    [it_endostates_n, 1]), [it_endostates_n*it_exostates_n, it_exostates_n]); 
 233 

 234 
%% E. Fill mt_pol_idx_mesh_idx to mt_full_trans_mat SPARSE
 235 
% Try to always use sparse matrix, unless grid sizes very small, keeping
 236 
% non-sparse code here for comparison. Sparse matrix is important for
 237 
% allowing the code to be fast and memory efficient. Otherwise this method
 238 
% is much slower than iterative method.
 239 

< 0.001 
      1 
 240
i = mt_pol_idx_mesh_max(:); 
< 0.001 
      1 
 241
j = repmat((1:1:it_endostates_n*it_exostates_n),[1,it_exostates_n])'; 
< 0.001 
      1 
 242
v = mt_trans_prob(:); 
< 0.001 
      1 
 243
m = it_endostates_n*it_exostates_n; 
< 0.001 
      1 
 244
n = it_endostates_n*it_exostates_n; 
  0.011 
      1 
 245
mt_full_trans_mat = sparse(i, j, v, m, n); 
 246 

 247 
%% F. Stationary Distribution *Method A*, Eigenvector Approach
 248 
% Given that markov chain we have constructured for all state-space
 249 
% elements, we can now find the stationary distribution using standard
 250 
% <https://en.wikipedia.org/wiki/Markov_chain#Stationary_distribution_relation_to_eigenvectors_and_simplices
 251 
% eigenvector> approach. See
 252 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds_vecsv.html
 253 
% ff_az_ds_vecsv> for additional methods using the full states markov
 254 
% structure.
 255 

< 0.001 
      1 
 256
if (strcmp(st_analytical_stationary_type, 'eigenvector')) 
  0.159 
      1 
 257
    [V, ~] = eigs(mt_full_trans_mat,1,1); 
< 0.001 
      1 
 258
    ar_stationary = V/sum(V); 
< 0.001 
      1 
 259
end 
 260 

 261 
%% G. Stationary Vector to Stationary Matrix in Original Dimensions
 262 

< 0.001 
      1 
 263
mt_dist_akz = reshape(ar_stationary, size(mt_pol_a)); 
 264 

 265 

 266 
%% End Time and Profiler
 267 

 268 
% End Timer
< 0.001 
      1 
 269
if (bl_time) 
< 0.001 
      1 
 270
    toc; 
< 0.001 
      1 
 271
end 
 272 

 273 
% End Profile
< 0.001 
      1 
 274
if (bl_profile_dist) 
  0.004 
      1 
 275
    profile off 
 276 
    profile viewer
 277 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 278 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 279 
end
 280 

 281 

 282 
%% *f(y), f(c), f(a), f(k)*: Generate Key Distributional Statistics for Each outcome
 283 
% Having derived f({a,k},z) the probability mass function of the joint discrete
 284 
% random variables, we now obtain distributional statistics. Note that we
 285 
% know f({a,k},z), and we also know relevant policy functions a'(a,z), c(a,z),
 286 
% or other policy functions. We can simulate any choices that are a
 287 
% function of the random variables (a,z), using f({a,k},z). We call function
 288 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html
 289 
% ff_az_ds_post_stats> which uses
 290 
% <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html
 291 
% fft_disc_rand_var_stats> and
 292 
% <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html
 293 
% fft_disc_rand_var_mass2outcomes> to compute various statistics of
 294 
% interest.
 295 

 296 
result_map = ff_az_ds_post_stats(support_map, result_map, mt_dist_akz);
 297 

 298 
end

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