Repository navigation
Closed
Description
I have reviewed the file genmod.families.family.py and list the findings below. Some issues are already known. Sometimes a precise definition is missing.
General issues:
- Is
devianceandresid_devscaled or unscaled (SAS parlance), i.e. with or without the factor1/scale? (I would include1/scaleas it is mostly implemented.) This has consequences forGLM.estimate_scale(..). - Do distributions with
scale=1(Poisson, Binomial, Neg. Binomial) use scale indevianceandresid_devnevertheless? (loglikeshould be implemented withscale==1, otherwise it would not be a valid log-likelihood!) - Precise definition of Anscombe residuals. Usually, it is r_A=(A(y)-A(mu))/(A'(mu) sqrt(v(mu))), where mu=E[Y], Var[Y]=scale v(mu) and A(x)=int_0^x v(m)^(-1/3) dm. To be comparable to
resid_devand in case ofvar_weights, one might consider r_A=(A(y)-A(mu))/(A'(mu) sqrt(Var[y])) with Var[y] = scale/w v(mu).
Issues (often the docstring needs according changes):
- Poisson:
-
deviance: term- (endog - mu)is missing, see BUG/ENH: poisson deviance #3355, solved in PR BUG: missing term in family Poisson deviance #3426 -
resid_dev:1/scaleshould be insidesqrt(..), solved in PR var_weights and #3543 #3692 -
loglike:scaleshould not occur, solved in PR var_weights and #3543 #3692
-
- Gaussian
-
loglike: should take input scale and not compute it implicitly, see BUG: family Gaussian.loglike with identity/power link ignores scale argument #2351 and BUG: Gaussian loglikelihood in GLM #3356 -
resid_dev:1/scaleshould be insidesqrt(..), solved in PR var_weights and #3543 #3692
-
- Gamma:
-
deviance:1/scaleis missing, solved in PR var_weights and #3543 #3692 -
resid_dev:1/scaleinsidesqrt(..)is missing, solved in PR var_weights and #3543 #3692
-
- Binomial
-
_clean: missing function could be useful (same as Poisson). All1e-200could be resolved by_clean, solved in PR var_weights and #3543 #3692 -
devianceandresid_dev: case distinction between y=0 and y>0 not necessary, use_clean, solved in PR var_weights and #3543 #3692 -
devianceandresid_dev: should read..(self.n - endog) * np.log((self.n - endog)/(1 - mu)).., i.e.self.ninstead of1. (Binomial is internally rescaled to [0,1], see docstrings) -
devianceandresid_dev:n_ibeforefreq_weightsis too much, compare docstring where it is correct. Similar inresid_dev, solved in PR var_weights and #3543 #3692 -
deviance:1/scalemissing, solved in PR var_weights and #3543 #3692 -
resid_dev:1/scaleshould be insidesqrt(..), solved in PR var_weights and #3543 #3692 -
loglike:scaleshould not occur, solved in PR var_weights and #3543 #3692 -
resid_anscombe: should read n^(2/3) * (Beta(y/n,2/3,2/3)-Beta(mu/n,2/3,2/3)) / (mu*(1-mu/n))^(1/6), where Beta(x,2/3,2/3)=cox_snell(x) (info: scipy implements thebatinc(a,b,x)as regularized version of Beta(x,a,b), mention this in doc?), solved in PR var_weights and #3543 #3692 - simplify Binomial: self.n should be an ancillary parameter (for every observation), like alpha for Negative Binomial. (why always
self.n=1?)
Binomial(n,p) should calculatemu=n*pandendog=#successes, whereas Binomial(1,p) withvar_weightsshould calculatemu=pandendog=#successes/n => changeinitialize.
-
- Inverse Gaussian
-
resid_dev:1/scaleshould be insidesqrt(..), solved in PR var_weights and #3543 #3692 -
loglike: docstring should read ../(Y_i * \mu_i^2 * scale), note the squared \mu_i, solved in PR var_weights and #3543 #3692
-
- Negative Binomial
-
_clean: missing function could be useful (same as Poisson), solved in PR var_weights and #3543 #3692 -
devianceandresid_dev: case distinction between y=0 and y>0 not necessary, use_clean, solved in PR var_weights and #3543 #3692 -
resid_dev:1/scaleshould be insidesqrt(..), solved in PR var_weights and #3543 #3692 -
loglike:muinstead ofexp_lin_pred = np.exp(lin_pred); as it is, it is only correct for a log link, solved in PR var_weights and #3543 #3692 -
resid_anscombe: should read 3/2 * (y^(2/3)*H2F1(-alpha * y) - mu^(2/3)*H2F1(-alpha * mu)) / (mu * (1+alpha * mu))^(1/6) with H2F1(x) = Hypergeometric2F1(2/3, 1/3, 5/3, x).
Note, that it is the same as for Binomial with -1/n <-> alpha and with Beta(z,a,b) = z^a/a * H2F1(a,1-b,a+1,z).
H2F1 is advantageous because the version with Beta would involve complex numbers, solved in PR var_weights and #3543 #3692
-
- Tweedie
-
_clean:FLOAT_EPSinstead of0 -
deviance: andresid_dev: special cases should either be a copy paste of code or use the method of the special class. -
deviance: andresid_dev: 1/scale missing, solved in PR var_weights and #3543 #3692 -
loglike: might returnraise NotImplementedErrorinstead ofnp.nan
-
Activity
josef-pkt commentedon Jan 16, 2017
@lorentzenchr Thanks for the careful review, That's very very helpful.
Most of the issues look good to me, for some I don't know the details.
I was looking a bit at your comments for the scale. You found several bugs if the scale is not the default
1..two thoughts
compare_lr_test)I think we understand GLM now well enough to add
compare_lr_test#2641 (comment) #30 with loglike and deviance option including over/under dispersion.one specific issue:
(The number of trials
nis not used as a parameter, but it is part of the data like exposure, i.e. we never try to estimaten. In that sense it belongs to__init__and treated as fixed for a model not as an argument to any of the methods.)After realizing this issue is about the family: I have no idea why it was implemented this way. I think there were some issues and, maybe incomplete, refactorings related to equivalent representation of Binomial.
(Aside: Handling it through weights as in R's glm is confusing, and I saw many questions on stats.stackexchange for what weights in R's glm are really doing and what they can be used for. I.e. it's confusing for users that are not very familiar with GLM.)
lorentzenchr commentedon Jan 16, 2017
GLM.estimate_scalemust callself.family.deviance(..., scale=1). For the future ofscale, see SUMM/FAQ-D: Refactor scale handling, especially GLM, RLM #3359.nare external parameters (from the data) that must be given in the same way asalphamust be given for Negative Binomial. Otherwise, the two distributions are not members of the exponential dispersion model/family - as far as I understand GLMs.var_weightsonce they are implemented. (This is a small promotions for PR DOC: Improve documentation about glm #2959 #3364 😄. Apologies also for my normative language.)By the way: Would it be better to give one PR for every point of the list or just one PR say for all scale related issues of the list?
josef-pkt commentedon Jan 16, 2017
The best would be to separate "obvious" things from ambiguous or potentially controversial parts. For example a bug-fix and documentation PR can be merged easily with standard review. Refactorings where we clearly know what we want, are also fine and easy to merge, subject to backwards compatibility and similar. (e.g. #3359 don't use scale as model attribute.)
General refactorings might be more controversial and require more review/decisions especially without immediate use case. (I have a tendency to postpone those decisions until we know what we really need in the next expansion to new cases. That is, it's often difficult to evaluate changes based on general principles and fictitious use cases.)
Example weights: we initially had freq_weights in the resid_xxx, then removed them again, and I'm still not sure where all the weights "wiring" is supposed to go.
Another area where status-quo won so far: Theoretically, the score, score_obs and similar have scale in GLM and OLS. However, since they just drop out of the estimating equations, we keep them out. I.e. are we implementing full MLE or estimating equations? The outcome is the same but the former would have an extra scale factor in there. status-quo means that there is currently no consistent policy, and whether it's MLE-score or estimating equation is model/implementation specific.
Not a problem with me, although I consider any "must" just strongly in favor of one side of any trade-offs. And again, I appreciate the discussion and review (besides PRs), because sometimes those designs and decisions are difficult to figure out all by myself with little or no feedback.
josef-pkt commentedon Jan 16, 2017
(parking something I just remembered before running out the door)
Related to var_weights, we also have the family variance function, and there are some issues or ideas to manipulate or combine them for the actual estimation. (I had forgotten the variance function that is already implemented in the var_weights discussion (var_function is just a function of mu, but we could combine it with a second term, similar to the n_i in Binomial or
1/sqrt(n_i)if endog is in fraction or proportion.josef-pkt commentedon Jan 17, 2017
I'm reading through large parts of the technical documentation for GLM for SAS and SPSS again, not the latest version because I printed them a few years ago. Especially the SPSS manual has a nice formula collection, and quite a bit of computational details.
To incorporating scale: We can clean this up but for backwards compatibility we cannot change the defaults without having a way to prevent users from getting the wrong or unintended numbers.
In the context of weights, I thought about adding a new method for the resid_xxx that is not a cached attribute so we can use arguments for the options which kind of residuals, which weighting and which scaling, to return. Today I thought the full word
residualsshould be a good name for the new method.Otherwise, we would get a proliferation of resid__xxx_scaled and wresid_xxx_scaled.
jbrockmendel commentedon May 20, 2017
Related to #3231 which adds more 1e-200 terms.
Tweedie._cleanuses a lower bound of 0 where the other_cleanmethods useFLOAT_EPS. Is there a reason for the difference? It also looks like Tweedie doesn't actually use its_clean, but callsnp.clipdirectly in deviance and resid_dev. Changing the_cleanmethod and using it instead ofnp.clipdoes not appear to break any tests.josef-pkt commentedon May 20, 2017
In general I think we are moving to using clean instead of some floating adjustments like 1e-200.
The problem is when we need different kinds of "cleaning" or different kinds at different places. (e.g. cases of 0 * log(0) should or could switch to a newish scipy.special function, if it's available in our minimum scipy version)
tweedie might be a special case because the behavior is different for different power
p, it has poisson and gamma as special cases. In your deviance link, the clipping is only needed in one interval of p, so we would have endog >= 0 for some p and endog > 0 for other p.One reason the 1e-200 have
growninto the code is that mu should also be in the interior, but some datasets might result in perfect prediction or perfect separation where mu approaches or is equal to the boundary value, So, we might have for example log (0 / 0) which should be well defined.using
cleanwould be the preferred solution, but still needs case by case checking.Please add comments if you look at specific cases, confirming that something looks correct for special or boundary cases is also useful for review, besides pointing out what doesn't look "good".
lorentzenchr commentedon Aug 20, 2017
I consider this issue as solved. Still open are the Gaussian log-likelihood, which has its own issue, and Binomial, which is caused by internally rescaling y to [0,1] (a probability).