The Ornstein-Uhlenbeck process is widely used in the stochastic modelling of mean-reverting processes. In the present note, I want to record a derivation I produced for a lecture of the pdf and moments of an Ornstein-Uhlenbeck process exhibiting mean-reversion to zero with a stochastic differential equation (SDE) of the form
where and
are constants and
is a standard Wiener process. I used the Euler–Maruyama method with a time step of
in Python to generate and plot a sample path of this process from
up to
, setting the initial position as
and the values
and
. The following picture shows this sample path:

The mean-reversion to zero is clear from the plot and arises due to the parameter in the drift term of the SDE, which tends to pull the process back down when
goes above zero and back up when
goes below zero.
The Fokker-Planck equation of the SDE in (1) is easily obtained as
and this can be used to derive a stationary pdf for the Ornstein-Uhlenbeck process corresponding to a long-term equilibrium situation in which the pdf is not time-varying, so . From (2) we then get
Equation (3) tells us that the sum of the two terms in the square brackets must be equal to a constant. Setting this constant equal to zero gives us
and so
from which we deduce the form of the stationary pdf to be
We can find an expression for from the normalisation condition
Making the change of variable we can rewrite (4) as
from which we get
where
The moments of the Ornstein-Uhlenbeck process are obtained as
Making the change of variable , we have
and
. Using these substitutions together with the expression for
in (5) above we get
where
From (8), we see that the first two moments of the Ornstein-Uhlenbeck process are
and
These can be computed directly in Python using code such as the following (quad is from the scipy.integrate library):

This is useful to know for more complicated stochastic processes where the integrals ,
and
might be difficult to evaluate manually, but in the case of the Ornstein-Uhlenbeck process these three integrals have simple exact values. For
in (6), we use the well-known trick of solving the double integral
Changing to polar coordinates and
, with limits
to
for
and
to
for
, and the Cartesian area element
becoming the polar coordinates area element
, the double integral becomes
Since (12) is the square of , we conclude
. It is easily shown that
, and we can calculate
by integrating
by parts:
Thus,
We can therefore write the first two moments of the Ornstein-Uhlenbeck process with the SDE in (1) above in exact form as
and
To check these calculations, I used Python to compute sample paths of the Ornstein-Uhlenbeck process
with the same specifications as the plot above, and plotted the mean
and mean squared distance
as functions of time together with the corresponding exact values. The plots below show that for sufficiently long times the numerical calculations do indeed fluctuate around the values predicted on the basis of the stationary pdf derived above:

I also calculated the histogram of the probability density of the random variable evaluated at
using the
simulated sample paths, and plotted it together with the stationary pdf,
, derived above from the Fokker-Planck equation. The plot below shows good agreement between the two:

