Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Toolbox (bookdown), Matlab Code Examples Repository (bookdown), or Math for Econ with Matlab Repository (bookdown).
Examples](https://fanwangecon.github.io/M4Econ/), or** Dynamic Asset This is the example vignette for function: ff_optim_bisec_savezrone from the MEconTools Package. This functions solves for optimal savings/borrowing level given an anonymous function that provides the derivative of a intertemporal savings problem. The function is solves over a grid of state-space elements that are embeded in the anonymous function. By default, it iterates over 15 iterations with bisection.
The vectorized and looped bisection savings problem rely on this function to solve for optimal savings choices:
States Grid + Continuous Exact Savings as Share of Cash-on-Hand Loop:ff_vfi_az_bisec_loop, high precision even with small grid
States Grid + Continuous Exact Savings as Share of Cash-on-Hand Vectorized: ff_vfi_az_bisec_vec, precision and speed
Call the function with defaults, this solves concurrently for many state-space points’ optimization problems:
ff_optim_bisec_savezrone();
Elapsed time is 0.105423 seconds.
BISECT END: iteration=16, norm(ar_mid_fx)=0.00030653
vartype paramgroup2 paramgroup3 paramgroup4 paramgroup5 paramgroup6 paramgroup7 paramgroup8 paramgroup9
_______ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________
a "init" 1e-05 1e-05 1e-05 1e-05 1e-05 1e-05 1e-05 1e-05
b "init" 0.99999 0.99999 0.99999 0.99999 0.99999 0.99999 0.99999 0.99999
f_a "init" 33802 40925 67047 15411 63263 1.9839e+05 25282 70686
f_b "init" -46789 -1.2672e+05 -1.8532e+05 -67518 -48900 -1.2164e+05 -23149 -49303
it1_fp "fatx" -0.25973 -1.7159 -2.3655 -1.0421 0.28726 1.535 0.042644 0.42766
it1_p "x" 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
it2_fp "fatx" 0.72822 -0.052631 0.21087 -0.28379 -1.1125 -2.2202 -0.58887 -1.0296
it2_p "x" 0.25 0.25 0.25 0.25 0.74999 0.74999 0.74999 0.74999
it3_fp "fatx" 0.15277 1.8256 -1.1773 0.46124 -0.29179 -0.069428 -0.21281 -0.18376
it3_p "x" 0.375 0.12501 0.375 0.12501 0.625 0.625 0.625 0.625
it4_fp "fatx" -0.059183 0.62299 -0.55013 -0.0090579 0.0069602 0.74664 -0.079677 0.12972
it4_p "x" 0.4375 0.18751 0.3125 0.18751 0.5625 0.5625 0.5625 0.5625
it5_fp "fatx" 0.044028 0.2488 -0.19454 0.1861 -0.13821 0.34715 -0.017964 -0.023106
it5_p "x" 0.40625 0.21876 0.28125 0.15626 0.59375 0.59375 0.53125 0.59375
it6_fp "fatx" -0.0080863 0.090981 0.00054305 0.081339 -0.064832 0.14171 0.012387 0.054017
it6_p "x" 0.42188 0.23438 0.26563 0.17188 0.57812 0.60937 0.51562 0.57812
it7_fp "fatx" 0.017822 0.017593 -0.098707 0.034591 -0.028768 0.036948 -0.0027658 0.015665
it7_p "x" 0.41406 0.24219 0.27344 0.17969 0.57031 0.61719 0.52344 0.58594
it8_fp "fatx" 0.0048335 -0.017893 -0.049532 0.012405 -0.010865 -0.016025 0.0048149 -0.003664
it8_p "x" 0.41797 0.2461 0.26954 0.1836 0.5664 0.62109 0.51953 0.58984
it9_fp "fatx" -0.0016347 -0.00024633 -0.02461 0.0015865 -0.0019434 0.010514 0.0010259 0.0060142
it9_p "x" 0.41992 0.24415 0.26758 0.18555 0.56445 0.61914 0.52148 0.58789
it10_fp "fatx" 0.0015973 0.0086488 -0.012063 -0.0037571 0.0025106 -0.0027422 -0.00086962 0.0011786
it10_p "x" 0.41895 0.24317 0.26661 0.18653 0.56348 0.62011 0.52246 0.58887
it11_fp "fatx" -1.9235e-05 0.0041952 -0.0057672 -0.0010907 0.00028416 0.0038891 7.8199e-05 -0.0012418
it11_p "x" 0.41944 0.24366 0.26612 0.18604 0.56396 0.61963 0.52197 0.58935
it12_fp "fatx" 0.00078889 0.0019729 -0.0026139 0.00024655 -0.0008295 0.00057428 -0.00039569 -3.1408e-05
it12_p "x" 0.41919 0.2439 0.26587 0.1858 0.56421 0.61987 0.52222 0.58911
it13_fp "fatx" 0.00038479 0.00086292 -0.0010359 -0.00042242 -0.00027263 -0.0010838 -0.00015874 0.00057363
it13_p "x" 0.41931 0.24402 0.26575 0.18592 0.56409 0.61999 0.52209 0.58899
it14_fp "fatx" 0.00018277 0.0003082 -0.00024654 -8.8022e-05 5.7721e-06 -0.00025469 -4.0269e-05 0.00027113
it14_p "x" 0.41937 0.24408 0.26569 0.18586 0.56402 0.61993 0.52203 0.58905
it15_fp "fatx" 8.1766e-05 3.0909e-05 0.00014822 7.9241e-05 -0.00013343 0.00015981 1.8966e-05 0.00011986
it15_p "x" 0.4194 0.24412 0.26566 0.18583 0.56406 0.6199 0.522 0.58908
it15_level "level" 0.56205 -0.070025 0.044431 -0.039424 1.0402 0.48151 2.1656 0.9076
exactSoluSaveborrFrac "exact" 0.41943 0.24412 0.26567 0.18584 0.56403 0.61991 0.52201 0.5891
exactSoluSaveborrLevel "exact" 0.56211 -0.070022 0.044438 -0.039403 1.0402 0.48152 2.1656 0.90765
exactSoluSaveborrFracGap "exact" 2.4705e-05 3.402e-06 1.1458e-05 1.4456e-05 2.9252e-05 1.1766e-05 9.771e-06 2.4181e-05
exactSoluSaveborrLevelGap "exact" 5.28e-05 2.6845e-06 6.1825e-06 2.1411e-05 5.9818e-05 9.6728e-06 4.2208e-05 4.9045e-05
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_container_map ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
i idx ndim numel rowN colN sum mean std coefvari min max
_ ___ ____ _____ ____ ____ __________ __________ __________ ________ ___________ __________
ar_opti_foc_obj 1 1 2 8 1 8 0.00050535 6.3168e-05 9.4141e-05 1.4903 -0.00013343 0.00015981
ar_opti_save_frac 2 2 2 8 1 8 3.41 0.42626 0.17279 0.40536 0.18583 0.6199
xxx TABLE:ar_opti_foc_obj xxxxxxxxxxxxxxxxxx
c1 c2 c3 c4 c5 c6 c7 c8
__________ __________ __________ __________ ___________ __________ __________ __________
r1 8.1766e-05 3.0909e-05 0.00014822 7.9241e-05 -0.00013343 0.00015981 1.8966e-05 0.00011986
xxx TABLE:ar_opti_save_frac xxxxxxxxxxxxxxxxxx
c1 c2 c3 c4 c5 c6 c7 c8
______ _______ _______ _______ _______ ______ _____ _______
r1 0.4194 0.24412 0.26566 0.18583 0.56406 0.6199 0.522 0.58908
Bisection for savings choice at one state:
% Generate the state-space and function
[fl_z1, fl_z2, fl_r, fl_beta] = deal(0.4730, 0.6252, 0.0839, 0.7365);
% ffi_intertemporal_max is a function in ff_optim_bisec_savezrone for testing
fc_deri_wth_uniroot = @(x) ffi_intertemporal_max(x, fl_z1, fl_z2, fl_r, fl_beta);
% Call Function
bl_verbose = true;
ff_optim_bisec_savezrone(fc_deri_wth_uniroot, bl_verbose);
BISECT END: iteration=16, norm(ar_mid_fx)=0.00016724
vartype paramgroup2
_______ ___________
a "init" 1e-05
b "init" 0.99999
f_a "init" 70155
f_b "init" -95255
it1_fp "fatx" -0.502
it1_p "x" 0.5
it2_fp "fatx" 1.5361
it2_p "x" 0.25
it3_fp "fatx" 0.34671
it3_p "x" 0.375
it4_fp "fatx" -0.089881
it4_p "x" 0.4375
it5_fp "fatx" 0.12259
it5_p "x" 0.40625
it6_fp "fatx" 0.015276
it6_p "x" 0.42188
it7_fp "fatx" -0.037529
it7_p "x" 0.42969
it8_fp "fatx" -0.011188
it8_p "x" 0.42578
it9_fp "fatx" 0.0020277
it9_p "x" 0.42383
it10_fp "fatx" -0.0045843
it10_p "x" 0.42481
it11_fp "fatx" -0.0012793
it11_p "x" 0.42432
it12_fp "fatx" 0.00037392
it12_p "x" 0.42407
it13_fp "fatx" -0.00045276
it13_p "x" 0.4242
it14_fp "fatx" -3.9436e-05
it14_p "x" 0.42413
it15_fp "fatx" 0.00016724
it15_p "x" 0.4241
it15_level "level" -0.13158
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_container_map Scalars
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
i idx value
_ ___ __________
ar_opti_foc_obj 1 1 0.00016724
ar_opti_save_frac 2 2 0.4241
Solve the two period intertemporal optimization problem with only 6 individual states:
% Generate the state-space and function
ar_z1 = [1,2,3]';
ar_z2 = [3,2,1]';
ar_r = [1.05, 1.50, 1.30]';
ar_beta = [0.80, 0.95, 1.50]';
mt_fc_inputs = [ar_z1, ar_z2, ar_r, ar_beta];
% ffi_intertemporal_max is a function in ff_optim_bisec_savezrone for testing
fc_deri_wth_uniroot = @(x) ffi_intertemporal_max(x, ar_z1, ar_z2, ar_r, ar_beta);
% Call Function
bl_verbose = true;
ff_optim_bisec_savezrone(fc_deri_wth_uniroot, bl_verbose);
BISECT END: iteration=16, norm(ar_mid_fx)=8.9847e-05
vartype paramgroup2 paramgroup3 paramgroup4
_______ ___________ ___________ ___________
a "init" 1e-05 1e-05 1e-05
b "init" 0.99999 0.99999 0.99999
f_a "init" 32475 33928 43671
f_b "init" -40594 -35714 -29113
it1_fp "fatx" -0.16238 -0.035714 0.29114
it1_p "x" 0.5 0.5 0.5
it2_fp "fatx" 0.75773 0.88092 -0.58225
it2_p "x" 0.25 0.25 0.74999
it3_fp "fatx" 0.21649 0.33333 -0.077629
it3_p "x" 0.375 0.375 0.625
it4_fp "fatx" 0.020615 0.14059 0.11091
it4_p "x" 0.4375 0.4375 0.5625
it5_fp "fatx" -0.07132 0.051539 0.018865
it5_p "x" 0.46875 0.46875 0.59375
it6_fp "fatx" -0.025599 0.0078193 -0.028659
it6_p "x" 0.45313 0.48438 0.60937
it7_fp "fatx" -0.0025711 -0.013955 -0.0047386
it7_p "x" 0.44531 0.49219 0.60156
it8_fp "fatx" 0.0090001 -0.0030715 0.0071001
it8_p "x" 0.44141 0.48828 0.59765
it9_fp "fatx" 0.0032093 0.0023727 0.0011903
it9_p "x" 0.44336 0.48633 0.59961
it10_fp "fatx" 0.00031783 -0.00034971 -0.0017717
it10_p "x" 0.44434 0.4873 0.60058
it11_fp "fatx" -0.0011269 0.0010114 -0.00029011
it11_p "x" 0.44483 0.48682 0.6001
it12_fp "fatx" -0.00040464 0.00033083 0.00045024
it12_p "x" 0.44458 0.48706 0.59985
it13_fp "fatx" -4.3425e-05 -9.4396e-06 8.0103e-05
it13_p "x" 0.44446 0.48718 0.59997
it14_fp "fatx" 0.0001372 0.0001607 -0.000105
it14_p "x" 0.4444 0.48712 0.60003
it15_fp "fatx" 4.6884e-05 7.5628e-05 -1.2444e-05
it15_p "x" 0.44443 0.48715 0.6
it15_level "level" -0.3686 0.56403 1.6261
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_container_map ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
i idx ndim numel rowN colN sum mean std coefvari min max
_ ___ ____ _____ ____ ____ __________ __________ __________ ________ ___________ __________
ar_opti_foc_obj 1 1 2 3 1 3 0.00011007 3.6689e-05 4.4913e-05 1.2241 -1.2444e-05 7.5628e-05
ar_opti_save_frac 2 2 2 3 1 3 1.5316 0.51053 0.080379 0.15744 0.44443 0.6
xxx TABLE:ar_opti_foc_obj xxxxxxxxxxxxxxxxxx
c1 c2 c3
__________ __________ ___________
r1 4.6884e-05 7.5628e-05 -1.2444e-05
xxx TABLE:ar_opti_save_frac xxxxxxxxxxxxxxxxxx
c1 c2 c3
_______ _______ ___
r1 0.44443 0.48715 0.6
Test Speed doing 6.25 million bisections for a savings problem:
% Generate the state-space and function
rng(123);
it_draws = 6250000; % must be even number
ar_z1 = exp(rand([it_draws,1])*3-1.5);
ar_z2 = exp(rand([it_draws,1])*3-1.5);
ar_r = (rand(it_draws,1)*10.0);
ar_beta = [rand(round(it_draws/2),1)*0.9+0.1; rand(round(it_draws/2),1)*0.9+1];
% ffi_intertemporal_max is a function in ff_optim_bisec_savezrone for testing
fc_deri_wth_uniroot = @(x) ffi_intertemporal_max(x, ar_z1, ar_z2, ar_r, ar_beta);
% Call Function
bl_verbose = false;
bl_timer = true;
[ar_opti_save_frac, ar_opti_save_level] = ff_optim_bisec_savezrone(fc_deri_wth_uniroot, bl_verbose, bl_timer);
Elapsed time is 2.570982 seconds.
mp_container_map = containers.Map('KeyType','char', 'ValueType','any');
mp_container_map('ar_opti_save_frac') = ar_opti_save_frac;
mp_container_map('ar_opti_save_level') = ar_opti_save_level;
mp_container_map('ar_opti_save_frac_notnan') = ar_opti_save_frac(~isnan(ar_opti_save_frac));
ff_container_map_display(mp_container_map);
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_container_map ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
i idx ndim numel rowN colN sum mean std coefvari min max
_ ___ ____ ________ ________ ____ __________ _______ _______ ________ _______ _______
ar_opti_save_frac 1 1 2 6.25e+06 6.25e+06 1 2.884e+06 0.46144 0.15306 0.33171 0.09092 0.65518
ar_opti_save_frac_notnan 2 2 2 6.25e+06 6.25e+06 1 2.884e+06 0.46144 0.15306 0.33171 0.09092 0.65518
ar_opti_save_level 3 3 2 6.25e+06 6.25e+06 1 2.9482e+06 0.47172 0.66667 1.4133 -3.9805 2.9221
figure();
histogram(ar_opti_save_frac(~isnan(ar_opti_save_frac)),100);
title('Distribution of Optimal Savings Fractions');
xlabel('Savings Fractions');
grid on;
function [ar_deri_zero, ar_saveborr_level] = ffi_intertemporal_max(ar_saveborr_frac, z1, z2, r, beta)
ar_saveborr_level = ar_saveborr_frac.*(z1+z2./(1+r)) - z2./(1+r);
ar_deri_zero = 1./(ar_saveborr_level-z1) + (beta.*(r+1))./(z2 + ar_saveborr_level.*(r+1));
end