Skip to content

Commit 6d53ad9

Browse files
0.24.26
1 parent ab55926 commit 6d53ad9

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.24.25"
10+
version = "0.24.26"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/utils/stats.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def partial_correlation(x, method="pearson") -> dict:
4444
Commun Stat Appl Methods 22, 6 (Nov 2015), 665–674.
4545
4646
Examples:
47-
>>> from spotpython.utils.stats import cov_to_cor
47+
>>> from spotpython.utils.stats import partial_correlation
4848
>>> import numpy as np
4949
>>> import pandas as pd
5050
>>> data = pd.DataFrame({
@@ -61,6 +61,7 @@ def partial_correlation(x, method="pearson") -> dict:
6161
[0. , 0. , 0. ]]), ...
6262
}
6363
"""
64+
eps = 1e-6
6465
if isinstance(x, pd.DataFrame):
6566
x = x.to_numpy()
6667
if not isinstance(x, np.ndarray):
@@ -83,13 +84,12 @@ def partial_correlation(x, method="pearson") -> dict:
8384
p_cor = -cov_to_cor(icvx)
8485
np.fill_diagonal(p_cor, 1)
8586

86-
epsilon = 1e-10 # small value to prevent division by zero
8787
if method == "kendall":
8888
denominator = np.sqrt(2 * (2 * (n - gp) + 5) / (9 * (n - gp) * (n - 1 - gp)))
8989
statistic = p_cor / denominator
9090
p_value = 2 * norm.cdf(-np.abs(statistic))
9191
else:
92-
factor = np.sqrt((n - 2 - gp) / (1 - p_cor**2 + epsilon))
92+
factor = np.sqrt((n - 2 - gp) / (1 + eps - p_cor**2))
9393
statistic = p_cor * factor
9494
p_value = 2 * t.cdf(-np.abs(statistic), df=n - 2 - gp)
9595

@@ -99,8 +99,9 @@ def partial_correlation(x, method="pearson") -> dict:
9999
return {"estimate": p_cor, "p_value": p_value, "statistic": statistic, "n": n, "gp": gp, "method": method}
100100

101101

102-
def pairwise_partial_correlation(x, y, z, method="pearson") -> dict:
103-
"""Calculate the pairwise partial correlation between two variables given others.
102+
def partial_correlation_test(x, y, z, method="pearson") -> dict:
103+
"""The partial correlation coefficient between x and y given z.
104+
x and y should be arrays (vectors) of the same length, and z should be a data frame (matrix).
104105
105106
Args:
106107
x (array-like): The first variable as a 1-dimensional array or list.

test/test_part_corr.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import numpy as np
33
import pandas as pd
4-
from spotpython.utils.stats import cov_to_cor, partial_correlation, pairwise_partial_correlation
4+
from spotpython.utils.stats import cov_to_cor, partial_correlation, partial_correlation_test
55

66
def test_cov_to_cor():
77
covariance = np.array([[1, 0.8], [0.8, 1]])
@@ -24,11 +24,11 @@ def test_partial_correlation():
2424
assert result['gp'] == 1, "The number of given parameters should be 1"
2525

2626

27-
def test_pairwise_partial_correlation():
27+
def test_partial_correlation_test():
2828
x = [1, 2, 3, 4]
2929
y = [2, 3, 4, 5]
3030
z = pd.DataFrame({'C': [4, 5, 6, 7]})
31-
result = pairwise_partial_correlation(x, y, z, method='pearson')
31+
result = partial_correlation_test(x, y, z, method='pearson')
3232

3333
print(result) # Debug: Output the result for inspection
3434

@@ -51,13 +51,13 @@ def test_partial_correlation_input_validation():
5151
with pytest.raises(ValueError):
5252
partial_correlation(pd.DataFrame({'A': ['a', 'b', 'c']}))
5353

54-
def test_pairwise_partial_correlation_input_validation():
54+
def test_partial_correlation_test_input_validation():
5555
x = [1, 2, 3, 4]
5656
y = [2, 3, 4, 5]
5757
z_invalid = "not a dataframe"
5858

5959
with pytest.raises(ValueError):
60-
pairwise_partial_correlation(x, y, z_invalid)
60+
partial_correlation_test(x, y, z_invalid)
6161

6262
if __name__ == "__main__":
6363
pytest.main()

0 commit comments

Comments
 (0)