Gaussian processes for practical regression
A Gaussian process (GP) turns regression into a probability problem. Rather than returning one predicted value, it treats training observations and future predictions as samples from jointly distributed Gaussian variables. Its output is a posterior predictive distribution containing a mean estimate and predictive uncertainty. This distinction matters when a forecast influences purchasing, staffing or energy use and a precise-looking answer is insufficient.
The model is especially useful for small or medium datasets whose inputs have a meaningful notion of similarity. Kernel functions express assumptions about that similarity without imposing a rigid curve shape. The explanation below covers predictive mathematics, kernel and noise choices, implementation cost, and checks needed before a GP influences an Australian business or public service.
Choosing a regression approach
A point prediction answers what value is expected, but not how much that answer might move. Linear models provide a transparent baseline, tree ensembles handle mixed data well, and neural networks scale when examples and computation are abundant. A GP offers nonlinear regression with uncertainty generated by the model itself, provided its covariance assumptions are defensible.
| Method | Typical output | Uncertainty handling | Practical scale | Main trade-off |
|---|---|---|---|---|
| Linear regression | One fitted value | Analytical intervals under assumptions | Small to large | Interpretable, but restricted relationships |
| Gaussian process regression | Mean and predictive variance | Native to the model; requires validation | Small to medium | Flexible yet computationally demanding |
| Neural-network regression | Usually one point estimate | Extra modelling or calibration needed | Large | High capacity and training demands |
| Random forest | Point estimate and tree spread | Informal rather than probabilistic | Medium | Robust, with weak extrapolation |
Method selection depends on data volume, response smoothness and the cost of mistakes. Exact GP training is rarely appropriate for tens of thousands of observations, yet it can suit hundreds of examples or costly-to-collect measurements. The open community library provides useful background across regression, data structures and programming techniques.
How the posterior prediction works
Let (X) contain training inputs and (y) the observed values. A GP places a joint Gaussian prior over a latent function (f), while a noise term allows (y=f(X)+\varepsilon). Using a zero prior mean and a kernel (k) that measures similarity, the noisy covariance matrix is
[ K=k(X,X)+\sigma_n^2I. ]
Conditioning the prior on (y) produces a Gaussian posterior at new inputs (X_). With (k_=k(X_*,X)), its predictive mean and variance are
[ \mu_=k_^\top K^{-1}y,\qquad \sigma_^2=k(X_,X_)-k_^\top K^{-1}k_*. ]
The expression (\mu_\pm1.96\sigma_) gives an approximate 95% interval for the latent function. A prediction interval for a new noisy observation must also include the observation-noise variance.
The kernel determines which nearby inputs share information. Under an RBF kernel, two inputs receive similar predictions when their scaled Euclidean distance is small. A Matérn kernel permits rougher functions, a linear kernel models additive structure, and a product kernel combines behaviours such as linearity and smoothness. For any chosen kernel, Cholesky factorisation of (K) replaces repeated matrix inverses with stable triangular solves.
Probabilistic modelling also extends beyond regression. The text classification guide shows how a Naive Bayes model forms class posteriors, providing a useful contrast with a GP’s continuous predictive distribution.
Selecting kernels and data
The squared-exponential kernel, often called RBF, starts with the form (k(r)=\sigma_f^2\exp(-r^2/(2\ell^2))). The signal scale (\sigma_f) controls how much the function can vary, while the lengthscale (\ell) controls how rapidly that variation occurs with input distance. Automatic relevance determination gives each feature its own lengthscale, helping the optimiser discover which measurements matter. A Matérn kernel is a sensible alternative when the target changes abruptly or has rougher local structure than an RBF prior can represent.
Kernel choice cannot compensate for poor inputs or an unsuitable observation process. Standardise numerical features so no unit dominates distance calculations, and remove exact duplicate inputs or give repeated measurements a sensible noise term. A white-noise component represents measurement error, while a small numerical jitter stabilises Cholesky decomposition. Optimise the signal scale, lengthscales and noise level by maximising the log marginal likelihood, then compare that fit with held-out data rather than relying on the training score alone.
A practical Australian example is household electricity forecasting in the National Electricity Market across eastern Australia. Inputs might include temperature, time, tariff period and cloud cover. Sydney households with rooftop solar can export power to the grid, while Melbourne’s changeable weather and Canberra’s cold winters alter demand profiles. Weekday school runs, commutes and weekend shopping create recurring patterns that should be represented as calendar and local-time features. A model trained in one city or season needs validation elsewhere before deployment.
Building an efficient Python workflow
Exact GP inference is matrix-bound. Training a model with (n) observations normally requires (O(n^3)) time for a dense Cholesky factorisation and (O(n^2)) memory. Standard prediction costs are also quadratic in the training count. Algorithmic comparisons must match the workload: the guide to fast predecessor searches explains how van Emde Boas trees accelerate ordered-key lookups, but that speedup does not apply to the dense covariance factorisation used by a GP.
Scikit-learn provides a compact implementation. This deliberately small example tunes the kernel through log marginal likelihood, standardises the input columns and normalises the target:
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ConstantKernel, RBF, WhiteKernel
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X = np.array([
[0, 12], [1, 13], [2, 15], [3, 14],
[4, 18], [5, 17], [6, 20], [7, 19]
], dtype=float)
y = np.array([18.0, 19.0, 21.0, 20.5, 24.0, 23.5, 27.0, 26.0])
X_new = np.array([[8, 21], [9, 22]], dtype=float)
kernel = (
ConstantKernel(1.0, (1e-2, 1e2))
* RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
+ WhiteKernel(noise_level=0.05, noise_level_bounds=(1e-4, 1.0))
)
model = make_pipeline(
StandardScaler(),
GaussianProcessRegressor(
kernel=kernel,
normalize_y=True,
alpha=1e-8,
random_state=0
)
)
model.fit(X, y)
mean, std = model.predict(X_new, return_std=True)
print(mean, std)
The returned standard deviation describes the latent function. Add the estimated observation noise when an interval must cover a new measurement. For thousands of observations, consider a sparse variational GP, an inducing-point approximation or Nyström approximation, then retain a strict final test set. Approximate methods save computation but can alter both the mean and the uncertainty, so compare their interval coverage with the exact model on a manageable subset.
Reliable deployment and evaluation
A GP’s uncertainty is conditional on its prior, kernel and training distribution. It may widen sensibly in sparse regions, yet it can become overconfident far outside those regions. Time-aware or grouped validation is essential for forecasts and repeated measurements. Report coverage as well as mean error: a narrow interval that misses 20% of observed values is worse than a wider interval that achieves its advertised 95% coverage.
Data governance belongs beside model evaluation. Under the Privacy Act 1988 and Australian Privacy Principles, organisations covered by the legislation must handle personal information lawfully and transparently, including purpose limitation, security, and access to and correction of personal information. Model outputs can also contain personal information in some settings. Australian Consumer Law requires commercial claims about accuracy, risk or energy savings to be supported and not misleading, so an uncertainty band does not remove the need for sound validation.
Before deployment, apply these practices:
- Define the prediction target, prediction horizon and unit of analysis before selecting the kernel.
- Compare the GP with a simple linear or baseline model using identical splits and metrics.
- Inspect missing values, outliers, duplicated inputs and leakage between training and test records.
- Tune lengthscales and noise with marginal likelihood, then verify the result on untouched data.
- Measure interval coverage and sharpness separately from mean squared error or mean absolute error.
- Document the data window, model configuration, known limitations and the person accountable for review.
A credible release records the kernel, optimised hyperparameters, baseline results, interval coverage and known failure modes. That record allows another engineer to reproduce the work and gives decision-makers a defensible basis for acting on the uncertainty.