Question: In $\mathbb{R}^2$, it is given $n$ points $(x_i,Y_i)$, where $x_i$ are known constants, and given independent random variables $Y_i\sim U(l_i,r_i)$. Fit a straight line through these $n$ points using the method of least squares. Find the expected value of the sum of squares of the residuals.
My solution is as follows, and I wonder what the mistakes are.
By the least square, it is known that the slope and the intercept of the fitted line are
$$k=\frac{\sum(x_i-\bar{x})(y_i-\bar{y})}{\sum(x_i-\bar{x})^2},\ b=\bar{y}-k\bar{x}$$
where $\bar{x},\bar{y}$ are the average values.
It is required: $\mathbb{E}\left[\sum(kx_i+b-Y_i)^2\right]$
First due to $Y_i\sim U(l_i,r_i)$, we can immediately get:
$$\mathbb E[Y_i]=\dfrac{l_i+r_i}{2}, \mathrm{Var}(Y_i)=\dfrac{(r_i-l_i)^2}{12}$$
$$\mathbb{E}[\bar{Y}]=\frac{1}{n}\sum\mathbb E[Y_i], \mathrm{Var}(\bar{Y})=\frac{1}{n^2}\sum\mathrm{Var}(Y_i)$$
$$\mathrm{Cov}(Y_i,\bar{Y})=\frac{\mathrm{Var}(Y_i)}{n}$$
$$\begin{aligned}\mathrm{Var}(Y_i-\bar Y)&=\mathrm{Var}(Y_i)+\mathrm{Var}(\bar Y)-2\mathrm{Cov}(Y_i,\bar Y)\\&=\left(1-\frac2n\right)\mathrm{Var}(Y_i)+\mathrm{Var}(\bar Y)\end{aligned}$$
Then by the linearity of expectations:
$$\begin{aligned}&\mathbb{E}\left[\sum(kx_i+b-Y_i)^2\right]\\ &= \sum\mathbb{E}\left[(kx_i+b-Y_i)^2\right]\\&=\sum\mathbb{E}\left[[k(x_i-\bar{x})-(Y_i-\bar{Y})]^2\right]\\&=\sum\mathbb{E}\left[k^2(x_i-\bar{x})^2+(Y_i-\bar{Y})^2-2k(x_i-\bar{x})(Y_i-\bar{Y})\right]\\&= \mathbb{E}[k^2]\sum(x_i-\bar{x})^2+\sum\mathbb{E}[(Y_i-\bar{Y})^2]-\mathbb{E}\left[\sum2k(x_i-\bar{x})(Y_i-\bar{Y})\right]\end{aligned}$$
For the second term, by the formula $\mathrm{Var}(X)=\mathbb{E}[X^2]-(\mathbb{E}[X])^2$,
$$\begin{aligned}\sum\mathbb{E}[(Y_i-\bar{Y})^2] &= \sum\mathrm{Var}(Y_i-\bar{Y})+\sum(\mathbb{E}[Y_i-\bar{Y}])^2\\\sum\mathrm{Var}(Y_i-\bar{Y})&=\sum\mathrm{Var}(Y_i)+\mathrm{Var}(\bar{Y})-2\mathrm{Cov}(Y_i,\bar{Y})=n(n-1)\mathrm{Var}(\bar{Y})\\\sum(\mathbb{E}[Y_i-\bar{Y}])^2&=\sum(\mathbb {E}[Y_i]-\mathbb{E}[\bar{Y}])^2\end{aligned}$$
For the third term, plug in $k$,
$$\begin{aligned}&=\mathbb{E}\left[2k\sum(x_i-\bar{x})(Y_i-\bar{Y})\right]\\&=\mathbb{E}\left[2\cdot\frac{\sum(x_i-\bar{x})(Y_i-\bar{Y})}{\sum(x_i-\bar{x})^2}\cdot\sum(x_i-\bar{x})(Y_i-\bar{Y})\right]\\&=2\mathbb{E}\left[\frac{\left(\sum(x_i-\bar{x})(Y_i-\bar{Y})\right)^2}{\sum(x_i-\bar{x})^2}\right]\\&=2\mathbb{E}[k^2]\sum(x_i-\bar{x})^2\end{aligned}$$
which can be merged with the first term.
Now consider $\mathbb{E}[k^2]=\mathrm{Var}(k)+(\mathbb{E}[k])^2$.
$$\begin{aligned}\mathrm{Var}(k) &= \mathrm{Var}\left(\frac{\sum(x_i-\bar{x})(Y_i-\bar{Y})}{\sum(x_i-\bar{x})^2}\right)\\&=\dfrac{\sum(x_i-\bar{x})^2\mathrm{Var}(Y_i-\bar Y)}{\left(\sum(x_i-\bar{x})^2\right)^2}\\\mathbb{E}[k]&=\mathbb{E}\left[\frac{\sum(x_i-\bar{x})(Y_i-\bar{Y})}{\sum(x_i-\bar{x})^2}\right]\\&=\frac{\sum(x_i-\bar{x})(\mathbb{E}[Y_i]-\mathbb E[\bar{Y}])}{\sum(x_i-\bar{x})^2}\\\end{aligned}$$
To conclude, the answer is:
$$\begin{aligned} n(n-1)\mathrm{Var}(\bar{Y})+\sum(\mathbb {E}[Y_i]-\mathbb{E}[\bar{Y}])^2-\dfrac{\sum(x_i-\bar{x})^2\mathrm{Var}(Y_i-\bar Y)}{\sum(x_i-\bar{x})^2} -\frac{\left(\sum(x_i-\bar{x})(\mathbb{E}[Y_i]-\mathbb E[\bar{Y}])\right)^2}{\sum(x_i-\bar{x})^2}\end{aligned}$$