This is a static copy of a profile report

Home

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

Home

ff_akz_vf_vec (Calls: 1, Time: 91.716 s)
Generated 03-Jul-2019 19:58:45 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_akz\solve\ff_akz_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
200
mt_utility = f_util_crra(mt_c)...
151556.989 s62.1%
212
mt_utility(mt_c <= fl_c_min...
151520.165 s22.0%
193
mt_c = f_cons(mt_coh_wkb(:, it...
151512.617 s13.8%
211
mt_utility = mt_utility + fl_b...
15150.859 s0.9%
217
[ar_opti_val1_z, ar_opti_idx_z...
15150.633 s0.7%
All other lines  0.451 s0.5%
Totals  91.716 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function303052.569 s57.3%
...coh,bprime,kprime)(coh-kprime-bprime)anonymous function151510.362 s11.3%
meanfunction1010.011 s0.0%
Self time (built-ins, overhead, etc.)  28.773 s31.4%
Totals  91.716 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function305
Non-code lines (comments, blank lines)167
Code lines (lines that can run)138
Code lines that did run48
Code lines that did not run90
Coverage (did run/can run)34.78 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_akz_vf_vec(varargin)
   8 
%% FF_AKZ_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_akz_vf.html
  13 
% ff_akz_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
  33 
% grid safe asset
  34 
% * mt_pol_k matrix states_n by shock_n matrix of converged policy function
  35 
% grid risky asset
  36 
% * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
  37 
% difference between iteration
  38 
% * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
  39 
% function difference between iterations
  40 
% * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
  41 
% proportion of grid points at which policy function changed between
  42 
% current and last iteration for each element of shock
  43 
%
  44 
% @example
  45 
%
  46 
%   it_param_set = 2;
  47 
%   [param_map, support_map] = ffs_akz_set_default_param(it_param_set);   
  48 
%   % Simulation Accuracy
  49 
%   param_map('it_w_n') = 750;
  50 
%   param_map('it_ak_n') = param_map('it_w_n');
  51 
%   param_map('it_z_n') = 11;
  52 
%   % Display Parameters
  53 
%   support_map('bl_display') = false;
  54 
%   support_map('bl_display_final') = false;
  55 
%   support_map('bl_time') = true;
  56 
%   support_map('bl_profile') = false;
  57 
%   % Call Program with external parameters that override defaults
  58 
%   ff_akz_vf_vec(param_map, support_map);
  59 
% 
  60 
% @include
  61 
%
  62 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_default_param.html ffs_akz_set_default_param>
  63 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_get_funcgrid.html ffs_akz_get_funcgrid>
  64 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solvepost/html/ff_akz_vf_post.html ff_akz_vf_post>
  65 
%
  66 
% @seealso
  67 
%
  68 
% * concurrent (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html ff_akz_vf>
  69 
% * concurrent (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vec.html ff_akz_vf_vec>
  70 
% * concurrent (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html ff_akz_vf_vecsv>
  71 
% * two-stage (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf.html ff_wkz_vf>
  72 
% * two-stage (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vec.html ff_wkz_vf_vec>
  73 
% * two-stage (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vecsv.html ff_wkz_vf_vecsv>
  74 
% * two-stage + interpolate (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf.html ff_iwkz_vf>
  75 
% * two-stage + interpolate (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vec.html ff_iwkz_vf_vec>
  76 
% * 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>
  77 
%
  78 

  79 
%% Default
  80 
% * it_param_set = 1: quick test
  81 
% * it_param_set = 2: benchmark run
  82 
% * it_param_set = 3: benchmark profile
  83 
% * it_param_set = 4: press publish button
  84 

  85 
it_param_set = 3;
  86 
bl_input_override = true;
  87 
[param_map, support_map] = ffs_akz_set_default_param(it_param_set);
  88 

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

  93 
% get armt and func map
  94 
[armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
  95 
default_params = {param_map support_map armt_map func_map};
  96 

  97 
%% Parse Parameters 1
  98 

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

 115 
% append function name
 116 
st_func_name = 'ff_akz_vf_vec';
 117 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 118 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 119 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 120 

 121 
%% Parse Parameters 2
 122 

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

 144 
%% Initialize Output Matrixes
 145 

 146 
mt_val_cur = zeros(length(ar_a_meshk),length(ar_z));
 147 
mt_val = mt_val_cur - 1;
 148 
mt_pol_a = zeros(length(ar_a_meshk),length(ar_z));
 149 
mt_pol_a_cur = mt_pol_a - 1;
 150 
mt_pol_k = zeros(length(ar_a_meshk),length(ar_z));
 151 
mt_pol_k_cur = mt_pol_k - 1;
 152 
mt_pol_idx = zeros(length(ar_a_meshk),length(ar_z));
 153 

 154 
%% Initialize Convergence Conditions
 155 

 156 
bl_vfi_continue = true;
 157 
it_iter = 0;
 158 
ar_val_diff_norm = zeros([it_maxiter_val, 1]);
 159 
ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
 160 
mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
 161 

 162 
%% Iterate Value Function
 163 
% Loop solution with 4 nested loops
 164 
%
 165 
% # loop 1: over exogenous states
 166 
% # loop 2: over endogenous states
 167 
% # loop 3: over choices
 168 
% # loop 4: add future utility, integration--loop over future shocks
 169 
%
 170 

 171 
% Start Profile
 172 
if (bl_profile)
 173 
    close all;
 174 
    profile off;
 175 
    profile on;
< 0.001 
      1 
 176
end 
 177 

 178 
% Start Timer
< 0.001 
      1 
 179
if (bl_time) 
< 0.001 
      1 
 180
    tic; 
< 0.001 
      1 
 181
end 
 182 

 183 
% Value Function Iteration
< 0.001 
      1 
 184
while bl_vfi_continue 
< 0.001 
    101 
 185
    it_iter = it_iter + 1; 
 186 
    
 187 
    %% Solve Optimization Problem Current Iteration
 188 
    
 189 
    % loop 1: over exogenous states
< 0.001 
    101 
 190
    for it_z_i = 1:length(ar_z) 
 191 
        
 192 
        % Consumption
 12.617 
   1515 
 193
        mt_c = f_cons(mt_coh_wkb(:, it_z_i)', ar_a_meshk, ar_k_mesha); 
 194 
        
 195 
        % EVAL current utility: N by N, f_util defined earlier
  0.001 
   1515 
 196
        if (fl_crra == 1) 
 197 
            mt_utility = f_util_log(mt_c);
 198 
            fl_u_neg_c = f_util_log(fl_c_min);            
< 0.001 
   1515 
 199
        else 
 56.989 
   1515 
 200
            mt_utility = f_util_crra(mt_c); 
  0.048 
   1515 
 201
            fl_u_neg_c = f_util_crra(fl_c_min);             
< 0.001 
   1515 
 202
        end 
 203 

 204 
        % f(z'|z)
  0.004 
   1515 
 205
        ar_z_trans_condi = mt_z_trans(it_z_i,:); 
 206 
        
 207 
        % 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.040 
   1515 
 208
        mt_evzp_condi_z = mt_val_cur * ar_z_trans_condi'; 
 209 
        
 210 
        % EVAL add on future utility, N by N + N by 1
  0.859 
   1515 
 211
        mt_utility = mt_utility + fl_beta*mt_evzp_condi_z; 
 20.165 
   1515 
 212
        mt_utility(mt_c <= fl_c_min) = fl_u_neg_c; 
 213 

 214 
        % Optimization: remember matlab is column major, rows must be
 215 
        % choices, columns must be states
 216 
        % <https://en.wikipedia.org/wiki/Row-_and_column-major_order COLUMN-MAJOR>
  0.633 
   1515 
 217
        [ar_opti_val1_z, ar_opti_idx_z] = max(mt_utility); 
  0.049 
   1515 
 218
        mt_val(:,it_z_i) = ar_opti_val1_z; 
  0.038 
   1515 
 219
        mt_pol_a(:,it_z_i) = ar_a_meshk(ar_opti_idx_z); 
  0.024 
   1515 
 220
        mt_pol_k(:,it_z_i) = ar_k_mesha(ar_opti_idx_z);         
  0.001 
   1515 
 221
        if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
     15 
 222
            mt_pol_idx(:,it_z_i) = ar_opti_idx_z; 
< 0.001 
     15 
 223
        end 
 224 

  0.002 
   1515 
 225
    end 
 226 
    
 227 
    %% Check Tolerance and Continuation
 228 
    
 229 
    % Difference across iterations
  0.083 
    101 
 230
    ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur); 
  0.101 
    101 
 231
    ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur) + norm(mt_pol_k - mt_pol_k_cur); 
  0.010 
    101 
 232
    ar_pol_a_perc_change = sum((mt_pol_a ~= mt_pol_a_cur))/(it_ameshk_n); 
  0.008 
    101 
 233
    ar_pol_k_perc_change = sum((mt_pol_k ~= mt_pol_k_cur))/(it_ameshk_n);     
  0.015 
    101 
 234
    mt_pol_perc_change(it_iter, :) = mean([ar_pol_a_perc_change;ar_pol_k_perc_change]); 
 235 
    
 236 
    % Update
  0.008 
    101 
 237
    mt_val_cur = mt_val; 
  0.004 
    101 
 238
    mt_pol_a_cur = mt_pol_a; 
  0.004 
    101 
 239
    mt_pol_k_cur = mt_pol_k; 
 240 
    
 241 
    % Print Iteration Results
< 0.001 
    101 
 242
    if (bl_display && (rem(it_iter, it_display_every)==0)) 
 243 
        fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
 244 
            it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
 245 
        tb_valpol_iter = array2table([mean(mt_val_cur,1);...
 246 
                                      mean(mt_pol_a_cur,1); ...
 247 
                                      mean(mt_pol_k_cur,1); ...
 248 
                                      mt_val_cur(it_ameshk_n,:); ...
 249 
                                      mt_pol_a_cur(it_ameshk_n,:); ...
 250 
                                      mt_pol_k_cur(it_ameshk_n,:)]);
 251 
        tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
 252 
        tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'mak', 'Hval', 'Hap', 'Hak'};
 253 
        disp('mval = mean(mt_val_cur,1), average value over a')
 254 
        disp('map  = mean(mt_pol_a_cur,1), average choice over a')
 255 
        disp('mkp  = mean(mt_pol_k_cur,1), average choice over k')
 256 
        disp('Hval = mt_val_cur(it_ameshk_n,:), highest a state val')
 257 
        disp('Hap = mt_pol_a_cur(it_ameshk_n,:), highest a state choice')
 258 
        disp('mak = mt_pol_k_cur(it_ameshk_n,:), highest k state choice')                
 259 
        disp(tb_valpol_iter);
 260 
    end
 261 
    
 262 
    % Continuation Conditions:
 263 
    % 1. if value function convergence criteria reached
 264 
    % 2. if policy function variation over iterations is less than
 265 
    % threshold
< 0.001 
    101 
 266
    if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
      1 
 267
        bl_vfi_continue = false; 
  0.001 
    100 
 268
    elseif ((it_iter == it_maxiter_val) || ... 
    100 
 269
            (ar_val_diff_norm(it_iter) < fl_tol_val) || ... 
    100 
 270
            (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol)) 
 271 
        % Fix to max, run again to save results if needed
< 0.001 
      1 
 272
        it_iter_last = it_iter; 
< 0.001 
      1 
 273
        it_iter = it_maxiter_val;         
< 0.001 
      1 
 274
    end 
 275 
    
< 0.001 
    101 
 276
end 
 277 

 278 
% End Timer
< 0.001 
      1 
 279
if (bl_time) 
< 0.001 
      1 
 280
    toc; 
< 0.001 
      1 
 281
end 
 282 

 283 
% End Profile
< 0.001 
      1 
 284
if (bl_profile) 
  0.004 
      1 
 285
    profile off 
 286 
    profile viewer
 287 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 288 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 289 
end
 290 

 291 
%% Process Optimal Choices
 292 

 293 
result_map = containers.Map('KeyType','char', 'ValueType','any');
 294 
result_map('mt_val') = mt_val;
 295 

 296 
mt_coh = f_coh(ar_z, ar_a_meshk, ar_k_mesha);
 297 
result_map('cl_mt_pol_coh') = {mt_coh, zeros(1)};
 298 
result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
 299 
result_map('cl_mt_pol_k') = {mt_pol_k, zeros(1)};
 300 
result_map('cl_mt_pol_c') = {f_cons(mt_coh, mt_pol_a, mt_pol_k), zeros(1)};
 301 
result_map('ar_st_pol_names') = ["cl_mt_pol_coh", "cl_mt_pol_a", "cl_mt_pol_k", "cl_mt_pol_c"];
 302 

 303 
if (bl_post)
 304 
    bl_input_override = true;
 305 
    result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
 306 
    result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
 307 
    result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
 308 
    result_map = ff_akz_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
 309 
end
 310 

 311 
end

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