Repository navigation
BUG/ENH: Tweedie parameterization and better handling of exposure, inclusion of 'perry' and 'taylor' methods. - #3079
thequackdaddy wants to merge 5 commits into
Conversation
| # Already logged | ||
| exposure = np.exp(self.exposure) | ||
| elif exposure is None: | ||
| exposure = np.ones(len(self.endog)) |
There was a problem hiding this comment.
I think exposure = 1. should work correctly with broadcasting in the following.
|
If there are descriptive names available, then we try to avoid using author names. There might be a wls version with weights/var_weights depending on mu to get something similar to glm-gamma, assuming known variance of residual**2. (maybe in future) |
| elif method == 'taylor': | ||
| from statsmodels.regression.linear_model import WLS | ||
|
|
||
| y = np.log(np.power(endog2 - mu2, 2)) |
There was a problem hiding this comment.
my guess is that we need to clip endog2 - mu2 to be larger than zero. Somebody will come up with a case with zero residuals. I'm not sure what's a good threshold, 1e-6?
8be1e22 to
b68d653
Compare
|
I put together a gist of what I think you mean by a monte-carlo simulation. The implied p that I'd like to calculate is ~1.76, however, I'm getting ~1.95. Didn't spend a lot of time on this, so could be a problem lurking somewhere. The p-values are very, very low, so perhaps I need to make the model not as good? |
|
@thequackdaddy I have not yet tried to catch up with this and related issues. Is there a bug somewhere that should be fixed for statsmodels 0.8, or can this wait for 0.9? |
|
@josef-pkt Sorry for not getting back to you earlier. I'm working on some work things and haven't had time to commit anything. I think there is a bug if you are working with I'm happy to have this wait until 0.9. I wrote a Notebook, by the way, that uses the series approximation method (like the R dtweedie package). It works very well and runs reasonably quickly on my datasets. Unfortunately, that's on my work laptop. I'll try to clean it up and upload it as a gist. So feel free to leave as-is and we'll clean up later. If you have free time to review the notebook once its ready, I'd appreciate it. I really need someone much smarter than me to buy-in before I believe what I've done myself. On a side note, the Tweedie-family functionality that we've programmed so far works very well for my models. I'm hoping to have management approve gen1 in the next few weeks with production following a few months later. I've basically assumed a Tweedie variance power parameter of 1.6 based on the CV of the size of loss distributions. The models validate extremely well and far, far outperform our current programs, so that's exciting. The bad news (from my perspective) is that with significant performance gains, that means many customers will see large increases (and some will see decreases), so management will have some heartburn from that. That's more an implementation question than a model question, however. My department has increased staffing and after some new people are trained I'm hoping maybe I can be more active. |
|
After experimenting with this some more, I'm really now leaning towards thinking that the BrentQ/psi method might only really work in some limited situations. [I made this gist)[https://gist.github.com/thequackdaddy/25a32272e1219973eab308948ab89a2a] showing the two methods on a random dataset I generated. The SeriesApproximation method works extremely well--the optimum p-values from the scipy The psi_p value is all over the map. @josef-pkt - At one point, you suggested that you were able to get the psi_p method to approxmiate very well. Do you have that code? I'm wondering if I've screwed something up. (Not unlikely). |
|
AFAIR, using the GLM-gamma worked well. (I'm trying to finish release preparation, and might not look closely very soon.) |
b68d653 to
b56fec7
Compare
|
@josef-pkt As mentioned in the other thread, I think this works pretty well. Would you have some time to work through this with me :) |
|
Not really actively working on this anymore as I think its pretty much good enough for me for now. Comparing to R's tweedie package, there's also qtweedie and ptweedie which return the quantiles and cumulative distribution (Pr(X <= x)) which would be very useful for q-q plots. I can't seem to find literature on that though. Having that would allow me to run q-q plots--which I've never done on Tweedie but I found a stackexchange explanation of. Seems do-able... good wishlist item. |
b56fec7 to
c528d32
Compare
|
Since exploring with |
Codecov Report
@@ Coverage Diff @@
## master #3079 +/- ##
==========================================
+ Coverage 79.48% 79.73% +0.25%
==========================================
Files 558 562 +4
Lines 83455 84919 +1464
Branches 9538 9808 +270
==========================================
+ Hits 66331 67713 +1382
- Misses 14951 14986 +35
- Partials 2173 2220 +47
Continue to review full report at Codecov.
|
|
I sat down to work through this again and I think I hit something of a breakthrough. I re-did the method to essentially:
I have a gist outlining how this works below. Still not totally ready. I will try this technique using real data (right now... the data is simulated) so perhaps that will give me a better judgement of if this is truly working. |
|
I should add that I need to see how accurate/inaccurate this all is if there are |
1 similar comment
| scale /= self.var_weights | ||
|
|
||
| def psi_p(power): | ||
| psi = ((self.endog - mu) ** 2 / (scale * mu ** power) - 1) |
There was a problem hiding this comment.
var_weights always needs to go in in the same way as inverse scale (1 / scale), i.e. line 806 above
so, this looks fine to me.
| model = GLM(y, x, freq_weights=weights, | ||
| family=families.Gamma(families.links.log)) | ||
| res = model.fit() | ||
| scale, power = res.params |
There was a problem hiding this comment.
res.params here is in log scale, i.e. links.log
Don't you need to rescale params? Or is this implied/correctly parameterized in model?
| x = np.column_stack((np.ones(len(self.endog)), np.log(mu))) | ||
| model = WLS(endog=y, exog=x, weights=weights) | ||
| res = model.fit() | ||
| scale, power = res.params |
There was a problem hiding this comment.
here there is no log-link, so I think either this or perry/glm has the wrong parameterization.
?
There was a problem hiding this comment.
correction: endog here is loged
|
I don't want to take the time right now to go back into the optimization problems here with the power parameter. The gist also has runtime warnings that we might be able to work around by clipping or restricting to valid ranges. (I didn't check, and don't remember running into it when I was playing with Tweedie.) Given that this is just a helper method and won't directly affect other code, I think we can merge if you think it works better than the current version. (Based on my earlier experiments, it might be necessary to use some global optimizer to work around multiple roots. I'm not sure about that because we don't have a loglike to rank roots/zeros. Aside: Why does the precision of the var_power matter? Mean parameters or glm tweedie should be fine even if tweedie var power is off by a bit (?). Maybe we can use other, higher moments or quantiles or ... to better match the power parameter. E.g. like trying to find the appropriate count distribution after we have more of them.) |
…clusion of 'perry' and 'taylor' methods.
…fference between initial and final p.
c7c3260 to
bef9d90
Compare
Hello,
See #2915 for the cause of this.
(I just realized that you probably wanted me to name the method 'gamma-glm', but I named it 'perry'. I also made a 'taylor' which is supposed to be WLS version of the same thing. If you want me to rename, let me know.)
I'm not sure this totally works. I first used the
fairdataset to see how well/poorly this works (I generated sample weights, exposures. On a scale of 1-10, I'd give that a 7 as far as how good it felt.taylorandperrywere very close, but the rest was quite different.When I ran this on my real dataset, my confidence fell to a 2. I'm getting much different estimates for p using all the different methods. So I might need to think this out more.
On the bright side, all the methods feel like converge with a guess-and-check approach. So that keeps it above a 1.