Repository navigation
ENH: Relative tolerance for GLM convergence - #2803
Conversation
|
Thanks for pointing this out some unsorted thoughts
a tip
scipy optimizers all have tolerance absolute and in terms of parameters, IIRC as a workaround: We need to do something, but I'm not quite sure what. (I haven't looked at off-by-1 yet.) |
|
@kshedden Any opinion on this? |
| def _check_convergence(criterion, iteration, tol): | ||
| return not (np.fabs(criterion[iteration] - criterion[iteration-1]) > tol) | ||
| if iteration == 0: | ||
| return False |
There was a problem hiding this comment.
Why did you need to special case iteration 0?
We could have converged after one iteration.
There was a problem hiding this comment.
Maybe I'm confused, but the 0th element of criterion[iteration] is set to be np.inf. The first iteration will compare np.inf vs. the deviance after the first IRLS iteration. So, it shouldn't converge ever.
So I think you are right, we don't need the iteration == 0 case as np.inf - x should always be > tol.
No?
There was a problem hiding this comment.
I think you are right in general. We need one extra iteration to check convergence even with gaussian and OLS, because we don't know yet that we already converged. (I had forgotten about this.)
However, following your off-by-1 argument, I think the initial np.inf is redundant, since we initialized with the starting estimate. If the starting values (e.g. user provided start_params) are already optimal, then we only need the iteration=0 round to verify that we are converged.
|
GLM is a core model and it has been this way for a long time, most likely since the beginning. |
|
RLM is similar and the convergence criterion is on the value of the objective function, but it has an option for choosing it GLSAR uses max rtol for params MixedLM uses max atol for fe params Quantile regression uses max atol for params those are the only ones with our model specific estimators (GLSHet uses fixed number of iterations, open issue IIRC) our scipy optimizers are mixed according to our docstrings rtol or tol on gradient |
|
I added prio-high label for some solution to the convergence criterion. |
|
I opened an issue #2825 for the record |
|
Yes, that's the impetus for my original pull request. I was under the On Thursday, February 18, 2016, Josef Perktold [email protected]
|
|
As another aside, when I was fitting GLM's with R, I would get convergence in 5-10 iterations. statsmodels was taking 25 or so for the same data. |
|
About the number of iterations it takes: Do you know what the speed difference is between statsmodels and R? |
|
I think this PR looks good now. I think we can go the direction of RLM and add another option to choose what is used for convergence, e.g. so users or we, if or when we want to change the default, can switch to convergence in params. I think convergence in params, and score, are a lot more important than convergence in deviance, sum of squares or loglike. |
|
I haven't looked at the test results yet, and didn't check what the default tolerances should be. |
|
Ok. I'm going to go radio silent on this PR then and leave it up to your discretion how you want to implement. I think the failing tests are due to...
If you wish for me to add stuff let me know. I hadn't thought about using other metrics for the tolerance calculation. Off the top of my head, parameters, log-likelihood, and scores sound simple enough. But I'd want a list before I invest any time programming it. |
|
One other thought... log-likelihood seems especially compelling considering the basis for the calculus underlying this is MLE. I'd be interested if we could find a use case where an iteration causes likelihood to fall. If MLE falls, that might be enough to make us want to break the algorithm as the fit is probably garbage. |
|
loglikelihood and deviance are essentially the same, up to constant factors. The only two options right now would be Our IRLS is plain vanilla and doesn't do backtracking nor checking for no improvement. There are some substantial differences between IRLS and scipy optimizers for non-canonical links. In that case the Hessian and the Fisher information matrix are not the same and they don't solve exactly the same problem. (I haven't investigated that in details.) |
|
I'm starting to look a bit at the test failures (edited while adding cases) statsmodels.discrete.tests.test_constrained.TestGLMLogitConstrained2.test_basic_method statsmodels.discrete.tests.test_constrained.TestGLMPoissonConstrained1b.test_compare_glm_poisson test_glm test_gradient_irls statsmodels.stats.tests.test_mediation.test_framing_example |
|
I loosened some of the tests to show it now passes. Like I said, I'm fairly confident that before IRLS would continue to run well after the convergence criterion was met. That made some results too good. The tests seem to be passing here. The fails are the other one's we've had sitting around. |
|
The problem is that the old setting affects convergence very differently depending on the deviance, and indirectly the sample size. Even though we loosen for large sample sizes, we shouldn't loosen (too much) for small sample sizes. I still don't have a good overview how this affects different cases. The increase in the tolerance in here might be too large. Furthermore, I think I'd like to move to using params as default convergence target. There are two things in this PR:
correction
One extra check to verify how good the convergence is, is to look at the resulting |
|
Ok. Well, I'll leave this into your hands. You have a better understanding on the fancy-shmancy math anyways. Let me know if you want me to try something. I'm happy to experiment with using param convergence as the standard, but a little concerned that will break even more tests. If you don't mind me taking a whirl at it, let me know. In that event I'd probably revert the last change I made and then change the |
|
I think the easiest way forward is to add the option that lets user choose the convergence target, i.e. add This PR and issue have convinced me that we need to change the default criterion, which will always have the affect on some numbers. However, I would prefer that we have to do this only once, so I would like to understand better what we are switching to. |
f61d27d to
9949006
Compare
|
Ok, I lied. I was able to find some time to work on this today. I'm probably a little better with python than I was previously, so I sort of started from scratch here. 3 real changes behavioral changes:
As I'm reviewing this, I realized a small typo in my documentation, so I'll fix that, but I think this is ready for review. Some of these commits can be squashed, if necessary. Let me know. Thanks. |
| fit_kwds={'tol': 1e-10}) | ||
| cls.constraints = lc | ||
| cls.res1m = mod.fit_constrained(constr)._results | ||
| cls.res1m = mod.fit_constrained(constr, tol=1e-10)._results |
There was a problem hiding this comment.
it's better to be explicit and use atol instead of tol in these kinds of tests
There was a problem hiding this comment.
Fair point. I changed this in the most recent revision.
| values for gradient optimization. Only relevant if | ||
| `method` is set to something other than 'IRLS'. | ||
|
|
||
| IRLS Parameters |
There was a problem hiding this comment.
we cannot use nonstandard titles/headers in the docstrings because they are not recognized by the doc rendering
AFAICS either use Other Parameters or no headers for this section just continue with parameters
section 7 https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
AFAIK we don't use Other Parameters anywhere so I'm not sure how it works.
There was a problem hiding this comment.
Whoops. Didn't realize that was a "thing". I presume you could use any header. Thanks for that link. I tried to follow
|
that's all I can see right now. I will go over it in a bit more details on Friday and merge. One thing I haven't looked at or thought about is whether we want to do something with the |
|
Sounds good, as always Josef, thank you for your insights and helpfulness. I really appreciate it. |
|
|
||
| Notes | ||
| ----- | ||
| This method does not take any extra undocumented ``kwargs``. |
There was a problem hiding this comment.
can you delete this Notes section,
AFAICS, this is outdated because there are also optimization kwargs for fit_gradient
There was a problem hiding this comment.
I will. To avoid having a billion commits, I'll commit that in a few hours in case my OCD comes back.
|
Given that there were a few back and forth commits, I think this could be squashed. I don't see good breaks in the list of commits, so it could be squashed into a single commit. Can you do that? To clarify and summarize, the default has not changed at all. The only change in default behavior is coming from fixing the off-by-one error. Is that correct. I think I've seen enough, ready for merge, preferably after squashing, and when travis is as green as it gets. |
|
Yes, I can squash. I'm not as useless at that as I used to be.
|
Had to adjust 2 tests to pass... One more test... **fit_kwds != fit_kwds DOC typos and fixes Using atol instead of tol. Doc typo with default rtol = 0. More doc clarifications... need to stop being so OCD. IRLS Parameters not a numpydoc-approved header. Remove Notes.
|
Squashed and all green. Feel free to merge at your leisure. |
|
@thequackdaddy Thanks for another improvement to GLM |
|
It would be useful for future reference and recommendation and possible changes in default settings if you could add some feedback comments, once you get experience with how well this works and have recommendation for good values for rtol in your kind of use cases. Based on the discussion here I still think we need to change the defaults, but this PR is backwards compatible so much easier to merge. |
|
I don't disagree. Honestly, what I've been doing is just setting With that said, I can understand the advantages of Thanks again. |
|
The crucial qualifier is "relatively clean and straightforward". Overall, I think the current (a)tol on deviance is a bad idea (even if many packages use it), if we want reliable parameter estimates, or an indication and convergence warnings if the parameters don't converge cleanly and straightforwardly. |
|
Fair point. I had never considered using (Thinking out loud below) The one issue that I have with this is that in practicality, I set one column in the dataset to a constant 1 ( With that said, at this point I statsmodels can fit a GLM with 3.5 million records and 25 columns on a virtual machine with a eight 1.9 GHZ processors in about a 1.5 minutes when I limit to 10 iterations. After this change, I think that time is down to around about one minute (its about 5-7 iterations for |
|
Did you try plain numpy for creating the dummy variables? for tolerance I think that in cases like yours, both atol and rtol would have to be set. For example in unit tests I usually fine tune both for assert_allclose. My guess is that a default of rtol=atol=1e-8 or 1e-6 would work in most cases for params. However, atol might be too large if we have a tiny param but a large effect because the scale of the exog is large. The proper tolerance might be in terms of standardized coefficients, i.e. |
Hello,
I'd like to propose changing how you do convergence checking for GLM IRLS fits.
A few notes:
1e-8. I think this makes more sense because deviance increases as you add more observations. On a large dataset, waiting for deviance changes as small as 1e-8 requires many (essentially useless) iterations.Thanks for your consideration.