# OLG Transition Path Error

**URL:** https://discourse.vfitoolkit.com/t/olg-transition-path-error/314
**Category:** Uncategorized
**Created:** [October 29, 2024, 6:53am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314 "2024-10-29T06:53:07Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [October 29, 2024, 6:53am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/1 "2024-10-29T06:53:07Z")

</div>

Hi Robert,

I tried to use method in Aiyagari1994TransitionPath.m to OLGModel6 to see the impact of increasing in alpha. To make it run faster, I reduce the grid size, keep only 2 prices (r and Accbeq), and use only one shock (z).

vfiToolkit got initial and final equilibrium successfully, but when it ran PricePath=TransitionPath\_Case1(), the following error appears:

> Error using ReturnFnParamNamesFn (line 63)  
> The parameter agej must be scalar or age-dependent (check the size of this parameter; it is needed as an inputto the ReturnFn)  
> Error in TransitionPath\_Case1 (line 282)  
> ReturnFnParamNames=ReturnFnParamNamesFn(ReturnFn,n\_d,n\_a,n\_z,0,vfoptions,Parameters);  
> Error in transition\_test (line 215)  
> PricePath=TransitionPath\_Case1(PricePath0, ParamPath, T, V\_final, StationaryDist\_init, n\_d, n\_a, n\_z, pi\_z, d\_grid,a\_grid,z\_grid, ReturnFn, FnsToEvaluate, TransPathGeneralEqmEqns, Params, DiscountFactorParamNames, transpathoptions);

I don’t understand the error message here since size(agej)=[1,81], which is the same as [1,Nj]. It should be age-dependent.

---

<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: [October 29, 2024, 7:34am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/2 "2024-10-29T07:34:07Z")

</div>

If I understand correctly you used  
`PricePath=TransitionPath_Case1()`  
which is the infinite horizon model version.

What you need instead is  
`PricePath=TransitionPath_Case1_FHorz()`  
which is the finite-horizon model version.

The way everything works is otherwise much the same in terms of how to set things up.

[Much like Aiyagari model uses ValueFnIter\_Case1() while OLG models use ValueFnIter\_Case1\_FHorz()]

[Since infinite-horizon models don’t understand N\_j, you got that error.]

---

<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: [October 29, 2024, 7:36am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/3 "2024-10-29T07:36:41Z")

</div>

PS. turn on `vfoptions.divideandconquer=1` and `transpathoptions.fastOLG=1` when you do the transition path. Will make it vastly faster.

[`vfoptions.divideandconquer=1` requires that the model jas montone policy functions, and OLGModel6 will satisfy this. Actually you should also use this to speed up the stationary eqm calculations.]

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [October 29, 2024, 1:45pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/4 "2024-10-29T13:45:55Z")

</div>

I see. I changed to FHorz and get the new error:

> Unrecognized function or variable ‘CreateReturnFnMatrix\_Case1\_Disc\_fastOLG\_DC1\_Par2’.  
> Error in ValueFnIter\_FHorz\_TPath\_SingleStep\_fastOLG\_DC1\_raw (line 50)  
> ReturnMatrix\_ii=CreateReturnFnMatrix\_Case1\_Disc\_fastOLG\_DC1\_Par2(ReturnFn, n\_d, n\_z, N\_j, d\_gridvals, a\_grid, a\_grid(level1ii), z\_gridvals\_J, ReturnFnParamsAgeMatrix,1);

> Error in ValueFnIter\_Case1\_FHorz\_TPath\_SingleStep\_fastOLG (line 24)  
> [VKron, PolicyKron]=ValueFnIter\_FHorz\_TPath\_SingleStep\_fastOLG\_DC1\_raw(VKron,n\_d,n\_a,n\_z, N\_j, d\_grid, a\_grid, z\_gridvals\_J, pi\_z\_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);

> Error in TransitionPath\_Case1\_FHorz\_shooting\_fastOLG (line 235)  
> [V, Policy]=ValueFnIter\_Case1\_FHorz\_TPath\_SingleStep\_fastOLG(V,n\_d,n\_a,n\_z,N\_j,d\_grid, a\_grid, z\_gridvals\_J, pi\_z\_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);

> Error in TransitionPath\_Case1\_FHorz (line 738)  
> PricePathOld=TransitionPath\_Case1\_FHorz\_shooting\_fastOLG(PricePathOld, PricePathNames, PricePathSizeVec, ParamPath, ParamPathNames, ParamPathSizeVec, T, V\_final, AgentDist\_init, jequalOneDist, n\_d, n\_a, n\_z, N\_j, d\_grid,a\_grid,z\_gridvals\_J, pi\_z\_J, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeights, ReturnFnParamNames, vfoptions, simoptions,transpathoptions);

> Error in transition\_test (line 215)  
> PricePath=TransitionPath\_Case1\_FHorz(PricePath0, ParamPath, T, V\_final, StationaryDist\_init, jequaloneDist,n\_d, n\_a, n\_z, N\_j, pi\_z, d\_grid,a\_grid,z\_grid, ReturnFn, FnsToEvaluate, TransPathGeneralEqmEqns, Params, DiscountFactorParamNames, AgeWeightsParamNames, transpathoptions, simoptions, vfoptions);

Seems that a function is missing, or maybe different name?

---

<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: [October 29, 2024, 6:45pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/5 "2024-10-29T18:45:03Z")

</div>

Oops sorry, was missing a file. Should be there now 🙂

(I just overhauled this last week and has missed a file when pushing to github)

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [October 29, 2024, 10:02pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/6 "2024-10-29T22:02:53Z")

</div>

Thanks. Now it runs successfully. Also I wonder does it work for two assets model?

---

<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: [October 30, 2024, 12:43am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/7 "2024-10-30T00:43:19Z")

</div>

Currently vfoptions.divideandconquer=1 for the transition path has only been coded for the case of just one endogenous state.

In principle a transition path with vfoptions.divideandconquer=0 and two endogenous states should work (I’ve never actually tried it) but it is likely both slow and very demanding of GPU memory.

If you specifically want to be doing an OLG transition path for a two asset model tell me and I will put coding it for vfoptions.divideandconquer=1 as a priority (currently it is not a priority for me. I don’t know how well it will work, in the sense it will work okay but might be very demanding of GPU memory given the fastOLG=1)

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [October 30, 2024, 1:18am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/8 "2024-10-30T01:18:16Z")

</div>

I’d appreciate if you could add that feature since I’m doing a research related to housing portfolio, which has two assets.  
i did try to run it without using divideandconquer. It reports an error when I ran

> PricePath=TransitionPath\_Case1\_FHorz(.)

The issue is from line 261 of TransitionPath\_Case1\_FHorz.m, it requires size(a\_grid)==[N\_a,1]. But N\_a=prod(n\_a), so if my assets grid n\_a is [101,6], N\_a becomes 606, while the size of stacked a\_grid is [107,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: [October 30, 2024, 8:37pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/9 "2024-10-30T20:37:11Z")

</div>

@Shiki If you are working on an OLG two-asset model with transition, I think the example based on K.Chen’s RED paper can be a useful reference

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [October 30, 2024, 9:01pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/10 "2024-10-30T21:01:40Z")

</div>

Actually this afternoon I tried to incorporate transition path code to [Chen(2010) sample codes](https://github.com/robertdkirkby/Chen2010housing/blob/main/Chen2010.m), and get the same error. In the sample, n\_a=[301,35], but [TransitionPath\_Case1\_FHorz](https://github.com/vfitoolkit/VFIToolkit-matlab/blob/master/TransitionPaths/FHorz/TransitionPath_Case1_FHorz.m) will check the size of inputs (line 260,261)

> if ~all(size(a\_grid)==[N\_a, 1])  
> error(‘a\_grid is not the correct shape (should be of size N\_a-by-1) \n’)

In this case, LHS is [301+35,1] and RHS is [301\*35,1], so it will stop running. I haven’t looked into the codes in details, not sure if it will cause problem if I change the calculation of N\_a.

---

<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: [October 31, 2024, 12:43am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/11 "2024-10-31T00:43:34Z")

</div>

The earlier error was my fault, should be all(size(a\_grid)==[sum(n\_a),1]). I hope to find time to fix this and run a test tomorrow.

[As I mentioned, while I think I coded for two assets, I have never run a test code before, so it is a little rough.]

---

<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: [November 1, 2024, 9:18pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/12 "2024-11-01T21:18:44Z")

</div>

I was looking and realised that a transition path with two endogenous states is going to take more work than I thought.

I am pretty busy on some projects at the moment. Doing this is a moderate priority for me, but I suspect will happen in January (the two projects that need all my time both finish by late Dec). Hopefully I will be back here in early Jan to post that it works now 🙂

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [November 2, 2024, 2:10am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/13 "2024-11-02T02:10:47Z")

</div>

Thanks, Robert. I’ll wait for the new update. And could you explain briefly how vfitoolkit calculate the price path from the initial price path?

---

<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: [November 2, 2024, 3:56am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/14 "2024-11-02T03:56:03Z")

</div>

I added a brief description of how transition paths work to the VFI Toolkit [Pseudo-Codes pdf](https://github.com/vfitoolkit/Pseudocodes/blob/main/VFI_PseudoCodes.pdf)

I copy paste it here for convenience

* * *

We consider a transition path of length T time periods. Solving for a transition path general equilibrium, we are looking for some path-of-parameters that are determined in general equilibrium, call them \Theta such that our general equilibrium equations equal zero in every time period, GEeqn(\Theta)=0. Note that at some level this is the same thing we did for a stationary equilibrium, except that now \Theta=\{\theta\_1,....,\theta\_T\} is a time-path of general-eqm parameters, rather than just parameters at a single point in time.

There are various algorithms for doing this (\textit{heteroagentoptions.fminalgo} determines which is used), but all of them essentially boil down to the following,  
\begin{algorithmic}  
\State Guess some initial \Theta^0 (time-path on general-equilibrium parameters)  
\State Define GeneralEqmConditions=\infty  
\While GeneralEqmConditions\>tolerance  
\State Solve backward, t=T-1,\dots,2,1 for the value fn and policy at each t (given current \theta^k\_t)  
\State Solve forward, t=1,2,\dots,T for the agent distribution (given current \theta^k\_t and Policy\_t we just found)  
\State Solve the aggregate variables at each t (given current \theta\_t^k and policy/agent dist we just found for t)  
\State Evaluate GeneralEqmConditions=[GEeqn\_1(AggVars\_1,\theta\_1^k), $GEeqn\_1(AggVars\_1,\theta\_1^k),$$\dots,$ GEeqn\_T(AggVars\_T,\theta\_T^k)] (evaluate general eqm eqns at each time period)  
\State Update \Theta^{k+1} (in some way, likely based on \Theta^k and GeneralEqmConditions)  
\EndWhile  
\ \Return \theta  
\end{algorithmic}  
The difference between the different \textit{fminalgo} is how we update the \Theta. The most common way to update is as a shooting algorithm.

In a stationary equilibrium, parameters other than those parameters which were being determined in general equilibrium (the general equilibrium parameters) were all constant (in time) and kept in Parameters structure. When we solve transition paths, some parameters might vary over time, and this is done with the ParamPath (a path on exogenous parameters), while any other parameters not in this path are assumed to be constant at their values in Parameters structure. It is not described in algorithm above, but obviously we have to make sure to use the correct parameters in each time period.

It is important to solve backward for policy and forward for agent distribution. For convenience, aggregate variables and general eqm conditions are often solved forwards while doing agent distribution, but in principle how we deal with these (in terms of timing) is not important is most models (can be important if we use lagged/leading parameter values for something).

Whether the problem is finite or infinite-horizon value functions, and whether or not there are permanent types of different kinds of shocks is unimportant, in the sense that the above steps are all the same steps, just that the details of their implementation is changed.

* * *

PS. This looks a lot like how stationary general eqm is solved (for \theta^k, solve V and Policy, solve agent dist, evaluate agg vars, evaluate general eqm eqns, update \theta^{k+1}), just that now everything is a path instead of a value at a single point in time.

---

<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: [January 9, 2025, 8:01pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/15 "2025-01-09T20:01:34Z")

</div>

Was a typo. Should be sum(n\_a)-by-1. Have just pushed a fix.

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [January 10, 2025, 6:53am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/16 "2025-01-10T06:53:06Z")

</div>

I still get the same error.

> Error using TransitionPath\_Case1\_FHorz (line 254)  
> a\_grid is not the correct shape (should be of size sum(n\_a)-by-1) \n

Now size(a\_grid) equals to [sum(n\_a), 1] and I don’t know why this error message still appears. Here is my [code](https://github.com/shiki-jiang/chen2010_transitionpath/blob/main/chen2010_test.m). The first half is the same (I reduce the grid size) as your sample code and I made a few adjustment for transition path part.

---

<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: [January 12, 2025, 11:29pm UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/17 "2025-01-12T23:29:59Z")

</div>

Error was just because your inputs were not in correct order.

(I moved pi\_z: [Transition Paths for FHorz (minor breaking change)](https://discourse.vfitoolkit.com/t/transition-paths-for-fhorz-minor-breaking-change/341) )

I suspect you got the order copying from some other example. If you did then let me know which I will update that example to the correct order.

---

<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: [January 13, 2025, 12:27am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/18 "2025-01-13T00:27:49Z")

</div>

Here is a [version of your code with some more comments](https://www.dropbox.com/scl/fi/n1cfma9fj3ahp5jx81dtw/chen2010_test.m?rlkey=fwaacpmibyqvbfc8xs1evrlbm&st=svf7rb1w&dl=0) (you can use this to update the version in [your repo](https://github.com/shiki-jiang/chen2010_transitionpath/tree/main))

The transition is not yet working, but before you get to that there is an issue.

You are looking at a transition path where initial eqm is gamma=0.2 and final eqm is gamma=0.1. gamma is the collateral constraint.

You set up asset\_grid as having a minimum point.  
`minassets=-(1-Params.gamma)*maxh`  
And then you change gamma, which requires care as the grid won’t change.  
Your reform decreases gamma, which increase 1-gamma, and so you should have a larger number for `minassets`. But you don’t get this as the asset grid remains fixed at what it was originally.

---

<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: [January 13, 2025, 2:20am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/19 "2025-01-13T02:20:28Z")

</div>

Just pushed to github. Should run okay now for transition path in FHorz model with two endogenous states. That said, there is not yet divide-and-conquer, nor fastOLG, so while I think it works okay, it is going to be slow.

---

<div class="post-metadata">

### Author: ![Shiki](https://discourse.vfitoolkit.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@Shiki](https://discourse.vfitoolkit.com/u/Shiki)
#### Post date: [January 13, 2025, 5:26am UTC](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314/20 "2025-01-13T05:26:52Z")

</div>

Thanks Robert. Yes it works now.  
As for the issue of gamma, can I add some grids based on -(1-Params.gamma1)\*maxh, where Params.gamma1=0.1, to the original asset\_grid? By doing this, the new value of minassets can be chosen.

[Next page](https://discourse.vfitoolkit.com/t/olg-transition-path-error/314.md?page=2)
