This is a static copy of a profile report

Home

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

Home

ff_az_ds_vec (Calls: 1, Time: 1.730 s)
Generated 03-Jul-2019 20:30:05 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_az\solve\ff_az_ds_vec.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
ff_akz_ds_vecscript1
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
238
mt_zi_cumu_prob = accumarray([...
19950.574 s33.2%
309
result_map = ff_az_ds_post_sta...
10.343 s19.8%
233
[ar_idx_full, ~, ar_idx_of_uni...
19950.317 s18.3%
237
[mt_idx_of_unique_mesh, mt_col...
19950.270 s15.6%
234
mt_zi_prob = mt_dist_az_cur(:,...
19950.127 s7.3%
All other lines  0.100 s5.8%
Totals  1.730 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
ff_az_ds_post_statsfunction10.335 s19.4%
uniquefunction19950.291 s16.8%
ndgridfunction19950.186 s10.8%
Self time (built-ins, overhead, etc.)  0.917 s53.0%
Totals  1.730 s100% 
Code Analyzer results
Line numberMessage
273The value assigned to variable 'it_iter_last' might be unused.
Coverage results
Show coverage for parent directory
Total lines in function305
Non-code lines (comments, blank lines)215
Code lines (lines that can run)90
Code lines that did run63
Code lines that did not run27
Coverage (did run/can run)70.00 %
Function listing
time 
Calls 
 line
   7 
function [result_map] = ff_az_ds_vec(varargin)
   8 
%% FF_AZ_DS_VEC finds the stationary asset distributions Vectorized
   9 
% Building on the Asset Dynamic Programming Problem
  10 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html
  11 
% ff_az_vf_vecsv>, here we solve for the asset distribution using
  12 
% vectorized codes.
  13 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html
  14 
% ff_az_ds> shows looped codes for finding asset distribution. The solution
  15 
% is the same. Both *ff_az_ds* and *ff_az_ds_vec* using
  16 
% optimized-vectorized dynamic programming code from ff_az_vf_vecsv. The
  17 
% idea here is that in addition to vectornizing the dynamic programming
  18 
% funcion, we can also vectorize the distribution code here. 
  19 
%
  20 
% This function finds the distributio based on the outputs of several
  21 
% dynamic programming problems, both single and multiple assets:
  22 
%
  23 
% # One Asset DP Savings <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html ff_az_vf_vecsv>
  24 
% # One Asset DP Savings and Borrowing <https://fanwangecon.github.io/CodeDynaAsset/m_abz/solve/html/ff_abz_vf_vecsv.html ff_abz_vf_vecsv>
  25 
% # Risky + Safe Asset Concurrent DP <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html ff_akz_vf_vecsv>
  26 
% # Risky + Safe Asset Two-Stage DP <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vecsv.html ff_wkz_vf_vecsv>
  27 
%
  28 
% Similar to
  29 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html
  30 
% ff_az_ds>. The code here works when we are looking for the distribution
  31 
% of f(a,z), where a'(a,z), meaning that the a next period is determined by
  32 
% a last period and some shock. Given this, the a' is fixed for all z'. If
  33 
% however, the outcome of interest is such that: y'(y,z,z'), meaning that
  34 
% y' is different depending on realized z', the code below does not work.
  35 
%
  36 
% Distributions of Interest:
  37 
%
  38 
% * $p(a,z)$
  39 
% * $p(Y=y, z) = \sum_{a} \left( 1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$
  40 
% * $p(Y=y, a) = \sum_{z} \left( 1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$
  41 
% * $p(Y=y) = \sum_{a,z} \left( 1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$
  42 
%
  43 
% Statistics include:
  44 
%
  45 
% * $\mu_y = \sum_{y} p(Y=y) \cdot y$
  46 
% * $\sigma_y = \sqrt{ \sum_{y} p(Y=y) \cdot \left( y - \mu_y \right)^2}$
  47 
% * $p(y=0)$
  48 
% * $p(y=\max(y))$
  49 
% * percentiles: $min_{y} \left\{ P(Y \le y) - percentile \mid P(Y \le y) \ge percentile \right\}$
  50 
% * fraction of outcome held by up to percentiles: $E(Y<y)/E(Y)$
  51 
%
  52 
% @param param_map container parameter container
  53 
%
  54 
% @param support_map container support container
  55 
%
  56 
% @param armt_map container container with states, choices and shocks
  57 
% grids that are inputs for grid based solution algorithm
  58 
%
  59 
% @param func_map container container with function handles for
  60 
% consumption cash-on-hand etc.
  61 
%
  62 
% @return result_map container contains policy function matrix, value
  63 
% function matrix, iteration results, and policy function, value function
  64 
% and iteration results tables.
  65 
%
  66 
% new keys included in result_map in addition to the output from
  67 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html
  68 
% ff_az_vf_vecsv> are various distribution statistics for each model
  69 
% outcome, keys include *cl_mt_pol_a*, *cl_mt_pol_c*, *cl_mt_pol_coh*, etc.
  70 
%
  71 
% @example
  72 
%
  73 
%    % Get Default Parameters
  74 
%    it_param_set = 6;
  75 
%    [param_map, support_map] = ffs_az_set_default_param(it_param_set);
  76 
%    % Change Keys in param_map
  77 
%    param_map('it_a_n') = 500;
  78 
%    param_map('it_z_n') = 11;
  79 
%    param_map('fl_a_max') = 100;
  80 
%    param_map('fl_w') = 1.3;
  81 
%    % Change Keys support_map
  82 
%    support_map('bl_display') = false;
  83 
%    support_map('bl_post') = true;
  84 
%    support_map('bl_display_final') = false;
  85 
%    % Call Program with external parameters that override defaults
  86 
%    ff_az_ds_vec(param_map, support_map);
  87 
%
  88 
%
  89 
% @include
  90 
%
  91 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html ff_az_vf_vecsv>
  92 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html ff_az_ds_post_stats>
  93 
% * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html fft_disc_rand_var_stats>
  94 
% * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html fft_disc_rand_var_mass2outcomes>
  95 
%
  96 
% @seealso
  97 
%
  98 
% * 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>
  99 
% * 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>
 100 
% * 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>
 101 
% * 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>
 102 
% * 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>
 103 
% * 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>
 104 
% * 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>
 105 
%
 106 

 107 
%% Default
 108 
% Program can be externally invoked with _az_, _abz_ or various other
 109 
% programs. By default, program invokes using _az_ model programs:
 110 
%
 111 
% # it_subset = 5 is basic invoke quick test
 112 
% # it_subset = 6 is invoke full test
 113 
% # it_subset = 7 is profiling invoke
 114 
% # it_subset = 8 is matlab publish
 115 
% # it_subset = 9 is invoke operational (only final stats) and coh graph
 116 
%
 117 

< 0.001 
      1 
 118
params_len = length(varargin); 
< 0.001 
      1 
 119
bl_input_override = 0; 
< 0.001 
      1 
 120
if (params_len == 6) 
< 0.001 
      1 
 121
    bl_input_override = varargin{6}; 
< 0.001 
      1 
 122
end 
 123 

< 0.001 
      1 
 124
if (bl_input_override) 
 125 
    % if invoked from outside override fully
< 0.001 
      1 
 126
    [param_map, support_map, armt_map, ~, result_map, ~] = varargin{:}; 
 127 

 128 
else
 129 
    % default invoke
 130 
    close all;
 131 

 132 
    it_param_set = 7;
 133 
    bl_input_override = true;
 134 

 135 
    % 1. Generate Parameters
 136 
    [param_map, support_map] = ffs_az_set_default_param(it_param_set);
 137 

 138 
    % Note: param_map and support_map can be adjusted here or outside to override defaults
 139 
    % param_map('it_a_n') = 750;
 140 
    % param_map('it_z_n') = 15;
 141 

 142 
    % 2. Generate function and grids
 143 
    [armt_map, func_map] = ffs_az_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
 144 

 145 
    % 3. Solve value and policy function using az_vf_vecsv, if want to solve
 146 
    % other models, solve outside then provide result_map as input
 147 
    [result_map] = ff_az_vf_vecsv(param_map, support_map, armt_map, func_map);
 148 

< 0.001 
      1 
 149
end 
 150 

 151 
%% Parse Parameters
 152 

 153 
% append function name
< 0.001 
      1 
 154
st_func_name = 'ff_az_ds_vec'; 
< 0.001 
      1 
 155
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')]; 
< 0.001 
      1 
 156
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')]; 
< 0.001 
      1 
 157
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')]; 
 158 

 159 
% result_map
 160 
% ar_st_pol_names is from section _Process Optimal Choices_ in the value
 161 
% function code.
< 0.001 
      1 
 162
params_group = values(result_map, {'mt_pol_idx'}); 
< 0.001 
      1 
 163
[mt_pol_idx] = params_group{:}; 
 164 

 165 
% armt_map
< 0.001 
      1 
 166
params_group = values(armt_map, {'mt_z_trans'}); 
< 0.001 
      1 
 167
[mt_z_trans] = params_group{:}; 
 168 

 169 
% param_map
< 0.001 
      1 
 170
params_group = values(param_map, {'it_maxiter_dist', 'fl_tol_dist'}); 
< 0.001 
      1 
 171
[it_maxiter_dist, fl_tol_dist] = params_group{:}; 
 172 

 173 
% support_map
< 0.001 
      1 
 174
params_group = values(support_map, {'bl_profile_dist', 'st_profile_path', ... 
 175 
    'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
 176 
    'bl_time', 'bl_display_dist', 'it_display_every'});
< 0.001 
      1 
 177
[bl_profile_dist, st_profile_path, ... 
      1 
 178
    st_profile_prefix, st_profile_name_main, st_profile_suffix, ... 
      1 
 179
    bl_time, bl_display_dist, it_display_every] = params_group{:}; 
 180 

 181 
%% Start Profiler and Timer
 182 

 183 
% Start Profile
< 0.001 
      1 
 184
if (bl_profile_dist) 
 185 
    close all;
 186 
    profile off;
 187 
    profile on;
 188 
end
 189 

 190 
% Start Timer
< 0.001 
      1 
 191
if (bl_time) 
< 0.001 
      1 
 192
    tic; 
< 0.001 
      1 
 193
end 
 194 

 195 
%% Get Size of Endogenous and Exogenous State
 196 
% The key idea is that all information for policy function is captured by
 197 
% _mt_pol_idx_ matrix, its rows are the number of endogenous states, and
 198 
% its columns are the exogenous shocks.
 199 

< 0.001 
      1 
 200
[it_endostates_rows_n, it_exostates_cols_n] = size(mt_pol_idx); 
 201 

 202 
%% *f(a,z)*: Initialize Output Matrixes
 203 
% Initialize the distribution to be uniform
< 0.001 
      1 
 204
mt_dist_az_init = ones(it_endostates_rows_n,it_exostates_cols_n)/it_endostates_rows_n/it_exostates_cols_n; 
< 0.001 
      1 
 205
mt_dist_az_cur = mt_dist_az_init; 
< 0.001 
      1 
 206
mt_dist_az_zeros = zeros(it_endostates_rows_n,it_exostates_cols_n); 
 207 

 208 
%% *f(a,z)*: Initialize Convergence Conditions
 209 

< 0.001 
      1 
 210
bl_histiter_continue = true; 
< 0.001 
      1 
 211
it_iter = 0; 
< 0.001 
      1 
 212
ar_dist_diff_norm = zeros([it_maxiter_dist, 1]); 
< 0.001 
      1 
 213
mt_dist_perc_change = zeros([it_maxiter_dist, it_exostates_cols_n]); 
 214 

 215 
%% *f(a,z)*: Derive Stationary Distribution
 216 
% Iterate over the discrete joint random variable variables (a,z)
< 0.001 
      1 
 217
while (bl_histiter_continue) 
 218 

< 0.001 
    133 
 219
    it_iter = it_iter + 1; 
 220 

 221 
    %% *f(a,z)*: Vectorized Solution
 222 
    % this is the only part of the code that differs from
 223 
    % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_ds.html
 224 
    % ff_az_ds.html> the looped code.
 225 
    
 226 
    % 1. initialize empty
< 0.001 
    133 
 227
    mt_dist_az = mt_dist_az_zeros; 
 228 

 229 
    % 2. One loop remains
< 0.001 
    133 
 230
    for i = 1:it_exostates_cols_n 
 231 

 232 
        % 3. Get Unique Index (future states receive from multiple current states)
  0.317 
   1995 
 233
        [ar_idx_full, ~, ar_idx_of_unique] = unique(mt_pol_idx(:,i)); 
  0.127 
   1995 
 234
        mt_zi_prob = mt_dist_az_cur(:,i) * mt_z_trans(i,:); 
 235 

 236 
        % 4. Cumulative probability received at state from zi
  0.270 
   1995 
 237
        [mt_idx_of_unique_mesh, mt_col_idx] = ndgrid(ar_idx_of_unique, 1:size(mt_zi_prob,2)); 
  0.574 
   1995 
 238
        mt_zi_cumu_prob = accumarray([mt_idx_of_unique_mesh(:) mt_col_idx(:)], mt_zi_prob(:)); 
 239 

 240 
        % 5. Adding up
  0.031 
   1995 
 241
        mt_dist_az(ar_idx_full, :) = mt_zi_cumu_prob +  mt_dist_az(ar_idx_full,:); 
  0.001 
   1995 
 242
    end 
 243 
    
 244 

 245 
    %% *f(a,z)*: Check Tolerance and Continuation
 246 

 247 
    % Difference across iterations
  0.048 
    133 
 248
    ar_dist_diff_norm(it_iter) = norm(mt_dist_az - mt_dist_az_cur); 
  0.012 
    133 
 249
    mt_dist_perc_change(it_iter, :) = sum((mt_dist_az ~= mt_dist_az))/(it_endostates_rows_n); 
 250 

 251 
    % Update
  0.004 
    133 
 252
    mt_dist_az_cur = mt_dist_az; 
 253 

 254 
    % Print Iteration Results
< 0.001 
    133 
 255
    if (bl_display_dist && (rem(it_iter, it_display_every)==0)) 
 256 
        fprintf('Dist it_iter:%d, fl_dist_diff:%d\n', it_iter, ar_dist_diff_norm(it_iter));
 257 
        tb_hist_iter = array2table([sum(mt_dist_az_cur,1); std(mt_dist_az_cur,1); ...
 258 
                                    mt_dist_az_cur(1,:); mt_dist_az_cur(it_endostates_rows_n,:)]);
 259 
        tb_hist_iter.Properties.VariableNames = strcat('z', string((1:size(mt_dist_az,2))));
 260 
        tb_hist_iter.Properties.RowNames = {'mdist','sddist', 'Ldist', 'Hdist'};
 261 
        disp('mdist = sum(mt_dist_az_cur,1) = sum_{a}(p(a)|z)')
 262 
        disp('sddist = std(mt_pol_a_cur,1) = std_{a}(p(a)|z)')
 263 
        disp('Ldist = mt_dist_az_cur(1,:) = p(min(a)|z)')
 264 
        disp('Hdist = mt_dist_az_cur(it_a_n,:) = p(max(a)|z)')
 265 
        disp(tb_hist_iter);
 266 
    end
 267 

 268 
    % Continuation Conditions:
< 0.001 
    133 
 269
    if (it_iter == (it_maxiter_dist + 1)) 
< 0.001 
      1 
 270
        bl_histiter_continue = false; 
< 0.001 
    132 
 271
    elseif ((it_iter == it_maxiter_dist) || ... 
    132 
 272
            (ar_dist_diff_norm(it_iter) < fl_tol_dist)) 
< 0.001 
      1 
 273
        it_iter_last = it_iter; 
< 0.001 
      1 
 274
        it_iter = it_maxiter_dist; 
< 0.001 
      1 
 275
    end 
 276 

< 0.001 
    133 
 277
end 
 278 

 279 
%% End Time and Profiler
 280 

 281 
% End Timer
< 0.001 
      1 
 282
if (bl_time) 
< 0.001 
      1 
 283
    toc; 
< 0.001 
      1 
 284
end 
 285 

 286 
% End Profile
< 0.001 
      1 
 287
if (bl_profile_dist) 
 288 
    profile off
 289 
    profile viewer
 290 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 291 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 292 
end
 293 

 294 
%% *f(y), f(c), f(a)*: Generate Key Distributional Statistics for Each outcome
 295 
% Having derived f(a,z) the probability mass function of the joint discrete
 296 
% random variables, we now obtain distributional statistics. Note that we
 297 
% know f(a,z), and we also know relevant policy functions a'(a,z), c(a,z),
 298 
% or other policy functions. We can simulate any choices that are a
 299 
% function of the random variables (a,z), using f(a,z). We call function
 300 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solvepost/html/ff_az_ds_post_stats.html
 301 
% ff_az_ds_post_stats> which uses
 302 
% <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html
 303 
% fft_disc_rand_var_stats> and
 304 
% <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html
 305 
% fft_disc_rand_var_mass2outcomes> to compute various statistics of
 306 
% interest.
 307 

< 0.001 
      1 
 308
bl_input_override = true; 
  0.343 
      1 
 309
result_map = ff_az_ds_post_stats(support_map, result_map, mt_dist_az, bl_input_override); 
 310 

< 0.001 
      1 
 311
end 

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