(GP-MeansAndCovs)=

Mean and Covariance Functions

:::{post} Mar 22, 2022 :tags: gaussian process :category: intermediate, reference :author: Bill Engels, Oriol Abril Pla :::

A large set of mean and covariance functions are available in PyMC. It is relatively easy to define custom mean and covariance functions. Since PyMC uses PyTensor, their gradients do not need to be defined by the user.

Mean functions

The following mean functions are available in PyMC.

  • {class}pymc.gp.mean.Zero
  • {class}pymc.gp.mean.Constant
  • {class}pymc.gp.mean.Linear

All follow a similar usage pattern. First, the mean function is specified. Then it can be evaluated over some inputs. The first two mean functions are very simple. Regardless of the inputs, gp.mean.Zero returns a vector of zeros with the same length as the number of input values.

Zero

The default mean functions for all GP implementations in PyMC is Zero.

Constant

gp.mean.Constant returns a vector whose value is provided.

As long as the shape matches the input it will receive, gp.mean.Constant can also accept a PyTensor tensor or vector of PyMC random variables.

Linear

gp.mean.Linear is a takes as input a matrix of coefficients and a vector of intercepts (or a slope and scalar intercept in one dimension).

Defining a custom mean function

To define a custom mean function, subclass gp.mean.Mean, and provide __call__ and __init__ methods. For example, the code for the Constant mean function is

python

Remember that PyTensor must be used instead of NumPy.

Covariance functions

PyMC contains a much larger suite of {mod}built-in covariance functions <pymc.gp.cov>. The following shows functions drawn from a GP prior with a given covariance function, and demonstrates how composite covariance functions can be constructed with Python operators in a straightforward manner. Our goal was for our API to follow kernel algebra (see Ch.4 of {cite:t}rasmussen2003gaussian) as closely as possible. See the main documentation page for an overview on their usage in PyMC.

Exponentiated Quadratic

k(x,x)=exp[(xx)222]k(x, x') = \mathrm{exp}\left[ -\frac{(x - x')^2}{2 \ell^2} \right]

Two (and higher) Dimensional Inputs

Both dimensions active

It is easy to define kernels with higher dimensional inputs. Notice that the ls (lengthscale) parameter is an array of length 2. Lists of PyMC random variables can be used for automatic relevance determination (ARD).

One dimension active

Product of covariances over different dimensions

Note that this is equivalent to using a two dimensional ExpQuad with separate lengthscale parameters for each dimension.

White Noise

k(x,x)=σ2Ixxk(x, x') = \sigma^2 \mathrm{I}_{xx}

Constant

k(x,x)=ck(x, x') = c

Rational Quadratic

k(x,x)=(1+(xx)22α2)αk(x, x') = \left(1 + \frac{(x - x')^2}{2\alpha\ell^2} \right)^{-\alpha}

Exponential

k(x,x)=exp[xx22]k(x, x') = \mathrm{exp}\left[ -\frac{||x - x'||}{2\ell^2} \right]

Matern 5/2

k(x,x)=(1+5(xx)2+5(xx)232)exp[5(xx)2]k(x, x') = \left(1 + \frac{\sqrt{5(x - x')^2}}{\ell} + \frac{5(x-x')^2}{3\ell^2}\right) \mathrm{exp}\left[ - \frac{\sqrt{5(x - x')^2}}{\ell} \right]

Matern 3/2

k(x,x)=(1+3(xx)2)exp[3(xx)2]k(x, x') = \left(1 + \frac{\sqrt{3(x - x')^2}}{\ell}\right) \mathrm{exp}\left[ - \frac{\sqrt{3(x - x')^2}}{\ell} \right]

Matern 1/2

k(x,x)=exp[(xx)2]k(x, x') = \mathrm{exp}\left[ -\frac{(x - x')^2}{\ell} \right]

Cosine

k(x,x)=cos(2πxx2)k(x, x') = \mathrm{cos}\left( 2 \pi \frac{||x - x'||}{ \ell^2} \right)

Linear

k(x,x)=(xc)(xc)k(x, x') = (x - c)(x' - c)

Polynomial

k(x,x)=[(xc)(xc)+offset]dk(x, x') = [(x - c)(x' - c) + \mathrm{offset}]^{d}

Multiplication with a precomputed covariance matrix

A covariance function cov can be multiplied with numpy matrix, K_cos, as long as the shapes are appropriate.

Applying an arbitrary warping function on the inputs

If $k(x, x')$ is a valid covariance function, then so is $k(w(x), w(x'))$.

The first argument of the warping function must be the input X. The remaining arguments can be anything else, including random variables.

Constructing Periodic using WarpedInput

The WarpedInput kernel can be used to create the Periodic covariance. This covariance models functions that are periodic, but are not an exact sine wave (like the Cosine kernel is).

The periodic kernel is given by

k(x,x)=exp(2sin2(πxx1T)2)k(x, x') = \exp\left( -\frac{2 \sin^{2}(\pi |x - x'|\frac{1}{T})}{\ell^2} \right)

Where T is the period, and $\ell$ is the lengthscale. It can be derived by warping the input of an ExpQuad kernel with the function $\mathbf{u}(x) = (\sin(2\pi x \frac{1}{T}),, \cos(2 \pi x \frac{1}{T}))$. Here we use the WarpedInput kernel to construct it.

The input X, which is defined at the top of this page, is 2 "seconds" long. We use a period of $0.5$, which means that functions drawn from this GP prior will repeat 4 times over 2 seconds.

Periodic

There is no need to construct the periodic covariance this way every time. A more efficient implementation of this covariance function is built in.

Circular

Circular kernel is similar to Periodic one but has an additional nuisance parameter $\tau$

In {cite:t}padonou2015polar, the Weinland function is used to solve the problem and ensures positive definite kernel on the circular domain (and not only).

Wc(t)=(1+τtc)(1tc)+τW_c(t) = \left(1 + \tau \frac{t}{c}\right)\left(1-\frac{t}{c}\right)_+^\tau

where $c$ is maximum value for $t$ and $\tau\ge 4$ is some positive number

The kernel itself for geodesic distance (arc length) on a circle looks like

kg(x,y)=Wπ(distgeo(x,y))k_g(x, y) = W_\pi(\text{dist}_{\mathit{geo}}(x, y))

Briefly, you can think

  • $t$ is time, it runs from $0$ to $24$ and then goes back to $0$
  • $c$ is maximum distance between any timestamps, here it would be $12$
  • $\tau$ controls for correlation strength, larger $\tau$ leads to less smooth functions

We can see the effect of $\tau$, it adds more non-smooth patterns

Gibbs

The Gibbs covariance function applies a positive definite warping function to the lengthscale. Similarly to WarpedInput, the lengthscale warping function can be specified with parameters that are either fixed or random variables.

Scaled Covariance

One can construct a new kernel or covariance function by multiplying some base kernel by a nonnegative function $\phi(x)$,

kscaled(x,x)=ϕ(x)kbase(x,x)ϕ(x).k_{\mathrm{scaled}}(x, x') = \phi(x) k_{\mathrm{base}}(x, x') \phi(x') \,.

This is useful for specifying covariance functions whose amplitude changes across the domain.

Constructing a Changepoint kernel using ScaledCov

The ScaledCov kernel can be used to create the Changepoint covariance. This covariance models a process that gradually transitions from one type of behavior to another.

The changepoint kernel is given by

k(x,x)=ϕ(x)k1(x,x)ϕ(x)+(1ϕ(x))k2(x,x)(1ϕ(x))k(x, x') = \phi(x)k_{1}(x, x')\phi(x) + (1 - \phi(x))k_{2}(x, x')(1 - \phi(x'))

where $\phi(x)$ is the logistic function.

Combination of two or more Covariance functions

You can combine different covariance functions to model complex data.

In particular, you can perform the following operations on any covaraince functions:

  • Add other covariance function with equal or broadcastable dimensions with first covariance function
  • Multiply with a scalar or a covariance function with equal or broadcastable dimensions with first covariance function
  • Exponentiate with a scalar.

Addition

Multiplication

Exponentiation

Defining a custom covariance function

Covariance function objects in PyMC need to implement the __init__, diag, and full methods, and subclass gp.cov.Covariance. diag returns only the diagonal of the covariance matrix, and full returns the full covariance matrix. The full method has two inputs X and Xs. full(X) returns the square covariance matrix, and full(X, Xs) returns the cross-covariances between the two sets of inputs.

For example, here is the implementation of the WhiteNoise covariance function:

python

If we have forgotten an important covariance or mean function, please feel free to submit a pull request!

References

:::{bibliography} :filter: docname in docnames :::

Authors

Watermark

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

Empty cell