Skip to content

DOC/TST/Review: binomial with non-canonical link  #4598

Open

Description

@josef-pkt

#4597 (comment)

test_glm.py has a comment, which I don't know what it means, or what's the reason for the comment

#TODO:
#Non-Canonical Links for the Binomial family require the algorithm to be
#slightly changed

I never looked at this case, i.e. Binomial count model with n_trials != 1, not Bernoulli.

The link function for the binomial probability can be anything.
The question is whether the current code uses some computation that assumes canonical link, i.e. logit.
The only source could be in the aggregation for n_trials which is the only difference to other families in the implementation.

e.g. related: log-binomial is an example that we looked at, but only with respect to params and possible problems with support violations. ( #2208, #2215)

There might be nothing wrong in this case, but it needs checking.

Activity

  1. added this to the 0.10 milestone on May 6, 2018
  2. added 2 commits that reference this issue on May 3, 2019
    641c5da
    29f9e2a
  3. bashtage commented on May 3, 2019

    @bashtage
    Member

    Wrongly closed.

  4. modified the milestones: 0.10, 0.13 on Dec 16, 2020
  5. josef-pkt commented on Dec 21, 2020

    @josef-pkt
    MemberAuthor

    checking cloglog against Stata with constrict example

    params and bse seem to agree well (based on summary prints)

    ours has nan llf because 2 observations have fittedvalues = 1, both with newton and with irls, irls doesn't raise with perfect separation exception

    glm in Stata doesn't show a warning or nan
    glm constrict log_rate log_volumne, family(binomial) link(cloglog)

    
    res_cll = GLM(df['constrict'], df[['log_rate', 'log_volumne', 'const']],
              family=families.Binomial(link=links.cloglog())).fit(method="newton", atol=1e-10, max_start_irls=10, maxiter=500,
                                                                cov_type="nonrobust")
    print(res_cll.summary())
    
                     Generalized Linear Model Regression Results                  
    ==============================================================================
    Dep. Variable:              constrict   No. Observations:                   39
    Model:                            GLM   Df Residuals:                       36
    Model Family:                Binomial   Df Model:                            2
    Link Function:                cloglog   Scale:                          1.0000
    Method:                        newton   Log-Likelihood:                    nan
    Date:                Mon, 21 Dec 2020   Deviance:                          nan
    Time:                        16:06:01   Pearson chi2:                     34.0
    No. Iterations:                     2                                         
    Covariance Type:            nonrobust                                         
    ===============================================================================
                      coef    std err          z      P>|z|      [0.025      0.975]
    -------------------------------------------------------------------------------
    log_rate        3.9703      1.507      2.635      0.008       1.017       6.924
    log_volumne     4.3362      1.649      2.630      0.009       1.104       7.568
    const          -2.9737      1.125     -2.643      0.008      -5.179      -0.769
    ===============================================================================
    ...\statsmodels\statsmodels\genmod\families\family.py:894: RuntimeWarning: invalid value encountered in true_divide
      n_endog_mu = self._clean((1. - endog) / (1. - mu))
    ...\statsmodels\statsmodels\genmod\families\family.py:946: RuntimeWarning: divide by zero encountered in true_divide
      special.gammaln(n - y + 1) + y * np.log(mu / (1 - mu)) +
    ...\statsmodels\statsmodels\genmod\families\family.py:947: RuntimeWarning: divide by zero encountered in log
      n * np.log(1 - mu)) * var_weights
    ...\statsmodels\statsmodels\genmod\families\family.py:947: RuntimeWarning: invalid value encountered in add
      n * np.log(1 - mu)) * var_weights
    
  6. josef-pkt commented on Dec 21, 2020

    @josef-pkt
    MemberAuthor

    Stata has cloglog and loglog with the same signs of params (i.e. seems to have the same definition of prob)
    We only have cloglog.

    I guess we should get loglog to have skewness in the other direction from cloglog without having to flip/reparameterize endog

  7. josef-pkt commented on Dec 21, 2020

    @josef-pkt
    MemberAuthor

    2 corner problems in binomial deviance and loglike

    0 / 0, and 0 log 0

    _clean clips away from zero, so we don't have log 0, (old code)
    I added now 1e-20 in denominator so we don't have 0/0
    then cloglog finishes without nan llf and deviance and without RuntimeWarnings.

    deviance and llf are the same as Stata base on print summary.

            n_endog_mu = self._clean((1. - endog) / (1. - mu + 1e-20))
            resid_dev = endog * np.log(endog_mu) + (1 - endog) * np.log(n_endog_mu)
    

    The part of the code where we have 0 log 0 should be refactored to use the scipy special functions that have existed now already for some time. They were added to scipy to handle cases like this.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions