3

I'm using the distfit function of Matlab to fit a probability distribution to my data. Sometimes the following warning message appears:

Maximum likelihood estimation did not converge. Iteration limit exceeded

In this case the distribution is fitted (the negative log likelihood is not complex or infinite) but the fit is very bad (high AIC).

How can I check in Matlab if this warning appears? If such a warning appears I want to throw an error (and catch it).

Currently, I'm investigating if the neagtive log likelihood is complex or infinite and if so, I'm throwing an error. Are there other checks which I should do?

4
  • These functions usually have optional outputs giving you feedback about the goodness of the fit. You can check that. Looking at the docs, this might be in the distribution object returned. Mar 6, 2016 at 13:03
  • @AndrasDeak I'm already calculating the AIC score but it is difficult to determine if the fit is ok or not.
    – machinery
    Mar 6, 2016 at 13:57
  • You possible also want to check that it's ~isnan() too. But rather than throw an error (in any of the checked cases), just assign a large cost to the negative likelihood function. This will tell the optimizer that it's currently looking in a bad region, but won't terminate it. Mar 6, 2016 at 16:51
  • Usually is not always. I'm trying to catch the case where save ignores a variable because it's too big. And save returns nothing. Apr 5, 2022 at 20:30

2 Answers 2

5

You can't directly catch a warning, but can fake it by keying off the warning message being given by using the following construct

% reset warnings
lastwarn('');

% Do your fitting
<your code here>

% Check which warning occured (if any)
[msgstr, msgid] = lastwarn;
switch msgid
   case 'ThisParticularMessageID'
      % In your case you say you want to throw an error
      error(msgstr); % or your custom error message
   %case 'SomeOtherMessageIDIfYouWantToCheckForSomethingElse'

end

The tricky thing is finding the correct msgid. The easiest way is to use your existing code, and after you see the warning message, at the Command Line type

[msgstr,msgid] = lastwarn

That will tell you what you want to use for 'ThisParticularMessageID'.

3
  • I think this is perfectly good for OPs problem: they want to check after fitting whether there were any problems. Mar 6, 2016 at 16:01
  • Using an undocumented syntax, you can catch warnings. See my answer. Mar 6, 2016 at 19:55
  • 1
    +1, the best solution known to me. Be aware that any code first throwing the critical warning and then any other warning will not raise an error. It is only safe to use when no other warning can occur, which you never can guarantee because a migration warning might always appear in future MATLAB versions. I typically prefer code which false-positive throws exceptions over code which false-negative ignores problems, so I would raise any warning to an error.
    – Daniel
    Mar 6, 2016 at 20:45
3

Using the undocumented syntax warning('error', 'mycomponent:myMessageID') will tell MATLAB to convert the warning to an error, which you can then catch with a try-catch block and handle appropriately:

You can find the message ID for your warning using lastwarn just after it occurs.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.