Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    4 of 13 committers (30.8%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.5%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: mattjj
  • License: mit
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 5.01 MB
Statistics
  • Stars: 563
  • Watchers: 55
  • Forks: 177
  • Open Issues: 46
  • Releases: 0
Created over 14 years ago · Last pushed over 1 year ago
Metadata Files
Readme License

README.md

Build
Status

⚠️ Warning: this package is not maintained anymore ⚠️

Bayesian inference in HSMMs and HMMs

This is a Python library for approximate unsupervised inference in Bayesian Hidden Markov Models (HMMs) and explicit-duration Hidden semi-Markov Models (HSMMs), focusing on the Bayesian Nonparametric extensions, the HDP-HMM and HDP-HSMM, mostly with weak-limit approximations.

There are also some extensions:

Installing from PyPI

Give this a shot:

bash pip install pyhsmm

You may need to install a compiler with -std=c++11 support, like gcc-4.7 or higher.

To install manually from the git repo, you'll need cython. Then try this:

bash python setup.py install

It might also help to look at the travis file to see how to set up a working install from scratch. The last Python version this package has been tested with is Python 3.7.

Running

See the examples directory.

For the Python interpreter to be able to import pyhsmm, you'll need it on your Python path. Since the current working directory is usually included in the Python path, you can probably run the examples from the same directory in which you run the git clone with commands like python pyhsmm/examples/hsmm.py. You might also want to add pyhsmm to your global Python path (e.g. by copying it to your site-packages directory).

A Simple Demonstration

Here's how to draw from the HDP-HSMM posterior over HSMMs given a sequence of observations. (The same example, along with the code to generate the synthetic data loaded in this example, can be found in examples/basic.py.)

Let's say we have some 2D data in a data.txt file:

bash $ head -5 data.txt -3.711962552600095444e-02 1.456401745267922598e-01 7.553818775915704942e-02 2.457422192223903679e-01 -2.465977987699214502e+00 5.537627981813508793e-01 -7.031638516485749779e-01 1.536468304146855757e-01 -9.224669847039665971e-01 3.680035337673161489e-01

In Python, we can plot the data in a 2D plot, collapsing out the time dimension:

```python import numpy as np from matplotlib import pyplot as plt

data = np.loadtxt('data.txt') plt.plot(data[:,0],data[:,1],'kx') ```

2D data

We can also make a plot of time versus the first principal component:

python from pyhsmm.util.plot import pca_project_data plt.plot(pca_project_data(data,1))

Data first principal component vs time

To learn an HSMM, we'll use pyhsmm to create a WeakLimitHDPHSMM instance using some reasonable hyperparameters. We'll ask this model to infer the number of states as well, so we'll give it an Nmax parameter:

```python import pyhsmm import pyhsmm.basic.distributions as distributions

obs_dim = 2 Nmax = 25

obshypparams = {'mu0':np.zeros(obsdim), 'sigma0':np.eye(obsdim), 'kappa0':0.3, 'nu0':obsdim+5} durhypparams = {'alpha0':2*30, 'beta_0':2}

obsdistns = [distributions.Gaussian(**obshypparams) for state in range(Nmax)] durdistns = [distributions.PoissonDuration(**durhypparams) for state in range(Nmax)]

posteriormodel = pyhsmm.models.WeakLimitHDPHSMM( alpha=6.,gamma=6., # better to sample over these; see concentration-resampling.py initstateconcentration=6., # pretty inconsequential obsdistns=obsdistns, durdistns=durdistns) ```

(The first two arguments set the "new-table" proportionality constant for the meta-Chinese Restaurant Process and the other CRPs, respectively, in the HDP prior on transition matrices. For this example, they really don't matter at all, but on real data it's much better to infer these parameters, as in examples/concentration_resampling.py.)

Then, we add the data we want to condition on:

python posteriormodel.add_data(data,trunc=60)

The trunc parameter is an optional argument that can speed up inference: it sets a truncation limit on the maximum duration for any state. If you don't pass in the trunc argument, no truncation is used and all possible state duration lengths are considered. (pyhsmm has fancier ways to speed up message passing over durations, but they aren't documented.)

If we had multiple observation sequences to learn from, we could add them to the model just by calling add_data() for each observation sequence.

Now we run a resampling loop. For each iteration of the loop, all the latent variables of the model will be resampled by Gibbs sampling steps, including the transition matrix, the observation means and covariances, the duration parameters, and the hidden state sequence. We'll also copy some samples so that we can plot them.

python models = [] for idx in progprint_xrange(150): posteriormodel.resample_model() if (idx+1) % 10 == 0: models.append(copy.deepcopy(posteriormodel))

Now we can plot our saved samples:

python fig = plt.figure() for idx, model in enumerate(models): plt.clf() model.plot() plt.gcf().suptitle('HDP-HSMM sampled after %d iterations' % (10*(idx+1))) plt.savefig('iter_%.3d.png' % (10*(idx+1)))

Sampled models

I generated these data from an HSMM that looked like this:

Randomly-generated model and data

So the posterior samples look pretty good!

A convenient shortcut to build a list of sampled models is to write

python model_samples = [model.resample_and_copy() for itr in progprint_xrange(150)]

That will build a list of model objects (each of which can be inspected, plotted, pickled, etc, independently) in a way that won't duplicate data that isn't changed (like the observations or hyperparameter arrays) so that memory usage is minimized. It also minimizes file size if you save samples like

python import cPickle with open('sampled_models.pickle','w') as outfile: cPickle.dump(model_samples,outfile,protocol=-1)

Extending the Code

To add your own observation or duration distributions, implement the interfaces defined in basic/abstractions.py. To get a flavor of the style, see pybasicbayes.

References

bibtex @article{johnson2013hdphsmm, title={Bayesian Nonparametric Hidden Semi-Markov Models}, author={Johnson, Matthew J. and Willsky, Alan S.}, journal={Journal of Machine Learning Research}, pages={673--701}, volume={14}, month={February}, year={2013}, }

Authors

Matt Johnson, Alex Wiltschko, Yarden Katz, Chia-ying (Jackie) Lee, Scott Linderman, Kevin Squire, Nick Foti.

Owner

  • Name: Matthew Johnson
  • Login: mattjj
  • Kind: user
  • Location: San Francisco
  • Company: Google

research scientist @ Google Brain

GitHub Events

Total
  • Issues event: 2
  • Watch event: 14
  • Issue comment event: 3
  • Push event: 1
  • Pull request review event: 1
  • Pull request event: 2
  • Fork event: 5
Last Year
  • Issues event: 2
  • Watch event: 14
  • Issue comment event: 3
  • Push event: 1
  • Pull request review event: 1
  • Pull request event: 2
  • Fork event: 5

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 1,301
  • Total Committers: 13
  • Avg Commits per committer: 100.077
  • Development Distribution Score (DDS): 0.048
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Matthew Johnson m****j@c****u 1,238
Alex Wiltschko a****w@g****m 16
Yarden Katz y****z@g****m 15
Scott Linderman s****n@g****m 12
Jacqueline Lee c****g@c****u 6
He-chien Tsai d****1@g****m 3
Scott Linderman s****n@s****u 3
Kevin Squire k****e@g****m 2
Chia-ying c****g@s****) 2
allista a****a@g****m 1
Dan Stowell d****l@u****t 1
Andrea Pierré 6****l 1
Adam Novak a****1@u****u 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 85
  • Total pull requests: 19
  • Average time to close issues: 19 days
  • Average time to close pull requests: 21 days
  • Total issue authors: 62
  • Total pull request authors: 14
  • Average comments per issue: 3.36
  • Average comments per pull request: 1.95
  • Merged pull requests: 9
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 2
  • Average time to close issues: 1 day
  • Average time to close pull requests: 1 day
  • Issue authors: 2
  • Pull request authors: 1
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • mattjj (6)
  • jmgo (5)
  • gxhrid (5)
  • yarden (4)
  • manasa001 (2)
  • mathDR (2)
  • nfoti (2)
  • AnuragDataGeek (2)
  • tansey (2)
  • Krozard (2)
  • alexbw (2)
  • falakmasir (1)
  • FancyBrush (1)
  • dchouren (1)
  • Wusir2018 (1)
Pull Request Authors
  • yarden (3)
  • kir0ul (2)
  • bacalfa (2)
  • ghost (2)
  • tdhopper (1)
  • mmargenot (1)
  • NickHoernle (1)
  • audiracmichelle (1)
  • ameya98 (1)
  • danstowell (1)
  • interfect (1)
  • TrellixVulnTeam (1)
  • slinderman (1)
  • allista (1)
Top Labels
Issue Labels
question (5) bug (2) enhancement (2)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 226 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 10
  • Total versions: 8
  • Total maintainers: 1
pypi.org: pyhsmm

Bayesian inference in HSMMs and HMMs

  • Versions: 8
  • Dependent Packages: 0
  • Dependent Repositories: 10
  • Downloads: 226 Last month
Rankings
Stargazers count: 2.7%
Forks count: 3.8%
Dependent repos count: 4.6%
Average: 8.1%
Dependent packages count: 10.0%
Downloads: 19.5%
Maintainers (1)
Last synced: 10 months ago

Dependencies

setup.py pypi
  • numpy *