This is a static copy of a profile report

Home

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

Home

ff_wkz_vf (Calls: 1, Time: 593.191 s)
Generated 03-Jul-2019 20:22:30 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_akz\solve\ff_wkz_vf.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
211
ar_val_cur(it_cohp_k) = f_util...
96581250203.237 s34.3%
212
fl_u_neg_c = f_util_crra(fl_c_...
96581250161.534 s27.2%
204
fl_c = f_cons(fl_coh, fl_w_ast...
96581250156.847 s26.4%
222
ar_val_cur(it_cohp_k) = fl_u_n...
2841806731.307 s5.3%
218
ar_val_cur(it_cohp_k) = ar_val...
965812505.538 s0.9%
All other lines  34.728 s5.9%
Totals  593.191 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function193162500174.901 s29.5%
...coh,bprime,kprime)(coh-kprime-bprime)anonymous function9658125077.547 s13.1%
ff_wkz_evffunction1010.122 s0.0%
meanfunction1010.014 s0.0%
Self time (built-ins, overhead, etc.)  340.606 s57.4%
Totals  593.191 s100% 
Code Analyzer results
Line numberMessage
113The value assigned here to 'ar_k_mesha' appears to be unused. Consider replacing it by ~.
116The value assigned here to 'f_coh' appears to be unused. Consider replacing it by ~.
120The value assigned here to 'fl_r_save' appears to be unused. Consider replacing it by ~.
120The value assigned here to 'fl_r_borr' appears to be unused. Consider replacing it by ~.
120The value assigned here to 'fl_wage' appears to be unused. Consider replacing it by ~.
Coverage results
Show coverage for parent directory
Total lines in function313
Non-code lines (comments, blank lines)165
Code lines (lines that can run)148
Code lines that did run60
Code lines that did not run88
Coverage (did run/can run)40.54 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_wkz_vf(varargin)
   8 
%% FF_WKZ_VF 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 two step solution
  11 
% version of
  12 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html
  13 
% ff_akz_vf>. See
  14 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_evf.html
  15 
% ff_wkz_evf> for details about the second stage. 
  16 
%
  17 
% @param param_map container parameter container
  18 
%
  19 
% @param support_map container support container
  20 
%
  21 
% @param armt_map container container with states, choices and shocks
  22 
% grids that are inputs for grid based solution algorithm
  23 
%
  24 
% @param func_map container container with function handles for
  25 
% consumption cash-on-hand etc.
  26 
%
  27 
% @return result_map container contains policy function matrix, value
  28 
% function matrix, iteration results, and policy function, value function
  29 
% and iteration results tables. 
  30 
%
  31 
% keys included in result_map:
  32 
%
  33 
% * mt_val matrix states_n by shock_n matrix of converged value function grid
  34 
% * mt_pol_a matrix states_n by shock_n matrix of converged policy function grid
  35 
% * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
  36 
% difference between iteration
  37 
% * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
  38 
% function difference between iterations
  39 
% * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
  40 
% proportion of grid points at which policy function changed between
  41 
% current and last iteration for each element of shock
  42 
%
  43 
% @example
  44 
%
  45 
% @include
  46 
%
  47 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_evf.html ff_wkz_evf>
  48 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_default_param.html ffs_akz_set_default_param>
  49 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_get_funcgrid.html ffs_akz_get_funcgrid>
  50 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solvepost/html/ff_akz_vf_post.html ff_akz_vf_post>
  51 
%
  52 
% @seealso
  53 
%
  54 
% * concurrent (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html ff_akz_vf>
  55 
% * concurrent (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vec.html ff_akz_vf_vec>
  56 
% * concurrent (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html ff_akz_vf_vecsv>
  57 
% * two-stage (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf.html ff_wkz_vf>
  58 
% * two-stage (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vec.html ff_wkz_vf_vec>
  59 
% * two-stage (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vecsv.html ff_wkz_vf_vecsv>
  60 
% * two-stage + interpolate (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf.html ff_iwkz_vf>
  61 
% * two-stage + interpolate (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vec.html ff_iwkz_vf_vec>
  62 
% * 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>
  63 
%
  64 

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

  71 
it_param_set = 2;
  72 
bl_input_override = true;
  73 
[param_map, support_map] = ffs_akz_set_default_param(it_param_set);
  74 

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

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

  83 
%% Parse Parameters 1
  84 

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

 101 
% append function name
 102 
st_func_name = 'ff_wkz_vf';
 103 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 104 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 105 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 106 

 107 
%% Parse Parameters 2
 108 

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

 131 
%% Initialize Output Matrixes
 132 

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

 140 
%% Initialize Convergence Conditions
 141 

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

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

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

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

 169 
% Value Function Iteration
< 0.001 
      1 
 170
while bl_vfi_continue 
< 0.001 
    101 
 171
    it_iter = it_iter + 1; 
 172 
    
 173 
    %% Solve Second Stage Problem k*(w,z)
 174 
    % This is the key difference between this function and
 175 
    % <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_functions.html
 176 
    % ffs_akz_set_functions> which solves the two stages jointly
 177 
    
< 0.001 
    101 
 178
    bl_input_override = true; 
  0.127 
    101 
 179
    [mt_ev_condi_z_max, ~, mt_ev_condi_z_max_kp, mt_ev_condi_z_max_bp] = ... 
    101 
 180
        ff_wkz_evf(mt_val_cur, param_map, support_map, armt_map, bl_input_override); 
 181 
    
 182 
    %% Solve First Stage Problem w*(z) given k*(w,z)
 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.002 
   1515 
 187
        ar_ev_condi_z_max_z = mt_ev_condi_z_max(:, it_z_i);         
  0.001 
   1515 
 188
        ar_w_kstar_z = mt_ev_condi_z_max_kp(:, it_z_i); 
  0.001 
   1515 
 189
        ar_w_astar_z = mt_ev_condi_z_max_bp(:, it_z_i);         
 190 
        
 191 
        % loop 2: over endogenous states
< 0.001 
   1515 
 192
        for it_coh_j = 1:length(ar_a_meshk) 
 193 
            % Get cash-on-hand which include k,b,z
  0.094 
1931625 
 194
            fl_coh = mt_coh_wkb(it_coh_j, it_z_i); 
 195 
            
 196 
            % loop 3: over choices, only w vector
 197 
            % we choose w(z), know from ff_wkz_evf k*(w,z), b*=w-k*
  1.753 
1931625 
 198
            ar_val_cur = zeros(size(ar_w)); 
  0.119 
1931625 
 199
            for it_cohp_k = 1:length(ar_w)                                 
  3.807 
96581250 
 200
                fl_w_kstar_z = ar_w_kstar_z(it_cohp_k); 
  3.926 
96581250 
 201
                fl_w_astar_z = ar_w_astar_z(it_cohp_k); 
 202 
                
 203 
                % consumption
 156.847 
96581250 
 204
                fl_c = f_cons(fl_coh, fl_w_astar_z, fl_w_kstar_z); 
 205 
                
 206 
                % current utility
  4.127 
96581250 
 207
                if (fl_crra == 1) 
 208 
                    ar_val_cur(it_cohp_k) = f_util_log(fl_c);
 209 
                    fl_u_neg_c = f_util_log(fl_c_min);
  3.451 
96581250 
 210
                else 
 203.237 
96581250 
 211
                    ar_val_cur(it_cohp_k) = f_util_crra(fl_c); 
 161.534 
96581250 
 212
                    fl_u_neg_c = f_util_crra(fl_c_min); 
  3.212 
96581250 
 213
                end 
 214 
                
 215 
                % loop 4: add future utility, integration already done in
 216 
                % ff_wkz_evf
  3.642 
96581250 
 217
                fl_ev_condi_z_max_z = ar_ev_condi_z_max_z(it_cohp_k); 
  5.538 
96581250 
 218
                ar_val_cur(it_cohp_k) = ar_val_cur(it_cohp_k) + fl_beta*fl_ev_condi_z_max_z; 
 219 
                
 220 
                % Replace if negative consumption
  3.402 
96581250 
 221
                if fl_c <= 0 
 31.307 
28418067 
 222
                    ar_val_cur(it_cohp_k) = fl_u_neg_c; 
  1.105 
28418067 
 223
                end 
 224 
                
  3.437 
96581250 
 225
            end 
 226 
            
 227 
            % maximization over loop 3 choices for loop 1+2 states
  1.815 
1931625 
 228
            it_max_lin_idx = find(ar_val_cur == max(ar_val_cur)); 
  0.093 
1931625 
 229
            mt_val(it_coh_j,it_z_i) = ar_val_cur(it_max_lin_idx(1)); 
  0.091 
1931625 
 230
            mt_pol_a(it_coh_j,it_z_i) = ar_w_astar_z(it_max_lin_idx(1)); 
  0.095 
1931625 
 231
            mt_pol_k(it_coh_j,it_z_i) = ar_w_kstar_z(it_max_lin_idx(1)); 
 232 
            
  0.076 
1931625 
 233
        end 
  0.002 
   1515 
 234
    end 
 235 
    
 236 
    %% Check Tolerance and Continuation
 237 
    
 238 
    % Difference across iterations
  0.115 
    101 
 239
    ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur); 
  0.150 
    101 
 240
    ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur) + norm(mt_pol_k - mt_pol_k_cur); 
  0.015 
    101 
 241
    ar_pol_a_perc_change = sum((mt_pol_a ~= mt_pol_a_cur))/(it_ameshk_n); 
  0.011 
    101 
 242
    ar_pol_k_perc_change = sum((mt_pol_k ~= mt_pol_k_cur))/(it_ameshk_n);     
  0.019 
    101 
 243
    mt_pol_perc_change(it_iter, :) = mean([ar_pol_a_perc_change;ar_pol_k_perc_change]); 
 244 
    
 245 
    % Update
  0.012 
    101 
 246
    mt_val_cur = mt_val; 
  0.008 
    101 
 247
    mt_pol_a_cur = mt_pol_a; 
  0.008 
    101 
 248
    mt_pol_k_cur = mt_pol_k; 
 249 
    
 250 
    % Print Iteration Results
< 0.001 
    101 
 251
    if (bl_display && (rem(it_iter, it_display_every)==0)) 
 252 
        fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
 253 
            it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
 254 
        tb_valpol_iter = array2table([mean(mt_val_cur,1);...
 255 
                                      mean(mt_pol_a_cur,1); ...
 256 
                                      mean(mt_pol_k_cur,1); ...
 257 
                                      mt_val_cur(it_ameshk_n,:); ...
 258 
                                      mt_pol_a_cur(it_ameshk_n,:); ...
 259 
                                      mt_pol_k_cur(it_ameshk_n,:)]);
 260 
        tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
 261 
        tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'mak', 'Hval', 'Hap', 'Hak'};
 262 
        disp('mval = mean(mt_val_cur,1), average value over a')
 263 
        disp('map  = mean(mt_pol_a_cur,1), average choice over a')
 264 
        disp('mkp  = mean(mt_pol_k_cur,1), average choice over k')
 265 
        disp('Hval = mt_val_cur(it_ameshk_n,:), highest a state val')
 266 
        disp('Hap = mt_pol_a_cur(it_ameshk_n,:), highest a state choice')
 267 
        disp('mak = mt_pol_k_cur(it_ameshk_n,:), highest k state choice')                
 268 
        disp(tb_valpol_iter);
 269 
    end
 270 
    
 271 
    % Continuation Conditions:
 272 
    % 1. if value function convergence criteria reached
 273 
    % 2. if policy function variation over iterations is less than
 274 
    % threshold
< 0.001 
    101 
 275
    if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
      1 
 276
        bl_vfi_continue = false; 
  0.002 
    100 
 277
    elseif ((it_iter == it_maxiter_val) || ... 
    100 
 278
            (ar_val_diff_norm(it_iter) < fl_tol_val) || ... 
    100 
 279
            (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol)) 
 280 
        % Fix to max, run again to save results if needed
< 0.001 
      1 
 281
        it_iter_last = it_iter; 
< 0.001 
      1 
 282
        it_iter = it_maxiter_val;         
< 0.001 
      1 
 283
    end 
 284 
    
  0.001 
    101 
 285
end 
 286 

 287 
% End Timer
< 0.001 
      1 
 288
if (bl_time) 
< 0.001 
      1 
 289
    toc; 
< 0.001 
      1 
 290
end 
 291 

 292 
% End Profile
< 0.001 
      1 
 293
if (bl_profile) 
  0.004 
      1 
 294
    profile off 
 295 
    profile viewer
 296 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 297 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 298 
end
 299 

 300 
%% Process Optimal Choices
 301 

 302 
result_map = containers.Map('KeyType','char', 'ValueType','any');
 303 
result_map('mt_val') = mt_val;
 304 

 305 
result_map('cl_mt_pol_coh') = {mt_coh_wkb, zeros(1)};
 306 
result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
 307 
result_map('cl_mt_pol_k') = {mt_pol_k, zeros(1)};
 308 
result_map('cl_mt_pol_c') = {f_cons(mt_coh_wkb, mt_pol_a, mt_pol_k), zeros(1)};
 309 
result_map('ar_st_pol_names') = ["cl_mt_pol_coh", "cl_mt_pol_a", "cl_mt_pol_k", "cl_mt_pol_c"];
 310 

 311 
if (bl_post)
 312 
    bl_input_override = true;
 313 
    result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
 314 
    result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
 315 
    result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
 316 
    result_map = ff_akz_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
 317 
end
 318 

 319 
end

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