| """Padding must be a pure performance device: results must match exactly. |
| |
| Observation arrays are padded to shapes that depend only on the deployment index |
| so XLA compiles each shape once per job rather than once per campaign. That is |
| only legitimate if the padded points provably change nothing downstream. |
| """ |
|
|
| import jax |
| import jax.numpy as jnp |
| import numpy as np |
|
|
| jax.config.update("jax_enable_x64", True) |
|
|
| from ballast.experiment import SYNTH_PARAMS as P |
| from ballast.experiment import _pad |
| from ballast.gp import ( |
| log_marginal_likelihood, |
| posterior_ext_state, |
| posterior_mean_field, |
| ) |
| from ballast.policies import eig_utilities |
| from ballast.trajectory import Grid |
|
|
| SIGMA = 0.1 |
|
|
|
|
| def _data(n=17, seed=0): |
| k = jax.random.PRNGKey(seed) |
| k1, k2, k3 = jax.random.split(k, 3) |
| S = jax.random.uniform(k1, (n, 2), minval=-2, maxval=2) |
| t = jax.random.uniform(k2, (n,), minval=0.0, maxval=4.0) |
| y = jax.random.normal(k3, (n, 2)) |
| return S, t, y |
|
|
|
|
| def _grid(): |
| return Grid(jnp.linspace(-2, 2, 5), jnp.linspace(-2, 2, 5)) |
|
|
|
|
| def test_pad_preserves_posterior_mean_field(): |
| S, t, y = _data() |
| g = _grid() |
| te = jnp.array([0.0, 1.0, 2.0]) |
| ref = posterior_mean_field(S, t, y, g.R, te, P, SIGMA) |
| Sp, tp, yp, mp = _pad(np.asarray(S), np.asarray(t), np.asarray(y), 40) |
| got = posterior_mean_field(Sp, tp, yp, g.R, te, P, SIGMA, mask=mp) |
| np.testing.assert_allclose(got, ref, rtol=1e-10, atol=1e-12) |
|
|
|
|
| def test_pad_preserves_extended_state_posterior(): |
| S, t, y = _data(seed=1) |
| g = _grid() |
| m_ref, c_ref = posterior_ext_state(S, t, y, g.R, 4.0, P, SIGMA) |
| Sp, tp, yp, mp = _pad(np.asarray(S), np.asarray(t), np.asarray(y), 33) |
| m_got, c_got = posterior_ext_state(Sp, tp, yp, g.R, 4.0, P, SIGMA, mask=mp) |
| np.testing.assert_allclose(m_got, m_ref, rtol=1e-9, atol=1e-11) |
| np.testing.assert_allclose(c_got @ c_got.T, c_ref @ c_ref.T, rtol=1e-9, atol=1e-11) |
|
|
|
|
| def test_pad_preserves_eig_ranking(): |
| S, t, y = _data(seed=2) |
| g = _grid() |
| ref = eig_utilities(g, S, t, 4.0, P, SIGMA) |
| Sp, tp, yp, mp = _pad(np.asarray(S), np.asarray(t), np.asarray(y), 64) |
| got = eig_utilities(g, Sp, tp, 4.0, P, SIGMA, mask=mp) |
| np.testing.assert_allclose(got, ref, rtol=1e-9, atol=1e-11) |
|
|
|
|
| def test_pad_shifts_marginal_likelihood_by_a_constant_only(): |
| """Each padded point adds an identity block: the log-likelihood picks up a |
| fixed -log(2pi) per padded row and nothing else, so the optimiser's argmax |
| over hyperparameters is unchanged.""" |
| S, t, y = _data(seed=3) |
| ref = log_marginal_likelihood(P, S, t, y, SIGMA) |
| n_pad = 11 |
| Sp, tp, yp, mp = _pad(np.asarray(S), np.asarray(t), np.asarray(y), 17 + n_pad) |
| got = log_marginal_likelihood(P, Sp, tp, yp, SIGMA, mask=mp) |
| shift = -0.5 * (2 * n_pad) * np.log(2 * np.pi) |
| np.testing.assert_allclose(got, ref + shift, rtol=1e-9, atol=1e-10) |
|
|