Deriving the identity: $\hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}$ For some reason I am having an extremely hard time finding out how the following expression is derived 
$$
\hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}
$$
Is there some kind of identity that I am missing in order to derive this expression?
I've tried plugging in $\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}$ in to $\sum x_i y_i = \hat{\beta}_0 \sum x_i + \hat{\beta}_1 \sum x_i^2$, which gives me
$$
\hat{\beta}_1 = \frac{\sum x_iy_i - \bar{y} \sum x_i}{\sum x_i^2 - \bar{x} \sum x_i}
$$
but I do not see how I should proceed from here. I feel that there should be an identity linking $\bar{x}$ or $\bar{y}$ to $\sum x_i y_i$ or $\sum x_i^2$, but nothing comes to mind at the moment.
 A: You may observe that
$$
\hat{\beta}_1 =\frac{\sum_{i=1}^n x_i y_i - n \bar{x}\bar{y}}{\sum_{i=1}^n x_i^2 -n\bar{x}^2}
=\frac{\frac1n\sum_{i=1}^n x_i y_i - \bar{x}\bar{y}}{\frac1n\sum_{i=1}^n x_i^2 -\bar{x}^2}.\tag1 $$ Then you may prove that
$$
\frac1n\sum_{i=1}^n \left(x_i -\bar{x}\right)^2=\frac1n\sum_{i=1}^n x_i^2 -\bar{x}^2 \tag2
$$ and that
$$
\frac1n\sum_{i=1}^n \left(x_i -\bar{x}\right)\left(y_i -\bar{y}\right)=\frac1n\sum_{i=1}^n x_iy_i -\bar{x}\bar{y}. \tag3
$$ 
Let's see a proof of $(2)$. We have
$$
\begin{align}
\frac1n\sum_{i=1}^n \left(x_i -\bar{x}\right)^2&=\frac1n\sum_{i=1}^n \left(x^2_i -2x_i\bar{x}+\bar{x}^2\right)
\\\\&=\frac1n\sum_{i=1}^n x^2_i -2\bar{x}\frac1n\sum_{i=1}^nx_i+\frac1n\sum_{i=1}^n\bar{x}^2
\\\\&=\frac1n\sum_{i=1}^n x^2_i -2\:\bar{x}\times \bar{x}+\frac1n \times n\:\bar{x}^2
\\\\&=\frac1n\sum_{i=1}^n x^2_i -\bar{x}^2.
\end{align}
$$ Now use a similar path to get $(3)$.
Thus, you obtain
$$
\hat{\beta}_1 =\frac{\sum_{i=1}^n x_i y_i - n \bar{x}\bar{y}}{\sum_{i=1}^n x_i^2 -n\bar{x}^2}
=\frac{\frac1n\sum_{i=1}^n x_i y_i - \bar{x}\bar{y}}{\frac1n\sum_{i=1}^n x_i^2 -\bar{x}^2}=\frac{\frac1n\sum_{i=1}^n \left(x_i -\bar{x}\right)\left(y_i -\bar{y}\right)}{\frac1n\sum_{i=1}^n \left(x_i -\bar{x}\right)^2}.\tag4
$$
A: Here's a neat trick. Notice that the formula you cited
$$
\hat{\beta}_1 = \frac{\sum x_iy_i - \bar{y} \sum x_i}{\sum x_i^2 - \bar{x} \sum x_i}
$$
simplifies to
$$
\hat{\beta}_1 = \frac{\sum x_iy_i}{\sum x_i^2}
$$
in the special case where the $x$'s average to zero and the $y$'s average to zero. So if we modify our data to replace each $x_i$ with $x_i-\bar x$ and each $y_i$ with $y_i-\bar y$, we'll get the expression you're after:
$$
\hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}
$$
Now the insight is to observe that modifying our data in this way doesn't change the slope of the regression line, since it amounts to a horizontal and/or vertical shift of the entire scatter plot. In other words, as far as the slope of the regression line is concerned, it is safe to make this modification to the data: the special case is the general case.
