I have the following process
$$dX_t = (b-aX_t)dt + dW_t$$ $$X_0 = x_0$$ with $a,b,x_0$ constants, and I wish to calculate the variance. (I have already solved for the process and for the expected value). To find $E(X^2)$, my idea was to use to set $Z_t = X_t^2$, apply Itos lemma and take expectations on each side. That is $$dZ_t = 2X_tdX + 2(dX)^2$$ $$= 2X_t(b-aX_t)dt +2X_tdW_t+2dt$$ Integrating from $0$ to $t$ and taking expectations $$E(Z_t) = x_0^2 +2b\int_0^tE(X_s)ds-2a\int_0^tZ_sds + 2t$$ where $E(X_t) = e^{-at}(x_0 - \frac{b}{a})+\frac{b}{a}$.
I have no idea how to solve the above equation. Is there another way to calculate this variance that I am missing?
The equation gets a little bit easier if you use Itô's formula to calculate $\mathbb{E}((X_t-\mathbb{E}(X_t))^2)$ instead of $\mathbb{E}(X_t^2)$.
Set $\mu_t := \mathbb{E}(X_t) = e^{-at} \left( x_0- \frac{b}{a} \right) + \frac{b}{a}$. Applying Itô's formula to
$$Y_t := (X_t-\mu_t)^2$$
gives
$$dY_t = 2 (X_t-\mu_t)(b-a X_t-\mu'(t)) + 2 (X_t-\mu_t) \, dW_t + 2 dt,$$
and this implies
$$\mathbb{E}(Y_t^2) = 2t -2a \int_0^t \mathbb{E}(Y_s) \, ds.$$
This means that $f(t) := \mathbb{E}(Y_t^2)$ is a solution to the ordinary differential equation
$$f'(t) = 2 - 2a f(t) \qquad f(0)=0.$$
Solve it...