Pearson's Coefficient and Covariance calculation in Matlab

允我心安 提交于 2019-12-22 05:02:19

问题


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 expect 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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/5644981/pearsons-coefficient-and-covariance-calculation-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!