⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pearson_correlation_coefficient.m

📁 Pearson 系数
💻 M
字号:
function r=pearson_correlation_coefficient(A,B)
%get pearson correlation coefficient
%r=(<ab>-<a><b>)/squr( (<a^2>-<a>^2)(<b^2>-<b>^2) ) 
% =( sum(ab)-sum(a)sum(b)/N )/sqrt( ( (sum(a^2)-sum(a)^2/N )(sum(b^2)-sum(b)^2/N ) )
%see Ref[PRE70,046126,2004]
%
%Properties of Pearson's Correlation
%http://www.stat.tamu.edu/stat30x/notes/node40.html
%1.The value of r does not depend upon the units of measurement. For example, 
%if X is the weight in pounds and Y is the height in inches of a person, 
%then the correlation between X and Y would be the same if we measured X in kilograms and Y in centimeters.
%2.The value of r does not depend upon which variable is labeled X and which variable is labeled Y.
%3.1-<=r<=1.A positive value of r means a positive linear relationship, a negative value of r means a negative linear relationship.
%4.r=1 or -1 happens only when all the points of the scatter plot lie exactly on a straight line.
%5.r measures only the linear relationship between X and Y. 
%Usually we will refer to Pearson's correlation simply by saying ``the correlation between X and Y is  '' 
%
%
%
%Input: A -- N*1 vector
%       B -- N*1 vector
%Output: r -- pearson's correlation coefficient
%
%Writed by Rock on 06/03/19
%%Modified on 06.09.15 for r=0

TEST=0;

if TEST==1
    A=[1, 2, 3]%http://davidmlane.com/hyperstat/A34739.html
    B=[2, 5, 6]
end

N=length(A);

ab=sum(A.*B);
s_a=sum(A);
s_a2=sum(A.^2);
s_b=sum(B);
s_b2=sum(B.^2);

if (s_a2-s_a^2/N)*(s_b2-s_b^2/N)==0%Modified on 06.09.15
    r=0;
else
    r=(ab-s_a*s_b/N)/sqrt((s_a2-s_a^2/N)*(s_b2-s_b^2/N));
end

return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -