# Finding the general equilibrium

**URL:** <https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305>\
**Category:** Uncategorized\
**Created:** [September 8, 2024, 4:01pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305 "2024-09-08T16:01:27Z")\
**Posts on this page:** 9\
**Page:** 2

<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 8, 2024, 8:47pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/21 "2024-10-08T20:47:36Z")

</div>

I was able to test fminsearch vs fsolve on your Aiyagari example, and fsolve is faster. I think fzero will be even faster 🙂  
Note: I measure only the time to run the GE function, so the time reported below is NOT the time to run the entire program Aiyagari1994.m

```auto
Algorithm = 1 (fminsearch)
 p_eqm = 0.038969 
 GE residual = 0.000009 
 Running time = 4.422830
 
 Algorithm = 7 (fsolve)
 p_eqm = 0.038969 
 GE residual = 0.000009 
 Running time = 3.373976
```

---

<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 8, 2024, 8:56pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/22 "2024-10-08T20:56:41Z")

</div>

I just ran my Aiyagari example and had that exact problem, now I see your comment so I will try modify in the manner you advise. Hopefully back soon with a fix 😃

---

<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 8, 2024, 9:10pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/23 "2024-10-08T21:10:45Z")

</div>

Looks like fzero() just needed heteroagentoptioins.outputGEform=1 (so vector of GE conditions rather than their sum-of-squares). I’ve just pushed this fix.

For simplicity, I changed it all back to fminsearch() as the default.

In any case, it is nice that fzero() is now a working option 😃

If you get different results for the runtimes, let me know.

PS. 4s vs 6s, clearly your GPU is better than mine 🤩

_[edited to remove something that was incorrect]_

---

<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 8, 2024, 11:47pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/24 "2024-10-08T23:47:58Z")

</div>

**Bug in `HeteroAgentStationaryEqm_Case1`**

Actually it seems there is still a problem. If I set fminalgo=0, then it gets changed internally to fminalgo=1.

I debugged the code and I found out that upon entry of the function `HeteroAgentStationaryEqm_Case1`, fminalgo is still equal to 0, as it should be, but then it gets changed to fminalgo=1 at some point. Therefore even if the user sets options.fminalgo=0, the toolkit uses fminsearch.

You can have a look at my forked repository on Github. See line 112 of

> <https://github.com/aledinola/VFItoolkit-matlab-examples/blob/master/HeterogeneousAgentModels/Aiyagari1994.m>

**fzero initial condition**  
As of calling fzero with interval: Matlab claims that it is faster, see this

 ![image](https://discourse.vfitoolkit.com/uploads/default/original/1X/5f1d4d2f40ae83eb383f394f5dda5ba2acd3f1a0.png)

I wanted to do some tests and indeed you can find it in my aiyagari file. I wrote a function `fun_GE` that computes the residual of the general equilibrium condition. In the main script Aiyagari1994.m I call fzero to find the root of the function `fun_GE`, passing as initial condition for the GE price (in this model, the interest rate) an interval instead of a scalar.

---

<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 9, 2024, 8:30pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/25 "2024-10-09T20:30:10Z")

</div>

oops, I’d accidently forced fminalgo=1 (fminsearch). Fixed now.

fzero() (fminalgo=0) does still give error due to trying negative interest rate and returning complex.

fsolve() is now about 20% faster than fminsearch()

---

<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 9, 2024, 8:35pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/26 "2024-10-09T20:35:04Z")

</div>

You can’t do fzero() using an interval. I will think about if there is a nice way to implement this.

---

<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 9, 2024, 9:58pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/27 "2024-10-09T21:58:40Z")

</div>

Thanks for fixing this! But now fzero has the same error that you already fixed yesterday 🙂

In HeteroAgentStationaryEqm\_Case1, line 223, you set `heteroagentoptions.outputGEform=1;` before calling fzero, but this has no effect since heteroagentoptions is not passed to GeneralEqmConditionsFnOpt which was defined above.

In `HeteroAgentStationaryEqm_Case1_subfn`, `heteroagentoptions.outputGEform=0` and the code evaluates line 33

```auto
GeneralEqmConditions=sqrt(sum(heteroagentoptions.multiGEweights.*(GeneralEqmConditionsVec.^2)));

```

This is not correct since fzero is a rootfinding and not a minimization algorithm. The correct implementation should have

```auto
GeneralEqmConditions=GeneralEqmConditionsVec;
GeneralEqmConditions=gather(GeneralEqmConditions);

```

I’m not sure how to modify the code. In pseudocode, I would do simply

````auto
if fzero or fsolve %Rootfinding 
  GeneralEqmConditions=gather(GeneralEqmConditionsVec);
elseif fminsearch %Minimization
  GeneralEqmConditions=sqrt(sum(GeneralEqmConditionsVec.^2));
end
```
````

---

<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 9, 2024, 10:38pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/28 "2024-10-09T22:38:34Z")

</div>

Fixed. Just needed to set it earlier (like I did for other fminalgo variants). Phew, serious amateur hour on my part 😅 Thanks for your patience to keep on telling me what I did wrong 🤣

You are right, fzero is fast!!! It is not currently the default, but is 1.5s, vs 6s for fminsearch on the Aiyagari model.

---

<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 9, 2024, 11:04pm UTC](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305/29 "2024-10-09T23:04:36Z")

</div>

No problem, I’m always happy to help improve the toolkit 🙂 By the way, the toolkit is becoming seriously fast 🥳

One note: the default tolerance TolX is 1e-4, which is good for fminsearch (it is the same in Matlab) but it may be too loose for fzero. This explains also in part why you found fzero so much faster than the alternatives.

Using the toolkit defaults I get

```auto
Algorithm = 0 
 p_eqm = 0.039075 
 GE residual = 0.006544 
 Running time = 0.961645

```

which is super fast but less accurate than fminsearch (see GE residual; interest rate is good though).  
In my codes I usually set TolX=1e-6 for fzero and if I do so by setting `heteroagentoptions.toleranceGEprices = 1e-6;` the results are:

```auto
Algorithm = 0 
 p_eqm = 0.038969 
 GE residual = 0.000010 
 Running time = 2.905725  

```

Still faster than fminsearch/fsolve but more accurate than before. Matlab sets the following defaults:

[fminsearch](https://www.mathworks.com/help/matlab/ref/fminsearch.html)  
TolX = 1e-4  
TolFun = 1e-4

[fzero](https://www.mathworks.com/help/matlab/ref/fzero.html)  
TolX = 2.2204e–16

The tolerance for fzero is very very strict, in my experience far too strict. Also, fzero does not accept TolFun (no idea why), so if you set it, it is irrelevant.

I’m not sure what to recommend as default options. Maybe the best way is to let them as they are now. An advanced user can modify them if needed.

[Previous page](https://discourse.vfitoolkit.com/t/finding-the-general-equilibrium/305.md?page=1)
