HDP-HSMM#

HDP-HSMM – Bayesian non-parametric state detection with Gibbs sampling.

Description#

HDP-HSMM (Hierarchical Dirichlet Process Hidden Semi-Markov Model) is a non-parametric Bayesian model for time series segmentation that does not require the number of states to be specified in advance. It extends the HMM by modelling arbitrary (non-geometric) state durations through explicit duration distributions.

The generative model (Johnson & Willsky, 2013):

  • A Hierarchical Dirichlet Process (weak limit, n_max_states states) draws the global state weights and the transition distributions; self-transitions are removed, durations being explicit. alpha and gamma control state reuse.

  • Each state has Normal-Inverse-Wishart emission parameters, allowing multivariate Gaussian observations with learned mean and covariance.

  • Each state has a shifted Poisson duration distribution with a Gamma prior on its rate (negative binomial optional).

  • Inference is blocked Gibbs sampling over n_iter sweeps, as in the authors’ pyhsmm; the segmentation returned is the last sample.

Type: state detection
Supervision: fully unsupervised
Scope: univariate and multivariate
Complexity: \(O(d\,n)\) for the block means, then \(O(S\,\tilde{n}\,(\tilde{n} + S + d^{2}))\) per Gibbs sweep, where S is n_max_states and \(\tilde{n} = \min(n, 2000)\) (max_len); with trunc=None, every segment duration is considered

Parameters#

Name

Type

Default

Description

alpha

float

6.0

Concentration of the transition distributions around the global weights.

gamma

float

6.0

Concentration of the global state weights.

init_state_concentration

float

6.0

Concentration of the initial state distribution.

n_iter

int

20

Number of Gibbs sweeps (quality is flat from the second sweep).

n_max_states

int

20

Weak-limit truncation of the number of states.

trunc

int / None

None

Maximum segment duration in samples; None: no truncation.

kappa0

float

0.25

NIW pseudo-count on the mean.

nu0

float / None

None

NIW degrees of freedom (default: d + 1 + emission_strength).

prior_mean

float / array

0.0

NIW prior mean.

prior_scale

float / array

1.0

Prior expected covariance when nu0 is None, else the inverse-Wishart scale.

emission_strength

float

100.0

Pseudo-observations behind the prior covariance.

dur_family

str

"poisson"

Duration distribution, "poisson" or "negbin".

dur_alpha

float

5.0

Strength of the duration prior (Gamma shape for the Poisson rate).

dur_beta

float / None

None

Gamma rate of the Poisson prior; None: prior mean duration of T/3 (see below).

dur_r

float

1.0

Negative binomial: fixed number of failures.

max_len

int / None

2000

Longer series are reduced by block means before the fit.

block_features

str

"mean"

Block features when reducing: "mean" or "meanstd".

normalize

bool

True

z-normalise each channel before the fit.

init

str

"states"

Chain start: "states" (drawn from the prior HSMM, as pyhsmm) or "params".

axis

int

0

Time axis.

Usage#

from tsseg.algorithms import HdpHsmmDetector

detector = HdpHsmmDetector()
states = detector.fit_predict(X)

Implementation: NumPy/SciPy Gibbs sampler, forward messages compiled with numba when it is installed (tsseg[accelerators]; otherwise interpreted, about 100 times slower), checked against pyhsmm and against exact enumeration. Origin: new code. Replaces an earlier pyhsmm-backed implementation and a first NumPy implementation (HdpHsmmDetectorV1), both removed.

References: Johnson & Willsky (2013), Bayesian Nonparametric Hidden Semi-Markov Models, JMLR 14, for the model and the sampler implemented here; Johnson & Willsky (2010), The Hierarchical Dirichlet Process Hidden Semi-Markov Model, UAI, the conference paper that introduced the model.

API reference#

HDP-HSMM state detector: the weak-limit Gibbs sampler of Johnson & Willsky (2013).

The model and the sampler follow Johnson & Willsky (2013), which extends Johnson & Willsky (2010). The code was checked against the authors’ pyhsmm and against exact enumeration of the posterior on small problems.

class HdpHsmmDetector(*, alpha=6.0, gamma=6.0, init_state_concentration=6.0, n_iter=20, n_max_states=20, trunc=None, kappa0=0.25, nu0=None, prior_mean=0.0, prior_scale=1.0, emission_strength=100.0, dur_family='poisson', dur_alpha=5.0, dur_beta=None, dur_r=1.0, max_len=2000, block_features='mean', normalize=True, init='states', axis=0)[source]#

Bases: BaseSegmenter

Bayesian non-parametric HDP-HSMM state detector (Gibbs sampling).

Weak-limit HDP-HSMM (Johnson & Willsky, 2013) with Gaussian emissions (Normal-Inverse-Wishart prior) and shifted Poisson or negative-binomial durations. The number of states is inferred, up to n_max_states. The segmentation returned is the last Gibbs sample.

Parameters:
  • axis (int) – Axis along which the time index lies.

  • alpha (float) – Concentration of the transition distributions around the global weights.

  • gamma (float) – Concentration of the global state weights.

  • init_state_concentration (float) – Concentration of the initial state distribution.

  • n_iter (int) – Number of Gibbs sweeps.

  • n_max_states (int) – Weak-limit truncation of the number of states.

  • trunc (int | None) – Maximum segment duration, in samples of the input series. None means no truncation.

  • kappa0 (float) – NIW pseudo-count on the mean.

  • nu0 (float | None) – NIW degrees of freedom. Defaults to d + 1 + emission_strength.

  • prior_mean (Any) – NIW prior mean.

  • prior_scale (Any) – Prior expected covariance E[Sigma] (times the identity if scalar) when nu0 is None; the inverse-Wishart scale matrix when nu0 is given. With nu0 = d + 2 the two readings coincide.

  • emission_strength (float) – Pseudo-observations behind E[Sigma] when nu0 is None: the inverse-Wishart prior has nu0 = d + 1 + emission_strength degrees of freedom and scale emission_strength * prior_scale.

  • dur_family (str) – Duration distribution, shifted to start at 1.

  • dur_alpha (float) – Poisson: Gamma shape of the prior on the rate (strength of the prior); its mean is set by dur_beta. Negative binomial: strength of the Beta prior on p.

  • dur_beta (float | None) – Poisson: Gamma rate of the prior on the rate, in samples of the input series. None sets the prior mean duration to a third of the series length, the most influential setting of the detector.

  • dur_r (float) – Negative binomial: fixed number of failures r; the Beta prior on p has the same mean duration as the Poisson prior.

  • max_len (int | None) – Series longer than this are reduced by block means over ceil(T / max_len) samples before the fit, and the labels are expanded back. trunc and dur_beta are converted to blocks. None disables the reduction; a sweep then costs O(T^2 K) when trunc is None.

  • block_features (str) – Features of a block when the series is reduced: block means, or block means and standard deviations.

  • normalize (bool) – z-normalise each channel (after the reduction) before the fit. The emission prior is expressed in these units.

  • init (str) – Start of the chain: a state sequence drawn from the prior HSMM, as pyhsmm, or parameters drawn from the prior, as the previous tsseg detector.

log_likelihood_#

log p(X | theta) at the last sample, state sequence integrated out, on the series actually modelled (reduced and normalised).

Type:

float

References

Johnson, M. J. and Willsky, A. S. (2013). Bayesian Nonparametric Hidden Semi-Markov Models. Journal of Machine Learning Research, 14, 673-701.

Johnson, M. J. and Willsky, A. S. (2010). The Hierarchical Dirichlet Process Hidden Semi-Markov Model. Conference on Uncertainty in Artificial Intelligence (UAI).

set_fit_request(*, axis='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

axis (Union[bool, None, str]) – Metadata routing for axis parameter in fit.

Returns:

self – The updated object.

Return type:

HdpHsmmDetector

set_predict_request(*, axis='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

axis (Union[bool, None, str]) – Metadata routing for axis parameter in predict.

Returns:

self – The updated object.

Return type:

HdpHsmmDetector