Skip to main content

Discrete Hidden Markov Models with Numba

Project description

Hmmkay Build Status Documentation Status python versions

🤔👌

Discrete Hidden Markov Models with Numba

Hmmkay is a basic library for discrete Hidden Markov Models that relies on numba's just-in-time compilation. It supports decoding, likelihood scoring, fitting (parameter estimation), and sampling.

Hmmkay accepts sequences of arbitrary length, e.g. 2d numpy arrays or lists of iterables. Hmmkay internally converts lists of iterables into Numba typed lists of numpy arrays.

Installation

pip install hmmkay

Requires Python 3.6 or higher.

Examples

Scoring and decoding:

from hmmkay.utils import make_observation_sequences
sequences = make_observation_sequences(n_seq=100, n_observable_states=4, random_state=0)
hmm.fit(sequences)

from hmmkay.utils import make_proba_matrices
from hmmkay import HMM

init_probas, transition_probas, emission_probas = make_proba_matrices(
    n_hidden_states=2,
    n_observable_states=4,
    random_state=0
)
hmm = HMM(init_probas, transition_probas, emission_probas)

sequences = [[0, 1, 2, 3], [0, 2]]
hmm.log_likelihood(sequences)
-8.336
hmm.decode(sequences)  # most likely sequences of hidden states
[array([1, 0, 0, 1], dtype=int32), array([1, 0], dtype=int32)]

Fitting:

from hmmkay.utils import make_observation_sequences
sequences = make_observation_sequences(n_seq=100, n_observable_states=4, random_state=0)
hmm.fit(sequences)

Sampling:

hmm.sample(n_obs=2, n_seq=3)  # return sequences of hidden and observable states
(array([[0, 1],
        [1, 1],
        [0, 0]]), array([[0, 2],
        [2, 3],
        [0, 0]]))

Documentation

Docs are online at https://hmmkay.readthedocs.io/en/latest/

Benchmark

It should be faster than hmmlearn. Here's the result of the benchmark.py script on my laptop:

bench

Status

Highly experimental, API subjet to change without deprecation.

Development

The following packages are required for testing:

pip install pytest hmmlearn scipy

For benchmarks:

pip install matplotlib hmmlearn

For docs:

pip install sphinx sphinx_rtd_theme

For development, use pre-commit hooks for black and flake8.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

hmmkay-0.0.1-py3-none-any.whl (10.6 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page