# Why is the Pearson correlation 1 when only two data values are available?

I am trying to obtain a Pearson correlation between 6 different variables (represented by columns in the matrix below) with two datapoints each (rows). This is the matrix:

     scer       bay      par       mik      glab       lac
var1 2.2273444 2.0923416 2.044007 1.7664921 1.3832924 2.4294228
var2 0.3000878 0.2792936 0.286928 0.3246768 0.4946222 0.3083171


When I apply the standard R code for correlation:

cor(mat)


I obtain the following result:

     scer bay par mik glab lac
scer    1   1   1   1    1   1
bay     1   1   1   1    1   1
par     1   1   1   1    1   1
mik     1   1   1   1    1   1
glab    1   1   1   1    1   1
lac     1   1   1   1    1   1


If I add another two rows to the original matrix:

                scer       bay       par       mik      glab       lac
var1    2.2273444 2.0923416 2.0440068 1.7664921 1.3832924 2.4294228
var2    0.3000878 0.2792936 0.2869280 0.3246768 0.4946222 0.3083171
var3    1.1399738 1.2899311 1.1071462 1.0180361 1.4507592 2.4078977
var4    0.7107440 0.6415944 0.7197905 0.7357125 0.4571745 0.3173547


and re-execute the above code with the new matrix, I obtain a more familiar result:

          scer       bay       par       mik      glab       lac
scer 1.0000000 0.9895959 0.9991065 0.9967358 0.7860344 0.8246286
bay  0.9895959 1.0000000 0.9916464 0.9890492 0.8647974 0.8958393
par  0.9991065 0.9916464 1.0000000 0.9991332 0.7928330 0.8310776
mik  0.9967358 0.9890492 0.9991332 1.0000000 0.7845007 0.8235245
glab 0.7860344 0.8647974 0.7928330 0.7845007 1.0000000 0.9978420
lac  0.8246286 0.8958393 0.8310776 0.8235245 0.9978420 1.0000000


Why does the correlation function return a matrix of 1s if I use 2 values?

• Please start sentences with capital letters and end them with appropriate punctuation. And use "I" when that's what you mean. Writing otherwise may be quicker or seem cuter, but it just makes questions more difficult to read, which is not in your interest. – Nick Cox Apr 17 '14 at 16:19
• the statistical question is whether there is some inherent limitation in calculating Pearson correlation with only 2 datapoints. – Tito Candelli Apr 17 '14 at 16:23
• This is actually the answer i was looking for, thank you. I will mark it as such if you post it. – Tito Candelli Apr 17 '14 at 16:25
• There really isn't a clear question here. Although you ask for a reason why you cannot "perform Pearson correlation," you actually have done so, as evidenced after "obtained the following result." From the comments it sounds like you should change the entire text of your question to read "Why is the Pearson correlation $\pm 1$ when only two distinct data values are available?" – whuber Apr 17 '14 at 16:28
• @whuber in hindsight i agree with you, i will fix the title and the text. i wasn't sure if the matrix of 1s were really a result or some sort of erroneous output due to the fact that the correlation could not be performed correctly. – Tito Candelli Apr 17 '14 at 16:30

Correlation, meaning Pearson correlation, can be thought of as a numerical answer to the question: Is there a linear relationship between two variables?

If you have two distinct data points, the only possible correlation result is $+1$ or $-1$, because two such points define a perfect linear relationship.

This matches the observation that a straight line can be found to interpolate two distinct points exactly.

The only choice is between a rising and a falling straight line, which give $+1$ or $-1$ respectively.

(If your two points are identical on either of the two variables, the correlation is indeterminate.)

In scientific terms, a correlation involving just two points is useless by itself.

• Stated another way: Pearson's correlation coefficient between two random variables $\zeta$ and $\eta$ will be +/-1 when you can find an $a$ and $b$ such that $\zeta = a + b \eta$. This can be easily done if you only have one sample for each variable by setting $a$ as the difference between the two variables. – Nathan Apr 17 '14 at 16:47
• @Nathan You need an extra condition $b \ne 0$. Otherwise the correlation is also 0. – Nick Cox Sep 16 '17 at 10:01

Besides Nick's intuitive and graphical explanation, I believe it is valid to also point out a mathematical one. Consider two variables with two positive observations each:

$X = \{x_1, x_2\}$

$Y = \{y_1, y_2\}$

Now let's calculate the standard deviation of $X$:

$S_X = \sqrt{\frac{\sum{X^2} - n\bar{X}^2}{n - 1}} = \sqrt{\frac{x_1^2 + x_2^2 - 2\frac{(x_1 + x_2)^2}{2^2}}{2 - 1}} = \sqrt{x_1^2 + x_2^2 - \frac{1}{2}(x^2 + 2x_1x_2 + x^2)} = \sqrt{\frac{1}{2}(2x_1^2 + 2x_2^2 - x_1 - 2x_1x_2 - x_2)} = \sqrt{\frac{1}{2}(x_1^2 + x_2^2 - 2x_1x_2)} = \sqrt{\frac{1}{2}(x_1 - x_2)^2} = \frac{1}{\sqrt{2}}(x_1 - x_2)$

That means the standard deviation of $Y$ is equal to $\frac{1}{\sqrt{2}}(y_1 - y_2)$.

Now let's calculate the covariance of $X$ and $Y$:

$S_{XY} = \frac{\sum{XY} - n\bar{X}\bar{Y}}{n - 1} = \frac{x_1y_1 + x_2y_2 - 2\frac{(x_1+x_2)}{2}\frac{(y_1+y_2)}{2}}{2 - 1} = x_1y_1 + x_2y_2 - \frac{1}{2}(x_1+x_2)(y_1+y_2) = \frac{1}{2}(2x_1y_1 + 2x_2y_2 - x_1y_1 - x_1y_2 - x_2y_1 - x_2y_2) = \frac{1}{2}(x_1y_1 + x_2y_2 - x_1y_2 - x_2y_1) = \frac{1}{2}(x_1y_1 + x_2y_2 - x_1y_2 - x_2y_1)$

The correlation between $X$ and $Y$, $R_{XY}$, is defined as follows:

$R_{XY} = \frac{S_{XY}}{S_XS_Y} = \frac{\frac{1}{2}(x_1y_1 + x_2y_2 - x_1y_2 - x_2y_1)}{\frac{1}{\sqrt{2}}(x_1 - x_2)\frac{1}{\sqrt{2}}(y_1 - y_2)} = \frac{(x_1y_1 + x_2y_2 - x_1y_2 - x_2y_1)}{(x_1 - x_2)(y_1 - y_2)} = \frac{(x_1y_1 + x_2y_2 - x_1y_2 - x_2y_1)}{(x_1y_1 + x_2y_2 - x_1y_2 - x_2y_1)} = 1$

If you define some of those values as negative so that the covariance would yield some negative products, the correlation would be -1. Compare, for instance, the following results in R:

> cor(c(1, 2), c(3, 4))
[1] 1
> cor(c(1, 2), c(-3, -4))
[1] -1
> cor(c(1, 2), c(-3, 4))
[1] 1
> cor(c(1, -2), c(-3, 4))
[1] -1
> cor(c(-1, -2), c(-3, -4))
[1] 1

• (1) Why do you assume the $x_i$ and $y_j$ are positive in your example? (2) The calculation threatens to obscure the essential point. It can be avoided by noting that the standardized values of the $x_i$ are $\{1,-1\}$ and the standardized values of the $y_j$ are the same. The correlation, by definition, is the average product. Depending on how the $x_i$ are associated with the $y_j,$ all products are either $1$ or $-1$, QED. – whuber Apr 18 '14 at 0:37
• @whuber, (1) I assumed positive values to make the covariance calculations easier and to be in line with the OP data; I hope my last paragraph is enough to shed a light on cases with negative values. (2) You're right, my answer is more convoluted than it needs to be, and I appreciate your observation on the standardized values. However, I feel my basic-yet-verbose approach contributes to the discussion and might even be easier to understand for some folks. – Waldir Leoncio Apr 18 '14 at 0:59
• Thank you both for your constructive answers and discussions. i really appreciate it. – Tito Candelli Apr 18 '14 at 11:19
• @whuber What do you mean by standardized value? Because I thought that for $x_i$ that is $\frac{x_i - μ_x}{\sigma_x}$, but then that is $\frac{1}{\sqrt{2}}$ and not 1? – user106860 Apr 25 at 15:17
• @user106860 It doesn't matter how you standardize, but the result is immediate when you use the empirical standard deviation (for which the values are $\pm 1$ as I claimed) rather than the standard deviation estimator. – whuber Apr 25 at 15:20