How to count number of times a sequence didn't converge in a for loop
1 view (last 30 days)
Show older comments
Harriet Cant on 3 Jun 2017
Answered: Arnav Mendiratta on 13 Jun 2017
I'm writing a for loop, and each time the for loop runs I'm finding the mle using Matlab's inbuilt function. Sometimes the mle doesn't converge, and I want to introduce a counter to tell me how many times this happened. Something like "if converges, add 0, if not, add 1" kind of thing.
Arnav Mendiratta on 13 Jun 2017
As the foot note of mle function points out, with a poor starting point, the algorithm might converge to a local maxima instead of a global one or fail to converge at all. In case it does fail to converge, you will receive a warning
Warning: Maximum likelihood estimation did not converge. Iteration limit exceeded.
In such a case, you can not trust the results. You can, however, get the confidence levels of the estimated parameters if you use the function as:
[phat, pci] = mle(___,Name,Value)
If you see such a warning and you have Optimization toolbox, you can use the 'OptimFun' name value pair to use 'fmincon' as the optimization function. You can then change the maximum number of iterations ('MaxIter') and step of derivative ('DerivStep') to make sure the algorithms reaches an optima. By default, the mle function uses the following settings:
struct with fields:
Of course, this does not point out the exact number of iterations taken to converge to a parameter estimate.
If this warning occurred, this means that the function wasn't able to converge and you can find the warning ID using "lastwarn".
Warning: Maximum likelihood estimation did not converge. Iteration
> In mlecustom (line 241)
In mle (line 239)
In FitRicianDistributionwithKnownScaleParameterExample (line 18)
>> [msgStr,msgId] = lastwarn
'Maximum likelihood estimation did not converge. Iteration limit exceeded.'
Now if you have this warning, you can implement something that identifies if the algorithm did not converge and then add 0 or 1 based on your use case. This post has some discussion on how to achieve this.
Find more on Choose a Solver in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!