Skip to content

test_compare_numdiff flaky test #4396

Closed

Description

@jbrockmendel

test_lme.TestMixedLM.test_compare_numdiff is occasionally failing when run locally. A bit surprising that it isn't consistent because it calls np.random.seed(3558). It's marked with @dec.slow. Does it not get run by the CI?

Activity

  1. josef-pkt commented on Mar 24, 2018

    @josef-pkt
    Member

    ci testing does not skip slow test, so the test should be run.

    The main reason it could sometimes fail is floating point, numerical noise and tolerance to tight in some cases, e.g. when gradients or hessians are very small. This is a problem when the derivatives are evaluated at the MLE, but not so much in this case with random parameters.

    I don't remember having seen that this test fails.

  2. jbrockmendel commented on Mar 24, 2018

    @jbrockmendel
    ContributorAuthor

    OK, I'll keep trying to track this down. I'm finding it fails about 1/5 runs. If you get a chance to run locally a few times, pls let me know.

    but not so much in this case with random parameters

    Shouldnt calling np.random.seed at the top make it non random?

  3. josef-pkt commented on Mar 25, 2018

    @josef-pkt
    Member

    Shouldn't calling np.random.seed at the top make it non random?

    It makes the random number generator non-random.

    But there is also floating point noise in any numerical computation that may not be deterministic, and might fluctuate for technical reasons, memory usage, ....
    If an assert checks at the precision of this floating point noise, then it might fail sometimes or on some machines.

  4. jbrockmendel commented on Mar 25, 2018

    @jbrockmendel
    ContributorAuthor

    But there is also floating point noise in any numerical computation that may not be deterministic, and might fluctuate for technical reasons, memory usage, ...

    I have a vague recollection that we may have had this conversation before, and that I found this as strange then as I do now. The stack* results I see all seem to relate to threading issues, which shouldn't be at play here.

    The assertion in question is assert_allclose(hess, nhess, rtol=1e-3) and the smallest exponent I'm seeing in the traceback is "e-01", which I think suggests that the difference is of order e-4, which I hope isn't explained by the floating point randomness theory.

    Also looks like a) this reproduces on OSX but not on Linux, b) its always exactly one element that mismatches.

  5. josef-pkt commented on Mar 25, 2018

    @josef-pkt
    Member

    We don't have CI testing for OSX, just Windows and Linux which both use MKL as linear algebra library.
    MKL by default uses multiple processors. Even in the old times when I was using ATLAS on Windows on a single core, some linear algebra computation had systematic fluctuations at floating point precision when I ran it repeatedly.

    Maybe there is a problem with the linear algebra library that you are using on OSX.

    Can you figure out a change to the unit test so it always passes on OSX?
    I have no experience with OSX.

  6. jbrockmendel commented on Mar 25, 2018

    @jbrockmendel
    ContributorAuthor

    Yah I'll keep poking at it

  7. jbrockmendel commented on Mar 27, 2018

    @jbrockmendel
    ContributorAuthor

    It looks like I can replicate the error on Linux when copy/pasting the code interactively, but not when running via pytest. When convenient, can you give it a shot and see if the behavior replicates? Dropping n_grp from 200 to 20 makes the error happen much more reliably (and faster)

  8. matthew-brett commented on Sep 10, 2018

    @matthew-brett
    Contributor

    I think I've just hit this in the wheel tests, on macOS:

    _______________________ TestMixedLM.test_compare_numdiff _______________________
    ../venv/lib/python2.7/site-packages/statsmodels/regression/tests/test_lme.py:160: in test_compare_numdiff
        assert_allclose(hess, nhess, rtol=1e-3)
    E   AssertionError:
    E   Not equal to tolerance rtol=0.001, atol=0
    E
    E   (mismatch 3.125%)
    E    x: array([[  3.334425e+02,  -1.555541e+01,   3.319610e+01,  -2.756426e+00
    E             3.304692e+00,  -3.172214e-01,  -4.619011e+00,  -5.672191e-01]
    E          [ -1.555541e+01,   3.411996e+02,   2.134977e+01,  -1.826591e+00,...
    E    y: array([[  3.334425e+02,  -1.555536e+01,   3.319612e+01,  -2.756431e+00
    E             3.304554e+00,  -3.172168e-01,  -4.619012e+00,  -5.672190e-01]
    E          [ -1.555536e+01,   3.411996e+02,   2.134976e+01,  -1.826606e+00,...
     1 failed, 6813 passed, 298 skipped, 4 xfailed, 1 xpassed, 72 warnings in 1103.31 seconds
    

    https://travis-ci.org/MacPython/statsmodels-wheels/jobs/426763185

    It does seem there's some randomness, this is only for Python 2.7, and we didn't get the failure at all for the previous runs, yesterday.

  9. josef-pkt commented on Sep 10, 2018

    @josef-pkt
    Member

    we can increase rtol to rtol=2e-3, i.e doubling it.

    a bit of background using pdb, test cases are in a loop, my numbers look the same as in the failing test, so I guess I have the same case (I put rtol=1e-4 to get a failing case that drops into pdb)

    It doesn't look like a close to zero case where rtol would have problems. numerical hessian are difficult to check and depend a lot on the parameters and curvature, rtol=2e-3 is still a good verification.

    -> assert_allclose(hess, nhess, rtol=1e-4)
    (Pdb) np.max(np.abs(hess - nhess))
    0.0037647303021017819
    (Pdb) np.max(np.abs(hess / nhess - 1))
    0.00063778204680287054
    (Pdb) np.min(np.abs(hess))
    0.10224572874907807
    (Pdb) np.min(np.abs(nhess))
    0.10225036252756525
    
    (Pdb) hess.ravel()[np.argmax(np.abs(hess / nhess - 1))]
    0.75958933282678431
    (Pdb) nhess.ravel()[np.argmax(np.abs(hess / nhess - 1))]
    0.75910518916549974
    

    (as in another test failure: I have no clue about reliability and numerical noise on OSX, and no way to check.)

  10. jbrockmendel commented on Sep 10, 2018

    @jbrockmendel
    ContributorAuthor

    I have no clue about reliability and numerical noise on OSX, and no way to check.

    Out of curiosity I'm going to check if using bottleneck affects this. I seem to recall its results matching those from python (slow) sum in a case where np.sum bounced around.

  11. jbrockmendel commented on Sep 10, 2018

    @jbrockmendel
    ContributorAuthor

    BTW troubleshooting this would be made easier if this particular test were parametrized, so the test log would tell us which of the 2**3 cases failed

  12. jbrockmendel commented on Sep 10, 2018

    @jbrockmendel
    ContributorAuthor

    e.g. when I drop n_grp to 20 (faster test, plus failures are much more consistent), it looks like half of the cases fail, precisely those with profile_fe = True woops, use_sqrt=True

  13. jbrockmendel commented on Sep 10, 2018

    @jbrockmendel
    ContributorAuthor

    There's also reason to believe there may be a non-numerical problem in the affected code: #4414 (comment)

  14. added a commit that references this issue on Sep 11, 2018
    23cd072
  15. jbrockmendel commented on May 10, 2019

    @jbrockmendel
    ContributorAuthor

    This is now conditionally xfailed on Mac, and Mac is in the CI. Closing.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions