Sample correlation coefficient (source code)

= Sample correlation coefficient
{title2=$r_{XY}$}

= Sample correlation
{synonym}

For paired observations $(x_i,y_i)$, the <sample correlation coefficient> is
$$
r_{XY}=\frac{\sum_i(x_i-\overline x)(y_i-\overline y)}
{\sqrt{\sum_i(x_i-\overline x)^2\sum_i(y_i-\overline y)^2}}.
$$
It is the normalized <inner product> of the centered data <vectors>, or the <sample covariance> divided by the product of the corresponding sample <standard deviations>. The choice of divisor $n$ or $n-1$ cancels when used consistently in all three quantities. It is undefined if either centered data <vector> is zero, which occurs when that variable is constant in the observed <statistical sample>.

For $x=(1,2,3,4)$ and $y=(2,1,4,3)$, both <sample means> are $2.5$. The sum of centered products is $3$, and each sum of centered squares is $5$, hence \b[the sample correlation coefficient is $3/5=0.6$]. This is a summary of these four observed pairs; it is not automatically the population <Pearson correlation coefficient>. https://www.stat.berkeley.edu/~stark/SticiGui/Text/computeR.htm[Philip Stark, Computing the correlation coefficient] explains the centered and standardized calculation.