State Space Model (Base class)#
- class SSM[source]#
A base class for state space models. Such models consist of parameters, which we may learn, as well as hyperparameters, which specify static properties of the model. This base class allows parameters to be indicated a standardized way so that they can easily be converted to/from unconstrained form for optimization.
Abstract Methods
Models that inherit from SSM must implement a few key functions and properties:
initial_distribution()
returns the distribution over the initial state given parameterstransition_distribution()
returns the conditional distribution over the next state given the current state and parametersemission_distribution()
returns the conditional distribution over the emission given the current state and parameterslog_prior()
(optional) returns the log prior probability of the parametersemission_shape
returns a tuple specification of the emission shapeinputs_shape
returns a tuple specification of the input shape, or None if there are no inputs.
The shape properties are required for properly handling batches of data.
Sampling and Computing Log Probabilities
Once these have been implemented, subclasses will inherit the ability to sample and compute log joint probabilities from the base class functions:
sample()
draws samples of the states and emissions for given parameterslog_prob()
computes the log joint probability of the states and emissions for given parameters
Inference
Many subclasses of SSMs expose basic functions for performing state inference.
marginal_log_prob()
computes the marginal log probability of the emissions, summing over latent statesfilter()
computes the filtered posteriorssmoother()
computes the smoothed posteriors
Learning
Likewise, many SSMs will support learning with expectation-maximization (EM) or stochastic gradient descent (SGD).
For expectation-maximization, subclasses must implement the E- and M-steps.
e_step()
computes the expected sufficient statistics for a sequence of emissions, given parametersm_step()
finds new parameters that maximize the expected log joint probability
Once these are implemented, the generic SSM class allows to fit the model with EM
fit_em()
run EM to find parameters that maximize the likelihood (or posterior) probability.
For SGD, any subclass that implements
marginal_log_prob()
inherits the base class fitting functionfit_sgd()
run SGD to minimize the negative marginal log probability.
- abstract initial_distribution(params, inputs)[source]#
Return an initial distribution over latent states.
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
inputs (Float[Array, 'input_dim'] | None) – optional inputs \(u_t\)
- Returns:
distribution over initial latent state, \(p(z_1 \mid \theta)\).
- Return type:
Distribution
- abstract transition_distribution(params, state, inputs)[source]#
Return a distribution over next latent state given current state.
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of next latent state \(p(z_{t+1} \mid z_t, u_t, \theta)\).
- Return type:
Distribution
- abstract emission_distribution(params, state, inputs=None)[source]#
Return a distribution over emissions given current state.
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of current emission \(p(y_t \mid z_t, u_t, \theta)\)
- Return type:
Distribution
- log_prior(params)[source]#
Return the log prior probability of any model parameters.
- Returns:
log prior probability.
- Return type:
lp (Scalar)
- Parameters:
params (ParameterSet)
- abstract property emission_shape: Tuple[int]#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s emissions.
For example, a GaussianHMM with \(D\) dimensional emissions would return (D,).
- property inputs_shape: Tuple[int] | None#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s inputs.
- sample(params, key, num_timesteps, inputs=None)[source]#
Sample states \(z_{1:T}\) and emissions \(y_{1:T}\) given parameters \(\theta\) and (optionally) inputs \(u_{1:T}\).
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
key (Array) – random number generator
num_timesteps (int) – number of timesteps \(T\)
inputs (Float[Array, 'num_timesteps input_dim'] | None) – inputs \(u_{1:T}\)
- Returns:
latent states and emissions
- Return type:
Tuple[Float[Array, ‘num_timesteps state_dim’], Float[Array, ‘num_timesteps emission_dim’]]
- log_prob(params, states, emissions, inputs=None)[source]#
Compute the log joint probability of the states and observations
- Parameters:
params (ParameterSet)
states (Float[Array, 'num_timesteps state_dim'])
emissions (Float[Array, 'num_timesteps emission_dim'])
inputs (Float[Array, 'num_timesteps input_dim'] | None)
- Return type:
float | Float[Array, ‘’]
- marginal_log_prob(params, emissions, inputs=None)[source]#
Compute log marginal likelihood of observations, \(\log \sum_{z_{1:T}} p(y_{1:T}, z_{1:T} \mid \theta)\).
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
state – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
emissions (Float[Array, 'ntime emission_dim'])
- Returns:
marginal log probability
- Return type:
float | Float[Array, ‘’]
- filter(params, emissions, inputs=None)[source]#
Compute filtering distributions, \(p(z_t \mid y_{1:t}, u_{1:t}, \theta)\) for \(t=1,\ldots,T\).
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
state – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
emissions (Float[Array, 'ntime emission_dim'])
- Returns:
filtering distributions
- Return type:
Posterior
- smoother(params, emissions, inputs=None)[source]#
Compute smoothing distribution, \(p(z_t \mid y_{1:T}, u_{1:T}, \theta)\) for \(t=1,\ldots,T\).
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
state – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
emissions (Float[Array, 'ntime emission_dim'])
- Returns:
smoothing distributions
- Return type:
Posterior
- e_step(params, emissions, inputs=None)[source]#
Perform an E-step to compute expected sufficient statistics under the posterior, \(p(z_{1:T} \mid y_{1:T}, u_{1:T}, \theta)\).
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
emissions (Float[Array, 'num_timesteps emission_dim']) – emissions \(y_{1:T}\)
inputs (Float[Array, 'num_timesteps input_dim'] | None) – optional inputs \(u_{1:T}\)
- Returns:
Expected sufficient statistics under the posterior.
- Return type:
- m_step(params, props, batch_stats, m_step_state)[source]#
Perform an M-step to find parameters that maximize the expected log joint probability.
Specifically, compute
\[\theta^\star = \mathrm{argmax}_\theta \; \mathbb{E}_{p(z_{1:T} \mid y_{1:T}, u_{1:T}, \theta)} \big[\log p(y_{1:T}, z_{1:T}, \theta \mid u_{1:T}) \big]\]- Parameters:
params (ParameterSet) – model parameters \(\theta\)
props (PropertySet) – properties specifying which parameters should be learned
batch_stats (SuffStatsSSM) – sufficient statistics from each sequence
m_step_state (Any) – any required state for optimizing the model parameters.
- Returns:
new parameters
- Return type:
- fit_em(params, props, emissions, inputs=None, num_iters=50, verbose=True)[source]#
Compute parameter MLE/ MAP estimate using Expectation-Maximization (EM).
EM aims to find parameters that maximize the marginal log probability,
\[\theta^\star = \mathrm{argmax}_\theta \; \log p(y_{1:T}, \theta \mid u_{1:T})\]It does so by iteratively forming a lower bound (the “E-step”) and then maximizing it (the “M-step”).
Note:
emissions
andinputs
can either be single sequences or batches of sequences.- Parameters:
params (ParameterSet) – model parameters \(\theta\)
props (PropertySet) – properties specifying which parameters should be learned
emissions (Float[Array, 'num_timesteps emission_dim'] | Float[Array, 'num_batches num_timesteps emission_dim']) – one or more sequences of emissions
inputs (Float[Array, 'num_timesteps input_dim'] | Float[Array, 'num_batches num_timesteps input_dim'] | None) – one or more sequences of corresponding inputs
num_iters (int) – number of iterations of EM to run
verbose (bool) – whether or not to show a progress bar
- Returns:
tuple of new parameters and log likelihoods over the course of EM iterations.
- Return type:
Tuple[ParameterSet, Float[Array, ‘num_iters’]]
- fit_sgd(params, props, emissions, inputs=None, optimizer=(<function chain.<locals>.init_fn>, <function chain.<locals>.update_fn>), batch_size=1, num_epochs=50, shuffle=False, key=Array([0, 0], dtype=uint32))[source]#
Compute parameter MLE/ MAP estimate using Stochastic Gradient Descent (SGD).
SGD aims to find parameters that maximize the marginal log probability,
\[\theta^\star = \mathrm{argmax}_\theta \; \log p(y_{1:T}, \theta \mid u_{1:T})\]by minimizing the _negative_ of that quantity.
Note:
emissions
andinputs
can either be single sequences or batches of sequences.On each iteration, the algorithm grabs a minibatch of sequences and takes a gradient step. One pass through the entire set of sequences is called an epoch.
- Parameters:
params (ParameterSet) – model parameters \(\theta\)
props (PropertySet) – properties specifying which parameters should be learned
emissions (Float[Array, 'num_timesteps emission_dim'] | Float[Array, 'num_batches num_timesteps emission_dim']) – one or more sequences of emissions
inputs (Float[Array, 'num_timesteps input_dim'] | Float[Array, 'num_batches num_timesteps input_dim'] | None) – one or more sequences of corresponding inputs
optimizer (GradientTransformation) – an optax optimizer for minimization
batch_size (int) – number of sequences per minibatch
num_epochs (int) – number of epochs of SGD to run
key (Array) – a random number generator for selecting minibatches
verbose – whether or not to show a progress bar
shuffle (bool)
- Returns:
tuple of new parameters and losses (negative scaled marginal log probs) over the course of SGD iterations.
- Return type:
Tuple[ParameterSet, Float[Array, ‘niter’]]
Parameters#
Parameters and their associated properties are stored as jax.DeviceArray
and dynamax.parameters.ParameterProperties
, respectively. They are bundled together into a
dynamax.parameters.ParameterSet
and a dynamax.parameters.PropertySet
, which are simply
aliases for immutable datastructures (in our case, NamedTuple
).
- class ParameterSet(*args, **kwargs)[source]#
A
NamedTuple
with parameters stored asjax.DeviceArray
in the leaf nodes.
- class PropertySet(*args, **kwargs)[source]#
A matching
NamedTuple
withParameterProperties
stored in the leaf nodes.
- class ParameterProperties(trainable=True, constrainer=None)[source]#
A PyTree containing parameter metadata (properties).
Note: the properties are stored in the aux_data of this PyTree so that changes will trigger recompilation of functions that rely on them.
- Parameters:
trainable (bool) – flag specifying whether or not to fit this parameter is adjustable.
constrainer (Optional tfb.Bijector) – bijector mapping to constrained form.
Linear Gaussian SSM#
High-level class#
- class LinearGaussianSSM(state_dim, emission_dim, input_dim=0, has_dynamics_bias=True, has_emissions_bias=True)[source]#
Linear Gaussian State Space Model.
The model is defined as follows
\[p(z_1) = \mathcal{N}(z_1 \mid m, S)\]\[p(z_t \mid z_{t-1}, u_t) = \mathcal{N}(z_t \mid F_t z_{t-1} + B_t u_t + b_t, Q_t)\]\[p(y_t \mid z_t) = \mathcal{N}(y_t \mid H_t z_t + D_t u_t + d_t, R_t)\]where
\(z_t\) is a latent state of size state_dim,
\(y_t\) is an emission of size emission_dim
\(u_t\) is an input of size input_dim (defaults to 0)
\(F\) = dynamics (transition) matrix
\(B\) = optional input-to-state weight matrix
\(b\) = optional input-to-state bias vector
\(Q\) = covariance matrix of dynamics (system) noise
\(H\) = emission (observation) matrix
\(D\) = optional input-to-emission weight matrix
\(d\) = optional input-to-emission bias vector
\(R\) = covariance function for emission (observation) noise
\(m\) = mean of initial state
\(S\) = covariance matrix of initial state
The parameters of the model are stored in a
ParamsLGSSM
. You can create the parameters manually, or by callinginitialize()
.- Parameters:
state_dim (int) – Dimensionality of latent state.
emission_dim (int) – Dimensionality of observation vector.
input_dim (int) – Dimensionality of input vector. Defaults to 0.
has_dynamics_bias (bool) – Whether model contains an offset term \(b\). Defaults to True.
has_emissions_bias (bool) – Whether model contains an offset term \(d\). Defaults to True.
- property emission_shape#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s emissions.
For example, a GaussianHMM with \(D\) dimensional emissions would return (D,).
- property inputs_shape#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s inputs.
- initialize(key=Array([0, 0], dtype=uint32), initial_mean=None, initial_covariance=None, dynamics_weights=None, dynamics_bias=None, dynamics_input_weights=None, dynamics_covariance=None, emission_weights=None, emission_bias=None, emission_input_weights=None, emission_covariance=None)[source]#
Initialize model parameters that are set to None, and their corresponding properties.
- Parameters:
key (Array) – Random number key. Defaults to jr.PRNGKey(0).
initial_mean (Float[Array, 'state_dim'] | None) – parameter \(m\). Defaults to None.
initial_covariance – parameter \(S\). Defaults to None.
dynamics_weights – parameter \(F\). Defaults to None.
dynamics_bias – parameter \(b\). Defaults to None.
dynamics_input_weights – parameter \(B\). Defaults to None.
dynamics_covariance – parameter \(Q\). Defaults to None.
emission_weights – parameter \(H\). Defaults to None.
emission_bias – parameter \(d\). Defaults to None.
emission_input_weights – parameter \(D\). Defaults to None.
emission_covariance – parameter \(R\). Defaults to None.
- Returns:
parameters and their properties.
- Return type:
Tuple[ParamsLGSSM, ParamsLGSSM]
- initial_distribution(params, inputs=None)[source]#
Return an initial distribution over latent states.
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
inputs (Float[Array, 'ntime input_dim'] | None) – optional inputs \(u_t\)
- Returns:
distribution over initial latent state, \(p(z_1 \mid \theta)\).
- Return type:
Distribution
- transition_distribution(params, state, inputs=None)[source]#
Return a distribution over next latent state given current state.
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of next latent state \(p(z_{t+1} \mid z_t, u_t, \theta)\).
- Return type:
Distribution
- emission_distribution(params, state, inputs=None)[source]#
Return a distribution over emissions given current state.
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of current emission \(p(y_t \mid z_t, u_t, \theta)\)
- Return type:
Distribution
- sample(params, key, num_timesteps, inputs=None)[source]#
Sample states \(z_{1:T}\) and emissions \(y_{1:T}\) given parameters \(\theta\) and (optionally) inputs \(u_{1:T}\).
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
key (Array) – random number generator
num_timesteps (int) – number of timesteps \(T\)
inputs (Float[Array, 'ntime input_dim'] | None) – inputs \(u_{1:T}\)
- Returns:
latent states and emissions
- Return type:
- marginal_log_prob(params, emissions, inputs=None)[source]#
Compute log marginal likelihood of observations, \(\log \sum_{z_{1:T}} p(y_{1:T}, z_{1:T} \mid \theta)\).
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
state – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
emissions (Float[Array, 'ntime emission_dim'])
- Returns:
marginal log probability
- Return type:
float | Float[Array, ‘’]
- filter(params, emissions, inputs=None)[source]#
Compute filtering distributions, \(p(z_t \mid y_{1:t}, u_{1:t}, \theta)\) for \(t=1,\ldots,T\).
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
state – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
emissions (Float[Array, 'ntime emission_dim'])
- Returns:
filtering distributions
- Return type:
- smoother(params, emissions, inputs=None)[source]#
Compute smoothing distribution, \(p(z_t \mid y_{1:T}, u_{1:T}, \theta)\) for \(t=1,\ldots,T\).
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
state – current latent state \(z_t\)
inputs (Float[Array, 'ntime input_dim'] | None) – current inputs \(u_t\)
emissions (Float[Array, 'ntime emission_dim'])
- Returns:
smoothing distributions
- Return type:
- posterior_predictive(params, emissions, inputs=None)[source]#
Compute marginal posterior predictive smoothing distribution for each observation.
- Parameters:
params (ParamsLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – sequence of observations.
inputs (Float[Array, 'ntime input_dim'] | None) – optional sequence of inputs.
- Returns:
posterior predictive means \(\mathbb{E}[y_{t,d} \mid y_{1:T}]\) and standard deviations \(\mathrm{std}[y_{t,d} \mid y_{1:T}]\)
- Return type:
Tuple[Float[Array, ‘ntime emission_dim’], Float[Array, ‘ntime emission_dim’]]
- e_step(params, emissions, inputs=None)[source]#
Perform an E-step to compute expected sufficient statistics under the posterior, \(p(z_{1:T} \mid y_{1:T}, u_{1:T}, \theta)\).
- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
emissions (Float[Array, 'num_timesteps emission_dim'] | Float[Array, 'num_batches num_timesteps emission_dim']) – emissions \(y_{1:T}\)
inputs (Float[Array, 'num_timesteps input_dim'] | Float[Array, 'num_batches num_timesteps input_dim'] | None) – optional inputs \(u_{1:T}\)
- Returns:
Expected sufficient statistics under the posterior.
- Return type:
- m_step(params, props, batch_stats, m_step_state)[source]#
Perform an M-step to find parameters that maximize the expected log joint probability.
Specifically, compute
\[\theta^\star = \mathrm{argmax}_\theta \; \mathbb{E}_{p(z_{1:T} \mid y_{1:T}, u_{1:T}, \theta)} \big[\log p(y_{1:T}, z_{1:T}, \theta \mid u_{1:T}) \big]\]- Parameters:
params (ParamsLGSSM) – model parameters \(\theta\)
props (ParamsLGSSM) – properties specifying which parameters should be learned
batch_stats (SuffStatsLGSSM) – sufficient statistics from each sequence
m_step_state (Any) – any required state for optimizing the model parameters.
- Returns:
new parameters
- Return type:
Low-level inference#
- lgssm_filter(params, emissions, inputs=None)[source]#
Run a Kalman filter to produce the marginal likelihood and filtered state estimates.
- Parameters:
params (ParamsLGSSM) – model parameters
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
- Returns:
filtered posterior object
- Return type:
- lgssm_smoother(params, emissions, inputs=None)[source]#
Run forward-filtering, backward-smoother to compute expectations under the posterior distribution on latent states. Technically, this implements the Rauch-Tung-Striebel (RTS) smoother.
- Parameters:
params (ParamsLGSSM) – an LGSSMParams instance (or object with the same fields)
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
inputs (Float[Array, 'ntime input_dim'] | None) – array of inputs.
- Returns:
smoothed posterior object.
- Return type:
- lgssm_posterior_sample(key, params, emissions, inputs=None, jitter=0)[source]#
Run forward-filtering, backward-sampling to draw samples from \(p(z_{1:T} \mid y_{1:T}, u_{1:T})\).
- Parameters:
key (Array) – random number key.
params (ParamsLGSSM) – parameters.
emissions (Float[Array, 'ntime emission_dim']) – sequence of observations.
inputs (Float[Array, 'ntime input_dim'] | None) – optional sequence of inptus.
jitter (float | Float[Array, ''] | None) – padding to add to the diagonal of the covariance matrix before sampling.
- Returns:
one sample of \(z_{1:T}\) from the posterior distribution on latent states.
- Return type:
Float[Array, “ntime state_dim”]
Types#
- class ParamsLGSSM(initial, dynamics, emissions)[source]#
Parameters of a linear Gaussian SSM.
- Parameters:
initial (ParamsLGSSMInitial) – initial distribution parameters
dynamics (ParamsLGSSMDynamics) – dynamics distribution parameters
emissions (ParamsLGSSMEmissions) – emission distribution parameters
- class ParamsLGSSMInitial(mean, cov)[source]#
Parameters of the initial distribution
\[p(z_1) = \mathcal{N}(z_1 \mid \mu_1, Q_1)\]The tuple doubles as a container for the ParameterProperties.
- Parameters:
mean (Float[Array, 'state_dim'] | ParameterProperties) – \(\mu_1\)
cov (Float[Array, 'state_dim state_dim'] | Float[Array, 'state_dim_triu'] | ParameterProperties) – \(Q_1\)
- class ParamsLGSSMDynamics(weights, bias, input_weights, cov)[source]#
Parameters of the emission distribution
\[p(z_{t+1} \mid z_t, u_t) = \mathcal{N}(z_{t+1} \mid F z_t + B u_t + b, Q)\]The tuple doubles as a container for the ParameterProperties.
- Parameters:
weights (ParameterProperties | Float[Array, 'state_dim state_dim'] | Float[Array, 'ntime state_dim state_dim']) – dynamics weights \(F\)
bias (ParameterProperties | Float[Array, 'state_dim'] | Float[Array, 'ntime state_dim']) – dynamics bias \(b\)
input_weights (ParameterProperties | Float[Array, 'state_dim input_dim'] | Float[Array, 'ntime state_dim input_dim']) – dynamics input weights \(B\)
cov (ParameterProperties | Float[Array, 'state_dim state_dim'] | Float[Array, 'ntime state_dim state_dim'] | Float[Array, 'state_dim_triu']) – dynamics covariance \(Q\)
- class ParamsLGSSMEmissions(weights, bias, input_weights, cov)[source]#
Parameters of the emission distribution
\[p(y_t \mid z_t, u_t) = \mathcal{N}(y_t \mid H z_t + D u_t + d, R)\]The tuple doubles as a container for the ParameterProperties.
- Parameters:
weights (ParameterProperties | Float[Array, 'emission_dim state_dim'] | Float[Array, 'ntime emission_dim state_dim']) – emission weights \(H\)
bias (ParameterProperties | Float[Array, 'emission_dim'] | Float[Array, 'ntime emission_dim']) – emission bias \(d\)
input_weights (ParameterProperties | Float[Array, 'emission_dim input_dim'] | Float[Array, 'ntime emission_dim input_dim']) – emission input weights \(D\)
cov (ParameterProperties | Float[Array, 'emission_dim emission_dim'] | Float[Array, 'ntime emission_dim emission_dim'] | Float[Array, 'emission_dim'] | Float[Array, 'ntime emission_dim'] | Float[Array, 'emission_dim_triu']) – emission covariance \(R\)
- class PosteriorGSSMFiltered(marginal_loglik, filtered_means=None, filtered_covariances=None, predicted_means=None, predicted_covariances=None)[source]#
Marginals of the Gaussian filtering posterior.
- Parameters:
marginal_loglik (float | Float[Array, ''] | Float[Array, 'ntime']) – marginal log likelihood, \(p(y_{1:T} \mid u_{1:T})\)
filtered_means (Float[Array, 'ntime state_dim'] | None) – array of filtered means \(\mathbb{E}[z_t \mid y_{1:t}, u_{1:t}]\)
filtered_covariances (Float[Array, 'ntime state_dim state_dim'] | None) – array of filtered covariances \(\mathrm{Cov}[z_t \mid y_{1:t}, u_{1:t}]\)
predicted_means (Float[Array, 'ntime state_dim'] | None)
predicted_covariances (Float[Array, 'ntime state_dim state_dim'] | None)
- class PosteriorGSSMSmoothed(marginal_loglik, filtered_means, filtered_covariances, smoothed_means, smoothed_covariances, smoothed_cross_covariances=None)[source]#
Marginals of the Gaussian filtering and smoothing posterior.
- Parameters:
marginal_loglik (float | Float[Array, '']) – marginal log likelihood, \(p(y_{1:T} \mid u_{1:T})\)
filtered_means (Float[Array, 'ntime state_dim']) – array of filtered means \(\mathbb{E}[z_t \mid y_{1:t}, u_{1:t}]\)
filtered_covariances (Float[Array, 'ntime state_dim state_dim']) – array of filtered covariances \(\mathrm{Cov}[z_t \mid y_{1:t}, u_{1:t}]\)
smoothed_means (Float[Array, 'ntime state_dim']) – array of smoothed means \(\mathbb{E}[z_t \mid y_{1:T}, u_{1:T}]\)
smoothed_covariances (Float[Array, 'ntime state_dim state_dim']) – array of smoothed marginal covariances, \(\mathrm{Cov}[z_t \mid y_{1:T}, u_{1:T}]\)
smoothed_cross_covariances (Float[Array, 'ntime_minus1 state_dim state_dim'] | None) – array of smoothed cross products, \(\mathbb{E}[z_t z_{t+1}^T \mid y_{1:T}, u_{1:T}]\)
Nonlinear Gaussian GSSM#
High-level class#
- class NonlinearGaussianSSM(state_dim, emission_dim, input_dim=0)[source]#
Nonlinear Gaussian State Space Model.
The model is defined as follows
\[p(z_t | z_{t-1}, u_t) = N(z_t | f(z_{t-1}, u_t), Q_t)\]\[p(y_t | z_t) = N(y_t | h(z_t, u_t), R_t)\]\[p(z_1) = N(z_1 | m, S)\]where the model parameters are
\(z_t\) = hidden variables of size state_dim,
\(y_t\) = observed variables of size emission_dim
\(u_t\) = input covariates of size input_dim (defaults to 0).
\(f\) = dynamics (transition) function
\(h\) = emission (observation) function
\(Q\) = covariance matrix of dynamics (system) noise
\(R\) = covariance matrix for emission (observation) noise
\(m\) = mean of initial state
\(S\) = covariance matrix of initial state
These parameters of the model are stored in a separate object of type
ParamsNLGSSM
.- property emission_shape#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s emissions.
For example, a GaussianHMM with \(D\) dimensional emissions would return (D,).
- property inputs_shape#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s inputs.
- initial_distribution(params, inputs=None)[source]#
Return an initial distribution over latent states.
- Parameters:
params (ParamsNLGSSM) – model parameters \(\theta\)
inputs (Float[Array, 'input_dim'] | None) – optional inputs \(u_t\)
- Returns:
distribution over initial latent state, \(p(z_1 \mid \theta)\).
- Return type:
Distribution
- transition_distribution(params, state, inputs=None)[source]#
Return a distribution over next latent state given current state.
- Parameters:
params (ParamsNLGSSM) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of next latent state \(p(z_{t+1} \mid z_t, u_t, \theta)\).
- Return type:
Distribution
- emission_distribution(params, state, inputs=None)[source]#
Return a distribution over emissions given current state.
- Parameters:
params (ParamsNLGSSM) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of current emission \(p(y_t \mid z_t, u_t, \theta)\)
- Return type:
Distribution
Low-level inference#
- extended_kalman_filter(params, emissions, num_iter=1, inputs=None, output_fields=['filtered_means', 'filtered_covariances', 'predicted_means', 'predicted_covariances'])[source]#
Run an (iterated) extended Kalman filter to produce the marginal likelihood and filtered state estimates.
- Parameters:
params (ParamsNLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – observation sequence.
num_iter (int) – number of linearizations around posterior for update step (default 1).
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
output_fields (List[str] | None) – list of fields to return in posterior object. These can take the values “filtered_means”, “filtered_covariances”, “predicted_means”, “predicted_covariances”, and “marginal_loglik”.
- Returns:
posterior object.
- Return type:
post
- iterated_extended_kalman_filter(params, emissions, num_iter=2, inputs=None)[source]#
Run an iterated extended Kalman filter to produce the marginal likelihood and filtered state estimates.
- Parameters:
params (ParamsNLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – observation sequence.
num_iter (int) – number of linearizations around posterior for update step (default 2).
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
- Returns:
posterior object.
- Return type:
post
- extended_kalman_smoother(params, emissions, filtered_posterior=None, inputs=None)[source]#
Run an extended Kalman (RTS) smoother.
- Parameters:
params (ParamsNLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – observation sequence.
filtered_posterior (PosteriorGSSMFiltered | None) – optional output from filtering step.
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
- Returns:
posterior object.
- Return type:
post
- iterated_extended_kalman_smoother(params, emissions, num_iter=2, inputs=None)[source]#
Run an iterated extended Kalman smoother (IEKS).
- Parameters:
params (ParamsNLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – observation sequence.
num_iter (int) – number of linearizations around posterior for update step (default 2).
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
- Returns:
posterior object.
- Return type:
post
- unscented_kalman_filter(params, emissions, hyperparams, inputs=None, output_fields=['filtered_means', 'filtered_covariances', 'predicted_means', 'predicted_covariances'])[source]#
Run a unscented Kalman filter to produce the marginal likelihood and filtered state estimates.
- Parameters:
params (ParamsNLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
hyperparams (UKFHyperParams) – hyper-parameters.
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
- Returns:
posterior object.
- Return type:
filtered_posterior
- unscented_kalman_smoother(params, emissions, hyperparams, inputs=None)[source]#
Run a unscented Kalman (RTS) smoother.
- Parameters:
params (ParamsNLGSSM) – model parameters.
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
hyperperams – hyper-parameters.
inputs (Float[Array, 'ntime input_dim'] | None) – optional inputs.
hyperparams (UKFHyperParams)
- Returns:
posterior object.
- Return type:
nlgssm_posterior
Types#
- class ParamsNLGSSM(initial_mean, initial_covariance, dynamics_function, dynamics_covariance, emission_function, emission_covariance)[source]#
Parameters for a NLGSSM model.
\[p(z_t | z_{t-1}, u_t) = N(z_t | f(z_{t-1}, u_t), Q_t)\]\[p(y_t | z_t) = N(y_t | h(z_t, u_t), R_t)\]\[p(z_1) = N(z_1 | m, S)\]If you have no inputs, the dynamics and emission functions do not to take \(u_t\) as an argument.
- Parameters:
dynamics_function (Callable[[Float[Array, 'state_dim']], Float[Array, 'state_dim']] | Callable[[Float[Array, 'state_dim'], Float[Array, 'input_dim']], Float[Array, 'state_dim']]) – \(f\)
dynamics_covariance (Float[Array, 'state_dim state_dim']) – \(Q\)
emissions_function – \(h\)
emissions_covariance – \(R\)
initial_mean (Float[Array, 'state_dim']) – \(m\)
initial_covariance (Float[Array, 'state_dim state_dim']) – \(S\)
emission_function (Callable[[Float[Array, 'state_dim']], Float[Array, 'emission_dim']] | Callable[[Float[Array, 'state_dim'], Float[Array, 'input_dim']], Float[Array, 'emission_dim']])
emission_covariance (Float[Array, 'emission_dim emission_dim'])
Generalized Gaussian GSSM#
High-level class#
- class GeneralizedGaussianSSM(state_dim, emission_dim, input_dim=0)[source]#
Generalized Gaussian State Space Model.
The model is defined as follows
\[p(z_t | z_{t-1}, u_t) = N(z_t | f(z_{t-1}, u_t), Q_t)\]\[p(y_t | z_t) = q(y_t | h(z_t, u_t), R(z_t, u_t))\]\[p(z_1) = N(z_1 | m, S)\]where the model parameters are
\(z_t\) = hidden variables of size state_dim,
\(y_t\) = observed variables of size emission_dim
\(u_t\) = input covariates of size input_dim (defaults to 0).
\(f\) = dynamics (transition) function
\(h\) = emission (observation) function
\(Q\) = covariance matrix of dynamics (system) noise
\(R\) = covariance function for emission (observation) noise
\(m\) = mean of initial state
\(S\) = covariance matrix of initial state
The parameters of the model are stored in a separate object of type
ParamsGGSSM
.For example usage, see probml/dynamax.
- property emission_shape#
Return a pytree matching the pytree of tuples specifying the shape of a single time step’s emissions.
For example, a GaussianHMM with \(D\) dimensional emissions would return (D,).
- initial_distribution(params, inputs=None)[source]#
Return an initial distribution over latent states.
- Parameters:
params (ParamsGGSSM) – model parameters \(\theta\)
inputs (Float[Array, 'input_dim'] | None) – optional inputs \(u_t\)
- Returns:
distribution over initial latent state, \(p(z_1 \mid \theta)\).
- Return type:
Distribution
- transition_distribution(params, state, inputs=None)[source]#
Return a distribution over next latent state given current state.
- Parameters:
params (ParamsGGSSM) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of next latent state \(p(z_{t+1} \mid z_t, u_t, \theta)\).
- Return type:
Distribution
- emission_distribution(params, state, inputs=None)[source]#
Return a distribution over emissions given current state.
- Parameters:
params (ParamsGGSSM) – model parameters \(\theta\)
state (Float[Array, 'state_dim']) – current latent state \(z_t\)
inputs (Float[Array, 'input_dim'] | None) – current inputs \(u_t\)
- Returns:
conditional distribution of current emission \(p(y_t \mid z_t, u_t, \theta)\)
- Return type:
Distribution
Low-level inference#
- conditional_moments_gaussian_filter(model_params, inf_params, emissions, num_iter=1, inputs=None)[source]#
Run an (iterated) conditional moments Gaussian filter to produce the marginal likelihood and filtered state estimates.
- Parameters:
model_params (ParamsGGSSM) – model parameters.
inf_params (EKFIntegrals | UKFIntegrals | GHKFIntegrals) – inference parameters that specify how to compute moments.
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
num_iter (int) – optional number of linearizations around prior/posterior for update step (default 1).
inputs (Float[Array, 'ntime input_dim'] | None) – optopnal array of inputs.
- Returns:
posterior object.
- Return type:
filtered_posterior
- iterated_conditional_moments_gaussian_filter(model_params, inf_params, emissions, num_iter=2, inputs=None)[source]#
Run an iterated conditional moments Gaussian filter.
- Parameters:
model_params (ParamsGGSSM) – model parameters.
inf_params (EKFIntegrals | UKFIntegrals | GHKFIntegrals) – inference parameters that specify how to compute moments.
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
num_iter (int) – optional number of linearizations around prior/posterior for update step (default 1).
inputs (Float[Array, 'ntime input_dim'] | None) – optional array of inputs.
- Returns:
posterior object.
- Return type:
filtered_posterior
- conditional_moments_gaussian_smoother(model_params, inf_params, emissions, filtered_posterior=None, inputs=None)[source]#
Run a conditional moments Gaussian smoother.
- Parameters:
model_params (ParamsGGSSM) – model parameters.
inf_params (EKFIntegrals | UKFIntegrals | GHKFIntegrals) – inference parameters that specify how to compute moments.
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
num_iter – optional number of linearizations around prior/posterior for update step (default 1).
inputs (Float[Array, 'ntime input_dim'] | None) – optopnal array of inputs.
filtered_posterior (PosteriorGSSMFiltered | None)
- Returns:
posterior object.
- Return type:
post
- iterated_conditional_moments_gaussian_smoother(model_params, inf_params, emissions, num_iter=2, inputs=None)[source]#
Run an iterated conditional moments Gaussian smoother.
- Parameters:
model_params (ParamsGGSSM) – model parameters.
inf_params (EKFIntegrals | UKFIntegrals | GHKFIntegrals) – inference parameters that specify how to compute moments.
emissions (Float[Array, 'ntime emission_dim']) – array of observations.
num_iter (int) – optional number of linearizations around prior/posterior for update step (default 1).
inputs (Float[Array, 'ntime input_dim'] | None) – optopnal array of inputs.
- Returns:
posterior object.
- Return type:
post
Types#
- class ParamsGGSSM(initial_mean, initial_covariance, dynamics_function, dynamics_covariance, emission_mean_function, emission_cov_function, emission_dist=<function ParamsGGSSM.<lambda>>)[source]#
Container for Generalized Gaussian SSM parameters. Specifically, it defines the following model:
\[p(z_t | z_{t-1}, u_t) = N(z_t | f(z_{t-1}, u_t), Q_t)\]\[p(y_t | z_t) = q(y_t | h(z_t, u_t), R(z_t, u_t))\]\[p(z_1) = N(z_1 | m, S)\]This differs from NLGSSM in by allowing a general emission model. If you have no inputs, the dynamics and emission functions do not to take \(u_t\) as an argument.
- Parameters:
initial_mean (Float[Array, 'state_dim']) – \(m\)
initial_covariance (Float[Array, 'state_dim state_dim']) – \(S\)
dynamics_function (Callable[[Float[Array, 'state_dim']], Float[Array, 'state_dim']] | Callable[[Float[Array, 'state_dim'], Float[Array, 'input_dim']], Float[Array, 'state_dim']]) – \(f\). This has the signature \(f: Z * U -> Y\) or \(h: Z -> Y\).
dynamics_covariance (Float[Array, 'state_dim state_dim']) – \(Q\)
emission_mean_function (Callable[[Float[Array, 'state_dim']], Float[Array, 'emission_dim']] | Callable[[Float[Array, 'state_dim'], Float[Array, 'input_dim']], Float[Array, 'emission_dim']]) – \(h\). This has the signature \(h: Z * U -> Z\) or \(h: Z -> Z\).
emission_cov_function (Callable[[Float[Array, 'state_dim']], Float[Array, 'emission_dim emission_dim']] | Callable[[Float[Array, 'state_dim'], Float[Array, 'input_dim']], Float[Array, 'emission_dim emission_dim']]) – \(R\). This has the signature \(R: Z * U -> Z*Z\) or \(R: Z -> Z*Z\).
emission_dist (Callable[[Float[Array, 'state_dim'], Float[Array, 'state_dim state_dim']], Distribution]) – the observation distribution \(q\). This is a callable that takes the predicted mean and covariance of Y, and returns a tfp distribution object: \(q: Z * (Z*Z) -> Dist(Y)\).
Utilities#
- find_permutation(z1, z2)[source]#
Find the permutation of the state labels in sequence
z1
so that they best align with the labels inz2
.- Parameters:
z1 (Int[Array, 'num_timesteps']) – The first state vector.
z2 (Int[Array, 'num_timesteps']) – The second state vector.
- Returns:
permutation such that
jnp.take(perm, z1)
best aligns withz2
. Thus,len(perm) = min(z1.max(), z2.max()) + 1
.