RationalQuadraticKernel<T>

Models & Types · AiDotNet.Kernels

Implements the Rational Quadratic kernel, equivalent to an infinite mixture of RBF kernels.

Implements: IKernelFunction<T>

For Beginners

The Rational Quadratic (RQ) kernel is a powerful kernel that can be thought of as an infinite mixture of RBF kernels with different length scales.

In mathematical terms: k(x, x’) = σ² × (1 + r²/(2αl²))^(-α)

Where:

  • r = |x - x’| is the Euclidean distance
  • l is the length scale
  • α is the “scale mixture” parameter
  • σ² is the variance

The α parameter controls how the kernel behaves:

  • α → ∞: Approaches the RBF kernel (single length scale)
  • Small α: More heavy-tailed (multiple length scales contribute)

How It Works

Why use Rational Quadratic?

  1. Multi-scale: Naturally captures patterns at different scales
  2. Robustness: Less sensitive to length scale choice than RBF
  3. Heavy tails: Points far apart still have some correlation
  4. Flexibility: Interpolates between RBF and more flexible behaviors

When to use:

  • Data has patterns at multiple scales
  • You’re not sure what length scale to use
  • RBF seems too restrictive

Example

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

var kernel = new RationalQuadraticKernel<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($"RationalQuadraticKernel: K(a, b) = {similarity:F4}");

Constructors

Constructor Summary
RationalQuadraticKernel(double lengthScale, double alpha, double variance) Initializes a new Rational Quadratic kernel.

Properties

Property Summary
double Alpha { get; } Gets the scale mixture parameter (α).
double LengthScale { get; } Gets the length scale.
double Variance { get; } Gets the signal variance.

Methods

Method Summary
Dictionary<string, T> CalculateHyperparameterGradients(Vector<T> x1, Vector<T> x2) Computes the gradient with respect to hyperparameters.
T Calculate(Vector<T> x1, Vector<T> x2) Calculates the Rational Quadratic kernel value between two vectors.
Vector<T> CalculateGradient(Vector<T> x1, Vector<T> x2) Computes the gradient of the kernel with respect to the input.
bool IsEffectivelyRBF() Determines if this kernel approximates an RBF kernel.
double EffectiveLengthScales() Returns the “effective number of length scales” in the mixture.