Repository navigation
ENH: Truncated, hurdle count model rebased - #7973
Conversation
|
Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2022-01-21 19:05:00 UTC |
|
my experimental truncation class in #7922 (comment) The test with this PR for TestZeroTruncatedPoissonModel also passes I also get with my class a bit different params at low=6, but I get a closer match to the reference numbers with low=5. |
|
This pull request introduces 5 alerts when merging 0f2328e into 590384e - view on LGTM.com new alerts:
|
|
and I think pytest runs |
|
Iirc or runs it once per worker which is 2 on CI. There is a switch that tried to parallelize by keeping test files on the same worker. Not sure if we use that or not. |
|
most likely I was mistaken, |
0f2328e to
22332e5
Compare
|
amended last commit to fix remaining errors when running unit tests warnings are RuntimeWarnings (zero division, invalid values, ...) and Convergence warnings mainly in fit_regularized about numeric failures update when I switch to test_truncated_model.TestTruncatedNBP_predict test_var fails by a large amount, 5.5 instead of 15.0, |
|
This pull request introduces 5 alerts when merging 22332e5 into 590384e - view on LGTM.com new alerts:
|
22332e5 to
47ea32c
Compare
|
my git has changed, no automatic trailing whitespace cleanup anymore during rebase this only includes unit test for truncated poisson and truncated NBP. I deleted the Hurdle model combines Censored for zeros and Truncated model for y>0 I made a comment in the original PR to rename Censored or privatize it, because it hardcodes the censoring location y=0 versus y>0. |
| exposure=exposure, | ||
| offset=offset) | ||
| self.model_dist = truncatedpoisson | ||
| self.result = TruncatedPoissonResults |
There was a problem hiding this comment.
these should have class in the name as other count models
it looks like we only have it in ZI models self.result_class ,...
Note: |
|
This pull request introduces 5 alerts when merging 47ea32c into 590384e - view on LGTM.com new alerts:
|
|
|
||
| pmf = np.zeros_like(self.endog, dtype=np.float64) | ||
| score_trunc = np.zeros_like(score_main, dtype=np.float64) | ||
| for i in range(self.trunc + 1): |
There was a problem hiding this comment.
this should use cdf when available, i.e. Poisson and NBP
instead of loop
|
There is still much work to do, and to read up on the models. (I don't know the formulas, e.g. for predict or loglike for hurdle) We want to include the hurdle model as counterpart to zero-inflated models. I renamed the module to truncated_model, something more with "hurdle" would have been more appropriate. One question is whether making Truncated models here public or whether to keep those class names reserved for more general versions. on possibility would be to add "FT" to the class name for "fixed truncation". Then we can still optimize the compuations for this case. Censored is too specialized to use those class names. https://data.princeton.edu/wws509/notes/countmoments |
| cov_kwds = {} | ||
|
|
||
| result._get_robustcov_results(cov_type=cov_type, | ||
| use_self=True, use_t=use_t, **cov_kwds) |
There was a problem hiding this comment.
this shouldn't be needed here.
The call to super in results class should handle this already
|
BUG: truncated models, from summary of a simple example (simulated poisson data, for nobs=5000, example is truncation=1) No. Observations: 3080 as a consequence wald_test with use_f=True is wrong.
|
|
prob plot doesn't look correct for truncated poisson prob plot for zero-truncated seems to use truncated sample for freq, |
|
question: Is negbin dispersion parameter identified in the constant only zero-model ? In some runs with constant only negbin-negbin hurdle I get non-invertible hessian. (just guessing, I haven't looked for references for this.) Aside: current code uses full count data for computation of start_params, so this will be standard negbin which is identified. seems to be the problem update Mullahy 1986 only looks at poisson and geometric hurdle with both submodels from the same distribution. update if |
|
Even in a model with a continuous exog, [constant, trend], the negbin zero model still has problems, still not well identified. |
|
aside currently I have these two, "prob-main" (1 - "prob-zero") and "prob-trunc" only separately This gives directly a measure about excess zeros. |
|
Given the identification problems in the negbin zero model, the null model will have unidentified parameters, even when the full model does have enough variation. Default null is constant(s) only model. In the Hurdle case, this means no all slope coefficients are zero, but we still have constant zero-modification. Another "null" model would be no slope effects and no zero-modification. i.e. simple Negbin with constant only. Another possible case will be with score_test that has constant zero-modification as alternative, eg. |
|
This pull request introduces 6 alerts when merging e78dbb0 into 55f9e73 - view on LGTM.com new alerts:
|
|
starting with renamings Hurdle is also too generic if we add it to the statsmodels.api I'm renaming |
| else: | ||
| counts = np.atleast_2d(np.arange(0, np.max(self.endog)+1)) | ||
| mu = self.predict(params, exog=exog, exposure=np.exp(exposure), | ||
| offset=offset, which="mean-main")[:, None] |
There was a problem hiding this comment.
mu can be computed without calling predict again
| offset=offset, which="prob", y_values=counts)[:, None] | ||
| return probs | ||
| elif which == 'var': | ||
| mu = np.exp(linpred) |
There was a problem hiding this comment.
add special cases, truncation=0 as in which=mean
|
This pull request introduces 6 alerts when merging f6fd400 into ad64444 - view on LGTM.com new alerts:
|
|
NBP (and GPP) truncated and hurdle models still have a bug with recreating model, hurdle has two, both hurdle and truncated models currently only attach p/parameterization in submodel(s). |
| mnc2_tregion = (np.arange(self.truncation + 1)**2 * | ||
| probs).sum(1) | ||
| p = self.model_main.parameterization | ||
| vm = mu * (1 + alpha * mu**(p-1)) |
There was a problem hiding this comment.
this is distribution specific -> outsource
| mu = np.exp(linpred) | ||
| if self.truncation == 0: | ||
| p = self.model_main.parameterization | ||
| prob_zero = (1 + alpha * mu**(p-1))**(- 1 / alpha) |
There was a problem hiding this comment.
prob_zero is now available in base model
| mean = (mu - mean_tregion) / (1 - prob_tregion) | ||
| mnc2_tregion = (np.arange(self.truncation + 1)**2 * | ||
| probs).sum(1) | ||
| mnc2 = (mu**2 + mu - mnc2_tregion) / (1 - prob_tregion) |
There was a problem hiding this comment.
mu here is poisson variance, distribution specific
f6fd400 to
6ad39f0
Compare
|
naming inconsistency The definition in hurdle is in analogy to the zero-inflated model, I guess I need a better name for truncated models. "prob-base" ? referring to the |
|
This pull request introduces 4 alerts when merging 6ad39f0 into ad64444 - view on LGTM.com new alerts:
|
|
merging |

rebased on main.
it's working, but unit tests fail, and some merge conflict resolutions might be incorrect