def calculate_residual_correlation_matrix(returns):
# find the market return constraining on the selected companies (first PCA)
# regress each stock on that and find correlation of residuals
returns_matrix = returns.as_matrix().transpose()
covar_matrix = np.cov(returns_matrix)
pca = decomposition.PCA(n_components=1)
pca.fit(covar_matrix)
X = pca.transform(covar_matrix)
regr = linear_model.LinearRegression()
dim = covar_matrix.shape[1]
res = np.zeros(shape=(dim,dim))
for x in range(0, dim):
regr = linear_model.LinearRegression()
regr = regr.fit(X, covar_matrix[:,x])
res[:,x] = covar_matrix[:,x] - regr.predict(X)
res_corr = np.corrcoef(res)
return pd.DataFrame(res_corr, index = returns.columns, columns = returns.columns)
评论列表
文章目录