@@ -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.
0 commit comments