Skip to Content
OchreML
Kernel, MLP & Gaussian Process

GaussianProcessRegressor

Bayesian Gaussian Process regression with Cholesky factorization, predictive uncertainty, and Nelder-Mead LML optimization.

Overview

GaussianProcessRegressor implements Bayesian Gaussian Process (GP) regression backed by Rust Cholesky factorization and optional Nelder-Mead optimization of the log marginal likelihood (LML). At inference time, it returns the predictive mean and can optionally return either the per-point predictive standard deviation (return_std=True) or the full joint predictive covariance matrix (return_cov=True). (^▽^)

Backend Support

  • Primitives: N3 (pairwise_l2_sq for RBF, Matern, and Rational Quadratic kernels), N2 (elementwise_op), E1 (matvec / matmul)
  • Devices: cpu (Rayon parallel f64), cuda / cuda:0 (dynamic cuBLAS), wgpu (Vulkan/Metal/DX12), tpu (OpenXLA PJRT), and auto
  • Persistence: .dit binary format via save() / load() (and ochreml.save() / ochreml.load()), plus Python pickle support

Import & Signature

from ochreml.gaussian_process import GaussianProcessRegressor
# Also available from top-level: from ochreml import GaussianProcessRegressor

GaussianProcessRegressor(
    kernel: str | dict | Any | None = "rbf",
    length_scale: float = 1.0,
    constant_value: float = 1.0,
    nu: float = 1.5,
    alpha: float = 1e-10,
    noise_level: float = 1.0,
    rq_alpha: float = 1.0,
    optimize_hyperparams: bool = False,
    n_restarts_optimizer: int = 0,
    normalize_y: bool = False,
    random_state: int | None = None,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
kernelstr, dict, or None"rbf"Kernel specification: "rbf", "matern", "white", "rational_quadratic", or "rbf_white". Also accepts a dict or kernel-like object with matching attributes.
length_scalefloat1.0Length scale of the covariance kernel (length_scale > 0).
constant_valuefloat1.0Constant variance scaling factor of the kernel (constant_value > 0).
nufloat1.5Smoothness parameter for the Matern kernel (0.5, 1.5, or 2.5).
alphafloat1e-10Non-negative jitter added to the diagonal of the kernel matrix during fitting (alpha >= 0) for numerical stability or observation noise variance.
noise_levelfloat1.0Noise variance for "white" and "rbf_white" kernels (noise_level > 0).
rq_alphafloat1.0Scale mixture parameter for "rational_quadratic" kernel (rq_alpha > 0).
optimize_hyperparamsboolFalseWhether to optimize kernel hyperparameters via Nelder-Mead maximization of the log marginal likelihood.
n_restarts_optimizerint0Number of random restarts for the hyperparameter optimizer.
normalize_yboolFalseWhether to normalize target values y by removing the mean and scaling to unit standard deviation.
random_stateint | NoneNoneRandom seed used for optimizer restarts.
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
X_train_ndarray of shape (n_samples, n_features)Training feature matrix stored for posterior prediction.
L_ndarray of shape (n_samples, n_samples)Lower-triangular Cholesky factor of K(X_train, X_train) + alpha * I.
alpha_ndarray of shape (n_samples,)Dual coefficients solving L * L^T * alpha_ = y.
length_scale_floatFitted kernel length scale (updated when optimize_hyperparams=True).
constant_value_floatFitted constant variance scale.
noise_level_floatFitted noise level for white-noise kernels.
rq_alpha_floatFitted rational quadratic scale mixture parameter.
y_train_mean_floatMean of training targets (non-zero when normalize_y=True).
y_train_std_floatStandard deviation of training targets (used when normalize_y=True).
log_marginal_likelihood_value_float | NoneLog marginal likelihood of the fitted hyperparameters.
n_features_in_int | NoneNumber of features seen during fit().
device_str | NoneResolved execution device string.

Methods

MethodSignatureDescription
fitfit(X, y) -> GaussianProcessRegressorFit the Gaussian Process regression model on (X, y).
predictpredict(X, return_std=False, return_cov=False)Predict posterior mean y_mean, or (y_mean, y_std) / (y_mean, y_cov).
log_marginal_likelihoodlog_marginal_likelihood() -> floatReturn the log marginal likelihood of the fitted model.
scorescore(X, y) -> floatReturn the coefficient of determination (R^2) on (X, y).
toto(device) -> GaussianProcessRegressorMove the estimator's compute target to another device.
savesave(path) -> NoneSave the fitted model to a .dit binary file.
loadGaussianProcessRegressor.load(path, device=None) -> GaussianProcessRegressorClass method to load a fitted model from a .dit file.

Example

import numpy as np
from ochreml.gaussian_process import GaussianProcessRegressor

X_train = np.linspace(0.0, 5.0, 15).reshape(-1, 1)
y_train = np.sin(X_train[:, 0])

gpr = GaussianProcessRegressor(
    kernel="rbf",
    length_scale=1.0,
    alpha=1e-6,
    optimize_hyperparams=True,
    n_restarts_optimizer=2,
    random_state=42,
)
gpr.fit(X_train, y_train)

X_query = np.array([[0.5], [2.5], [4.5]])
y_mean, y_std = gpr.predict(X_query, return_std=True)
print("Predictive mean:", y_mean)
print("Predictive std:", y_std)
print("LML:", gpr.log_marginal_likelihood())
print("Optimized length_scale_:", gpr.length_scale_)

On this page