Conditional sample paths

This example generates Gaussian process sample paths conditional on noise-free observations. Conditional paths support simulation studies, uncertainty visualization, and algorithms that require full random functions instead of only posterior means and variances.

What this example does

The script builds a one-dimensional GP, conditions it on observations, and draws sample paths from the posterior process. The conditional paths are generated so that they are consistent with the observations and with the selected covariance model.

Mathematical object

The script first draws prior paths from \(Z \sim \mathcal{GP}(m, k_\theta)\). It then transforms those paths into conditional paths. In the noise-free case, the target distribution is

\[Z_t\mid Z_i=z_i \sim \mathcal{N}\left( m_t + k_{ti} k_{ii}^{-1}(z_i-m_i), k_{tt} - k_{ti} k_{ii}^{-1} k_{it} \right).\]

The conditioning step changes each prior path so that the values at the observation points match the realized data \(z_i\).

Outputs

The sample paths pass through the noise-free observations. Away from the observations, paths spread according to posterior uncertainty. The posterior mean and uncertainty envelope summarize the conditional distribution, while the sample paths show possible function realizations.

API points

  • Conditional sample paths use the same covariance model as prediction.

  • Use sample paths when an algorithm needs random functions, beyond pointwise marginal uncertainty.

  • The noise-free setting forces conditional paths to match observations exactly.

../_images/sample_paths_0_0.png

Script: examples/gpmp_example10_sample_paths.py

 1"""GP Conditional Sample Paths
 2
 3This script constructs a GP model using a Matérn kernel and a constant
 4mean function. The script generates sample paths from the GP prior,
 5and then generates conditional sample paths given the observed data.
 6
 7Copyright (c) 2022-2023, CentraleSupelec
 8Author: Emmanuel Vazquez <emmanuel.vazquez@centralesupelec.fr>
 9License: GPLv3 (see LICENSE)
10
11"""
12
13import math
14import numpy as np
15import gpmp.num as gnp
16import gpmp as gp
17import gpmp.plot as gpplot
18
19
20def generate_data():
21    nt = 200
22    xt = np.linspace(-1, 1, nt).reshape(-1, 1)
23    zt = gp.misc.testfunctions.twobumps(xt)
24
25    ind = [10, 45, 100, 130, 155]
26    xi = xt[ind]
27    zi = zt[ind]
28
29    return xt, zt, xi, zi, ind
30
31
32def kernel(x, y, covparam, pairwise=False):
33    p = 2
34    return gp.kernel.maternp_covariance(x, y, p, covparam, pairwise)
35
36
37def constant_mean(x, param):
38    return gnp.ones((x.shape[0], 1))
39
40
41def visualization(xt, zt, zpsim, xi, zi, zpm, zpv):
42    fig = gpplot.Figure(isinteractive=True)
43    fig.plot(xt, zt, 'C2', linewidth=1, label='truth')
44    fig.plot(xt, zpsim[:, 1], 'C0', linewidth=1, label='posterior sample paths')
45    fig.plot(xt, zpsim[:, 1:], 'C0', linewidth=1)
46    fig.plot(xi, zi, 'rs')
47    fig.plotgp(xt, zpm, zpv)
48    fig.title('Conditional sample paths')
49    fig.legend()
50    fig.show()
51
52
53def main():
54    xt, zt, xi, zi, xi_ind = generate_data()
55
56    mean = constant_mean
57    meanparam = None
58    covparam = gnp.array([math.log(0.5 ** 2), math.log(1 / .7)])
59    model = gp.core.Model(mean, kernel, meanparam, covparam)
60
61    n_samplepaths = 6
62    zsim = model.sample_paths(xt, n_samplepaths, method='chol')
63
64    zpm, zpv, lambda_t = model.predict(xi, zi, xt, return_lambdas=True)
65    zpsim = model.conditional_sample_paths(zsim, xi_ind, zi, gnp.arange(xt.shape[0]), lambda_t)
66
67    visualization(xt, zt, zpsim, xi, zi, zpm, zpv)
68
69
70if __name__ == "__main__":
71    main()