Skip to content

BUG: GLM Gaussian scale in normalized_cov_params and cov_params, bse is wrong #5316

Closed

Description

@josef-pkt

It looks like GLM _fit_gradient divides normalized_cov_params by the scale even though scale is set to 1 during optimization, i.e. refactoring bug when we added fixed scale during optimization. This is relevant for models that use cov_params = scale * normalized_cov_params and scale != 1

#5315 (comment)

testcase class TestGlmGaussianGradient(TestGlmGaussian): which just uses 'bfgs' instea of irls as optimizer.

self = <statsmodels.genmod.tests.test_glm.TestGlmGaussianGradient object at 0x00
0000000A8D2080>

    def test_standard_errors(self):
>       assert_almost_equal(self.res1.bse, self.res2.bse, self.decimal_bse)
E       AssertionError:
E       Arrays are not almost equal to 10 decimals
E
E       (mismatch 100.0%)
E        x: array([  2.7855503991e-01,   1.0986117050e-04,   1.6020985175e-03,
E                7.0287826745e-04,   7.4159250241e-04,   1.4940892352e+00,
E                2.9208139065e+03])
E        y: array([  8.4914925800e+01,   3.3491007800e-02,   4.8839968200e-01,
E                2.1427416300e-01,   2.2607320000e-01,   4.5547849900e+02,
E                8.9042038400e+05])

..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\genmod\tests\test_glm.
py:63: AssertionError
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> ...\statsmodels\genmod\tests\test_glm.py(63)test_standard_errors()
-> assert_almost_equal(self.res1.bse, self.res2.bse, self.decimal_bse)

If I removed the division by scale, then bse looks fine.

This bug has not been released yet, AFAIR

Activity

  1. added
    type-bug-wrong
    on Oct 10, 2018
  2. josef-pkt commented on Oct 10, 2018

    @josef-pkt
    MemberAuthor

    missing part in unit test
    test_gradient_irls compares directly computed gradient_bse based on hessian(observed=False) instead of verifying rslt_gradient.bse
    The difference between OIM and EIM needs rtol=0.2 (so far in my checking)
    but gamma fails much larger

    E                       AssertionError:
    E                       Not equal to tolerance rtol=0.2, atol=5e-05
    E
    E                       (mismatch 100.0%)
    E                        x: array([ 0.102674,  0.10059 ,  0.097687])
    E                        y: array([ 0.069351,  0.072539,  0.070041])
    
    ..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\genmod\tests\test_glm.
    py:1141: AssertionError
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    > m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\genmod\tes
    ts\test_glm.py(1141)test_gradient_irls()
    -> assert_allclose(rslt_gradient.bse, rslt_irls.bse, rtol=0.2, atol=5e-5)
    (Pdb) rslt_gradient.bse / rslt_irls.bse - 1
    array([ 0.48050172,  0.38671099,  0.39470236])
    (Pdb) family_class
    <class 'statsmodels.genmod.families.family.Gamma'>
    (Pdb) rslt_gradient.scale
    0.45409984586701435
    (Pdb) rslt_gradient.bse * np.sqrt(rslt_gradient.scale) / rslt_irls.bse - 1
    array([-0.00233533, -0.06553802, -0.06015288])
    
  3. josef-pkt commented on Oct 10, 2018

    @josef-pkt
    MemberAuthor

    The fix is to call hessian at the end of fit_gradient after scaletype has been reset to original.

    To make bse of fit_irls and fit_gradient comparable, we need a cov_type eim option.
    However, 'nonrobust' has special treatment in generic cov_type handling, Using cov_type='eim' causes and exception/ValueError in generic cov_type, cov_type not recognized even though I handled it in GLM._fit_gradient directly.

    maybe choosing hessian eim or oim should be in cov_kwds
    Note if we have a eim/oim/default option, then we need/should use oim/hessian also after irls instead of using the WLS normalized_cov_params.

    Overall, I think it would be better to move GLM more towards standard MLE, and eg. use hessian directly instead of normalized_cov_params = hessian / scale.
    This would make IRLS and the analogy to the linear model as special case instead of being the dominant pattern for GLM.
    After the GLM scale refactoring, scale can be properly handled by the families, loglike and derivatives as extra or nuisance parameter.

    Another possible refactoring: now scale is a keyword argument for loglike and similar methods. The fit method (fit_gradient) and results class should actually use the estimated scale instead of having scale_type as "stale attribute" in the model.
    This requires that we add generic support for "nuisance parameters" that are not included in the optimization and cov_params.
    one case for this: generic cov_type handling needs to use extra kwds with nuisance parameters like scale when calling hessian and score_obs. And we need a model specific attribute to store what those nuisance parameter results attribute are. E.g. discrete models don't have scale.

    However, this sounds like a lot of work, needs more use and test cases, and is potentially a rich source of more refactoring bugs.

    At the moment we just need the bug fix. If we compare oim in fit_gradient with eim in fit_irls, then we can get some low precision unit tests without needing the eim covtype option yet.

    more unit tests needed:
    It is likely that all sandwich cov_type unit tests for GLM use IRLS. and there might be possible problems in that case also when fit_gradient is used. (Although that sounds unlikely because sandwiches use generically hessian and score_obs which are correct, and correctly include scale based on scale argument in fit.

  4. added this to the 0.10 milestone on Oct 18, 2018
  5. josef-pkt commented on Oct 18, 2018

    @josef-pkt
    MemberAuthor

    A temporary solution is in #5296 which adds an incompletely supported eim option to GLM fit for use in the unit tests.

  6. josef-pkt commented on May 9, 2019

    @josef-pkt
    MemberAuthor

    closing as fixed for 0.10, but I opened #5676 to review this again

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