Why is the correlation between independent variables/regressor and residuals zero for OLS? In page 4 of https://web.stanford.edu/~mrosenfe/soc_meth_proj3/matrix_OLS_NYU_notes.pdf, it states that the regressors have zero correlation with the residuals for OLS, but I don't think this is true.
The assertion is based on the fact that
$$
X^Te = 0
$$
where $e$ are the residuals $y - \hat{y}$.
But why does this mean the regressor is uncorrelated with the residual?
I tried to derive this using the definition of covariance for 2 random variables. $X_p$ is the random variable corresponding to the p-th regressor.
\begin{align}
    cov(X_p, e) = E[(X_p - \mu_{X_p})(e - \mu_e)]  \\
    cov(X_p, e) = E[(X_p - \mu_{X_p})(e - \mu_e)]  \\
    = E[X_p e - \mu_{X_p} e - \mu_e X_p + \mu_{X_p} \mu_e] \\
    = E[X_p e] - \mu_{X_p} \mu_e
\end{align}
We know that $E[X_p e] = 0$, but $X_p$ is only uncorrelated with $e$ if one of their means are zero.
Edit. I think there may be a mistake in my derivation. I do not believe $E[X_p e] = 0$.
 A: In any model with an intercept, the residuals are uncorrelated with the predictors $X$ by construction; this is true whether or not the linear model is a good fit and it has nothing to do with assumptions.
It's important here to distinguish between the residuals and the unobserved things often called the errors.
The covariance between residuals $R$ and $X$ is
$$\frac{1}{n}\sum RX-\frac{1}{n}(\sum R)\frac{1}{n}(\sum X)$$
If the model includes an intercept $\sum R=0$, so the covariance is just $\frac{1}{n}\sum RX$. But the Normal equations to estimate $\hat\beta$ are
$X(Y-\hat Y)=0$, ie, $\frac{1}{n}\sum XR=0$.
So the residuals and $X$ are exactly uncorrelated.
When there is actually a model
$$Y = X\beta+e$$
the assumption that the errors $e$ are uncorrelated with $X$ is necessary to make $\hat\beta$ unbiased for $\beta$ (and we assume the errors have mean zero to make the intercept identifiable). So $E[X^Te]=0$ is an assumption, not a theorem.
The residuals typically are not uncorrelated with $Y$. Neither are the errors.
