markovflow.kernels

Package containing kernels.

Package Contents

class Constant(variance: float, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Introduces a constant variance. This kernel has the formula:

\[C(x, x') = σ²\]

…where \(σ²\) is a kernel parameter representing the constant variance, which is supplied as a parameter to the constructor.

The transition matrix \(F\) in the SDE form for this kernel is \(F = [[1]]\).

Covariance for the steady state is \(P∞ = [[σ²]]\).

The state transition matrix is \(Aₖ = [[1]]\).

The process covariance is \(Qₖ = [[0]]\).

Parameters
  • variance – Initial variance for the kernel. Must be a positive float.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number used to make sure that matrix inversion is numerically stable.

property state_dimint

Return the state dimension of the generated StateSpaceModel.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the generated StateSpaceModel.

The state transition matrix at time step \(k\) is \(Aₖ = [[1]]\).

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Note this is ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

process_covariances(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the process covariance matrices of the generated StateSpaceModel.

The process covariance for time step k is \(Qₖ = [[0]]\).

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape `` batch_shape + [num_transitions]``. Note this is ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

transition_statistics(transition_times: tf.Tensor, time_deltas: tf.Tensor)Tuple[tf.Tensor, tf.Tensor]

Return the state_transitions and process_covariances.

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Note this is ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tuple of two tensors with respective shapes batch_shape + [num_transitions, state_dim, state_dim] batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]
Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance \(P∞\) of the generated StateSpaceModel. This is given by \(P∞ = [[σ²]]\).

Returns

A tensor with shape [state_dim, state_dim].

property variancegpflow.Parameter

Return the variance parameter.

class Kernel(name=None)[source]

Bases: tf.Module, abc.ABC

Abstract class generating a StateSpaceModel for a given set of time points.

For a given set of time points \(tₖ\), define a state space model of the form:

\[xₖ₊₁ = Aₖ xₖ + qₖ\]

…where:

\[\begin{split}&qₖ \sim 𝓝(0, Qₖ)\\ &x₀ \sim 𝓝(μ₀, P₀)\\ &xₖ ∈ ℝ^d\\ &Aₖ ∈ ℝ^{d × d}\\ &Qₖ ∈ ℝ^{d × d}\\ &μ₀ ∈ ℝ^{d × 1}\\ &P₀ ∈ ℝ^{d × d}\\ &d \verb| is the state_dim|\end{split}\]

And an EmissionModel for a given output dimension:

\[fₖ = H xₖ\]

…where:

\[\begin{split}&x ∈ ℝ^d\\ &f ∈ ℝ^m\\ &H ∈ ℝ^{m × d}\\ &m \verb| is the output_dim|\end{split}\]

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.

property output_dimint

Return the output dimension of the kernel.

abstract build_finite_distribution(time_points: tf.Tensor)markovflow.gauss_markov.GaussMarkovDistribution

Return the GaussMarkovDistribution that this kernel represents on the provided time points.

Note

Currently the only representation we can use is a StateSpaceModel.

Parameters

time_points – The times between which to define the distribution, with shape batch_shape + [num_data].

abstract generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Return the EmissionModel associated with this kernel that maps from the latent GaussMarkovDistribution to the observations.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data].

class LatentExponentiallyGenerated(N: tf.Tensor, R: tf.Tensor, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Represents the LEG-GPs kernel.

This kernel defines an SDE with state dimension \(d\), whose dynamics are governed by:

\[dx = -½ G x dt + N dw (w Brownian motion)\]

…with \(G = N Nᵀ + R - Rᵀ\), and \(N, R\) both arbitrary square matrices of size \(d × d\).

Note that:

  • \(C = R - Rᵀ\) is skew symmetric \((Cᵀ = -C)\)

  • If \(d\) is even, \(C\) has imaginary conjugate eigenvalue pairs \((iλ₁ ,-iλ₁, ...)\)

  • \(expm(C)\) is an orthogonal matrix (specifying an isometry)

The key reference is:

@article{loper2020general,
    title={General linear-time inference for Gaussian Processes on one dimension},
    author={Loper, Jackson and Blei, David and Cunningham, John P and Paninski, Liam},
    journal={arXiv preprint arXiv:2003.05554},
    year={2020}

}

Parameters
  • N – The Noise mixing matrix.

  • R – The Rotation inducing matrix.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Obtain the state transition matrices. That is:

\[Aₖ = expm[-½G Δtₖ]\]
Parameters
  • transition_times – Time points at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – Time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor of shape batch_shape + [num_transitions, state_dim, state_dim]

property feedback_matrixtf.Tensor

Return the feedback matrix.

Here, this is \(F (=-G/2)\) with shape \(d × d\).

property steady_state_covariancetf.Tensor

Obtain the steady state covariance \(P∞ = I\).

Returns

A tensor with shape [state_dim, state_dim].

process_covariances(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Obtain the process covariance at time \(k\). This is calculated as:

\[Qₖ = P∞ - Aₖ P∞ Aₖᵀ = I - Aₖ Aₖᵀ\]
Parameters
  • transition_times – Time points at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – Time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

class Matern12(lengthscale: float, variance: float, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Represents the Matern1/2 kernel. This kernel has the formula:

\[C(x, x') = σ² exp(-|x - x'| / ℓ)\]

…where lengthscale \(ℓ\) and signal variance \(σ²\) are kernel parameters.

This defines an SDE where:

\[\begin{split}&F = - 1/ℓ\\ &L = 1\end{split}\]

…so that \(Aₖ = exp(-Δtₖ/ℓ)\).

Parameters
  • lengthscale – A value for the lengthscale parameter.

  • variance – A value for the variance parameter.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension of the kernel, which is always one.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices kernel.

The state dimension is one, so the matrix exponential reduces to a standard one:

\[Aₖ = exp(-Δtₖ/ℓ)\]

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]

For this kernel, note that \(F = - 1 / ℓ\).

Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance \(P∞\). For this kernel, this is the variance hyperparameter.

Returns

A tensor with shape [state_dim, state_dim].

property lengthscalegpflow.Parameter

Return the lengthscale parameter. This is a GPflow Parameter.

property variancegpflow.Parameter

Return the variance parameter. This is a GPflow Parameter.

class Matern32(lengthscale: float, variance: float, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Represents the Matern3/2 kernel. This kernel has the formula:

\[C(x, x') = σ² (1 + λ|x - x'|) exp(λ|x - x'|)\]

…where \(λ = √3 / ℓ\), and lengthscale \(ℓ\) and signal variance \(σ²\) are kernel parameters.

The transition matrix \(F\) in the SDE form for this kernel is:

\[\begin{split}F = &[[0, 1]\\ &[[-λ², -2λ]]\end{split}\]

Covariance for the initial state is:

\[\begin{split}P∞ = [&[1, 0],\\ &[0, λ²]] * \verb|variance|\end{split}\]

…where variance is a kernel parameter.

Since the characteristic equation for the feedback matrix \(F\) for this kernel is \((λI + F)² = 0\), the state transition matrix is:

\[\begin{split}Aₖ &= expm(FΔtₖ)\\ &= exp(-λΔtₖ) expm((λI + F)Δtₖ)\\ &= exp(-λΔtₖ) (I + (λI + F)Δtₖ)\end{split}\]

…where \(expm\) is the matrix exponential operator. Note that all higher order terms of \(expm((λI + F)Δtₖ)\) disappear.

Parameters
  • lengthscale – A value for the lengthscale parameter.

  • variance – A value for the variance parameter.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property _lambdatf.Tensor

λ the scalar used elsewhere in the docstrings

property state_dimint

Return the state dimension of the kernel, which is always two.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices for the kernel.

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]

For this kernel, note that:

\[\begin{split}F = &[0 &1]\\ &[-λ² &-2λ]\end{split}\]
Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance \(P∞\). This is given by:

\[\begin{split}P∞ = σ² [&[1, 0],\\ &[0, λ²]]\end{split}\]
Returns

A tensor with shape [state_dim, state_dim].

property lengthscalegpflow.Parameter

Return the lengthscale parameter. This is a GPflow Parameter.

property variancegpflow.Parameter

Return the variance parameter. This is a GPflow Parameter.

class Matern52(lengthscale: float, variance: float, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Represents the Matern5/2 kernel. This kernel has the formula:

\[C(x, x') = σ² (1 + λ|x - x'| + λ²|x - x'|²/3) exp(λ|x - x'|)\]

…where \(λ = √5 / ℓ\), and lengthscale \(ℓ\) and signal variance \(σ²\) are kernel parameters.

The transition matrix \(F\) in the SDE form for this kernel is:

F = [  0,    1,   0]
    [  0,    0,   1]
    [-λ³, -3λ², -3λ]

Covariance for the initial state is:

P∞ = σ² [    1,    0, -λ²/3]
        [    0, λ²/3,     0]
        [-λ²/3,    0,    λ⁴]

Since the characteristic equation for the feedback matrix \(F\) for this kernel is \((λI + F)³ = 0\), the state transition matrix is:

\[\begin{split}Aₖ &= expm(FΔtₖ)\\ &= exp(-λΔtₖ) expm((λI + F)Δtₖ)\\ &= exp(-λΔtₖ) (I + (λI + F)Δtₖ + (λI + F)²Δtₖ²/2)\end{split}\]

…where \(expm\) is the matrix exponential operator. Note that all higher order terms disappear.

Parameters
  • lengthscale – A value for the lengthscale parameter.

  • variance – A value for the variance parameter.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property _lambdatf.Tensor

λ the scalar used elsewhere in the docstrings

property state_dimint

Return the state dimension of the kernel, which is always three.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices for the kernel.

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]

For this kernel, note that:

F = [[  0,    1,   0]
     [  0,    0,   1]
     [-λ³, -3λ², -3λ]]
Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance \(P∞\). This is given by:

P∞ = σ² [    1,    0, -λ²/3]
        [    0, λ²/3,     0]
        [-λ²/3,    0,    λ⁴]
Returns

A tensor with shape [state_dim, state_dim].

property lengthscalegpflow.Parameter

Return the lengthscale parameter. This is a GPflow Parameter.

property variancegpflow.Parameter

Return the variance parameter. This is a GPflow Parameter.

class OrnsteinUhlenbeck(decay: float, diffusion: float, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Represents the Ornstein–Uhlenbeck kernel. This is an alternative parameterization of the Matern1/2 kernel. This kernel has the formula:

\[C(x, x') = q/2λ exp(-λ|x - x'|)\]

…where decay \(λ\) and diffusion coefficient \(q\) are kernel parameters.

This defines an SDE where:

\[\begin{split}&F = - λ\\ &L = q\end{split}\]

…so that \(Aₖ = exp(-λ Δtₖ)\).

Parameters
  • decay – A value for the decay parameter.

  • diffusion – A value for the diffusion parameter.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension of the kernel, which is always one.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices kernel.

The state dimension is one, so the matrix exponential reduces to a standard one:

\[Aₖ = exp(-λ Δtₖ)\]

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]

For this kernel, note that \(F = -λ\).

Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance \(P∞\). For this kernel, this is q/2λ.

Returns

A tensor with shape [state_dim, state_dim].

property decaygpflow.Parameter

Return the decay parameter. This is a GPflow Parameter.

property diffusiongpflow.Parameter

Return the diffusion parameter. This is a GPflow Parameter.

class HarmonicOscillator(variance: float, period: float, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.StationaryKernel

Represents a periodic kernel. The definition is in the paper “Explicit Link Between Periodic Covariance Functions and State Space Models”.

This kernel has the formula:

\[C(x, x') = σ² cos(2π/p * (x-x'))\]

…where:

  • \(σ²\) is a kernel parameter, representing the constant variance this kernel introduces

  • \(p\) is the period of the oscillator in radius

The transition matrix \(F\) in the SDE form for this kernel is:

\[\begin{split}F = [&[0, -λ],\\ &[λ, 0]].\end{split}\]

…where \(λ = 2π / period\).

Covariance for the steady state is:

\[\begin{split}P∞ = [&[σ², 0],\\ &[0, σ²]].\end{split}\]

The state transition matrix is:

\[\begin{split}Aₖ = [&[cos(Δtₖλ), -sin(Δtₖλ)],\\ &[sin(Δtₖλ), cos(Δtₖλ)]]\end{split}\]

The process covariance is:

\[\begin{split}Qₖ = [&[0, 0],\\ &[0, 0]].\end{split}\]
Parameters
  • variance – Initial variance for the kernel. Must be a positive float.

  • period – The period of the Harmonic oscillator, in radius. Must be a positive float.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number used to make sure that matrix inversion is numerically stable.

property _lambdatf.Tensor

λ the scalar used elsewhere in the docstrings

property state_dimint

Return the state dimension of the generated StateSpaceModel.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the kernel.

The state transition matrix at time step \(k\) is:

\[\begin{split}Aₖ = [&[cos(Δtₖλ), -sin(Δtₖλ)],\\ &[sin(Δtₖλ), cos(Δtₖλ)]].\end{split}\]

…where \(λ = 2π / period\).

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

process_covariances(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the kernel.

The process covariance for time step k is:

\[\begin{split}Qₖ = [&[0, 0],\\ &[0, 0]].\end{split}\]

Because this is a stationary kernel, transition_times is ignored.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions]. Ignored.

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]

For this kernel, note that:

\[\begin{split}F = [&[0, -λ],\\ &[λ, 0]].\end{split}\]
Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the initial covariance of the generated StateSpaceModel.

The steady state covariance \(P∞\) is given by:

\[\begin{split}P∞ = [&[σ², 0],\\ &[0, σ²]].\end{split}\]
Returns

A tensor with shape [state_dim, state_dim].

property variancegpflow.Parameter

Return the variance parameter. This is a GPflow Parameter.

property periodgpflow.Parameter

Return the period parameter. This is a GPflow Parameter.

class PiecewiseKernel(kernels: List[markovflow.kernels.sde_kernel.StationaryKernel], change_points: gpflow.base.TensorType, output_dim: int = 1, jitter: float = 0.0)[source]

Bases: markovflow.kernels.sde_kernel.NonStationaryKernel

Construct an SDE kernel whose state dynamic is governed by different SDEs. These are on the \(K+1\) intervals specified by the \(K\) change points \(cₖ\).

On interval \([cₖ, cₖ₊₁]\), the dynamics are governed by a SDE kernel \(kₖ\) where \(c₀ = -∞\):

\[\begin{split}&dx(t)/dt = Fₖ x(t) + Lₖ w(t),\\ &f(t) = Hₖ x(t)\end{split}\]

Note the following:

  • This is currently restricted to cases where the kernels are the same.

  • State space models constructed by marginalizing out the process to time points \(t\) are only valid if no transitions cross a change point.

Parameters
  • kernels – An iterable over the kernels forming this kernel.

  • change_points – Sorted change points.

  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension of the generated StateSpaceModel.

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the EmissionModel associated with this kernel that maps from the latent StateSpaceModel to the observations.

The emission matrix is the Kronecker product of all the children emission matrices.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data].

initial_covariance(initial_time_point: tf.Tensor)tf.Tensor

Return the initial covariance of the generated StateSpaceModel.

This is the covariance of the stationary distribution \(P∞\) for the kernel active at the time passed in.

Parameters

initial_time_point – The time point associated with the first state, with shape batch_shape + [1,].

Returns

A tensor with shape batch_shape + [state_dim, state_dim].

split_time_indices(time_points: tf.Tensor)tf.Tensor

Gives each time point an index that refers to which interval it resides in.

The sub-kernel that governs the SDE is different on different sub intervals, as specified by the change points.

If there are \(K\) change points, then \(0\) is the index before the first change point and \(K + 1\) is the index after the last change point.

Parameters

time_points – A tensor with shape batch_shape + [num_time_points].

Returns

A tensor of indices in range 0 - num_change_points, with shape batch_shape + [num_time_points].

split_input(input_tensor: tf.Tensor, indices: tf.Tensor)List[tf.Tensor]

Partitions input_tensor into regions determined by the change points.

If there are \(K\) change points, then \(0\) is the index before the first change point and \(K + 1\) is the index after the last change point.

Parameters
  • input_tensor – An arbitrary input tensor, with shape batch_shape + [N].

  • indices – The index for each input of the input tensor, with shape batch_shape + [N].

Returns

A list of tensors each with shape batch_shape + [Nₖ], where \((Σₖ Nₖ = N)\).

steady_state_covariances(time_points: tf.Tensor)tf.Tensor

For each time point, return the steady state covariance of the kernel active for that time point.

Parameters

time_points – A tensor with shape batch_shape + [num_time_points].

Returns

The steady state covariance at each time point, with shape batch_shape + [num_time_points, state_dim, state_dim].

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the generated StateSpaceModel \(Aₖ = exp(FΔtₖ)\).

Note

Transitions are only valid if they do not cross a change point.

Parameters
  • transition_times – Time points at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – Time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

transition_statistics(transition_times: tf.Tensor, time_deltas: tf.Tensor)Tuple[tf.Tensor, tf.Tensor]

Return state_transitions() and process_covariances() together to save having to compute them twice.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tuple of two tensors, with respective shapes batch_shape + [num_transitions, state_dim, state_dim]. batch_shape + [num_transitions, state_dim, state_dim].

feedback_matrices(time_points: tf.Tensor)tf.Tensor

For each time point, return the non-stationary feedback matrix \(F(t)\) of the kernel active for that time point.

Parameters

time_points – A tensor with shape batch_shape + [num_time_points].

Returns

The feedback matrix at each time point, with shape batch_shape + [num_time_points, state_dim, state_dim].

state_offsets(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state offsets \(bₖ\) of the generated StateSpaceModel.

This will usually be zero, but can be overridden if necessary. :param transition_times: A tensor of times at which to produce matrices, with shape

batch_shape + [num_transitions].

Parameters

time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim]

state_means(time_points: tf.Tensor)tf.Tensor

For each time point, return the state mean of the kernel active for that time point.

Parameters

time_points – A tensor with shape batch_shape + [num_time_points].

Returns

The state mean at each time point batch_shape + [num_time_points, state_dim].

class ConcatKernel(kernels: List[SDEKernel], jitter: float = 0.0)[source]

Bases: StationaryKernel, abc.ABC

Abstract class implementing the state space model of multiple kernels that have been combined together. Combined with differing emission models this can give rise to the Sum kernel or to a multi-output kernel.

The state space of any ConcatKernel consists of all the state spaces of child kernels concatenated (in the tensorflow.concat sense) together:

\[\begin{split}[x¹(t),\\ x²(t)]\end{split}\]

So the SDE of the kernel becomes:

\[\begin{split}&dx(t)/dt = &[[F¹ 0], &[x¹(t) &[[L¹ 0], &[w¹(t),\\ & &[0 F²]] &x²(t)] + &[0 L²]] &w²(t)]\\ &f(t) = [H¹ H²] x(t)\end{split}\]
Parameters
  • kernels – A list of child kernels that will have their state spaces concatenated together.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension of the generated StateSpaceModel.

property kernelsList[SDEKernel]

Return a list of child kernels.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the generated StateSpaceModel \(Aₖ = exp(FΔtₖ)\).

The state transition matrix is the block diagonal matrix of the child state transition matrices.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

initial_mean(batch_shape: tf.TensorShape)tf.Tensor

Return the initial mean of the generated StateSpaceModel.

The combined mean is the child means concatenated together:

\[[μ1 μ2, ...]\]

…to form a longer mean vector.

Parameters

batch_shape – A tuple of leading dimensions for the initial mean.

Returns

A tensor of zeros with shape batch_shape + [state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix. This is the block diagonal matrix of child feedback matrices.

Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance. This is the block diagonal matrix of child steady state covariance matrices.

Returns

A tensor with shape [state_dim, state_dim].

class FactorAnalysisKernel(weight_function: Callable[[gpflow.base.TensorType], gpflow.base.TensorType], kernels: List[SDEKernel], output_dim: int, trainable: bool = True, jitter: float = 0.0)[source]

Bases: ConcatKernel

Produces an emission model which performs a linear mixing of Gaussian processes according to a known time varying weight function and a learnable loading matrix:

\[fᵢ(t) = Σⱼₖ Aᵢⱼ(t)Bⱼₖgₖ(t)\]

…where:

  • \(\{fᵢ\}ₙ\) are the observable processes

  • \(\{gₖ\}ₘ\) are the latent GPs

  • \(A^{n × m}\) is a known, possibly time dependant, weight matrix

  • \(B^{m × m}\) is either the identity or a trainable loading matrix

Parameters
  • weight_function – A function that, given TensorType time points with shape batch_shape + [num_data, ], returns a weight matrix with the relative mixing of the tensors, with shape batch_shape + [num_data, output_dim, n_latents].

  • kernels – An iterable over child kernels that will have their state spaces concatenated together, with shape [n_latents, ].

  • output_dim – The output dimension of the kernel. This should have the same shape as the output_dim of the weight matrix returned by the weight function.

  • trainable – Whether the loading matrix \(B\) should be trainable.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the emission matrix \(WH\). This is where:

\[H = H₁ ⊕ H₂ ⊕ ... ⊕ Hₙ\]

…as per the multi-output kernel, and \(W = AB\).

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data, ].

Returns

The emission model associated with this kernel.

class IndependentMultiOutput(kernels: List[SDEKernel], jitter: float = 0.0)[source]

Bases: ConcatKernel

Takes a concatenated state space model consisting of multiple child kernels and projects the state space associated with each kernel into a separate observation vector.

The result is similar to training several kernels on the same data separately, except that because of the covariance terms in the state space there can be correlation between the separate observation vectors.

Parameters
  • kernels – An iterable over child kernels which will have their state spaces concatenated together.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the emission matrix \(H\). This is the direct sum of the child emission matrices, for example:

\[H = H₁ ⊕ H₂ ⊕ ... ⊕ Hₙ\]

…where \(\{Hᵢ\}ₙ\) are the emission matrices of the child kernels.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data].

Returns

The emission model associated with this kernel.

class IndependentMultiOutputStack(kernels: List[SDEKernel], jitter: float = 0.0)[source]

Bases: StackKernel

Takes a stacked state space model consisting of multiple child kernels and projects the state space associated with each kernel into a separate observation vector.

The result is similar to training several kernels on the same data separately. There will be no correlations between the processes, in the prior or the posterior.

Parameters
  • kernels – An iterable over child kernels which will have their state spaces concatenated together. Since we model each output independently the length of the kernel list defines the number of the outputs.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the emission matrix \(H\). This is a stacking of the child emission matrices, which are first augmented (if necessary) so that they have the same state_dim.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data] where batch_shape = (..., num_kernels).

Returns

The emission model associated with this kernel.

__add__(other: IndependentMultiOutputStack)IndependentMultiOutputStack

Operator for combining kernel objects by summing them.

Overrides the base class SDEKernel.__add__() method.

__mul__(other: IndependentMultiOutputStack)IndependentMultiOutputStack

Operator for combining kernel objects by multiplying them.

Overrides the base class SDEKernel.__mul__() method.

class NonStationaryKernel(output_dim: int = 1, jitter: float = 0)[source]

Bases: SDEKernel, abc.ABC

Abstract class representing non-stationary kernels defined by the Stochastic Differential Equation:

\[\begin{split}&dx(t)/dt = F(t) x(t) + L(t) w(t),\\ &f(t) = H(t) x(t)\end{split}\]

For most kernels \(H\) will not be time varying; that is, \(f(t) = H x(t)\).

Parameters
  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

abstract feedback_matrices(time_points: tf.Tensor)tf.Tensor

The non-stationary feedback matrix \(F(t)\) at times \(t\), where:

\[dx(t)/dt = F(t) x(t) + L w(t)\]
Parameters

time_points – The times at which the feedback matrix is evaluated, with shape batch_shape + [num_time_points].

Returns

A tensor with shape batch_shape + [num_time_points, state_dim, state_dim].

abstract state_offsets(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state offsets \(bₖ\) of the generated StateSpaceModel.

This will usually be zero, but can be overridden if necessary. :param transition_times: A tensor of times at which to produce matrices, with shape

batch_shape + [num_transitions].

Parameters

time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim]

class Product(kernels: List[SDEKernel], jitter: float = 0.0)[source]

Bases: StationaryKernel

Multiplies a list of child kernels.

The feedback matrix is the Kronecker product of the feedback matrices from the child kernels. We will use a product kernel with two child kernels as an example. Let \(A\) and \(B\) be the feedback matrix from these two child kernels. The feedback matrix \(F\) of the product kernel is:

\[\begin{split}&F &= &A ⊗ B\\ & &= &[[A₁₁ B, ..., A₁ₙ B],\\ & & &...,\\ & & &[Aₙ₁ B, ..., Aₙₙ B]]\end{split}\]

…where \(⊗\) is the Kronecker product operator.

The state transition matrix is the Kronecker product of the state transition matrices from the child kernels. Let \(Aₖ\) and \(Bₖ\) be the state transition matrix from these two child kernels at time step \(k\). The state transition matrix \(Sₖ\) of the product kernel is:

\[\begin{split}&Sₖ &= &Aₖ ⊗ Bₖ\\ & &= &[[Aₖ₁₁ Bₖ, ..., Aₖ₁ₙ Bₖ],\\ & & &...,\\ & & &[Aₖₙ₁ Bₖ, ..., Aₖₙₙ Bₖ]]\end{split}\]

The steady state covariance matrix is the Kronecker product of the steady covariance matrix from the child kernels. Let \(A∞\) and \(B∞\) be the steady covariance matrix from these two child kernels. The state transition matrix \(P∞\) of the product kernel is:

\[\begin{split}&P∞ &= &A∞ ⊗ B∞\\ & &= &[[A∞₁₁ B∞, ..., A∞ B∞],\\ & & &...,\\ & & &[A∞ₙ₁ B∞, ..., A∞ₙₙ B∞]]\end{split}\]

The process covariance matrix \(Qₖ\) at time step \(k\) is calculated using the same formula as defined in the parent class SDEKernel:

\[Qₖ = P∞ - Sₖ P∞ Sₖᵀ\]

…where the steady state matrix \(P∞\) and the state transition \(Sₖ\) are defined above.

Parameters
  • kernels – An iterable over the kernels to be multiplied together.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension of the generated StateSpaceModel.

property kernelsList[SDEKernel]

Return a list of child kernels.

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition. This is the Kronecker product of the child state transitions.

Parameters
  • transition_times – A tensor of times at which to produce matrices, shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix. This is the Kronecker product of the child feedback matrices.

Returns

A tensor with shape [state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance. This is the Kronecker product of the child steady state covariances.

Returns

A tensor with shape [state_dim, state_dim].

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the emission matrix. This is the Kronecker product of all the child emission matrices.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data].

class SDEKernel(output_dim: int = 1, jitter: float = 0)[source]

Bases: markovflow.kernels.kernel.Kernel, abc.ABC

Abstract class representing kernels defined by the Stochastic Differential Equation:

\[\begin{split}&dx(t)/dt = F(t) x(t) + L(t) w(t),\\ &f(t) = H(t) x(t)\end{split}\]

For most kernels \(F, L, H\) are not time varying; these have the more restricted form:

\[\begin{split}&dx(t)/dt = F x(t) + L w(t),\\ &f(t) = H x(t)\end{split}\]

…with \(w(t)\) white noise process with spectral density \(Q_c\), where:

\[\begin{split}&x ∈ ℝ^d\\ &F, L ∈ ℝ^{d × d}\\ &H ∈ ℝ^{d × o}\\ &Q_c ∈ ℝ^d\\ &d \verb|is the state dimension|\\ &o \verb|is the observation dimension|\end{split}\]

See the documentation for the StationaryKernel class.

Usually:

\[x(t) = [a(t), da(t)/dt, d²a(t)/dt ...]\]

…for some \(a(t)\), so the state dimension represents the degree of the stochastic differential equation in terms of \(a(t)\). Writing it in the above form is a standard trick for converting a higher order linear differential equation into a first order linear one.

Since \(F, L, H\) are constant matrices, the solution can be written analytically. For a given set of time points \(tₖ\), we can solve this SDE and define a state space model of the form:

\[xₖ₊₁ = Aₖ xₖ + bₖ + qₖ\]

…where:

\[\begin{split}&qₖ \sim 𝓝(0, Qₖ)\\ &x₀ \sim 𝓝(μ₀, P₀)\\ &xₖ ∈ ℝ^d\\ &Aₖ ∈ ℝ^{d × d}\\ &bₖ ∈ ℝ^d\\ &Qₖ ∈ ℝ^{d × d}\\ &μ₀ ∈ ℝ^{d × 1}\\ &P₀ ∈ ℝ^{d × d}\end{split}\]

If \(Δtₖ = tₖ₊₁ - tₖ\), then the transition matrix \(Aₜ\) between states \(x(tₖ)\) and \(x(tₖ₊₁)\) is given by:

\[Aₖ = exp(FΔtₖ)\]

The process noise covariance matrix \(Qₖ\) between states \(x(tₖ)\) and \(x(tₖ₊₁)\) is given by:

\[Qₖ = ∫ exp(F (Δtₖ - τ)) L Q_c Lᵀ exp(F (Δtₖ - τ))ᵀ dτ\]

We can write this in terms of the steady state covariance \(P∞\) as:

\[Qₖ = P∞ - Aₖ P∞ Aₖᵀ\]

We also define an emission model for a given output dimension:

\[fₖ = H xₖ\]

…where:

\[\begin{split}&x ∈ ℝ^d\\ &f ∈ ℝ^m\\ &H ∈ ℝ^{m × d}\\ &m \verb| is the output_dim|\end{split}\]
Parameters
  • output_dim – The output dimension of the kernel.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property output_dimint

Return the output dimension of the kernel.

build_finite_distribution(time_points: tf.Tensor)markovflow.gauss_markov.GaussMarkovDistribution

Return the GaussMarkovDistribution that this kernel represents on the provided time points.

Note

Currently the only representation we can use is StateSpaceModel.

Parameters

time_points – The times between which to define the distribution, with shape batch_shape + [num_data].

state_space_model(time_points: tf.Tensor)markovflow.state_space_model.StateSpaceModel

Return the StateSpaceModel that this kernel represents on the provided time points.

Parameters

time_points – The times between which to define the state space model, with shape batch_shape + [num_data]. This must be strictly increasing.

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the EmissionModel associated with this kernel that maps from the latent StateSpaceModel to the observations.

For any SDEKernel, the state representation is usually:

\[x(t) = [a(t), da(t)/dt, d²a(t)/dt ...] \verb| for some | a(t)\]

In this case, we are interested only in the first element of \(x\). That is, the output \(f(t)\) is given by \(f(t) = a(t)\), so \(H\) is given by \([1, 0, 0, ...]\).

If different behaviour is required, this method should be overridden.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data].

initial_mean(batch_shape: tf.TensorShape)tf.Tensor

Return the initial mean of the generated StateSpaceModel.

This will usually be zero, but can be overridden if necessary.

Parameters

batch_shape – Leading dimensions for the initial mean.

Returns

A tensor of zeros with shape batch_shape + [state_dim].

property state_dimint

Return the state dimension of the generated StateSpaceModel.

abstract initial_covariance(initial_time_point: tf.Tensor)tf.Tensor

Return the initial covariance of the generated StateSpaceModel.

For stationary kernels this is typically the covariance of the stationary distribution for \(x, P∞\).

In the general case the initial covariance depends on time, so we need the initial_time_point to generate it.

Parameters

initial_time_point – The time_point associated with the first state, with shape batch_shape + [1,].

Returns

A tensor with shape batch_shape + [state_dim, state_dim].

transition_statistics_from_time_points(time_points: tf.Tensor)

Generate the transition matrices when the time deltas are between adjacent time_points.

Parameters

time_points – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions + 1].

Returns

A tuple of two tensors, with respective shapes batch_shape + [num_transitions, state_dim, state_dim] batch_shape + [num_transitions, state_dim, state_dim].

abstract transition_statistics(transition_times: tf.Tensor, time_deltas: tf.Tensor)Tuple[tf.Tensor, tf.Tensor]

Return the state_transitions() and process_covariances() together to save having to compute them twice.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tuple of two tensors, with respective shapes batch_shape + [num_transitions, state_dim, state_dim]. batch_shape + [num_transitions, state_dim, state_dim].

abstract state_offsets(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state offsets \(bₖ\) of the generated StateSpaceModel.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim].

abstract state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the generated StateSpaceModel \(Aₖ = exp(FΔtₖ)\).

Parameters
  • transition_times – Time points at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – Time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

process_covariances(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the process covariance matrices of the generated StateSpaceModel.

The process covariance at time \(k\) is calculated as:

\[Qₖ = P∞ - Aₖ P∞ Aₖᵀ\]

These transition matrices can be overridden for more specific use cases if necessary.

Parameters
  • transition_times – Time points at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – Time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim].

property jitter_matrixtf.Tensor

Jitter to add to the output of process_covariances() and initial_covariance() shape.

Returns

A tensor with shape [state_dim, state_dim].

__add__(other: SDEKernel)Sum

Operator for combining kernel objects by summing them.

__mul__(other: SDEKernel)Product

Operator for combining kernel objects by multiplying them.

class StackKernel(kernels: List[SDEKernel], jitter: float = 0.0)[source]

Bases: StationaryKernel

Implements the state space model of multiple kernels that have been combined together. Unlike a ConcatKernel, it manages the multiple kernels by introducing a leading dimension (stacking), rather than forming a block diagonal form of each parameter explicitly.

The prior of both a StackKernel and a ConcatKernel is the same (independent). However, posterior state space models built upon a StackKernel will maintain this independency, in contrast to the posteriors building upon a ConcatKernel, which model correlations between the processes.

Combined with different emission models this can give rise to a multi-output stack kernel, and perhaps in the future an additive kernel.

The state space of this kernel consists of all the state space of the child kernels stacked (in the tensorflow.stack sense) together, with padded zeros when the state space of one of the kernels is larger than any of the others:

[ x₁⁽¹⁾(t) ] ᨞
[   0   ]   [ x₁⁽ᵐ⁾(t) ]
          ᨞ [ x₂⁽ᵐ⁾(t) ]

…where \(m\) are the number of kernels / outputs.

So the SDE of the kernel becomes:

dx(t)/dt = [F⁽¹⁾] ᨞   [x⁽¹⁾(t)] ᨞    + [L⁽¹⁾] ᨞    [w⁽¹⁾(t)] ᨞
              ᨞ [F⁽ᵐ⁾]   ᨞ [x⁽ᵐ⁾(t)]      ᨞ [L⁽ᵐ⁾]    ᨞ [w⁽ᵐ⁾(t)]

f(t) = [H⁽¹⁾] ᨞   [x⁽¹⁾(t)] ᨞
          ᨞ [H⁽ᵐ⁾]   ᨞ [x⁽ᵐ⁾(t)]
Parameters
  • kernels – A list of child kernels that will have their state spaces concatenated together. Since we model each output independently, the length of the kernel list defines the number of the outputs. Note that each kernel should have individual output_dim 1.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

property state_dimint

Return the state dimension of the generated StateSpaceModel.

property kernelsList[SDEKernel]

Return a list of child kernels.

_check_batch_shape_is_compatible(batch_shape: tf.TensorShape)None

Helper method to check the compatibility of batch_shape. For the StackKernel the batch_shape must have the following shape:

(…, num_kernels)

In any other case this method raises a tf.errors.InvalidArgumentError.

Parameters

batch_shape – a tuple with the shape to check

state_transitions(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state transition matrices of the generated StateSpaceModel \(Aₖ = exp(FΔtₖ)\).

The state transition matrix is the stacked matrix of the child state transition matrices, padded with zeros (if necessary) to match the largest state dim across kernels.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions] where batch_shape = (..., num_kernels).

Returns

A tensor with shape batch_shape + [num_transitions, state_dim, state_dim] where batch_shape = (..., num_kernels).

initial_mean(batch_shape: tf.TensorShape)tf.Tensor

Return the initial mean of the generated StateSpaceModel.

This will usually be zero, but can be overridden if necessary.

We override SDEKernel.initial_mean() from the parent class to check there is a compatible batch_shape.

Parameters

batch_shape – A tuple of leading dimensions for the initial mean, where batch_shape can be (..., num_kernels).

Returns

A tensor of zeros with shape batch_shape + [state_dim], where batch_shape = (..., num_kernels).

state_offsets(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

Return the state offsets \(bₖ\) of the generated StateSpaceModel.

This will usually be zero, but can be overridden if necessary.

We override SDEKernel.state_offsets() from the parent class to check there is a compatible batch_shape.

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim]

property feedback_matrixtf.Tensor

Return the feedback matrix. This is the stacked matrix of child feedback matrices, padded with zeros to have matching state dims.

Returns

A tensor with shape [num_kernels, state_dim, state_dim].

property steady_state_covariancetf.Tensor

Return the steady state covariance. This is the stacked matrix of child steady state covariance matrices, padded with the identity (if necessary) to have matching state dims.

Note that we further append a singleton dimensions after the num_kernels so it can broadcast across the number of data.

Returns

A tensor with shape [num_kernels, 1, state_dim, state_dim].

initial_covariance(initial_time_point: tf.Tensor)tf.Tensor

Return the initial covariance of the generated StateSpaceModel.

This is typically the covariance of the stationary distribution for \(x, P∞\).

We override SDEKernel.initial_covariance() from the parent class to check there is a compatible batch_shape.

Parameters

initial_time_point – The time point associated with the first state, shape batch_shape + [1,].

Returns

A tensor with shape batch_shape + [state_dim, state_dim], where batch_shape = (..., num_kernels).

class StationaryKernel(output_dim: int = 1, jitter: float = 0, state_mean: Optional[tf.Tensor] = None, **kwargs)[source]

Bases: SDEKernel, abc.ABC

Abstract class representing stationary kernels defined by the Stochastic Differential Equation:

\[\begin{split}&dx(t)/dt = F x(t) + L w(t),\\ &f(t) = H(t) x(t)\end{split}\]

For most kernels \(H\) will not be time varying; that is, \(f(t) = H x(t)\).

Parameters
  • output_dim – The output dimension of the kernel.

  • state_mean – A tensor with shape [state_dim,].

initial_mean(batch_shape: tf.TensorShape)tf.Tensor

Return the initial mean of the generated StateSpaceModel.

This will usually be zero, but can be overridden if necessary.

Parameters

batch_shape – Leading dimensions for the initial mean.

Returns

A tensor of zeros with shape batch_shape + [state_dim].

set_state_mean(state_mean: tf.Tensor, trainable: bool = False)

Sets the state mean for the kernel.

Parameters
  • state_mean – A tensor with shape [state_dim,].

  • trainable – Boolean value to set the state mean trainable.

initial_covariance(initial_time_point: tf.Tensor)tf.Tensor

Return the initial covariance of the generated StateSpaceModel.

For stationary kernels this is the covariance of the stationary distribution for \(x,P∞\) and is independent of the time passed in.

Parameters

initial_time_point – The time point associated with the first state, with shape batch_shape + [1,].

Returns

A tensor with shape batch_shape + [state_dim, state_dim].

transition_statistics(transition_times: tf.Tensor, time_deltas: tf.Tensor)Tuple[tf.Tensor, tf.Tensor]

Return state_transitions() and process_covariances() together to save having to compute them twice.

By default this uses the state transitions to calculate the process covariance:

\[Qₖ = P∞ - Aₖ P∞ Aₖᵀ\]
Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tuple of two tensors, with respective shapes batch_shape + [num_transitions, state_dim, state_dim]. batch_shape + [num_transitions, state_dim, state_dim].

property feedback_matrixtf.Tensor

Return the feedback matrix \(F\). This is where:

\[dx(t)/dt = F x(t) + L w(t)\]
Returns

A tensor with shape [state_dim, state_dim].

state_offsets(transition_times: tf.Tensor, time_deltas: tf.Tensor)tf.Tensor

\(dx = F (x - m)dt o x(t) = A x(0) + (I-A)m\)

Parameters
  • transition_times – A tensor of times at which to produce matrices, with shape batch_shape + [num_transitions].

  • time_deltas – A tensor of time gaps for which to produce matrices, with shape batch_shape + [num_transitions].

Returns

A tensor with shape batch_shape + [num_transitions, state_dim]

property steady_state_covariancetf.Tensor

Return the steady state covariance \(P∞\), given implicitly by:

\[F P∞ + P∞ Fᵀ + LQ_cLᵀ = 0\]
Returns

A tensor with shape [state_dim, state_dim].

property state_meantf.Tensor

Return the state mean.

Returns

A tensor with shape [state_dim,].

class Sum(kernels: List[SDEKernel], jitter: float = 0.0)[source]

Bases: ConcatKernel

Sums a list of child kernels.

There are two ways to implement this kernel: Stacked and Concatenated.

This class implements the Concatenated version, where the state space of the Sum kernel includes covariance terms between the child kernels.

Parameters
  • kernels – A list of child kernels that will have their state spaces concatenated together.

  • jitter – A small non-negative number to add into a matrix’s diagonal to maintain numerical stability during inversion.

generate_emission_model(time_points: tf.Tensor)markovflow.emission_model.EmissionModel

Generate the emission matrix \(H\). This is the concatenation:

\[H = [H₁, H₂, ..., Hₙ]\]

…where \(\{Hᵢ\}ₙ\) are the emission matrices of the child kernels. Thus the state dimension for this kernel is the sum of the state dimension of the child kernels.

Parameters

time_points – The time points over which the emission model is defined, with shape batch_shape + [num_data].

Returns

The emission model associated with this kernel, with emission matrix with shape batch_shape + [num_data, output_dim, state_dim].