Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correlation between two vectors?

I have two vectors:

A_1 = 

      10
      200
      7
      150

A_2 = 
      0.001
      0.450
      0.0007
      0.200

I would like to know if there is correlation between these two vectors.

I could subtract to each value the mean of the vector and than do:

  A_1' * A_2

Are there any better ways?

like image 867
dynamic Avatar asked Jan 15 '13 17:01

dynamic


People also ask

How do you find the correlation between two vectors in r?

Correlation in R can be calculated using cor() function. In R, Cor() function is used to calculate correlation among vectors, Matrices and data frames.

How do you correlate two variables?

Complete correlation between two variables is expressed by either + 1 or -1. When one variable increases as the other increases the correlation is positive; when one decreases as the other increases it is negative. Complete absence of correlation is represented by 0.

How do you find the correlation between two vectors in Matlab?

R = corrcoef( A ) returns the matrix of correlation coefficients for A , where the columns of A represent random variables and the rows represent observations. R = corrcoef( A , B ) returns coefficients between two random variables A and B .

How do you calculate the correlation between two signals?

If x(n), y(n) and z(n) are the samples of the signals, the correlation coefficient between x and y is given by Sigma x(n) * y(n) divided by the root of [Sigma x(n)^2 * y(n)^2], where ' * ' denotes simple multiplication and ^2 denotes squaring. The summation is taken over all the samples of the signals.


2 Answers

Given:

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

(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)

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 

Which is closely related to the cov function:

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

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

And it does.

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.

like image 163
Pursuit Avatar answered Oct 09 '22 23:10

Pursuit


Try xcorr, it's a built-in function in MATLAB for cross-correlation:

c = xcorr(A_1, A_2);

However, note that it requires the Signal Processing Toolbox installed. If not, you can look into the corrcoef command instead.

like image 10
Eitan T Avatar answered Oct 09 '22 23:10

Eitan T