Repository navigation
Avoid setting model.df_model in results.__init__ - #3735
jbrockmendel wants to merge 4 commits into
Conversation
Test case to check that model.df_model is not changed on call to fit_regularized closes statsmodels#3734
josef-pkt
left a comment
There was a problem hiding this comment.
minor comment on unit tests
otherwise, looks good to me
| data = sm.datasets.randhie.load() | ||
| model = sm.NegativeBinomial(data.endog, data.exog, loglike_method='nb2') | ||
|
|
||
| res1 = model.fit() |
There was a problem hiding this comment.
in general: If convergence is not of interest in a test, then use start_params with solution, or close to solution.
that saves some time in the test runs
| res3 = model.fit() | ||
| # res3 should be identical to res1. This test is being added in a PR | ||
| # that fixes a bug in which the call to `fit_regularized` modifies | ||
| # model.df_model, leading to res3.df_model != res1.df_model |
There was a problem hiding this comment.
this is not supposed to be a novel :)
one line (or maybe more for essential information) should be enough, details are in the issue.
e.g. "df should not be changed by having called fit_regularized"
also you can also check the model df directly, e.g.
assert_equal(res2.model.df_model, res1.model.df_model)
There was a problem hiding this comment.
A lot of authors weren't appreciated in their own eras...
also you can also check the model df directly, e.g.
assert_equal(res2.model.df_model, res1.model.df_model)
Aren't res2.model and res1.model the same object?
There was a problem hiding this comment.
yes, you're right.
the res2.model.df_model check needs to assert the integer, or a copy of the df
There was a problem hiding this comment.
I'm not entirely clear: does your last comment mean there is still an edit needed?
There was a problem hiding this comment.
You can remove the comment, but I was thinking that a direct test the model.df_model remains == 8 and similar for df_resid would be the more direct test that it is unchanged.
There was a problem hiding this comment.
I guess so. The assertion regarding res2.df_model is needed to ensure we don't accidentally have a trivially-passing test. We could even skip the fit() calls if we really wanted. I'm inclined to keep it as is because of a very specific laziness.
|
Related: #1203 |
|
Looks like similar incrementing of df_model and df_resid happens in generalized_linear_model and in The same fix here can be applied to genmod. Unfortunately that method is not getting covered when I run tests locally. Coveralls says it is covered; I'll look into the discrepancy. The version in _constraints is called only from tests, and is a near copy/paste of the methods in discrete and genmod, so I'm guessing it is on its way out. |
|
Are you running the full test suite? The version in statsmodels.base are the most generic solutions, e.g. IIRC Poisson -> GLM -> generic was the development path. The main problem for adding base._constrained fit_constrained to all models is that it needs offset, which was or is only available in GLM and discrete.CountModels. Enhancement to other models are still missing. So. base._constraints is waiting for the subclasses to catch up before taking over. |
I must not have been. Now it is getting hit as expected.
Luckily the code there doesn't alter self.model.df_foo inplace, only increments self._results.df_foo. In a perfect world I'd like to avoid even doing that much, but I'm not going to sweat this peripheral case for now. |
Luck or design? In some parts we can still see the age of the code. In the early development of statsmodels it wasn't so clear that we have to be careful about whether attributes are immutable model attributes or mutable results attributes. discrete fit_regularized hasn't been refactored since the beginning quite some time ago. (And that's another reason that a code review every 5 (?) years is useful.) In a near perfect world, I can still argue that df_xxx are attributes of an instance and are supposed to be set in another special/generic case |
Yah, I stumbled on that a few minutes ago. Looks like the only place it gets hit is in I propose we patch arma_mle so that it doesn't reach that branch, then require that model.df_model be defined before the call to
The more I look at this, the more I think that is feasible. Moreover, the shim that will make it work may also take care of the "#create dummy results Instance, TODO: wire up properly" workaround in all three appearances of The only cases I see where The L1 case is pretty easy, let's focus on the constrained case. We can pass I'll try it and check that this actually works. |
|
GenericLikelihoodModel and associated Results are allowed and supposed to have some "magic" so it works for different usecases without requiring the user writing a MLE Model as subclass to figure out and set many details. I don't really like hardcoding and imposing something like k_constr in the base classes. Any assumptions and pattern that are hard coded based on current models might make future extensions more difficult |
Suppose hypothetically that the suggestion 1) does work for all existing cases and 2) reflects an underlying logic that tentatively appears to apply generally. What would an appropriate place for that be? |
|
I think a relatively simple way will be to have eventually a proper A related problem with a large amount of retrofitting the results instance is The problem is that the results classes were initially designed to be closely linked to a specific model class. Essentially, it is one class split into two to take advantage of inheritance (and to keep results attributes separate from model attributes). also related to #3738 (comment) |
|
Between this thread and #3738, it seems like there are two inter-related topics here:
(And at the intersection of these two, the actual topic of the PR, in-place modification of model attributes outside model methods.) On the first, I'm resigned to punting on creation/inheritance/wrapping as being not worth the effort to try to push significant design changes. A better use of time will be incrementally moving attribute setting into On the second, in a perfect world df_model and df_resid would be functions of model attributes, data/params shapes etc in a way that was internally consistent-- i.e. a natural fit for properties. I'm giving up on the properties suggestion for now in favor of more incremental adjustments towards systematization. As to the PR itself, I think the only outstanding question is whether the same bugfix should be applied to GenericLikelihoodModelResults. I'd venture "yes", but would settle for a "# TODO: consider changing this" note and kicking it down the road. |
|
GenericLikelihoodModelResults This does not modify an existing model.df_resid, so it should be fine. I had forgotten that this discussion is in a PR, given the simultaneous discussion in issues. Any more general changes will need to check and make sure that df_resid, df_model have enough test coverage across models. df_resid is not used in the basic results with asymptotic normal assumption, i.e. when use_t=False, So the basic unit tests for a model will or might not always cover it.
|
You're right, but it is modifying the
In the tests, the only model that reaches that code is
Let's wrap this up and move this part of the discussion to the next PR/Issue. |
base t_test uses I don't find any search result for |
|
@josef-pkt I think we've reached an agreement on the actual content of this PR. Some of the discussion above is on related topics and can be moved to an Issue. I'll rebase and de-conflict once you give the go-ahead. |
Codecov Report
@@ Coverage Diff @@
## master #3735 +/- ##
==========================================
+ Coverage 78.97% 78.98% +<.01%
==========================================
Files 541 541
Lines 80818 80822 +4
Branches 9185 9185
==========================================
+ Hits 63829 63840 +11
+ Misses 14915 14909 -6
+ Partials 2074 2073 -1
Continue to review full report at Codecov.
|
|
looks good, needs rebase to remove "Merge branch 'master' ..." |
|
Excellent! Really psyched to see this one go through |
Test case to check that model.df_model is not changed on call to fit_regularized
closes #3734