Expectation maximisation and inference for hidden Markov models with one or multiple variables.
Project description
hmmBaumWelch
Implements the Baum-Welch expectation maximisation algorithm to update the parameters and perform inference on hidden Markov models (HMM) with observed time-series data (for one or multiple variables). In brief, this package maximises the local probabilty of observing the given sequence of variables by updating the HMM initial state, prior, and transition state probabilities. It then performs inference to give the hidden state sequence as well as rendering the probability of a given hidden state at each time point.
The package is designed to work with SciPy's probability distribution functions to simplify the defining of the Bayesian priors for each variable and state; and a simple form of "early stopping" has been implemented to optimise performance.
Installation
Use the package manager pip to install hmmBaumWelch.
pip install -i https://test.pypi.org/simple/ hmmBaumWelch
Usage
from hmmBaumWelch import BaumWelch
For examples of package implementation, see the examples
directory.
When instantiating the BaumWelch object, several arguments are expected:
- N: the number of hidden states within the system. E.g., if N=2, Zi will be in set {0,1}.
- O_list: a list of list representing each observed, numeric variable. These can be continuous or discreet, but must contain real numbers. They must also be of equal lengths.
- pi: the starting probabilities (i.e. at t=0) for all hidden states. This is a list of length=N.
- A: the transition state matrix: a square numpy array, in which each dimension is equal to the number of states N. Each position represents the probability of transitioning between states, i.e., A(0,0) = P(Zi=0,t=t, Zi=0,t=t+1) - the probability that a hidden state will remain in Zi=0 from one time point to the next. Therefore each row of A must sum to one.
- B_list: a list of prior probability distributions for each variable and all hidden states. These probability distributions can be defined straight from the Scipy package.
An example might be:
HMM = BaumWelch(N=2, O_list=O_list, pi=[0.5, 0.5], A=np.array([[0.8, 0.2], [0.2, 0.8]]), B_list=B_list)
After instantiating the object, expectation maximisation is performed to fit the hidden Markov model. For example:
HMM.baumwelch_expectationMaximisation(iter=50, update_pi=True, update_A=True, update_B=False, early_stopping=True)
The arguments state that there will be a maximum of 50 iterations of expectation maximisation (stopping early if a good fit found); the starting probabilities, pi, and transition state probability matrix, A, will be updated also, but the priors will remain static. The results can then be extracted with the Z_state_probs_inference
method, e.g.:
_, _, Z_inference, gamma_meanR = HMM.Z_state_probs_inference()
Z_inference
is an array of integers, stating the inferred hidden states at each time step; and gamma_meanR is the probability of each hidden state at each time step, given the final HMM parameters. If the optional observables_weights
parameter was entered at instantiation, each variable will have a concomitant weighting in the parameter updates and final inference.
Theory
For a summary of the Baum-Welch algorithm, see the Wikipedia page; and for formal mathematical notation, see the project readme.
Contributors
George Stanley.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hmmbaumwelch-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4a252fb985d8404623933d752cb291ca0a75754150312c0c63018667dab7415 |
|
MD5 | b053576df6a9f0da2328b39fe5679927 |
|
BLAKE2b-256 | d3cd65df8af239bd83a6443c65b28fb2b64afe3946c9ae8724e9bb507e926dc7 |