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
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
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
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
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
fzero initial condition
As of calling fzero with interval: Matlab claims that it is faster, see this
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.
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
I’m not sure how to modify the code. In pseudocode, I would do simply
if fzero or fsolve %Rootfinding
GeneralEqmConditions=gather(GeneralEqmConditionsVec);
elseif fminsearch %Minimization
GeneralEqmConditions=sqrt(sum(GeneralEqmConditionsVec.^2));
end
```
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.
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
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:
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:
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.