All formulas

Statistics

Pearson correlation coefficient

stats.pearson-correlationAlso called pearson correlation, pearson r, correlation, corr

r=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2∑i=1n(yi−yˉ)2r = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=1}^{n}(x_i - \bar{x})^2}\sqrt{\sum_{i=1}^{n}(y_i - \bar{y})^2}}

wherexˉ=1n∑j=1nxj\bar{x} = \frac{1}{n}\sum_{j=1}^{n} x_j

whereyˉ=1n∑j=1nyj\bar{y} = \frac{1}{n}\sum_{j=1}^{n} y_j

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

xx

first variable

x ∈ ℝn

yy

second variable

y ∈ ℝn

nn

number of observations

Size, at least 2

rr

Result: the correlation, in [−1, 1]

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    x=[1,2,3,4],y=[2,4,5,9]x = [1, 2, 3, 4], y = [2, 4, 5, 9]

    Result

    r=11130130\displaystyle r = \frac{11 \sqrt{130}}{130}

    ≈ 0.964763821238

  2. Given

    x=[1,2,3],y=[3,2,1]x = [1, 2, 3], y = [3, 2, 1]

    Result

    r=−1\displaystyle r = -1
  3. Given

    x=[0,1,2],y=[1,0,2]x = [0, 1, 2], y = [1, 0, 2]

    Result

    r=12\displaystyle r = \frac{1}{2}

    ≈ 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

    doi.org