This is a static copy of a profile report

Home

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

Home

ff_akz_vf_vecsv (Calls: 1, Time: 3.314 s)
Generated 03-Jul-2019 19:51:39 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_akz\solve\ff_akz_vf_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
241
mt_utility = mt_utility.*(~mt_...
15150.860 s25.9%
246
[ar_opti_val1_z, ar_opti_idx_z...
15150.637 s19.2%
214
mt_utility = f_util_crra(mt_c)...
150.559 s16.9%
235
mt_utility = cl_u_c_store{it_z...
15150.540 s16.3%
220
mt_utility(mt_it_c_valid_idx) ...
150.192 s5.8%
All other lines  0.525 s15.8%
Totals  3.314 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function300.539 s16.3%
...coh,bprime,kprime)(coh-kprime-bprime)anonymous function150.108 s3.3%
meanfunction1010.010 s0.3%
Self time (built-ins, overhead, etc.)  2.657 s80.2%
Totals  3.314 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function335
Non-code lines (comments, blank lines)187
Code lines (lines that can run)148
Code lines that did run55
Code lines that did not run93
Coverage (did run/can run)37.16 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_akz_vf_vecsv(varargin)
   8 
%% FF_AKZ_VF_VECSV 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 shocks. This is the optimized-vectorized version
  11 
% of
  12 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html
  13 
% ff_akz_vf>. See that file for more descriptions. See
  14 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html
  15 
% ff_az_vf_vecsv> for the idea for optimized-vectorized code. This file
  16 
% here is meant to handle savings only or borrowing (without default) will
  17 
% also work as well.
  18 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akbz/solve/html/ff_akbz_vf_vecsv.html
  19 
% ff_akbz_vf_vecsv> version of this code deals with borrowing as well.
  20 
%
  21 
% @param param_map container parameter container
  22 
%
  23 
% @param support_map container support container
  24 
%
  25 
% @param armt_map container container with states, choices and shocks
  26 
% grids that are inputs for grid based solution algorithm
  27 
%
  28 
% @param func_map container container with function handles for
  29 
% consumption cash-on-hand etc.
  30 
%
  31 
% @return result_map container contains policy function matrix, value
  32 
% function matrix, iteration results, and policy function, value function
  33 
% and iteration results tables. 
  34 
%
  35 
% keys included in result_map:
  36 
%
  37 
% * mt_val matrix states_n by shock_n matrix of converged value function grid
  38 
% * mt_pol_a matrix states_n by shock_n matrix of converged policy function
  39 
% grid safe asset
  40 
% * mt_pol_k matrix states_n by shock_n matrix of converged policy function
  41 
% grid risky asset
  42 
% * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
  43 
% difference between iteration
  44 
% * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
  45 
% function difference between iterations
  46 
% * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
  47 
% proportion of grid points at which policy function changed between
  48 
% current and last iteration for each element of shock
  49 
%
  50 
% @example
  51 
%
  52 
%   it_param_set = 2;
  53 
%   [param_map, support_map] = ffs_akz_set_default_param(it_param_set);   
  54 
%   % Simulation Accuracy
  55 
%   param_map('it_w_n') = 750;
  56 
%   param_map('it_ak_n') = param_map('it_w_n');
  57 
%   param_map('it_z_n') = 11;
  58 
%   % Display Parameters
  59 
%   support_map('bl_display') = false;
  60 
%   support_map('bl_display_final') = false;
  61 
%   support_map('bl_time') = true;
  62 
%   support_map('bl_profile') = false;
  63 
%   % Call Program with external parameters that override defaults
  64 
%   ff_akz_vf_vecsv(param_map, support_map);
  65 
% 
  66 
% @include
  67 
%
  68 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_default_param.html ffs_akz_set_default_param>
  69 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_get_funcgrid.html ffs_akz_get_funcgrid>
  70 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solvepost/html/ff_akz_vf_post.html ff_akz_vf_post>
  71 
%
  72 
% @seealso
  73 
%
  74 
% * concurrent (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html ff_akz_vf>
  75 
% * concurrent (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vec.html ff_akz_vf_vec>
  76 
% * concurrent (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html ff_akz_vf_vecsv>
  77 
% * two-stage (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf.html ff_wkz_vf>
  78 
% * two-stage (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vec.html ff_wkz_vf_vec>
  79 
% * two-stage (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vecsv.html ff_wkz_vf_vecsv>
  80 
% * two-stage + interpolate (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf.html ff_iwkz_vf>
  81 
% * two-stage + interpolate (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vec.html ff_iwkz_vf_vec>
  82 
% * 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>
  83 
%
  84 

  85 
%% Default
  86 
% * it_param_set = 1: quick test
  87 
% * it_param_set = 2: benchmark run
  88 
% * it_param_set = 3: benchmark profile
  89 
% * it_param_set = 4: press publish button
  90 

  91 
it_param_set = 3;
  92 
bl_input_override = true;
  93 
[param_map, support_map] = ffs_akz_set_default_param(it_param_set);
  94 

  95 
% Note: param_map and support_map can be adjusted here or outside to override defaults
  96 
% param_map('it_w_n') = 50;
  97 
% param_map('it_z_n') = 15;
  98 

  99 
% get armt and func map
 100 
[armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
 101 
default_params = {param_map support_map armt_map func_map};
 102 

 103 
%% Parse Parameters 1
 104 

 105 
% if varargin only has param_map and support_map,
 106 
params_len = length(varargin);
 107 
[default_params{1:params_len}] = varargin{:};
 108 
param_map = [param_map; default_params{1}];
 109 
support_map = [support_map; default_params{2}];
 110 
if params_len >= 1 && params_len <= 2
 111 
    % If override param_map, re-generate armt and func if they are not
 112 
    % provided
 113 
    bl_input_override = true;
 114 
    [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override);
 115 
else
 116 
    % Override all
 117 
    armt_map = [armt_map; default_params{3}];
 118 
    func_map = [func_map; default_params{4}];
 119 
end
 120 

 121 
% append function name
 122 
st_func_name = 'ff_akz_vf_vecsv';
 123 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 124 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 125 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 126 

 127 
%% Parse Parameters 2
 128 

 129 
% armt_map
 130 
params_group = values(armt_map, {'mt_z_trans', 'ar_z'});
 131 
[ mt_z_trans, ar_z] = params_group{:};
 132 
params_group = values(armt_map, {'ar_a_meshk', 'ar_k_mesha', 'mt_coh_wkb', 'it_ameshk_n'});
 133 
[ar_a_meshk, ar_k_mesha, mt_coh_wkb, it_ameshk_n] = params_group{:};
 134 
% func_map
 135 
params_group = values(func_map, {'f_util_log', 'f_util_crra', 'f_cons', 'f_coh'});
 136 
[f_util_log, f_util_crra, f_cons, f_coh] = params_group{:};
 137 
% param_map
 138 
params_group = values(param_map, {'it_z_n', 'fl_crra', 'fl_beta', 'fl_c_min'});
 139 
[it_z_n, fl_crra, fl_beta, fl_c_min] = params_group{:};
 140 
params_group = values(param_map, {'it_maxiter_val', 'fl_tol_val', 'fl_tol_pol', 'it_tol_pol_nochange'});
 141 
[it_maxiter_val, fl_tol_val, fl_tol_pol, it_tol_pol_nochange] = params_group{:};
 142 
% support_map
 143 
params_group = values(support_map, {'bl_profile', 'st_profile_path', ...
 144 
    'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
 145 
    'bl_time', 'bl_display', 'it_display_every', 'bl_post'});
 146 
[bl_profile, st_profile_path, ...
 147 
    st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
 148 
    bl_time, bl_display, it_display_every, bl_post] = params_group{:};
 149 

 150 
%% Initialize Output Matrixes
 151 

 152 
mt_val_cur = zeros(length(ar_a_meshk),length(ar_z));
 153 
mt_val = mt_val_cur - 1;
 154 
mt_pol_a = zeros(length(ar_a_meshk),length(ar_z));
 155 
mt_pol_a_cur = mt_pol_a - 1;
 156 
mt_pol_k = zeros(length(ar_a_meshk),length(ar_z));
 157 
mt_pol_k_cur = mt_pol_k - 1;
 158 
mt_pol_idx = zeros(length(ar_a_meshk),length(ar_z));
 159 

 160 
% We did not need these in ff_oz_vf or ff_oz_vf_vec
 161 
% see
 162 
% <https://fanwangecon.github.io/M4Econ/support/speed/partupdate/fs_u_c_partrepeat_main.html
 163 
% fs_u_c_partrepeat_main> for why store using cells.
 164 
cl_u_c_store = cell([it_z_n, 1]);
 165 
cl_c_valid_idx = cell([it_z_n, 1]);
 166 

 167 
%% Initialize Convergence Conditions
 168 

 169 
bl_vfi_continue = true;
 170 
it_iter = 0;
 171 
ar_val_diff_norm = zeros([it_maxiter_val, 1]);
 172 
ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
 173 
mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
 174 

 175 
%% Iterate Value Function
 176 
% Loop solution with 4 nested loops
 177 
%
 178 
% # loop 1: over exogenous states
 179 
% # loop 2: over endogenous states
 180 
% # loop 3: over choices
 181 
% # loop 4: add future utility, integration--loop over future shocks
 182 
%
 183 

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

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

 196 
% Value Function Iteration
< 0.001 
      1 
 197
while bl_vfi_continue 
< 0.001 
    101 
 198
    it_iter = it_iter + 1; 
 199 
    
 200 
    %% Solve Optimization Problem Current Iteration
 201 
    
 202 
    % loop 1: over exogenous states
< 0.001 
    101 
 203
    for it_z_i = 1:length(ar_z) 
 204 
        
< 0.001 
   1515 
 205
        if (it_iter == 1) 
 206 
            % Consumption
  0.129 
     15 
 207
            mt_c = f_cons(mt_coh_wkb(:, it_z_i)', ar_a_meshk, ar_k_mesha); 
 208 

 209 
            % EVAL current utility: N by N, f_util defined earlier
< 0.001 
     15 
 210
            if (fl_crra == 1) 
 211 
                mt_utility = f_util_log(mt_c);
 212 
                fl_u_neg_c = f_util_log(fl_c_min);            
< 0.001 
     15 
 213
            else 
  0.559 
     15 
 214
                mt_utility = f_util_crra(mt_c); 
  0.002 
     15 
 215
                fl_u_neg_c = f_util_crra(fl_c_min);             
< 0.001 
     15 
 216
            end 
 217 

 218 
            % Eliminate Complex Numbers
  0.008 
     15 
 219
            mt_it_c_valid_idx = (mt_c <= fl_c_min); 
  0.192 
     15 
 220
            mt_utility(mt_it_c_valid_idx) = fl_u_neg_c;         
 221 
            
 222 
            % Store in cells
< 0.001 
     15 
 223
            cl_u_c_store{it_z_i} = mt_utility;             
  0.002 
     15 
 224
            cl_c_valid_idx{it_z_i} = mt_it_c_valid_idx; 
 225 
            
< 0.001 
     15 
 226
        end 
 227 
        
 228 
        % f(z'|z)
  0.002 
   1515 
 229
        ar_z_trans_condi = mt_z_trans(it_z_i,:); 
 230 
        
 231 
        % EVAL EV((A',K'),Z'|Z) = V((A',K'),Z') x p(z'|z)', (N by Z) x (Z by 1) = N by 1
  0.024 
   1515 
 232
        mt_evzp_condi_z = mt_val_cur * ar_z_trans_condi'; 
 233 
        
 234 
        % EVAL add on future utility, N by N + N by 1
  0.540 
   1515 
 235
        mt_utility = cl_u_c_store{it_z_i}  + fl_beta*mt_evzp_condi_z; 
 236 
        
 237 
        % Index update
 238 
        % using the method below is much faster than index replace
 239 
        % see <https://fanwangecon.github.io/M4Econ/support/speed/index/fs_subscript.html fs_subscript>
  0.004 
   1515 
 240
        mt_it_c_valid_idx = cl_c_valid_idx{it_z_i};         
  0.860 
   1515 
 241
        mt_utility = mt_utility.*(~mt_it_c_valid_idx) + fl_u_neg_c*(mt_it_c_valid_idx); 
 242 

 243 
        % Optimization: remember matlab is column major, rows must be
 244 
        % choices, columns must be states
 245 
        % <https://en.wikipedia.org/wiki/Row-_and_column-major_order COLUMN-MAJOR>  
  0.637 
   1515 
 246
        [ar_opti_val1_z, ar_opti_idx_z] = max(mt_utility); 
  0.038 
   1515 
 247
        mt_val(:,it_z_i) = ar_opti_val1_z; 
  0.035 
   1515 
 248
        mt_pol_a(:,it_z_i) = ar_a_meshk(ar_opti_idx_z); 
  0.026 
   1515 
 249
        mt_pol_k(:,it_z_i) = ar_k_mesha(ar_opti_idx_z);         
< 0.001 
   1515 
 250
        if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
     15 
 251
            mt_pol_idx(:,it_z_i) = ar_opti_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.091 
    101 
 259
    ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur); 
  0.110 
    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.008 
    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.013 
    101 
 263
    mt_pol_perc_change(it_iter, :) = mean([ar_pol_a_perc_change;ar_pol_k_perc_change]); 
 264 
    
 265 
    % Update
  0.007 
    101 
 266
    mt_val_cur = mt_val; 
  0.004 
    101 
 267
    mt_pol_a_cur = mt_pol_a; 
  0.004 
    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 
mt_coh = f_coh(ar_z, ar_a_meshk, ar_k_mesha);
 327 
result_map('cl_mt_pol_coh') = {mt_coh, zeros(1)};
 328 
result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
 329 
result_map('cl_mt_pol_k') = {mt_pol_k, zeros(1)};
 330 
result_map('cl_mt_pol_c') = {f_cons(mt_coh, mt_pol_a, mt_pol_k), zeros(1)};
 331 
result_map('ar_st_pol_names') = ["cl_mt_pol_coh", "cl_mt_pol_a", "cl_mt_pol_k", "cl_mt_pol_c"];
 332 

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

 341 
end

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