This is a static copy of a profile report

Home

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

Home

ff_akz_vf (Calls: 1, Time: 25618.244 s)
Generated 04-Jul-2019 03:30:45 using performance time.
function in file C:\Users\fan\CodeDynaAsset\m_akz\solve\ff_akz_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
236
ar_val_cur(it_cohp_k) = f_util...
24628218757814.828 s30.5%
247
ar_val_cur(it_cohp_k) = fl_u_n...
10890372475755.462 s22.5%
237
fl_u_neg_c = f_util_crra(fl_c_...
24628218754128.946 s16.1%
229
fl_c = f_cons(fl_coh, fl_ap, f...
24628218753975.264 s15.5%
242
ar_val_cur(it_cohp_k) = ar_val...
369423281251984.555 s7.7%
All other lines  1959.188 s7.6%
Totals  25618.244 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
...c)(((c).^(1-fl_crra)-1)./(1-fl_crra))anonymous function49256437504582.183 s17.9%
...coh,bprime,kprime)(coh-kprime-bprime)anonymous function24628218752002.859 s7.8%
meanfunction1010.015 s0.0%
Self time (built-ins, overhead, etc.)  19033.187 s74.3%
Totals  25618.244 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function339
Non-code lines (comments, blank lines)195
Code lines (lines that can run)144
Code lines that did run55
Code lines that did not run89
Coverage (did run/can run)38.19 %
Function listing
time 
Calls 
 line
   7 
function result_map = ff_akz_vf(varargin)
   8 
%% FF_AKZ_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. The two assets could be a safe
  11 
% bond and a risky stock if the risky asset has constant return to scale.
  12 
% Alternatively, the risky asset could be a capital investment asset with
  13 
% decreasing return to scale. There is risky component to the risky capital
  14 
% investment, but one could also potentially resale a fraction of the risky
  15 
% capital after depreciation, giving the household/investor/entrepreur a
  16 
% safe minimum return to risky investment. The state variable is the
  17 
% cash-on-hand, which is determined by risky and safe asset as well as the
  18 
% shock jointly.
  19 
%
  20 
% This problem is more computationally intensive to solve
  21 
% compared to the single asset problem, shown here:
  22 
% <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf.html
  23 
% ff_az_vf>. Here I show the looped solution. As before, as we expand the 
  24 
% state and choices spaces to have a looped slow version of the code so
  25 
% that one could debug and make sure the model works. 
  26 
%
  27 
% Vectorized, optimized vectorized versions of the code are shown here:
  28 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vec.html
  29 
% ff_akz_vf_vec> and here:
  30 
% <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html
  31 
% ff_akz_vf_vecsv>. These improve upon the speed, yet the
  32 
% speed is still not as fast as we need for some applications. 
  33 
%
  34 
% The basic forms of the one and two asset problems have analytically
  35 
% tractable mathmatical solutions. The benefit of these grid based solution
  36 
% algorithm is that we are able to add very flexible constraints to the
  37 
% problem or incorporate additional discrete choices that make the
  38 
% underlying problem non-continous and non-differentiable and mathamtically
  39 
% untractable.
  40 
%
  41 
% @param param_map container parameter container
  42 
%
  43 
% @param support_map container support container
  44 
%
  45 
% @param armt_map container container with states, choices and shocks
  46 
% grids that are inputs for grid based solution algorithm
  47 
%
  48 
% @param func_map container container with function handles for
  49 
% consumption cash-on-hand etc.
  50 
%
  51 
% @return result_map container contains policy function matrix, value
  52 
% function matrix, iteration results, and policy function, value function
  53 
% and iteration results tables. 
  54 
%
  55 
% keys included in result_map:
  56 
%
  57 
% * mt_val matrix states_n by shock_n matrix of converged value function grid
  58 
% * mt_pol_a matrix states_n by shock_n matrix of converged policy function
  59 
% grid safe asset
  60 
% * mt_pol_k matrix states_n by shock_n matrix of converged policy function
  61 
% grid risky asset
  62 
% * ar_val_diff_norm array if bl_post = true it_iter_last by 1 val function
  63 
% difference between iteration
  64 
% * ar_pol_diff_norm array if bl_post = true it_iter_last by 1 policy
  65 
% function difference between iterations
  66 
% * mt_pol_perc_change matrix if bl_post = true it_iter_last by shock_n the
  67 
% proportion of grid points at which policy function changed between
  68 
% current and last iteration for each element of shock
  69 
%
  70 
% @example
  71 
%
  72 
%   it_param_set = 2;
  73 
%   [param_map, support_map] = ffs_akz_set_default_param(it_param_set);   
  74 
%   % Simulation Accuracy
  75 
%   param_map('it_w_n') = 750;
  76 
%   param_map('it_ak_n') = param_map('it_w_n');
  77 
%   param_map('it_z_n') = 11;
  78 
%   % Display Parameters
  79 
%   support_map('bl_display') = false;
  80 
%   support_map('bl_display_final') = false;
  81 
%   support_map('bl_time') = true;
  82 
%   support_map('bl_profile') = false;
  83 
%   % Call Program with external parameters that override defaults
  84 
%   ff_akz_vf(param_map, support_map);
  85 
% 
  86 
% @include
  87 
%
  88 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_set_default_param.html ffs_akz_set_default_param>
  89 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/paramfunc/html/ffs_akz_get_funcgrid.html ffs_akz_get_funcgrid>
  90 
% * <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solvepost/html/ff_akz_vf_post.html ff_akz_vf_post>
  91 
%
  92 
% @seealso
  93 
%
  94 
% * concurrent (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf.html ff_akz_vf>
  95 
% * concurrent (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vec.html ff_akz_vf_vec>
  96 
% * concurrent (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_akz_vf_vecsv.html ff_akz_vf_vecsv>
  97 
% * two-stage (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf.html ff_wkz_vf>
  98 
% * two-stage (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vec.html ff_wkz_vf_vec>
  99 
% * two-stage (safe + risky) optimized-vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_wkz_vf_vecsv.html ff_wkz_vf_vecsv>
 100 
% * two-stage + interpolate (safe + risky) loop: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf.html ff_iwkz_vf>
 101 
% * two-stage + interpolate (safe + risky) vectorized: <https://fanwangecon.github.io/CodeDynaAsset/m_akz/solve/html/ff_iwkz_vf_vec.html ff_iwkz_vf_vec>
 102 
% * 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>
 103 
%
 104 

 105 
%% Default
 106 
% * it_param_set = 1: quick test
 107 
% * it_param_set = 2: benchmark run
 108 
% * it_param_set = 3: benchmark profile
 109 
% * it_param_set = 4: press publish button
 110 

 111 
it_param_set = 2;
 112 
bl_input_override = true;
 113 
[param_map, support_map] = ffs_akz_set_default_param(it_param_set);
 114 

 115 
% Note: param_map and support_map can be adjusted here or outside to override defaults
 116 
% param_map('it_w_n') = 50;
 117 
% param_map('it_z_n') = 15;
 118 

 119 
% get armt and func map
 120 
[armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override); % 1 for override
 121 
default_params = {param_map support_map armt_map func_map};
 122 

 123 
%% Parse Parameters 1
 124 

 125 
% if varargin only has param_map and support_map,
 126 
params_len = length(varargin);
 127 
[default_params{1:params_len}] = varargin{:};
 128 
param_map = [param_map; default_params{1}];
 129 
support_map = [support_map; default_params{2}];
 130 
if params_len >= 1 && params_len <= 2
 131 
    % If override param_map, re-generate armt and func if they are not
 132 
    % provided
 133 
    bl_input_override = true;
 134 
    [armt_map, func_map] = ffs_akz_get_funcgrid(param_map, support_map, bl_input_override);
 135 
else
 136 
    % Override all
 137 
    armt_map = [armt_map; default_params{3}];
 138 
    func_map = [func_map; default_params{4}];
 139 
end
 140 

 141 
% append function name
 142 
st_func_name = 'ff_akz_vf';
 143 
support_map('st_profile_name_main') = [st_func_name support_map('st_profile_name_main')];
 144 
support_map('st_mat_name_main') = [st_func_name support_map('st_mat_name_main')];
 145 
support_map('st_img_name_main') = [st_func_name support_map('st_img_name_main')];
 146 

 147 
%% Parse Parameters 2
 148 

 149 
% armt_map
 150 
params_group = values(armt_map, {'mt_z_trans', 'ar_z'});
 151 
[ mt_z_trans, ar_z] = params_group{:};
 152 
params_group = values(armt_map, {'ar_a_meshk', 'ar_k_mesha', 'mt_coh_wkb', 'it_ameshk_n'});
 153 
[ar_a_meshk, ar_k_mesha, mt_coh_wkb, it_ameshk_n] = params_group{:};
 154 
% func_map
 155 
params_group = values(func_map, {'f_util_log', 'f_util_crra', 'f_cons', 'f_coh'});
 156 
[f_util_log, f_util_crra, f_cons, f_coh] = params_group{:};
 157 
% param_map
 158 
params_group = values(param_map, {'it_z_n', 'fl_crra', 'fl_beta', 'fl_c_min'});
 159 
[it_z_n, fl_crra, fl_beta, fl_c_min] = params_group{:};
 160 
params_group = values(param_map, {'it_maxiter_val', 'fl_tol_val', 'fl_tol_pol', 'it_tol_pol_nochange'});
 161 
[it_maxiter_val, fl_tol_val, fl_tol_pol, it_tol_pol_nochange] = params_group{:};
 162 
% support_map
 163 
params_group = values(support_map, {'bl_profile', 'st_profile_path', ...
 164 
    'st_profile_prefix', 'st_profile_name_main', 'st_profile_suffix',...
 165 
    'bl_time', 'bl_display', 'it_display_every', 'bl_post'});
 166 
[bl_profile, st_profile_path, ...
 167 
    st_profile_prefix, st_profile_name_main, st_profile_suffix, ...
 168 
    bl_time, bl_display, it_display_every, bl_post] = params_group{:};
 169 

 170 
%% Initialize Output Matrixes
 171 

 172 
mt_val_cur = zeros(length(ar_a_meshk),length(ar_z));
 173 
mt_val = mt_val_cur - 1;
 174 
mt_pol_a = zeros(length(ar_a_meshk),length(ar_z));
 175 
mt_pol_a_cur = mt_pol_a - 1;
 176 
mt_pol_k = zeros(length(ar_a_meshk),length(ar_z));
 177 
mt_pol_k_cur = mt_pol_k - 1;
 178 

 179 
%% Initialize Convergence Conditions
 180 

 181 
bl_vfi_continue = true;
 182 
it_iter = 0;
 183 
ar_val_diff_norm = zeros([it_maxiter_val, 1]);
 184 
ar_pol_diff_norm = zeros([it_maxiter_val, 1]);
 185 
mt_pol_perc_change = zeros([it_maxiter_val, it_z_n]);
 186 

 187 
%% Iterate Value Function
 188 
% Loop solution with 4 nested loops
 189 
%
 190 
% # loop 1: over exogenous states
 191 
% # loop 2: over endogenous states
 192 
% # loop 3: over choices
 193 
% # loop 4: add future utility, integration--loop over future shocks
 194 
%
 195 

 196 
% Start Profile
 197 
if (bl_profile)
 198 
    close all;
 199 
    profile off;
 200 
    profile on;
< 0.001 
      1 
 201
end 
 202 

 203 
% Start Timer
< 0.001 
      1 
 204
if (bl_time) 
< 0.001 
      1 
 205
    tic; 
< 0.001 
      1 
 206
end 
 207 

 208 
% Value Function Iteration
< 0.001 
      1 
 209
while bl_vfi_continue 
< 0.001 
    101 
 210
    it_iter = it_iter + 1; 
 211 
    
 212 
    %% Solve Optimization Problem Current Iteration
 213 
    
 214 
    % loop 1: over exogenous states
< 0.001 
    101 
 215
    for it_z_i = 1:length(ar_z) 
 216 
        
 217 
        % loop 2: over endogenous states
< 0.001 
   1515 
 218
        for it_coh_j = 1:length(ar_a_meshk) 
 219 
            % Get cash-on-hand which include k,b,z
  0.155 
1931625 
 220
            fl_coh = mt_coh_wkb(it_coh_j, it_z_i); 
 221 
            
 222 
            % loop 3: over choices
 19.691 
1931625 
 223
            ar_val_cur = zeros(size(ar_a_meshk)); 
  0.116 
1931625 
 224
            for it_cohp_k = 1:length(ar_a_meshk) 
 98.518 
2462821875 
 225
                fl_ap = ar_a_meshk(it_cohp_k); 
 91.268 
2462821875 
 226
                fl_kp = ar_k_mesha(it_cohp_k); 
 227 
                
 228 
                % consumption
 3975.264 
2462821875 
 229
                fl_c = f_cons(fl_coh, fl_ap, fl_kp); 
 230 
                
 231 
                % current utility
 90.007 
2462821875 
 232
                if (fl_crra == 1) 
 233 
                    ar_val_cur(it_cohp_k) = f_util_log(fl_c);
 234 
                    fl_u_neg_c = f_util_log(fl_c_min);
 82.271 
2462821875 
 235
                else 
 7814.828 
2462821875 
 236
                    ar_val_cur(it_cohp_k) = f_util_crra(fl_c); 
 4128.946 
2462821875 
 237
                    fl_u_neg_c = f_util_crra(fl_c_min); 
 88.669 
2462821875 
 238
                end 
 239 
                
 240 
                % loop 4: add future utility, integration--loop over future shocks
 96.695 
2462821875 
 241
                for it_zp_q = 1:length(ar_z) 
 1984.555 
36942328125 
 242
                    ar_val_cur(it_cohp_k) = ar_val_cur(it_cohp_k) + fl_beta*mt_z_trans(it_z_i,it_zp_q)*mt_val_cur(it_cohp_k,it_zp_q); 
 1179.525 
36942328125 
 243
                end 
 244 
                
 245 
                % Replace if negative consumption
 77.053 
2462821875 
 246
                if fl_c <= 0 
 5755.462 
1089037247 
 247
                    ar_val_cur(it_cohp_k) = fl_u_neg_c; 
 38.662 
1089037247 
 248
                end 
 249 
                
 86.512 
2462821875 
 250
            end 
 251 
            
 252 
            % maximization over loop 3 choices for loop 1+2 states
  9.034 
1931625 
 253
            it_max_lin_idx = find(ar_val_cur == max(ar_val_cur)); 
  0.183 
1931625 
 254
            mt_val(it_coh_j,it_z_i) = ar_val_cur(it_max_lin_idx(1)); 
  0.170 
1931625 
 255
            mt_pol_a(it_coh_j,it_z_i) = ar_a_meshk(it_max_lin_idx(1)); 
  0.161 
1931625 
 256
            mt_pol_k(it_coh_j,it_z_i) = ar_k_mesha(it_max_lin_idx(1)); 
 257 
            
  0.139 
1931625 
 258
        end 
  0.002 
   1515 
 259
    end 
 260 
    
 261 
    %% Check Tolerance and Continuation
 262 
    
 263 
    % Difference across iterations
  0.120 
    101 
 264
    ar_val_diff_norm(it_iter) = norm(mt_val - mt_val_cur); 
  0.142 
    101 
 265
    ar_pol_diff_norm(it_iter) = norm(mt_pol_a - mt_pol_a_cur) + norm(mt_pol_k - mt_pol_k_cur); 
  0.016 
    101 
 266
    ar_pol_a_perc_change = sum((mt_pol_a ~= mt_pol_a_cur))/(it_ameshk_n); 
  0.011 
    101 
 267
    ar_pol_k_perc_change = sum((mt_pol_k ~= mt_pol_k_cur))/(it_ameshk_n);     
  0.021 
    101 
 268
    mt_pol_perc_change(it_iter, :) = mean([ar_pol_a_perc_change;ar_pol_k_perc_change]); 
 269 
    
 270 
    % Update
  0.012 
    101 
 271
    mt_val_cur = mt_val; 
  0.009 
    101 
 272
    mt_pol_a_cur = mt_pol_a; 
  0.008 
    101 
 273
    mt_pol_k_cur = mt_pol_k; 
 274 
    
 275 
    % Print Iteration Results
< 0.001 
    101 
 276
    if (bl_display && (rem(it_iter, it_display_every)==0)) 
 277 
        fprintf('VAL it_iter:%d, fl_diff:%d, fl_diff_pol:%d\n', ...
 278 
            it_iter, ar_val_diff_norm(it_iter), ar_pol_diff_norm(it_iter));
 279 
        tb_valpol_iter = array2table([mean(mt_val_cur,1);...
 280 
                                      mean(mt_pol_a_cur,1); ...
 281 
                                      mean(mt_pol_k_cur,1); ...
 282 
                                      mt_val_cur(it_ameshk_n,:); ...
 283 
                                      mt_pol_a_cur(it_ameshk_n,:); ...
 284 
                                      mt_pol_k_cur(it_ameshk_n,:)]);
 285 
        tb_valpol_iter.Properties.VariableNames = strcat('z', string((1:size(mt_val_cur,2))));
 286 
        tb_valpol_iter.Properties.RowNames = {'mval', 'map', 'mak', 'Hval', 'Hap', 'Hak'};
 287 
        disp('mval = mean(mt_val_cur,1), average value over a')
 288 
        disp('map  = mean(mt_pol_a_cur,1), average choice over a')
 289 
        disp('mkp  = mean(mt_pol_k_cur,1), average choice over k')
 290 
        disp('Hval = mt_val_cur(it_ameshk_n,:), highest a state val')
 291 
        disp('Hap = mt_pol_a_cur(it_ameshk_n,:), highest a state choice')
 292 
        disp('mak = mt_pol_k_cur(it_ameshk_n,:), highest k state choice')                
 293 
        disp(tb_valpol_iter);
 294 
    end
 295 
    
 296 
    % Continuation Conditions:
 297 
    % 1. if value function convergence criteria reached
 298 
    % 2. if policy function variation over iterations is less than
 299 
    % threshold
< 0.001 
    101 
 300
    if (it_iter == (it_maxiter_val + 1)) 
< 0.001 
      1 
 301
        bl_vfi_continue = false; 
  0.002 
    100 
 302
    elseif ((it_iter == it_maxiter_val) || ... 
    100 
 303
            (ar_val_diff_norm(it_iter) < fl_tol_val) || ... 
    100 
 304
            (sum(ar_pol_diff_norm(max(1, it_iter-it_tol_pol_nochange):it_iter)) < fl_tol_pol)) 
 305 
        % Fix to max, run again to save results if needed
< 0.001 
      1 
 306
        it_iter_last = it_iter; 
< 0.001 
      1 
 307
        it_iter = it_maxiter_val;         
< 0.001 
      1 
 308
    end 
 309 
    
< 0.001 
    101 
 310
end 
 311 

 312 
% End Timer
< 0.001 
      1 
 313
if (bl_time) 
< 0.001 
      1 
 314
    toc; 
< 0.001 
      1 
 315
end 
 316 

 317 
% End Profile
< 0.001 
      1 
 318
if (bl_profile) 
  0.012 
      1 
 319
    profile off 
 320 
    profile viewer
 321 
    st_file_name = [st_profile_prefix st_profile_name_main st_profile_suffix];
 322 
    profsave(profile('info'), strcat(st_profile_path, st_file_name));
 323 
end
 324 

 325 
%% Process Optimal Choices
 326 

 327 
result_map = containers.Map('KeyType','char', 'ValueType','any');
 328 
result_map('mt_val') = mt_val;
 329 

 330 
mt_coh = f_coh(ar_z, ar_a_meshk, ar_k_mesha);
 331 
result_map('cl_mt_pol_coh') = {mt_coh, zeros(1)};
 332 
result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
 333 
result_map('cl_mt_pol_k') = {mt_pol_k, zeros(1)};
 334 
result_map('cl_mt_pol_c') = {f_cons(mt_coh, mt_pol_a, mt_pol_k), zeros(1)};
 335 
result_map('ar_st_pol_names') = ["cl_mt_pol_coh", "cl_mt_pol_a", "cl_mt_pol_k", "cl_mt_pol_c"];
 336 

 337 
if (bl_post)
 338 
    bl_input_override = true;
 339 
    result_map('ar_val_diff_norm') = ar_val_diff_norm(1:it_iter_last);
 340 
    result_map('ar_pol_diff_norm') = ar_pol_diff_norm(1:it_iter_last);
 341 
    result_map('mt_pol_perc_change') = mt_pol_perc_change(1:it_iter_last, :);
 342 
    result_map = ff_akz_vf_post(param_map, support_map, armt_map, func_map, result_map, bl_input_override);
 343 
end
 344 

 345 
end

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