Statistics
Pearson correlation coefficient
stats.pearson-correlationAlso called pearson correlation, pearson r, correlation, corr
where
where
Covariance of two paired samples divided by the product of their standard deviations; the divisors cancel, so the result lies in [−1, 1] whichever convention is used, as long as it is used throughout.
Notation
first variable
x ∈ ℝn
second variable
y ∈ ℝn
number of observations
Size, at least 2
Result: the correlation, in [−1, 1]
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ 0.964763821238
Given
Result
Given
Result
≈ 0.5
Common mistakes
Sample covariance over population standard deviations
The covariance divides by n − 1 but the standard deviations divide by n, so the result is n/(n − 1) times too large and can exceed 1.
Fix: Use the same divisor in the covariance and both standard deviations.
Population covariance over sample standard deviations
The covariance divides by n but the standard deviations divide by n − 1, so the result is (n − 1)/n times too small.
Fix: Use the same divisor in the covariance and both standard deviations.
Source
Pearson 1895
Pearson, Karl. Note on regression and inheritance in the case of two parents. Proceedings of the Royal Society of London 58: 240-242, 1895. doi:10.1098/rspl.1895.0041