MaternKernel<T>

Models & Types · AiDotNet.Kernels

Implements the Matérn family of kernels with configurable smoothness parameter.

Implements: IKernelFunction<T>

For Beginners

The Matérn kernel is one of the most important kernels in GP practice. It provides a middle ground between the very smooth RBF kernel and rougher kernels.

The smoothness parameter ν controls how “wiggly” the functions can be:

  • ν = 1/2: Exponential kernel (functions are continuous but not differentiable)
  • ν = 3/2: Once differentiable (good for many real-world applications)
  • ν = 5/2: Twice differentiable (often used as default)
  • ν → ∞: RBF/Gaussian kernel (infinitely differentiable)

In mathematical terms: k(r) = (2^(1-ν)/Γ(ν)) × (√(2ν)×r/l)^ν × K_ν(√(2ν)×r/l)

Where:

  • r = |x - x’| is the distance
  • l is the length scale
  • ν is the smoothness parameter
  • K_ν is the modified Bessel function

How It Works

Why use Matérn over RBF?

  1. More realistic: Real-world functions are rarely infinitely smooth
  2. Better extrapolation: RBF can be overly smooth for extrapolation
  3. Physical motivation: Many physical processes have finite smoothness
  4. Computational: ν = 1/2, 3/2, 5/2 have simple closed forms

Rule of thumb:

  • If you’re unsure, start with ν = 5/2 (Matérn 5/2)
  • For rough data, try ν = 3/2
  • For very noisy data, ν = 1/2 might help

Example

using AiDotNet.Kernels;
using AiDotNet.Tensors.LinearAlgebra;

var kernel = new MaternKernel<double>();
var a = new Vector<double>(new[] { 1.0, 2.0, 3.0 });
var b = new Vector<double>(new[] { 1.5, 1.0, 2.5 });
double similarity = kernel.Calculate(a, b);
Console.WriteLine($"MaternKernel: K(a, b) = {similarity:F4}");

Constructors

Constructor Summary
MaternKernel(double nu, double lengthScale, double variance) Initializes a new Matérn kernel.

Properties

Property Summary
double LengthScale { get; } Gets the length scale.
double Nu { get; } Gets the smoothness parameter (ν).
double Variance { get; } Gets the signal variance.

Methods

Method Summary
MaternKernel<T> Matern12(double lengthScale, double variance) Creates a Matérn 1/2 (exponential) kernel.
MaternKernel<T> Matern32(double lengthScale, double variance) Creates a Matérn 3/2 kernel.
MaternKernel<T> Matern52(double lengthScale, double variance) Creates a Matérn 5/2 kernel.
T Calculate(Vector<T> x1, Vector<T> x2) Calculates the Matérn kernel value between two vectors.