Correlation is a measure of how well two vectors track with each other as they change. You can't track mutual change when one vector doesn't change.
As noted in OP comments, the formula for Pearson's product-moment correlation coefficient divides the covariance of X
and Y
by the product of their standard deviations. Since Y
has zero variance in your example, its standard deviation is also zero. That's why you get the true_divide
error - you're trying to divide by zero.
Note: It might seem tempting, from an engineering standpoint, to simply add a very small quantity (say, a value just above machine epsilon) onto one of the entries in Y
, in order to get around the zero-division issue. But that's not statistically viable. Even adding 1e-15
will seriously derange your correlation coefficient, depending on which value you add it to.
Consider the difference between these two cases:
X = [1.0, 2.0, 3.0, 4.0]
tiny = 1e-15
# add tiny amount to second element
Y1 = [2., 2.+tiny, 2., 2.]
np.corrcoef(X, Y1)[0, 1]
-0.22360679775
# add tiny amount to fourth element
Y2 = [2., 2., 2., 2.+tiny]
np.corrcoef(X, Y2)[0, 1]
0.67082039325
This may be obvious to statisticians, but given the nature of the question it seems like a relevant caveat.
stddev
) of the constant listY
is0
. I'm not sure that it makes sense to calculate the covariance of something with respect to something that's constant... – Ambush