osl_dynamics.inference.layers.MarkovChainLayer#

class osl_dynamics.inference.layers.MarkovChainLayer(n_states, *, initial_trans_prob=None, trans_prob_prior=None, initial_state_probs=None, learn_trans_prob=True, learn_initial_state_probs=True, implementation='log', **kwargs)#

Bases: tensorflow.keras.layers.Layer

Hidden Markov chain.

Holds the transition probability matrix and initial state probabilities, and infers the posterior distribution of the states with the Baum-Welch algorithm. The probabilities are updated with an exponential moving average of their posterior estimates (update) rather than with gradients.

Parameters:
n_statesint

Number of states.

initial_trans_probnp.ndarray, optional

Initial transition probability matrix. Defaults to the normalized prior if trans_prob_prior is passed, otherwise to 0.9 on the diagonal with the rest spread evenly.

trans_prob_priornp.ndarray, optional

Dirichlet prior for each row of the transition probability matrix.

initial_state_probsnp.ndarray, optional

Initial state probabilities. Defaults to uniform.

learn_trans_probbool, optional

Should the transition probability matrix be updated?

learn_initial_state_probsbool, optional

Should the initial state probabilities be updated?

implementationstr, optional

Implementation of the forward-backward algorithm. Either 'log' or 'rescale'.

summary_rows()#

Rows for the layer in the summary of a model.

Returns:
rowslist of tuple

(name, details, learned by, number of learned values) for each row.

reset()#

Set the probabilities to their initial values.

set_trans_prob(trans_prob, update_initializer=True)#

Set the transition probability matrix.

Parameters:
trans_probnp.ndarray

Transition probability matrix. Shape must be (n_states, n_states). Rows must sum to one.

update_initializerbool, optional

Should reset set the matrix to this value?

posterior(log_likelihood)#

Posterior distribution of the states given the data.

No gradient is passed back through the posterior.

Parameters:
log_likelihoodtf.Tensor

Log-likelihood of each state. Shape is (batch_size, sequence_length, n_states).

Returns:
gammatf.Tensor

Marginal posterior, \(q(s_t)\), in float64. Shape is (batch_size, sequence_length, n_states).

xitf.Tensor

Joint posterior, \(q(s_t, s_{t+1})\), in float64. Shape is (batch_size, sequence_length - 1, n_states, n_states).

update(gamma, xi, decay, apply)#

Update the probabilities with an exponential moving average.

new = (1 - decay) * old + decay * estimate, where the estimate is the posterior mean of the probabilities given gamma and xi, pooled over all replicas.

Parameters:
gammatf.Tensor

Marginal posterior. Shape is (batch_size, sequence_length, n_states).

xitf.Tensor

Joint posterior. Shape is (batch_size, sequence_length - 1, n_states, n_states).

decaytf.Tensor

Decay of the exponential moving average.

applytf.Tensor

Boolean. The probabilities are only changed if this is True.

entropy(gamma, xi)#

Entropy of the posterior, averaged over sequences.

\[-\sum_{t=1}^{T-1} \int q(s_t, s_{t+1}) \log q(s_t, s_{t+1}) ds_t ds_{t+1} + \sum_{t=2}^{T-1} \int q(s_t) \log q(s_t) ds_t\]

For sequences with one time point, this is \(-\int q(s_1) \log q(s_1) ds_1\).

Parameters:
gammanp.ndarray

Marginal posterior. Shape is (batch_size, sequence_length, n_states).

xinp.ndarray

Joint posterior. Shape is (batch_size, sequence_length - 1, n_states, n_states).

Returns:
entropyfloat
expected_log_prior(gamma, xi)#

Expected log-prior of the states under the posterior, averaged over sequences.

\[\int q(s_1) \log p(s_1) ds_1 + \sum_{t=1}^{T-1} \int q(s_t, s_{t+1}) \log p(s_{t+1} | s_t) ds_t ds_{t+1}\]
Parameters:
gammanp.ndarray

Marginal posterior. Shape is (batch_size, sequence_length, n_states).

xinp.ndarray

Joint posterior. Shape is (batch_size, sequence_length - 1, n_states, n_states).

Returns:
expected_log_priorfloat
log_evidence(log_likelihood)#

Log-evidence, \(\log p(x_{1:T})\), of each sequence.

Calculated with the forward algorithm.

Parameters:
log_likelihoodnp.ndarray

Log-likelihood of each state. Shape is (batch_size, sequence_length, n_states).

Returns:
log_evidencenp.ndarray

Shape is (batch_size,).

viterbi(log_likelihood)#

Most likely state sequence with the Viterbi algorithm.

Parameters:
log_likelihoodnp.ndarray

Log-likelihood of each state. Shape is (batch_size, sequence_length, n_states).

Returns:
pathnp.ndarray

State at each time point. Shape is (batch_size, sequence_length).