开发者

Pearson's Coefficient and Covariance calculation in Matlab

开发者 https://www.devze.com 2023-02-24 12:40 出处:网络
I want to calculate Pearson\'s correlation coefficent in Matlab (without using Matlab\'s corr function).

I want to calculate Pearson's correlation coefficent in Matlab (without using Matlab's corr function).

Simply, I have two vectors A and B (each of them is 1x100) and I am trying to calculate the Pearson's coefficient like this:

P = cov(x, y)/std(x, 1)std(y,1)

I am using Matlab's cov and std functions. What I don't get is, the cov function returns me a square matrix like this:

corrAB =
    0.8000    0.2000
    0.2000    4.8000

But I ex开发者_如何学Cpect a single number as the covariance so I can come up with a single P (pearson's coefficient) number. What is the point I'm missing?


I think you're just confused with covariance and covariance matrix, and the mathematical notation and MATLAB's function inputs do look similar. In math, cov(x,y) means the covariance of the two variables x and y. In MATLAB, cov(x,y) calculates the covariance matrix of x and y. Here cov is a function and x and y are the inputs.

Just to make it clearer, let me denote the covariance by C. MATLAB's cov(x,y) returns a matrix of the form

C_xx    C_xy
C_yx    C_yy

As RichC pointed out, you need the off-diagonals, C_xy (note that C_xy=C_yx for real variables x and y). A MATLAB script that gives you the Pearson's coefficient for two variables x and y, is:

C=cov(x,y);
p=C(2)/(std(x)*std(y));


From the docs:

cov(X,Y), where X and Y are matrices with the same number of elements, is equivalent to cov([X(:) Y(:)]).

use:

C = cov(X,Y);
coeff = C(1,2) / sqrt(C(1,1) * C(2,2))
0

精彩评论

暂无评论...
验证码 换一张
取 消