Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/interrogate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Clone the reference repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Set up Python 3.10.6
- name: Set up Python 3.10
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: '3.10.6'
python-version: '3.10'

- name: Install dependencies
run: |
Expand Down
32 changes: 32 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,38 @@ Types
.. autoclass:: dynamax.linear_gaussian_ssm.PosteriorGSSMFiltered
.. autoclass:: dynamax.linear_gaussian_ssm.PosteriorGSSMSmoothed

Switching Linear Dynamical System
==================================

Base class
----------

.. autoclass:: dynamax.slds.SLDS
:show-inheritance:
:members: initialize, fit_em, approximate_posterior, most_likely_states, expected_emissions, sample

High-level models
-----------------

.. autoclass:: dynamax.slds.GaussianSLDS
:show-inheritance:

.. autoclass:: dynamax.slds.RecurrentGaussianSLDS
:show-inheritance:

Types
-----

.. autoclass:: dynamax.slds.ParamsSLDS
.. autoclass:: dynamax.slds.ParamsSLDSDynamics
.. autoclass:: dynamax.slds.ParamsSLDSEmissions
.. autoclass:: dynamax.slds.ParamsStationarySLDSTransitions
.. autoclass:: dynamax.slds.ParamsRecurrentSLDSTransitions
.. autoclass:: dynamax.slds.PosteriorSLDS
.. autoclass:: dynamax.slds.DiscretePosteriorSLDS
.. autoclass:: dynamax.slds.ContinuousPosteriorSLDS
.. autoclass:: dynamax.linear_gaussian_ssm.block_tridiag.BlockTridiagPosterior

Nonlinear Gaussian GSSM
========================

Expand Down
8 changes: 8 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ If you're new to these models, we recommend you start at the top and work your w
notebooks/generalized_gaussian_ssm/cmgf_poisson_demo.ipynb


.. toctree::
:maxdepth: 1
:caption: SLDS

notebooks/slds/slds.ipynb
notebooks/slds/rslds.ipynb


API documentation
==================

Expand Down
594 changes: 594 additions & 0 deletions docs/notebooks/slds/rslds.ipynb

Large diffs are not rendered by default.

771 changes: 771 additions & 0 deletions docs/notebooks/slds/slds.ipynb

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions dynamax/hidden_markov_model/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,8 @@ def _step(carry, args):
A = get_trans_mat(transition_matrix, transition_fn, t)

# Fold in the next state (Eq. 8.2 of Saarka, 2013)
# If hard 0. in predicted_probs_next, set relative_probs_next as 0. to avoid NaN values
relative_probs_next = jnp.where(jnp.isclose(predicted_probs_next, 0.0), 0.0,
smoothed_probs_next / predicted_probs_next)
# Divide by 1 where predicted_probs_next is 0: relative_probs_next is only ever multiplied by 0 there
relative_probs_next = smoothed_probs_next / jnp.where(predicted_probs_next == 0, 1., predicted_probs_next)
smoothed_probs = filtered_probs * (A @ relative_probs_next)
smoothed_probs /= smoothed_probs.sum()

Expand Down Expand Up @@ -580,9 +579,8 @@ def _step(carry, args: Tuple[Array, Array, Array, Int[Array, ""]]):
A = _get_params(transition_matrix, 2, t)

# Compute smoothed transition probabilities (Eq. 8.4 of Saarka, 2013)
# If hard 0. in predicted_probs_next, set relative_probs_next as 0. to avoid NaN values
relative_probs_next = jnp.where(jnp.isclose(predicted_probs_next, 0.0), 0.0,
smoothed_probs_next / predicted_probs_next)
# Divide by 1 where predicted_probs_next is 0: relative_probs_next is only ever multiplied by 0 there
relative_probs_next = smoothed_probs_next / jnp.where(predicted_probs_next == 0, 1., predicted_probs_next)
smoothed_trans_probs = filtered_probs[:, None] * A * relative_probs_next[None, :]
smoothed_trans_probs /= smoothed_trans_probs.sum()
return carry + smoothed_trans_probs, None
Expand Down Expand Up @@ -618,7 +616,8 @@ def _compute_all_transition_probs(
filtered_probs = hmm_posterior.filtered_probs[:-1]
smoothed_probs_next = hmm_posterior.smoothed_probs[1:]
predicted_probs_next = hmm_posterior.predicted_probs[1:]
relative_probs_next = smoothed_probs_next / predicted_probs_next
# Divide by 1 where predicted_probs_next is 0: relative_probs_next is only ever multiplied by 0 there
relative_probs_next = smoothed_probs_next / jnp.where(predicted_probs_next == 0, 1., predicted_probs_next)

def _compute_probs(t):
"""Compute the transition probabilities at time t."""
Expand Down
15 changes: 15 additions & 0 deletions dynamax/hidden_markov_model/inference_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,18 @@ def test_hmm_fixed_lag_smoother_nonstationary():
posterior = core.hmm_fixed_lag_smoother(
jnp.array([0.5, 0.5]), transitions, log_likelihoods, window_size=2)
assert jnp.allclose(posterior.smoothed_probs[1], jnp.array([[0.9, 0.1], [0.9, 0.1]]))


def test_compute_transition_probs_nonstationary_zero_columns(key=0, num_timesteps=5, num_states=3):
"""Test the time-varying transition probabilities when predicted_probs_next is 0."""
initial_probs, transition_matrix, log_likelihoods = random_hmm_args(jr.PRNGKey(key), num_timesteps, num_states)
transition_matrix = transition_matrix.at[:, 2].set(0.0)
transition_matrix /= transition_matrix.sum(1, keepdims=True)
transition_matrices = jnp.tile(transition_matrix, (num_timesteps - 1, 1, 1))
posterior = core.hmm_smoother(initial_probs, transition_matrices, log_likelihoods)

log_joint = big_log_joint(initial_probs, transition_matrix, log_likelihoods)
joint = jnp.exp(log_joint - logsumexp(log_joint))
for t in range(num_timesteps - 1):
trans_probs_t = jnp.sum(joint, axis=tuple(jnp.arange(t)) + tuple(jnp.arange(t + 2, num_timesteps)))
assert jnp.allclose(posterior.trans_probs[t], trans_probs_t, atol=1e-4)
268 changes: 268 additions & 0 deletions dynamax/linear_gaussian_ssm/block_tridiag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
"""
Gaussian distributions over a sequence of states whose precision matrix is block tridiagonal.

Such a Gaussian is stored in information form: a precision block for each time step, a block coupling
each pair of consecutive time steps, and a linear term for each time step. This is the form of the
posterior of a linear Gaussian state space model, and of the approximate posterior over the continuous
states of a switching linear dynamical system. The cost of every routine here grows linearly with the
number of time steps.
"""
from typing import NamedTuple, Tuple

import jax.numpy as jnp
import jax.random as jr
from jax import lax
from jax.scipy.linalg import cho_solve, solve_triangular
from jaxtyping import Array, Float

from dynamax.types import PRNGKeyT, Scalar
from dynamax.utils.utils import symmetrize


class BlockTridiagPotentials(NamedTuple):
"""Natural parameters of a Gaussian over a sequence of states with a block-tridiagonal precision matrix.

:param J_diag: diagonal blocks of the precision matrix, shape (T, D, D)
:param J_lower: blocks just below the diagonal of the precision matrix, shape (T-1, D, D); each entry couples
a pair of consecutive time steps
:param h: linear potentials, shape (T, D)
"""
J_diag: Float[Array, "num_timesteps state_dim state_dim"]
J_lower: Float[Array, "num_timesteps_minus_1 state_dim state_dim"]
h: Float[Array, "num_timesteps state_dim"]


class BlockTridiagPosterior(NamedTuple):
"""Marginals of a Gaussian with block-tridiagonal precision, as returned by :func:`block_tridiag_mvn_smoother`.

:param J_diag: diagonal blocks of the precision matrix, shape (T, D, D)
:param J_lower: blocks just below the diagonal of the precision matrix, shape (T-1, D, D)
:param h: linear potentials, shape (T, D)
:param log_normalizer: log normalizing constant of the Gaussian
:param log_det_precision: log determinant of the full precision matrix
:param filtered_precisions: precisions of the filtering distributions, shape (T, D, D)
:param filtered_linear_potentials: linear potentials of the filtering distributions, shape (T, D)
:param smoothed_means: mean of each state, shape (T, D)
:param smoothed_covariances: covariance of each state, shape (T, D, D)
:param smoothed_cross_covariances: expected outer product of each state with the next one, shape
(T-1, D, D). The product of the means is not subtracted, as in
:func:`~dynamax.linear_gaussian_ssm.lgssm_smoother`.
"""
J_diag: Float[Array, "num_timesteps state_dim state_dim"]
J_lower: Float[Array, "num_timesteps_minus_1 state_dim state_dim"]
h: Float[Array, "num_timesteps state_dim"]
log_normalizer: Scalar
log_det_precision: Scalar
filtered_precisions: Float[Array, "num_timesteps state_dim state_dim"]
filtered_linear_potentials: Float[Array, "num_timesteps state_dim"]
smoothed_means: Float[Array, "num_timesteps state_dim"]
smoothed_covariances: Float[Array, "num_timesteps state_dim state_dim"]
smoothed_cross_covariances: Float[Array, "num_timesteps_minus_1 state_dim state_dim"]


def block_tridiag_mvn_filter(
J_diag: Float[Array, "num_timesteps state_dim state_dim"],
J_lower: Float[Array, "num_timesteps_minus_1 state_dim state_dim"],
h: Float[Array, "num_timesteps state_dim"],
) -> Tuple[Scalar, Scalar,
Float[Array, "num_timesteps state_dim state_dim"],
Float[Array, "num_timesteps state_dim"]]:
"""Run the Kalman filter in information form on a Gaussian with block-tridiagonal precision.

Args:
J_diag: diagonal blocks of the precision matrix, shape (T, D, D)
J_lower: blocks just below the diagonal of the precision matrix, shape (T-1, D, D)
h: linear potentials, shape (T, D)

Returns:
log_normalizer: log normalizing constant of the Gaussian
log_det_precision: log determinant of the full precision matrix
filtered_precisions: precisions of the filtering distributions, shape (T, D, D)
filtered_linear_potentials: linear potentials of the filtering distributions, shape (T, D)
"""
num_timesteps, dim = h.shape
dtype = jnp.result_type(J_diag, J_lower, h)
J_diag, J_lower, h = J_diag.astype(dtype), J_lower.astype(dtype), h.astype(dtype)
J_lower_padded = jnp.concatenate([J_lower, jnp.zeros((1, dim, dim), dtype=dtype)], axis=0)

def _step(carry, t):
"""Condition on block t, then marginalize x_t out of the pair (x_t, x_{t+1})."""
J_pred, h_pred, log_normalizer, log_det = carry
J_cond = J_diag[t] + J_pred
h_cond = h[t] + h_pred
chol = jnp.linalg.cholesky(J_cond)
log_diag = jnp.log(jnp.diag(chol))
whitened_h = solve_triangular(chol, h_cond, lower=True) # chol^{-1} h_cond
whitened_L = solve_triangular(chol, J_lower_padded[t].T, lower=True) # chol^{-1} L_t^T
log_normalizer += 0.5 * dim * jnp.log(2 * jnp.pi) - log_diag.sum() + 0.5 * whitened_h @ whitened_h
log_det += 2 * log_diag.sum()
J_pred_next = -whitened_L.T @ whitened_L
h_pred_next = -whitened_L.T @ whitened_h
return (J_pred_next, h_pred_next, log_normalizer, log_det), (symmetrize(J_cond), h_cond)

carry = (jnp.zeros((dim, dim), dtype=h.dtype), jnp.zeros(dim, dtype=h.dtype),
jnp.zeros((), dtype=h.dtype), jnp.zeros((), dtype=h.dtype))
(_, _, log_normalizer, log_det), (filtered_precisions, filtered_linear_potentials) = \
lax.scan(_step, carry, jnp.arange(num_timesteps))
return log_normalizer, log_det, filtered_precisions, filtered_linear_potentials


def _backward_pass(filtered_precisions, filtered_linear_potentials, J_lower):
"""Backward pass over the filtered potentials, returning the means, covariances, and cross-covariances."""
dim = filtered_linear_potentials.shape[-1]
eye = jnp.eye(dim, dtype=filtered_linear_potentials.dtype)

def _last(J_cond, h_cond):
"""Posterior of the last state from its filtered potential."""
chol = jnp.linalg.cholesky(J_cond)
mean = cho_solve((chol, True), h_cond)
cov = symmetrize(cho_solve((chol, True), eye))
return mean, cov

def _step(carry, args):
"""Step back one time step.

Given the next state and all the data, the current state is Gaussian with the filtered precision,
and a linear potential equal to the filtered one minus the coupling term from the next state.
"""
mean_next, cov_next = carry
J_cond, h_cond, L = args
chol = jnp.linalg.cholesky(J_cond)
gain = -cho_solve((chol, True), L.T) # -J_cond^{-1} L_t^T
mean = cho_solve((chol, True), h_cond) + gain @ mean_next
cov = symmetrize(cho_solve((chol, True), eye) + gain @ cov_next @ gain.T)
cross = gain @ cov_next + jnp.outer(mean, mean_next) # E[x_t x_{t+1}^T]
return (mean, cov), (mean, cov, cross)

mean_last, cov_last = _last(filtered_precisions[-1], filtered_linear_potentials[-1])
args = (filtered_precisions[:-1], filtered_linear_potentials[:-1], J_lower)
_, (means, covs, crosses) = lax.scan(_step, (mean_last, cov_last), args, reverse=True)
return (jnp.concatenate([means, mean_last[None]]),
jnp.concatenate([covs, cov_last[None]]),
crosses)


def block_tridiag_mvn_smoother(potentials: BlockTridiagPotentials) -> BlockTridiagPosterior:
"""Compute the mean and covariance of each state of a Gaussian with block-tridiagonal precision.

Runs :func:`block_tridiag_mvn_filter` forward in time, then a backward pass.

Args:
potentials: natural parameters of the Gaussian

Returns:
the marginals of each state and of each pair of consecutive states, together with the potentials
and the filtered potentials
"""
J_diag, J_lower, h = potentials
log_normalizer, log_det, filtered_precisions, filtered_linear_potentials = \
block_tridiag_mvn_filter(J_diag, J_lower, h)
means, covs, crosses = _backward_pass(filtered_precisions, filtered_linear_potentials, J_lower)
return BlockTridiagPosterior(J_diag=J_diag, J_lower=J_lower, h=h,
log_normalizer=log_normalizer,
log_det_precision=log_det,
filtered_precisions=filtered_precisions,
filtered_linear_potentials=filtered_linear_potentials,
smoothed_means=means,
smoothed_covariances=covs,
smoothed_cross_covariances=crosses)


def _sample_with_noise(posterior: BlockTridiagPosterior,
noise: Float[Array, "num_timesteps state_dim"]) -> Float[Array, "num_timesteps state_dim"]:
"""Draw a sample by backward sampling, using given standard normal noise of shape (T, D) instead of a key."""
filtered_precisions = posterior.filtered_precisions
filtered_linear_potentials = posterior.filtered_linear_potentials

def _draw(J_cond, h_cond, eps):
"""Turn standard normal noise into a draw from the Gaussian with precision J_cond and potential h_cond."""
chol = jnp.linalg.cholesky(J_cond)
return cho_solve((chol, True), h_cond) + solve_triangular(chol.T, eps, lower=False)

x_last = _draw(filtered_precisions[-1], filtered_linear_potentials[-1], noise[-1])

def _step(x_next, args):
"""Sample the state at one time step given the next state and all the data."""
J_cond, h_cond, L, eps = args
x = _draw(J_cond, h_cond - L.T @ x_next, eps)
return x, x

_, xs = lax.scan(_step, x_last,
(filtered_precisions[:-1], filtered_linear_potentials[:-1], posterior.J_lower, noise[:-1]),
reverse=True)
return jnp.concatenate([xs, x_last[None]])


def block_tridiag_mvn_sample(key: PRNGKeyT,
posterior: BlockTridiagPosterior) -> Float[Array, "num_timesteps state_dim"]:
"""Draw one sample of the sequence of states from a Gaussian with block-tridiagonal precision.

To draw several samples, ``vmap`` over keys.

Args:
key: random number generator
posterior: the Gaussian, as returned by :func:`block_tridiag_mvn_smoother`

Returns:
a sample, shape (T, D)
"""
noise = jr.normal(key, posterior.h.shape, dtype=posterior.h.dtype)
return _sample_with_noise(posterior, noise)


def block_tridiag_mvn_solve(
J_diag: Float[Array, "num_timesteps state_dim state_dim"],
J_lower: Float[Array, "num_timesteps_minus_1 state_dim state_dim"],
v: Float[Array, "num_timesteps state_dim"],
) -> Float[Array, "num_timesteps state_dim"]:
"""Solve a linear system whose matrix is block tridiagonal and symmetric positive definite.

The solution is the mean of the Gaussian with this precision matrix and linear potential ``v``.

Args:
J_diag: diagonal blocks of the matrix, shape (T, D, D)
J_lower: blocks just below the diagonal of the matrix, shape (T-1, D, D)
v: right-hand side, shape (T, D)

Returns:
the solution, shape (T, D)
"""
return block_tridiag_mvn_smoother(BlockTridiagPotentials(J_diag, J_lower, v)).smoothed_means


def block_tridiag_mvn_matvec(
J_diag: Float[Array, "num_timesteps state_dim state_dim"],
J_lower: Float[Array, "num_timesteps_minus_1 state_dim state_dim"],
v: Float[Array, "num_timesteps state_dim"],
) -> Float[Array, "num_timesteps state_dim"]:
"""Multiply a symmetric block-tridiagonal matrix by a vector.

The blocks above the diagonal are the transposes of the blocks below it.

Args:
J_diag: diagonal blocks of the matrix, shape (T, D, D)
J_lower: blocks just below the diagonal of the matrix, shape (T-1, D, D)
v: vector, shape (T, D)

Returns:
the product, shape (T, D)
"""
out = jnp.einsum("tij,tj->ti", J_diag, v)
out = out.at[:-1].add(jnp.einsum("tji,tj->ti", J_lower, v[1:])) # L_t^T v_{t+1}
out = out.at[1:].add(jnp.einsum("tij,tj->ti", J_lower, v[:-1])) # L_{t-1} v_{t-1}
return out


def block_tridiag_mvn_entropy(log_det_precision: Scalar, num_timesteps: int, state_dim: int) -> Scalar:
"""Compute the entropy of a Gaussian over a sequence of states from the log determinant of its precision.

Args:
log_det_precision: log determinant of the full precision matrix, as returned by
:func:`block_tridiag_mvn_filter` or :func:`block_tridiag_mvn_smoother`
num_timesteps: number of time steps T
state_dim: dimension D of each state

Returns:
the entropy
"""
return 0.5 * num_timesteps * state_dim * (1.0 + jnp.log(2 * jnp.pi)) - 0.5 * log_det_precision
Loading
Loading