Repository navigation
Conversation
|
the last commit I could split out GLM score_test so that we can add diagnostic tests for GLM without the discrete changes. Or merge this essentially as is with a lot of code duplication. How good is unit test coverage for score tests ? |
Codecov Report
@@ Coverage Diff @@
## master #5711 +/- ##
==========================================
- Coverage 82.22% 82.21% -0.01%
==========================================
Files 593 593
Lines 93716 93952 +236
Branches 10353 10370 +17
==========================================
+ Hits 77058 77245 +187
- Misses 14256 14293 +37
- Partials 2402 2414 +12
Continue to review full report at Codecov.
|
| dparams = dmudb * (-a4 / a1 + | ||
| a3 * a2 / (a1 ** 2) + | ||
| (1 + a4) * ((y - 1) / a2 - 1 / a1) + | ||
| 1 / mu) |
There was a problem hiding this comment.
-
1/muterm factors out and when multiplied bydmudbis just 1 - Substituting in
a4=a3*yI think will make it easier to identify cancellable terms, as well as terms common between this anddalpha
There was a problem hiding this comment.
Musing: I'm confident Mathematica or something has a maximally-simplify command, but that isn't what we want here. We want some convex combination of maximally-simplify and maximal-readability/interprebility. I wonder if there is a tool for this.
|
|
||
| def hessian_factor(self, params): | ||
| """ | ||
| Generalized Poisson model Hessian matrix of the loglikelihood |
There was a problem hiding this comment.
I'm not clear on how this is different from just hessian. My understanding of hessian_factor is it gives the X such that exog.dot(X).dot(exog) gives hessian or something like that.
There was a problem hiding this comment.
hessian factor is 1-D, and can also be multiplied with other exog than the ones in the model, which is what I use in the score_test and conditional moment tests
|
Formatting comments are already in the old PR |
|
|
||
| def score_factor(self, params): | ||
| """ | ||
| Logit model derivative of the log-likelihood with respect to linpred |
There was a problem hiding this comment.
same comment as with hessian_factor above
There was a problem hiding this comment.
similar answer
score_factor is 1-D and can be multiplied by any "instruments" or other exog not in the model.
|
|
||
| @classmethod | ||
| def setup_class(cls): | ||
| cls.idx = slice(None) |
There was a problem hiding this comment.
if this isn't overriden elsewhere, can you set it at the class level outside of setup_class? These are easier to reason about when they are less stateful
There was a problem hiding this comment.
defensive programming,
There is no requirement that it is not changed somewhere, and I don't have to think about inheritance for class attributes when adding subclasses.
|
|
||
| # not used to match Stata for HC | ||
| # nobs, k_params = mod1.exog.shape | ||
| # k_params -= 1 # one constraint |
|
|
||
|
|
||
| def check_jac(self): | ||
| # moved from CheckModelResults |
There was a problem hiding this comment.
why moved? If it takes self, this should probably be a method in a mixin class
| #basic cross check | ||
| jacsum = self.res1.model.score_obs(self.res1.params).sum(0) | ||
| score = self.res1.model.score(self.res1.params) | ||
| assert_almost_equal(jacsum, score, DECIMAL_9) #Poisson has low precision ? |
There was a problem hiding this comment.
space after #. Question mark suggests this probably merits a TODO:
| assert_allclose(hess1, hess2, rtol=1e-10) | ||
|
|
||
| if isinstance(res2.model, OLS): | ||
| # skip the rest |
There was a problem hiding this comment.
half a sentence on why OLS gets skipped?
There was a problem hiding this comment.
OLS does not have score_factor, hessian_factor
only GLM and now some discrete
There was a problem hiding this comment.
That will make a great addition to the comment here
| rtol=0.01) | ||
|
|
||
| def test_jac(self): | ||
| self.res1 = self.res |
There was a problem hiding this comment.
Modifying self inplace here is a code smell. Luckily there is a solution that goes hand-in-hand with the refactoring-out of check_jac: that function only needs res1, can get rid of the self argument and just pass res1=self.res and knock out two smells at once.
|
a quick monte carlo based on my notebook example, Poisson cases are 1000 replication, sample size 1000 so
The other cases have mean_misspecified=True and should reject, which all cases do. effects of misspecification and power are large because the misspecification and deviation from null are large in the data generating model and sample size 1000 is very large for a test about mean functions. |
| hessian = -np.dot(ex.T * hessian_factor, ex) | ||
| # see #4714 | ||
| from statsmodels.genmod.generalized_linear_model import GLM | ||
| if isinstance(model, GLM): |
There was a problem hiding this comment.
A better approach would be the have models defind an attribute, e.g. hessain_factor that would be 1 by default and could be overridden when necessary, e.g., by GLM, rather than encodign model-class-specific information in a base function.
There was a problem hiding this comment.
That would be better if this were a permanent feature.
However, I hope that we can make this consistent across models and then we just need to delete these lines here.
I have not checked yet how this works in other models like OLS/WLS and RLM, or models in survival.
|
|
||
| # this is a copy from stats._diagnostic_other | ||
| # this is a copy from stats._diagnostic_other to avoid circular imports | ||
| def _lm_robust(score, constraint_matrix, score_deriv_inv, cov_score, |
There was a problem hiding this comment.
This copy should be removed and it should be imported right before first use, with a note explaining why it is imported there.
There was a problem hiding this comment.
Or just don’t remove the import of disgnostic_other at the top of the file
| hypothesis='joint', cov_type=None, cov_kwds=None, | ||
| k_constraints=None, observed=True): | ||
|
|
||
| res = infer.score_test(self, exog_extra=exog_extra, |
There was a problem hiding this comment.
the original GLM.score_test is not used anymore?
The new version allows for some robust sandwich forms
|
I'm going to split out the changes to non Poisson count models so I can get the main changes including Logit ready for merge. count models should use cut and paste instead of copy-paste for score and hessian factors |
|
how much of this is included in #7166 or other PRs? needs rebase to check what the conflicts with main are. |
| X = self.exog | ||
| L = np.exp(np.dot(X,params) + exposure + offset) | ||
| return L | ||
| return -L |
There was a problem hiding this comment.
why does this change the sign?
(it's in one of the merge conflicts, and I'm not sure what's correct)
|
trying to rebase this on either main or #7597, the first commit here has a large number of merge conflicts in discrete_model.py strange thing is change in sign of hessian. Is there a bug? in main and 7597, Poisson hessian and hessian_factor have opposite signs. same for others GLM, Logit, glm score and score_factor have the same sign. base._parameter_inference.score_test for variable addition exog_extra assumes that the negative sign is in hessian function, but not in hessian_factor so we shouldn't flip sign as in this PR, unless we redifine hessian_factor for all models. |
|
This is going to have a lot of merge conflicts. Maybe I should skip the first commit, or large parts of it. e.g. the copy paste in GPP and NB. Logit score_factor will already be in master. Unit tests in first commit still look useful. |
|
merged in #7825 |
This is a rebased version of #4716
changes to order of import statements got lost in merge conflict resolution
otherwise unchanged
see #4716 for notes, notebook for application of score test https://gist.github.com/josef-pkt/f20abdbd360c2e549f73e98844f5d2c4
ENH:
count models already have offset
MNLogit does not have offset
ZeroInflated models don't have offset for the binary model, but have it already for the count submodel
DiscreteResults.score_test will not work for all models that inherit from it.
Which models work currently? Which are unit tested?
Unit Tests:
statsmodels/discrete/tests/test_sandwich_cov.pyCheckDiscreteGLMcovered: score_test, score_factor, including binary models with offset
models: Poisson, Logit, Probit, Gauss (is skipped in new assert because methods are not available in OLS)
statsmodels/discrete/tests/test_constrained.pynew Logit subclasses for comparing with GLM
CheckGLMConstrainedMixintests fit_constrained, and implicilty offset (I guess)
statsmodels/discrete/tests/test_discrete.pycompare score_factor, hessian_factor with existing methods,
Warning: this will not test anything when the code duplication is removed.
CheckModelResults.test_jacis inherited by test classes for most models.NO new tests for verifying correctness of
score_test(unit tests check only that GLM and discrete models produce the same results up to tolerance)