Back to models
Detector

HMM

Implements a simple HMM fitted with Viterbi algorithm.

Quickstart

python
from sktime.detection.hmm import HMM

estimator = HMM(emission_funcs: list, transition_prob_mat: ndarray, initial_probs: ndarray=None)

Parameters(3)

emission_funcslist, shape = [num hidden states]

List should be of length n (the number of hidden states) Either a list of callables [fx_1, fx_2] with signature fx_1(X) -> float or a list of callables and matched keyword arguments for those callables [(fx_1, kwarg_1), (fx_2, kwarg_2)] with signature fx_1(X, **kwargs) -> float (or a list with some mixture of the two). The callables should take a value and return a probability when passed a single observation. All functions should be properly normalized PDFs over the same space as the observed data.

transition_prob_mat: 2D np.ndarry, shape = [num_states, num_states]
Each row should sum to 1 in order to be properly normalized (ie the j’th column in the i’th row represents the probability of transitioning from state i to state j.)
initial_probs: 1D np.ndarray, shape = [num hidden states], optional

A array of probabilities that the sequence of hidden states starts in each of the hidden states. If passed, should be of length n the number of hidden states and should match the length of both the emission funcs list and the transition_prob_mat. The initial probs should be reflective of prior beliefs. If none is passed will each hidden state will be assigned an equal initial prob.

Examples

>>> from sktime.detection.hmm import HMM
>>> from scipy.stats import norm
>>> from numpy import asarray
>>> # define the emission probs for our HMM model:
>>> centers = [3.5, - 5 ]
>>> sd = [.25 for i in centers ]
>>> emi_funcs = [(norm. pdf, { 'loc': mean,
... 'scale': sd [ind ]}) for ind, mean in enumerate (centers)]
>>> hmm_est = HMM (emi_funcs, asarray ([[0.25, 0.75 ], [0.666, 0.333 ]]))
>>> # generate synthetic data (or of course use your own!)
>>> obs = asarray ([3.7, 3.2, 3.4, 3.6, - 5.1, - 5.2, - 4.9 ])
>>> hmm_est = hmm_est. fit (obs)
>>> labels = hmm_est. predict (obs)