predict_f and predict_y as NamedTuple by antonykamp · Pull Request #1657 · GPflow/GPflow

PR type: enhancement

Related issue: #1567

Summary

Proposed changes

  • By requesting predict_f or predict_y, GPflow returns an instance of MeanAndVariance, a subclass of NamedTuple. By that, the user can access mean and variance individually.
  • You can find a hint to this enhancement in the example "Basic (Gaussian likelihood) GP regression model".

What alternatives have you considered?

I've considered shorter names for.mean and .variance but rejected this idea because the current resulting code is a) more readable b) already shorter than before.

Minimal working example

import gpflow
import numpy as np

rng = np.random.RandomState(0)

data = rng.randn(100, 2), rng.randn(100, 1)
Xtest, _ = rng.randn(Ntest, D), rng.randn(Ntest, 1)
kernel = Matern32() + gpflow.kernels.White()
model_gp = gpflow.models.GPR(data, kernel=kernel)

mu_f = model_gp.predict_f(Xtest).mean # instead of 'model.predict_f(Xtest)[0]' or 'mu_f, _ = model.predict_f(Xtest)'
var_y = model_gp.predict_y(Xtest).variance # instead of 'model.predict_y(Xtest)[1]' or '_, var_y = model.predict_y(Xtest)'

PR checklist

  • New features: code is well-documented
    • detailed docstrings (API documentation)
    • notebook examples (usage demonstration)
  • The bug case / new feature is covered by unit tests
  • Code has type annotations
  • I ran the black+isort formatter (make format)
  • I locally tested that the tests pass (make check-all)

Release notes

Fully backwards compatible: yes

Commit message (for release notes):

  • MeanAndVariance as NamedTuple
  • Added test about backwards compatibility
  • Added hint in docs, format

(Is likely to be rebased)