Error message of Life Cycle Model 35

Hi Robert, I try to run the LifeCycleModel35.m but got the following error:

Test ValueFnIter
vfoptions =
struct with fields:

    survivalprobability: 'sj'
             riskyasset: 1
      exoticpreferences: 'EpsteinZin'
      EZpositiveutility: 0
         EZriskaversion: 'phi'
               aprimeFn: [function_handle]
                    n_u: 5
                 u_grid: [5×1 gpuArray]
                   pi_u: [5×1 gpuArray]
                verbose: 1
               parallel: 2
           returnmatrix: 2
              lowmemory: 0
               paroverz: 1
       divideandconquer: 0
        incrementaltype: 0
            polindorval: 1
             outputkron: 0
policy_forceintegertype: 0
                dynasty: 0
        experienceasset: 0
       experienceassetu: 0
          residualasset: 0

vfoptions =
struct with fields:

    survivalprobability: 'sj'
             riskyasset: 1
      exoticpreferences: 'EpsteinZin'
      EZpositiveutility: 0
         EZriskaversion: 'phi'
               aprimeFn: [function_handle]
                    n_u: 5
                 u_grid: [5×1 gpuArray]
                   pi_u: [5×1 gpuArray]
                verbose: 1
               parallel: 2
           returnmatrix: 2
              lowmemory: 0
               paroverz: 1
       divideandconquer: 0
        incrementaltype: 0
            polindorval: 1
             outputkron: 0
policy_forceintegertype: 0
                dynasty: 0
        experienceasset: 0
       experienceassetu: 0
          residualasset: 0

Finite horizon: 80 of 81

Error using +
Arrays have incompatible sizes for this operation.

Error in ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw (line 447)
EV1=EV(aprimeIndex+N_a*((1:1:N_z)-1)); % (d,u,z), the lower aprime

Error in ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset (line 170)
[VKron, PolicyKron]=ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw(n_d,n_a1,n_a2,n_z,n_u, N_j, d_grid, a1_grid,a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions, sj, warmglow, ezc1,ezc2,ezc3,ezc4,ezc5,ezc6,ezc7,ezc8);

Error in ValueFnIter_Case1_FHorz_RiskyAsset (line 54)
[V,Policy]=ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset(n_d,n_a1,n_a2,n_z,n_u,N_j,d_grid,a1_grid, a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, aprimeFn, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions);

Error in ValueFnIter_Case1_FHorz (line 560)
[V,Policy]=ValueFnIter_Case1_FHorz_RiskyAsset(n_d,n_a1,n_a2,n_z,vfoptions.n_u, N_j, d_grid, a1_grid, a2_grid, z_gridvals_J, vfoptions.u_grid, pi_z_J, vfoptions.pi_u, ReturnFn, vfoptions.aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);

Error in LifeCycleModel35 (line 173)
[V, Policy]=ValueFnIter_Case1_FHorz(n_d,n_a,n_z,N_j,d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);

I didn’t change any parameters’ value, not sure which part goes wrong.

1 Like

Hi @Shiki, I also ran Model 35 and got exactly the same mistake.

Should be fixed now (update latest version of toolkit from github). Thanks for letting me know.

PS. I cleaned up a comment in the example itself, so you might want to update that too (will run either way, comment is just clearer now; comment referred to old notation that is no longer used)

1 Like

Now the code of example 35 runs successfully (and is also fast!). However the policy functions for savings and riskyshare have quite a strange behavior, see attached figure. I was wondering if this is just a result of the coarse discretization (i.e. not enough grid points) or there is something else going on.
Moreover, it seems that savings can be negative at age j=81 but there is a constraint savings>=0 when the agent is retired.

%% Ban pensioners from negative assets
if agej>=Jr && savings<0
F=-Inf;
end

In any case this example is great. The toolkit is close to being able to replicate very well-known papers with portfolio choice and housing in the finance/household finance literature, such as Cocco (2005) or Li et al. (2016)

My guess is that is correct and is just a stupid parametrization (zero thought has been put into the parametrization). Housing only takes five values and there seem to be five ‘humps’ in savings, suggesting that agents just really like housing; they save some assets, and then as soon as they have enough they cash them all in for housing. The risky share is 1 when there are savings, and zero otherwise, suggesting agents love taking risks.

If you set the return function to have same contents as Life Cycle Model 31 (same model but without housing) then it gives same answers that does. And if you put something crazy like “if rem(h,agej)>0 F=-Inf; end” in the return function you get a sawtooth for housing. But I have not done substantial testing. Ideally one would replicate a paper with housing and portfolio-choice, but this is not currently something I intend to do (I am going to turn to two assets models, hopefully later this year, but before then I am not looking at this).

1 Like

Agree, thanks for your answer!
P.S. Regarding two assets models, the toolkit should be able to habdle them already, correct? Two endogenous state variables a1 and a2 with either infinite horizon or finite horizon. I guess probably they will be a bit slow. Even though the distribution with the Tan improvement should be ok.

In principle it does four endogenous state variables, but in practice more than one is too slow to be useful so not really documented anywhere.

I’m hoping to try implement the divide-and-conquer algorithm for two endogenous states later this year, I think/hope that will make them nice and usable.

1 Like

Just pushed a minor improvement to riskyasset when there is another endogenous state.

(Interpolation onto grid suffered from numerical precision rounding errors when the two end-points had same value, I now detect and treat separately the interpolations where the end points are both equal. I don’t think this will really change anything, but can improve the value/policy in retirement when the value fn was independent of some states, not relevant to this riskyasset example, but often was in human capital models.)

1 Like

Hi Robert, thanks for the updates.
I have another question. When I ran codes for some life-cycle or OLG model examples, I got error message about out of memory. For instance, after I run OLGModel6.m,

Error using gpuArray/arrayfun
Out of memory on device. To view more detail about available memory on the GPU, use
‘gpuDevice()’. If the problem persists, reset the GPU by calling ‘gpuDevice(1)’.
Error in CreateReturnFnMatrix_Case1_Disc_Par2e (line 179)
Fmatrix=arrayfun(ReturnFn, d1vals, aprime1vals, a1vals, z1vals, e1vals, ParamCell{:});
Error in ValueFnIter_Case1_FHorz_e_raw (line 164)
ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2e(ReturnFn, n_d, n_a, n_z, n_e, d_grid, a_grid, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec);
Error in ValueFnIter_Case1_FHorz (line 832)
[VKron, PolicyKron]=ValueFnIter_Case1_FHorz_e_raw(n_d,n_a,n_z, vfoptions.n_e, N_j, d_grid, a_grid, z_gridvals_J, vfoptions.e_gridvals_J, pi_z_J, vfoptions.pi_e_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);
Error in OLGModel6 (line 137)
[V, Policy]=ValueFnIter_Case1_FHorz(n_d,n_a,n_z,N_j, d_grid, a_grid, z_grid_J, pi_z_J, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);

and here are my GPU details:

CUDADevice with properties:

                  Name: 'NVIDIA GeForce RTX 3050 Ti Laptop GPU'
                 Index: 1
     ComputeCapability: '8.6'
        SupportsDouble: 1
 GraphicsDriverVersion: '528.92'
           DriverModel: 'WDDM'
        ToolkitVersion: 11.8000
    MaxThreadsPerBlock: 1024
      MaxShmemPerBlock: 49152 (49.15 KB)
    MaxThreadBlockSize: [1024 1024 64]
           MaxGridSize: [2.1475e+09 65535 65535]
             SIMDWidth: 32
           TotalMemory: 4294443008 (4.29 GB)
       AvailableMemory: 3266494339 (3.27 GB)
           CachePolicy: 'balanced'
   MultiprocessorCount: 20
          ClockRateKHz: 1485000
           ComputeMode: 'Default'
  GPUOverlapsTransfers: 1
KernelExecutionTimeout: 1
      CanMapHostMemory: 1
       DeviceSupported: 1
       DeviceAvailable: 1
        DeviceSelected: 1

Are there any methods that I can use to bypass this problem?

When VFI Toolkit tries to create a matrix this requires memory, and if the matrix is too big you get an out-of-memory error, which is exactly what you are seeing.

Note that it is out of GPU memory (VFI Toolkit depends a lot on creating matrices on GPU). In a computer you have standard CPU memory (e.g. you might have 64gb DDR memory), and separate to this the the GPU memory (e.g., you might have 8gb GDDR memory). I see can see your GPU model from your post, and quick search tells me it has either 6 or 8gb of GPU memory.

There are essentially three solutions, none of which is entirely useful. First, for many models you can set vfoptions.lowmemory=1, this helps by using less memory but is slower (essentially it does more loops with smaller matrices, rather than one big matrix). Second, use smaller grids, that way the matrices created are smaller and use less memory (problem is obviously that the smaller grids will also be less accurate). Third, buy a GPU with more memory (in principle this is an easy solution, but obviously not actually useful as advice).

2 Likes

I see. Thanks for your advise.

I updated the newest version and the error message of model 35 reappears. (It works before.)

Test ValueFnIter

vfoptions =

struct with fields:

    survivalprobability: 'sj'
             riskyasset: 1
      exoticpreferences: 'EpsteinZin'
      EZpositiveutility: 0
         EZriskaversion: 'phi'
               aprimeFn: [function_handle]
                    n_u: 5
                 u_grid: [5×1 gpuArray]
                   pi_u: [5×1 gpuArray]
                verbose: 1
               parallel: 2
           returnmatrix: 2
              lowmemory: 0
               paroverz: 1
       divideandconquer: 0
        incrementaltype: 0
            polindorval: 1
             outputkron: 0
policy_forceintegertype: 0
                dynasty: 0
        experienceasset: 0
       experienceassetu: 0
          residualasset: 0

vfoptions =

struct with fields:

    survivalprobability: 'sj'
             riskyasset: 1
      exoticpreferences: 'EpsteinZin'
      EZpositiveutility: 0
         EZriskaversion: 'phi'
               aprimeFn: [function_handle]
                    n_u: 5
                 u_grid: [5×1 gpuArray]
                   pi_u: [5×1 gpuArray]
                verbose: 1
               parallel: 2
           returnmatrix: 2
              lowmemory: 0
               paroverz: 1
       divideandconquer: 0
        incrementaltype: 0
            polindorval: 1
             outputkron: 0
policy_forceintegertype: 0
                dynasty: 0
        experienceasset: 0
       experienceassetu: 0
          residualasset: 0

Finite horizon: 80 of 81
Error using +
Arrays have incompatible sizes for this operation.
Error in ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw (line 487)
skipinterp=logical(EV(aprimeIndex+N_a*((1:1:N_z)-1))==EV(aprimeplus1Index+N_a*((1:1:N_z)-1))); % Note, probably just do this off of a2prime values
Error in ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset (line 170)
[VKron, PolicyKron]=ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw(n_d,n_a1,n_a2,n_z,n_u, N_j, d_grid, a1_grid,a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions, sj, warmglow, ezc1,ezc2,ezc3,ezc4,ezc5,ezc6,ezc7,ezc8);
Error in ValueFnIter_Case1_FHorz_RiskyAsset (line 54)
[V, Policy]=ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset(n_d,n_a1,n_a2,n_z,n_u,N_j,d_grid,a1_grid, a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, aprimeFn, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions);
Error in ValueFnIter_Case1_FHorz (line 560)
[V,Policy]=ValueFnIter_Case1_FHorz_RiskyAsset(n_d,n_a1,n_a2,n_z,vfoptions.n_u, N_j, d_grid, a1_grid, a2_grid, z_gridvals_J, vfoptions.u_grid, pi_z_J, vfoptions.pi_u, ReturnFn, vfoptions.aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);
Error in LifeCycleModel35 (line 173)
[V, Policy]=ValueFnIter_Case1_FHorz(n_d,n_a,n_z,N_j,d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);

Oops, sorry (it was that minor improvement to the interpolation I implemented).

Fixed now.

2 Likes

Thank you. If I want to add warm-glow of bequests to this model, besides the following codes:

vfoptions.WarmGlowBequestsFn=@(aprime,wg,sigma,agej,J) LifeCycleModel33_WarmGlowBequestsFn(aprime,wg,sigma,agej,J);
Params.wg=1;

are there anything I need to change? because when I add these two lines of codes, error message said index exceeds matrix dimension.

vfoptions =

struct with fields:

          survivalprobability: 'sj'
           WarmGlowBequestsFn: [function_handle]
                   riskyasset: 1
            exoticpreferences: 'EpsteinZin'
            EZpositiveutility: 0
               EZriskaversion: 'phi'
                     aprimeFn: [function_handle]
                          n_u: 5
                       u_grid: [5×1 gpuArray]
                         pi_u: [5×1 gpuArray]
                      verbose: 1
                     parallel: 2
                 returnmatrix: 2
                    lowmemory: 0
                     paroverz: 1
             divideandconquer: 0
              incrementaltype: 0
                  polindorval: 1
                   outputkron: 0
      policy_forceintegertype: 0
                      dynasty: 0
              experienceasset: 0
             experienceassetu: 0
                residualasset: 0
WarmGlowBequestsFnParamsNames: {'sigma'  'agej'  'J'}

Index exceeds matrix dimension.

Error in ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw (line 62)
skipinterp=logical(WGmatrix(aprimeIndex)==WGmatrix(aprimeplus1Index)); % Note, probably just do this off of a2prime values

Error in ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset (line 170)
[VKron, PolicyKron]=ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw(n_d,n_a1,n_a2,n_z,n_u, N_j, d_grid, a1_grid,a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions, sj, warmglow, ezc1,ezc2,ezc3,ezc4,ezc5,ezc6,ezc7,ezc8);

Error in ValueFnIter_Case1_FHorz_RiskyAsset (line 54)
[V, Policy]=ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset(n_d,n_a1,n_a2,n_z,n_u,N_j,d_grid,a1_grid, a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, aprimeFn, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions);

Error in ValueFnIter_Case1_FHorz (line 560)
[V,Policy]=ValueFnIter_Case1_FHorz_RiskyAsset(n_d,n_a1,n_a2,n_z,vfoptions.n_u, N_j, d_grid, a1_grid, a2_grid, z_gridvals_J, vfoptions.u_grid, pi_z_J, vfoptions.pi_u, ReturnFn, vfoptions.aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);

Error in LifeCycleModel35 (line 173)
[V, Policy]=ValueFnIter_Case1_FHorz(n_d,n_a,n_z,N_j,d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);

1 Like

Hmm, I haven’t actually implemented warm-glow with two assets yet. I will do this but pretty busy with teaching just now so will likely take me about 3-4 weeks.

1 Like

I have another question. (sorry keep posting questions, I recently read some housing portfolio papers, so I’m interested how life cycle model works for housing). Model 35 uses Epstein-Zin preferences. Is it possible to run the codes for ordinary CES utility function?

After I removed these lines of codes:

vfoptions.exoticpreferences=‘EpsteinZin’;
vfoptions.EZpositiveutility=0;
vfoptions.EZriskaversion=‘phi’;
vfoptions.survivalprobability=‘sj’;
DiscountFactorParamNames={‘beta’};

and added back

DiscountFactorParamNames={‘beta’,‘sj’};

Other things stay the same, and an error occurs:

Error using +
Arrays have incompatible sizes for this operation.
Error in ValueFnIter_Case1_FHorz_RiskyAsset_raw (line 271)
skipinterp=logical(EV(aprimeIndex+N_a*((1:1:N_z)-1))==EV(aprimeplus1Index+N_a*((1:1:N_z)-1))); % Note, probably just do this off of a2prime values
Error in ValueFnIter_Case1_FHorz_RiskyAsset (line 90)
[VKron, PolicyKron]=ValueFnIter_Case1_FHorz_RiskyAsset_raw(n_d,n_a1,n_a2,n_z,n_u, N_j, d_grid, a1_grid, a2_grid, z_gridvals_J, u_grid, pi_z_J, pi_u, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions);
Error in ValueFnIter_Case1_FHorz (line 560)
[V,Policy]=ValueFnIter_Case1_FHorz_RiskyAsset(n_d,n_a1,n_a2,n_z,vfoptions.n_u, N_j, d_grid, a1_grid, a2_grid, z_gridvals_J, vfoptions.u_grid, pi_z_J, vfoptions.pi_u, ReturnFn, vfoptions.aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);
Error in untitled2 (line 173)
[V, Policy]=ValueFnIter_Case1_FHorz(n_d,n_a,n_z,N_j,d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);

1 Like

Related to model 35: I was looking at the documentation of “Intro to OLG models” and I noticed that the doc stops at model 30 and does not cover anymore the models with portfolio choice and risky assets.

I was looking at the documentation of “Intro to OLG models” and I noticed that the doc stops at model 30 and does not cover anymore the models with portfolio choice and risky assets.

Where was this? Just checked github and the pdf in the Intro to Life-Cycle Models is fine there.

1 Like

The changes you describe are exactly the correct ones to make this change (in terms of the lines you remove, and how you change discount factors). Should work now (just made minor push to github updating VFI Toolkit). [Was exact same issue as in the EZ preferences when you asked at start of this thread]

Just a note, the way to handle warm-glow of bequests with and without EZ preferences is quite different. [EZ has dedicated vfoptions for warm glow, when not using EZ you can just implement them via the return function]

[I still need to set up the warm-glow for EZ preferences with housing, that is a trickier bit of code, is probably two weeks away :slight_smile: ]

1 Like