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_statesstates) draws the global state weights and the transition distributions; self-transitions are removed, durations being explicit.alphaandgammacontrol 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_itersweeps, as in the authors’pyhsmm; the segmentation returned is the last sample.
n_max_states and \(\tilde{n} = \min(n, 2000)\) (max_len);
with trunc=None, every segment duration is consideredParameters#
Name |
Type |
Default |
Description |
|---|---|---|---|
|
float |
|
Concentration of the transition distributions around the global weights. |
|
float |
|
Concentration of the global state weights. |
|
float |
|
Concentration of the initial state distribution. |
|
int |
|
Number of Gibbs sweeps (quality is flat from the second sweep). |
|
int |
|
Weak-limit truncation of the number of states. |
|
int / None |
|
Maximum segment duration in samples; |
|
float |
|
NIW pseudo-count on the mean. |
|
float / None |
|
NIW degrees of freedom (default: |
|
float / array |
|
NIW prior mean. |
|
float / array |
|
Prior expected covariance when |
|
float |
|
Pseudo-observations behind the prior covariance. |
|
str |
|
Duration distribution, |
|
float |
|
Strength of the duration prior (Gamma shape for the Poisson rate). |
|
float / None |
|
Gamma rate of the Poisson prior; |
|
float |
|
Negative binomial: fixed number of failures. |
|
int / None |
|
Longer series are reduced by block means before the fit. |
|
str |
|
Block features when reducing: |
|
bool |
|
z-normalise each channel before the fit. |
|
str |
|
Chain start: |
|
int |
|
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:
BaseSegmenterBayesian 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.Nonemeans no truncation.kappa0 (
float) – NIW pseudo-count on the mean.nu0 (
float|None) – NIW degrees of freedom. Defaults tod + 1 + emission_strength.prior_mean (
Any) – NIW prior mean.prior_scale (
Any) – Prior expected covarianceE[Sigma](times the identity if scalar) whennu0is None; the inverse-Wishart scale matrix whennu0is given. Withnu0 = d + 2the two readings coincide.emission_strength (
float) – Pseudo-observations behindE[Sigma]whennu0is None: the inverse-Wishart prior hasnu0 = d + 1 + emission_strengthdegrees of freedom and scaleemission_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 bydur_beta. Negative binomial: strength of the Beta prior onp.dur_beta (
float|None) – Poisson: Gamma rate of the prior on the rate, in samples of the input series.Nonesets 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 onphas the same mean duration as the Poisson prior.max_len (
int|None) – Series longer than this are reduced by block means overceil(T / max_len)samples before the fit, and the labels are expanded back.truncanddur_betaare converted to blocks.Nonedisables the reduction; a sweep then costs O(T^2 K) whentruncis 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, aspyhsmm, 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:
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
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.
- set_predict_request(*, axis='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
predictmethod.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(seesklearn.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 topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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.