# Pearson correlation to a uniformly distributed dataset

Why when I try to compute Pearson to a Uniformly distributed observed dataset (the most trivial being: [1, 1, 1]) I get nan?

I'm using python, here is a simple test:

scipy.stats.pearsonr([24 29 47], [1,1,1])


Where [24 29 47] was read from a random number generator.

What I can notice is that Pearson is defined based on $Var(x)$ as in https://stackoverflow.com/questions/7653993/encountered-invalid-value-when-i-use-pearsonr. So how should I reliably compute correlation if I assume that my dataset could be constant?

• Upon running it, I get this error: numpy division with RuntimeWarning: invalid value encountered in double_scalars. So, have a look at this post if it helps. – Dawny33 Jun 27 '16 at 6:04
• You could try adding some very small noise to it so it will compute and be a small correlation. – PascalVKooten Jun 27 '16 at 8:49
• @PascalvKooten Then the result will be completely dependent on the noise you add. I can't see that solution as anything better than arbitrary. – Nick Cox Jun 27 '16 at 13:37
• @NickCox For approximation / solving 1 in X bad cases it could be fine. – PascalVKooten Jun 27 '16 at 17:46
• The idea of approximation appeals when and only when there is an identifiable solution. What are you approximating here? – Nick Cox Jun 27 '16 at 17:50

Correlation, meaning Pearson correlation, is not defined for such a case. If you consider the definition $\text{cov}(x, y)/[\text{sd}(x) \text{sd}(y)]$ you can see that if either standard deviation is zero, the correlation is indeterminate: as school mathematics underlined, you cannot divide by zero. This isn't a quirk of Python numerics; it's a generic problem.