Chapter 1 Savings Dynamic Programming

1.1 FF_VFI_AZ_LOOP Savings Loop Grid Examples

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_vfi_az_loop from the MEconTools Package. This function solves the dynamic programming problem for a (a,z) model. Households can save a, and face AR(1) shock z. The problem is solved over the infinite horizon.

This is the looped code, it is slow for larger state-space problems. The code uses common grid, with the same state space and choice space grids.

Links to Other Code:

Core Savings/Borrowing Dynamic Programming Solution Functions that are functions in the MEconTools Package. :

  • Common Choice and States Grid Loop: ff_vfi_az_loop

  • Common Choice and States Grid Vectorized: ff_vfi_az_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC, Loop:ff_vfi_az_bisec_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC Vectorized: ff_vfi_az_bisec_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Loop:ff_vfi_az_mzoom_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Vectorized: ff_vfi_az_mzoom_vec

The sample codes are written for the standard dynamic savings problem. The code can be adapted for multiple assets, savings and borrowing, discrete and continuous choice, etc. A large proportion of dynamic economic models are based on the underlying structure of solving a model with endogenous states and exogenous shocks, and that is what the (a,z) model does. In general, one can write looped code first to make sure the economics is correct, then vectorized code can be adopted to increase speed.

1.1.1 Test FF_VFI_AZ_LOOP Defaults

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('fl_crra') = 1.5;
mp_params('fl_beta') = 0.94;
% call function
ff_vfi_az_loop(mp_params);

Elapsed time is 2.378952 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min    max
          _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    ___

    ap    1     1      2       700     100      7      9855.1    14.079    14.408     1.0234      0     50 

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3         c4         c5         c6         c7  
            ______    ______    ______    ________    _______    _______    ______

    r1           0         0         0    0.045213    0.25576    0.61095    1.0362
    r2           0         0         0    0.045213    0.25576    0.61095    1.0362
    r3           0         0         0    0.045213    0.25576    0.61095    1.0362
    r4           0         0         0     0.06647    0.25576    0.61095    1.0362
    r5           0         0         0     0.06647    0.25576    0.61095     1.164
    r96     43.924    43.924    43.924      43.924     43.924     45.102    45.102
    r97     45.102    45.102    45.102      45.102     45.102     46.298    46.298
    r98     46.298    46.298    46.298      46.298     46.298     47.513    47.513
    r99     47.513    47.513    47.513      47.513     47.513     48.747    48.747
    r100    48.747    48.747    48.747      48.747     48.747         50        50

1.1.2 Test FF_VFI_AZ_BISEC_VEC Speed Tests

Call the function with different a and z grid size, print out speed:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('ls_ffcmd') = {};
% A grid 50, shock grid 5:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 0.715890 seconds.

% A grid 750, shock grid 15:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 15;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 300.576571 seconds.

% A grid 600, shock grid 45:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 600;
mp_params('it_z_n') = 45;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 910.111661 seconds.

1.1.3 Test FF_VFI_AZ_LOOP Control Outputs

Run the function first without any outputs, but only the timer.

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {};
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 0.400105 seconds.

Run the function and show policy function for savings choice. For ls_ffcmd, ls_ffsna, ls_ffgrh, can include these: ‘v,’ ‘ap,’ ‘c,’ ‘y,’ ‘coh,’ ‘savefraccoh.’ These are value, aprime savings choice, consumption, income, cash on hand, and savings fraction as cash-on-hand.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 0.410866 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       a        mean_z_0_4858    mean_z_0_67798    mean_z_0_9462    mean_z_1_3205    mean_z_1_8429
    _____    ________    _____________    ______________    _____________    _____________    _____________

      1             0              0                0         0.071865          0.20862          0.36462   
      2      0.002975              0                0         0.071698          0.20827          0.36418   
      3      0.016829              0                0         0.070928          0.20666          0.36216   
      4      0.046375              0        0.0029827         0.069341          0.20331          0.35793   
      5      0.095198      0.0038183         0.044243          0.11681          0.27649          0.35114   
      6        0.1663       0.054362         0.084837          0.17517          0.26637          0.34171   
      7       0.26234       0.099899          0.13609          0.16422          0.25383          0.41847   
      8       0.38568        0.15381          0.19428          0.22348          0.32132          0.40047   
      9       0.53852        0.21153          0.25554          0.28573          0.39055          0.47258   
     10       0.72291        0.26934          0.31659          0.34814          0.36175          0.44538   
     11       0.94076         0.3247          0.37504          0.40848          0.42229          0.50941   
     12        1.1939        0.37617          0.42941          0.46521           0.4802          0.57087   
     13         1.484        0.53695          0.47898          0.51743           0.5344           0.5291   
     14        1.8128        0.57847          0.52356          0.56473          0.58429          0.58056   
     15        2.1817        0.61468          0.56329           0.6071          0.62958          0.62823   
     16        2.5924         0.6462           0.5985          0.64475          0.67028          0.67186   
     17        3.0463        0.67365          0.62963          0.67804          0.60721          0.71141   
     18        3.5449        0.69762          0.65713          0.70737           0.6404          0.65255   
     19        4.0894        0.71859          0.68142          0.73318          0.67021          0.68509   
     20        4.6813        0.73701          0.70293          0.75587           0.6969          0.71446   
     21        5.3218        0.75325            0.722          0.77584          0.72078          0.74089   
     22        6.0121        0.76763          0.73895          0.79344          0.74211          0.76461   
     23        6.7536         0.7804          0.75407          0.80897          0.76119          0.78587   
     24        7.5474         0.7918          0.76759           0.8227          0.77824          0.80491   
     25        8.3948        0.80201          0.77972          0.83486          0.79351          0.82194   
     26        9.2967        0.81119          0.79063          0.84567          0.80719          0.83719   
     27        10.254        0.81947          0.80049           0.8553          0.81948          0.85083   
     28        11.269        0.82697          0.80941          0.86389          0.83053          0.86306   
     29        12.342        0.83379          0.81752          0.87159          0.84048          0.87401   
     30        13.473        0.84001           0.8249          0.87849          0.84946          0.88384   
     31        14.665        0.84569          0.83165           0.8847          0.85759          0.82241   
     32        15.918         0.8509          0.83782           0.8903          0.86495          0.83188   
     33        17.233         0.8557           0.8435          0.89536          0.87163          0.84053   
     34        18.611        0.86012          0.84872          0.89995           0.8777          0.84844   
     35        20.053        0.86421          0.85354          0.90411          0.88324          0.85568   
     36         21.56        0.86799            0.858           0.9079           0.8883          0.86231   
     37        23.133        0.87151          0.86214          0.91136          0.89292          0.86841   
     38        24.773        0.87479          0.86598          0.91452          0.89716          0.87401   
     39        26.481        0.87784          0.86955          0.91741          0.90105          0.87917   
     40        28.258         0.8807          0.87289          0.92007          0.90463          0.88393   
     41        30.104        0.88337          0.87601          0.92251          0.90793          0.88833   
     42        32.021        0.88588          0.87893          0.92475          0.91097           0.8924   
     43         34.01        0.88824          0.88166          0.92683          0.91378          0.89617   
     44         36.07        0.89046          0.88423          0.92874          0.91638          0.89966   
     45        38.204        0.89256          0.88665          0.93052          0.91879          0.90291   
     46        40.412        0.89453           0.9403          0.93216          0.92102          0.90592   
     47        42.695         0.8964          0.94141          0.93368           0.9231          0.90873   
     48        45.053        0.89817          0.94245           0.9351          0.92504          0.91135   
     49        47.488        0.89985          0.94341          0.93642          0.92684           0.9138   
     50            50        0.90144           0.9443          0.93765          0.92853          0.91608   

Run the function and show summaries for savings and fraction of coh saved:

mp_params('it_a_n') = 100;
mp_params('it_z_n') = 9;
mp_support('ls_ffcmd') = {'ap', 'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_support('bl_vfi_store_all') = true; % store c(a,z), y(a,z)
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 3.281815 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    ap             1     1      2       900     100      9       12904     14.338     14.524      1.013      0          50
    savefraccoh    2     2      2       900     100      9      619.51    0.68834    0.26953    0.39157      0     0.93151

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3          c4           c5         c6         c7         c8        c9  
            ______    ______    ______    __________    ________    _______    _______    ______    ______

    r1           0         0         0             0    0.092813    0.25576    0.61095    1.0362    1.6023
    r2           0         0         0             0    0.092813    0.25576    0.61095    1.0362    1.6023
    r3           0         0         0             0    0.092813    0.25576    0.61095    1.0362    1.6023
    r4           0         0         0    0.00051272    0.092813    0.25576    0.61095    1.0362    1.6023
    r5           0         0         0     0.0029004    0.092813    0.25576    0.61095    1.0362    1.6023
    r96     43.924    43.924    43.924        43.924      43.924     45.102     45.102    45.102    46.298
    r97     45.102    45.102    45.102        45.102      45.102     46.298     46.298    46.298    47.513
    r98     46.298    46.298    46.298        46.298      46.298     47.513     47.513    47.513    48.747
    r99     47.513    47.513    47.513        47.513      47.513     48.747     48.747    48.747        50
    r100    48.747    48.747    48.747        48.747      48.747         50         50        50        50

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3           c4           c5         c6         c7         c8         c9   
            _______    _______    _______    __________    ________    _______    _______    _______    _______

    r1            0          0          0             0    0.070073    0.15255    0.28789    0.38573    0.47121
    r2            0          0          0             0    0.070045     0.1525    0.28781    0.38565    0.47114
    r3            0          0          0             0    0.069914    0.15228    0.28748     0.3853     0.4708
    r4            0          0          0    0.00048613    0.069636     0.1518    0.28676    0.38454    0.47007
    r5            0          0          0     0.0027273    0.069182    0.15101    0.28559    0.38329    0.46886
    r96     0.92625    0.92358    0.92022         0.916     0.91072    0.92836    0.91992    0.90945    0.92033
    r97     0.92676    0.92416    0.92088       0.91677     0.91162    0.92918    0.92095    0.91073    0.92169
    r98     0.92727    0.92473    0.92153       0.91752     0.91249    0.92998    0.92194    0.91196      0.923
    r99     0.92776    0.92528    0.92216       0.91824     0.91333    0.93076    0.92291    0.91315    0.92426
    r100    0.92823    0.92581    0.92277       0.91895     0.91416    0.93151    0.92384    0.91431    0.90252

1.1.4 Test FF_VFI_AZ_LOOP Change Interest Rate and Discount

Show only save fraction of cash on hand:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with several different interest rates and discount factor:

% Lower Savings Incentives
mp_params('fl_beta') = 0.80;
mp_params('fl_r') = 0.01;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 0.825240 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      357.49    0.5107    0.2755    0.53945      0     0.80531

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5           c6           c7   
            _______    _______    _______    _______    _______    __________    ________

    r1            0          0          0          0          0     0.0002246    0.041573
    r2            0          0          0          0          0    0.00022455    0.041566
    r3            0          0          0          0          0     0.0012689    0.041533
    r4            0          0          0          0          0      0.001266    0.041462
    r5            0          0          0          0          0     0.0034759    0.041345
    r96     0.78455    0.78145    0.79995    0.79456     0.7876       0.77865     0.76719
    r97     0.78669    0.78366    0.77972    0.79679    0.78998       0.78122     0.77001
    r98     0.78878    0.78582    0.78197    0.79897    0.79231       0.78374     0.77276
    r99     0.79084    0.78794    0.78417    0.77927    0.79459        0.7862     0.77545
    r100    0.79285    0.79001    0.78633    0.78154    0.79682        0.7886     0.77808

% Higher Savings Incentives
mp_params('fl_beta') = 0.95;
mp_params('fl_r') = 0.04;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 2.386791 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      479.94    0.68563    0.27152    0.39602      0     0.93121

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2           c3           c4         c5         c6         c7   
            _______    _______    __________    ________    _______    _______    _______

    r1            0          0             0     0.07007    0.17967    0.30874    0.43404
    r2            0          0             0    0.070042    0.17961    0.30866    0.43396
    r3            0          0             0    0.069911    0.17935    0.30833     0.4336
    r4            0          0             0    0.069633    0.17881    0.30762    0.43284
    r5            0          0    0.00049972    0.069179    0.17792    0.30645    0.43158
    r96     0.92489    0.92134       0.91672     0.91072    0.92717    0.91691    0.92776
    r97     0.92544    0.92198       0.91747     0.91162    0.92802    0.91801    0.92895
    r98     0.92598     0.9226        0.9182     0.91249    0.92885    0.91908     0.9301
    r99      0.9265     0.9232       0.91891     0.91333    0.92965    0.92011    0.93121
    r100      0.927    0.92379        0.9196     0.91416    0.93042     0.9211    0.90914

1.1.5 Test FF_VFI_AZ_LOOP Changing Risk Aversion

Here, again, show fraction of coh saved in summary tabular form, but also show it graphically.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {'savefraccoh'};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with different risk aversion levels, higher preferences for risk:

% Lower Risk Aversion
mp_params('fl_crra') = 0.5;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 1.327261 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    ______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      450.35    0.64336    0.2803    0.43568      0     0.90711

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4           c5         c6         c7   
            _______    _______    _______    _________    ________    _______    _______

    r1            0          0          0    0.0060341    0.093241    0.19572    0.30604
    r2            0          0          0    0.0060316    0.093213    0.19567    0.30599
    r3            0          0          0    0.0060204     0.09308    0.19546    0.30574
    r4            0          0          0    0.0059964    0.092798    0.19501     0.3052
    r5            0          0          0     0.012229    0.092335    0.19427    0.30431
    r96     0.90049    0.89703    0.89253      0.88669     0.90296    0.89297    0.90379
    r97     0.90128    0.89791    0.89351      0.88781     0.90404    0.89429    0.88181
    r98     0.90205    0.89876    0.89447      0.88891      0.9051    0.89557    0.88337
    r99      0.9028    0.89959    0.89541      0.88998     0.90612    0.89681    0.88489
    r100    0.90354     0.9004    0.89632      0.89101     0.90711    0.89802    0.88636

When risk aversion increases, at every state-space point, the household wants to save more.

% Higher Risk Aversion
mp_params('fl_crra') = 5;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 2.680109 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      500.59    0.71513    0.25488    0.35641      0     0.94324

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2          c3         c4         c5         c6         c7   
            _______    _______    ________    _______    _______    _______    _______

    r1            0          0    0.044811    0.15534    0.25694    0.40177    0.48276
    r2            0          0    0.044787    0.15528    0.25686    0.40168    0.48268
    r3            0          0    0.044678    0.15499     0.2565    0.40124    0.48228
    r4            0          0    0.044445    0.15437    0.25572    0.40032    0.48143
    r5            0          0    0.064784    0.15337    0.25445    0.39879    0.48003
    r96     0.92489    0.92134     0.94129    0.93513    0.92717    0.91691    0.92776
    r97     0.92544    0.92198      0.9418     0.9358    0.92802    0.91801    0.92895
    r98     0.92598     0.9226      0.9423    0.93644    0.92885    0.91908     0.9301
    r99      0.9265     0.9232     0.94278    0.93706    0.92965    0.92011    0.93121
    r100      0.927    0.92379     0.94324    0.93765    0.93042     0.9211    0.90914

1.1.6 Test FF_VFI_AZ_LOOP with Higher Uncertainty

Increase the standard deviation of the Shock.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 150;
mp_params('it_z_n') = 15;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';
% graph color spectrum
mp_params('cl_colors') = 'copper';

Lower standard deviation of shock:

% Lower Risk Aversion
mp_params('fl_shk_std') = 0.10;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 13.492999 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1506.3    0.66947    0.28673     0.4283      0     0.93222

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.14061     0.1891    0.24154     0.2699    0.32439
    r2            0          0          0          0          0     0.1406    0.18908    0.24152    0.26988    0.32437
    r3            0          0          0          0          0    0.14053      0.189    0.24142    0.26977    0.32426
    r4            0          0          0          0          0    0.14038    0.18881     0.2412    0.26956    0.32402
    r5            0          0          0          0          0    0.14013    0.18851    0.24085     0.2692    0.32362
    r146    0.93087    0.92957    0.92815    0.92661    0.92492    0.92712    0.92403    0.92069    0.91706    0.91312
    r147    0.93121    0.92994    0.92854    0.92702    0.92537    0.92768    0.92465    0.92135    0.91778    0.91391
    r148    0.93156     0.9303    0.92893    0.92743    0.92581    0.92823    0.92525    0.92201    0.91849    0.91467
    r149    0.93189    0.93065     0.9293    0.92783    0.92623    0.92878    0.92584    0.92264    0.91918    0.91542
    r150    0.93222      0.931    0.92967    0.92823    0.92665     0.9293    0.92641    0.92327    0.91986    0.91616

Higher shock standard deviation: low shock high asset save more, high shock more asset save less, high shock low asset save more:

% Higher Risk Aversion
mp_params('fl_shk_std') = 0.40;
ff_vfi_az_loop(mp_params, mp_support);

Elapsed time is 18.680264 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1678.8    0.74614    0.22779    0.30529      0     0.93141

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.53612    0.59853    0.67884    0.73891    0.77675
    r2            0          0          0          0          0    0.53609     0.5985    0.67882    0.73889    0.77674
    r3            0          0          0          0          0    0.53594    0.59839    0.67873    0.73883    0.77669
    r4            0          0          0          0          0    0.53563    0.59814    0.67853    0.73868    0.77658
    r5            0          0          0          0          0    0.53511    0.59774    0.67821    0.73843     0.7764
    r146    0.92696     0.9262    0.92513    0.92359    0.92142    0.91653     0.9078    0.88992    0.86057    0.80415
    r147    0.92721    0.92647    0.92541     0.9239    0.92176    0.91741    0.90895    0.89144    0.84828    0.79341
    r148    0.92746    0.92673    0.92569    0.92421     0.9221    0.91827    0.91007    0.87813    0.83621    0.78284
    r149     0.9277    0.92698    0.92596     0.9245    0.92243     0.9191    0.89605    0.86507    0.82436    0.77245
    r150    0.92794    0.92724    0.92623     0.9248    0.92276    0.90467    0.88233    0.85227    0.81273    0.76223

1.2 FF_VFI_AZ_VEC Savings Vectorized Grid Examples

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_vfi_az_vec from the MEconTools Package. This function solves the dynamic programming problem for a (a,z) model. Households can save a, and face AR(1) shock z. The problem is solved over the infinite horizon.

This is the vectorized code, its speed is much faster than the looped code. The function is designed to have small memory footprint and requires low computing resources, yet is fast.

The code uses common grid, with the same state space and choice space grids. ff_vfi_az_bisec_vec from the MEconTools Package solves the same problem but using continuous exact percentage asset choices, which is more precise than the solution here, and perhaps a little bit slower and relies on First Order Conditions. The ff_vfi_az_mzoom_vec also solves the same class of problems with continuous exact percentage asset choices, and does not rely on First Order Conditions, but is slower than ff_vfi_az_bisec_vec.

Links to Other Code:

Core Savings/Borrowing Dynamic Programming Solution Functions that are functions in the MEconTools Package. :

  • Common Choice and States Grid Loop: ff_vfi_az_loop

  • Common Choice and States Grid Vectorized: ff_vfi_az_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC, Loop:ff_vfi_az_bisec_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC Vectorized: ff_vfi_az_bisec_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Loop:ff_vfi_az_mzoom_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Vectorized: ff_vfi_az_mzoom_vec

1.2.1 Test FF_VFI_AZ_VEC Defaults

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('fl_crra') = 1.5;
mp_params('fl_beta') = 0.94;
ff_vfi_az_vec(mp_params);

Elapsed time is 0.136223 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min    max
          _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    ___

    ap    1     1      2       700     100      7      9855.1    14.079    14.408     1.0234      0     50 

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3         c4         c5         c6         c7  
            ______    ______    ______    ________    _______    _______    ______

    r1           0         0         0    0.045213    0.25576    0.61095    1.0362
    r2           0         0         0    0.045213    0.25576    0.61095    1.0362
    r3           0         0         0    0.045213    0.25576    0.61095    1.0362
    r4           0         0         0     0.06647    0.25576    0.61095    1.0362
    r5           0         0         0     0.06647    0.25576    0.61095     1.164
    r96     43.924    43.924    43.924      43.924     43.924     45.102    45.102
    r97     45.102    45.102    45.102      45.102     45.102     46.298    46.298
    r98     46.298    46.298    46.298      46.298     46.298     47.513    47.513
    r99     47.513    47.513    47.513      47.513     47.513     48.747    48.747
    r100    48.747    48.747    48.747      48.747     48.747         50        50

1.2.2 Test FF_VFI_AZ_BISEC_VEC Speed Tests

Call the function with different a and z grid size, print out speed:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('ls_ffcmd') = {};
% A grid 50, shock grid 5:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.025309 seconds.

% A grid 750, shock grid 15:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 15;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 4.855482 seconds.

% A grid 600, shock grid 45:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 600;
mp_params('it_z_n') = 45;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 12.201130 seconds.

1.2.3 Test FF_VFI_AZ_VEC Control Outputs

Run the function first without any outputs, but only the timer.

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {};
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.022504 seconds.

Run the function and show policy function for savings choice. For ls_ffcmd, ls_ffsna, ls_ffgrh, can include these: ‘v,’ ‘ap,’ ‘c,’ ‘y,’ ‘coh,’ ‘savefraccoh.’ These are value, aprime savings choice, consumption, income, cash on hand, and savings fraction as cash-on-hand.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.041571 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       a        mean_z_0_4858    mean_z_0_67798    mean_z_0_9462    mean_z_1_3205    mean_z_1_8429
    _____    ________    _____________    ______________    _____________    _____________    _____________

      1             0              0                0         0.071865          0.20862          0.36462   
      2      0.002975              0                0         0.071698          0.20827          0.36418   
      3      0.016829              0                0         0.070928          0.20666          0.36216   
      4      0.046375              0        0.0029827         0.069341          0.20331          0.35793   
      5      0.095198      0.0038183         0.044243          0.11681          0.27649          0.35114   
      6        0.1663       0.054362         0.084837          0.17517          0.26637          0.34171   
      7       0.26234       0.099899          0.13609          0.16422          0.25383          0.41847   
      8       0.38568        0.15381          0.19428          0.22348          0.32132          0.40047   
      9       0.53852        0.21153          0.25554          0.28573          0.39055          0.47258   
     10       0.72291        0.26934          0.31659          0.34814          0.36175          0.44538   
     11       0.94076         0.3247          0.37504          0.40848          0.42229          0.50941   
     12        1.1939        0.37617          0.42941          0.46521           0.4802          0.57087   
     13         1.484        0.53695          0.47898          0.51743           0.5344           0.5291   
     14        1.8128        0.57847          0.52356          0.56473          0.58429          0.58056   
     15        2.1817        0.61468          0.56329           0.6071          0.62958          0.62823   
     16        2.5924         0.6462           0.5985          0.64475          0.67028          0.67186   
     17        3.0463        0.67365          0.62963          0.67804          0.60721          0.71141   
     18        3.5449        0.69762          0.65713          0.70737           0.6404          0.65255   
     19        4.0894        0.71859          0.68142          0.73318          0.67021          0.68509   
     20        4.6813        0.73701          0.70293          0.75587           0.6969          0.71446   
     21        5.3218        0.75325            0.722          0.77584          0.72078          0.74089   
     22        6.0121        0.76763          0.73895          0.79344          0.74211          0.76461   
     23        6.7536         0.7804          0.75407          0.80897          0.76119          0.78587   
     24        7.5474         0.7918          0.76759           0.8227          0.77824          0.80491   
     25        8.3948        0.80201          0.77972          0.83486          0.79351          0.82194   
     26        9.2967        0.81119          0.79063          0.84567          0.80719          0.83719   
     27        10.254        0.81947          0.80049           0.8553          0.81948          0.85083   
     28        11.269        0.82697          0.80941          0.86389          0.83053          0.86306   
     29        12.342        0.83379          0.81752          0.87159          0.84048          0.87401   
     30        13.473        0.84001           0.8249          0.87849          0.84946          0.88384   
     31        14.665        0.84569          0.83165           0.8847          0.85759          0.82241   
     32        15.918         0.8509          0.83782           0.8903          0.86495          0.83188   
     33        17.233         0.8557           0.8435          0.89536          0.87163          0.84053   
     34        18.611        0.86012          0.84872          0.89995           0.8777          0.84844   
     35        20.053        0.86421          0.85354          0.90411          0.88324          0.85568   
     36         21.56        0.86799            0.858           0.9079           0.8883          0.86231   
     37        23.133        0.87151          0.86214          0.91136          0.89292          0.86841   
     38        24.773        0.87479          0.86598          0.91452          0.89716          0.87401   
     39        26.481        0.87784          0.86955          0.91741          0.90105          0.87917   
     40        28.258         0.8807          0.87289          0.92007          0.90463          0.88393   
     41        30.104        0.88337          0.87601          0.92251          0.90793          0.88833   
     42        32.021        0.88588          0.87893          0.92475          0.91097           0.8924   
     43         34.01        0.88824          0.88166          0.92683          0.91378          0.89617   
     44         36.07        0.89046          0.88423          0.92874          0.91638          0.89966   
     45        38.204        0.89256          0.88665          0.93052          0.91879          0.90291   
     46        40.412        0.89453           0.9403          0.93216          0.92102          0.90592   
     47        42.695         0.8964          0.94141          0.93368           0.9231          0.90873   
     48        45.053        0.89817          0.94245           0.9351          0.92504          0.91135   
     49        47.488        0.89985          0.94341          0.93642          0.92684           0.9138   
     50            50        0.90144           0.9443          0.93765          0.92853          0.91608   

Run the function and show summaries for savings and fraction of coh saved:

mp_params('it_a_n') = 100;
mp_params('it_z_n') = 9;
mp_support('ls_ffcmd') = {'ap', 'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_support('bl_vfi_store_all') = true; % store c(a,z), y(a,z)
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.230510 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    ap             1     1      2       900     100      9       12904     14.338     14.524      1.013      0          50
    savefraccoh    2     2      2       900     100      9      619.51    0.68834    0.26953    0.39157      0     0.93151

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3          c4           c5         c6         c7         c8        c9  
            ______    ______    ______    __________    ________    _______    _______    ______    ______

    r1           0         0         0             0    0.092813    0.25576    0.61095    1.0362    1.6023
    r2           0         0         0             0    0.092813    0.25576    0.61095    1.0362    1.6023
    r3           0         0         0             0    0.092813    0.25576    0.61095    1.0362    1.6023
    r4           0         0         0    0.00051272    0.092813    0.25576    0.61095    1.0362    1.6023
    r5           0         0         0     0.0029004    0.092813    0.25576    0.61095    1.0362    1.6023
    r96     43.924    43.924    43.924        43.924      43.924     45.102     45.102    45.102    46.298
    r97     45.102    45.102    45.102        45.102      45.102     46.298     46.298    46.298    47.513
    r98     46.298    46.298    46.298        46.298      46.298     47.513     47.513    47.513    48.747
    r99     47.513    47.513    47.513        47.513      47.513     48.747     48.747    48.747        50
    r100    48.747    48.747    48.747        48.747      48.747         50         50        50        50

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3           c4           c5         c6         c7         c8         c9   
            _______    _______    _______    __________    ________    _______    _______    _______    _______

    r1            0          0          0             0    0.070073    0.15255    0.28789    0.38573    0.47121
    r2            0          0          0             0    0.070045     0.1525    0.28781    0.38565    0.47114
    r3            0          0          0             0    0.069914    0.15228    0.28748     0.3853     0.4708
    r4            0          0          0    0.00048613    0.069636     0.1518    0.28676    0.38454    0.47007
    r5            0          0          0     0.0027273    0.069182    0.15101    0.28559    0.38329    0.46886
    r96     0.92625    0.92358    0.92022         0.916     0.91072    0.92836    0.91992    0.90945    0.92033
    r97     0.92676    0.92416    0.92088       0.91677     0.91162    0.92918    0.92095    0.91073    0.92169
    r98     0.92727    0.92473    0.92153       0.91752     0.91249    0.92998    0.92194    0.91196      0.923
    r99     0.92776    0.92528    0.92216       0.91824     0.91333    0.93076    0.92291    0.91315    0.92426
    r100    0.92823    0.92581    0.92277       0.91895     0.91416    0.93151    0.92384    0.91431    0.90252

1.2.4 Test FF_VFI_AZ_VEC Change Interest Rate and Discount

Show only save fraction of cash on hand:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with several different interest rates and discount factor:

% Lower Savings Incentives
mp_params('fl_beta') = 0.80;
mp_params('fl_r') = 0.01;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.058079 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      357.49    0.5107    0.2755    0.53945      0     0.80531

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5           c6           c7   
            _______    _______    _______    _______    _______    __________    ________

    r1            0          0          0          0          0     0.0002246    0.041573
    r2            0          0          0          0          0    0.00022455    0.041566
    r3            0          0          0          0          0     0.0012689    0.041533
    r4            0          0          0          0          0      0.001266    0.041462
    r5            0          0          0          0          0     0.0034759    0.041345
    r96     0.78455    0.78145    0.79995    0.79456     0.7876       0.77865     0.76719
    r97     0.78669    0.78366    0.77972    0.79679    0.78998       0.78122     0.77001
    r98     0.78878    0.78582    0.78197    0.79897    0.79231       0.78374     0.77276
    r99     0.79084    0.78794    0.78417    0.77927    0.79459        0.7862     0.77545
    r100    0.79285    0.79001    0.78633    0.78154    0.79682        0.7886     0.77808

% Higher Savings Incentives
mp_params('fl_beta') = 0.95;
mp_params('fl_r') = 0.04;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.177867 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      479.94    0.68563    0.27152    0.39602      0     0.93121

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2           c3           c4         c5         c6         c7   
            _______    _______    __________    ________    _______    _______    _______

    r1            0          0             0     0.07007    0.17967    0.30874    0.43404
    r2            0          0             0    0.070042    0.17961    0.30866    0.43396
    r3            0          0             0    0.069911    0.17935    0.30833     0.4336
    r4            0          0             0    0.069633    0.17881    0.30762    0.43284
    r5            0          0    0.00049972    0.069179    0.17792    0.30645    0.43158
    r96     0.92489    0.92134       0.91672     0.91072    0.92717    0.91691    0.92776
    r97     0.92544    0.92198       0.91747     0.91162    0.92802    0.91801    0.92895
    r98     0.92598     0.9226        0.9182     0.91249    0.92885    0.91908     0.9301
    r99      0.9265     0.9232       0.91891     0.91333    0.92965    0.92011    0.93121
    r100      0.927    0.92379        0.9196     0.91416    0.93042     0.9211    0.90914

1.2.5 Test FF_VFI_AZ_VEC Changing Risk Aversion

Here, again, show fraction of coh saved in summary tabular form, but also show it graphically.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {'savefraccoh'};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with different risk aversion levels, higher preferences for risk:

% Lower Risk Aversion
mp_params('fl_crra') = 0.5;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.181638 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    ______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      450.35    0.64336    0.2803    0.43568      0     0.90711

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4           c5         c6         c7   
            _______    _______    _______    _________    ________    _______    _______

    r1            0          0          0    0.0060341    0.093241    0.19572    0.30604
    r2            0          0          0    0.0060316    0.093213    0.19567    0.30599
    r3            0          0          0    0.0060204     0.09308    0.19546    0.30574
    r4            0          0          0    0.0059964    0.092798    0.19501     0.3052
    r5            0          0          0     0.012229    0.092335    0.19427    0.30431
    r96     0.90049    0.89703    0.89253      0.88669     0.90296    0.89297    0.90379
    r97     0.90128    0.89791    0.89351      0.88781     0.90404    0.89429    0.88181
    r98     0.90205    0.89876    0.89447      0.88891      0.9051    0.89557    0.88337
    r99      0.9028    0.89959    0.89541      0.88998     0.90612    0.89681    0.88489
    r100    0.90354     0.9004    0.89632      0.89101     0.90711    0.89802    0.88636

When risk aversion increases, at every state-space point, the household wants to save more.

% Higher Risk Aversion
mp_params('fl_crra') = 5;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.152901 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      500.59    0.71513    0.25488    0.35641      0     0.94324

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2          c3         c4         c5         c6         c7   
            _______    _______    ________    _______    _______    _______    _______

    r1            0          0    0.044811    0.15534    0.25694    0.40177    0.48276
    r2            0          0    0.044787    0.15528    0.25686    0.40168    0.48268
    r3            0          0    0.044678    0.15499     0.2565    0.40124    0.48228
    r4            0          0    0.044445    0.15437    0.25572    0.40032    0.48143
    r5            0          0    0.064784    0.15337    0.25445    0.39879    0.48003
    r96     0.92489    0.92134     0.94129    0.93513    0.92717    0.91691    0.92776
    r97     0.92544    0.92198      0.9418     0.9358    0.92802    0.91801    0.92895
    r98     0.92598     0.9226      0.9423    0.93644    0.92885    0.91908     0.9301
    r99      0.9265     0.9232     0.94278    0.93706    0.92965    0.92011    0.93121
    r100      0.927    0.92379     0.94324    0.93765    0.93042     0.9211    0.90914

1.2.6 Test FF_VFI_AZ_VEC with Higher Uncertainty

Increase the standard deviation of the Shock.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 150;
mp_params('it_z_n') = 15;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';
% graph color spectrum
mp_params('cl_colors') = 'copper';

Lower standard deviation of shock:

% Lower Risk Aversion
mp_params('fl_shk_std') = 0.10;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.544499 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1506.3    0.66947    0.28673     0.4283      0     0.93222

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.14061     0.1891    0.24154     0.2699    0.32439
    r2            0          0          0          0          0     0.1406    0.18908    0.24152    0.26988    0.32437
    r3            0          0          0          0          0    0.14053      0.189    0.24142    0.26977    0.32426
    r4            0          0          0          0          0    0.14038    0.18881     0.2412    0.26956    0.32402
    r5            0          0          0          0          0    0.14013    0.18851    0.24085     0.2692    0.32362
    r146    0.93087    0.92957    0.92815    0.92661    0.92492    0.92712    0.92403    0.92069    0.91706    0.91312
    r147    0.93121    0.92994    0.92854    0.92702    0.92537    0.92768    0.92465    0.92135    0.91778    0.91391
    r148    0.93156     0.9303    0.92893    0.92743    0.92581    0.92823    0.92525    0.92201    0.91849    0.91467
    r149    0.93189    0.93065     0.9293    0.92783    0.92623    0.92878    0.92584    0.92264    0.91918    0.91542
    r150    0.93222      0.931    0.92967    0.92823    0.92665     0.9293    0.92641    0.92327    0.91986    0.91616

Higher shock standard deviation: low shock high asset save more, high shock more asset save less, high shock low asset save more:

% Higher Risk Aversion
mp_params('fl_shk_std') = 0.40;
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.515060 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1678.8    0.74614    0.22779    0.30529      0     0.93141

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.53612    0.59853    0.67884    0.73891    0.77675
    r2            0          0          0          0          0    0.53609     0.5985    0.67882    0.73889    0.77674
    r3            0          0          0          0          0    0.53594    0.59839    0.67873    0.73883    0.77669
    r4            0          0          0          0          0    0.53563    0.59814    0.67853    0.73868    0.77658
    r5            0          0          0          0          0    0.53511    0.59774    0.67821    0.73843     0.7764
    r146    0.92696     0.9262    0.92513    0.92359    0.92142    0.91653     0.9078    0.88992    0.86057    0.80415
    r147    0.92721    0.92647    0.92541     0.9239    0.92176    0.91741    0.90895    0.89144    0.84828    0.79341
    r148    0.92746    0.92673    0.92569    0.92421     0.9221    0.91827    0.91007    0.87813    0.83621    0.78284
    r149     0.9277    0.92698    0.92596     0.9245    0.92243     0.9191    0.89605    0.86507    0.82436    0.77245
    r150    0.92794    0.92724    0.92623     0.9248    0.92276    0.90467    0.88233    0.85227    0.81273    0.76223

1.3 FF_VFI_AZ_BISEC_LOOP Savings Loop Exact (FOC) Examples

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_vfi_az_bisec_loopfrom the MEconTools Package. This function solves the dynamic programming problem for a (a,z) model. Households can save a, and face AR(1) shock z. The problem is solved over the infinite horizon.

This is the looped code, it is slow for larger state-space problems.

The code uses continuous choices, solved with bisection. The state-space is on a grid, but choice grids are in terms of percentage of resources to save and solved exactly.

Links to Other Code:

Core Savings/Borrowing Dynamic Programming Solution Functions that are functions in the MEconTools Package. :

  • Common Choice and States Grid Loop: ff_vfi_az_loop

  • Common Choice and States Grid Vectorized: ff_vfi_az_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC, Loop:ff_vfi_az_bisec_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC Vectorized: ff_vfi_az_bisec_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Loop:ff_vfi_az_mzoom_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Vectorized: ff_vfi_az_mzoom_vec

1.3.1 Test FF_VFI_AZ_BISEC_LOOP Defaults

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('fl_crra') = 1.5;
mp_params('fl_beta') = 0.94;
% call function
ff_vfi_az_bisec_loop(mp_params);

Elapsed time is 33.158577 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min     max  
          _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    ______

    ap    1     1      2       700     100      7      9863.4    14.091    14.388     1.0211      0     50.117

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3         c4         c5         c6         c7  
            ______    ______    ______    ________    _______    _______    ______

    r1           0         0         0    0.053491    0.25574    0.60604    1.1157
    r2           0         0         0    0.053998    0.25571     0.6066    1.1163
    r3           0         0         0    0.056449    0.25576    0.60907    1.1187
    r4           0         0         0    0.061799    0.26016     0.6109    1.1239
    r5           0         0         0    0.066463    0.26897    0.61141    1.1327
    r96     43.388     43.52    43.701      43.925     44.222      44.68    45.228
    r97     44.566    44.695    44.878      45.103     45.398     45.856    46.403
    r98     45.761    45.892    46.072      46.298     46.592      47.05    47.597
    r99     46.973    47.107    47.286      47.514     47.806     48.263    48.815
    r100    48.206    48.338    48.519      48.746     49.037     49.497    50.117

1.3.2 Test FF_VFI_AZ_BISEC_LOOP Speed Tests

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('ls_ffcmd') = {};
% A grid 50, shock grid 5:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 14.819629 seconds.

% A grid 750, shock grid 15:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 15;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 783.169420 seconds.

%A grid 600, shock grid 45:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 600;
mp_params('it_z_n') = 45;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 1955.142516 seconds.

1.3.3 Test FF_VFI_AZ_BISEC_LOOP Control Outputs

Run the function first without any outputs;

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {};
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.122166 seconds.

Run the function and show policy function for savings choice. For ls_ffcmd, ls_ffsna, ls_ffgrh, can include these: ‘v,’ ‘ap,’ ‘c,’ ‘y,’ ‘coh,’ ‘savefraccoh.’ These are value, aprime savings choice, consumption, income, cash on hand, and savings fraction as cash-on-hand.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 20.812511 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       a        mean_z_0_4858    mean_z_0_67798    mean_z_0_9462    mean_z_1_3205    mean_z_1_8429
    _____    ________    _____________    ______________    _____________    _____________    _____________

      1             0              0                0         0.067239          0.20859          0.35953   
      2      0.002975              0                0         0.069375          0.20829          0.36032   
      3      0.016829              0                0         0.070901           0.2139          0.36215   
      4      0.046375              0        0.0061439         0.087319           0.2266          0.36264   
      5      0.095198      0.0087684         0.034403           0.1168           0.2468          0.37473   
      6        0.1663       0.054361         0.077248           0.1522          0.26639          0.39151   
      7       0.26234       0.099892          0.13132          0.19388          0.29929          0.41281   
      8       0.38568        0.15958          0.19309          0.24112          0.33017          0.43088   
      9       0.53852        0.23417          0.25553          0.29215          0.37436          0.45969   
     10       0.72291         0.3071          0.31656          0.34812          0.41153          0.48386   
     11       0.94076        0.37595          0.37503          0.40842          0.44925          0.50992   
     12        1.1939        0.43881          0.42941          0.45755          0.48697          0.54367   
     13         1.484        0.49509          0.48129          0.50381          0.53262          0.56979   
     14        1.8128        0.54489          0.53018          0.54642          0.56778          0.59634   
     15        2.1817        0.58871          0.57382          0.58548          0.60055           0.6282   
     16        2.5924        0.62716          0.61258          0.62076          0.63101          0.65249   
     17        3.0463        0.66079          0.64682          0.65243          0.65884           0.6752   
     18        3.5449        0.69027          0.67709          0.68069          0.68423          0.69638   
     19        4.0894        0.71621          0.70376          0.70596          0.70724          0.71591   
     20        4.6813        0.73703          0.72732          0.72848          0.72799          0.73385   
     21        5.3218        0.75326          0.74813           0.7485          0.74673          0.75021   
     22        6.0121        0.76913          0.76657          0.76632          0.76364          0.76535   
     23        6.7536        0.78536          0.78286          0.78231          0.77889           0.7842   
     24        7.5474        0.79983          0.79745          0.79653          0.79269          0.79678   
     25        8.3948        0.81271          0.81039          0.80929          0.80514          0.80831   
     26        9.2967        0.82418          0.82198          0.82076          0.81637          0.81875   
     27        10.254         0.8345          0.83242          0.83114          0.82656          0.82833   
     28        11.269        0.84377          0.84176          0.84042          0.83584          0.83706   
     29        12.342        0.85214          0.85024          0.84884           0.8442          0.84499   
     30        13.473        0.85964          0.85781          0.85647          0.85183          0.85232   
     31        14.665        0.86648          0.86471          0.86337          0.85879          0.85897   
     32        15.918        0.87264          0.87099          0.86965          0.86507          0.86507   
     33        17.233        0.87826          0.87667          0.87533          0.87161          0.87063   
     34        18.611        0.88338          0.88186          0.88052          0.87771          0.87582   
     35        20.053        0.88802          0.88656          0.88528          0.88326          0.88052   
     36         21.56         0.8923          0.89089          0.88967          0.88833          0.88485   
     37        23.133        0.89614          0.89486          0.89364           0.8926          0.88888   
     38        24.773        0.89974          0.89852           0.8973          0.89626           0.8926   
     39        26.481        0.90304          0.90182          0.90072          0.89968          0.89608   
     40        28.258        0.90603          0.90493          0.90383          0.90279          0.89925   
     41        30.104        0.90884          0.90774           0.9067          0.90572          0.90218   
     42        32.021         0.9114          0.91036          0.90932          0.90841          0.90493   
     43         34.01        0.91378           0.9128          0.91183          0.91091          0.90749   
     44         36.07        0.91598          0.91506          0.91408          0.91317          0.90987   
     45        38.204        0.91805          0.91714          0.91622          0.91537          0.91207   
     46        40.412        0.91994          0.91909          0.91817          0.91732          0.91415   
     47        42.695        0.92171          0.92086          0.92001          0.91921           0.9161   
     48        45.053        0.92336          0.92257          0.92171          0.92092          0.91799   
     49        47.488        0.92489          0.92409          0.92336          0.92257          0.92025   
     50            50        0.92629          0.92562          0.92489          0.92428          0.92403   

Run the function and show summaries for savings and fraction of coh saved:

mp_params('it_a_n') = 100;
mp_params('it_z_n') = 9;
mp_support('ls_ffcmd') = {'ap', 'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_support('bl_vfi_store_all') = true; % store c(a,z), y(a,z)
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 57.010652 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    ap             1     1      2       900     100      9       12926     14.362     14.544     1.0127      0      51.171
    savefraccoh    2     2      2       900     100      9      621.24    0.69027    0.26896    0.38965      0     0.92739

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3          c4           c5         c6         c7         c8        c9  
            ______    ______    ______    __________    ________    _______    _______    ______    ______

    r1           0         0         0             0    0.087442    0.27778    0.58243    1.0038    1.5724
    r2           0         0         0             0    0.087962    0.27828    0.58297    1.0044    1.5731
    r3           0         0         0             0    0.090477    0.28074    0.58547    1.0069    1.5755
    r4           0         0         0    0.00055771     0.09279    0.28605     0.5907    1.0122    1.5808
    r5           0         0         0     0.0059496     0.09602    0.29477    0.59952    1.0209    1.5895
    r96     43.845    43.923    44.022        44.198      44.428     44.722     45.103    45.546    46.186
    r97     45.031    45.101    45.208        45.384      45.613      45.91     46.293    46.735    47.382
    r98     46.237    46.297    46.411         46.59      46.818     47.115     47.501    47.948    48.605
    r99      47.46    47.512    47.635        47.812      48.041      48.34     48.726    49.191    49.869
    r100    48.703    48.746    48.878        49.055      49.283     49.586     49.978    50.495    51.171

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3           c4           c5         c6         c7         c8         c9   
            _______    _______    _______    __________    ________    _______    _______    _______    _______

    r1            0          0          0             0    0.066018    0.16569    0.27445    0.37369    0.46243
    r2            0          0          0             0    0.066384    0.16593    0.27463    0.37381    0.46256
    r3            0          0          0             0    0.068154    0.16715    0.27549    0.37442    0.46292
    r4            0          0          0    0.00052879    0.069619    0.16978    0.27726    0.37564    0.46378
    r5            0          0          0     0.0055946    0.071572    0.17405    0.28025    0.37766    0.46512
    r96     0.92458    0.92354    0.92226       0.92171     0.92116    0.92055    0.91994    0.91842    0.91811
    r97     0.92531    0.92416    0.92306       0.92251     0.92196    0.92141    0.92086    0.91933    0.91915
    r98     0.92605     0.9247    0.92379        0.9233     0.92275     0.9222    0.92171    0.92031    0.92031
    r99     0.92672    0.92525    0.92452       0.92403     0.92348      0.923    0.92251    0.92147    0.92184
    r100    0.92739     0.9258    0.92525       0.92477     0.92422    0.92379    0.92342    0.92336    0.92367

1.3.4 Test FF_VFI_AZ_BISEC_LOOP Change Interest Rate and Discount

Show only save fraction of cash on hand:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with several different interest rates and discount factor:

% Lower Savings Incentives
mp_params('fl_beta') = 0.80;
mp_params('fl_r') = 0.01;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 10.824225 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      357.85    0.51122    0.27528    0.53848      0     0.79965

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5           c6           c7   
            _______    _______    _______    _______    _______    __________    ________

    r1            0          0          0          0          0    0.00022362    0.041544
    r2            0          0          0          0          0    0.00022362    0.041544
    r3            0          0          0          0          0     0.0011391    0.041544
    r4            0          0          0          0          0     0.0016884    0.041483
    r5            0          0          0          0          0     0.0034584     0.04136
    r96     0.79586    0.79275    0.78945    0.78591    0.78225       0.77853     0.77059
    r97     0.79684    0.79379    0.79055    0.78713    0.78359       0.77993     0.77212
    r98     0.79782    0.79482    0.79171    0.78835    0.78488       0.78127     0.77365
    r99     0.79873    0.79586    0.79275    0.78951     0.7861       0.78262     0.77548
    r100    0.79965    0.79684    0.79385    0.79061    0.78732        0.7839      0.7781

% Higher Savings Incentives
mp_params('fl_beta') = 0.95;
mp_params('fl_r') = 0.04;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 53.369195 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      481.37    0.68768    0.27118    0.39435      0     0.92702

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4         c5         c6         c7   
            _______    _______    _______    ________    _______    _______    _______

    r1            0          0          0    0.065774    0.18076    0.30655    0.41654
    r2            0          0          0    0.066201    0.18101    0.30674     0.4166
    r3            0          0          0     0.06791    0.18223    0.30747    0.41709
    r4            0          0          0    0.069619    0.18467    0.30759    0.41812
    r5            0          0          0    0.071694    0.18876    0.30838    0.41983
    r96     0.92428    0.92245    0.92178     0.92116    0.92049    0.91872    0.91824
    r97     0.92501    0.92324    0.92257     0.92196    0.92129    0.91958    0.91921
    r98     0.92574    0.92397    0.92336     0.92275    0.92208    0.92049    0.92025
    r99     0.92647     0.9247    0.92409     0.92348    0.92287    0.92147    0.92159
    r100    0.92702    0.92544    0.92483     0.92422    0.92373    0.92336    0.92348

1.3.5 Test FF_VFI_AZ_BISEC_LOOP Changing Risk Aversion

Here, again, show fraction of coh saved in summary tabular form, but also show it graphically.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {'savefraccoh'};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with different risk aversion levels, higher preferences for risk:

% Lower Risk Aversion
mp_params('fl_crra') = 0.5;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 47.635241 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      452.13    0.6459    0.28031    0.43398      0     0.90359

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4           c5         c6         c7   
            _______    _______    _______    _________    ________    _______    _______

    r1            0          0          0    0.0047401    0.089089    0.19822    0.30783
    r2            0          0          0    0.0051674    0.089394     0.1984    0.30796
    r3            0          0          0    0.0060218    0.090676    0.19926    0.30851
    r4            0          0          0    0.0082801    0.092812    0.20115    0.30973
    r5            0          0          0     0.012247    0.092995     0.2042    0.31174
    r96     0.90047    0.89925    0.89828       0.8973     0.89632    0.89376    0.89297
    r97     0.90127    0.90017    0.89919      0.89828      0.8973     0.8948    0.89394
    r98     0.90206    0.90102    0.90011      0.89919     0.89828    0.89577    0.89498
    r99     0.90279    0.90188    0.90102      0.90011     0.89919    0.89681     0.8959
    r100    0.90359    0.90273    0.90188      0.90096     0.90011    0.89803    0.89687

When risk aversion increases, at every state-space point, the household wants to save more.

% Higher Risk Aversion
mp_params('fl_crra') = 5;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 46.937845 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      502.71    0.71816    0.25437     0.3542      0     0.93587

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2          c3         c4         c5         c6         c7   
            _______    _______    ________    _______    _______    _______    _______

    r1            0          0    0.047037    0.15537    0.27573     0.3909    0.48782
    r2            0          0    0.047525    0.15531    0.27591    0.39102    0.48795
    r3            0          0    0.049844     0.1569    0.27695     0.3917    0.48837
    r4            0          0    0.054788    0.16025    0.27915     0.3931    0.48929
    r5            0          0    0.062905    0.16569    0.28275    0.39542    0.49075
    r96     0.93307    0.93258     0.93203    0.93154     0.9302    0.92995    0.92971
    r97     0.93374    0.93325     0.93276    0.93227    0.93111    0.93105    0.93117
    r98     0.93441    0.93398     0.93349    0.93307    0.93209    0.93227     0.9327
    r99     0.93508    0.93465     0.93423    0.93392    0.93331    0.93368    0.93435
    r100    0.93575    0.93539     0.93508     0.9349    0.93496    0.93526    0.93587

1.3.6 Test FF_VFI_AZ_BISEC_LOOP with Higher Uncertainty

Increase the standard deviation of the Shock.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 150;
mp_params('it_z_n') = 15;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';
% graph color spectrum
mp_params('cl_colors') = 'copper';

Lower standard deviation of shock:

% Lower Risk Aversion
mp_params('fl_shk_std') = 0.10;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 150.979328 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1507.5    0.67001    0.28668    0.42788      0     0.92568

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.13847    0.18485    0.23026    0.27378    0.31729
    r2            0          0          0          0          0    0.13853    0.18491    0.23032    0.27384    0.31736
    r3            0          0          0          0          0    0.13895    0.18528    0.23063    0.27408     0.3176
    r4            0          0          0          0          0    0.13987    0.18607     0.2313    0.27469    0.31809
    r5            0          0          0          0          0    0.14011    0.18735     0.2324    0.27567    0.31888
    r146    0.92373    0.92354     0.9233    0.92312    0.92287    0.92086    0.92068    0.92049    0.91952    0.91933
    r147    0.92422    0.92403    0.92385    0.92361    0.92342    0.92141    0.92123    0.92098    0.92007    0.91988
    r148     0.9247    0.92452    0.92434    0.92409    0.92391     0.9219    0.92171    0.92153    0.92062    0.92043
    r149    0.92519    0.92501    0.92483    0.92458     0.9244    0.92245    0.92226    0.92208    0.92116     0.9211
    r150    0.92568     0.9255    0.92531    0.92507    0.92489    0.92293    0.92275    0.92257    0.92245    0.92232

Higher shock standard deviation: low shock high asset save more, high shock more asset save less, high shock low asset save more:

% Higher Risk Aversion
mp_params('fl_shk_std') = 0.40;
ff_vfi_az_bisec_loop(mp_params, mp_support);

Elapsed time is 136.803951 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1685.6    0.74914    0.22909     0.3058      0     0.93679

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0     0.5264    0.61264    0.68271    0.73922    0.78433
    r2            0          0          0          0          0    0.52646    0.61264    0.68271    0.73922    0.78433
    r3            0          0          0          0          0    0.52658     0.6127    0.68271    0.73922    0.78433
    r4            0          0          0          0          0    0.52682    0.61288    0.68283    0.73928    0.78439
    r5            0          0          0          0          0    0.52731    0.61313    0.68295    0.73934    0.78439
    r146    0.92983    0.92928    0.92873    0.92806    0.92739    0.92269    0.92354     0.9258    0.92904    0.93331
    r147     0.9302    0.92971     0.9291    0.92849    0.92788    0.92361    0.92477     0.9269    0.93001    0.93423
    r148    0.93056    0.93008    0.92953    0.92892    0.92831    0.92458    0.92593      0.928    0.93105    0.93508
    r149    0.93093    0.93044    0.92995    0.92934    0.92873     0.9258    0.92702     0.9291    0.93203      0.936
    r150     0.9313    0.93087    0.93032    0.92977    0.92916    0.92696    0.92818    0.93014    0.93294    0.93679

1.4 FF_VFI_AZ_BISEC_VEC Savings Vectorized Exact (FOC) Examples

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_vfi_az_bisec_vec from the MEconTools Package. This function solves the dynamic programming problem for a (a,z) model. Households can save a, and face AR(1) shock z. The problem is solved over the infinite horizon.

This is the vectorized code, its speed is much faster than the looped code. The function is designed to have small memory footprint and requires low computing resources, yet is fast.

The code uses continuous choices, solved with bi(multi)section. The state-space is on a grid, but choice grids are in terms of percentage of resources available, which is individual specific, to save and solved exactly up to ((1/(2)^16)*100=0.001525878) percentage of cash on hand. The ff_vfi_az_vec from the MEconTools Package solves the same problem using vectorized common grid code where the choice set and state space share the same grid. The common grid function is faster, but less precise for the same number of asset grid points.

Links to Other Code:

Core Savings/Borrowing Dynamic Programming Solution Functions that are functions in the MEconTools Package. :

  • Common Choice and States Grid Loop: ff_vfi_az_loop

  • Common Choice and States Grid Vectorized: ff_vfi_az_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC, Loop:ff_vfi_az_bisec_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC Vectorized: ff_vfi_az_bisec_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Loop:ff_vfi_az_mzoom_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Vectorized: ff_vfi_az_mzoom_vec

1.4.1 Test FF_VFI_AZ_BISEC_VEC Defaults

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('fl_crra') = 1.5;
mp_params('fl_beta') = 0.94;
% call function
ff_vfi_az_bisec_vec(mp_params);

Elapsed time is 1.762201 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min     max  
          _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    ______

    ap    1     1      2       700     100      7      9863.4    14.091    14.388     1.0211      0     50.117

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3         c4         c5         c6         c7  
            ______    ______    ______    ________    _______    _______    ______

    r1           0         0         0    0.053491    0.25574    0.60604    1.1157
    r2           0         0         0    0.053998    0.25571     0.6066    1.1163
    r3           0         0         0    0.056449    0.25576    0.60907    1.1187
    r4           0         0         0    0.061799    0.26016     0.6109    1.1239
    r5           0         0         0    0.066463    0.26897    0.61141    1.1327
    r96     43.388     43.52    43.701      43.925     44.222      44.68    45.228
    r97     44.566    44.695    44.878      45.103     45.398     45.856    46.403
    r98     45.761    45.892    46.072      46.298     46.592      47.05    47.597
    r99     46.973    47.107    47.286      47.514     47.806     48.263    48.815
    r100    48.206    48.338    48.519      48.746     49.037     49.497    50.117

1.4.2 Test FF_VFI_AZ_BISEC_VEC Speed Tests

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('ls_ffcmd') = {};
% A grid 50, shock grid 5:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 0.792541 seconds.

% A grid 750, shock grid 15:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 15;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 43.095190 seconds.

% A grid 600, shock grid 45:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 600;
mp_params('it_z_n') = 45;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 80.139775 seconds.

1.4.3 Test FF_VFI_AZ_BISEC_VEC Control Outputs

Run the function first without any outputs;

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {};
ff_vfi_az_vec(mp_params, mp_support);

Elapsed time is 0.029901 seconds.

Run the function and show policy function for savings choice. For ls_ffcmd, ls_ffsna, ls_ffgrh, can include these: ‘v,’ ‘ap,’ ‘c,’ ‘y,’ ‘coh,’ ‘savefraccoh.’ These are value, aprime savings choice, consumption, income, cash on hand, and savings fraction as cash-on-hand.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 0.494900 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       a        mean_z_0_4858    mean_z_0_67798    mean_z_0_9462    mean_z_1_3205    mean_z_1_8429
    _____    ________    _____________    ______________    _____________    _____________    _____________

      1             0              0                0         0.067239          0.20859          0.35953   
      2      0.002975              0                0         0.069375          0.20829          0.36032   
      3      0.016829              0                0         0.070901           0.2139          0.36215   
      4      0.046375              0        0.0061439         0.087319           0.2266          0.36264   
      5      0.095198      0.0087684         0.034403           0.1168           0.2468          0.37473   
      6        0.1663       0.054361         0.077248           0.1522          0.26639          0.39151   
      7       0.26234       0.099892          0.13132          0.19388          0.29929          0.41281   
      8       0.38568        0.15958          0.19309          0.24112          0.33017          0.43088   
      9       0.53852        0.23417          0.25553          0.29215          0.37436          0.45969   
     10       0.72291         0.3071          0.31656          0.34812          0.41153          0.48386   
     11       0.94076        0.37595          0.37503          0.40842          0.44925          0.50992   
     12        1.1939        0.43881          0.42941          0.45755          0.48697          0.54367   
     13         1.484        0.49509          0.48129          0.50381          0.53262          0.56979   
     14        1.8128        0.54489          0.53018          0.54642          0.56778          0.59634   
     15        2.1817        0.58871          0.57382          0.58548          0.60055           0.6282   
     16        2.5924        0.62716          0.61258          0.62076          0.63101          0.65249   
     17        3.0463        0.66079          0.64682          0.65243          0.65884           0.6752   
     18        3.5449        0.69027          0.67709          0.68069          0.68423          0.69638   
     19        4.0894        0.71621          0.70376          0.70596          0.70724          0.71591   
     20        4.6813        0.73703          0.72732          0.72848          0.72799          0.73385   
     21        5.3218        0.75326          0.74813           0.7485          0.74673          0.75021   
     22        6.0121        0.76913          0.76657          0.76632          0.76364          0.76535   
     23        6.7536        0.78536          0.78286          0.78231          0.77889           0.7842   
     24        7.5474        0.79983          0.79745          0.79653          0.79269          0.79678   
     25        8.3948        0.81271          0.81039          0.80929          0.80514          0.80831   
     26        9.2967        0.82418          0.82198          0.82076          0.81637          0.81875   
     27        10.254         0.8345          0.83242          0.83114          0.82656          0.82833   
     28        11.269        0.84377          0.84176          0.84042          0.83584          0.83706   
     29        12.342        0.85214          0.85024          0.84884           0.8442          0.84499   
     30        13.473        0.85964          0.85781          0.85647          0.85183          0.85232   
     31        14.665        0.86648          0.86471          0.86337          0.85879          0.85897   
     32        15.918        0.87264          0.87099          0.86965          0.86507          0.86507   
     33        17.233        0.87826          0.87667          0.87533          0.87161          0.87063   
     34        18.611        0.88338          0.88186          0.88052          0.87771          0.87582   
     35        20.053        0.88802          0.88656          0.88528          0.88326          0.88052   
     36         21.56         0.8923          0.89089          0.88967          0.88833          0.88485   
     37        23.133        0.89614          0.89486          0.89364           0.8926          0.88888   
     38        24.773        0.89974          0.89852           0.8973          0.89626           0.8926   
     39        26.481        0.90304          0.90182          0.90072          0.89968          0.89608   
     40        28.258        0.90603          0.90493          0.90383          0.90279          0.89925   
     41        30.104        0.90884          0.90774           0.9067          0.90572          0.90218   
     42        32.021         0.9114          0.91036          0.90932          0.90841          0.90493   
     43         34.01        0.91378           0.9128          0.91183          0.91091          0.90749   
     44         36.07        0.91598          0.91506          0.91408          0.91317          0.90987   
     45        38.204        0.91805          0.91714          0.91622          0.91537          0.91207   
     46        40.412        0.91994          0.91909          0.91817          0.91732          0.91415   
     47        42.695        0.92171          0.92086          0.92001          0.91921           0.9161   
     48        45.053        0.92336          0.92257          0.92171          0.92092          0.91799   
     49        47.488        0.92489          0.92409          0.92336          0.92257          0.92025   
     50            50        0.92629          0.92562          0.92489          0.92428          0.92403   

Run the function and show summaries for savings and fraction of coh saved:

mp_params('it_a_n') = 100;
mp_params('it_z_n') = 9;
mp_support('ls_ffcmd') = {'ap', 'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_support('bl_vfi_store_all') = true; % store c(a,z), y(a,z)
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 1.164186 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    ap             1     1      2       900     100      9       12926     14.362     14.544     1.0127      0      51.171
    savefraccoh    2     2      2       900     100      9      621.24    0.69027    0.26896    0.38965      0     0.92739

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3          c4           c5         c6         c7         c8        c9  
            ______    ______    ______    __________    ________    _______    _______    ______    ______

    r1           0         0         0             0    0.087442    0.27778    0.58243    1.0038    1.5724
    r2           0         0         0             0    0.087962    0.27828    0.58297    1.0044    1.5731
    r3           0         0         0             0    0.090477    0.28074    0.58547    1.0069    1.5755
    r4           0         0         0    0.00055771     0.09279    0.28605     0.5907    1.0122    1.5808
    r5           0         0         0     0.0059496     0.09602    0.29477    0.59952    1.0209    1.5895
    r96     43.845    43.923    44.022        44.198      44.428     44.722     45.103    45.546    46.186
    r97     45.031    45.101    45.208        45.384      45.613      45.91     46.293    46.735    47.382
    r98     46.237    46.297    46.411         46.59      46.818     47.115     47.501    47.948    48.605
    r99      47.46    47.512    47.635        47.812      48.041      48.34     48.726    49.191    49.869
    r100    48.703    48.746    48.878        49.055      49.283     49.586     49.978    50.495    51.171

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3           c4           c5         c6         c7         c8         c9   
            _______    _______    _______    __________    ________    _______    _______    _______    _______

    r1            0          0          0             0    0.066018    0.16569    0.27445    0.37369    0.46243
    r2            0          0          0             0    0.066384    0.16593    0.27463    0.37381    0.46256
    r3            0          0          0             0    0.068154    0.16715    0.27549    0.37442    0.46292
    r4            0          0          0    0.00052879    0.069619    0.16978    0.27726    0.37564    0.46378
    r5            0          0          0     0.0055946    0.071572    0.17405    0.28025    0.37766    0.46512
    r96     0.92458    0.92354    0.92226       0.92171     0.92116    0.92055    0.91994    0.91842    0.91811
    r97     0.92531    0.92416    0.92306       0.92251     0.92196    0.92141    0.92086    0.91933    0.91915
    r98     0.92605     0.9247    0.92379        0.9233     0.92275     0.9222    0.92171    0.92031    0.92031
    r99     0.92672    0.92525    0.92452       0.92403     0.92348      0.923    0.92251    0.92147    0.92184
    r100    0.92739     0.9258    0.92525       0.92477     0.92422    0.92379    0.92342    0.92336    0.92367

1.4.4 Test FF_VFI_AZ_BISEC_VEC Change Interest Rate and Discount

Show only save fraction of cash on hand:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with several different interest rates and discount factor:

% Lower Savings Incentives
mp_params('fl_beta') = 0.80;
mp_params('fl_r') = 0.01;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 0.271658 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      357.85    0.51122    0.27528    0.53848      0     0.79965

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5           c6           c7   
            _______    _______    _______    _______    _______    __________    ________

    r1            0          0          0          0          0    0.00022362    0.041544
    r2            0          0          0          0          0    0.00022362    0.041544
    r3            0          0          0          0          0     0.0011391    0.041544
    r4            0          0          0          0          0     0.0016884    0.041483
    r5            0          0          0          0          0     0.0034584     0.04136
    r96     0.79586    0.79275    0.78945    0.78591    0.78225       0.77853     0.77059
    r97     0.79684    0.79379    0.79055    0.78713    0.78359       0.77993     0.77212
    r98     0.79782    0.79482    0.79171    0.78835    0.78488       0.78127     0.77365
    r99     0.79873    0.79586    0.79275    0.78951     0.7861       0.78262     0.77548
    r100    0.79965    0.79684    0.79385    0.79061    0.78732        0.7839      0.7781

% Higher Savings Incentives
mp_params('fl_beta') = 0.95;
mp_params('fl_r') = 0.04;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 0.971218 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      481.37    0.68768    0.27118    0.39435      0     0.92702

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4         c5         c6         c7   
            _______    _______    _______    ________    _______    _______    _______

    r1            0          0          0    0.065774    0.18076    0.30655    0.41654
    r2            0          0          0    0.066201    0.18101    0.30674     0.4166
    r3            0          0          0     0.06791    0.18223    0.30747    0.41709
    r4            0          0          0    0.069619    0.18467    0.30759    0.41812
    r5            0          0          0    0.071694    0.18876    0.30838    0.41983
    r96     0.92428    0.92245    0.92178     0.92116    0.92049    0.91872    0.91824
    r97     0.92501    0.92324    0.92257     0.92196    0.92129    0.91958    0.91921
    r98     0.92574    0.92397    0.92336     0.92275    0.92208    0.92049    0.92025
    r99     0.92647     0.9247    0.92409     0.92348    0.92287    0.92147    0.92159
    r100    0.92702    0.92544    0.92483     0.92422    0.92373    0.92336    0.92348

1.4.5 Test FF_VFI_AZ_BISEC_VEC Changing Risk Aversion

Here, again, show fraction of coh saved in summary tabular form, but also show it graphically.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {'savefraccoh'};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with different risk aversion levels, higher preferences for risk:

% Lower Risk Aversion
mp_params('fl_crra') = 0.5;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 0.873752 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      452.13    0.6459    0.28031    0.43398      0     0.90359

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4           c5         c6         c7   
            _______    _______    _______    _________    ________    _______    _______

    r1            0          0          0    0.0047401    0.089089    0.19822    0.30783
    r2            0          0          0    0.0051674    0.089394     0.1984    0.30796
    r3            0          0          0    0.0060218    0.090676    0.19926    0.30851
    r4            0          0          0    0.0082801    0.092812    0.20115    0.30973
    r5            0          0          0     0.012247    0.092995     0.2042    0.31174
    r96     0.90047    0.89925    0.89828       0.8973     0.89632    0.89376    0.89297
    r97     0.90127    0.90017    0.89919      0.89828      0.8973     0.8948    0.89394
    r98     0.90206    0.90102    0.90011      0.89919     0.89828    0.89577    0.89498
    r99     0.90279    0.90188    0.90102      0.90011     0.89919    0.89681     0.8959
    r100    0.90359    0.90273    0.90188      0.90096     0.90011    0.89803    0.89687

When risk aversion increases, at every state-space point, the household wants to save more.

% Higher Risk Aversion
mp_params('fl_crra') = 5;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 0.970314 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      502.71    0.71816    0.25437     0.3542      0     0.93587

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2          c3         c4         c5         c6         c7   
            _______    _______    ________    _______    _______    _______    _______

    r1            0          0    0.047037    0.15537    0.27573     0.3909    0.48782
    r2            0          0    0.047525    0.15531    0.27591    0.39102    0.48795
    r3            0          0    0.049844     0.1569    0.27695     0.3917    0.48837
    r4            0          0    0.054788    0.16025    0.27915     0.3931    0.48929
    r5            0          0    0.062905    0.16569    0.28275    0.39542    0.49075
    r96     0.93307    0.93258     0.93203    0.93154     0.9302    0.92995    0.92971
    r97     0.93374    0.93325     0.93276    0.93227    0.93111    0.93105    0.93117
    r98     0.93441    0.93398     0.93349    0.93307    0.93209    0.93227     0.9327
    r99     0.93508    0.93465     0.93423    0.93392    0.93331    0.93368    0.93435
    r100    0.93575    0.93539     0.93508     0.9349    0.93496    0.93526    0.93587

1.4.6 Test FF_VFI_AZ_BISEC_VEC with Higher Uncertainty

Increase the standard deviation of the Shock.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 150;
mp_params('it_z_n') = 15;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';
% graph color spectrum
mp_params('cl_colors') = 'copper';

Lower standard deviation of shock:

% Lower Risk Aversion
mp_params('fl_shk_std') = 0.10;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 2.595920 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1507.5    0.67001    0.28668    0.42788      0     0.92568

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.13847    0.18485    0.23026    0.27378    0.31729
    r2            0          0          0          0          0    0.13853    0.18491    0.23032    0.27384    0.31736
    r3            0          0          0          0          0    0.13895    0.18528    0.23063    0.27408     0.3176
    r4            0          0          0          0          0    0.13987    0.18607     0.2313    0.27469    0.31809
    r5            0          0          0          0          0    0.14011    0.18735     0.2324    0.27567    0.31888
    r146    0.92373    0.92354     0.9233    0.92312    0.92287    0.92086    0.92068    0.92049    0.91952    0.91933
    r147    0.92422    0.92403    0.92385    0.92361    0.92342    0.92141    0.92123    0.92098    0.92007    0.91988
    r148     0.9247    0.92452    0.92434    0.92409    0.92391     0.9219    0.92171    0.92153    0.92062    0.92043
    r149    0.92519    0.92501    0.92483    0.92458     0.9244    0.92245    0.92226    0.92208    0.92116     0.9211
    r150    0.92568     0.9255    0.92531    0.92507    0.92489    0.92293    0.92275    0.92257    0.92245    0.92232

Higher shock standard deviation: low shock high asset save more, high shock more asset save less, high shock low asset save more:

% Higher Risk Aversion
mp_params('fl_shk_std') = 0.40;
ff_vfi_az_bisec_vec(mp_params, mp_support);

Elapsed time is 2.805227 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1685.6    0.74914    0.22909     0.3058      0     0.93679

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0     0.5264    0.61264    0.68271    0.73922    0.78433
    r2            0          0          0          0          0    0.52646    0.61264    0.68271    0.73922    0.78433
    r3            0          0          0          0          0    0.52658     0.6127    0.68271    0.73922    0.78433
    r4            0          0          0          0          0    0.52682    0.61288    0.68283    0.73928    0.78439
    r5            0          0          0          0          0    0.52731    0.61313    0.68295    0.73934    0.78439
    r146    0.92983    0.92928    0.92873    0.92806    0.92739    0.92269    0.92354     0.9258    0.92904    0.93331
    r147     0.9302    0.92971     0.9291    0.92849    0.92788    0.92361    0.92477     0.9269    0.93001    0.93423
    r148    0.93056    0.93008    0.92953    0.92892    0.92831    0.92458    0.92593      0.928    0.93105    0.93508
    r149    0.93093    0.93044    0.92995    0.92934    0.92873     0.9258    0.92702     0.9291    0.93203      0.936
    r150     0.9313    0.93087    0.93032    0.92977    0.92916    0.92696    0.92818    0.93014    0.93294    0.93679

1.5 FF_VFI_AZ_MZOOM_LOOP Savings Loop Exact (VALUE) Examples

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_vfi_az_mzoom_loop from the MEconTools Package. This function solves the dynamic programming problem for a (a,z) model. The state-space is on a grid, but choice grids are in terms of percentage of resources to save and solved exactly.

This is a looped code for continuous choices, solved with the mzoom algorithm. In contrast to the bisection based solution, this is slower, but this does not rely on first order conditions.

Links to Other Code:

Core Savings/Borrowing Dynamic Programming Solution Functions that are functions in the MEconTools Package. :

  • Common Choice and States Grid Loop: ff_vfi_az_loop

  • Common Choice and States Grid Vectorized: ff_vfi_az_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC, Loop:ff_vfi_az_bisec_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC Vectorized: ff_vfi_az_bisec_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Loop:ff_vfi_az_mzoom_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Vectorized: ff_vfi_az_mzoom_vec

1.5.1 Test FF_VFI_AZ_MZOOM_LOOP Defaults

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('fl_crra') = 1.5;
mp_params('fl_beta') = 0.94;
% call function
ff_vfi_az_mzoom_loop(mp_params);

Elapsed time is 83.956071 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min     max  
          _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    ______

    ap    1     1      2       700     100      7      9861.5    14.088    14.386     1.0212      0     50.115

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3         c4         c5         c6         c7  
            ______    ______    ______    ________    _______    _______    ______

    r1           0         0         0     0.05343    0.25568    0.60598    1.1155
    r2           0         0         0    0.053451    0.25571    0.60652    1.1161
    r3           0         0         0    0.056468    0.25574    0.60897    1.1174
    r4           0         0         0    0.061232    0.25995    0.61042    1.1238
    r5           0         0         0    0.065929     0.2689    0.61091    1.1323
    r96     43.387    43.517      43.7      43.922     44.221     44.657    45.225
    r97     44.562    44.694    44.876      45.095     45.392     45.847    46.394
    r98     45.758     45.89    46.071      46.287     46.583     47.037    47.596
    r99     46.972    47.103    47.285        47.5     47.794     48.247    48.812
    r100    48.183    48.337    48.518      48.732     49.025     49.478    50.115

1.5.2 Test FF_VFI_AZ_MZOOM_LOOP Speed Tests

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('ls_ffcmd') = {};

A grid 50, shock grid 5:

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 26.554641 seconds.

A grid 750, shock grid 15:

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 15;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 2148.508425 seconds.

A grid 600, shock grid 45:

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 600;
mp_params('it_z_n') = 45;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 8507.097739 seconds.

1.5.3 Test FF_VFI_AZ_MZOOM_LOOP Control Outputs

Run the function first without any outputs, but only the timer.

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {};
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 24.011245 seconds.

Run the function and show policy function for savings choice. For ls_ffcmd, ls_ffsna, ls_ffgrh, can include these: ‘v,’ ‘ap,’ ‘c,’ ‘y,’ ‘coh,’ ‘savefraccoh.’ These are value, aprime savings choice, consumption, income, cash on hand, and savings fraction as cash-on-hand.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 23.773078 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       a        mean_z_0_4858    mean_z_0_67798    mean_z_0_9462    mean_z_1_3205    mean_z_1_8429
    _____    ________    _____________    ______________    _____________    _____________    _____________

      1             0             0                 0         0.067148           0.2084          0.35952   
      2      0.002975             0                 0         0.069345          0.20826          0.36029   
      3      0.016829             0                 0         0.070749           0.2136          0.36206   
      4      0.046375             0         0.0059631          0.08732          0.22641          0.36263   
      5      0.095198      0.008725          0.033935          0.11637          0.24674           0.3747   
      6        0.1663      0.054327          0.077152          0.15198          0.26635          0.39127   
      7       0.26234      0.099882           0.13131           0.1936          0.29922          0.41248   
      8       0.38568       0.15954            0.1928          0.24107          0.33005          0.43049   
      9       0.53852       0.23411           0.25482          0.29164          0.37407           0.4593   
     10       0.72291       0.30704           0.31604          0.34806          0.41148          0.48371   
     11       0.94076       0.37567           0.37487          0.40768          0.44925          0.50972   
     12        1.1939       0.43849           0.42939           0.4573          0.48691          0.54333   
     13         1.484       0.49491           0.48129          0.50332          0.53253          0.56934   
     14        1.8128       0.54486           0.53013          0.54642          0.56773          0.59615   
     15        2.1817       0.58868           0.57335          0.58545          0.60016          0.62817   
     16        2.5924        0.6271           0.61254          0.62056          0.63057          0.65247   
     17        3.0463       0.66058            0.6468          0.65237          0.65884          0.67518   
     18        3.5449       0.69019           0.67699          0.68069          0.68379          0.69636   
     19        4.0894       0.71615           0.70375           0.7058          0.70719           0.7159   
     20        4.6813       0.73661           0.72701          0.72843          0.72781          0.73341   
     21        5.3218       0.75302            0.7481          0.74821          0.74661          0.74981   
     22        6.0121       0.76912           0.76622          0.76622          0.76342          0.76534   
     23        6.7536       0.78503           0.78285          0.78223          0.77885          0.78383   
     24        7.5474       0.79943           0.79703          0.79623          0.79223          0.79677   
     25        8.3948       0.81264           0.81024           0.8093          0.80504          0.80784   
     26        9.2967       0.82384           0.82198          0.82064          0.81634          0.81874   
     27        10.254       0.83447           0.83225          0.83065          0.82653          0.82824   
     28        11.269       0.84345           0.84174          0.84025          0.83545          0.83703   
     29        12.342       0.85185           0.85017          0.84865          0.84417          0.84497   
     30        13.473       0.85962           0.85746          0.85642          0.85178          0.85185   
     31        14.665       0.86626           0.86466          0.86306          0.85873          0.85895   
     32        15.918       0.87226           0.87066          0.86959          0.86504          0.86466   
     33        17.233       0.87786           0.87626          0.87529          0.87146          0.87061   
     34        18.611       0.88332           0.88182          0.88026          0.87766          0.87546   
     35        20.053         0.888           0.88656          0.88507          0.88267          0.88026   
     36         21.56       0.89187           0.89087          0.88947          0.88825          0.88483   
     37        23.133       0.89587           0.89484          0.89347          0.89256          0.88867   
     38        24.773        0.8997           0.89827          0.89727          0.89587          0.89259   
     39        26.481         0.903           0.90147          0.90066          0.89964          0.89587   
     40        28.258       0.90601           0.90467          0.90376          0.90278          0.89907   
     41        30.104       0.90881            0.9077          0.90628          0.90547          0.90216   
     42        32.021       0.91137           0.91035          0.90908          0.90838          0.90467   
     43         34.01       0.91377           0.91275          0.91148          0.91068          0.90708   
     44         36.07       0.91595           0.91468          0.91388          0.91308          0.90983   
     45        38.204       0.91788           0.91708          0.91617          0.91531          0.91204   
     46        40.412       0.91948           0.91868          0.91788          0.91708          0.91388   
     47        42.695       0.92168           0.92085          0.91998          0.91915          0.91604   
     48        45.053       0.92331           0.92251          0.92171          0.92091          0.91788   
     49        47.488       0.92485           0.92408          0.92331          0.92254           0.9202   
     50            50       0.92588           0.92555          0.92485          0.92423          0.92402   

Run the function and show summaries for savings and fraction of coh saved:

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
% mp_params('fl_crra') = 1.5;
% mp_params('fl_beta') = 0.94;
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 9;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
% call function
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 111.419370 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group        a         mean_z_0_36853    mean_z_0_46648    mean_z_0_59047    mean_z_0_74742    mean_z_0_94608    mean_z_1_1975    mean_z_1_5159    mean_z_1_9188    mean_z_2_4288
    _____    __________    ______________    ______________    ______________    ______________    ______________    _____________    _____________    _____________    _____________

       1              0               0                0                 0                  0         0.065547          0.16519          0.27438          0.37327          0.46242   
       2     0.00051272               0                0                 0                  0         0.066347          0.16588          0.27443           0.3738           0.4625   
       3      0.0029004               0                0                 0                  0         0.067948          0.16679          0.27523          0.37407           0.4625   
       4      0.0079925               0                0                 0         0.00050216         0.069549          0.16975          0.27721          0.37559           0.4633   
       5       0.016407               0                0                 0           0.005563         0.071534          0.17359           0.2802          0.37727           0.4649   
       6       0.028662               0                0                 0           0.011926         0.080274          0.17999          0.28363          0.38054           0.4665   
       7       0.045213               0                0                 0           0.022095         0.090757          0.18443          0.28523          0.38087           0.4665   
       8        0.06647               0                0         0.0043625           0.033935          0.10076          0.19478          0.29244          0.38621           0.4697   
       9       0.092813      0.00076108        0.0091251          0.017748           0.047979          0.11397           0.2068          0.30084          0.39207          0.47371   
      10        0.12459         0.02539         0.027791          0.036336           0.066347          0.13237          0.21681          0.31124          0.39938          0.47898   
      11        0.16214        0.049062         0.054743          0.057497           0.087289          0.14878          0.23341          0.31965           0.4076          0.48451   
      12        0.20576        0.080353         0.076351          0.084213            0.11115          0.16729          0.24668          0.33295          0.41295          0.48895   
      13        0.25576         0.11036          0.10076           0.11357            0.13677           0.1944          0.26643          0.34326          0.42327          0.49539   
      14        0.31242         0.14798          0.12866           0.14076            0.16483          0.21731          0.28363          0.35886          0.43195          0.50332   
      15        0.37601         0.17839          0.16439           0.16895              0.194          0.24107          0.30164          0.37165          0.44362          0.51204   
      16         0.4468          0.2098          0.20032            0.1988            0.22401          0.26563          0.32575          0.38953           0.4565          0.51772   
      17        0.52503         0.24246          0.23721           0.23371            0.25482          0.29153          0.34564          0.40412          0.46694          0.52773   
      18        0.61095         0.28123          0.27422           0.26803            0.28577          0.31725          0.36526          0.42169          0.48129           0.5355   
      19         0.7048         0.31861          0.30964           0.30224            0.31644          0.34326          0.38636          0.43889          0.49228          0.54649   
      20         0.8068         0.35352          0.34406           0.33561            0.34646          0.37247          0.41168           0.4555          0.50395          0.55454   
      21        0.91719         0.38727          0.37774           0.36766            0.37639          0.40048          0.43289          0.47181          0.51932          0.56671   
      22         1.0362         0.42001          0.40688           0.39888            0.40495          0.42569           0.4541          0.49222          0.53173          0.57495   
      23          1.164          0.4501          0.43289           0.42881            0.43266           0.4501          0.47451          0.50812          0.54573          0.58735   
      24         1.3008         0.47851          0.45746           0.45719            0.45922          0.47371           0.4948          0.52453           0.5608          0.59678   
      25         1.4468         0.50572          0.48514           0.48371            0.48451          0.49652          0.51432          0.54067          0.57335          0.60616   
      26         1.6023         0.53093          0.51118           0.50952            0.50892          0.51852          0.53322          0.56014          0.58575          0.61895   
      27         1.7673         0.55214          0.53571           0.53333            0.53173          0.53973          0.55258          0.57573          0.60152          0.62867   
      28         1.9422         0.57052          0.55854           0.55614            0.55374          0.55981          0.57335          0.59055          0.61336          0.63834   
      29          2.127         0.58782          0.58031           0.57735            0.57415          0.57893          0.59055          0.60534          0.62569          0.65071   
      30         2.3221         0.60768          0.60016           0.59758            0.59375          0.59695          0.60696           0.6194           0.6375          0.65978   
      31         2.5275         0.62577          0.61947           0.61496            0.61226          0.61416          0.62256          0.63297          0.65018          0.66898   
      32         2.7434         0.64351          0.63697           0.63101            0.62956          0.63057           0.6376          0.64617          0.66298          0.68097   
      33           2.97         0.65976          0.65338           0.64537            0.64591          0.64617          0.65178          0.66218          0.67378          0.68986   
      34         3.2075         0.67458          0.66898           0.66058            0.66124          0.66058          0.66498          0.67432          0.68445          0.69819   
      35          3.456         0.68919          0.68379           0.67538            0.67538          0.67458          0.67829          0.68592          0.69419          0.70716   
      36         3.7158          0.7022          0.69739           0.68939            0.68928          0.68779          0.69019          0.69659           0.7042           0.7154   
      37         3.9869          0.7146           0.7098            0.7022            0.70205          0.70039           0.7022           0.7074          0.71372          0.72609   
      38         4.2696         0.72668           0.7218            0.7146             0.7138          0.71209            0.713          0.71779           0.7254          0.73341   
      39          4.564         0.73741          0.73341            0.7262             0.7254          0.72317           0.7234          0.72748          0.73419          0.74121   
      40         4.8702         0.74798          0.74381           0.73711            0.73581          0.73341          0.73341          0.73661          0.74221          0.74821   
      41         5.1884         0.75768          0.75382           0.74727            0.74581          0.74348          0.74301          0.74551          0.75041          0.75542   
      42         5.5188         0.76679           0.7618           0.75684            0.75542          0.75281          0.75217          0.75382          0.75782          0.76232   
      43         5.8615         0.77502          0.76862           0.76542            0.76422          0.76165          0.76022          0.76182          0.76502          0.77102   
      44         6.2166         0.78303          0.77658           0.77422            0.77262          0.76996          0.76862          0.76941          0.77229          0.77742   
      45         6.5844         0.79063          0.78452           0.78223            0.78063          0.77742          0.77633          0.77661          0.77895           0.7835   
      46         6.9649         0.79783          0.79196           0.78983            0.78823          0.78529          0.78356          0.78543          0.78503          0.78903   
      47         7.3583         0.80499          0.79863           0.79695            0.79543          0.79223          0.79043          0.79223          0.79143          0.79463   
      48         7.7647         0.81024          0.80566           0.80343            0.80231          0.79863          0.79699          0.79863          0.79943          0.80023   
      49         8.1844         0.81504          0.81184           0.81003            0.80862          0.80504          0.80329          0.80471          0.80504          0.80547   
      50         8.6173         0.81984          0.81744           0.81584            0.81424          0.81104          0.80997          0.81024          0.81024          0.81024   
      51         9.0637         0.82544          0.82351           0.82144            0.82031          0.81664          0.81584          0.81573          0.81579          0.81733   
      52         9.5237         0.83065          0.82881           0.82664            0.82544          0.82224          0.82224          0.82064          0.82064          0.82191   
      53         9.9975         0.83545          0.83385           0.83217            0.83065          0.82744          0.82704          0.82544          0.82537          0.82624   
      54         10.485         0.84025          0.83863           0.83697            0.83545          0.83225           0.8321          0.83044          0.82985          0.83054   
      55         10.987         0.84494          0.84315           0.84155            0.84023          0.83703          0.83625          0.83465          0.83385          0.83457   
      56         11.502         0.84919          0.84705           0.84585            0.84425          0.84105            0.841          0.83915          0.83785          0.83844   
      57         12.032         0.85319          0.85156           0.85002            0.84785          0.84562          0.84505          0.84322          0.84185          0.84185   
      58         12.577         0.85666          0.85506           0.85396            0.85174          0.84945          0.84865          0.84665          0.84585          0.84574   
      59         13.136         0.86064          0.85906           0.85746            0.85506          0.85338          0.85265          0.85079          0.84945          0.84919   
      60         13.709         0.86386          0.86226           0.86122            0.85826          0.85666          0.85639          0.85425          0.85265          0.85245   
      61         14.298         0.86706          0.86596           0.86461            0.86138          0.86042          0.85978          0.85746          0.85586          0.85562   
      62         14.901         0.87052          0.86906           0.86746            0.86464          0.86372          0.86304          0.86066          0.85906          0.85826   
      63         15.519         0.87306          0.87215           0.87066            0.86746          0.86682          0.86615          0.86386          0.86226          0.86146   
      64         16.152         0.87626          0.87466           0.87378            0.87066          0.86981          0.86906          0.86698          0.86535          0.86464   
      65         16.801         0.87866          0.87779           0.87626             0.8736          0.87226          0.87196          0.86981          0.86812           0.8687   
      66         17.465         0.88163          0.88026           0.87923            0.87626          0.87538          0.87466          0.87226          0.87066          0.87126   
      67         18.144         0.88409          0.88267           0.88179            0.87866          0.87786          0.87706          0.87466          0.87386          0.87375   
      68         18.839         0.88646          0.88507           0.88422            0.88107          0.88026          0.87946           0.8776          0.87706          0.87612   
      69          19.55         0.88867          0.88747           0.88653            0.88347          0.88267          0.88187          0.87997          0.87946          0.87843   
      70         20.277         0.89087          0.88947           0.88867            0.88587          0.88507          0.88427          0.88187          0.88187          0.88026   
      71          21.02         0.89267          0.89187           0.89087            0.88787          0.88736          0.88659          0.88427          0.88419          0.88267   
      72         21.778         0.89493          0.89347           0.89267            0.89027          0.88945          0.88867          0.88656          0.88587          0.88477   
      73         22.553         0.89667          0.89582           0.89487            0.89187          0.89107          0.89027          0.88859          0.88825          0.88667   
      74         23.345         0.89827          0.89747           0.89667            0.89422          0.89336          0.89262          0.89027          0.89016          0.88862   
      75         24.152         0.90034          0.89907           0.89827            0.89587          0.89507          0.89427          0.89237          0.89187          0.89027   
      76         24.977         0.90204          0.90111           0.89987            0.89747          0.89667          0.89587          0.89416          0.89347          0.89187   
      77         25.818         0.90361          0.90274           0.90147            0.89907          0.89827          0.89797          0.89587          0.89507          0.89347   
      78         26.675         0.90515          0.90387           0.90307            0.90067          0.89987          0.89961          0.89747          0.89711          0.89551   
      79          27.55         0.90628          0.90547           0.90467            0.90227          0.90147          0.90117          0.89907          0.89827          0.89667   
      80         28.441         0.90788          0.90708           0.90547            0.90387          0.90307          0.90227          0.90066          0.89987          0.89827   
      81          29.35         0.90908           0.9086           0.90708            0.90547          0.90467          0.90387          0.90213          0.90147          0.89987   
      82         30.276         0.91068          0.90988           0.90825            0.90697          0.90623          0.90547          0.90354          0.90307          0.90146   
      83         31.219         0.91195          0.91121           0.90908            0.90828          0.90758           0.9069          0.90467          0.90444          0.90281   
      84         32.179         0.91308          0.91228           0.91035            0.90958          0.90887          0.90788          0.90623          0.90547          0.90387   
      85         33.157         0.91388          0.91361           0.91148            0.91068          0.90988          0.90908          0.90708          0.90703           0.9054   
      86         34.153         0.91543          0.91468           0.91228            0.91198           0.9113          0.91063          0.90872          0.90788          0.90628   
      87         35.166         0.91628          0.91548            0.9138            0.91308          0.91228          0.91148          0.90988          0.90908           0.9078   
      88         36.198         0.91708          0.91688           0.91468            0.91388          0.91355          0.91291          0.91068          0.91057          0.90893   
      89         37.247         0.91851          0.91786           0.91548            0.91527          0.91463          0.91388          0.91214          0.91148          0.90988   
      90         38.314         0.91946          0.91868           0.91691            0.91628          0.91548          0.91468          0.91308          0.91228          0.91108   
      91         39.399         0.92028          0.91948           0.91788            0.91708          0.91628          0.91604          0.91422          0.91374          0.91228   
      92         40.503         0.92108          0.92028           0.91868            0.91788          0.91761            0.917           0.9154          0.91468          0.91388   
      93         41.625         0.92188          0.92108           0.91948            0.91868          0.91851          0.91788          0.91628          0.91548          0.91527   
      94         42.765         0.92268          0.92188           0.92028            0.92001           0.9194          0.91868          0.91771          0.91628           0.9162   
      95         43.924         0.92348          0.92268           0.92108            0.92085          0.92026          0.91948          0.91868          0.91708          0.91708   
      96         45.102         0.92428          0.92348           0.92188            0.92168          0.92108          0.92028          0.91948          0.91838          0.91788   
      97         46.298         0.92508          0.92414           0.92268            0.92248          0.92188          0.92108          0.92028          0.91931          0.91868   
      98         47.513         0.92588          0.92469           0.92348            0.92325          0.92268          0.92188          0.92168          0.92026          0.92028   
      99         48.747         0.92668          0.92508           0.92428            0.92398          0.92347          0.92268          0.92248          0.92108           0.9218   
     100             50         0.92737           0.9258           0.92508            0.92428           0.9242          0.92348          0.92337          0.92334          0.92348   

1.5.4 Test FF_VFI_AZ_MZOOM_LOOP Change Interest Rate and Discount

Show only save fraction of cash on hand:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 9;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with several different interest rates and discount factor:

% Lower Savings Incentives
mp_params('fl_beta') = 0.80;
mp_params('fl_r') = 0.01;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 294.329574 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    _______    ________    ___    _______

    savefraccoh    1     1      2      6750     750      9      3468.2    0.5138    0.27192    0.52924      0     0.80103

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c6         c7         c8          c9   
            _______    _______    _______    _______    _______    _______    _______    _______    ________

    r1            0          0          0          0          0          0          0    0.02073    0.065955
    r2            0          0          0          0          0          0          0    0.02073    0.065955
    r3            0          0          0          0          0          0          0    0.02073    0.065955
    r4            0          0          0          0          0          0          0    0.02073    0.065955
    r5            0          0          0          0          0          0          0    0.02073    0.065987
    r746     0.8008    0.79843     0.7959    0.79303    0.78983    0.78663    0.78303    0.77903     0.77502
    r747    0.80092    0.79855    0.79603    0.79303    0.79058    0.78713    0.78362    0.77953     0.77553
    r748    0.80102    0.79863    0.79615     0.7935    0.79063    0.78729    0.78378    0.77972     0.77568
    r749    0.80103    0.79863    0.79623    0.79369    0.79063    0.78743    0.78383    0.77983     0.77582
    r750    0.80103    0.79904    0.79623    0.79378    0.79063    0.78743    0.78383    0.77983     0.77582

% Higher Savings Incentives
mp_params('fl_beta') = 0.95;
mp_params('fl_r') = 0.04;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 1309.412430 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    _______    ________    ___    _______

    savefraccoh    1     1      2      6750     750      9      4667.7    0.6915    0.26685     0.3859      0     0.92668

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4          c5         c6         c7         c8         c9   
            _______    _______    _______    _______    ________    _______    _______    _______    _______

    r1            0          0          0          0      0.0647    0.16668    0.27352    0.37327     0.4617
    r2            0          0          0          0      0.0647    0.16668    0.27352    0.37327     0.4617
    r3            0          0          0          0    0.064731    0.16668    0.27352    0.37327     0.4617
    r4            0          0          0          0    0.064731    0.16668    0.27355    0.37327     0.4617
    r5            0          0          0          0    0.064747    0.16671    0.27355    0.37327     0.4617
    r746    0.92657    0.92588    0.92508    0.92428     0.92348    0.92268    0.92235    0.92188    0.92188
    r747    0.92664    0.92588    0.92508    0.92428     0.92402    0.92318    0.92248    0.92188    0.92235
    r748    0.92668    0.92588    0.92508    0.92478     0.92411    0.92328     0.9226    0.92188     0.9226
    r749    0.92668    0.92588    0.92555    0.92488      0.9242     0.9234    0.92268    0.92254    0.92268
    r750    0.92668    0.92588    0.92565    0.92497     0.92427    0.92348    0.92268    0.92268    0.92268

1.5.5 Test FF_VFI_AZ_MZOOM_LOOP Changing Risk Aversion

Here, again, show fraction of coh saved in summary tabular form, but also show it graphically.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {'savefraccoh'};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with different risk aversion levels, higher preferences for risk:

% Lower Risk Aversion
mp_params('fl_crra') = 0.5;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 84.461743 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      452.03    0.64575    0.28029    0.43406      0     0.90354

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4           c5         c6         c7   
            _______    _______    _______    _________    ________    _______    _______

    r1            0          0          0    0.0047077    0.089109      0.198    0.30781
    r2            0          0          0    0.0051079    0.089156      0.198    0.30793
    r3            0          0          0    0.0059631    0.090679     0.1988    0.30848
    r4            0          0          0    0.0079639    0.092358    0.20109    0.30964
    r5            0          0          0     0.011926    0.092758    0.20413    0.31171
    r96     0.90047    0.89907    0.89826      0.89727     0.89587    0.89347    0.89267
    r97     0.90127    0.89987    0.89907      0.89822     0.89727    0.89477    0.89394
    r98     0.90204    0.90067    0.89987      0.89907     0.89822    0.89573    0.89493
    r99     0.90278    0.90147    0.90067      0.89987     0.89907    0.89667    0.89587
    r100    0.90354    0.90227    0.90147      0.90067     0.89987    0.89801    0.89667

When risk aversion increases, at every state-space point, the household wants to save more.

% Higher Risk Aversion
mp_params('fl_crra') = 5;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 88.697274 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum     mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    _____    _____    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      502.6    0.718    0.25437    0.35427      0     0.93587

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2          c3         c4         c5         c6         c7   
            _______    _______    ________    _______    _______    _______    _______

    r1            0          0     0.04674    0.15532    0.27563    0.39047    0.48771
    r2            0          0    0.047493    0.15525    0.27563    0.39101    0.48771
    r3            0          0    0.049541    0.15685    0.27693    0.39127    0.48834
    r4            0          0    0.054343    0.16018    0.27883    0.39287    0.48923
    r5            0          0    0.062848    0.16566    0.28272    0.39528    0.49071
    r96     0.93269    0.93251     0.93189    0.93108    0.93014    0.92988    0.92968
    r97     0.93349    0.93322     0.93269    0.93189    0.93107    0.93104    0.93108
    r98     0.93429    0.93349     0.93347    0.93269    0.93189    0.93189    0.93269
    r99     0.93507    0.93429     0.93424    0.93349    0.93331    0.93349    0.93429
    r100    0.93575    0.93509     0.93507    0.93488    0.93491    0.93509    0.93587

1.5.6 Test FF_VFI_AZ_MZOOM_LOOP with Higher Uncertainty

Increase the standard deviation of the Shock.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 150;
mp_params('it_z_n') = 15;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Lower standard deviation of shock:

% Lower Risk Aversion
mp_params('fl_shk_std') = 0.10;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 304.022067 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1507.2    0.66985    0.28667    0.42796      0     0.92565

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.13838    0.18479    0.23021    0.27363    0.31725
    r2            0          0          0          0          0    0.13838    0.18479    0.23027    0.27363    0.31725
    r3            0          0          0          0          0    0.13894    0.18526    0.23041    0.27407    0.31725
    r4            0          0          0          0          0    0.13987    0.18606    0.23121    0.27443    0.31805
    r5            0          0          0          0          0    0.13998    0.18719    0.23201    0.27563    0.31885
    r146    0.92348    0.92348    0.92328    0.92268    0.92268    0.92085    0.92028    0.92028    0.91948    0.91931
    r147     0.9242    0.92398    0.92348    0.92348    0.92337    0.92108    0.92108    0.92097    0.92001    0.91948
    r148    0.92428    0.92428    0.92428    0.92408    0.92348    0.92188    0.92171    0.92108    0.92028    0.92028
    r149    0.92508    0.92497    0.92478    0.92428    0.92428    0.92241    0.92188    0.92188    0.92108    0.92107
    r150    0.92565    0.92508    0.92508    0.92507    0.92485    0.92268    0.92268    0.92254    0.92238    0.92188

Higher shock standard deviation: low shock high asset save more, high shock more asset save less, high shock low asset save more:

% Higher Risk Aversion
mp_params('fl_shk_std') = 0.40;
ff_vfi_az_mzoom_loop(mp_params, mp_support);

Elapsed time is 304.175092 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1685.2    0.74898    0.22908    0.30585      0     0.93669

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.52613    0.61256    0.68259    0.73901    0.78427
    r2            0          0          0          0          0    0.52613    0.61256    0.68259    0.73901     0.7843
    r3            0          0          0          0          0    0.52613    0.61256    0.68259    0.73901     0.7843
    r4            0          0          0          0          0    0.52682    0.61256    0.68259    0.73901    0.78433
    r5            0          0          0          0          0    0.52693    0.61309    0.68259    0.73901    0.78439
    r146    0.92948    0.92925    0.92828    0.92805    0.92737    0.92263    0.92348    0.92577    0.92901    0.93331
    r147    0.93017    0.92948    0.92868    0.92828    0.92748    0.92348    0.92428    0.92668    0.93002    0.93421
    r148    0.93028    0.93005    0.92948    0.92891    0.92827    0.92428    0.92587    0.92799    0.93101    0.93507
    r149    0.93091    0.93028    0.92948    0.92931    0.92828    0.92574    0.92668    0.92904    0.93189    0.93589
    r150    0.93108    0.93082    0.93027    0.92948    0.92868    0.92668    0.92814    0.93008    0.93269    0.93669

1.6 FF_VFI_AZ_MZOOM_VEC Savings Vectorized Exact (VALUE) Examples

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_vfi_az_mzoom_vec from the MEconTools Package. This function solves the dynamic programming problem for a (a,z) model. The state-space is on a grid, but choice grids are in terms of percentage of resources to save and solved exactly.

This is a vectorized code for continuous choices, solved with the mzoom algorithm. In contrast to the bisection based solution, this is slower, but this does not rely on first order conditions.

Links to Other Code:

Core Savings/Borrowing Dynamic Programming Solution Functions that are functions in the MEconTools Package. :

  • Common Choice and States Grid Loop: ff_vfi_az_loop

  • Common Choice and States Grid Vectorized: ff_vfi_az_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC, Loop:ff_vfi_az_bisec_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, rely on FOC Vectorized: ff_vfi_az_bisec_vec

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Loop:ff_vfi_az_mzoom_loop

  • States Grid + Continuous Exact Savings as Share of Cash-on-Hand, VALUE comparison, Vectorized: ff_vfi_az_mzoom_vec

1.6.1 Test FF_VFI_AZ_MZOOM_VEC Defaults

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('fl_crra') = 1.5;
mp_params('fl_beta') = 0.94;
% call function
ff_vfi_az_mzoom_vec(mp_params);

Elapsed time is 6.126702 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          i    idx    ndim    numel    rowN    colN     sum       mean      std      coefvari    min     max  
          _    ___    ____    _____    ____    ____    ______    ______    ______    ________    ___    ______

    ap    1     1      2       700     100      7      9861.5    14.088    14.386     1.0212      0     50.115

xxx TABLE:ap xxxxxxxxxxxxxxxxxx
              c1        c2        c3         c4         c5         c6         c7  
            ______    ______    ______    ________    _______    _______    ______

    r1           0         0         0     0.05343    0.25568    0.60598    1.1155
    r2           0         0         0    0.053451    0.25571    0.60652    1.1161
    r3           0         0         0    0.056468    0.25574    0.60897    1.1174
    r4           0         0         0    0.061232    0.25995    0.61042    1.1238
    r5           0         0         0    0.065929     0.2689    0.61091    1.1323
    r96     43.387    43.517      43.7      43.922     44.221     44.657    45.225
    r97     44.562    44.694    44.876      45.095     45.392     45.847    46.394
    r98     45.758     45.89    46.071      46.287     46.583     47.037    47.596
    r99     46.972    47.103    47.285        47.5     47.794     48.247    48.812
    r100    48.183    48.337    48.518      48.732     49.025     49.478    50.115

1.6.2 Test FF_VFI_AZ_MZOOM_VEC Speed Tests

Call the function with defaults. By default, shows the asset policy function summary. Model parameters can be changed by the mp_params.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('ls_ffcmd') = {};
% A grid 50, shock grid 5:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 1.996365 seconds.

% A grid 750, shock grid 15:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 15;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 337.171768 seconds.

% A grid 600, shock grid 45:
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 600;
mp_params('it_z_n') = 45;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 1758.273287 seconds.

1.6.3 Test FF_VFI_AZ_MZOOM_VEC Control Outputs

Run the function first without any outputs, but only the timer.

mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 50;
mp_params('it_z_n') = 5;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_timer') = true;
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {};
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 1.091918 seconds.

Run the function and show policy function for savings choice. For ls_ffcmd, ls_ffsna, ls_ffgrh, can include these: ‘v,’ ‘ap,’ ‘c,’ ‘y,’ ‘coh,’ ‘savefraccoh.’ These are value, aprime savings choice, consumption, income, cash on hand, and savings fraction as cash-on-hand.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 1.090424 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       a        mean_z_0_4858    mean_z_0_67798    mean_z_0_9462    mean_z_1_3205    mean_z_1_8429
    _____    ________    _____________    ______________    _____________    _____________    _____________

      1             0             0                 0         0.067148           0.2084          0.35952   
      2      0.002975             0                 0         0.069345          0.20826          0.36029   
      3      0.016829             0                 0         0.070749           0.2136          0.36206   
      4      0.046375             0         0.0059631          0.08732          0.22641          0.36263   
      5      0.095198      0.008725          0.033935          0.11637          0.24674           0.3747   
      6        0.1663      0.054327          0.077152          0.15198          0.26635          0.39127   
      7       0.26234      0.099882           0.13131           0.1936          0.29922          0.41248   
      8       0.38568       0.15954            0.1928          0.24107          0.33005          0.43049   
      9       0.53852       0.23411           0.25482          0.29164          0.37407           0.4593   
     10       0.72291       0.30704           0.31604          0.34806          0.41148          0.48371   
     11       0.94076       0.37567           0.37487          0.40768          0.44925          0.50972   
     12        1.1939       0.43849           0.42939           0.4573          0.48691          0.54333   
     13         1.484       0.49491           0.48129          0.50332          0.53253          0.56934   
     14        1.8128       0.54486           0.53013          0.54642          0.56773          0.59615   
     15        2.1817       0.58868           0.57335          0.58545          0.60016          0.62817   
     16        2.5924        0.6271           0.61254          0.62056          0.63057          0.65247   
     17        3.0463       0.66058            0.6468          0.65237          0.65884          0.67518   
     18        3.5449       0.69019           0.67699          0.68069          0.68379          0.69636   
     19        4.0894       0.71615           0.70375           0.7058          0.70719           0.7159   
     20        4.6813       0.73661           0.72701          0.72843          0.72781          0.73341   
     21        5.3218       0.75302            0.7481          0.74821          0.74661          0.74981   
     22        6.0121       0.76912           0.76622          0.76622          0.76342          0.76534   
     23        6.7536       0.78503           0.78285          0.78223          0.77885          0.78383   
     24        7.5474       0.79943           0.79703          0.79623          0.79223          0.79677   
     25        8.3948       0.81264           0.81024           0.8093          0.80504          0.80784   
     26        9.2967       0.82384           0.82198          0.82064          0.81634          0.81874   
     27        10.254       0.83447           0.83225          0.83065          0.82653          0.82824   
     28        11.269       0.84345           0.84174          0.84025          0.83545          0.83703   
     29        12.342       0.85185           0.85017          0.84865          0.84417          0.84497   
     30        13.473       0.85962           0.85746          0.85642          0.85178          0.85185   
     31        14.665       0.86626           0.86466          0.86306          0.85873          0.85895   
     32        15.918       0.87226           0.87066          0.86959          0.86504          0.86466   
     33        17.233       0.87786           0.87626          0.87529          0.87146          0.87061   
     34        18.611       0.88332           0.88182          0.88026          0.87766          0.87546   
     35        20.053         0.888           0.88656          0.88507          0.88267          0.88026   
     36         21.56       0.89187           0.89087          0.88947          0.88825          0.88483   
     37        23.133       0.89587           0.89484          0.89347          0.89256          0.88867   
     38        24.773        0.8997           0.89827          0.89727          0.89587          0.89259   
     39        26.481         0.903           0.90147          0.90066          0.89964          0.89587   
     40        28.258       0.90601           0.90467          0.90376          0.90278          0.89907   
     41        30.104       0.90881            0.9077          0.90628          0.90547          0.90216   
     42        32.021       0.91137           0.91035          0.90908          0.90838          0.90467   
     43         34.01       0.91377           0.91275          0.91148          0.91068          0.90708   
     44         36.07       0.91595           0.91468          0.91388          0.91308          0.90983   
     45        38.204       0.91788           0.91708          0.91617          0.91531          0.91204   
     46        40.412       0.91948           0.91868          0.91788          0.91708          0.91388   
     47        42.695       0.92168           0.92085          0.91998          0.91915          0.91604   
     48        45.053       0.92331           0.92251          0.92171          0.92091          0.91788   
     49        47.488       0.92485           0.92408          0.92331          0.92254           0.9202   
     50            50       0.92588           0.92555          0.92485          0.92423          0.92402   

Run the function and show summaries for savings and fraction of coh saved:

%mp_params
mp_params = containers.Map('KeyType','char', 'ValueType','any');
% mp_params('fl_crra') = 1.5;
% mp_params('fl_beta') = 0.94;
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 9;
mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
% ls_ffcmd: summary print which outcomes
mp_support('ls_ffcmd') = {};
% ls_ffsna: detail print which outcomes
mp_support('ls_ffsna') = {'savefraccoh'};
% ls_ffgrh: graphical print which outcomes
mp_support('ls_ffgrh') = {'savefraccoh'};
% call function
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 5.173849 seconds.
xxx  ff_vfi_az_vec, outcome=savefraccoh  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group        a         mean_z_0_36853    mean_z_0_46648    mean_z_0_59047    mean_z_0_74742    mean_z_0_94608    mean_z_1_1975    mean_z_1_5159    mean_z_1_9188    mean_z_2_4288
    _____    __________    ______________    ______________    ______________    ______________    ______________    _____________    _____________    _____________    _____________

       1              0               0                0                 0                  0         0.065547          0.16519          0.27438          0.37327          0.46242   
       2     0.00051272               0                0                 0                  0         0.066347          0.16588          0.27443           0.3738           0.4625   
       3      0.0029004               0                0                 0                  0         0.067948          0.16679          0.27523          0.37407           0.4625   
       4      0.0079925               0                0                 0         0.00050216         0.069549          0.16975          0.27721          0.37559           0.4633   
       5       0.016407               0                0                 0           0.005563         0.071534          0.17359           0.2802          0.37727           0.4649   
       6       0.028662               0                0                 0           0.011926         0.080274          0.17999          0.28363          0.38054           0.4665   
       7       0.045213               0                0                 0           0.022095         0.090757          0.18443          0.28523          0.38087           0.4665   
       8        0.06647               0                0         0.0043625           0.033935          0.10076          0.19478          0.29244          0.38621           0.4697   
       9       0.092813      0.00076108        0.0091251          0.017748           0.047979          0.11397           0.2068          0.30084          0.39207          0.47371   
      10        0.12459         0.02539         0.027791          0.036336           0.066347          0.13237          0.21681          0.31124          0.39938          0.47898   
      11        0.16214        0.049062         0.054743          0.057497           0.087289          0.14878          0.23341          0.31965           0.4076          0.48451   
      12        0.20576        0.080353         0.076351          0.084213            0.11115          0.16729          0.24668          0.33295          0.41295          0.48895   
      13        0.25576         0.11036          0.10076           0.11357            0.13677           0.1944          0.26643          0.34326          0.42327          0.49539   
      14        0.31242         0.14798          0.12866           0.14076            0.16483          0.21731          0.28363          0.35886          0.43195          0.50332   
      15        0.37601         0.17839          0.16439           0.16895              0.194          0.24107          0.30164          0.37165          0.44362          0.51204   
      16         0.4468          0.2098          0.20032            0.1988            0.22401          0.26563          0.32575          0.38953           0.4565          0.51772   
      17        0.52503         0.24246          0.23721           0.23371            0.25482          0.29153          0.34564          0.40412          0.46694          0.52773   
      18        0.61095         0.28123          0.27422           0.26803            0.28577          0.31725          0.36526          0.42169          0.48129           0.5355   
      19         0.7048         0.31861          0.30964           0.30224            0.31644          0.34326          0.38636          0.43889          0.49228          0.54649   
      20         0.8068         0.35352          0.34406           0.33561            0.34646          0.37247          0.41168           0.4555          0.50395          0.55454   
      21        0.91719         0.38727          0.37774           0.36766            0.37639          0.40048          0.43289          0.47181          0.51932          0.56671   
      22         1.0362         0.42001          0.40688           0.39888            0.40495          0.42569           0.4541          0.49222          0.53173          0.57495   
      23          1.164          0.4501          0.43289           0.42881            0.43266           0.4501          0.47451          0.50812          0.54573          0.58735   
      24         1.3008         0.47851          0.45746           0.45719            0.45922          0.47371           0.4948          0.52453           0.5608          0.59678   
      25         1.4468         0.50572          0.48514           0.48371            0.48451          0.49652          0.51432          0.54067          0.57335          0.60616   
      26         1.6023         0.53093          0.51118           0.50952            0.50892          0.51852          0.53322          0.56014          0.58575          0.61895   
      27         1.7673         0.55214          0.53571           0.53333            0.53173          0.53973          0.55258          0.57573          0.60152          0.62867   
      28         1.9422         0.57052          0.55854           0.55614            0.55374          0.55981          0.57335          0.59055          0.61336          0.63834   
      29          2.127         0.58782          0.58031           0.57735            0.57415          0.57893          0.59055          0.60534          0.62569          0.65071   
      30         2.3221         0.60768          0.60016           0.59758            0.59375          0.59695          0.60696           0.6194           0.6375          0.65978   
      31         2.5275         0.62577          0.61947           0.61496            0.61226          0.61416          0.62256          0.63297          0.65018          0.66898   
      32         2.7434         0.64351          0.63697           0.63101            0.62956          0.63057           0.6376          0.64617          0.66298          0.68097   
      33           2.97         0.65976          0.65338           0.64537            0.64591          0.64617          0.65178          0.66218          0.67378          0.68986   
      34         3.2075         0.67458          0.66898           0.66058            0.66124          0.66058          0.66498          0.67432          0.68445          0.69819   
      35          3.456         0.68919          0.68379           0.67538            0.67538          0.67458          0.67829          0.68592          0.69419          0.70716   
      36         3.7158          0.7022          0.69739           0.68939            0.68928          0.68779          0.69019          0.69659           0.7042           0.7154   
      37         3.9869          0.7146           0.7098            0.7022            0.70205          0.70039           0.7022           0.7074          0.71372          0.72609   
      38         4.2696         0.72668           0.7218            0.7146             0.7138          0.71209            0.713          0.71779           0.7254          0.73341   
      39          4.564         0.73741          0.73341            0.7262             0.7254          0.72317           0.7234          0.72748          0.73419          0.74121   
      40         4.8702         0.74798          0.74381           0.73711            0.73581          0.73341          0.73341          0.73661          0.74221          0.74821   
      41         5.1884         0.75768          0.75382           0.74727            0.74581          0.74348          0.74301          0.74551          0.75041          0.75542   
      42         5.5188         0.76679           0.7618           0.75684            0.75542          0.75281          0.75217          0.75382          0.75782          0.76232   
      43         5.8615         0.77502          0.76862           0.76542            0.76422          0.76165          0.76022          0.76182          0.76502          0.77102   
      44         6.2166         0.78303          0.77658           0.77422            0.77262          0.76996          0.76862          0.76941          0.77229          0.77742   
      45         6.5844         0.79063          0.78452           0.78223            0.78063          0.77742          0.77633          0.77661          0.77895           0.7835   
      46         6.9649         0.79783          0.79196           0.78983            0.78823          0.78529          0.78356          0.78543          0.78503          0.78903   
      47         7.3583         0.80499          0.79863           0.79695            0.79543          0.79223          0.79043          0.79223          0.79143          0.79463   
      48         7.7647         0.81024          0.80566           0.80343            0.80231          0.79863          0.79699          0.79863          0.79943          0.80023   
      49         8.1844         0.81504          0.81184           0.81003            0.80862          0.80504          0.80329          0.80471          0.80504          0.80547   
      50         8.6173         0.81984          0.81744           0.81584            0.81424          0.81104          0.80997          0.81024          0.81024          0.81024   
      51         9.0637         0.82544          0.82351           0.82144            0.82031          0.81664          0.81584          0.81573          0.81579          0.81733   
      52         9.5237         0.83065          0.82881           0.82664            0.82544          0.82224          0.82224          0.82064          0.82064          0.82191   
      53         9.9975         0.83545          0.83385           0.83217            0.83065          0.82744          0.82704          0.82544          0.82537          0.82624   
      54         10.485         0.84025          0.83863           0.83697            0.83545          0.83225           0.8321          0.83044          0.82985          0.83054   
      55         10.987         0.84494          0.84315           0.84155            0.84023          0.83703          0.83625          0.83465          0.83385          0.83457   
      56         11.502         0.84919          0.84705           0.84585            0.84425          0.84105            0.841          0.83915          0.83785          0.83844   
      57         12.032         0.85319          0.85156           0.85002            0.84785          0.84562          0.84505          0.84322          0.84185          0.84185   
      58         12.577         0.85666          0.85506           0.85396            0.85174          0.84945          0.84865          0.84665          0.84585          0.84574   
      59         13.136         0.86064          0.85906           0.85746            0.85506          0.85338          0.85265          0.85079          0.84945          0.84919   
      60         13.709         0.86386          0.86226           0.86122            0.85826          0.85666          0.85639          0.85425          0.85265          0.85245   
      61         14.298         0.86706          0.86596           0.86461            0.86138          0.86042          0.85978          0.85746          0.85586          0.85562   
      62         14.901         0.87052          0.86906           0.86746            0.86464          0.86372          0.86304          0.86066          0.85906          0.85826   
      63         15.519         0.87306          0.87215           0.87066            0.86746          0.86682          0.86615          0.86386          0.86226          0.86146   
      64         16.152         0.87626          0.87466           0.87378            0.87066          0.86981          0.86906          0.86698          0.86535          0.86464   
      65         16.801         0.87866          0.87779           0.87626             0.8736          0.87226          0.87196          0.86981          0.86812           0.8687   
      66         17.465         0.88163          0.88026           0.87923            0.87626          0.87538          0.87466          0.87226          0.87066          0.87126   
      67         18.144         0.88409          0.88267           0.88179            0.87866          0.87786          0.87706          0.87466          0.87386          0.87375   
      68         18.839         0.88646          0.88507           0.88422            0.88107          0.88026          0.87946           0.8776          0.87706          0.87612   
      69          19.55         0.88867          0.88747           0.88653            0.88347          0.88267          0.88187          0.87997          0.87946          0.87843   
      70         20.277         0.89087          0.88947           0.88867            0.88587          0.88507          0.88427          0.88187          0.88187          0.88026   
      71          21.02         0.89267          0.89187           0.89087            0.88787          0.88736          0.88659          0.88427          0.88419          0.88267   
      72         21.778         0.89493          0.89347           0.89267            0.89027          0.88945          0.88867          0.88656          0.88587          0.88477   
      73         22.553         0.89667          0.89582           0.89487            0.89187          0.89107          0.89027          0.88859          0.88825          0.88667   
      74         23.345         0.89827          0.89747           0.89667            0.89422          0.89336          0.89262          0.89027          0.89016          0.88862   
      75         24.152         0.90034          0.89907           0.89827            0.89587          0.89507          0.89427          0.89237          0.89187          0.89027   
      76         24.977         0.90204          0.90111           0.89987            0.89747          0.89667          0.89587          0.89416          0.89347          0.89187   
      77         25.818         0.90361          0.90274           0.90147            0.89907          0.89827          0.89797          0.89587          0.89507          0.89347   
      78         26.675         0.90515          0.90387           0.90307            0.90067          0.89987          0.89961          0.89747          0.89711          0.89551   
      79          27.55         0.90628          0.90547           0.90467            0.90227          0.90147          0.90117          0.89907          0.89827          0.89667   
      80         28.441         0.90788          0.90708           0.90547            0.90387          0.90307          0.90227          0.90066          0.89987          0.89827   
      81          29.35         0.90908           0.9086           0.90708            0.90547          0.90467          0.90387          0.90213          0.90147          0.89987   
      82         30.276         0.91068          0.90988           0.90825            0.90697          0.90623          0.90547          0.90354          0.90307          0.90146   
      83         31.219         0.91195          0.91121           0.90908            0.90828          0.90758           0.9069          0.90467          0.90444          0.90281   
      84         32.179         0.91308          0.91228           0.91035            0.90958          0.90887          0.90788          0.90623          0.90547          0.90387   
      85         33.157         0.91388          0.91361           0.91148            0.91068          0.90988          0.90908          0.90708          0.90703           0.9054   
      86         34.153         0.91543          0.91468           0.91228            0.91198           0.9113          0.91063          0.90872          0.90788          0.90628   
      87         35.166         0.91628          0.91548            0.9138            0.91308          0.91228          0.91148          0.90988          0.90908           0.9078   
      88         36.198         0.91708          0.91688           0.91468            0.91388          0.91355          0.91291          0.91068          0.91057          0.90893   
      89         37.247         0.91851          0.91786           0.91548            0.91527          0.91463          0.91388          0.91214          0.91148          0.90988   
      90         38.314         0.91946          0.91868           0.91691            0.91628          0.91548          0.91468          0.91308          0.91228          0.91108   
      91         39.399         0.92028          0.91948           0.91788            0.91708          0.91628          0.91604          0.91422          0.91374          0.91228   
      92         40.503         0.92108          0.92028           0.91868            0.91788          0.91761            0.917           0.9154          0.91468          0.91388   
      93         41.625         0.92188          0.92108           0.91948            0.91868          0.91851          0.91788          0.91628          0.91548          0.91527   
      94         42.765         0.92268          0.92188           0.92028            0.92001           0.9194          0.91868          0.91771          0.91628           0.9162   
      95         43.924         0.92348          0.92268           0.92108            0.92085          0.92026          0.91948          0.91868          0.91708          0.91708   
      96         45.102         0.92428          0.92348           0.92188            0.92168          0.92108          0.92028          0.91948          0.91838          0.91788   
      97         46.298         0.92508          0.92414           0.92268            0.92248          0.92188          0.92108          0.92028          0.91931          0.91868   
      98         47.513         0.92588          0.92469           0.92348            0.92325          0.92268          0.92188          0.92168          0.92026          0.92028   
      99         48.747         0.92668          0.92508           0.92428            0.92398          0.92347          0.92268          0.92248          0.92108           0.9218   
     100             50         0.92737           0.9258           0.92508            0.92428           0.9242          0.92348          0.92337          0.92334          0.92348   

1.6.4 Test FF_VFI_AZ_MZOOM_VEC Change Interest Rate and Discount

Show only save fraction of cash on hand:

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 750;
mp_params('it_z_n') = 9;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with several different interest rates and discount factor:

% Lower Savings Incentives
mp_params('fl_beta') = 0.80;
mp_params('fl_r') = 0.01;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 37.005214 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    _______    ________    ___    _______

    savefraccoh    1     1      2      6750     750      9      3468.2    0.5138    0.27192    0.52924      0     0.80103

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c6         c7         c8          c9   
            _______    _______    _______    _______    _______    _______    _______    _______    ________

    r1            0          0          0          0          0          0          0    0.02073    0.065955
    r2            0          0          0          0          0          0          0    0.02073    0.065955
    r3            0          0          0          0          0          0          0    0.02073    0.065955
    r4            0          0          0          0          0          0          0    0.02073    0.065955
    r5            0          0          0          0          0          0          0    0.02073    0.065987
    r746     0.8008    0.79843     0.7959    0.79303    0.78983    0.78663    0.78303    0.77903     0.77502
    r747    0.80092    0.79855    0.79603    0.79303    0.79058    0.78713    0.78362    0.77953     0.77553
    r748    0.80102    0.79863    0.79615     0.7935    0.79063    0.78729    0.78378    0.77972     0.77568
    r749    0.80103    0.79863    0.79623    0.79369    0.79063    0.78743    0.78383    0.77983     0.77582
    r750    0.80103    0.79904    0.79623    0.79378    0.79063    0.78743    0.78383    0.77983     0.77582

% Higher Savings Incentives
mp_params('fl_beta') = 0.95;
mp_params('fl_r') = 0.04;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 159.606266 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    ______    _______    ________    ___    _______

    savefraccoh    1     1      2      6750     750      9      4667.7    0.6915    0.26685     0.3859      0     0.92668

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4          c5         c6         c7         c8         c9   
            _______    _______    _______    _______    ________    _______    _______    _______    _______

    r1            0          0          0          0      0.0647    0.16668    0.27352    0.37327     0.4617
    r2            0          0          0          0      0.0647    0.16668    0.27352    0.37327     0.4617
    r3            0          0          0          0    0.064731    0.16668    0.27352    0.37327     0.4617
    r4            0          0          0          0    0.064731    0.16668    0.27355    0.37327     0.4617
    r5            0          0          0          0    0.064747    0.16671    0.27355    0.37327     0.4617
    r746    0.92657    0.92588    0.92508    0.92428     0.92348    0.92268    0.92235    0.92188    0.92188
    r747    0.92664    0.92588    0.92508    0.92428     0.92402    0.92318    0.92248    0.92188    0.92235
    r748    0.92668    0.92588    0.92508    0.92478     0.92411    0.92328     0.9226    0.92188     0.9226
    r749    0.92668    0.92588    0.92555    0.92488      0.9242     0.9234    0.92268    0.92254    0.92268
    r750    0.92668    0.92588    0.92565    0.92497     0.92427    0.92348    0.92268    0.92268    0.92268

1.6.5 Test FF_VFI_AZ_MZOOM_VEC Changing Risk Aversion

Here, again, show fraction of coh saved in summary tabular form, but also show it graphically.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {'savefraccoh'};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 100;
mp_params('it_z_n') = 7;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Solve the model with different risk aversion levels, higher preferences for risk:

% Lower Risk Aversion
mp_params('fl_crra') = 0.5;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 3.409484 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      452.03    0.64575    0.28029    0.43406      0     0.90354

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3          c4           c5         c6         c7   
            _______    _______    _______    _________    ________    _______    _______

    r1            0          0          0    0.0047077    0.089109      0.198    0.30781
    r2            0          0          0    0.0051079    0.089156      0.198    0.30793
    r3            0          0          0    0.0059631    0.090679     0.1988    0.30848
    r4            0          0          0    0.0079639    0.092358    0.20109    0.30964
    r5            0          0          0     0.011926    0.092758    0.20413    0.31171
    r96     0.90047    0.89907    0.89826      0.89727     0.89587    0.89347    0.89267
    r97     0.90127    0.89987    0.89907      0.89822     0.89727    0.89477    0.89394
    r98     0.90204    0.90067    0.89987      0.89907     0.89822    0.89573    0.89493
    r99     0.90278    0.90147    0.90067      0.89987     0.89907    0.89667    0.89587
    r100    0.90354    0.90227    0.90147      0.90067     0.89987    0.89801    0.89667

When risk aversion increases, at every state-space point, the household wants to save more.

% Higher Risk Aversion
mp_params('fl_crra') = 5;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 4.012888 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum     mean       std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    _____    _____    _______    ________    ___    _______

    savefraccoh    1     1      2       700     100      7      502.6    0.718    0.25437    0.35427      0     0.93587

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2          c3         c4         c5         c6         c7   
            _______    _______    ________    _______    _______    _______    _______

    r1            0          0     0.04674    0.15532    0.27563    0.39047    0.48771
    r2            0          0    0.047493    0.15525    0.27563    0.39101    0.48771
    r3            0          0    0.049541    0.15685    0.27693    0.39127    0.48834
    r4            0          0    0.054343    0.16018    0.27883    0.39287    0.48923
    r5            0          0    0.062848    0.16566    0.28272    0.39528    0.49071
    r96     0.93269    0.93251     0.93189    0.93108    0.93014    0.92988    0.92968
    r97     0.93349    0.93322     0.93269    0.93189    0.93107    0.93104    0.93108
    r98     0.93429    0.93349     0.93347    0.93269    0.93189    0.93189    0.93269
    r99     0.93507    0.93429     0.93424    0.93349    0.93331    0.93349    0.93429
    r100    0.93575    0.93509     0.93507    0.93488    0.93491    0.93509    0.93587

1.6.6 Test FF_VFI_AZ_MZOOM_VEC with Higher Uncertainty

Increase the standard deviation of the Shock.

mp_support = containers.Map('KeyType','char', 'ValueType','any');
mp_support('bl_print_params') = false;
mp_support('bl_print_iterinfo') = false;
mp_support('ls_ffcmd') = {'savefraccoh'};
mp_support('ls_ffsna') = {};
mp_support('ls_ffgrh') = {};
mp_params = containers.Map('KeyType','char', 'ValueType','any');
mp_params('it_a_n') = 150;
mp_params('it_z_n') = 15;
mp_params('fl_a_max') = 50;
mp_params('st_grid_type') = 'grid_powerspace';

Lower standard deviation of shock:

% Lower Risk Aversion
mp_params('fl_shk_std') = 0.10;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 16.599473 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1507.2    0.66985    0.28667    0.42796      0     0.92565

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.13838    0.18479    0.23021    0.27363    0.31725
    r2            0          0          0          0          0    0.13838    0.18479    0.23027    0.27363    0.31725
    r3            0          0          0          0          0    0.13894    0.18526    0.23041    0.27407    0.31725
    r4            0          0          0          0          0    0.13987    0.18606    0.23121    0.27443    0.31805
    r5            0          0          0          0          0    0.13998    0.18719    0.23201    0.27563    0.31885
    r146    0.92348    0.92348    0.92328    0.92268    0.92268    0.92085    0.92028    0.92028    0.91948    0.91931
    r147     0.9242    0.92398    0.92348    0.92348    0.92337    0.92108    0.92108    0.92097    0.92001    0.91948
    r148    0.92428    0.92428    0.92428    0.92408    0.92348    0.92188    0.92171    0.92108    0.92028    0.92028
    r149    0.92508    0.92497    0.92478    0.92428    0.92428    0.92241    0.92188    0.92188    0.92108    0.92107
    r150    0.92565    0.92508    0.92508    0.92507    0.92485    0.92268    0.92268    0.92254    0.92238    0.92188

Higher shock standard deviation: low shock high asset save more, high shock more asset save less, high shock low asset save more:

% Higher Risk Aversion
mp_params('fl_shk_std') = 0.40;
ff_vfi_az_mzoom_vec(mp_params, mp_support);

Elapsed time is 16.323916 seconds.
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CONTAINER NAME: mp_ffcmd ND Array (Matrix etc)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   i    idx    ndim    numel    rowN    colN     sum       mean        std      coefvari    min      max  
                   _    ___    ____    _____    ____    ____    ______    _______    _______    ________    ___    _______

    savefraccoh    1     1      2      2250     150      15     1685.2    0.74898    0.22908    0.30585      0     0.93669

xxx TABLE:savefraccoh xxxxxxxxxxxxxxxxxx
              c1         c2         c3         c4         c5         c11        c12        c13        c14        c15  
            _______    _______    _______    _______    _______    _______    _______    _______    _______    _______

    r1            0          0          0          0          0    0.52613    0.61256    0.68259    0.73901    0.78427
    r2            0          0          0          0          0    0.52613    0.61256    0.68259    0.73901     0.7843
    r3            0          0          0          0          0    0.52613    0.61256    0.68259    0.73901     0.7843
    r4            0          0          0          0          0    0.52682    0.61256    0.68259    0.73901    0.78433
    r5            0          0          0          0          0    0.52693    0.61309    0.68259    0.73901    0.78439
    r146    0.92948    0.92925    0.92828    0.92805    0.92737    0.92263    0.92348    0.92577    0.92901    0.93331
    r147    0.93017    0.92948    0.92868    0.92828    0.92748    0.92348    0.92428    0.92668    0.93002    0.93421
    r148    0.93028    0.93005    0.92948    0.92891    0.92827    0.92428    0.92587    0.92799    0.93101    0.93507
    r149    0.93091    0.93028    0.92948    0.92931    0.92828    0.92574    0.92668    0.92904    0.93189    0.93589
    r150    0.93108    0.93082    0.93027    0.92948    0.92868    0.92668    0.92814    0.93008    0.93269    0.93669