Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

GP with Uncertain Inputs (PILCO-style)

When a Gaussian process (GP) is queried at an uncertain input x∗∼N(μ∗,Σ∗)x_* \sim \mathcal{N}(\mu_*, \Sigma_*), the predictive distribution p(f(x∗))p(f(x_*)) is no longer Gaussian -- it is a mixture of Gaussians weighted by the input density. Approximating this distribution is central to several important models:

  • Multi-step dynamics prediction (Girard et al., 2003): the GP predicts the next state, and that uncertain prediction becomes the input for the following step. Uncertainty compounds.
  • PILCO (Deisenroth & Rasmussen, 2011): a GP dynamics model is rolled forward under a policy, and the expected cost is differentiated through the uncertain predictions to learn the policy.
  • Bayesian GPLVM (Titsias & Lawrence, 2010): latent inputs are uncertain, so the likelihood requires integrating the GP over the variational posterior of the inputs.
  • Heteroscedastic / input-noise GPs (McHutchon & Rasmussen, 2011): noisy inputs induce effective output noise that depends on the local gradient of the GP mean.

Predictive moments

Given the GP posterior f∣Df \mid \mathcal{D} with weights α=(K+σ2I)−1y\alpha = (K + \sigma^2 I)^{-1} y and an uncertain input x∗∼N(μ∗,Σ∗)x_* \sim \mathcal{N}(\mu_*, \Sigma_*), the predictive moments are

E[f(x∗)]=∫k(x∗)⊤α  N(x∗∣μ∗,Σ∗) dx∗,\mathbb{E}[f(x_*)] = \int \mathbf{k}(x_*)^\top \alpha \;\mathcal{N}(x_* \mid \mu_*, \Sigma_*)\,dx_*,
Var[f(x∗)]=∫(f(x∗)−E[f(x∗)])2  N(x∗∣μ∗,Σ∗) dx∗.\mathrm{Var}[f(x_*)] = \int \bigl(f(x_*) - \mathbb{E}[f(x_*)]\bigr)^2 \;\mathcal{N}(x_* \mid \mu_*, \Sigma_*)\,dx_*.

For the squared-exponential kernel, these integrals have closed-form solutions. For general kernels, we need numerical integration -- and that is where gaussx’s uncertainty propagation machinery comes in.

What this notebook covers

  1. Training a 1D GP on sin⁡(x)\sin(x) data
  2. Single uncertain prediction with Taylor, Unscented, and MC methods
  3. Multi-step dynamics rollout (PILCO-style) with growing uncertainty
  4. Expected cost computation under input uncertainty
  5. Expectation utilities: mean, gradient, and log-likelihood expectations
  6. AssumedDensityFilter diagnostics: skewness, kurtosis, Gaussianity

1. Setup: 1D GP regression on sin(x)

We train a standard GP with an RBF kernel on noisy observations of sin⁡(x)\sin(x), then extract the weight vector α=K−1y\alpha = K^{-1} y and the inverse kernel operator K−1K^{-1}.

RBF kernel

Quick sanity check: plot the GP posterior at deterministic test points.

<Figure size 1000x400 with 1 Axes>

2. Single uncertain prediction

Now suppose our test input is uncertain: x∗∼N(1.5,  0.3)x_* \sim \mathcal{N}(1.5, \; 0.3). We compare three approaches to computing the predictive moments E[f(x∗)]\mathbb{E}[f(x_*)] and Var[f(x∗)]\mathrm{Var}[f(x_*)].

2a. Taylor integrator (linearisation)

Taylor:     mean = 1.1024,  var = 0.0089

2b. Unscented integrator (sigma points)

Unscented:  mean = 0.9101,  var = 0.0000

2c. Monte Carlo

MC (1000):  mean = 0.9188,  var = 0.0724

2d. Comparison

All three methods should broadly agree, with Taylor slightly biased in regions of high curvature and MC showing some sampling noise.

Method             Mean      Var
--------------------------------
Taylor           1.1024   0.0089
Unscented        0.9101   0.0000
MC (1000)        0.9188   0.0724

3. Multi-step dynamics prediction (PILCO-style)

This is the core idea behind PILCO: chain uncertain GP predictions to roll out a trajectory. At each step, the output distribution becomes the input for the next step, and uncertainty grows.

We use a simple 1D dynamical system: xt+1=sin⁡(xt)+0.1 xt+εx_{t+1} = \sin(x_t) + 0.1\,x_t + \varepsilon, with ε∼N(0,0.01)\varepsilon \sim \mathcal{N}(0, 0.01).

Generate dynamics data

Roll out with uncertainty propagation

Starting from x0∼N(0.5,0.01)x_0 \sim \mathcal{N}(0.5, 0.01), we chain uncertain GP predictions for H=15H = 15 steps. At each step we use AssumedDensityFilter for KL-optimal Gaussian moment matching.

Ground truth: simulate many particles

Plot: predicted trajectory vs ground truth

<Figure size 1000x400 with 1 Axes>

The GP rollout captures the trajectory shape while the uncertainty fans out over the horizon -- exactly the behaviour exploited by PILCO for policy gradient computation.

4. Expected cost under input uncertainty

In PILCO, the policy is optimised by differentiating the expected cumulative cost through the uncertain rollout. Here we compute the expected quadratic cost at a single step.

Expected quadratic cost: 0.7958

This expected cost is differentiable with respect to the policy parameters (not shown here), which is the basis of PILCO’s analytic policy gradient.

5. Expectation utilities

gaussx provides several utilities for computing expectations under Gaussian distributions. These are the building blocks for the uncertain GP machinery above.

5a. Mean expectation: E[f(x)]\mathbb{E}[f(x)]

Compute the expected value of a function under a Gaussian input.

E[f(x)] with Unscented: [0.5]
Analytic E[x^3 - 2x]:  0.5000

5b. Gradient expectation: E[∇f(x)]\mathbb{E}[\nabla f(x)] via Stein’s lemma

Stein’s lemma states that for x∼N(μ,Σ)x \sim \mathcal{N}(\mu, \Sigma):

E[∇f(x)]=Σ−1 Cov[x,f(x)]\mathbb{E}[\nabla f(x)] = \Sigma^{-1}\,\mathrm{Cov}[x, f(x)]

This is useful for computing expected gradients without differentiating through the expectation integral.

E[nabla sin(x)] at mu=1, sigma^2=0.5: [0.54030226]
Analytic E[cos(x)] (moment generating fn):  0.4208

5c. Log-likelihood expectation: E[log⁡p(y∣f(x))]\mathbb{E}[\log p(y \mid f(x))]

For variational inference and EM with uncertain inputs, we need the expected log-likelihood under the input distribution.

E[log p(y|f(x))]: -0.9374

6. AssumedDensityFilter with diagnostics

The AssumedDensityFilter projects the true (non-Gaussian) output distribution onto the closest Gaussian in KL divergence. This is the workhorse behind multi-step rollouts. The integrate_with_diagnostics method additionally returns skewness and kurtosis of the output samples, which helps assess whether the Gaussian approximation is reasonable.

6a. Well-behaved case: near-linear function

Mild nonlinearity:
  Predicted mean: [-0.00669525]
  Skewness:       [0.03660105]
  Kurtosis:       [2.54982357]
  (Gaussian: skewness ~ 0, kurtosis ~ 3)

6b. Challenging case: strong nonlinearity

Strong nonlinearity:
  Predicted mean: [0.0548201]
  Skewness:       [-0.05444696]
  Kurtosis:       [2.36180005]
  (Deviations from 0 / 3 indicate non-Gaussianity)

When skewness is far from zero or kurtosis far from 3, the Gaussian projection loses information. In such cases, consider:

  • Using more sigma points / samples
  • Reducing the prediction horizon
  • Switching to a particle-based method

Summary

ToolPurpose
uncertain_gp_predictUncertain-input GP via quadrature
uncertain_gp_predict_mcGP prediction with uncertain inputs via Monte Carlo
AssumedDensityFilterKL-optimal Gaussian projection with diagnostics
mean_expectationE[f(x)]\mathbb{E}[f(x)] under Gaussian input
gradient_expectationE[∇f(x)]\mathbb{E}[\nabla f(x)] via Stein’s lemma
cost_expectationExpected cost under input uncertainty
log_likelihood_expectationE[log⁡p(y∣f(x))]\mathbb{E}[\log p(y \mid f(x))]

These building blocks compose naturally:

  1. Train a GP dynamics model on observed transitions
  2. Roll out uncertain predictions with uncertain_gp_predict
  3. Evaluate expected cost with cost_expectation
  4. Differentiate through the whole pipeline with jax.grad

This is the computational recipe behind PILCO and related model-based reinforcement learning algorithms.