Repository navigation
ENH: GLM Tweedie Family #2872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
ENH: GLM Tweedie Family #2872
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bb30c99
ENH: Tweedie!!!
thequackdaddy e2ade37
Rebase to remove freq_weights from residual calcs/
thequackdaddy c7c4382
Anscombe, some some tests, and a really bad estimator for the varianc…
thequackdaddy b517a7e
link isn't an instance yet
thequackdaddy 1dda90e
Floats are problems in 2.7 it seems
thequackdaddy 4d7c4d8
Reworked the estimate... seems stable now
thequackdaddy f7c06a2
Use df_resid in scale estimate
thequackdaddy cb459c5
Test Tweedie Power Formula
thequackdaddy bf32146
Josef's comments and some docs
thequackdaddy d90dce4
Making the deviance look more like Poisson/Gamma for p=1/2
thequackdaddy 1f34058
Rename to estimate_tweedie_power
thequackdaddy 4cf9f05
Fixed some tests
thequackdaddy 6cfcdf8
Adding fair test
thequackdaddy 82fb598
Tweedie sum of series sample...
thequackdaddy 512dccf
Tests... use classmethod and setupClass
thequackdaddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """ | ||
| Private experimental module for miscellaneous Tweedie functions. | ||
|
|
||
| References | ||
| ---------- | ||
|
|
||
| Dunn, Peter K. and Smyth, Gordon K. 2001. Tweedie family densities: methods of | ||
| evaluation. In Proceedings of the 16th International Workshop on | ||
| Statistical Modelling, Odense, Denmark, 2–6 July. | ||
|
|
||
| Jørgensen, B., Demétrio, C.G.B., Kristensen, E., Banta, G.T., Petersen, H.C., | ||
| Delefosse, M.: Bias-corrected Pearson estimating functions for Taylor’s | ||
| power law applied to benthic macrofauna data. Stat. Probab. Lett. 81, | ||
| 749–758 (2011) | ||
|
|
||
| Smyth G.K. and Jørgensen B. 2002. Fitting Tweedie's compound Poisson model to | ||
| insurance claims data: dispersion modelling. ASTIN Bulletin 32: 143–157 | ||
| """ | ||
| import numpy as np | ||
| from scipy._lib._util import _lazywhere as lazywhere | ||
| from scipy.special import gammaln | ||
|
|
||
|
|
||
| def _theta(mu, p): | ||
| return np.where(p == 1, np.log(mu), mu ** (1 - p) / (1 - p)) | ||
|
|
||
|
|
||
| def _alpha(p): | ||
| return (2 - p) / (1 - p) | ||
|
|
||
|
|
||
| def _logWj(y, j, p, phi): | ||
| alpha = _alpha(p) | ||
| logz = (-alpha * np.log(y) + alpha * np.log(p - 1) - (1 - alpha) * | ||
| np.log(phi) - np.log(2 - p)) | ||
| return (j * logz - gammaln(1 + j) - gammaln(-alpha * j)) | ||
|
|
||
|
|
||
| def kappa(mu, p): | ||
| return mu ** (2 - p) / (2 - p) | ||
|
|
||
|
|
||
| @np.vectorize | ||
| def _sumw(y, j_l, j_u, logWmax, p, phi): | ||
| j = np.arange(j_l, j_u + 1) | ||
| sumw = np.sum(np.exp(_logWj(y, j, p, phi) - logWmax)) | ||
| return sumw | ||
|
|
||
|
|
||
| def logW(y, p, phi): | ||
| alpha = _alpha(p) | ||
| jmax = y ** (2 - p) / ((2 - p) * phi) | ||
| logWmax = np.array((1 - alpha) * jmax) | ||
| tol = logWmax - 37 # Machine accuracy for 64 bit. | ||
| j = np.ceil(jmax) | ||
| while (_logWj(y, np.ceil(j), p, phi) > tol).any(): | ||
| j = np.where(_logWj(y, j, p, phi) > tol, j + 1, j) | ||
| j_u = j | ||
| j = np.floor(jmax) | ||
| j = np.where(j > 1, j, 1) | ||
| while (_logWj(y, j, p, phi) > tol).any() and (j > 1).any(): | ||
| j = np.where(_logWj(y, j, p, phi) > tol, j - 1, 1) | ||
| j_l = j | ||
| sumw = _sumw(y, j_l, j_u, logWmax, p, phi) | ||
| return logWmax + np.log(sumw) | ||
|
|
||
|
|
||
| def density_at_zero(y, mu, p, phi): | ||
| return np.exp(-(mu ** (2 - p)) / (phi * (2 - p))) | ||
|
|
||
|
|
||
| def density_otherwise(y, mu, p, phi): | ||
| theta = _theta(mu, p) | ||
| logd = logW(y, p, phi) - np.log(y) + (1 / phi * (y * theta - kappa(mu, p))) | ||
| return np.exp(logd) | ||
|
|
||
|
|
||
| def series_density(y, mu, p, phi): | ||
| density = lazywhere(np.array(y) > 0, | ||
| (y, mu, p, phi), | ||
| f=density_otherwise, | ||
| f2=density_at_zero) | ||
| return density | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| from scipy import stats | ||
| n = stats.poisson.rvs(.1, size=10000000) | ||
| y = stats.gamma.rvs(.1, scale=30000, size=10000000) | ||
| y = n * y | ||
| mu = stats.gamma.rvs(10, scale=30, size=10000000) | ||
| import time | ||
| t = time.time() | ||
| l = series_density(y=y, mu=mu, p=1.5, phi=20) | ||
| print('That took {} seconds'.format(time.time() - t)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't link_power be part of the link instance, instead of family?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh... you stumbled across something I noticed a while back, but never bothered to ask. I was copying from the other classes. I think it should really say
link : a link class, optional.linkis actually instantiated in the__init__function.Reading through statsmodels docs and examples we've always told people to say something like
sm.GLM(endog=y, exog=x, family=sm.families.Gamma(link=sm.families.links.Log))Is that the best approach? Personally, I agree with you it seems more natural to pass an instance of a class, not the class itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't compare this yet with the other classes.
We have some other issues open with the limitations of the current implementation.
I'll check this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. You might also want to look at this line of code. I think we really might want
if link is L.NegativeBinomial:I don't think that this works as currently written. (Mind you, I don't use Negative Binomial, so I'm not 100% sure here.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After an initial browsing through the families and link code:
Your tweedie implementation here follows the same pattern, except that there are no parameterized links yet.
I think this was refactored a long time ago to use only classes instead of instances for links.
I really prefer that we use
link=link.Power(link_power)in the signature but that needs refactoring of the valid link checkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I will proceed with this, then. Perhaps the refactoring can be in a new PR. Hate to break something at this point.