Skip to content

BUG: GLM Binomial count, incorrect score, score_obs #4614

Closed

Description

@josef-pkt

It looks like GLM Binomial score and score_obs methods ignore n_trials.

Needs to be confirmed. I am debugging a failure of a new test for penalized GLM Binomial count #4576 and score at IRLS params are very large.

p2 is model estimated with IRLS
res2.model has convergence problems with bfgs

(Pdb) res2.model.score(p2.params)
array([ 1.82844758, -0.57849861,  1.19638513, -0.32730399])
(Pdb) res2.model.score_obs(p2.params).sum(0)
array([ 1.82844758, -0.57849861,  1.19638513, -0.32730399])
(Pdb) res2.model.score_obs(p2.params).T.dot(res2.model.n_trials)
array([  1.10841891e-11,   1.00175789e-11,   7.95967285e-12,
         7.00894074e-12])

maybe the unit tests for the GLM refactoring doesn't have a Binomial count case

Activity

  1. added
    type-bug-wrong
    on May 9, 2018
  2. added this to the 0.9 milestone on May 9, 2018
  3. thequackdaddy commented on May 9, 2018

    @thequackdaddy
    Member

    Thanks for finding this, Josef. That's probably my doing. I agree with your other comment about our test case suite, although huge, has some pretty gaping holes. GLM binomial is a little different because it has its own bespoke "weight"-like thing, ntrials.

    While I tried to make it as generic as possible, I also got confused because there is overlap with with ntrials, var_weights, and freq_weights. It might make sense to assume/require that the user is either allowed to use ntrials or freq_weigths/var_weights, but not both.

  4. josef-pkt commented on May 9, 2018

    @josef-pkt
    MemberAuthor

    I'm not sure what to do with Binomial counts. There are too many special case handling for it in the current code, and it has been neglected as a standard model for unit testing (including by myself).

    The user doesn't use n_trials directly, but is allowed to specify endog as (successes, failures), except that I added n_trials now to kwargs for internal use.

    Some idea:

    • just fix bugs and add more unit tests (immediate approach)
    • use var_weights internally instead of n_trials once var_weights has stabilized and a bit battle tested
    • split BinomialCount from Binomial as separate family and keep endog as counts (similar to Poisson), avoids confusing endog as count versus endog as proportion.
    • drop (successes, failures) as permitted endog, but allow proportion with n_trials or var_weights similar to R. (this is what I use now for replicating a model)
    • related: we also have exposure as possible equivalent of n_trials if we generalize exposure, which is again almost or essentially equivalent to what we can do with var_weights.
  5. josef-pkt commented on May 10, 2018

    @josef-pkt
    MemberAuthor

    It looks like there is also a bug in the hessian with observed=True

    from a failing log-binomial test case with n_trials=10:
    there are convergence problem with bfgs, but if we start close to irls params, then we get to the same params as irls (at convergence tolerance)

    however, hessian is not invertible, and the difference between OIM and EIM is very large especially for the second parameter which has the wrong sign on the diagonal of OIM.

    (Pdb) rslt_irls.model.score(rslt_irls.params)
    array([  3.35782900e-07,   3.83264057e-05,  -5.74391843e-05])
    (Pdb) rslt_irls.model.hessian(rslt_irls.params)
    array([[ -577.12454715,  1267.24619673, -1228.98579486],
           [ 1267.24619673,  4501.71344637,   131.42380418],
           [-1228.98579486,   131.42380418, -5607.05729572]])
    
    (Pdb) rslt_irls.model.hessian(rslt_irls.params, observed=False)
    array([[-780.98173872, -270.96784394, -198.61793915],
           [-270.96784394, -789.92625106,   31.97257933],
           [-198.61793915,   31.97257933, -766.91849269]])
    

    log-binomial can be a difficult case to optimize, but the fittedvalues at IRLS params are between 0.3 and 0.6 and shouldn't run into problems with domain violations.

  6. josef-pkt commented on May 10, 2018

    @josef-pkt
    MemberAuthor

    And another bug in hessian with observed=True
    It looks like I'm using eimfactor twice in the tmp part,
    tmp already includes eim_factor and then we multiply again with it.
    oim_factor = eim_factor * (1 + tmp)

    I don't remember the derivation for the expressions here (of course not), but unit test pass if I replace this line with
    oim_factor = eim_factor * (1) + tmp

    edit this change should be correct
    based on collecting terms in the SPSS algorithm documentation, which I also did initially for hessian factor

  7. josef-pkt commented on May 10, 2018

    @josef-pkt
    MemberAuthor

    there is still a test failure with fit_constrained but that's just that tolerance is to tight

    >        assert_allclose(res1.cov_params()[self.idx_p_uc[:, None],
    >                       self.idx_p_uc], res2.cov_params(), rtol=1e-10, atol=1e-1
    5)
    E       AssertionError:
    E       Not equal to tolerance rtol=1e-10, atol=1e-15
    E
    E       (mismatch 1.5625%)
    E        x: array([[  2.877556e+00,  -1.294920e-03,   6.044716e-04,   9.005805e-
    04,
    E                 6.786921e-04,   2.364194e-02,  -1.869324e-01,  -5.049150e-02,
    E                -2.058958e-02,  -3.015464e-04,  -5.586616e-04,   5.540279e-04,.
    ..
    E        y: array([[  2.877556e+00,  -1.294920e-03,   6.044716e-04,   9.005805e-
    04,
    E                 6.786921e-04,   2.364194e-02,  -1.869324e-01,  -5.049150e-02,
    E                -2.058958e-02,  -3.015464e-04,  -5.586616e-04,   5.540279e-04,.
    ..
    
    ..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\genmod\tests\test_cons
    trained.py:58: AssertionError
    
    (Pdb) np.max(np.abs(res1.cov_params()[self.idx_p_uc[:, None], self.idx_p_uc] -
     res2.cov_params()))
    3.3864466786326375e-11
    
    (Pdb) np.max(np.abs(res1.cov_params()[self.idx_p_uc[:, None], self.idx_p_uc] /
     res2.cov_params()-1))
    1.0756122659216771e-09
    
  8. josef-pkt commented on May 10, 2018

    @josef-pkt
    MemberAuthor

    this reminds me:
    There was a problem comparing hessian between GLM Binomial with probit link and discrete.Probit where agreement was not very high compared to other cases.
    AFAIR, that was the only non-canonical link that I had checked when I wrote hessian_factor.
    I don't remember for which unit test that was.

    discrete/tests/test_sandwich_cov.py has a disabled test T_estGLMProbit(CheckDiscreteGLM): but based on the comment because of the link and not because of hessian.
    discrete/tests/test_constrained.py compares discrete and GLM but only Poisson and Logit.

  9. josef-pkt commented on May 10, 2018

    @josef-pkt
    MemberAuthor

    https://github.com/statsmodels/statsmodels/pull/1781/files
    added a CheckComparisonMixin in test_glm.py but it is used only for Logit and Poisson which are the only two cases in test_glm that overlap with discrete.

    discrete test_discrete.py doesn't have any GLM comparison tests.

  10. thequackdaddy commented on May 11, 2018

    @thequackdaddy
    Member

    Whoops I meant to paste this here... ignore same comment in other thread.

    Also, using that PPT, I got a different calculation of the OIM from the source code.

    I got something like like this...

    I was able to come up with this for the OIM...

    OIM = 1 / scale * ( 1 / (g'(mu) ** 2 * V(mu)) + (endog - mu) * (g'(mu) * V'(mu) + g''(mu) * V(mu)) / (g'(mu) ** 3 * V(mu) ** 2))
    

    I think that math agrees with your guess that the line... oim_factor = eim_factor * (1 + tmp) should really be oim_factor = eim_factor + tmp.

    Simplifying more...

    OIM = 1 / scale * ( EIM + score *  EIM * (g'(mu) * V'(mu) + g''(mu) * V(mu)))
    
  11. josef-pkt commented on May 11, 2018

    @josef-pkt
    MemberAuthor

    I think your OIM looks like SPSS manual.
    The problem with the computation that I used is that I collected terms, i.e. some part of the messy last term is the same as the score factor the other part is eim taking out some duplicates.

    But the bug is that I used the eim part twice.
    bugfix now in PR #4620

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-genmodprio-hightype-bug-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions