HMM
Implements a simple HMM fitted with Viterbi algorithm.
The HMM annotation estimator uses the the Viterbi algorithm to fit a sequence of ‘hidden state’ class annotations (represented by an array of integers the same size as the observation) to a sequence of observations.
This is done by finding the most likely path given the emission probabilities - (ie the probability that a particular observation would be generated by a given hidden state), the transition prob (ie the probability of transitioning from one state to another or staying in the same state) and the initial probabilities - ie the belief of the probability distribution of hidden states at the start of the observation sequence).
- Current assumptions/limitations of this implementation:
the spacing of time series points is assumed to be equivalent.
it only works on univariate data.
- the emission parameters and transition probabilities are
assumed to be known.
- if no initial probs are passed, uniform probabilities are
assigned (ie rather than the stationary distribution.)
requires and returns np.ndarrays.
_fit is currently empty as the parameters of the probability distribution are required to be passed to the algorithm.
_predict - first the transition_probability and transition_id matrices are calculated - these are both nxm matrices, where n is the number of hidden states and m is the number of observations. The transition probability matrices record the probability of the most likely sequence which has observation m being assigned to hidden state n. The transition_id matrix records the step before hidden state n that proceeds it in the most likely path. This logic is mostly carried out by helper function _calculate_trans_mats. Next, these matrices are used to calculate the most likely path (by backtracing from the final mostly likely state and the id’s that proceeded it.) This logic is done via a helper func hmm_viterbi_label.
Schnellstart
from sktime.detection.hmm import HMM
estimator = HMM(emission_funcs: list, transition_prob_mat: ndarray, initial_probs: ndarray=None)Parameter(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
nthe 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.
Beispiele
>>> 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)