For building a recommendation system, I also use the Pearson correlation coefficient. This is the definition:

$r(x, y)=\frac{\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})}{\sqrt{\sum_{i=1}^n (x_i-\bar{x})^2 \cdot \sum_{i=1}^n (y_i-\bar{y})^2}}$

$x$ and $y$ are part of $\mathbb{R}$.

Now for coding, it is important to take care of all potential outcomes. For example, if the denominator is zero, you will have to filter that or throw an exception.

I came up with some arguments, one of them being that if all values of $x_i$ and/or $y_i$ were equal to the average of $x$ and/or $y$, then the denominator would be zero.

But how can I prove that the coefficient is either undefined (zero denominator) or in between -1 and 1? What is the best approach?

  • 2
    $\begingroup$ It is the Schwartz Inequality (but I keep misspelling his name). Depending where you are from, you might call it Cauchy Schwartz, or throw in Bunyakovsky, maybe others. $\endgroup$ – André Nicolas Nov 13 '13 at 0:50

First of all Pearson's correlation coefficient is bounded between -1 and 1, not 0 and one. It's absolute value is bounded between 0 and 1, and that useful later.

Pearson's correlation coefficient is simply this ratio:

$\rho = \frac{Cov(X,Y)}{\sqrt{Var(X)Var(Y)}}$

Both of the variances are non-negative by definition, so the denominator is $\ge 0$. The only way a singularity can occur is if one of the variables has 0 variance.

If two random variables are perfectly uncorrelated, (i.e. independent) then their covariance is 0. So 0 is a valid lower bound.

This can be shown like so:

$Cov(X,Y) = E[(X-\bar{X})(Y-\bar{Y})] = E[XY] - E[X]E[Y]$

if two random variables are independent, then $E[XY]=E[X]E[Y]$, and

$Cov(X,Y) = E[XY] - E[X]E[Y] = E[X]E[Y] - E[X]E[Y] = 0$.

Now for the upper bound. Here we apply the Cauchy-Schwarz inequality.

$|Cov(X,Y)|^2 \le Var(X)Var(Y)$

$\therefore |Cov(X,Y)| \le \sqrt{Var(X)Var(Y)}$

plug this result from the Cauchy-Schwarz inequality into the formula for $\rho$, and we get:

$|\rho| = |\frac{Cov(X,Y)}{\sqrt{Var(X)Var(Y)}}| \le \frac{\sqrt{Var(X)Var(Y)}}{\sqrt{Var(X)Var(Y)}} = 1$

Thus we have the absolute value of the correlation is bounded below by 0 and above by 1.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.