# Sampling smooth paths

Sometimes it’s useful to generate random smooth paths that hang around the origin. The simplest way to obtain a random path is to generate a random walk where each step is independent and normally distributed. To make the path smooth, we have a couple of options: (1) we could smooth the path with a low-pass filter, or (2) we could integrate the random walk again such that the second derivative is normally distributed rather than the first derivative. However, one problem with these approaches is that the paths might stray arbitrarily far from the origin. Here’s a simple technique that I use to generate smooth paths that don’t stray too far.

Let’s consider the problem of sampling a path of length $N$ in one dimension. We can always sample multiple independent paths to obtain a multi-dimensional path. The idea is simply to sample from a Gaussian distribution with precision matrix (inverse covariance matrix)

where $\Lambda_1 = D_1^T D_1$ and $\Lambda_2 = D_2^T D_2$ and $D_{i}$ is a finite difference operator of order $i$.

Then finite difference matrix $D_i$ has shape $(N - i) \times N$.

To sample from the distribution $\mathcal{N}(0, \Lambda^{-1})$, we need a matrix $A$ such that $A A^{T} = \Lambda^{-1}$. This can be obtained by computing an eigendecomposition $\Lambda = V D V^T$ and setting $A = V D^{-\frac{1}{2}}$. To obtain a path, we then take $x = A \epsilon$ where $\epsilon$ is drawn from a unit normal distribution.

### Efficient technique

However, the above approach requires us to compute a factorization of a large (admittedly sparse) matrix.
This can be avoided if we are willing to instead consider sampling *periodic* smooth paths.
If we assume the paths to be periodic, then the finite difference matrices will be square and circulant.

Circulant matrices can be diagonalized using the Discrete Fourier Transform. For engineers like me, this diagonalization is hiding in the familiar convolution identity. Let $Y$ denote the matrix which corresponds to circular convolution with a signal $y$.

Here the $F$ matrix computes the DFT in the same way as the function `fft()`

in numpy.
Note that this matrix is not unitary but instead satisfies

For convenience, let us define $U = \frac{1}{\sqrt{N}} F$ so that $U^{-1} = U^{\ast}$ and the diagonalization can be expressed:

Now we can use this to diagonalize the finite difference matrix:

where $d_{1} = (-1, 1, 0, \dots, 0)$. The Gram matrix becomes

Finally, the entire precision matrix $\Lambda$ can therefore be diagonalized $\Lambda = U^{\ast} \operatorname{diag}(\lambda) U$ where

Note that $F \delta = 1$. Incidentally, since $d_2 = d_1 \star d_1$, we see that $F d_{2} = (F d_{1})^2$ and therefore $\lvert F d_{2} \rvert^2 = \lvert F d_{1} \rvert^4$.

Therefore, to sample from the distribution $\mathcal{N}(0, \Lambda^{-1})$, it seems that we could use the matrix

But wait, something seems wrong here.
The precision matrix $\Lambda$ is symmetric positive-definite and yet the eigenvectors in $U$ are complex?
In order to obtain samples $x = A \epsilon$, we need a *real* factorization $\Sigma = A A^{T}$.

### Real diagonalization

What’s happened here is that some eigenvalues occur twice and therefore the eigenvectors are not unique: we can take any (complex) rotation of two eigenvectors with the same eigenvalue and they are still unit-norm eigenvectors of the same matrix. It should be possible to mix these eigenvectors in a way that makes them real.

Recall that the Fourier transform of a real-valued signal has *conjugate* symmetry.
We can immediately observe that the transform $\lambda$ possesses *real* symmetry since it is obtained from the magnitude of a transform with conjugate symmetry.
Therefore the identical eigenvalues occur at $\lambda[k]$ and $\lambda[N - k]$.

From the diagonalization $\Lambda = U^{\ast} \operatorname{diag}(\lambda) U$ we can see that the eigenvalues correspond to rows of $U$. Happily, the rows of $U$ possess conjugate symmetry. Recall from the definition of the DFT matrix $F$ (which is related to the unitary matrix $U$ by a scalar) that element $s, t$ of the DFT matrix is $F_{s, t} = \omega_{N}^{s t}$ where $\omega_{N} = \exp(i 2 \pi / N)$. Using the fact that $\omega_{N}^{n} = \omega_{N}^{n \bmod N}$ we see the symmetry in the rows of $F$:

There are two special cases. For $s = 0$, we have $F_{s, t} = \omega_{N}^{0} = 1$, which is already real. For $s = N / 2$ (only when $N$ is even), we have $F_{s, t} = \omega_{N}^{(N/2) t} = (-1)^{t}$, which is also already real.

Therefore we propose to introduce an orthonormal complex matrix $Q$ such that $V = Q U$ is real and therefore $V^{T} = V^{\ast} = V^{-1}$. Let $U_{k}$ denote row $k$ of the matrix $U$. Let $Q_{k}$ denote the submatrix of elements $\{k, N - k\} \times \{k, N - k\}$ of the matrix $Q$. If we let $Q_{k}$ take the form

then we see that

which is real. To preserve orthogonality, we choose $\alpha = \beta = \frac{1}{\sqrt{2}}$. This will have no effect on the eigenvalues since

Overall our $Q$ matrix takes the form

Finally, we have our real $A = U^{\ast} Q^{\ast} \operatorname{diag}(\lambda^{-\frac{1}{2}})$. Since $U^{\ast} = \frac{1}{\sqrt{N}} F^{\ast} = \sqrt{N} F^{-1}$, samples can be obtained

Note that the operation $Q^{\ast}$ can be implemented as follows

When $h$ is real and symmetric (as above), this is equivalent to

Here are a few smaples from this distribution. I often use $\alpha = 0$ and $\beta \in [10^3, 10^6]$. Note that, since the paths are constrained to be periodic, shorter paths may be tightly clustered.

Of course, if we want to generate non-periodic paths, we can generate a periodic path of a larger size and then truncate it.