我运行了一个相关矩阵:
sns.pairplot(data.dropna())
corr = data.dropna().corr()
corr.style.background_gradient(cmap='coolwarm').set_precision(2)
,看来advisory_pct
与all_brokerage_pct
负相关(0.57)。据我了解,我可以断言我们可以肯定地说“当顾问的投资组合中顾问的百分比低时,他的投资组合中所有经纪的百分比就高”。
但是,这是“成对”相关,并且我们无法控制其余可能变量的影响。
我搜索了SO,却找不到如何运行“部分相关”的方法,其中相关矩阵可以提供每两个变量之间的相关性,同时控制其余变量。为此,假设brokerage %
+ etf brokerage %
+ advisory %
+ all brokerage %
=投资组合的〜100%。
这样的功能存在吗?
-编辑- 根据{{3}}运行数据:
dict = {'x1': [1, 2, 3, 4, 5], 'x2': [2, 2, 3, 4, 2], 'x3': [10, 9, 5, 4, 9], 'y' : [5.077, 32.330, 65.140, 47.270, 80.570]}
data = pd.DataFrame(dict, columns=['x1', 'x2', 'x3', 'y'])
partial_corr_array = df.as_matrix()
data_int = np.hstack((np.ones((partial_corr_array.shape[0],1)), partial_corr_array))
print(data_int)
[[ 1. 1. 2. 10. 5.077]
[ 1. 2. 2. 9. 32.33 ]
[ 1. 3. 3. 5. 65.14 ]
[ 1. 4. 4. 4. 47.27 ]
[ 1. 5. 2. 9. 80.57 ]]
arr = np.round(partial_corr(partial_corr_array)[1:, 1:], decimals=2)
print(arr)
[[ 1. 0.99 0.99 1. ]
[ 0.99 1. -1. -0.99]
[ 0.99 -1. 1. -0.99]
[ 1. -0.99 -0.99 1. ]]
corr_df = pd.DataFrame(arr, columns = data.columns, index = data.columns)
print(corr_df)
x1 x2 x3 y
x1 1.00 0.99 0.99 1.00
x2 0.99 1.00 -1.00 -0.99
x3 0.99 -1.00 1.00 -0.99
y 1.00 -0.99 -0.99 1.00
这些关联没有多大意义。使用我的真实数据,我得到了非常相似的结果,其中所有相关均四舍五入为-1。
答案 0 :(得分:3)
要在控制一个或多个协变量(即数据帧中的其他列)的同时计算pandas DataFrame的两列之间的相关性,可以使用partial_corr包的Pingouin函数( em>免责声明,我是其中的创建者):
from pingouin import partial_corr
partial_corr(data=df, x='X', y='Y', covar=['covar1', 'covar2'], method='pearson')
答案 1 :(得分:2)
AFAIK,尚无正式实现scipy / numpy中的部分相关。正如@J所指出的。 C. Rocamonde,该统计网站的功能可用于计算偏相关。
我相信这是原始来源:
https://gist.github.com/fabianp/9396204419c7b638d38f
注意:
如github页面中所述,如果您的数据未标准化(从数据来看并非如此),则可能需要添加一列以向您的拟合值添加偏差项。
< / li>如果我没记错的话,它将通过控制矩阵中所有其他剩余变量来计算偏相关。如果您只想控制一个变量,则可以将idx
更改为该特定变量的索引。
编辑1(如何添加+如何处理df):
如果您查看链接,他们已经讨论了如何添加链接。
为说明其工作原理,我使用链接中的给定数据添加了hstack
的另一种方法:
data_int = np.hstack((np.ones((data.shape[0],1)), data))
test1 = partial_corr(data_int)[1:, 1:]
print(test1)
# You can also add it on the right, as long as you select the correct coefficients
data_int_2 = np.hstack((data, np.ones((data.shape[0],1))))
test2 = partial_corr(data_int_2)[:-1, :-1]
print(test2)
data_std = data.copy()
data_std -= data.mean(axis=0)[np.newaxis, :]
data_std /= data.std(axis=0)[np.newaxis, :]
test3 = partial_corr(data_std)
print(test3)
输出:
[[ 1. -0.54341003 -0.14076948]
[-0.54341003 1. -0.76207595]
[-0.14076948 -0.76207595 1. ]]
[[ 1. -0.54341003 -0.14076948]
[-0.54341003 1. -0.76207595]
[-0.14076948 -0.76207595 1. ]]
[[ 1. -0.54341003 -0.14076948]
[-0.54341003 1. -0.76207595]
[-0.14076948 -0.76207595 1. ]]
如果要维护列,最简单的方法是提取列并在计算后放回去:
# Assume that we have a DataFrame with columns x, y, z
data_as_df = pd.DataFrame(data, columns=['x','y','z'])
data_as_array = data_as_df.values
partial_corr_array = partial_corr(np.hstack((np.ones((data_as_array.shape[0],1)), data_as_array))
)[1:,1:]
corr_df = pd.DataFrame(partial_corr_array, columns = data_as_df.columns)
print(corr_df)
输出:
x y z
0 1.000 -0.543 -0.141
1 -0.543 1.000 -0.762
2 -0.141 -0.762 1.000
希望有帮助!让我知道是否有任何不清楚的地方!
编辑2:
我认为问题出在每次拟合中都没有常数项...我重写了sklearn中的代码以使其更容易添加拦截:
def calculate_partial_correlation(input_df):
"""
Returns the sample linear partial correlation coefficients between pairs of variables,
controlling for all other remaining variables
Parameters
----------
input_df : array-like, shape (n, p)
Array with the different variables. Each column is taken as a variable.
Returns
-------
P : array-like, shape (p, p)
P[i, j] contains the partial correlation of input_df[:, i] and input_df[:, j]
controlling for all other remaining variables.
"""
partial_corr_matrix = np.zeros((input_df.shape[1], input_df.shape[1]));
for i, column1 in enumerate(input_df):
for j, column2 in enumerate(input_df):
control_variables = np.delete(np.arange(input_df.shape[1]), [i, j]);
if i==j:
partial_corr_matrix[i, j] = 1;
continue
data_control_variable = input_df.iloc[:, control_variables]
data_column1 = input_df[column1].values
data_column2 = input_df[column2].values
fit1 = linear_model.LinearRegression(fit_intercept=True)
fit2 = linear_model.LinearRegression(fit_intercept=True)
fit1.fit(data_control_variable, data_column1)
fit2.fit(data_control_variable, data_column2)
residual1 = data_column1 - (np.dot(data_control_variable, fit1.coef_) + fit1.intercept_)
residual2 = data_column2 - (np.dot(data_control_variable, fit2.coef_) + fit2.intercept_)
partial_corr_matrix[i,j] = stats.pearsonr(residual1, residual2)[0]
return pd.DataFrame(partial_corr_matrix, columns = input_df.columns, index = input_df.columns)
# Generating data in our minion world
test_sample = 10000;
Math_score = np.random.randint(100,600, size=test_sample) + 20 * np.random.random(size=test_sample)
Eng_score = np.random.randint(100,600, size=test_sample) - 10 * Math_score + 20 * np.random.random(size=test_sample)
Phys_score = Math_score * 5 - Eng_score + np.random.randint(100,600, size=test_sample) + 20 * np.random.random(size=test_sample)
Econ_score = np.random.randint(100,200, size=test_sample) + 20 * np.random.random(size=test_sample)
Hist_score = Econ_score + 100 * np.random.random(size=test_sample)
minions_df = pd.DataFrame(np.vstack((Math_score, Eng_score, Phys_score, Econ_score, Hist_score)).T,
columns=['Math', 'Eng', 'Phys', 'Econ', 'Hist'])
calculate_partial_correlation(minions_df)
输出:
---- ---------- ----------- ------------ ----------- ------------
Math 1 -0.322462 0.436887 0.0104036 -0.0140536
Eng -0.322462 1 -0.708277 0.00802087 -0.010939
Phys 0.436887 -0.708277 1 0.000340397 -0.000250916
Econ 0.0104036 0.00802087 0.000340397 1 0.721472
Hist -0.0140536 -0.010939 -0.000250916 0.721472 1
---- ---------- ----------- ------------ ----------- ------------
请告诉我这是否无效!
答案 2 :(得分:0)
半行代码:
import numpy as np
X=np.random.normal(0,1,(5,5000)) # 5 variable stored as rows
Par_corr = -np.linalg.inv(np.corrcoef(X)) # 5x5 matrix
答案 3 :(得分:0)
你可以试试这个:
from sklearn.linear_model import LinearRegression
from scipy.stats import pearsonr
feature_num = df.shape[1]
feature_name = df.columns
partial_corr_matrix = np.zeros((feature_num, feature_num))
for i in range(feature_num):
x1 = df.iloc[:, i]
for j in range(feature_num):
if i == j:
partial_corr_matrix[i, j] = 1
elif j < i:
partial_corr_matrix[i, j] = partial_corr_matrix[j, i]
else:
x2 = df.iloc[:, j]
df_control = df.drop(columns=[feature_name[i], feature_name[j]], axis=1)
L = LinearRegression().fit(df_control, x1)
Lx = L.predict(df_control)
x1_prime = x1 - Lx
L = LinearRegression().fit(df_control, x2)
Lx = L.predict(df_control)
x2_prime = x2 - Lx
partial_corr_matrix[i, j] = pearsonr(x1_prime, x2_prime)[0]