# Pure discretization with refinement

**URL:** https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206
**Category:** Uncategorized
**Created:** [July 28, 2023, 1:35pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206 "2023-07-28T13:35:11Z")
**Posts on this page:** 7
**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 28, 2023, 1:35pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/1 "2023-07-28T13:35:11Z")

</div>

I am using the toolkit to solve an infinite horizon model with endogenous labor supply (similar to Pijoan-Mas RED 2006) with the method ‘pure\_discretization\_refine’.

This method turns out to be 50% faster than the standard without refinement. As far as I understand it precomputes the optimal d choice for each triplet (a’,a,z) so that there is no need to optimize over d in each VF iteration.

I think there is a bug in the following code, though:  
[https://github.com/vfitoolkit/VFIToolkit-matlab/blob/master/ValueFnIter/InfHorz/Refinement/ValueFnIter\_Case1\_Refine.m](https://github.com/vfitoolkit/VFIToolkit-matlab/blob/master/ValueFnIter/InfHorz/Refinement/ValueFnIter_Case1_Refine.m)

When calling the function we pass as inputs n\_d,n\_a,n\_z but in the function we use sometimes N\_a and N\_z instead of n\_a, n\_z. Since Matlab is case sensitive, this throws an error. I have replaced N\_a with n\_a and N\_z with n\_z and all is fine.

---

<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 28, 2023, 9:16pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/2 "2023-07-28T21:16:24Z")

</div>

This is why ‘refine’ is not yet a publicly documented feature 😉

Your understanding is exactly right. That is also why this ‘refine’ made solving the Kitao (2008) stationary eqm easy, but did not help for the transition path (in the infinite horizon you can presolve for d, and then use this at every iteration, but for the transition path the d has to be different in every period, so even if you presolve for d you then would only use this once).

Nice that it also speeds up notably for a model similar to Pijoan-Mas (2006). I haven’t actually yet tried using it anywhere except on that Kitao (2008) code so good to know it is something I should maybe think about making the default.

I corrected this by putting,

> N\_a=prod(n\_a);  
> N\_z=prod(n\_z);

near start of this function.

VFI Toolkit uses n\_a to be a vector of the number of points in each dimension (e.g., if you have two endogenous states with 201 and 101 points respectively, then n\_a=[201,101]), whereas when everything then gets vectorized internally N\_a is used (so would have N\_a=20301, which is 201\*101). In your case as there was just one dimension for n\_a, you were able to just set N\_a=n\_a with no issues. Same for n\_d vs N\_d, n\_z vs N\_z etc., the lower case is the vector of number of points in each dimension and the upper case is the product of this vector (awkward wording, but it is the number of points in the vectorized problem). All of this is normally hidden from the user, but since you are looking inside you are seeing it all 🙂

---

<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 29, 2023, 1:58pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/3 "2023-07-29T13:58:44Z")

</div>

Hi Robert,  
Thanks for your answer! To me it makes sense using ‘refine’ as default. It should also be better in terms of memory requirements: during each VF iteration, the returnMatrix is na \* na \* nz instead of nd\* na \* na \* nz.

---

<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: [August 1, 2023, 9:18pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/4 "2023-08-01T21:18:38Z")

</div>

Hi Robert,

I checked the updated code on github  
[https://github.com/vfitoolkit/VFIToolkit-matlab/blob/master/ValueFnIter/InfHorz/Refinement/ValueFnIter\_Case1\_Refine.m](https://github.com/vfitoolkit/VFIToolkit-matlab/blob/master/ValueFnIter/InfHorz/Refinement/ValueFnIter_Case1_Refine.m)

and there is a typo:

> N\_a=prod(n\_a);  
> N\_a=prod(n\_z); %It should be N\_z=prod(n\_z);

I realized it because I got an error when I tried to run the code 🙂

I fixed it manually in my local copy and it works smooth and fast!

---

<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: [August 1, 2023, 9:51pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/5 "2023-08-01T21:51:58Z")

</div>

Oops, fixed, thanks!

---

<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: [August 29, 2023, 5:18pm UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/6 "2023-08-29T17:18:56Z")

</div>

The refinement option works really well… I’ve tested on the Pijoan-Mas model (baseline parametrization with possibly some tax function added) and it speeds up the code by 50%. I attach the codes which may be useful for some user

> **[GitHub - aledinola/PijoanMasTaxes: Aiyagari model with endogenous labor (as in...](https://github.com/aledinola/PijoanMasTaxes)**
>
> Aiyagari model with endogenous labor (as in Pijoan-Mas 2006), with VFI toolkit

In the file change the following options:

vfoptions.solnmethod = ‘purediscretization\_refinement’;  
%vfoptions.solnmethod = ‘purediscretization’;

The method ‘purediscretization’ is the toolkit default, while ‘purediscretization\_refinement’ is the refinement with precomputation of optimal d

---

<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: [September 11, 2023, 1:59am UTC](https://discourse.vfitoolkit.com/t/pure-discretization-with-refinement/206/7 "2023-09-11T01:59:40Z")

</div>

Refinement is now the default for infinite horizon value function problems. So everyone can benefit from code running a little bit faster 🙂
