Repository navigation
ENH: GLM Tweedie Family - #2872
Conversation
|
Getting closer... not there yet. Working residual isn't working for 1 of my test cases, don't have anscombe. I skimmed through the Tweedie LLF R function and was quite confused exactly how that function worked. Gonna take a look at it again. I'd like to have loglikelihoods, but deviance is better than nuthin'. |
|
@thequackdaddy We are not allowed to translate GPL licensed code! GPL is incompatible for use in a BSD licensed package. |
|
@josef-pkt Yes, I realized that after the fact. That copied code was removed. Instead, I used the deviance function and calculated the limit of the function as p-> 1 and p-> 2. The answer appears to be the same. Also, I think its slightly shorter, so that's nice. Apologies for the confusion. |
|
This looks already very good based on the passing tests. Note, if loglike is too difficult to implement right now, then we should raise an exception in the I doubt I have time to read it carefully today. I only briefly browsed the code yesterday. |
|
@josef-pkt Great. Any thoughts on anscombe residuals? I'll confess I don't use them much so I have little idea how to interpret them. I generally use pearson/deviance. Let me know if you can figure out the formula. Also, if you could get them from stata (or R if that's possible), I'd appreciate it. |
|
anscombe residuals are family specific formulas (where I never looked at the details) |
|
no luck on anscombe residuals in a bit of search |
|
@josef-pkt This may belong is a separate issue, but I couldn't find anything. Question about "working residuals", as far as I can tell, this appears to be an "r-specific" thing. I can't find any literature talking about this concept. The best I can find is here which suggests they are simply the residuals from the last IRLS fit. R appears to define them as However, we define working residuals as and basically the inverse. Specifically, I think we do The point of all this is that I remain unable to match R's working residuals, although I think that's just more of a result of the way R does the residuals vs. the way we do them. With that said, the working residuals appears to have been used many times before in testing. I suppose that the working residuals are identical when you have the identity link function, but under any any function, my guess is they'd be different. Alternatively, it could just be there's no clear definition of the concept. With that said, intuitively, So anyways, I'm confused by this. |
|
I never looked at working residuals. However, I would interpret the Stata manual as |
|
@josef-pkt Ok after looking at this more... 1.) I remain fairly confident that the way we are doing it now is incorrect. (Perhaps stata had a bug in their old version of their code and its now more apparent?) With 2) in mind, I seem to recall that you began leaning (towards the end of my commits for Our algorithm does not match Stata's documentation nor the R documentation. Perhaps Stata had a bug which is now fixed? I can't test as I don't have it. I can match R's working residuals perfectly if I just multiply instead of divide. |
|
I have Stata, but need to clean out previous work before I can run it on an example for this. Yes, I think it's better to leave the weights out of the residuals (remove them again). |
|
I get different working residuals than those in the test suite after running the cpunish poisson glm example in Stata. Also, those working residuals in the test suite are much larger than any of the other residual. @jseabold Do you remember why the working residuals might be wrong? I don't find any changelog or bug reports for Stata, so I cannot check what it looked like before Stata 12 |
|
I think I have anscombe residuals. which should specialize to Poisson with p=1 and gamma for p=2, gaussian for p=1 (I didn't try to check the limit as p -> 3 in the derivation.) |
|
Going back to Anscombe... do you mean this? def resid_anscombe(self, endog, mu):
"""
The Anscombe residuals for the Tweedie family
Parameters
----------
endog : array-like
Endogenous response variable
mu : array-like
Fitted mean response variable
Returns
-------
resid_anscombe : array
The Anscombe residuals as defined below.
Notes
-----
Not yet...
"""
c = (3 - self.link_power) / 3
if self.link_power == 3:
return (np.log(endog) - np.log(c)) / mu ** 2
else:
return c * (endog ** c - mu ** c) / mu ** (self.link_power / 6)I don't understand |
|
Ignore this last comment... In mixing var and link power... |
|
No, I think this has to be the variance power. |
|
I think I have a type there, if c=3, the expression should have mu not c i.e. residuals of log transformed endog and fittedvalues. |
|
yes, InverseGaussian family class has anscombe BTW: np.log(0) is -inf There are problems if endog=0, but inverse gaussian is continuous with support endog > 0, AFAIK |
|
Back to anscombe... Is there another typo in this? For inverse gaussian, I think we want sqrt(mu) in the denominator. For the other cases, I think we want to multiply by (1 / c), not by c. So something like this... if self.var_power == 3:
return (np.log(endog) - np.log(mu)) / np.sqrt(mu)
else:
c = (3 - self.var_power) / 3
return (1 / c) * (endog ** c - mu ** c) / mu ** (self.var_power / 6)This should become... Edit - Messed up link_power vs. var_power again. |
|
What you have now looks correct. |
|
I'm totally with you on unit tests... I've found them extremely helpful. That's actually how I found this. We should be able to validate that Tweedie's residuals match for p == 0, 1, 2, and 3. I guess we'll have to assume that the other values would match as well cause I don't have a source for any other p. (Specifically, I can't get anscombe residuals from R. I did find This package called "surveillance" which advertises anscombe residuals, but they appear to be poisson-specific and then they are standardized (for reasons I don't understand). Thanks. |
|
I think if anscombe residuals agree with the special cases, then there is no mistake left in the formula, and it's sufficient for testing (only p=3 is a special case, all others use the same formula). |
|
Ok I'm inching towards the finish line here... I added the fair test, and we're all green. As a side note, I tried to also compare the results of model = sm.GLM(endog=data.endog, exog=data.exog[['rate_marriage', 'age', 'yrs_married']], family=sm.families.Tweedie(var_power=1.5, link_power=0))
res = model.fit()
model.estimate_tweedie_power(res.mu)1.763 model2 <- tweedie.profile(affairs ~ rate_marriage + age + yrs_married - 1, data=data, link.power=0, p.vec=seq(1.1, 1.9, .1),
do.plot = TRUE)
model2$p.max1.52449 |
|
sounds good, in general: I will look at the latest changes, but also think that we should merge this soon. Further enhancements can be done in future PRs. Aside: |
|
R's I will commit add the gist of the calculation that I provided you as the function you requested. I played with this a little more, but haven't looked at it in a few weeks... and have forgotten much of how it works. It was quite slow (obviously) but there's probably optimizations that are possible with some patience. I won't be adding any tests for the code as I don't really have anything great to check against. If you want to add the parameter transformation so we can do some MC sampling, I think it sounds like that's going to be in a future PR for this or do you want included in this PR? After you are happy with this, I'm happy to squash. Thanks so much Josef. |
|
I think this is a good place to stop and merge, after you added the helper function. The deviance looks good now, I didn't look at the other things again. As followup, it would be good to have a notebook that explains a bit the new family, maybe with both test cases (just copied over). AFAIK, we don't have the changelog ready right now, but we will need one or a few paragraph announcing it. As further followup, we can also check to see if we have all possible results, given that we don't have loglike. Somewhere we need to check or mention that tweedie can only be estimated by irls and not with the scipy optimizers. (It would be possible to minimize deviance, but the class hierarchy is not set up for maximizing anything else than loglike.) |
|
|
||
|
|
||
| class TestTweediePower15(CheckTweedie): | ||
| def __init__(self): |
There was a problem hiding this comment.
convert all the __init__ to a classmethod setup_class
check other tests for the format
this "should" speed up the unit tests because nose runs __init__ separately for each method of the class, i.e. this currently estimates the same thing seven times.
|
I hadn't realized that you already rebase, looks good, and I don't think it needs any squashing. However, this adds about a minute or a bit more to the test run on TravisCI. |
|
Yeah I rebased because I think there was conda package update that made it so my build wouldn't work... I noticed you were fighting with it, so I rebased and viola, problem solved. I'm now using setupClass when I can. Builds on my machine... let's see what travis thinks. |
| from statsmodels.datasets.cpunish import load_pandas | ||
| self.data = load_pandas() | ||
| self.exog = self.data.exog[['INCOME', 'SOUTH']] | ||
| self.endog = self.data.endog |
There was a problem hiding this comment.
for next time:
one possible trick is to leave these in a method in the super class and call the method directly, which works (IIRC) if the method in the superclass is also a classmethod
(could be a setupClass or a getdata method in super)
super call needs the class twice, IIRC, e.g. super(TestTweediePower2, TestTweediePower2).get_data()
("IIRC" means it works but I never remember the details and have to look them up from other examples.)
There was a problem hiding this comment.
Ahhh...
I thought about a super but I couldn't get that to work (in about 2 tries)... Didn't attempt this way.
Thanks for the tip.
|
all green, only a little bit faster with the latest versions, and 1 minute or so faster on the older version. Ready to merge? From my side it looks ready. |
|
I'm happy if you are. I'm thinking my next topic is reviewing #2915. Let me re-read some of the papers. The full log likelihood will have to wait for another day. |
|
It's merged. Thanks @thequackdaddy |
Hello,
This will (eventually) resolve #2858 and part of #2804 .
This adds Tweedie to the family of distributions.
A few shortcomings...
So let's get rockin.
Updated 3/28/2016