我正在尝试计算所有样本的变量的所有对组合之间的Pearson系数。
假设我有一个m * n矩阵,其中m是变量,n是样本
我想计算我的数据的每个变量与每个其他变量的相关性。
所以,我设法用嵌套循环来做到这一点:
X = rand[1000 100];
for i = 1:1000
base = X(i, :);
for j = 1:1000
target = X(j, :);
correlation = corrcoef(base, target);
correlation = correlation(2, 1);
corData(1, j) = correlation
end
totalCor(i, :) = corData
end
它有效,但需要花费太多时间才能运行
我试图找到一种在行的基础上运行corrcoef函数的方法,这意味着可能创建一个带有基值的repmat的附加矩阵,并使用一些FUN函数与X数据相关联。
无法弄清楚如何使用数组输入的乐趣,在个人行/列之间运行
帮助将不胜感激
答案 0 :(得分:3)
这篇文章涉及一些 hacking ,所以请耐心等待!
阶段#0 要开始,我们有 -
for i = 1:N
base = X(i, :);
for j = 1:N
target = X(j, :);
correlation = corrcoef(base, target);
correlation = correlation(2, 1)
corData(1, j) = correlation;
end
end
第1阶段在其源代码中的corrcoef
文档中:
如果C是协方差矩阵,C = COV(X),则CORRCOEF(X)是 矩阵的第(i,j)个元素是:
C(i,j)/SQRT(C(i,i)*C(j,j))
。
在侵入covariance
的代码后,我们看到对于一个输入的默认情况,协方差公式只是 -
[m,n] = size(x);
xc = bsxfun(@minus,x,sum(x,1)/m);
xy = (xc' * xc) / (m-1);
因此,混合这两个定义并将它们放入手头的问题中,我们有 -
m = size(X,2);
for i = 1:N
base = X(i, :);
for j = 1:N
target = X(j, :);
BT = [base(:) target(:)];
xc = bsxfun(@minus,BT,sum(BT,1)/m);
C = (xc' * xc) / (m-1); %//'
corData = C(2,1)/sqrt(C(2,2)*C(1,1))
end
end
阶段#2 这是我们使用真正有趣又名bsxfun
来杀死所有循环的最后阶段,例如所以 -
%// Broadcasted subtract of each row by the average of it.
%// This corresponds to "xc = bsxfun(@minus,BT,sum(BT,1)/m)"
p1 = bsxfun(@minus,X,mean(X,2));
%// Get pairs of rows from X and get the dot product.
%// Thus, a total of "N x N" such products would be obtained.
p2 = sum(bsxfun(@times,permute(p1,[1 3 2]),permute(p1,[3 1 2])),3);
%// Scale them down by "size(X,2)-1".
%// This was for the part : "C = (xc' * xc) / (m-1)".
p3 = p2/(size(X,2)-1);
%// "C(2,2)" and "C(1,1)" are diagonal elements from "p3", so store them.
dp3 = diag(p3);
%// Get "sqrt(C(2,2)*C(1,1))" by broadcasting elementwise multiplication
%// of "dp3". Finally do elementwise division of "p3" by it.
totalCor_out = p3./sqrt(bsxfun(@times,dp3,dp3.'));
本节将原始方法与建议方法进行比较,并验证输出。这是基准测试代码 -
disp('---------- With original approach')
tic
X = rand(1000,100);
corData = zeros(1,1000);
totalCor = zeros(1000,1000);
for i = 1:1000
base = X(i, :);
for j = 1:1000
target = X(j, :);
correlation = corrcoef(base, target);
correlation = correlation(2, 1);
corData(1, j) = correlation;
end
totalCor(i, :) = corData;
end
toc
disp('---------- With the real fun aka BSXFUN')
tic
p1 = bsxfun(@minus,X,mean(X,2));
p2 = sum(bsxfun(@times,permute(p1,[1 3 2]),permute(p1,[3 1 2])),3);
p3 = p2/(size(X,2)-1);
dp3 = diag(p3);
totalCor_out = p3./sqrt(bsxfun(@times,dp3,dp3.')); %//'
toc
error_val = max(abs(totalCor(:)-totalCor_out(:)))
输出 -
---------- With original approach
Elapsed time is 186.501746 seconds.
---------- With the real fun aka BSXFUN
Elapsed time is 1.423448 seconds.
error_val =
4.996e-16