Skip to content

Fixes #453: _correl_pvalue does not divide by zero if r==1 #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AlexanderJCS
Copy link

Before this change, _correl_pvalue would create a division by zero warning when r==1.

Simply adding the condition near the top of the function:

    if np.isclose(r**2, 1):  # Avoid divide by zero error
        return 0.0  # Since p value approaches 0 as r approaches 1, just return 0

Fixes this. This assumes that n >= 2 since p would be undefined if n < 2, but I did not account for this case since it seems that the function already assumes that n >= 2.

Please let me know if you want me to make any other changes.

Copy link

codecov bot commented Jul 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.54%. Comparing base (c939d2d) to head (c1070de).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #474   +/-   ##
=======================================
  Coverage   98.54%   98.54%           
=======================================
  Files          19       19           
  Lines        3360     3362    +2     
  Branches      492      493    +1     
=======================================
+ Hits         3311     3313    +2     
  Misses         26       26           
  Partials       23       23           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Owner

@raphaelvallat raphaelvallat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AlexanderJCS — apologies for the long delay. One request to update the tests to make sure this line is covered by the tests. Thanks

@@ -54,6 +54,9 @@ def _correl_pvalue(r, n, k=0, alternative="two-sided"):
"less",
], "Alternative must be one of 'two-sided' (default), 'greater' or 'less'."

if np.isclose(r**2, 1): # Avoid divide by zero error
return 0.0 # Since p value approaches 0 as r approaches 1, just return 0

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test in https://github.com/raphaelvallat/pingouin/blob/main/tests/test_correlation.py test_corr function:

stats = corr(x, x, method="percbend")  # calls _correl_pvalue
assert np.isclose(stats.at["percbend", "r"], 1)
assert np.isclose(stats.at["percbend", "p_val"], 0)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Raphael, I added these lines in the test_corr(self) function under the perfect correlation test for the pearson method.

@AlexanderJCS
Copy link
Author

AlexanderJCS commented Jul 25, 2025

@raphaelvallat The edge case is covered by the correlation test. Please let me know if there's anything else you would like me to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants