Maybe I made a mistake. If so, I am sorry to ask this.
I want to calculate Pearson's correlation coefficent by using scipy's pearsonr
function.
from scipy.stats.stats import pearsonr
X = [4, 4, 4, 4, 4, 4]
Y = [4, 5, 5, 4, 4, 4]
pearsonr(X, Y)
I get an error below
RuntimeWarning: invalid value encountered in double_scalars ###
The reason why I get an error is E[X] = 4 (Excepted Value of X is 4)
I look at the code of pearsonr function in scpy.stats.stats.py. Some part of the pearsonr function is as follows.
mx = x.mean() # which is 4
my = y.mean() # not necessary
xm, ym = x-mx, y-my # xm = [0 0 0 0 0 0]
r_num = n*(np.add.reduce(xm*ym)) #r_num = 0, because xm*ym 1x6 Zero Vector.
r_den = n*np.sqrt(ss(xm)*ss(ym)) #r_den = 0
r = (r_num / r_den) # Invalid value encountered in double_scalars
At the end, pearsonr
returns (nan, 1.0)
Should pearsonr
return (0, 1.0)
?
I think if a vector has same value for every row/column, covariance should be zero. Thus Pearson's Correleation Coefficient should also be zero by the definition of PCC.
Pearson's correlation coefficient between two variables is defined as the covariance of the two variables divided by the product of their standard deviations.
Is it bug or where do I make a mistake?
The Pearson correlation coefficient [1] measures the linear relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y.
The Pearson correlation measures the strength of the linear relationship between two variables. It has a value between -1 to 1, with a value of -1 meaning a total negative linear correlation, 0 being no correlation, and + 1 meaning a total positive correlation.
Pearson's correlation coefficient between two variables is defined as the covariance of the two variables divided by the product of their standard deviations.
So it's the covariance over
[4, 5, 5, 4, 4, 4]
times [4, 4, 4, 4, 4, 4]
.The standard deviation of [4, 4, 4, 4, 4, 4]
is zero.
So it's the covariance over
[4, 5, 5, 4, 4, 4]
times So it's the covariance over
Anything divided by zero is nan
. The value of the covariance is irrelevant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With