Repository navigation
GSOC2017 Truncated and Hurdle count models - #3802
evgenyzhurko wants to merge 22 commits into
Conversation
| """ | ||
| llf_main = self.model_main.loglikeobs(params) | ||
|
|
||
| pmf = np.zeros_like(self.endog) |
josef-pkt
left a comment
There was a problem hiding this comment.
I made some comments on outsourcing truncation probability cdf.
However, after thinking a bit more, using the cdf instead of the pdf loop might not improve performance if the main use case is zero-truncation or at most a few counts.
| for i in range(self.trunc + 1): | ||
| model = self.model_main_name(np.ones_like(self.endog) * i, | ||
| self.exog) | ||
| pmf += np.exp(model.loglikeobs(params)) |
There was a problem hiding this comment.
this loop could be put into a cdf method that can be overwritten by a subclass.
E.g. poisson should have a build in method (or method delegated to scipy distribution cdf)
| self.exog) | ||
| pmf_i = np.exp(model.loglikeobs(params)) | ||
| score_trunc += (model.score_obs(params).T * pmf_i).T | ||
| pmf += pmf_i |
There was a problem hiding this comment.
I'm not sure whether it will be very useful, but this could be put into a separate cdf_deriv method.
Overall, for the design: we should outsource parts to separate methods, if it is useful to override the method for specific models or distributions that have a analytical or computational more efficient way to calculate those parts.
| self.exog) | ||
| pmf_i = np.exp(model.loglikeobs(params)) | ||
| score_trunc += (model.score_obs(params).T * pmf_i).T | ||
| pmf += pmf_i |
There was a problem hiding this comment.
? now this is cdf and deriv at the same time.
So maybe one method that optionally returns either or both.
(but then there is still hessian_trunc)
|
@josef-pkt I tried to evaluate generic hessian, but it isn't successfully. The main problem that truncated model have variable amount of pmf(depends of truncation value) For now, model works well for different trucation(i tested 0 and 5), results equal to STATA. |
| elif which == 'linear': | ||
| return linpred | ||
| elif which =='prob': | ||
| counts = np.atleast_2d(np.arange(0, np.max(self.endog)+1)) |
There was a problem hiding this comment.
I think starting at zero is good for consistency, even though probabilities below trunc will be zero.
I think we should add an optional count_prob or similarly named argument for this, in analogy to the n parameter in PoissonResults.predict_prob
|
|
||
| """ + base._missing_param_doc} | ||
|
|
||
| def __init__(self, endog, exog, truncation=0, offset=None, |
There was a problem hiding this comment.
truncation argument needs to be added to the docstring.
Specifically, it needs to specify whether the truncation point is in the support or out of support of the distribution.
i.e. pmf(k) = 0 for k < truncation or for k<= truncation?
|
doc strings still need work, several are incomplete |
|
travis test runs fail on python 3 because of a left in py 2 print statement |
|
|
||
| class TestTruncatedPoissonModel(object): | ||
| @classmethod | ||
| def setupClass(cls): |
There was a problem hiding this comment.
please use setup_class to simplfy moving to pytest.
|
|
||
| class TestZeroTruncatedPoissonModel(object): | ||
| @classmethod | ||
| def setupClass(cls): |
|
|
||
| """ + base._missing_param_doc} | ||
|
|
||
| def __init__(self, endog, exog, offset=None, |
There was a problem hiding this comment.
I think we should have two exog as in zero-inflated, one for the zeros and one for y > 0
|
|
||
| y = self.endog | ||
| zero_idx = np.nonzero(y == 0)[0] | ||
| nonzero_idx = np.nonzero(y)[0] |
There was a problem hiding this comment.
to avoid recomputing this, it would be better to put this in __init__ and attach as attributes.
It might be more efficient to also separate endog and exog into the zero and non-zero parts and attach as internal attributes. It would use more permanent memory, but might be faster. (maybe some timing is necessary to see whether it's worth it.) If we have time before the end of GSOC we could some performance checks to see whether and where the speed can be improved, but for now the priority is to get a working and unit tested version.
There was a problem hiding this comment.
actually, the split would be part of the models, since exog don't show up in these methods directly
| missing=missing, **kwargs) | ||
| self.model1 = self.model_name1( | ||
| np.zeros_like(self.endog, dtype=np.float64), self.exog) | ||
| self.model2 = self.model_name2(self.endog, self.exog) |
There was a problem hiding this comment.
shouldn't model2, the truncated model, only get endog and exog for non-zero endog observations (y > 0)?
y = 0 should not be an observation in the support of the zero-truncated model.
or is this not imposed at this point, i.e. in the generic setting?
|
@josef-pkt Do you have any idea how i can implement result class? |
|
This pull request introduces 32 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
This pull request introduces 34 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
This pull request introduces 34 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
This pull request introduces 37 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
This pull request introduces 34 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
This pull request introduces 33 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
This pull request introduces 38 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
@evgenyzhurko I don't think I have time to go through this in detail within the next few weeks (if we want to get the release out). main question: What did you use as references for this, especially for the censored model and the hurdle model implemented through censored + truncated? I just found an older Stata package http://www.stata-journal.com/article.html?article=st0219 and just realised that Stata 14 includes |
|
@josef-pkt You are right, that you need to implement Generic model with the same censoring param like in the truncation model. |
|
I did a brief check on the literature I haven't checked Winkelman's textbook yet, which is also in the Stata references for cpoisson. One possibility would be to consider the current Censored models as additional binary models, and allow Hurdle to use either the BinaryCensored or a binary model, Logit, Probit, for the zeros. (Another obvious extension would be to allow Hurdle models to specify a larger than zero transition point, so we combine a general Censored with a general Truncated count model, i.e. stitch together two count models. I haven't seen this in any reference but it would be just one additional parameter in hurdle models when the general Censored count models are available.) |
| llf_main = self.model_main.loglikeobs(params) | ||
|
|
||
| llf = np.concatenate((llf_main[self.zero_idx], | ||
| np.log(1 - np.exp(llf_main[self.nonzero_idx])))) |
There was a problem hiding this comment.
as explanation:
np.exp(llf_main[self.nonzero_idx])) is the probability for y=0 given x, because the model_main has endog=zeros
|
I'm trying to rebase this on current main merge conflict are very difficult |
This PR include following models:
a. Poisson - Poisson
b. Poisson - Negative Binomial
c. Negative Binomial - Negative Binomial
d. Negative Binomial - Poisson
Each model include this parts:
Status: - reviewing, need to improve tests for this models.
All this model include in a single PR, because hurdle model based on truncated and censored model.
Last commit before GSoC final evaluation: Implemented Censored Genralized Poisson model