Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/pingouin/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Contributor 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.

# Method 1: using a student T distribution
dof = n - k - 2
tval = r * np.sqrt(dof / (1 - r**2))
Expand Down
7 changes: 7 additions & 0 deletions tests/test_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ def test_corr(self):
stats = corr(x, x)
assert np.isclose(stats.at["pearson", "r"], 1)
assert np.isclose(stats.at["pearson", "power"], 1)

# Perfect correlation with percbend method
# https://github.com/raphaelvallat/pingouin/issues/453
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)

# When one column is a constant, the correlation is not defined
# and Pingouin return a DataFrame full of NaN, except for ``n``
x, y = [1, 1, 1], [1, 2, 3]
Expand Down
Loading