Skip to content

MAINT: RuntimeWarning: invalid value encountered in _nct_sf #8624

Closed

Description

@sam-s

update
resolution for 0.14 is to replace scipy.stats by scipy.special functions to avoid new boost versions.

Describe the bug

Recently I started to get a RuntimeWarning where I did not get one before:

import statsmodels.api as sm
analysis = sm.stats.TTestIndPower()
analysis.solve_power(nobs1=None, effect_size=0.978343, alpha=0.05, power=0.9)
/.../site-packages/scipy/stats/_continuous_distns.py:6832: RuntimeWarning: invalid value encountered in _nct_sf
  return np.clip(_boost._nct_sf(x, df, nc), 0, 1)
/.../site-packages/scipy/stats/_continuous_distns.py:6826: RuntimeWarning: invalid value encountered in _nct_cdf
  return np.clip(_boost._nct_cdf(x, df, nc), 0, 1)
22.959830247088764

Expected Output

Just the number 22.9...

Output of import statsmodels.api as sm; sm.show_versions()

Details

INSTALLED VERSIONS

Python: 3.10.6.final.0
OS: Linux 6.0.6-76060006-generic #202210290932166906205022.04~d94609a SMP PREEMPT_DYNAMIC Mon N x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: C

statsmodels

Installed: 0.13.5 (/.../site-packages/statsmodels)

Required Dependencies

cython: Not installed
numpy: 1.24.1 (/.../site-packages/numpy)
scipy: 1.10.0 (/.../site-packages/scipy)
pandas: 1.5.3 (/.../site-packages/pandas)
dateutil: 2.8.2 (/.../site-packages/dateutil)
patsy: 0.5.3 (/.../site-packages/patsy)

Optional Dependencies

matplotlib: 3.6.3 (/.../site-packages/matplotlib)
backend: QtAgg
cvxopt: Not installed
joblib: 1.2.0 (/.../site-packages/joblib)

Developer Tools

IPython: 8.8.0 (/.../site-packages/IPython)
jinja2: 3.1.2 (/.../site-packages/jinja2)
sphinx: Not installed
pygments: 2.14.0 (/.../site-packages/pygments)
pytest: 7.2.1 (/.../site-packages/pytest)
virtualenv: 20.17.1 (/.../site-packages/virtualenv)

Activity

  1. josef-pkt commented on Jan 19, 2023

    @josef-pkt
    Member

    scipy is moving many distributions from using the scipy special code to using boost.
    Some behavior is different and boost also still has some bugs in edge cases that are getting fixed over time.
    Also, I guess that warnings that are just an implementation detail should be removed or silenced over time in scipy or boost.

    For the specific case:
    This might occur during rootfinding if we have not imposed enough constraints to restrict values to valid ranges.
    You can reverse or roundtrip the results to check that the results are consistent

    AFAIR, this should work:
    analysis.power(nobs1=22.959830247088764, effect_size=0.978343, alpha=0.05)

    The scipy version that I currently use is too old for me to check those boost cases.
    e.g. What I usually do is use a unit test, convert warnings to raise an error and look at it with pytest --pdb. This is usually a quick way to find the parameters that raise a warning. Sometimes those parameters are edge cases that need special handling.
    In this case, scipy already clips the sf, cdf values to be within the [0, 1] range, so I guess statsmodels will not need specific code to treat this.

  2. josef-pkt commented on Jan 19, 2023

    @josef-pkt
    Member

    That's what I get

    import statsmodels.api as sm
    analysis = sm.stats.TTestIndPower()
    analysis.solve_power(nobs1=None, effect_size=0.978343, alpha=0.05, power=0.9)
    22.959831488068428
    
    analysis.power(nobs1=22.959831488068428, effect_size=0.978343, alpha=0.05)
    0.9000000004714136
    
    import scipy
    scipy.__version__
    '1.7.3'
    
  3. sam-s commented on Jan 19, 2023

    @sam-s
    Author

    Right, I know the results are correct, thank you.
    The question is more along the lines "this is a regression - how do I deal with it?"
    You explained that the regression is due to scipy moving to use boost, thank you.
    Now, who should change the code to avoid this warning?
    I would argue that your should change statsmodels rather than me (and countless others) changing user code.
    Or maybe I misunderstood you?
    Maybe it is scipy who should change?
    Thank you for such a speedy reply!

  4. josef-pkt commented on Jan 19, 2023

    @josef-pkt
    Member

    scipy needs to change if it's a numerical problem in boost. (In this case the problem will go away in one or a few scipy versions.)

    statsmodels needs to change if we evaluate at invalid parameter values.

    Currently I do not know which it is.

  5. josef-pkt commented on Jan 27, 2023

    @josef-pkt
    Member

    I just saw scipy/scipy#14901

    I don't know if that is the same as the warning here.

  6. josef-pkt commented on Feb 8, 2023

    @josef-pkt
    Member

    #8656 replaced scipy.stats distributions by direct use of scipy.special for ncf, nct in power and oneway modules

  7. added this to the 0.14 milestone on Feb 8, 2023
  8. changed the title [-]RuntimeWarning: invalid value encountered in _nct_sf[/-] [+]MAINT: RuntimeWarning: invalid value encountered in _nct_sf[/+] on Feb 8, 2023
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions