且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

两个向量之间的相关性?

更新时间:2022-11-17 13:53:16

给出:

A_1 = [10 200 7 150]';
A_2 = [0.001 0.450 0.007 0.200]';

(正如其他人已经指出的那样),有一些工具可以简单地计算相关性,最明显的是corr:

(As others have already pointed out) There are tools to simply compute correlation, most obviously corr:

corr(A_1, A_2);  %Returns 0.956766573975184  (Requires stats toolbox)

您还可以使用基本的Matlab的corrcoef函数,如下所示:

You can also use base Matlab's corrcoef function, like this:

M = corrcoef([A_1 A_2]):  %Returns [1 0.956766573975185; 0.956766573975185 1];
M(2,1);  %Returns 0.956766573975184 

cov函数密切相关:

cov([condition(A_1) condition(A_2)]);


正如您几乎在最初的问题中提到的那样,您可以根据需要自己缩放和调整向量,这使您对所发生的事情有了更好的了解.首先创建一个条件函数,将其减去平均值,然后除以标准差:


As you almost get to in your original question, you can scale and adjust the vectors yourself if you want, which gives a slightly better understanding of what is going on. First create a condition function which subtracts the mean, and divides by the standard deviation:

condition = @(x) (x-mean(x))./std(x);  %Function to subtract mean AND normalize standard deviation

然后相关性似乎是(A_1 * A_2)/(A_1 ^ 2),如下所示:

Then the correlation appears to be (A_1 * A_2)/(A_1^2), like this:

(condition(A_1)' * condition(A_2)) / sum(condition(A_1).^2);  %Returns 0.956766573975185

通过对称,这也应该起作用

By symmetry, this should also work

(condition(A_1)' * condition(A_2)) / sum(condition(A_2).^2); %Returns 0.956766573975185

确实如此.

我相信,但现在还没有力量去确认,只要处理维度时要格外小心,在处理多维输入时就可以使用相同的数学来计算相关项和互相关项和输入数组的方向.

I believe, but don't have the energy to confirm right now, that the same math can be used to compute correlation and cross correlation terms when dealing with multi-dimensiotnal inputs, so long as care is taken when handling the dimensions and orientations of the input arrays.