Skip to content

HMM

HMM

class HMM(emission_funcs: list, transition_prob_mat: ndarray, initial_probs: ndarray = None)[source]

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.

Parameters:
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.

Attributes:
emission_funcslist, shape = [num_hidden_states]

The functions to use in calculating the emission probabilities. Taken from the __init__ param of same name.

transition_prob_mat: 2D np.ndarry, shape = [num_states, num_states]

Matrix of transition probabilities from hidden state to hidden state. Taken from the __init__ param of same name.

initial_probs1D np.ndarray, shape = [num_hidden_states]

Probability over the hidden state identity of the first state. If the __init__ param of same name was passed it will take on that value. Otherwise it is set to be uniform over all hidden states.

num_statesint

The number of hidden states. Set to be the length of the emission_funcs parameter which was passed.

stateslist

A list of integers from 0 to num_states-1. Integer labels for the hidden states.

num_obsint

The length of the observations data. Extracted from data.

trans_prob2D np.ndarray, shape = [num_observations, num_hidden_states]

Shape [num observations, num hidden states]. The max probability that that observation is assigned to that hidden state. Calculated in _calculate_trans_mat and assigned in _predict.

trans_id2D np.ndarray, shape = [num_observations, num_hidden_states]

Shape [num observations, num hidden states]. The state id of the state proceeding the observation is assigned to that hidden state in the most likely path where that occurs. Calculated in _calculate_trans_mat and assigned in _predict.

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)

Methods

change_points_to_segments(y_sparse[, start, end])

Convert an series of change point indexes to segments.

check_is_fitted([method_name])

Check if the estimator has been fitted.

clone()

Obtain a clone of the object with same hyper-parameters and config.

clone_tags(estimator[, tag_names])

Clone tags from another object as dynamic override.

create_test_instance([parameter_set])

Construct an instance of the class, using first test parameter set.

create_test_instances_and_names([parameter_set])

Create list of all test instances and a list of names for them.

dense_to_sparse(y_dense)

Convert the dense output from an detector to a sparse format.

fit(X[, y])

Fit to training data.

fit_predict(X[, y])

Fit to data, then predict it.

fit_transform(X[, y])

Fit to data, then transform it.

get_class_tag(tag_name[, tag_value_default])

Get class tag value from class, with tag level inheritance from parents.

get_class_tags()

Get class tags from class, with tag level inheritance from parent classes.

get_config()

Get config flags for self.

get_fitted_params([deep])

Get fitted parameters.

get_param_defaults()

Get object's parameter defaults.

get_param_names([sort])

Get object's parameter names.

get_params([deep])

Get a dict of parameters values for this object.

get_tag(tag_name[, tag_value_default, ...])

Get tag value from instance, with tag level inheritance and overrides.

get_tags()

Get tags from instance, with tag level inheritance and overrides.

get_test_params([parameter_set])

Return testing parameter settings for the estimator.

is_composite()

Check if the object is composed of other BaseObjects.

load_from_path(serial)

Load object from file location.

load_from_serial(serial)

Load object from serialized memory container.

predict(X)

Create labels on test/deployment data.

predict_points(X)

Predict changepoints/anomalies on test/deployment data.

predict_scores(X)

Return scores for predicted labels on test/deployment data.

predict_segments(X)

Predict segments on test/deployment data.

reset()

Reset the object to a clean post-init state.

save([path, serialization_format])

Save serialized self to bytes-like object or to (.zip) file.

segments_to_change_points(y_sparse)

Convert segments to change points.

set_config(**config_dict)

Set config flags to given values.

set_params(**params)

Set the parameters of this object.

set_random_state([random_state, deep, ...])

Set random_state pseudo-random seed parameters for self.

set_tags(**tag_dict)

Set instance level tag overrides to given values.

sparse_to_dense(y_sparse, index)

Convert the sparse output from an detector to a dense format.

transform(X)

Create labels on test/deployment data.

transform_scores(X)

Return scores for predicted labels on test/deployment data.

update(X[, y])

Update model with new data and optional ground truth labels.

update_predict(X[, y])

Update model with new data and create labels for it.