Skip to content

ENH: Revise loglike/deviance to correctly handle scale - #3856

Merged
josef-pkt merged 16 commits into
statsmodels:masterfrom
thequackdaddy:scale_fix
Jan 9, 2018
Merged

josef-pkt merged 16 commits into
statsmodels:masterfrom
thequackdaddy:scale_fix

Conversation

@thequackdaddy

Copy link
Copy Markdown
Member

xref #3773

I apologize for this taking longer than had hoped, but my busy period was exacerbated by some unforeseen events. But I'm somewhat back now.

This is the first step to solving the above issue.

Still a lot is missing... I've only really gotten loglike to work for all the families except Gamma, Binomial, Gaussian, and Tweedie (which has never had loglike). I also need to re-work the docstrings. I'm thinking its more logical to have a loglike_obs function called in each family and then have a loglike function in the Family class so that loglike will simply be inherited. I think you the doctrings could be elegantly written to handle this too.

I'm thinking I will work on deviance next and then ciricle back to loglike... I feel like R takes some computational shortcuts...

@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 1 alert and fixes 1 - view on lgtm.com

new alerts:

  • 1 for Unreachable code

fixed alerts:

  • 1 for Wrong number of arguments in a call

Comment posted by lgtm.com

@thequackdaddy

Copy link
Copy Markdown
Member Author

Lunch does marvelous thing. I think all but Gamma and Tweedie are working now.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 1 alert - view on lgtm.com

new alerts:

  • 1 for Unreachable code

Comment posted by lgtm.com

@thequackdaddy

Copy link
Copy Markdown
Member Author

I have most of this working, I think. Deviance and loglike seem good for pretty much everything except loglike for Gaussian. Also, I'm not totally convinced that my binomial loglike is working.

I think the problem lies in this special case that I've never really understood (other than to match stata, I think).

I also need to work on the docstrings... I'll play with that later...

@thequackdaddy
thequackdaddy force-pushed the scale_fix branch 3 times, most recently from dbb7f01 to d6b62d8 Compare August 6, 2017 23:24
@thequackdaddy

Copy link
Copy Markdown
Member Author

OK I realized that with ordinary least squares, its a rather simple adjustment... scale needs to be divided by nobs and not df_resid. This brings up the problem that this doesn't currently correctly handle when freq_weights use classical OLS.

I worked through the docstrings with some drafts.... lotsa changes so there's likely lotsa problems. For NegativeBinomial, I realized R (and possibly stata) have different default scale behaviors.

I probably need to proofread this. I believe this latest commit will pass all unittests. Considering I had to adjust the unittests to make this happen, I'm not sure how valid this truly is.

I'll probably sleep on this progress.

@coveralls

coveralls commented Aug 6, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.006%) to 81.253% when pulling d6b62d8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

@coveralls

coveralls commented Aug 7, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.006%) to 81.253% when pulling d6b62d8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.006%) to 81.253% when pulling d6b62d8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.006%) to 81.253% when pulling d6b62d8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

@thequackdaddy

Copy link
Copy Markdown
Member Author

I'd like to re-approach how we do documentation/structure of family.py, just to help keep this cleaner. I have an idea I'm borrowing from scipy.

I've come to like the way that scipy.dists defines the distributions. There is a basic class e.g., rv_continuous with methods like pdf, cdf., etc. All you have to do is define private functions _pdf and _cdf and write some documentation at the class level and inherit rv_continuous and poof, you've got yourself a new distribution class.

What I'd propose is that we move all the documentation for each family to the class level and have Family define deviance, loglike, loglike_obs, resid_dev, and resid_anscombe. These methods would call privately defined methods in the subclass. Each individual family would just need _loglike_obs, _resid_dev, and _resid_anscombe defined as well as class-level documentation. If you wanted documentation for the distribution (e.g., the loglikelihood formula), you'd get it from that distribution's class-level documentation (e.g., Poisson). If you wanted the documentation for the methods (e.g., Poisson.loglike), you'd get it would just be standard documentation from the Family.loglike method.

@josef-pkt Any thoughts? Maybe do this in another PR? I think it would make maintaining this documentation much easier.

@josef-pkt

Copy link
Copy Markdown
Member

about documentation/class structure change:
I'm skeptical (which doesn't mean that I would rule it out at the moment). The main additional feature for the docstrings in scipy is an elaborate machinery to compose and generate the individual docstrings. This is useful for 100 distributions, but I don't think it's worth the overhead for our "few" families where most methods are mainly for internal use.

If the point is mainly to avoid duplication in docstrings, then I think using the template version that we use in several other classes that have elaborate inheritance.

@thequackdaddy

Copy link
Copy Markdown
Member Author

@josef-pkt Could you point me to a good example of a template version that statsmodels uses for documentation?

@josef-pkt

josef-pkt commented Aug 7, 2017 •

Copy link
Copy Markdown
Member

I'm not sure about good examples because I don't use them much.
Given what I recently saw, discrete Poisson and new GeneralizedPoisson use %(params)s to insert the basic params into the docstring.
We do something similar in several places. It uses basic string substitution, the scipy distribution version uses reversed template, the super class specifies the template and the subclasses specify the substitution string. all subject to: IIRC

@josef-pkt

Copy link
Copy Markdown
Member

one other thing I remembered:
One of the main purposes of the distinction in scipy.stats.distributions between base class e.g. pdf and distribution class _pdf is that the pdf method does a lot of generic computation so that the distribution specific _pdf method only needs minimal or no input checking (e.g. broadcasting, checking if arguments are in the valid range for the distribution, handle loc and scale) However, this overhead is pretty expensive. Given that our consumers of the family methods are internal, we already have all or most of the input checking done in the model, i.e. GLM.

@thequackdaddy

Copy link
Copy Markdown
Member Author

Sounds good. I might approach this in a different PR. My annoyance is that because I've had to re-do the API for a few of the methods, I was trying to make all the copypasta unnecessary. You are right though... the only other family that I guess we could add is multinomial. I'm unaware (generally) of any other family that we'd ever want to add and as this is all used internally, I have difficulty seeing any advantage here.

Back to this PR, I still need to check a bunch and I'm hoping to find time tomorrow or this weekend at the latest.

@thequackdaddy

Copy link
Copy Markdown
Member Author

@josef-pkt Any thoughts on this?

Basically, my strategery is that within Family, deviance and loglike are defined as the weighted sum of resid_dev and loglike_obs, respectively.

I also fixed what I think is a bug with loglike_obs and Gaussian because the scale used in the result llf (and dependents such as aic) needs to be divided by wnobs, but only for llf and dependents.

I gave the docstrings a read and they look pretty good to me. I fixed a few other things for readability-sake as well.

Can you let me know if this is on a good path?

@thequackdaddy thequackdaddy changed the title WIP: Revise loglike/deviance to correctly handle scale ENH: Revise loglike/deviance to correctly handle scale Aug 13, 2017
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 81.26% when pulling b2084b8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 81.26% when pulling b2084b8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

@coveralls

coveralls commented Aug 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 81.255% when pulling b2084b8 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

@coveralls

coveralls commented Aug 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.007%) to 81.255% when pulling 155bac0 on thequackdaddy:scale_fix into 143cc11 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

I updated my notebook in the gist again.
I also added some comparisons for fit_regularized.
Looks also fine for var_weights, but the parameterization of the penalization weight differs between GLM and OLS/WLS and so are not directly comparable for larger weights.

So no warning about var_weights are needed in fit_regularized.

@coveralls

coveralls commented Jan 8, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 82.13% when pulling 24d8a78 on thequackdaddy:scale_fix into 6dcc0ab on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 1 alert - view on lgtm.com

fixed alerts:

  • 1 for Variable defined multiple times

Comment posted by lgtm.com

@thequackdaddy

Copy link
Copy Markdown
Member Author

@josef-pkt Anything else? Feels pretty good to me. I cov-type is working now.

@josef-pkt

Copy link
Copy Markdown
Member

I was just playing :)
https://gist.github.com/josef-pkt/ae344b04b264ea3db02da9c363b6aa24 (Gaussian pretending to be Poisson and vice-versa.)
We have the unit tests and know this is supposed to work, but wasn't sure whether the scale estimate can be correctly overwritten.
I also wanted to see if the theoretical results can be easily seen, and how flexible our GLM options are.

I haven't seen any problems today, everything looks good in the examples.

@josef-pkt

Copy link
Copy Markdown
Member

I added Poisson replicating Binomial to the gist.
I think I'm done playing. I will have a brief look and merge after dinner.

@josef-pkt

Copy link
Copy Markdown
Member

Too many details to review again. So I'm just merging this.

Thanks @thequackdaddy Those things are a lot of work, and I'm glad I don't have to do it all myself, or for most parts don't have to do it at all myself.

@josef-pkt
josef-pkt merged commit a222cec into statsmodels:master Jan 9, 2018
@thequackdaddy
thequackdaddy deleted the scale_fix branch January 9, 2018 01:58
@thequackdaddy

Copy link
Copy Markdown
Member Author

Thanks, Josef. I think we can close #3030 and #3773 as well.

@thequackdaddy

thequackdaddy commented Jan 9, 2018 •

Copy link
Copy Markdown
Member Author

And possibly #2351 and #2353

@josef-pkt

Copy link
Copy Markdown
Member

Thanks @thequackdaddy I closed two, but leave #3030 open for now (based on original motivation not because of content)

@thequackdaddy

Copy link
Copy Markdown
Member Author

Great. Let me know if I can be of any assistance getting a release out. Happy to help make statsmodels great again.

@josef-pkt

Copy link
Copy Markdown
Member

Thanks, one task is to add the changes and enhancements to the release notes
very incomplete draft that can be edited is in the wiki https://github.com/statsmodels/statsmodels/wiki/Release-Notes-0.9

If the tweedie PR is not dangerous in the sense of having a not low probability of introducing new problems, then it could still be merged.
I will go back to checking what needs to be done for the release for the rest of the week, and hopefully merging some smaller fixes.
I was just looking at #3612, trying to figure out based on the updated gist whether that bug is fixed.

@josef-pkt

Copy link
Copy Markdown
Member

about #3030 GLM class does not have loglike_obs yet for the standard interface. only the family has loglike_obs

@josef-pkt

Copy link
Copy Markdown
Member

one more notebook in a gist (that's where I was missing loglike_obs)
comparison of residuals exposure versus var_weights.
I'm not sure about resid_anscombe, but it has unit tests against Stata according to the unit test matrix above
https://gist.github.com/josef-pkt/7157b6ab2929fb16617becbea2ffddd6

@thequackdaddy

Copy link
Copy Markdown
Member Author

@josef-pkt

re anscombe residuals... for reasons I don't remember, I did not weight the residuals by var_weights.

https://github.com/statsmodels/statsmodels/blob/master/statsmodels/genmod/generalized_linear_model.py#L206

Easy enough to change it.

@josef-pkt

Copy link
Copy Markdown
Member

yes, I'm pretty sure we need var_weights in resid_anscombe the same way as scale.
I just realized that we don't use the actual scale in resid_anscombe, with fixed scale=1, the resid_anscombe will in general not be standard normal distributed.
(I was thinking about doing a MonteCarlo for detection of outlier residuals, but didn't have time yet. Whatever the definition, those should show what scaling we need for the different residuals to be approximately standard normal)

all of the anscombe look like they need * sqrt(var_weights) corresponding to scale**0.5
binomial uses, for whatever reason:
...* scale ** 3) ** (1 / 6.)
which also corresponds to scale**0.5

@josef-pkt

Copy link
Copy Markdown
Member

using qqplot also shows that resid_anscombe without var_weights has the wrong scale/variance.

an updated version in a new gist
with some outliers added in and qqplot (note ppplot always adjust the scale, so is not useful for checking scale/variance)
https://gist.github.com/josef-pkt/b04b4fb3f42869df7f7335ba47583ee5

@josef-pkt

josef-pkt commented Jan 10, 2018 •

Copy link
Copy Markdown
Member

One more update to the latest notebook in gist
At the end I added a quick MonteCarlo for very simple correctly specified model (no outliers). I.e. a very nice case.
(One local change in code: Poisson resid_dev computation was warning about negative numbers (around -1e-15) in sqrt. I clipped those to zero. I didn't check more on this)

The agreement between frequencies and standard normal probabilities is good, even higher than I expected, given that Poisson itself is not close to normal.

(I just realized I forgot to reset the random.seed before running the var_weights version - FIXED in edit here)

with exposure

[ 0.01  0.5   0.1   0.2 ]
[[ 0.010248  0.498492  0.099052  0.200192]
 [ 0.010006  0.49808   0.100956  0.200286]
 [ 0.010384  0.498084  0.101066  0.200322]]

with var_weights (resid_anscombe not corrected yet)

[ 0.01  0.5   0.1   0.2 ]
[[  1.02480000e-02   4.98492000e-01   9.90520000e-02   2.00192000e-01]
 [  1.00060000e-02   4.98080000e-01   1.00956000e-01   2.00286000e-01]
 [  5.40000000e-05   1.36560000e-02   2.18800000e-03   3.01000000e-03]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants