This is a static copy of a profile report

Home

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

Home

ff_wkz_vf_vec (Calls: 1, Time: 3.828 s)
Generated 03-Jul-2019 19:57:14 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_akz\solve\ff_wkz_vf_vec.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
205
mt_utility = f_util_crra(mt_c)...
15152.145 s56.0%
214
mt_utility(mt_c <= fl_c_min...
15150.685 s17.9%
198
mt_c = f_cons(ar_coh_z', ar_w_...
15150.250 s6.5%
236
[mt_a_meshk_mesh_a_opti, mt_a_...
150.134 s3.5%
237
[mt_k_meshk_mesh_k_opti, mt_k_...
150.132 s3.4%
All other lines  0.483 s12.6%
Totals  3.828 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function30302.108 s55.1%
...coh,bprime,kprime)(coh-kprime-bprime)anonymous function15150.205 s5.4%
ndgridfunction300.171 s4.5%
ff_wkz_evffunction1010.080 s2.1%
meanfunction1010.009 s0.2%
Self time (built-ins, overhead, etc.)  1.256 s32.8%
Totals  3.828 s100% 
Code Analyzer results
Line numberMessage
109The value assigned here to 'ar_w' appears to be unused. Consider replacing it by ~.
114The value assigned here to 'f_coh' appears to be unused. Consider replacing it by ~.
118The value assigned here to 'fl_r_save' appears to be unused. Consider replacing it by ~.
118The value assigned here to 'fl_r_borr' appears to be unused. Consider replacing it by ~.
118The value assigned here to 'fl_wage' appears to be unused. Consider replacing it by ~.
Coverage results
Show coverage for parent directory
Total lines in function334
Non-code lines (comments, blank lines)183
Code lines (lines that can run)151
Code lines that did run61
Code lines that did not run90
Coverage (did run/can run)40.40 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_wkz_vf_vec(varargin)
   8 
%% FF_WKZ_VF_VEC solve infinite horizon exo shock + endo asset problem
   9 
% This program solves the infinite horizon dynamic savings and risky
  10 
% capital asset problem with some ar1 shock. This is the vectorized version
  11 
% of
  12 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf.html
  13 
% ff_wkz_vf>. See that file for more descriptions. 
  14 
%
  15 
% @param param_map container parameter container
  16 
%
  17 
% @param support_map container support container
  18 
%
  19 
% @param armt_map container container with states, choices and shocks
  20 
% grids that are inputs for grid based solution algorithm
  21 
%
  22 
% @param func_map container container with function handles for
  23 
% consumption cash-on-hand etc.
  24 
%
  25 
% @return result_map container contains policy function matrix, value
  26 
% function matrix, iteration results, and policy function, value function
  27 
% and iteration results tables. 
  28 
%
  29 
% keys included in result_map:
  30 
%
  31 
% * mt_val matrix states_n by shock_n matrix of converged value function grid
  32 
% * mt_pol_a matrix states_n by shock_n matrix of converged policy function grid
  33 
% * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
  34 
% difference between iteration
  35 
% * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
  36 
% function difference between iterations
  37 
% * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
  38 
% proportion of grid points at which policy function changed between
  39 
% current and last iteration for each element of shock
  40 
%
  41 
% @example
  42 
%
  43 
% @include
  44 
%
  45 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_evf.html ff_wkz_evf>
  46 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_default_param.html ffs_akz_set_default_param>
  47 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_get_funcgrid.html ffs_akz_get_funcgrid>
  48 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solvepost/html/ff_akz_vf_post.html ff_akz_vf_post>
  49 
%
  50 
% @seealso
  51 
%
  52 
% * concurrent (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html ff_akz_vf>
  53 
% * concurrent (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vec.html ff_akz_vf_vec>
  54 
% * concurrent (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html ff_akz_vf_vecsv>
  55 
% * two-stage (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf.html ff_wkz_vf>
  56 
% * two-stage (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vec.html ff_wkz_vf_vec>
  57 
% * two-stage (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vecsv.html ff_wkz_vf_vecsv>
  58 
% * two-stage + interpolate (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf.html ff_iwkz_vf>
  59 
% * two-stage + interpolate (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vec.html ff_iwkz_vf_vec>
  60 
% * two-stage + interpolate (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vecsv.html ff_iwkz_vf_vecsv>
  61 
%
  62 

  63 
%% Default
  64 
% * it_param_set = 1: quick test
  65 
% * it_param_set = 2: benchmark run
  66 
% * it_param_set = 3: benchmark profile
  67 
% * it_param_set = 4: press publish button
  68 

  69 
it_param_set = 3;
  70 
bl_input_override = true;
  71 
[param_map, support_map] = ffs_akz_set_default_param(it_param_set);
  72 

  73 
% Note: param_map and support_map can be adjusted here or outside to override defaults
  74 
% param_map('it_w_n') = 50;
  75 
% param_map('it_z_n') = 15;
  76 

  77 
% get armt and func map
  78 
[armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
  79 
default_params = {param_map support_map armt_map func_map};
  80 

  81 
%% Parse Parameters 1
  82 

  83 
% if varargin only has param_map and support_map,
  84 
params_len = length(varargin);
  85 
[default_params{1:params_len}] = varargin{:};
  86 
param_map = [param_map; default_params{1}];
  87 
support_map = [support_map; default_params{2}];
  88 
if params_len >= 1 && params_len <= 2
  89 
    % If override param_map, re-generate armt and func if they are not
  90 
    % provided
  91 
    bl_input_override = true;
  92 
    [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override);
  93 
else
  94 
    % Override all
  95 
    armt_map = [armt_map; default_params{3}];
  96 
    func_map = [func_map; default_params{4}];
  97 
end
  98 

  99 
% append function name
 100 
st_func_name = 'ff_wkz_vf_vec';
 101 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 102 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 103 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 104 

 105 
%% Parse Parameters 2
 106 

 107 
% armt_map
 108 
params_group = values(armt_map, {'ar_w', 'ar_z'});
 109 
[ar_w, ar_z] = params_group{:};
 110 
params_group = values(armt_map, {'ar_a_meshk', 'ar_k_mesha', 'mt_coh_wkb', 'it_ameshk_n'});
 111 
[ar_a_meshk, ar_k_mesha, mt_coh_wkb, it_ameshk_n] = params_group{:};
 112 
% func_map
 113 
params_group = values(func_map, {'f_util_log', 'f_util_crra', 'f_cons', 'f_coh'});
 114 
[f_util_log, f_util_crra, f_cons, f_coh] = params_group{:};
 115 
% param_map
 116 
params_group = values(param_map, {'fl_r_save', 'fl_r_borr', 'fl_w',...
 117 
    'it_z_n', 'fl_crra', 'fl_beta', 'fl_c_min'});
 118 
[fl_r_save, fl_r_borr, fl_wage, it_z_n, fl_crra, fl_beta, fl_c_min] = params_group{:};
 119 
params_group = values(param_map, {'it_maxiter_val', 'fl_tol_val', 'fl_tol_pol', 'it_tol_pol_nochange'});
 120 
[it_maxiter_val, fl_tol_val, fl_tol_pol, it_tol_pol_nochange] = params_group{:};
 121 
% support_map
 122 
params_group = values(support_map, {'bl_profile', 'st_profile_path', ...
 123 
    'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
 124 
    'bl_time', 'bl_display', 'it_display_every', 'bl_post'});
 125 
[bl_profile, st_profile_path, ...
 126 
    st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
 127 
    bl_time, bl_display, it_display_every, bl_post] = params_group{:};
 128 

 129 
%% Initialize Output Matrixes
 130 

 131 
mt_val_cur = zeros(length(ar_a_meshk),length(ar_z));
 132 
mt_val = mt_val_cur - 1;
 133 
mt_pol_a = zeros(length(ar_a_meshk),length(ar_z));
 134 
mt_pol_a_cur = mt_pol_a - 1;
 135 
mt_pol_k = zeros(length(ar_a_meshk),length(ar_z));
 136 
mt_pol_k_cur = mt_pol_k - 1;
 137 
mt_pol_idx = zeros(length(ar_a_meshk),length(ar_z));
 138 

 139 
%% Initialize Convergence Conditions
 140 

 141 
bl_vfi_continue = true;
 142 
it_iter = 0;
 143 
ar_val_diff_norm = zeros([it_maxiter_val, 1]);
 144 
ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
 145 
mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
 146 

 147 
%% Iterate Value Function
 148 
% Loop solution with 4 nested loops
 149 
%
 150 
% # loop 1: over exogenous states
 151 
% # loop 2: over endogenous states
 152 
% # loop 3: over choices
 153 
% # loop 4: add future utility, integration--loop over future shocks
 154 
%
 155 

 156 
% Start Profile
 157 
if (bl_profile)
 158 
    close all;
 159 
    profile off;
 160 
    profile on;
< 0.001 
      1 
 161
end 
 162 

 163 
% Start Timer
< 0.001 
      1 
 164
if (bl_time) 
< 0.001 
      1 
 165
    tic; 
< 0.001 
      1 
 166
end 
 167 

 168 
% Value Function Iteration
< 0.001 
      1 
 169
while bl_vfi_continue 
< 0.001 
    101 
 170
    it_iter = it_iter + 1; 
 171 
    
 172 
    %% Solve Second Stage Problem k*(w,z)
 173 
    % This is the key difference between this function and
 174 
    % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_functions.html
 175 
    % ffs_akz_set_functions> which solves the two stages jointly
 176 
    
< 0.001 
    101 
 177
    bl_input_override = true; 
  0.083 
    101 
 178
    [mt_ev_condi_z_max, ~, mt_ev_condi_z_max_kp, mt_ev_condi_z_max_bp] = ... 
    101 
 179
        ff_wkz_evf(mt_val_cur, param_map, support_map, armt_map, bl_input_override); 
 180 
    
 181 
    %% Solve First Stage Problem w*(z) given k*(w,z)
 182 
       
 183 
    % loop 1: over exogenous states
< 0.001 
    101 
 184
    for it_z_i = 1:length(ar_z) 
 185 

 186 
        % Get 2nd Stage Arrays
  0.004 
   1515 
 187
        ar_coh_z = mt_coh_wkb(:,it_z_i); 
< 0.001 
   1515 
 188
        ar_ev_condi_z_max_z = mt_ev_condi_z_max(:, it_z_i);         
< 0.001 
   1515 
 189
        ar_w_kstar_z = mt_ev_condi_z_max_kp(:, it_z_i); 
< 0.001 
   1515 
 190
        ar_w_astar_z = mt_ev_condi_z_max_bp(:, it_z_i); 
 191 

 192 
        % Consumption
 193 
        % Note that compared to
 194 
        % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_functions.html
 195 
        % ffs_akz_set_functions> the mt_c here is much smaller the same
 196 
        % number of columns (states) as in the ffs_akz_set_functions file,
 197 
        % but the number of rows equal to ar_w length.
  0.250 
   1515 
 198
        mt_c = f_cons(ar_coh_z', ar_w_astar_z, ar_w_kstar_z); 
 199 
        
 200 
        % EVAL current utility: N by N, f_util defined earlier
< 0.001 
   1515 
 201
        if (fl_crra == 1) 
 202 
            mt_utility = f_util_log(mt_c);
 203 
            fl_u_neg_c = f_util_log(fl_c_min);            
< 0.001 
   1515 
 204
        else 
  2.145 
   1515 
 205
            mt_utility = f_util_crra(mt_c); 
  0.013 
   1515 
 206
            fl_u_neg_c = f_util_crra(fl_c_min);             
< 0.001 
   1515 
 207
        end 
 208 
                
 209 
        % EVAL add on future utility, N by N + N by 1
 210 
        % do not need: mt_evzp_condi_z = mt_val_cur * ar_z_trans_condi'
 211 
        % step because evf_okz_vec solved already. 
 212 
        
  0.047 
   1515 
 213
        mt_utility = mt_utility + fl_beta*ar_ev_condi_z_max_z; 
  0.685 
   1515 
 214
        mt_utility(mt_c <= fl_c_min) = fl_u_neg_c; 
 215 

 216 
        % Optimization: remember matlab is column major, rows must be
 217 
        % choices, columns must be states
 218 
        % <https://en.wikipedia.org/wiki/Row-_and_column-major_order COLUMN-MAJOR>
  0.091 
   1515 
 219
        [ar_opti_val1_z, ar_opti_idx_z] = max(mt_utility); 
  0.011 
   1515 
 220
        mt_val(:,it_z_i) = ar_opti_val1_z; 
  0.019 
   1515 
 221
        mt_pol_a(:,it_z_i) = ar_w_astar_z(ar_opti_idx_z); 
  0.015 
   1515 
 222
        mt_pol_k(:,it_z_i) = ar_w_kstar_z(ar_opti_idx_z);         
 223 
        
 224 
        %% Obtain Choice Index for Vectorized/Analytical Distribution Programs
 225 
        % For deriving distributions using vectorized and semi-analytical
 226 
        % methods, at convergence, what index do optimal choices correspond
 227 
        % to in terms of the rows of mt_val and mt_pol_a, and mt_pol_k.
  0.001 
   1515 
 228
        if (it_iter == (it_maxiter_val + 1)) 
 229 
            
< 0.001 
     15 
 230
            ar_a_opti = mt_pol_a(:,it_z_i); 
< 0.001 
     15 
 231
            ar_k_opti = mt_pol_k(:,it_z_i); 
 232 
            
 233 
            % For the LHS matrixes here, each column a different optimal
 234 
            % choice, each row a different element of the a_meshk and
 235 
            % k_mesha vectors. 
  0.134 
     15 
 236
            [mt_a_meshk_mesh_a_opti, mt_a_opti_mesh_a_meshk] = ndgrid(ar_a_meshk, ar_a_opti); 
  0.132 
     15 
 237
            [mt_k_meshk_mesh_k_opti, mt_k_opti_mesh_k_meshk] = ndgrid(ar_k_mesha, ar_k_opti); 
 238 
            
 239 
            % For each column (one optimal choice), which row has that
 240 
            % optimal choice's k' and b' values. 
  0.009 
     15 
 241
            mt_a_opti_match = (mt_a_meshk_mesh_a_opti == mt_a_opti_mesh_a_meshk); 
  0.008 
     15 
 242
            mt_k_opti_match = (mt_k_meshk_mesh_k_opti == mt_k_opti_mesh_k_meshk);             
  0.008 
     15 
 243
            mt_ak_joint_match = mt_a_opti_match.*mt_k_opti_match; 
 244 
            
 245 
            % Full index, meaning, not in terms of the w=k'+b' grid's
 246 
            % length, but in terms of the full partial triangular matched
 247 
            % up combination of k' and b'.
  0.006 
     15 
 248
            [~, ar_opti_fullakvec_idx_z] = max(mt_ak_joint_match); 
 249 
            
 250 
            % Save full index
< 0.001 
     15 
 251
            mt_pol_idx(:,it_z_i) = ar_opti_fullakvec_idx_z; 
< 0.001 
     15 
 252
        end 
 253 

  0.002 
   1515 
 254
    end 
 255 
    
 256 
    %% Check Tolerance and Continuation
 257 
    
 258 
    % Difference across iterations
  0.041 
    101 
 259
    ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur); 
  0.074 
    101 
 260
    ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur) + norm(mt_pol_k - mt_pol_k_cur); 
  0.009 
    101 
 261
    ar_pol_a_perc_change = sum((mt_pol_a ~= mt_pol_a_cur))/(it_ameshk_n); 
  0.006 
    101 
 262
    ar_pol_k_perc_change = sum((mt_pol_k ~= mt_pol_k_cur))/(it_ameshk_n);     
  0.012 
    101 
 263
    mt_pol_perc_change(it_iter, :) = mean([ar_pol_a_perc_change;ar_pol_k_perc_change]); 
 264 
    
 265 
    % Update
  0.004 
    101 
 266
    mt_val_cur = mt_val; 
  0.002 
    101 
 267
    mt_pol_a_cur = mt_pol_a; 
  0.002 
    101 
 268
    mt_pol_k_cur = mt_pol_k; 
 269 
    
 270 
    % Print Iteration Results
< 0.001 
    101 
 271
    if (bl_display && (rem(it_iter, it_display_every)==0)) 
 272 
        fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
 273 
            it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
 274 
        tb_valpol_iter = array2table([mean(mt_val_cur,1);...
 275 
                                      mean(mt_pol_a_cur,1); ...
 276 
                                      mean(mt_pol_k_cur,1); ...
 277 
                                      mt_val_cur(it_ameshk_n,:); ...
 278 
                                      mt_pol_a_cur(it_ameshk_n,:); ...
 279 
                                      mt_pol_k_cur(it_ameshk_n,:)]);
 280 
        tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
 281 
        tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'mak', 'Hval', 'Hap', 'Hak'};
 282 
        disp('mval = mean(mt_val_cur,1), average value over a')
 283 
        disp('map  = mean(mt_pol_a_cur,1), average choice over a')
 284 
        disp('mkp  = mean(mt_pol_k_cur,1), average choice over k')
 285 
        disp('Hval = mt_val_cur(it_ameshk_n,:), highest a state val')
 286 
        disp('Hap = mt_pol_a_cur(it_ameshk_n,:), highest a state choice')
 287 
        disp('mak = mt_pol_k_cur(it_ameshk_n,:), highest k state choice')                
 288 
        disp(tb_valpol_iter);
 289 
    end
 290 
    
 291 
    % Continuation Conditions:
 292 
    % 1. if value function convergence criteria reached
 293 
    % 2. if policy function variation over iterations is less than
 294 
    % threshold
< 0.001 
    101 
 295
    if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
      1 
 296
        bl_vfi_continue = false; 
  0.001 
    100 
 297
    elseif ((it_iter == it_maxiter_val) || ... 
    100 
 298
            (ar_val_diff_norm(it_iter) < fl_tol_val) || ... 
    100 
 299
            (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol)) 
 300 
        % Fix to max, run again to save results if needed
< 0.001 
      1 
 301
        it_iter_last = it_iter; 
< 0.001 
      1 
 302
        it_iter = it_maxiter_val;         
< 0.001 
      1 
 303
    end 
 304 
    
< 0.001 
    101 
 305
end 
 306 

 307 
% End Timer
< 0.001 
      1 
 308
if (bl_time) 
< 0.001 
      1 
 309
    toc; 
< 0.001 
      1 
 310
end 
 311 

 312 
% End Profile
< 0.001 
      1 
 313
if (bl_profile) 
  0.005 
      1 
 314
    profile off 
 315 
    profile viewer
 316 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 317 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 318 
end
 319 

 320 
%% Process Optimal Choices
 321 

 322 
result_map = containers.Map('KeyType','char', 'ValueType','any');
 323 
result_map('mt_val') = mt_val;
 324 
result_map('mt_pol_idx') = mt_pol_idx;
 325 

 326 
result_map('cl_mt_pol_coh') = {mt_coh_wkb, zeros(1)};
 327 
result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
 328 
result_map('cl_mt_pol_k') = {mt_pol_k, zeros(1)};
 329 
result_map('cl_mt_pol_c') = {f_cons(mt_coh_wkb, mt_pol_a, mt_pol_k), zeros(1)};
 330 
result_map('ar_st_pol_names') = ["cl_mt_pol_coh", "cl_mt_pol_a", "cl_mt_pol_k", "cl_mt_pol_c"];
 331 

 332 
if (bl_post)
 333 
    bl_input_override = true;
 334 
    result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
 335 
    result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
 336 
    result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
 337 
    result_map = ff_akz_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
 338 
end
 339 

 340 
end

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