Plotting Matérn covariances

These examples plot Matérn covariance kernels as functions of scaled distance. The first script uses half-integer regularities, and the second script uses the nu-parameterized Matérn kernel for a range of nu values.

What this example does

gpmp_example01_materncov.py builds a one-dimensional grid of scaled distances h and evaluates gp.kernel.maternp_kernel(p, abs(h)) for several integer values of p. For half-integer Matérn kernels, nu = p + 1/2.

gpmp_example30_materncov.py uses gp.kernel.matern_kernel(nu, abs(h)) for non-integer and integer nu values. This displays the same Matérn family without restricting nu to half-integers.

Increasing nu produces smoother sample paths and a covariance function that is flatter near the origin.

Mathematical description

The function maternp_kernel returns the correlation part of a stationary Matérn covariance for \(\nu = p + 1/2\). GPmp evaluates

\[c_p(h) = \exp(-2\sqrt{\nu}\,h) \frac{\Gamma(p+1)}{\Gamma(2p+1)} \sum_{j=0}^{p} \frac{(p+j)!}{j!(p-j)!} \left(4\sqrt{\nu}\,h\right)^{p-j}.\]

The nu-parameterized kernel matern_kernel evaluates

\[r_\nu(h) = \frac{2^{1-\nu}}{\Gamma(\nu)} (2\sqrt{\nu}h)^\nu K_\nu(2\sqrt{\nu}h), \qquad r_\nu(0)=1,\]

where \(K_\nu\) is the modified Bessel function of the second kind.

The full anisotropic covariance used in later examples has the form

\[k_\theta(x, x') = \sigma^2 c_p\left( \left\| \left( (x_0-x'_0)/\rho_0,\ldots, (x_{d-1}-x'_{d-1})/\rho_{d-1} \right) \right\|_2 \right).\]

Outputs

All curves are normalized to one at h = 0. The exponential covariance is obtained at nu = 1/2 and decays sharply away from the origin. Larger nu values produce stronger local smoothness and a slower initial decay. No observations or parameter selection are involved.

Functions used

  • gp.kernel.maternp_kernel evaluates the correlation kernel as a function of scaled distance for half-integer regularity.

  • gp.kernel.matern_kernel evaluates the correlation kernel for positive regularity nu.

  • gp.plot.Figure is a small Matplotlib wrapper used throughout the examples.

  • For full covariance matrices with variance and lengthscales, use gp.kernel.maternp_covariance or gp.kernel.matern_covariance.

Half-integer regularities

../_images/materncov_1_0.png

General Matérn regularity

../_images/materncov_2_0.png

Script: examples/gpmp_example30_materncov.py

 1"""Plot Matérn covariance functions for several regularity values.
 2
 3This example uses the ``nu``-parameterized Matérn kernel
 4``gp.kernel.matern_kernel``. It complements ``gpmp_example01_materncov.py``,
 5which uses the half-integer Matérn kernel ``gp.kernel.maternp_kernel``.
 6
 7Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
 8Copyright (c) 2022-2026, CentraleSupelec
 9License: GPLv3 (see LICENSE)
10"""
11
12import gpmp as gp
13import gpmp.num as gnp
14import matplotlib.pyplot as plt
15from matplotlib.colors import LinearSegmentedColormap
16
17
18def main():
19    h = gnp.linspace(-2.0, 2.0, 2000)
20    h_abs = gnp.abs(h)
21    nu_values = [0.25, 0.5, 1.0, 1.5, 2.5, 5.0, 20.0]
22
23    fig = gp.plot.Figure(figsize=(7.0, 4.5))
24    cmap = LinearSegmentedColormap.from_list(
25        "matern_blue_teal",
26        ["#1f2a5c", "#255c99", "#1f8a9b", "#53b58f"],
27    )
28    denom = max(len(nu_values) - 1, 1)
29
30    for i, nu in enumerate(nu_values):
31        r = gp.kernel.matern_kernel(nu, h_abs)
32        color = cmap(i / denom)
33        fig.plot(h, r, color=color, linewidth=2.2, label=rf"$\nu={nu:g}$")
34
35    fig.title("Matérn covariances for several regularity values")
36    fig.xlabel("h")
37    fig.ylabel(r"$r_\nu(|h|)$")
38    fig.legend(title="Regularity")
39    fig.show(grid=True)
40
41
42if __name__ == "__main__":
43    main()