markovflow.block_tri_diag
Module representing block tridiagonal matrices.
BlockTriDiagonal
Bases: abc.ABC
abc.ABC
Abstract class representing a block tridiagonal matrix.
All precisions in Markovflow are of this form, so this class provides an adapter between the TensorFlow banded_matrices and the MarkovFlow code.
diagonal – A tensor with shape [... outer_dim, inner_dim, inner_dim].
[... outer_dim, inner_dim, inner_dim]
symmetric – Whether the block tridiagonal matrix will be symmetric.
sub_diagonal – A tensor with shape [... outer_dim - 1, inner_dim, inner_dim].
[... outer_dim - 1, inner_dim, inner_dim]
as_band
Return a TensorFlow tensor (or NumPy array) representing a banded matrix.
The (dense) tensor should be of dimension \(K×N\), where \(K\) is the bandwidth of the represented \(N×N\) matrix.
bandwidth
Return the (lower) bandwidth of the tensor.
batch_shape
Return the batch shape of this object.
inner_dim
Return the inner dimension of the block tridiagonal matrix. That is, the dimensions of the block.
outer_dim
Return the outer dimension of the block tridiagonal matrix. That is, the number of blocks.
block_diagonal
Return the block diagonal.
Diagonal with shape [... outer_dim, inner_dim, inner_dim].
block_sub_diagonal
Return the block sub-diagonal, if it exists.
Sub-diagonal with shape [... outer_dim, inner_dim, inner_dim] or None.
None
to_dense
Convert this object to a dense tensor.
This is useful mainly for debugging and testing purposes.
A tensor with shape [... outer_dim * inner_dim, outer_dim * inner_dim]
[... outer_dim * inner_dim, outer_dim * inner_dim]
dense_mult
Multiply a dense vector by this object.
If this object is \(L\) and right is \(x\), calculate \(Lx\) as a dense tensor.
right – A tensor with shape [... outer_dim, inner_dim].
[... outer_dim, inner_dim]
transpose_left – Whether to transpose \(L\) before multiplying.
A tensor with shape [... outer_dim, inner_dim].
__add__
Add two BlockTriDiagonal tensors together.
_convert_to_band
A BandedMatrixTensor representation of this matrix
BandedMatrixTensor
_flatten_right
Reshape the rhs for banded_ops compatibility. See also self._assert_compatible_right_shape.
right – the tensor whose shape we want to alter.
_unflatten_right
flat_right – the tensor whose shape we want to alter.
_assert_compatible_right_shape
Make sure the Tensor ‘right’ is a suitable right-hand-side tensor for multiplication and solving. The inner and outer dims should match, and the broadcast_dim should be compatible.
right – the tensor whose shape we want to check.
LowerTriangularBlockTriDiagonal
Bases: BlockTriDiagonal
Represents a lower triangular block tridiagonal matrix:
[D₁ ] [A₁ D₂ ] [ A₂ D₃ ] [ ᨞ ᨞ ] [ Aₙ₋₁ Dₙ]
This is typically the Cholesky of a SymmetricBlockTriDiagonal.
SymmetricBlockTriDiagonal
Each matrix \(Dᵢ\) is lower triangular and square with dimension inner_dim. \(Aᵢ\) is square with dimension inner_dim.
The outer_dim is \(n\); that is, the number of block matrices on the main diagonal.
\(Dᵢ\) are the diagonal matrices and \(Aᵢ\) are the sub-diagonal matrices.
block_diagonal_of_inverse
If this object is \(L\) and \(M = LLᵀ\), return the block diagonal elements of \(M⁻¹\).
\(M⁻¹\) will not, in general, be a banded matrix, and we are normally interested only in the diagonal elements.
A tensor with shape [... outer_dim, inner_dim, inner_dim].
solve
If this object is \(L\) and right is \(x\), calculate \(L⁻¹ x\) as a dense tensor.
transpose_left – Whether to transpose \(L\) before solving.
abs_log_det
Return the absolute log determinant of this matrix. This is just the log of the product of diagonal elements (since it is lower triangular).
For numerical stability and nicer gradients, we do:
A tensor with shape batch_shape, representing the log determinant.
Add two LowerTriangularBlockTriDiagonal tensors together.
Represents a symmetric block tridiagonal matrix:
[D₁ A₁ᵀ ] [A₁ D₂ A₂ᵀ ] [ A₂ D₃ A₂ᵀ ] [ ᨞ ᨞ Aₙ₋₁ᵀ] [ Aₙ₋₁ Dₙ ]
This is the form of the precision matrix for a StateSpaceModel.
StateSpaceModel
Each matrix \(Dᵢ\) is symmetric square with dimension inner_dim. \(Aᵢ\) is square with dimension inner_dim.
Add two SymmetricBlockTriDiagonal tensors together.
cholesky
Calculates the Cholesky factorisation of this matrix.
Cholesky factorisations require a symmetric matrix. The cholesky_band matrix only operates on the lower triangle, so no copy is needed.
cholesky_band
Cholesky factorisations preserve band structure, so the result is a LowerTriangularBlockTriDiagonal with the same shape.
A matrix of the same shape, representing the Cholesky.
upper_diagonal_lower
For this matrix, calculate the \(UDUᵀ\) factorisation. This is where:
Uᵀ = [ I ] D = [ D₀ ] [U₁ᵀ, I ] [ D₁ ] [ U₂ᵀ, I ] [ ᨞ ] [ ᨞ ᨞ ] [ ᨞ ] [ Uₙᵀ, I] [ Dₙ]
\(D₀, D₁... Dₙ\) are symmetric.
This is related to the Cholesky \(LLᵀ\) and \(LDLᵀ\) decompositions, but is more natural when dealing with the inverses of matrices. That is, if \(K⁻¹ = UDUᵀ\) then \(K = LD⁻¹Lᵀ\) where \(L=U⁻ᵀ\).
This can be used to find the StateSpaceModel that represents this SymmetricBlockTriDiagonal, since:
Hence we can identify the state transition matrices \(Aᵢ = -Uᵢᵀ\) and the initial and process noise covariance matrices \(D₀= P₀⁻¹, Dᵢ= Qᵢ⁻¹\) with the above form:
We can write the following recurrence relation:
…where \(Kⱼₖ\) is the block matrix at location j, k of this matrix. This method allows us to return the posterior from a KalmanFilter as a StateSpaceModel.
KalmanFilter
A tuple of \(Uᵀ\) and chol_D.
chol_D
_banded_to_block_tri
Convert a banded matrix to a LowerTriangularBlockTriDiagonal.
NOTE This is designed for internal use with this module as it doesn’t check the shapes, so may not work for `BandedMatrixTensor`s that aren’t LowerTriangularBlockTriDiagonal.