Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encountered invalid value when I use pearsonr

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?

like image 420
Baskaya Avatar asked Oct 04 '11 21:10

Baskaya


People also ask

What is pearsonr in python?

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.

What does Pearson correlation measure?

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.


1 Answers

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

  • the standard deviation of [4, 5, 5, 4, 4, 4] times
  • the standard deviation of [4, 4, 4, 4, 4, 4].

The standard deviation of [4, 4, 4, 4, 4, 4] is zero.

So it's the covariance over

  • the standard deviation of [4, 5, 5, 4, 4, 4] times
  • zero.

So it's the covariance over

  • zero.

Anything divided by zero is nan. The value of the covariance is irrelevant.

like image 78
agf Avatar answered Oct 05 '22 01:10

agf