Errors When Running Portfolio-Choice Models

Dear Robert,

I have started using your VFI toolkit again, and it’s great! I am finishing my PhD and decided to use AWS g4dn to run your codes, which require a GPU. My research investigates the interplay between homeownership and stock demand, so I am particularly interested in portfolio-choice models (models 31-35 and the example CGM2005), especially the one with housing (model 35).

I noticed that the codes for models 31-35 and CGM2005 are ending with errors. Could you please take a look and see what might be going wrong? I have pasted the error message below.

Thank you very much!

1 Like

Model 31

Now solve the value function iteration problem, just to check that things are working before we go to General Equilbrium
disp(‘Test ValueFnIter’)
tic;
[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);
toc

% V is now (a,z,j). This was already true, just that previously z was trivial (a single point)
% Compare
size(V)
% with
[n_a,n_z,N_j]
% there are the same.
% Policy is
size(Policy)
% which is the same as
[length(n_d),n_a,n_z,N_j]
% The n_a,n_z,N_j represent the state on which the decisions/policys
% depend, and there is one decision for each decision variable ‘d’
Test ValueFnIter
Unrecognized function or variable ‘N_a2’.

Error in CreateaprimeFnMatrix_RiskyAsset (line 144)
if N_dN_uN_a2<1000000

Error in ValueFnIter_Case1_FHorz_RiskyAsset_noa1_raw (line 213)
[aprimeIndex,aprimeProbs]=CreateaprimeFnMatrix_RiskyAsset(aprimeFn, n_d, n_a, n_u, d_grid, a_grid, u_grid, aprimeFnParamsVec,1); % Note, is actually aprime_grid (but a_grid is anyway same for all ages)

Error in ValueFnIter_Case1_FHorz_RiskyAsset (line 83)
[VKron, PolicyKron]=ValueFnIter_Case1_FHorz_RiskyAsset_noa1_raw(n_d,n_a2,n_z,n_u, N_j, d_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 571)
[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);

Model 32

Again at the same place:

Unrecognized function or variable ‘N_a2’.

Model 33

%% Now solve the value function iteration problem, just to check that things are working before we go to General Equilbrium

disp(‘Test ValueFnIter’)

tic;

[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);

toc

% V is now (a,z,j). This was already true, just that previously z was trivial (a single point)

% Compare

size(V)

% with

[n_a,n_z,N_j]

% there are the same.

% Policy is

size(Policy)

% which is the same as

[length(n_d),n_a,n_z,N_j]

% The n_a,n_z,N_j represent the state on which the decisions/policys

% depend, and there is one decision for each decision variable ‘d’

Test ValueFnIter

Unrecognized function or variable ‘l_a’.

Error in ValueFnIter_Case1_FHorz (line 406)

vfoptions.WarmGlowBequestsFnParamsNames={temp{l_a+1:end}}; % the first inputs will always be aprime

Model 34

Now solve the value function iteration problem, just to check that things are working before we go to General Equilbrium
disp(‘Test ValueFnIter’)
tic;
[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);
toc

% V is now (a,z,j). This was already true, just that previously z was trivial (a single point)
% Compare
size(V)
% with
[n_a,n_z,N_j]
% there are the same.
% Policy is
size(Policy)
% which is the same as
[length(n_d),n_a,n_z,N_j]
% The n_a,n_z,N_j represent the state on which the decisions/policys
% depend, and there is one decision for each decision variable ‘d’
Test ValueFnIter
Error using .*
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 ValueFnIter_FHorz_RiskyAsset_EpsteinZin_noa1_raw (line 95)
becareful=logical(isfinite(ReturnMatrix).*(ReturnMatrix~=0)); % finite and not zero

Error in ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset (line 165)
[VKron, PolicyKron]=ValueFnIter_FHorz_RiskyAsset_EpsteinZin_noa1_raw(n_d,n_a2,n_z,n_u, N_j, d_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 571)
[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);

Model 35 (similar to 31 and 32)

Now solve the value function iteration problem, just to check that things are working before we go to General Equilbrium
disp(‘Test ValueFnIter’)
vfoptions.verbose=1;
tic;
[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);
toc

% V is now (a,z,j). This was already true, just that previously z was trivial (a single point)
% Compare
size(V)
% with
[n_a,n_z,N_j]
% there are the same.
% Policy is
size(Policy)
% which is the same as
[length(n_d)+1,n_a,n_z,N_j]
% The n_a,n_z,N_j represent the state on which the decisions/policys
% depend, and there is one decision for each decision variable ‘d’ plus one
% more for the standard asset
Test ValueFnIter

vfoptions =

struct with fields:

    survivalprobability: 'sj'
             riskyasset: 1
      exoticpreferences: 'EpsteinZin'
      EZpositiveutility: 0
         EZriskaversion: 'phi'
               aprimeFn: @(savings,riskyshare,u,r)LifeCycleModel31_aprimeFn(savings,riskyshare,u,r)
                    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: @(savings,riskyshare,u,r)LifeCycleModel31_aprimeFn(savings,riskyshare,u,r)
                    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
Unrecognized function or variable ‘N_a2’.

Error in CreateaprimeFnMatrix_RiskyAsset (line 144)
if N_dN_uN_a2<1000000

Error in ValueFnIter_FHorz_RiskyAsset_EpsteinZin_raw (line 418)
[a2primeIndex,a2primeProbs]=CreateaprimeFnMatrix_RiskyAsset(aprimeFn, n_d, n_a2, n_u, d_grid, a2_grid, u_grid, aprimeFnParamsVec,2); % Note, is actually aprime_grid (but a_grid is anyway same for all ages)

Error in ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset (line 171)
[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 571)
[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);

CMG2005

%% Solve for the value function and policy fn
% vfoptions.lowmemory=1;
vfoptions.verbose=1;
tic;
[V, Policy]=ValueFnIter_Case1_FHorz_PType(n_d,n_a,n_z,N_j,Names_i, d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,vfoptions);
toc
Permanent type: 1 of 3
Error using ValueFnIter_Case1_FHorz (line 165)
No longer use vfoptions.e_grid_J, instead just put the age-dependent grid in vfoptions.e_grid (functionality of VFI Toolkit has
changed to make it easier to use)

Error in ValueFnIter_Case1_FHorz_PType (line 145)
[V_ii, Policy_ii]=ValueFnIter_Case1_FHorz(n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp, a_grid_temp, z_grid_temp, pi_z_temp, ReturnFn_temp, Parameters_temp, DiscountFactorParamNames_temp, , vfoptions_temp);

I will take a look later this week and get back to you.

1 Like

@bob_rb, I think they are all fixed (update both VFI Toolkit and the Intro to Life-Cycle models to the latest github). Thanks for bringing to my attention.

I had removed the “Case3” codes (which is what “riskyasset” was called before I decided that “riskyasset” was a better approach) and there were still some vestiges of it in use that error-ed as a result.

1 Like

That AWS instance is pretty nice. The T4 is a solid gpu. I see AWS actually let you have 8 T4’s in a single instance. Unfortunately VFI Toolkit is not yet at the point it can exploit multiple GPUs but in principle I think Matlab now can and it would be really nice for solving multiple permanent types across multiple GPUs all at once. Something I want to try if I ever get hold of a setup with multiple GPUs.

2 Likes

Thank you!

Models 31, 32, 33, and 35 now work smoothly for me, and I will start studying them now. However, the errors for Model 34 and CGM2005 still persist.

Model 34

From what I have read, there seems to be a memory issue on my device.


Test ValueFnIter
Error using .*
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 ValueFnIter_FHorz_RiskyAsset_EpsteinZin_noa1_raw (line 95)
becareful=logical(isfinite(ReturnMatrix).*(ReturnMatrix~=0)); % finite and not zero

Error in ValueFnIter_Case1_FHorz_EpsteinZin_RiskyAsset (line 165)
[VKron, PolicyKron]=ValueFnIter_FHorz_RiskyAsset_EpsteinZin_noa1_raw(n_d,n_a2,n_z,n_u, N_j, d_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 572)
[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 LifeCycleModel34 (line 140)
[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 am using an AWS g4dn.xlarge instance with the following specifications:

I do not know where is the problem. Should I use stronger instance?

Model CGM2005

It runs for a few minutes, and then I receive the following message:


CoccoGomesMaenhout2005
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
In CoccoGomesMaenhout2005 (line 100)

Warning: Colon operands must be real scalars. This warning will become an error in a future release.

In CoccoGomesMaenhout2005 (line 103)

Warning: Colon operands must be real scalars. This warning will become an error in a future release.

In CoccoGomesMaenhout2005 (line 106)

discretizeLifeCycleAR1_Kirkby: 1 moment in 0.00 cases, 2 moments in 0.13 cases, 3 moments in 0.03 cases, 4 moments in 0.84 cases (target was 4 moments)
discretizeLifeCycleAR1_Kirkby: 1 moment in 0.00 cases, 2 moments in 0.12 cases, 3 moments in 0.03 cases, 4 moments in 0.85 cases (target was 4 moments)
discretizeLifeCycleAR1_Kirkby: 1 moment in 0.00 cases, 2 moments in 0.12 cases, 3 moments in 0.03 cases, 4 moments in 0.85 cases (target was 4 moments)
Permanent type: 1 of 3
Error using ValueFnIter_Case1_FHorz (line 165)
No longer use vfoptions.e_grid_J, instead just put the age-dependent grid in vfoptions.e_grid (functionality of VFI Toolkit has
changed to make it easier to use)

Error in ValueFnIter_Case1_FHorz_PType (line 145)
[V_ii, Policy_ii]=ValueFnIter_Case1_FHorz(n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp, a_grid_temp, z_grid_temp, pi_z_temp, ReturnFn_temp, Parameters_temp, DiscountFactorParamNames_temp, , vfoptions_temp);

Error in CoccoGomesMaenhout2005 (line 292)
[V, Policy]=ValueFnIter_Case1_FHorz_PType(n_d,n_a,n_z,N_j,Names_i, d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,vfoptions);

Yes, so far, it seems to work very well. I am using an AWS g4dn.xlarge instance with the following specifications:


gpuDevice()

ans =

CUDADevice with properties:

                  Name: 'Tesla T4'
                 Index: 1
     ComputeCapability: '7.5'
        SupportsDouble: 1
 GraphicsDriverVersion: '560.94'
           DriverModel: 'TCC'
        ToolkitVersion: 12.2000
    MaxThreadsPerBlock: 1024
      MaxShmemPerBlock: 49152 (49.15 KB)
    MaxThreadBlockSize: [1024 1024 64]
           MaxGridSize: [2.1475e+09 65535 65535]
             SIMDWidth: 32
           TotalMemory: 15928852480 (15.93 GB)
       AvailableMemory: 15793710766 (15.79 GB)
           CachePolicy: 'balanced'
   MultiprocessorCount: 40
          ClockRateKHz: 1590000
           ComputeMode: 'Default'
  GPUOverlapsTransfers: 1
KernelExecutionTimeout: 0
      CanMapHostMemory: 1
       DeviceSupported: 1
       DeviceAvailable: 1
        DeviceSelected: 1

This is the weakest GPU solution available on AWS. I recommend it to those who want to run your codes with the VFI toolkit but lack a GPU. Although this solution is not free, it is reasonably priced if you need it.

1 Like

Is the CGM2005 code you ran from

(just making sure I check the right code. if not, where did you get the code?)

Yeah, that is a GPU out of memory error in life-cycle model 34 (you can make the grids smaller just editing n_d and n_a to be smaller numbers; should run then but obviously won’t be so accurate).

1 Like

Yes, it is the code in your link. I run it now again and get the same error.


clear
CoccoGomesMaenhout2005
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
In CoccoGomesMaenhout2005 (line 100)

Warning: Colon operands must be real scalars. This warning will become an error in a future release.

In CoccoGomesMaenhout2005 (line 103)

Warning: Colon operands must be real scalars. This warning will become an error in a future release.

In CoccoGomesMaenhout2005 (line 106)

discretizeLifeCycleAR1_Kirkby: 1 moment in 0.00 cases, 2 moments in 0.13 cases, 3 moments in 0.03 cases, 4 moments in 0.84 cases (target was 4 moments)
discretizeLifeCycleAR1_Kirkby: 1 moment in 0.00 cases, 2 moments in 0.12 cases, 3 moments in 0.03 cases, 4 moments in 0.85 cases (target was 4 moments)
discretizeLifeCycleAR1_Kirkby: 1 moment in 0.00 cases, 2 moments in 0.12 cases, 3 moments in 0.03 cases, 4 moments in 0.85 cases (target was 4 moments)
Permanent type: 1 of 3
Error using ValueFnIter_Case1_FHorz (line 165)
No longer use vfoptions.e_grid_J, instead just put the age-dependent grid in vfoptions.e_grid (functionality of VFI Toolkit has
changed to make it easier to use)

Error in ValueFnIter_Case1_FHorz_PType (line 145)
[V_ii, Policy_ii]=ValueFnIter_Case1_FHorz(n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp, a_grid_temp, z_grid_temp, pi_z_temp, ReturnFn_temp, Parameters_temp, DiscountFactorParamNames_temp, , vfoptions_temp);

Error in CoccoGomesMaenhout2005 (line 292)
[V, Policy]=ValueFnIter_Case1_FHorz_PType(n_d,n_a,n_z,N_j,Names_i, d_grid, a_grid, z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,vfoptions);

I will fix CGM2005 sometime tomorrow (likely) and let you know once done.

1 Like

@bob_rb CGM2005 should work now.

1 Like

Thank you, but unfortunately, I receive the following error after running the updated CGM2005 code:

CoccoGomesMaenhout2005
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
In CoccoGomesMaenhout2005 (line 100)

Warning: Colon operands must be real scalars. This warning will become an error in a future release.

In CoccoGomesMaenhout2005 (line 103)

Warning: Colon operands must be real scalars. This warning will become an error in a future release.

In CoccoGomesMaenhout2005 (line 106)

Unrecognized function or variable ‘discretizeLifeCycleAR1_KFTT’.

Error in CoccoGomesMaenhout2005 (line 163)
[z_grid_J.College, pi_z_J.College,jequaloneDistP,otheroutputs] = discretizeLifeCycleAR1_KFTT(zeros(1,N_j.College),Params.rho_zones(1,N_j.College),Params.sigma_epsilonz.Collegeones(1,N_j.College),n_z,N_j.College,kirkbyoptions);

Ah okay, apparently that “Colon operands” warning is something to do with the latest Matlab version. I will update and see sometime in next few weeks.

“Unrecognized function or variable ‘discretizeLifeCycleAR1_KFTT’.”
This error is probably that you need to update VFI Toolkit to latest github version.

2 Likes

OK, I will be able to try the code on the updated VFI Toolkit tomorrow morning.