Quick Start
Your first GP regression in six lines:
import numpy as np
import lightgp as gp
X = np.sort(np.random.uniform(-3, 3, 50)).reshape(-1, 1).astype(np.float32)
y = (np.sin(X[:, 0]) + 0.1 * np.random.randn(50)).astype(np.float32)
model = gp.GPExact(gp.RBF(), noise_var=0.01)
model.fit(X, y)
model.optimize(steps=100)
X_test = np.linspace(-4, 4, 200).reshape(-1, 1).astype(np.float32)
pred = model.predict(X_test)
print(pred["mean"].shape, pred["var"].shape) # (200,) (200,)
The predict method returns a dictionary with two keys:
"mean"— posterior mean predictions, shape(N_test,)"var"— posterior marginal variance, shape(N_test,)
Plotting the result
import matplotlib.pyplot as plt
mu = pred["mean"]
std = np.sqrt(pred["var"])
plt.fill_between(X_test[:, 0], mu - 2 * std, mu + 2 * std, alpha=0.2)
plt.plot(X_test[:, 0], mu, lw=2, label="GP mean")
plt.scatter(X[:, 0], y, c="black", s=20, zorder=5, label="data")
plt.legend()
plt.show()
What’s happening under the hood
gp.RBF()constructs an RBF (squared-exponential) kernel with default length scaleℓ = 1and signal varianceσ² = 1.model.fit(X, y)Cholesky-factorizesK + σ_n² Iand computesα = K_y⁻¹ y.model.optimize(100)runs 100 steps of Adam over the log-hyperparameters[log ℓ, log σ², log σ_n²]to maximize the log marginal likelihood.model.predict(X_test)returnsμ* = K_*ᵀ αfor the mean andσ*² = k(x*,x*) − K_*ᵀ K_y⁻¹ K_*for the marginal variance.
LightGP automatically picks the fastest backend for the problem shape:
Backend |
Library |
When |
|---|---|---|
|
Accelerate (macOS) / OpenBLAS (Linux) |
Dense Cholesky at any size |
|
Apple Metal compute |
|
|
cuBLAS / cuSOLVER / cuFFT |
Always preferred when compiled in |
You can force a backend explicitly with the backend= constructor argument
— see Backends.