Skip to content

BUG/ENH: scale, variance, var_weights in GLM #3773

Closed

Description

@josef-pkt

as followup to adding var_weights #3692 and several issues about scale in GLM.
#2353 has a correction for Gaussian loglike, (and other changes that are not decided yet.)

scale and similar is currently only in limited use in GLM. However, the underlying code does not handle scale and variance correctly if we want to go beyond the current use.

specific issue here: check and fix scale and var_weights in loglike and related methods for the non-fixed scale families, Gaussian, Gamma and InverseGaussian.
(QMLE and dispersion or var_weights in fixed scale families is a different issue, where it is not completely clear yet what to do.)

Activity

  1. josef-pkt commented on Jun 21, 2017

    @josef-pkt
    MemberAuthor

    @thequackdaddy a new issue for the non-fixed scale families where getting the correct solution for scale and var_weights should be reasonably straight forward, e.g. following the SPSS manual.

    I would like to have this part fixed before the release. It should also give us a better idea where we have to keep the var_weights separate from the other weights/iweights.

  2. thequackdaddy commented on Jun 21, 2017

    @thequackdaddy
    Member

    Thanks, Josef. Sadly, the next few weeks look a little on the busy side for me because of work... I will try to get to this.

    I read your other post that suggests you may be away for a bit. Safe travels.

  3. josef-pkt commented on Jun 21, 2017

    @josef-pkt
    MemberAuthor

    Thanks, It's not urgent, maybe sometimes in August.
    I will be in Europe for a month, but will try to "play" a bit with the new things in master during the time.
    My main internet and review time during summer is reserved for GSOC.

  4. thequackdaddy commented on Jul 2, 2017

    @thequackdaddy
    Member

    So I sat down to browse through the SPSS manual you linked to in an earlier thread. Amazing find.

    They have a very thorough listing of formulas... Makes me feel guilty for not having a SPSS license!

    Before I start ripping up what we have, I'd like to walk through my idea of how to get this to work with the minimum amount of coding. @josef-pkt , at your leisure, let me know if the following rough notes makes sense. Feel like this is something which needs a lot of planning and expectation-setting before I get too deep into a morass of refactoring.

    Scale - There are essentially 4 cases:

    • User provided/Fixed
    • dev: Deviance
    • chi2: Pearson Chi2
    • mle: Maximum likelihood estimated

    Default behavior by family:

    • Poisson and Binomial: fixed at 1.0
    • Others: dev

    Special ruleswhen mle scale:

    • Per the SPSS handbook, mle is not available for discrete distributions. I presume this is only Poisosn, Binomial, and NegativeBinomial (I'm not positive on Negative Binomial). So only pearson chi2, deviance, and fixed should work. The following paragraph is directly taken from from the manuals,
      "On the other hand, discrete distributions do not have this extra parameter [in reference to the scale parameter \phi in V(mu)` (it is theoretically equal to one). Because of it, the variance of y might not be equal to the nominal variance in practice (especially for Poisson and binomial because the negative binomial has an ancillary parameter k). A simple way to adjust this situation is to allow the variance of y for discrete distributions to have the scale parameter as well, but unlike continuous distributions, it can’t be estimated by the ML method. So for discrete distributions, there are two ways to obtain the value of phi: 1. deviance or pearson chi2 or 2. fixed specified"
    • For the normal distributions, Kerby Shedden uses np.mean(np.power(self.endog - mu, 2))
    • For other distributions, I presume the scipy optimizers could be used on loglike.

    Loglikelihood/Deviance in Family:

    • The current API of the Family use the iweight variable exclusively. I wonder if we shoudl just change that to var_weights? Reading through the SPSS manual, it refers to this as "scale weights" with w-looking greek \omega. (I've now seen this referred to as variance, analytic, prior, and scale weights... quite confusing!). We are using iweigthts as importance weights (I think) and generally they are all freq_weights * var_weights.
    • To keep us as close as possible to the current structure, we could use the SPSS formulas by factoring out the curly f_i from the formulas below. Those represent frequency weights and look factor-able to me.

    image

    Additionally, I would remove the deviance and loglike methods from each of the families. Replacing it, deviance would be defined within the GLMResults as np.sum(freq_weights * family.dev_resids(endog, mu, var_weights, scale)). Additionally, I would add a loglike_obs method (with freq_weights factored outand do a similarnp.sum(freq_weights * family.loglike_obs(endog, mu, var_weights, scale))inGLMResultsforloglike`

    As for Tweedie, I don't fully understand the formula that the SPSS manual provides. It looks totally new to me... I've seen an "extended quasi-loglikelihood" formula before (don't remember where). This looks like a quick-and-dirty simplification of that based on Tweedie's relatively straightforward deviance formula.

    All of this surgery would likely leave us with a pretty bloodied test suite... So ideally I hope this would match something. The original author spent a lot of time getting some of the results to match stata, R, and SPSS so pretty much every result other than params I would expect to need either new golden values or a thorough review.

    Am I missing anything here? Or grossly over complicating your vision?

  5. josef-pkt commented on Jul 3, 2017

    @josef-pkt
    MemberAuthor

    @thequackdaddy
    There are two issues here

    • getting phi/omega_i into the families, loglike, ... (and fixing current scale or phi)
    • estimating phi

    phi is scale in out terminology, and omega_i are var_weights.

    #2353 was changing both, but I haven't looked or thought enough about the implication of the change in estimating scale. (mle doesn't have degrees of freedom correction, which I think we should use by default also in gaussian. But we also have cov_type and cov_kwds as extra options for affecting the estimated cov_params and wald inference, i.e. using mle scale for llf and using pearson scale for cov_params could coexist.)

    The immediate issue for 0.9 is adding or fixing var_weights / scale in the loglike, llf, deviance, ... correctly so we have extended support for the added var_weights. The main issue, besides fixing the formulas in llf, is how much we have need to distinguish var_weights (omega_i) from all other weights (f_i). var_weights need to go into loglike, deviance, ..., but other weight are only really relevant for the aggregation (weighted sum or mean of xxx_obs).
    I need to look whether there are associated refactorings. However, deviance needs to be available in the model and not only the result, because we might want to use it during optimization.

    All of this surgery would likely leave us with a pretty bloodied test suite

    My guess is that this is not or should not be the case. Adding proper scale and var_weight handling is an extension and should not really affect the current results (if we keep scale=pearson as a default, or use it in the unit tests.) There might be changes if refactoring reshuffles attributes/options.

    All this is mainly for the families that don't have scale=1.

  6. josef-pkt commented on Jan 9, 2018

    @josef-pkt
    MemberAuthor

    issues should be fixed with merge of #3856
    closing but I added a FAQ label to indicate that there is some design/background information here.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions