markovflow.state_space_model
Module containing a state space model.
StateSpaceModel
Bases: markovflow.gauss_markov.GaussMarkovDistribution
markovflow.gauss_markov.GaussMarkovDistribution
Implements a state space model. This has the following form:
…where:
\(qₖ ~ 𝓝(0, Qₖ)\) \(x₀ ~ 𝓝(μ₀, P₀)\) \(xₖ ∈ ℝ^d\) \(bₖ ∈ ℝ^d\) \(Aₖ ∈ ℝ^{d × d}\) \(Qₖ ∈ ℝ^{d × d}\) \(μ₀ ∈ ℝ^{d × 1}\) \(P₀ ∈ ℝ^{d × d}\)
\(qₖ ~ 𝓝(0, Qₖ)\)
\(x₀ ~ 𝓝(μ₀, P₀)\)
\(xₖ ∈ ℝ^d\)
\(bₖ ∈ ℝ^d\)
\(Aₖ ∈ ℝ^{d × d}\)
\(Qₖ ∈ ℝ^{d × d}\)
\(μ₀ ∈ ℝ^{d × 1}\)
\(P₀ ∈ ℝ^{d × d}\)
The key reference is:
@inproceedings{grigorievskiy2017parallelizable, title={Parallelizable sparse inverse formulation Gaussian processes (SpInGP)}, author={Grigorievskiy, Alexander and Lawrence, Neil and S{"a}rkk{"a}, Simo}, booktitle={Int'l Workshop on Machine Learning for Signal Processing (MLSP)}, pages={1--6}, year={2017}, organization={IEEE} }
The model samples \(x₀\) with an initial Gaussian distribution in \(ℝ^d\) (in code \(d\) is state_dim).
state_dim
The model then proceeds for \(n\) (num_transitions) to generate \([x₁, ... xₙ]\), according to the formula above. The marginal distribution of samples at a point \(k\) is a Gaussian with mean \(μₖ, Pₖ\).
num_transitions
This class allows the user to generate samples from this process as well as to calculate the marginal distributions for each transition.
initial_mean – A TensorType containing the initial mean, with shape batch_shape + [state_dim].
TensorType
batch_shape + [state_dim]
chol_initial_covariance – A TensorType containing the Cholesky of the initial covariance, with shape batch_shape + [state_dim, state_dim]. That is, unless the initial covariance is zero, in which case it is zero.
batch_shape + [state_dim, state_dim]
state_transitions – A TensorType containing state transition matrices, with shape batch_shape + [num_transitions, state_dim, state_dim].
batch_shape + [num_transitions, state_dim, state_dim]
state_offsets – A TensorType containing the process means bₖ, with shape batch_shape + [num_transitions, state_dim].
batch_shape + [num_transitions, state_dim]
chol_process_covariances – A TensorType containing the Cholesky of the noise covariance matrices, with shape batch_shape + [num_transitions, state_dim, state_dim]. That is, unless the noise covariance is zero, in which case it is zero.
event_shape
Return the shape of the event.
The shape is [num_transitions + 1, state_dim].
[num_transitions + 1, state_dim]
batch_shape
Return the shape of any leading dimensions that come before event_shape.
Return the state dimension.
Return the number of transitions.
cholesky_process_covariances
Return the Cholesky of \([Q₁, Q₂, ....]\).
A TensorType with shape [... num_transitions, state_dim, state_dim].
[... num_transitions, state_dim, state_dim]
cholesky_initial_covariance
Return the Cholesky of \(P₀\).
A TensorType with shape [..., state_dim, state_dim].
[..., state_dim, state_dim]
initial_covariance
Return \(P₀\).
concatenated_cholesky_process_covariance
Return the Cholesky of \([P₀, Q₁, Q₂, ....]\).
A tensor with shape [... num_transitions + 1, state_dim, state_dim].
[... num_transitions + 1, state_dim, state_dim]
state_offsets
Return the state offsets \([b₁, b₂, ....]\).
A TensorType with shape [..., num_transitions, state_dim].
[..., num_transitions, state_dim]
initial_mean
Return the initial mean \(μ₀\).
A TensorType with shape [..., state_dim].
[..., state_dim]
concatenated_state_offsets
Return the concatenated state offsets \([μ₀, b₁, b₂, ....]\).
A tensor with shape [... num_transitions + 1, state_dim].
[... num_transitions + 1, state_dim]
state_transitions
Return the concatenated state offsets \([A₀, A₁, A₂, ....]\).
marginal_means
Return the mean of the marginal distributions at each time point. If:
…then return \(μₖ\).
If we let the concatenated state offsets be \(m = [μ₀, b₁, b₂, ....]\) and \(A\) be defined as in equation (5) of the SpInGP paper (see class docstring), then:
…which we can do quickly using a_inv_block().
a_inv_block()
The marginal means of the joint Gaussian, with shape batch_shape + [num_transitions + 1, state_dim].
batch_shape + [num_transitions + 1, state_dim]
marginal_covariances
Return the ordered covariances \(Σₖₖ\) of the multivariate normal marginal distributions over consecutive states \(xₖ\).
The marginal covariances of the joint Gaussian, with shape batch_shape + [num_transitions + 1, state_dim, state_dim].
batch_shape + [num_transitions + 1, state_dim, state_dim]
covariance_blocks
Return the diagonal and lower off-diagonal blocks of the covariance.
A tuple of tensors with respective shapes batch_shape + [num_transitions + 1, state_dim], batch_shape + [num_transitions, state_dim, state_dim].
a_inv_block
Return \(A⁻¹\).
This has the form:
A⁻¹ = [ I ] [-A₁, I ] [ -A₂, I ] [ ᨞ ᨞ ] [ -Aₙ, I]
…where \([A₁, ..., Aₙ]\) are the state transition matrices.
sample
Return sample trajectories.
sample_shape – The shape (and hence number of) trajectories to sample from the state space model.
A tensor containing state samples, with shape sample_shape + self.batch_shape + self.event_shape.
sample_shape + self.batch_shape + self.event_shape
subsequent_covariances
For each pair of subsequent states \(xₖ, xₖ₊₁\), return the covariance of their joint distribution. That is:
marginal_covariances – The marginal covariances of each state in the model, with shape batch_shape + [num_transitions + 1, state_dim, state_dim].
The covariance between subsequent state, with shape batch_shape + [num_transitions, state_dim, state_dim].
log_det_precision
Calculate the log determinant of the precision matrix. This uses the precision as defined in the SpInGP paper (see class summary above).
Precision is defined as:
so:
A tensor with shape batch_shape.
create_non_trainable_copy
Create a non-trainable version of GaussMarkovDistribution.
GaussMarkovDistribution
This is to convert a trainable version of this class back to being non-trainable.
A Gauss-Markov distribution that is a copy of this one.
create_trainable_copy
Create a trainable version of this state space model.
This is primarily for use with variational approaches where we want to optimise the parameters of a state space model that is initialised from a prior state space model.
The initial mean and state transitions are the same.
The initial and process covariances are ‘flattened’. Since they are lower triangular, we only want to parametrise this part of the matrix. For this purpose we use the params.triangular constraint which is the tfp.bijectors.FillTriangular bijector that converts between a triangular matrix \([dim, dim]\) and a flattened vector of shape \([dim (dim + 1) / 2]\).
params.triangular
tfp.bijectors.FillTriangular
A state space model that is a copy of this one and a dataclass containing the variables that can be trained.
_build_precision
Compute the compact banded representation of the Precision matrix using state space model parameters.
K⁻¹ = A⁻ᵀQ⁻¹A⁻¹
Using Q⁻¹ and A⁻¹ defined in equations (6) and (8) in the SpInGP paper (see class docstring)
It can be shown that
The precision as a SymmetricBlockTriDiagonal object
SymmetricBlockTriDiagonal
_log_pdf_factors
Return the value of the log of the factors of the probability density function (PDF) evaluated at a state trajectory:
[log p(x₀), log p(x₁|x₀), ..., log p(xₖ₊₁|xₖ)]
…with x₀ ~ 𝓝(μ₀, P₀) and xₖ₊₁|xₖ ~ 𝓝(Aₖ xₖ + bₖ, Qₖ)
The state trajectory has shape: sample_shape + self.batch_shape + self.event_shape
The log PDF of the factors with shape: sample_shape + self.batch_shape + [self.num_transitions + 1]
log_pdf
Return the value of the log of the probability density function (PDF) evaluated at states. That is:
states – The state trajectory, with shape sample_shape + self.batch_shape + self.event_shape.
The log PDF, with shape sample_shape + self.batch_shape.
sample_shape + self.batch_shape
kl_divergence
Return the KL divergence of the current Gauss-Markov distribution from the specified input dist. That is:
dist
To do so we first compute the marginal distributions from the Gauss-Markov form:
\(μᵢ\) are the marginal means \(Pᵢ\) are the banded precisions
\(μᵢ\) are the marginal means
\(Pᵢ\) are the banded precisions
The KL divergence is thus given by:
…where \(N = (\verb |num_transitions| + 1) * \verb |state_dim|\) (that is, the dimensionality of the Gaussian).
dist – Another similarly parameterised Gauss-Markov distribution.
A tensor of the KL divergences, with shape self.batch_shape.
self.batch_shape
normalizer
Conputes the normalizer Page 36 of Thang Bui :return:
state_space_model_from_covariances
Construct a state space model using the full covariance matrices for convenience.
initial_mean – The initial mean, with shape batch_shape + [state_dim].
initial_covariance – Initial covariance, with shape batch_shape + [state_dim, state_dim].
state_transitions – State transition matrices, with shape batch_shape + [num_transitions, state_dim, state_dim].
state_offsets – The process means \(bₖ\), with shape batch_shape + [num_transitions, state_dim].
process_covariances – Noise covariance matrices, with shape batch_shape + [num_transitions, state_dim, state_dim].