# Bug in infinite horizon codes after improvement?

**URL:** <https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292>\
**Category:** Uncategorized\
**Created:** [July 4, 2024, 1:41pm UTC](https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292 "2024-07-04T13:41:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aledinola](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aledinola](https://discourse.vfitoolkit.com/u/aledinola)\
**Post date:** [July 4, 2024, 1:41pm UTC](https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292/1 "2024-07-04T13:41:04Z")

</div>

Dear Robert,

I ran an infinite horizon model (see my repo [GitHub - aledinola/PijoanMasTaxes: Aiyagari model with endogenous labor (as in Pijoan-Mas 2006), with VFI toolkit · GitHub](https://github.com/aledinola/PijoanMasTaxes)) and the default options are good. Thanks for improving the code!  
However, if I set `vfoptions.lowmemory = 1;`, I get the following error message:

```auto
Unrecognized function or variable 'ReturnFnParams'.

Error in ValueFnIter_Case1_Refine (line 43)
        ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn,n_d, n_a, n_z_temp,d_grid, a_grid, zvals,ReturnFnParams,1); % the 1 at the end is to output for refine

Error in ValueFnIter_Case1 (line 622)
    [VKron,Policy]=ValueFnIter_Case1_Refine(V0,n_d,n_a,n_z,d_grid,a_grid,z_grid,pi_z,ReturnFn,ReturnFnParamsVec,DiscountFactorParamsVec,vfoptions);

Error in HeteroAgentStationaryEqm_Case1_subfn (line 8)
[~,Policy]=ValueFnIter_Case1(n_d,n_a,n_s,d_grid,a_grid,s_grid, pi_s, ReturnFn, Parameters, DiscountFactorParamNames,ReturnFnParamNames,vfoptions);

Error in HeteroAgentStationaryEqm_Case1>@(p)HeteroAgentStationaryEqm_Case1_subfn(p,n_d,n_a,n_z,l_p,pi_z,d_grid,a_grid,z_grid,ReturnFn,FnsToEvaluate,GeneralEqmEqns,Parameters,DiscountFactorParamNames,ReturnFnParamNames,FnsToEvaluateParamNames,GeneralEqmEqnParamNames,GEPriceParamNames,heteroagentoptions,simoptions,vfoptions) (line 197)
    GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_subfn(p, n_d, n_a, n_z, l_p, pi_z, d_grid, a_grid, z_grid, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, GeneralEqmEqnParamNames, GEPriceParamNames, heteroagentoptions, simoptions, vfoptions)

Error in fminsearch (line 216)
fv(:,1) = funfcn(x,varargin{:});

Error in HeteroAgentStationaryEqm_Case1 (line 216)
    [p_eqm_vec,GeneralEqmConditions]=fminsearch(GeneralEqmConditionsFnOpt,p0,optimoptions);

```

The code does not have the variable `ReturnFnParams` which is a cell array of characters with the names of the parameters in the ReturnFn. It used to be an input set manually but now the toolkit should do it automatically, I think.

Unrelated to the bug: is the **divide and conquer** method implemented also for infinite horizon models?

---

<div class="post-metadata">

**Author:** ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)\
**Post date:** [July 4, 2024, 10:48pm UTC](https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292/2 "2024-07-04T22:48:32Z")

</div>

Fixed.

Was not actually recent, has been there all along just no-one tried `vfoptions.lowmemory=1` (was just that it should have said ReturnFnParamsVec instead of ReturnFnParams).

---

<div class="post-metadata">

**Author:** ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)\
**Post date:** [July 4, 2024, 10:49pm UTC](https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292/3 "2024-07-04T22:49:08Z")

</div>

Divide-and-conquer is **not** implemented for infinite horizon (partly as I am not sure it would work that well)

---

<div class="post-metadata">

**Author:** ![aledinola](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aledinola](https://discourse.vfitoolkit.com/u/aledinola)\
**Post date:** [July 5, 2024, 12:41pm UTC](https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292/4 "2024-07-05T12:41:54Z")

</div>

Great, thanks! Pijoan-Mas is fixed. I ran the Aiyagari example and I found the same (?) problem with `lowmemory=1`. My codes are available here: [GitHub - aledinola/Aiyagari-with-taxes: Aiyagari model with progressive income taxation · GitHub](https://github.com/aledinola/Aiyagari-with-taxes)

I guess the difference is that my Aiyagari example does not have a `d` variable, contrary to Pijoan-Mas.

```auto
Calculating price corresponding to the stationary general eqm 

GeneralEqmConditionsFnOpt =

  function_handle with value:

    @(p)HeteroAgentStationaryEqm_Case1_subfn(p,n_d,n_a,n_z,l_p,pi_z,d_grid,a_grid,z_grid,ReturnFn,FnsToEvaluate,GeneralEqmEqns,Parameters,DiscountFactorParamNames,ReturnFnParamNames,FnsToEvaluateParamNames,GeneralEqmEqnParamNames,GEPriceParamNames,heteroagentoptions,simoptions,vfoptions)

Unrecognized function or variable 'l_z'.

Error in ValueFnIter_Case1_LowMem_NoD_Par2_raw (line 25)
        ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn,0, n_a, ones(l_z,1),0, a_grid, zvals,ReturnFnParams);

Error in ValueFnIter_Case1 (line 569)
                [VKron,Policy]=ValueFnIter_Case1_LowMem_NoD_Par2_raw(V0, n_a, n_z, a_grid, z_gridvals, pi_z, DiscountFactorParamsVec, ReturnFn, ReturnFnParamsVec, vfoptions.howards, vfoptions.maxhowards, vfoptions.tolerance);

Error in HeteroAgentStationaryEqm_Case1_subfn (line 8)
[~,Policy]=ValueFnIter_Case1(n_d,n_a,n_s,d_grid,a_grid,s_grid, pi_s, ReturnFn, Parameters, DiscountFactorParamNames,ReturnFnParamNames,vfoptions);

Error in HeteroAgentStationaryEqm_Case1>@(p)HeteroAgentStationaryEqm_Case1_subfn(p,n_d,n_a,n_z,l_p,pi_z,d_grid,a_grid,z_grid,ReturnFn,FnsToEvaluate,GeneralEqmEqns,Parameters,DiscountFactorParamNames,ReturnFnParamNames,FnsToEvaluateParamNames,GeneralEqmEqnParamNames,GEPriceParamNames,heteroagentoptions,simoptions,vfoptions) (line 197)
    GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_subfn(p, n_d, n_a, n_z, l_p, pi_z, d_grid, a_grid, z_grid, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, GeneralEqmEqnParamNames, GEPriceParamNames, heteroagentoptions, simoptions, vfoptions)

Error in fminsearch (line 216)
fv(:,1) = funfcn(x,varargin{:});

Error in HeteroAgentStationaryEqm_Case1 (line 216)
    [p_eqm_vec,GeneralEqmConditions]=fminsearch(GeneralEqmConditionsFnOpt,p0,optimoptions);

```

---

<div class="post-metadata">

**Author:** ![robertdkirkby](https://discourse.vfitoolkit.com/user_avatar/discourse.vfitoolkit.com/robertdkirkby/32/287_2.png) [@robertdkirkby](https://discourse.vfitoolkit.com/u/robertdkirkby)\
**Post date:** [July 7, 2024, 9:22pm UTC](https://discourse.vfitoolkit.com/t/bug-in-infinite-horizon-codes-after-improvement/292/5 "2024-07-07T21:22:04Z")

</div>

Fixed. Thanks for spotting.
