Solving the Ornstein–Uhlenbeck SDE
We are given
$$
dr_t = \lambda(\theta-r_t)\,dt+\sigma\,dW_t,
\qquad r_0 \text{ given}.
$$
This is a linear stochastic differential equation.
When $\lambda>0$, it is the classic
Ornstein–Uhlenbeck process
(or Vasicek model in interest-rate modeling).
1Rearrange the SDE
Expand the drift:
$$
dr_t
=
\lambda\theta\,dt
-
\lambda r_t\,dt
+
\sigma\,dW_t.
$$
Move the $r_t$ term to the left:
$$
dr_t+\lambda r_t\,dt
=
\lambda\theta\,dt+\sigma\,dW_t.
$$
This now looks exactly like a first-order linear ODE.
2Use an integrating factor
For the deterministic equation
$y'(t)+\lambda y(t)=\cdots$,
the integrating factor is
$$
e^{\lambda t}.
$$
Multiply the whole SDE by $e^{\lambda t}$:
$$
e^{\lambda t}dr_t
+
\lambda e^{\lambda t}r_t\,dt
=
\lambda\theta e^{\lambda t}\,dt
+
\sigma e^{\lambda t}\,dW_t.
$$
3Recognize a product derivative
Using Itô's product rule,
$$
d\left(e^{\lambda t}r_t\right)
=
e^{\lambda t}dr_t
+
\lambda e^{\lambda t}r_t\,dt.
$$
There is no quadratic variation correction here because
$e^{\lambda t}$ is deterministic.
Therefore,
$$
d\left(e^{\lambda t}r_t\right)
=
\lambda\theta e^{\lambda t}\,dt
+
\sigma e^{\lambda t}\,dW_t.
$$
4Integrate from $0$ to $t$
$$
e^{\lambda t}r_t-r_0
=
\lambda\theta
\int_0^t e^{\lambda s}\,ds
+
\sigma
\int_0^t e^{\lambda s}\,dW_s.
$$
For $\lambda\neq0$,
$$
\lambda
\int_0^t e^{\lambda s}\,ds
=
e^{\lambda t}-1.
$$
Hence
$$
e^{\lambda t}r_t
=
r_0
+
\theta(e^{\lambda t}-1)
+
\sigma\int_0^t e^{\lambda s}\,dW_s.
$$
Final solution
Multiply by $e^{-\lambda t}$:
$$
\boxed{
r_t
=
\theta
+
(r_0-\theta)e^{-\lambda t}
+
\sigma
\int_0^t
e^{-\lambda(t-s)}
\,dW_s
}
$$
The solution has three pieces:
Long-run level
$$
\theta
$$
The value toward which the process is pulled when $\lambda>0$.
Initial condition
$$
(r_0-\theta)e^{-\lambda t}
$$
The effect of $r_0$ decays exponentially.
Random noise
$$
\sigma
\int_0^t e^{-\lambda(t-s)}\,dW_s
$$
Past Brownian shocks decay over time.
Distribution of $r_t$
The stochastic integral has a deterministic integrand, so it is
Gaussian. Therefore $r_t$ is normally distributed.
Mean
Since an Itô integral has mean zero,
$$
\boxed{
\mathbb E[r_t]
=
\theta+(r_0-\theta)e^{-\lambda t}
}.
$$
Variance
Using Itô isometry,
$$
\begin{aligned}
\operatorname{Var}(r_t)
&=
\sigma^2
\int_0^t
e^{-2\lambda(t-s)}
\,ds \\[4pt]
&=
\boxed{
\frac{\sigma^2}{2\lambda}
\left(1-e^{-2\lambda t}\right)
}
\qquad (\lambda\neq0).
\end{aligned}
$$
Thus
$$
\boxed{
r_t
\sim
\mathcal N
\left(
\theta+(r_0-\theta)e^{-\lambda t},
\frac{\sigma^2}{2\lambda}
(1-e^{-2\lambda t})
\right)
}
$$
Intuition when $\lambda>0$
$$
dr_t=\lambda(\theta-r_t)dt+\sigma dW_t.
$$
If $r_t<\theta$, then
$$
\theta-r_t>0
\quad\Rightarrow\quad
\text{positive drift}.
$$
If $r_t>\theta$, then
$$
\theta-r_t<0
\quad\Rightarrow\quad
\text{negative drift}.
$$
↓
The process is pulled back toward $\theta$.
Long-run distribution
When $\lambda>0$,
$$
e^{-\lambda t}\to0
\qquad\text{as }t\to\infty.
$$
Therefore,
$$
\mathbb E[r_t]\to\theta
$$
and
$$
\operatorname{Var}(r_t)
\to
\frac{\sigma^2}{2\lambda}.
$$
Hence the stationary distribution is
$$
\boxed{
r_\infty
\sim
\mathcal N
\left(
\theta,
\frac{\sigma^2}{2\lambda}
\right)
}.
$$
Interview way to recognize the solution
The important pattern is
$$
dX_t+aX_t\,dt
=
\text{something}.
$$
Your immediate thought should be:
$$
\boxed{\text{Integrating factor }e^{at}}
$$
So for this problem:
$$
dr_t+\lambda r_tdt
=
\lambda\theta dt+\sigma dW_t
$$
↓
$$
\text{multiply by }e^{\lambda t}
$$
↓
$$
d(e^{\lambda t}r_t)
=
\lambda\theta e^{\lambda t}dt
+
\sigma e^{\lambda t}dW_t
$$
↓
$$
\boxed{
r_t
=
\theta+(r_0-\theta)e^{-\lambda t}
+
\sigma\int_0^t
e^{-\lambda(t-s)}dW_s
}.
$$