markovflow.likelihoods

Package containing observation models, or ‘likelihoods’.

Package Contents

class Likelihood(name=None)[source]

Bases: tf.Module, abc.ABC

Abstract class for likelihoods.

A likelihood defines the observation model relating the observed variables \(Y\) to the latent variables \(F\) of a generative model. The observation model is specified through its conditional density \(p(Y|F)\).

In order to perform variational inference with non-Gaussian likelihoods, a ‘variational expectation’ should be computed under a Gaussian distribution \(q(F) ~ N(μ, Σ)\). This can be defined as:

\[∫ q(F) log p(Y|F) dF\]

Note that the predictive density:

\[∫ q(F) p(Y|F) dF\]

…is a useful metric to evaluate the quality of a Gaussian approximation \(q(F) ~ N(μ, Σ)\) to the posterior density \(p(F|Y)\).

Note

Implementations of this class should typically avoid performing computation in their __init__ method. Performing computation in the constructor conflicts with running in TensorFlow’s eager mode (and computation of gradients etc).

abstract log_probability_density(fs: tf.Tensor, observations: tf.Tensor)tf.Tensor

Compute the log probability density \(log p(Y|F)\).

Parameters
  • fs – A conditioning variable, with shape batch_shape + [num_data, obs_dim].

  • observations – A conditioned variable, with shape batch_shape + [num_data, obs_dim].

Returns

A tensor representing \(log p(yᵢ | fᵢ)\), with shape batch_shape + [num_data].

abstract variational_expectations(f_means: tf.Tensor, f_covariances: tf.Tensor, observations: tf.Tensor)tf.Tensor

Calculate a variational expectation for each observation:

\[∫ log(p(yᵢ|fᵢ)) q(fᵢ) df\]

…where \(q(f) ~ N(μ, P)\).

Note that \(p(y |f)\) is defined by the type of likelihood function, as specified by the observation model.

This term is used when calculating the evidence lower bound (ELBO):

\[ℒ(q) = Σᵢ ∫ log(p(yᵢ|fᵢ)) q(fᵢ) df - KL[q(F) ‖ p(F)]\]
Parameters
  • f_means – The marginal \(f\) means for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim].

  • f_covariances – The marginal \(f\) covariances for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim, obs_dim].

  • observations – The \(y\) values at which to evaluate the log probability, with shape batch_shape + [num_data, obs_dim].

Returns

A tensor with shape batch_shape + [num_data].

abstract predict_density(f_means: tf.Tensor, f_covariances: tf.Tensor, observations: tf.Tensor)tf.Tensor

Predict the density.

That is, calculate \(∫ q(F) p(Y|F) dF\) of a Gaussian approximation \(q(F) ~ N(μ, Σ)\) to the posterior density \(p(F|Y)\).

Parameters
  • f_means – The marginal \(f\) means for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim].

  • f_covariances – The marginal \(f\) covariances for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim, obs_dim].

  • observations – The \(y\) values at which to evaluate the log probability, with shape batch_shape + [num_data, obs_dim].

Returns

A tensor with shape batch_shape + [num_data].

abstract predict_mean_and_var(f_means: tf.Tensor, f_covariances: tf.Tensor)Tuple[tf.Tensor, tf.Tensor]

Compute the means and covariances of the posterior predictive distribution over outputs \(y*\) at \(x*\).

The (in most case intractable) density is given by:

\[p(y* | x*, x, y) = ∫ p(y* | f*) p(f* | x*, x, y) df*\]

…where:

  • \(p(f* | x*, x, y)\) is Gaussian with moments f_means and f_covariances

  • \(p(y* | f*)\) is defined by the likelihood

Parameters
  • f_means – The marginal \(f\) means for some arbitrary predicted time points, with shape batch_shape + [num_data, obs_dim].

  • f_covariances – The marginal \(f\) covariances for some arbitrary predicted time points, with shape batch_shape + [num_data, obs_dim, obs_dim].

Returns

A tuple of tensors containing observation means and covariances, with respective shapes batch_shape + [num_time_points, obs_dim], batch_shape + [num_time_points, obs_dim, obs_dim].

class PEPGaussian(base: gpflow.likelihoods.Gaussian, **kwargs)[source]

Bases: PEPScalarLikelihood

Wrapper around the univariate Gaussian Likelihood.

Parameters
  • base – A Gaussian Likelihood object

  • kwargs – dictionary of additional parameters

log_expected_density(Fmu, Fvar, Y, alpha=1.0)

Compute log ∫ p(y=Y|f)ᵃ q(f) df, where q(f) = N(f;Fmu, Fvar)

log ∫ p(y=Y|f)ᵃ q(f) df = log ∫ N(y; f, σ²) ᵃ N(f; Fmu, Fvar) df = log N(y; Fmu, σ² + Fvar)

Parameters
  • Fmu – mean function evaluation Tensor, with shape […, latent_dim]

  • Fvar – variance of function evaluation Tensor, with shape […, latent_dim]

  • Y – observation Tensor, with shape […, latent_dim]

  • alpha – scalar

grad_log_expected_density(Fmu, Fvar, Y, alpha=1.0)

Noting I(q) = log ∫ p(y=Y|f)ᵃ q(f) df, where q(f) = N(Fmu, Fvar), this computes ∇I(q) and ∇∇I(q), where the gradient is wrt Fmu. :param Fmu: mean function evaluation Tensor, with shape […, latent_dim] :param Fvar: variance of function evaluation Tensor, with shape […, latent_dim] :param Y: observation Tensor, with shape […, observation_dim]: :param alpha: scalar

abstract _conditional_mean(F)

The conditional mean of Y|F

abstract _conditional_variance(F)

The conditional variance of Y|F

class PEPScalarLikelihood(base: gpflow.likelihoods.ScalarLikelihood, num_gauss_hermite_points=20, **kwargs)[source]

Bases: gpflow.likelihoods.ScalarLikelihood

Wrapper around GPflow likelihoods, adding functionality to compute Power Expectation Propagation updates

Parameters
  • base – base likelihood object

  • num_gauss_hermite_points – number of Gauss-Hermite points

  • kwargs – additional arguments dictionary

_scalar_log_prob(F, Y)

Compute log p(Y|F). :param F: function evaluation Tensor, with shape […, latent_dim] :param Y: observation Tensor, with shape […, latent_dim]

_scalar_alpha_prob(F, Y, alpha=1.0)

Compute p(Y|F) :param F: function evaluation Tensor, with shape […, latent_dim] :param Y: observation Tensor, with shape […, latent_dim] :param alpha: scalar

log_expected_density(Fmu, Fvar, Y, alpha=1.0)

Compute log ∫ p(y=Y|f)ᵃ q(f) df, where q(f) = N(Fmu, Fvar) :param Fmu: mean function evaluation Tenself._quadrature_reduction(

self.quadrature.logspace(self._scalar_log_prob, Fmu, Fvar, Y=Y)

)sor, with shape […, latent_dim] :param Fvar: variance of function evaluation Tensor, with shape […, latent_dim] :param Y: observation Tensor, with shape […, observation_dim]: :param alpha: scalar

grad_log_expected_density(Fmu, Fvar, Y, alpha=1.0)

Noting I(q) = log ∫ p(y=Y|f)ᵃ q(f) df, where q(f) = N(Fmu, Fvar), this computes ∇I(q) and ∇∇I(q), where the gradient is wrt Fmu. :param Fmu: mean function evaluation Tensor, with shape […, latent_dim] :param Fvar: variance of function evaluation Tensor, with shape […, latent_dim] :param Y: observation Tensor, with shape […, observation_dim]: :param alpha: scalar

abstract _conditional_mean(F)

The conditional mean of Y|F

abstract _conditional_variance(F)

The conditional variance of Y|F

class MultivariateGaussian(chol_covariance: gpflow.base.TensorType)[source]

Bases: markovflow.likelihoods.likelihoods.Likelihood

Represents a multivariate Gaussian likelihood. For example:

\[p(yᵢ | fᵢ) = 𝓝(yᵢ; fᵢ, Σ= LLᵀ)\]

See also the documentation for the base Likelihood class.

Parameters

chol_covariance – A TensorType containing the Cholesky factor of the covariance of the Gaussian noise, with shape [obs_dim, obs_dim].

property obs_dimint

Return the dimensionality of each observation.

log_probability_density(fs: tf.Tensor, observations: tf.Tensor)tf.Tensor

Compute the log probability density \(log p(Y|F)\).

For a multivariate Gaussian, this is \(log 𝓝(yᵢ; fᵢ, Σ)\).

Parameters
  • fs – A tensor representing a conditioning variable, with shape batch_shape + [num_data, obs_dim].

  • observations – A tensor representing a conditioned variable, with shape batch_shape + [num_data, obs_dim].

Returns

A tensor representing \(log p(yᵢ | fᵢ)\), with shape batch_shape + [num_data].

variational_expectations(f_means: tf.Tensor, f_covariances: tf.Tensor, observations: tf.Tensor)tf.Tensor

Calculate a variational expectation for each observation:

\[∫ q(fᵢ) log p(yᵢ|fᵢ) dfᵢ\]

…where:

  • \(q(fᵢ) ~ N(μᵢ, Σᵢ)\)

  • \(p(y |f)\) is a general likelihood function

For a multivariate Gaussian this is:

\[∫ 𝓝(fᵢ; μᵢ, Sᵢ) log𝓝(yᵢ; fᵢ, Σ= LLᵀ) dfᵢ = -½ Tr(Σ⁻¹Sᵢ) + log𝓝(yᵢ; μᵢ, Σ)\]
Parameters
  • f_means – The marginal \(f\) means for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim].

  • f_covariances – The marginal \(f\) covariances for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim, obs_dim].

  • observations – The \(y\) values at which to evaluate the log probability, with shape batch_shape + [num_data, obs_dim].

Returns

A tensor with shape batch_shape + [num_data].

predict_density(f_means: tf.Tensor, f_covariances: tf.Tensor, observations: tf.Tensor)tf.Tensor

Predict a density. This calculates:

\[∫ q(F) p(Y|F) dF\]

…of a Gaussian approximation \(q(F) ~ N(μ, Σ)\) to the posterior density \(p(F|Y)\).

For a multivariate Gaussian this is:

\[log ∫ 𝓝(fᵢ; μᵢ, Sᵢ) 𝓝(yᵢ; fᵢ, Σ= LLᵀ) dfᵢ = log 𝓝(yᵢ; μᵢ, Σ + Sᵢ)\]
Parameters
  • f_means – The marginal \(f\) means for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim].

  • f_covariances – The marginal \(f\) covariances for each state of the StateSpaceModel, with shape batch_shape + [num_data, obs_dim, obs_dim].

  • observations – The \(y\) values at which to evaluate the log probability, with shape batch_shape + [num_data, obs_dim].

predict_mean_and_var(f_means: tf.Tensor, f_covariances: tf.Tensor)Tuple[tf.Tensor, tf.Tensor]

Predict the observation means and covariances given the f-space means and covariances.

That is, calculate:

\[p(y* | x*, x, y) = ∫ p(y* | f*) p(f* | x*, x, y) df*\]

…where:

  • f_means and f_covariances is our representation of \(p(f* | x*, x, y)\)

  • \(p(y* | f*)\) is defined by the likelihood

For a multivariate Gaussian this is:

\[p(y* | x*, x, y) = ∫ 𝓝(f*; μ*, S*) 𝓝(y*; f*, Σ= LLᵀ) dfᵢ = 𝓝(y*; μ*, Σ + S*)\]
Parameters
  • f_means – The marginal \(f\) means for some arbitrary predicted time points, with shape batch_shape + [num_data, obs_dim].

  • f_covariances – The marginal \(f\) covariances for some arbitrary predicted time points, with shape batch_shape + [num_data, obs_dim, obs_dim].

Returns

A tuple of tensors containing observation means and covariances, with respective shapes batch_shape + [num_time_points, obs_dim], batch_shape + [num_time_points, obs_dim, obs_dim].