If we do linear regression we have the following decomposition of the sum of square. I call $y_i,x_i,\hat{y}_i$ the observed variables, the predictors and the linear predictions respectively. Then the residuals are $e_i=y_i-\hat{y}_i$. We also indicate with on overline the mean over all samples. Than:
$$\sum_i (y_i-\overline{y})^2=\sum_i \left(\hat{y}_i-\overline{\hat{y}}\right)^2+\sum_i (e_i-\overline{e})^2 \tag{1}$$
There are other few properties ($\overline{y}=\overline{\hat{y}}$ and $\overline{e}=0$).
A similar formula comes from the theory of conditional expectation. Given two r.v. $X,Y$:
$$Var(Y)=Var(E[Y|X])+E[Var(Y|X)] \tag{2}$$
I see some similarities in these two formulas, if we consider the $\hat{y}_i$ related to $E[Y|X]$.
My question is this. Can the first formula be derived from the second? How deep is their connection (if indeed there is one) ?