LifeCycleModel16 - Error with discretizeAR1_FarmerToda

I tried to run LifeCycleModel16 but got the following error message, when the script calls the function discretizeAR1_FarmerToda

Warning: Failed to find a solution from provided initial guess. Trying new initial guess.

In discreteApproximation (line 77)
In discretizeAR1_FarmerToda (line 145)
In LifeCycleModel16 (line 124)
Error using fminunc
Options Algorithm = ‘trust-region’ and SpecifyObjectiveGradient = false conflict.

Error in discreteApproximation (line 78)
lambdaBar = fminunc(@(lambda) entropyObjective(lambda,Tx,TBar,q),zeros(size(lambda0)),options);

Error in discretizeAR1_FarmerToda (line 145)
[p,lambda,momentError] = discreteApproximation(z_grid,@(x) [(x-condMean)./scalingFactor;…

Error in LifeCycleModel16 (line 124)
[z_grid,pi_z]=discretizeAR1_FarmerToda(0,Params.rho_z,Params.sigma_epsilon_z,n_z);

I am using Matlab R2023b

Hmm, finally got around to this but it does not error on my computer so not sure what the cause is :slightly_frowning_face: (I double checked that both the model and discretizeAR1_FarmerToda are fully synced).

I am in R2023a, so it is possible it is a version thing, although seems unlikely. I might take another look next time I update Matlab sometime next year. Otherwise I guess as a workaround you can always swap discretizeAR1_FarmerToda() for discretizeAR1_Tauchen() or discretizeAR1_Rouwenhorst()

2 Likes

Thanks! I suspect Matlab changed something in fminunc solver…

Unfortunately, changing to discretizeAR1_Tauchen() or discretizeAR1_Rouwenhorst() will give an error as well. So Matlab 2023b is not compatible with most of the examples. I tried the housing example, and also got the same error.

1 Like

Thanks both for letting me know. Updated my Matlab to R2023b so I could debug and now I think this is fixed [you will need to update to latest VFI toolkit on github].

I did not get any error switching to Tauchen and Rouwenhorst. Please let me know if you still do so I can look into it (Tauchen does not use fminunc so shouldn’t give an error related to it).

[Details of what was wrong and how it was fixed: When you use ‘trust-region’ algorithm for Matlab fminunc() you need to specify the gradients of the objective function. This used to just work if you supply the gradients without explicitly setting the option to say that the gradients were being supplied, but now for R2023b I just had to add ‘GradObj’,‘on’ (error msg was only half useful as it set you need to set ‘SpecifyObjectiveGradient’,true, which is correct if you set options using optimoptions, but same thing is called ‘GradObj’,‘on’ if you use optimset, which is what code deep inside the toolkit was using).]

1 Like

Glad to help! Unfortunately Matlab keeps on changing the settings of the options for numerical solvers like fminunc, fmincon, fsolve, etc
Sometimes the new features are not backward compatible, so if a coauthor has an older version of Matlab it becomes a problem (personal experience!)

As someone who regularly breaks backward compatibility I have no ground for complaint :stuck_out_tongue_closed_eyes:

1 Like

I am now getting the following error when running this example. Can you look into it? Thank you.
Error using fieldnames
Invalid input argument of type ‘double’. Input must be a structure or a Java or COM object.
Error in CreateVectorFromParams (line 18)
FullParamNames=fieldnames(Parameters);
Error in SimPanelValues_FHorz_Case1 (line 344)
ValuesFnParamsVec=CreateVectorFromParams(Parameters,FnsToEvaluateParamNames(vv).Names,jj);
Error in LifeCycleModel16 (line 220)
SimPanelValues=SimPanelValues_FHorz_Case1(InitialDist,Policy,FnsToEvaluate,,Params,n_d,n_a,n_z,N_j,d_grid,a_grid,z_grid,pi_z, simoptions);

Oops my mistake, sorry, now fixed (update copy of Intro to Life-Cycle Models from github)

[the order of inputs to SimPanelValues_FHorz_Case1() was incorrect]

Thanks a lot, Robert, and it works now. When running, it keep saying “zFAILED TO FIND PARAMETER”. Is it a normal situation to expect? Thanks.

I just ran Life-Cycle Model 16 and did not get that error. [My guess would be that you need to ‘clear all’ before running it, and error was because you already had a FnsToEvaluate in the workspace.]

When you run codes VFI Toolkit figures out which inputs are parameters, and tries to find them in the parameter structure (called ‘Params’ in all my codes). So, e.g., if the FnsToEvaluate.Income has inputs (h,aprime,a,z1,z2,phi,theta), then the toolkit looks at n_d, n_a, and n_z to figure out how many of these inputs are the state space (imagine n_d=21, n_a=101, and n_z=[5,5], then the first input should be d, the second will be aprime, the third will be a, the fourth and fifth should be the two z’s) and then the inputs after these are assumed to be parameters (so here the sixth and seventh should be parameters, that is phi and theta).

The error “z FAILED TO FIND PARAMETER” is telling you that VFI Toolkit read the function inputs, and one of the inputs is called “z”, and that it looked in the parameter structure but didn’t find this parameter.

The cause of the error is typically one of two things: (i) you forgot to create that parameter in the structure, (ii) VFI Toolkit has identified z as a parameter when it is part of the state space, the reason will either be that you have the first few inputs to one of the FnsToEvaluate wrong (or, while unlikely, it could be that you have one of n_d, n_a, n_z incorrect).

When I see this error, I first look at Params to make sure the parameter is there, and then I look at the ordering of inputs to the FnsToEvaluate (or the ReturnFn, etc., depends what you were doing when the error occured).

[Note: in your case, because z is the name of the exogenous shock, the cause of the error is going to be (ii), that the inputs to the FnsToEvaluate do not match the size of the state space.]

1 Like

You are right! When I clear all the variables, the messages go away. Thank you for the detailed explanation.