Repository navigation
GSOC2017 Generalized Poisson (GP-P) model - #3727
evgenyzhurko wants to merge 34 commits into
Conversation
|
@josef-pkt what is your opinion about p-param as a separate param? |
| mu = self.predict(params[:-1]) | ||
| return (np.log(mu) + (endog - 1) * np.log(mu + alpha * np.power(mu, p) * endog) - | ||
| np.log(1 + alpha * np.power(mu, p)) - gammaln(endog + 1) - | ||
| (mu + alpha * np.power(mu, p) * endog) / (1 + alpha * np.power(mu, p))) |
There was a problem hiding this comment.
it's useful to create some temporary variables to avoid computing several times the same thing
e.g. alpha * np.power(mu, p) is computed 4 times
There was a problem hiding this comment.
FWIW It looks like this loglikeobs matches the one here under the change of variables
xi = 1 - 1/(1 + alpha * mu**p)
p here bears a strong resemblance to Q in NegativeBinomial._ll_nbin. Is the relationship more than just aesthetic? Does p take on only a particular set of values?
There was a problem hiding this comment.
@josef-pkt You are right, i'll refactor this part
@jbrockmendel There is no a resemblance(only in names). I don't find some articles where GP use p>2 or p<0. In many articles say only about p=1 as GP-1 and p=2 as GP-2.
There was a problem hiding this comment.
The gammaln call is likely to be the slowest part of this function. Since the argument doesn't depend on params, there is some mileage to be gained by caching it.
For that matter, if endog is only taking on positive integer values, then it may be faster to use the
I'll take a look to see if any of this can be useful elsewhere.
Update: I take back the factorial suggestion. Benchmarking it looks like np.log(factorial(x)) is about twice as expensive as gammaln(x)
There was a problem hiding this comment.
@jbrockmendel
gammaln is the best function for this kind of computation.
It has one of the best precision and avoids overflow (and is heavily used in scipy.stats.distributions for this reason).
Also, we don't want to restrict to integers. (for quasi-maximum likelihood, endog could be any non-negative number.
About caching intermediate computation that are invariant in the optimization. This is a bit tricky in general and I think we should skip it in the first version of this PR/GSOC.
#3692 (comment)
(If it turns out that it looks relatively easy for a model, then we could try it out later.)
| super(GeneralizedPoisson, self).__init__(endog, exog, offset=offset, | ||
| exposure=exposure, | ||
| missing=missing, **kwargs) | ||
| self.p = p - 1 |
There was a problem hiding this comment.
it's better to store the argument, or change the argument to what is stored.
using the same name, makes it difficult to recreate a model
(we have currently the problem with exposure which is internally stored as log(exposure), which causes some problems
|
having |
|
Is there another reasonable name for |
|
@jbrockmendel What's about |
|
Either of those work on the name-length front. Ideally the name would also be informative to a reader who isn't familiar with the model (e.g. me). Maybe "overdispersion"? |
|
about naming of |
|
I implemented score function and tried to test it using This code gives the following result: I have checked dl/dalpha and dl/dparams many times, but results not equal. Update: i also tried to compare |
|
try a simple finite difference (Please copy the text from the console and not a picture) |
|
I change |
|
a few comments (I just realized that github didn't process my send a while ago, temporary connection problems)
|
|
another issue (it will be easier when I don't have to guess what you are doing)
The main pattern is the same so it should be easy to switch back and forth between CountModel and GenericLikelihoodModel. |
| offset = None | ||
| mod_poi = NegativeBinomial(self.endog, self.exog, offset=offset) | ||
| start_params = mod_poi.fit(disp=0).params | ||
| start_params = np.append(start_params, 0.1) |
There was a problem hiding this comment.
Does this still need the extra parameter 0.1?
AFAIU, we already have an extra params in NegativeBinomial, and for fixed p we also have only one extra params in Generalized Poisson.
There was a problem hiding this comment.
It's my mistake. I'll generate start_params using Poisson model. In case NegativeBinomial extra parameter 0.1 doesn't need.
Update: I'll change NegativeBinomial to Poisson in the next commit.
|
I tested following code and got results: Is it normally that i got different results in Poisson/GP-p and NB. |
|
If it's a dataset where convergence is difficcult, it is better to start with Nelder-Mead optimization first. Also, you should include a constant in general, otherwise the model might be strongly misspecified as a check whether the convergence makes sense: |
|
I tried different optimization, as a result |
|
You need to add these two lines https://github.com/statsmodels/statsmodels/blob/master/statsmodels/discrete/discrete_model.py#L1956 |
In general the parameters and prediction of the three models should be close to each other because they all have expected E(y |x) = exp( exog * params). If they are not close, then either the model is strongly misspecified or there is a bug. |
|
@josef-pkt but result for Is the analytic hessian can help to solve this problem? |
|
I would leave convergence problems for later, when the basic model is ok. newton is good to improve the estimate in the neighborhood of the optimum. It uses score and hessian, and if newton works locally, then it is a good indicator that score and hessian are fine. |
|
I tried to change method on Poisson model(in generating This way gives well result with all optimization method except |
|
I found the problem with under dispersed test, which was in rvs for genpoisson and not in the Generalized Poisson model. I will check tonight what we need for finalizing and merging the PR. I will likely make a PR against this branch. test coverage is low in a few places. |
|
you should have waited until I rebased my branch. There might be some duplicate commits now in your branch. (I need to check the branch/network tree to see.) |
|
@josef-pkt I can reset this merge if it's will require. |
|
@josef-pkt @evgenyzhurko: I just noticed that the lgtm.com prcheck reports that this PR introduces 7 new alerts. I've had a quick look — they're all true positives (I think), and at least two seem to indicate potential bugs. Some others are related to cyclic dependencies. |
|
@evgenyzhurko Don't change anything right now. I will look again into merging in an hour or so. (I just merged VECM) |
|
@sjvs I think these are general design issues or problems and not specific to this PR, and are not bugs. This PR follows mostly the pattern of the existing discrete models. about circular imports: I saw these LGTM warnings for several cases in master and other PRs. I don't think we had actual problems with this, because api's are not imported by regular code, and AFAIK we don't have circular imports. |
|
@josef-pkt: thanks for confirming! To a noob like me they looked like relevant warnings (especially line 1354 of discrete_model.py about CountModel), but if you've checked them and are happy: 👍 Functionality for disabling queries to suppress certain types of alerts is coming to lgtm.com soon — that may be useful for disabling the circular imports warnings. |
|
@sjvs specific end of line comments to disable some checks would be useful, like pylint. |
|
@evgenyzhurko I think we should rebase and open a new PR for merging, and leave this PR alone to have a full reference to the development. If you know how to do an interactive rebase, then you could open the new PR with partial squashing and rebased on master. Otherwise, I'll do it later today. |
|
@josef-pkt: it's on the roadmap for the next couple of months! |
|
merged rebased version of this in #3795 |
This PR include implementation of Generalized Poisson model
This model include this parts:
Status - merged #3795