Skip to content
SK Subham KumarTSF Hub · time-series forecasting
Machine learning2006BayesianKernelUncertainty

Gaussian process

Gaussian process regression

A Bayesian, non-parametric model: describe what the series looks like (smooth trend, yearly cycle, noise) with a kernel, and get forecasts with honest uncertainty bands.

Overview

A Gaussian process puts a probability distribution over functions. Instead of choosing a model form, you choose a kernel that says how similar two time points are: an RBF kernel for smooth variation, a periodic kernel for seasonality, a white-noise kernel for measurement error. Kernels add and multiply, so a “slowly changing yearly cycle on a smooth trend” is a single expression.

Conditioning on the data gives a predictive mean and variance at any future time. Bands widen naturally as you forecast further from the data, which is exactly the behaviour you want.

How it works

With training inputs \(X\), targets \(y\), kernel matrix \(K=k(X,X)\), noise \(\sigma_n^2\) and test point \(x_*\):

\[\mu_*=k_*^\top\,(K+\sigma_n^2 I)^{-1}y,\qquad \sigma_*^2=k(x_*,x_*)-k_*^\top(K+\sigma_n^2I)^{-1}k_*\]

Common kernels: \(k_{\text{RBF}}=\exp\!\big(-\tfrac{(t-t')^2}{2\ell^2}\big)\), \(k_{\text{per}}=\exp\!\big(-\tfrac{2\sin^2(\pi|t-t'|/p)}{\ell^2}\big)\). Hyperparameters are learned by maximising the marginal likelihood.

When to use it

Use it for

  • Short, smooth series (sensors, lab measurements, epidemiology)
  • When decisions depend on uncertainty
  • Irregularly sampled data: time is just an input

Avoid it for

  • Long series: exact GPs scale as \(O(n^3)\) (use sparse or inducing-point GPs)
  • Abrupt regime changes the kernel can’t express

Key settings

SettingWhat it controlsTypical value
kernelStructure: trend + seasonality + noiseRBF + ExpSineSquared·RBF + White
length_scaleHow quickly correlation decayslearned
periodicitySeasonal period (fix it if known)12 monthly
normalize_yStandardise targetsTrue

Python example

✓ Tested: runs end to endDownload gaussian_process.py
import numpy as np
import pandas as pd
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ExpSineSquared, WhiteKernel, ConstantKernel as C

URL = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
y = pd.read_csv(URL, index_col="Month", parse_dates=True)["Passengers"]
t = np.arange(len(y), dtype=float).reshape(-1, 1)          # time index in months
z = np.log(y.values)
t_tr, z_tr, t_te = t[:-12], z[:-12], t[-12:]

kernel = (C(1.0) * RBF(length_scale=60.0)                                    # long-term trend
          + C(0.1) * ExpSineSquared(length_scale=1.0, periodicity=12.0,
                                    periodicity_bounds="fixed") * RBF(100.0)  # slowly changing yearly cycle
          + WhiteKernel(noise_level=1e-3))                                    # observation noise
gp = GaussianProcessRegressor(kernel=kernel, normalize_y=True, n_restarts_optimizer=3, random_state=0)
gp.fit(t_tr, z_tr)
print("learned kernel:", gp.kernel_)

mean, std = gp.predict(t_te, return_std=True)
forecast, lo, hi = np.exp(mean), np.exp(mean - 1.28 * std), np.exp(mean + 1.28 * std)   # 80% band
print(f"MAE: {np.mean(np.abs(y.values[-12:] - forecast)):.1f}")
print(f"80% band covers {np.mean((y.values[-12:] >= lo) & (y.values[-12:] <= hi)):.0%} of the test months")

Tips & pitfalls

  • Fix the periodicity when you know it; learning it invites local optima.
  • Work in log space for multiplicative growth, then exponentiate mean and bands.
  • Check calibration: about 80% of test points should fall inside an 80% band.

References

  1. Rasmussen, C. E., & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.
  2. Roberts, S., Osborne, M., Ebden, M., Reece, S., Gibson, N., & Aigrain, S. (2013). Gaussian processes for time-series modelling. Philosophical Transactions of the Royal Society A, 371(1984), 20110550.