A (standard) Wiener process is a continuous-time stochastic process {W(t)}t0 with initial value W(0)=0 and instantaneous increments dW(t)N(0,dt). We can simulate such a process as follows. First, create a sequence of times t at which to store the value of W(t):

t_max = 100
dt = 1e-2

t = seq(0, t_max, by = dt)

Increasing t_max creates a longer path, while decreasing dt creates a smoother path. Now simulate the random increments and take their cumulative sum:

dW = rnorm(length(t) - 1, mean = 0, sd = sqrt(dt))
W = c(0, cumsum(dW))

Here are three sample paths generated by this procedure:

We can use {W(t)}t0 to construct an Ornstein-Uhlenbeck process {X(t)}t0. This process has instantaneous increments dX(t)=θX(t)dt+dW(t), where θ0 controls the process’ tendency to mean-revert. We can compute its values X(t) by iterating over dW:

theta = 1

X = rep(0, length(dW))
i = 1
while (i < length(dW)) {
  X[i + 1] = X[i] - theta * X[i] * dt + dW[i + 1]
  i = i + 1
}

The chart below compares the sample paths obtained using different θ values. Each path uses the same realization of the underlying Wiener process {W(t)}t0. If θ=0 then X(t)=W(t) for all t0. The mean magnitude of X(t) falls as θ rises because this makes the process more mean-reverting.