(GP-Kron)=

Kronecker Structured Covariances

:::{post} October, 2022 :tags: gaussian process :category: intermediate :author: Bill Engels, Raul-ing Average, Christopher Krapu, Danh Phan, Alex Andorra :::

PyMC contains implementations for models that have Kronecker structured covariances. This patterned structure enables Gaussian process models to work on much larger datasets. Kronecker structure can be exploited when

  • The dimension of the input data is two or greater ($\mathbf{x} \in \mathbb{R}^{d},, d \ge 2$)
  • The influence of the process across each dimension or set of dimensions is separable
  • The kernel can be written as a product over dimension, without cross terms:

k(x,x)=i=1dk(xi,xi).k(\mathbf{x}, \mathbf{x'}) = \prod_{i = 1}^{d} k(\mathbf{x}_{i}, \mathbf{x'}_i) \,.

The covariance matrix that corresponds to the covariance function above can be written with a Kronecker product

K=K2K2Kd.\mathbf{K} = \mathbf{K}_2 \otimes \mathbf{K}_2 \otimes \cdots \otimes \mathbf{K}_d \,.

These implementations support the following property of Kronecker products to speed up calculations, $(\mathbf{K}1 \otimes \mathbf{K}2)^{-1} = \mathbf{K}{1}^{-1} \otimes \mathbf{K}{2}^{-1}$, the inverse of the sum is the sum of the inverses. If $K_1$ is $n \times n$ and $K_2$ is $m \times m$, then $\mathbf{K}_1 \otimes \mathbf{K}_2$ is $mn \times mn$. For $m$ and $n$ of even modest size, this inverse becomes impossible to do efficiently. Inverting two matrices, one $n \times n$ and another $m \times m$ is much easier.

This structure is common in spatiotemporal data. Given that there is Kronecker structure in the covariance matrix, this implementation is exact -- not an approximation to the full Gaussian process. PyMC contains two implementations that follow the same pattern as {class}gp.Marginal <pymc.gp.Marginal> and {class}gp.Latent <pymc.gp.Latent>. For Kronecker structured covariances where the data likelihood is Gaussian, use {class}gp.MarginalKron <pymc.gp.MarginalKron>. For Kronecker structured covariances where the data likelihood is non-Gaussian, use {class}gp.LatentKron <pymc.gp.LatentKron>.

Our implementations follow Saatchi's Thesis. gp.MarginalKron follows "Algorithm 16" using the Eigendecomposition, and gp.LatentKron follows "Algorithm 14", and uses the Cholesky decomposition.

Using MarginalKron for a 2D spatial problem

The following is a canonical example of the usage of gp.MarginalKron. Like gp.Marginal, this model assumes that the underlying GP is unobserved, but the sum of the GP and normally distributed noise are observed.

For the simulated data set, we draw one sample from a Gaussian process with inputs in two dimensions whose covariance is Kronecker structured. Then we use gp.MarginalKron to recover the unknown Gaussian process hyperparameters $\theta$ that were used to simulate the data.

Example

We'll simulate a two dimensional data set and display it as a scatter plot whose points are colored by magnitude. The two dimensions are labeled x1 and x2. This could be a spatial dataset, for instance. The covariance will have a Kronecker structure since the points lie on a two dimensional grid.

The lengthscale along the x2 dimension is longer than the lengthscale along the x1 direction (l1_true < l2_true).

There are 1500 data points in this data set. Without using the Kronecker factorization, finding the MAP estimate would be much slower.

Since the two covariances are a product, we only require one scale parameter eta to model the product covariance function.

Next we use the map point mp to extrapolate in a region outside the original grid. We can also interpolate. There is no grid restriction on the new inputs where predictions are desired. It's important to note that under the current implementation, having a grid structure in these points doesn't produce any efficiency gains. The plot with the extrapolations is shown below. The original data is marked with circles as before, but the extrapolated posterior mean is marked with squares.

LatentKron

Like the gp.Latent implementation, the gp.LatentKron implementation specifies a Kronecker structured GP regardless of context. It can be used with any likelihood function, or can be used to model a variance or some other unobserved processes. The syntax follows that of gp.Latent exactly.

Model

To compare with MarginalLikelihood, we use same example as before where the noise is normal, but the GP itself is not marginalized out. Instead, it is sampled directly using NUTS. It is very important to note that gp.LatentKron does not require a Gaussian likelihood like gp.MarginalKron; rather, any likelihood is admissible.

Here though, we'll need to be more informative for our priors, at least those for the GP hyperparameters. This is a general rule when using GPs: use as informative priors as you can, as sampling lenghtscale and amplitude is a challenging business, so you want to make the sampler's work as easy as possible.

Here thankfully, we have a lot of information about our amplitude and lenghtscales -- we're the ones who created them ;) So we could fix them, but we'll show how you could code that prior knowledge in your own models, with, e.g, Truncated Normal distributions:

Posterior convergence

The posterior distribution of the unknown lengthscale parameters, covariance scaling eta, and white noise sigma are shown below. The vertical lines are the true values that were used to generate the original data set:

We can see how challenging sampling can be in these situations. Here, all went well because we were careful with our choice of priors -- especially in this simulated case, where parameters don't have a real interpretation.

What does the trace plot looks like?

All good, so let's go ahead with out-of-sample predictions!

Out-of-sample predictions

Below we show the original data set as colored circles, and the mean of the conditional samples as colored squares. The results closely follow those given by the gp.MarginalKron implementation.

Next we plot the original data set indicated with circles markers, along with four samples from the conditional distribution over fnew indicated with square markers. As we can see, the level of variation in the predictive distribution leads to distinctly different patterns in the values of fnew. However, these samples display the correct correlation structure - we see distinct sinusoidal patterns in the y-axis and proximal correlation structure in the x-axis. The patterns displayed in the observed data seamlessly blend into the conditional distribution.

Authors

Watermark

:::{include} ../page_footer.md :::