Giter VIP home page Giter VIP logo

iohmm's Introduction

IOHMM

A Python package of Input-Output Hidden Markov Model (IOHMM).

Build Status Coverage Status

IOHMM extends standard HMM by allowing (a) initial, (b) transition and (c) emission probabilities to depend on various covariates. A graphical representation of standard HMM and IOHMM:

Standard HMM IOHMM

The solid nodes represent observed information, while the transparent (white) nodes represent latent random variables. The top layer contains the observed input variables ut; the middle layer contains latent categorical variable zt; and the bottom layer contains observed output variables xt. The input for (a) initial, (b) transition and (c) emission probabilities does not have to be the same.

For more theoretical details:

Applications of IOHMM:

Installing

pip install IOHMM

Examples

The example directory contains a set of Jupyter Notebook of examples and demonstrations of:

Features

  • 3-in-1 IOHMM. IOHMM package supports:

    • UnSupervised IOHMM when you have no ground truth hidden states at any timestamp. Expectation-Maximization (EM) algorithm will be used to estimate parameters (maximization step) and posteriors (expectation step).
    • SemiSupervised IOHMM when you have certain amount of ground truth hidden states and would like to enforce these labeled hidden states during learning and use these labels to help direct the learning process.
    • Supervised IOHMM when you want to purely use labeled ground truth hidden states during the learning. There will be no expectation step and only one shot of maximization step during the learning since all the posteriors are from labeled ground truth.
  • Crystal clear structure. Know each step you go:

    • All sequences are represented by pandas dataframes. It has great interface to load csv, json, etc. files or to pull data from sql databases. It is also easy to visualize.
    • Inputs and outputs covariates are specified by the column names (strings) in the dataframes.
    • You can pass a list of sequences (dataframes) as data -- there is no more need to tag the start of each sequence in a single stacked sequence.
    • You can specify different set of inputs for (a) initial, (b) transition and (c) different emission models.
  • Forward Backward algorithm. Faster and more robust:

    • Fully vectorized. Only one 'for loop' (due to dynamic programming) in the forward/backward pass where most current implementations have more than one 'for loop'.
    • All calculations are at log level, this is more robust to long sequence for which the probabilities easily vanish to 0.
  • Json-serialization. Models on the go:

    • Save (to_json) and load (from_json) a trained model in json format. All the attributes are easily visualizable in the json dictionary/file. See Jupyter Notebook of examples for more details.
    • Use a json configuration file to specify the structure of an IOHMM model (from_config). This is useful when you have an application that uses IOHMM models and would like to specify the model before hand.
  • Statsmodels and scikit-learn as the backbone. Take the best of both and better:

    • Unified interface/wrapper to statsmodels and scikit-learn linear models/generalized linear models.
    • Supports fitting the model with sample frequency weights.
    • Supports regularizations in these models.
    • Supports estimation of standard error of the coefficients in certain models.
    • Json-serialization to save (to_json) and load (from_json) of trained linear models.

Credits

Licensing

Modified BSD (3-clause)

iohmm's People

Contributors

edeno avatar mogeng avatar thuener avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

iohmm's Issues

Prediction

How to do prediction from the output distributions?

Emissions Probability Distribution

Hi!

I have adjusted an unsupervised IOHMM with 3 states and an emission covariate (see code below).

SHMM = UnSupervisedIOHMM(num_states=3, max_EM_iter=200, EM_tol=1e-4)

SHMM.set_models(model_emissions = [OLS(est_stderr=True)], model_transition=CrossEntropyMNL(solver='lbfgs'), model_initial=CrossEntropyMNL(solver='lbfgs'))

SHMM.set_inputs(covariates_initial = [], covariates_transition = [], covariates_emissions = [['var']])

SHMM.set_outputs([['out']])

SHMM.set_data([df])

SHMM.train()

I am trying to get the Emissions Probability Distribution in each state based on the emission covariate value. How can I model this probability? I am getting two coefficients and two estimated standard error coefficients of the emission model in each state.

How can I use these coefficients?

Appreciate your answers!

Impose constraints on the transition model

I am very grateful to you and your team for the code that you provided. I am very interested in IOHMM models and want to use my dataset to train a simple UnsupervisedIOHMM. But๏ผŒthere is a problem in using the model.

I was wondering if it is possible to impose constraints on the transition probability when learning with EM. I want to train a specific type of IOHMM where there are no transitions from a higher indexed state to a lower indexed state (also called the Bakis model, left-to-right HMMs). By means of which, if a system goes from any state z๐‘– to another state z๐‘— where ๐‘– <= ๐‘—, then it cannot go back to the previous state. Does the existing IOHMM Library have this option? Or How can I add this constraint to the code?

SemiSupervisedIOHMM couldn't load model

On line:
SHMM_from_json = SemiSupervisedIOHMM.from_json(json_dict)
i get the following error:


ValueError Traceback (most recent call last)
in
----> 1 SHMM_from_json = SemiSupervisedIOHMM.from_json(json_dict)

~/.local/lib/python3.5/site-packages/IOHMM/IOHMM.py in from_json(cls, json_dict)
539 model_initial=getattr(
540 LinearModelLoader, json_dict['properties']['model_initial']['data_type']).from_json(
--> 541 json_dict['properties']['model_initial']),
542 model_transition=[getattr(
543 LinearModelLoader, model_transition_json['data_type']

~/.local/lib/python3.5/site-packages/IOHMM/linear_models.py in from_json(cls, json_dict)
316 l1_ratio=json_dict['properties']['l1_ratio'],
317 coef=np.load(json_dict['properties']['coef']['path']),
--> 318 stderr=np.load(json_dict['properties']['stderr']['path']))
319
320

~/.local/lib/python3.5/site-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
445 else:
446 return format.read_array(fid, allow_pickle=allow_pickle,
--> 447 pickle_kwargs=pickle_kwargs)
448 else:
449 # Try a pickle

~/.local/lib/python3.5/site-packages/numpy/lib/format.py in read_array(fp, allow_pickle, pickle_kwargs)
694 # The array contained Python objects. We need to unpickle the data.
695 if not allow_pickle:
--> 696 raise ValueError("Object arrays cannot be loaded when "
697 "allow_pickle=False")
698 if pickle_kwargs is None:

ValueError: Object arrays cannot be loaded when allow_pickle=False

Train an IOHMM with independent chains

I want to train an IOHMM with different independent sequences and I don't know how to specify them on the only one input panda data frame. Can you explain to me the fields corr and prev briefly and how to use them, please?

What if some sequences has only one record in it?

I was using UnSupervisedIOHMM to learn sequences, but the predict_log_proba in E_step keeps give me ValueError: zero-size array to reduction operation maximum which has no identity. The error arises from X = self._transform_X(X) in predict_log_proba and my self.inp_transitions[seq]=[] due to the length of these sequences are 1. So they don't have inp_transitions. Is there any way I have get rid of this problem?

Multivariate gaussian emissions

Hello,

I have blank inputs and am trying to reconcile the results between IOHMM and hmmlearn.GaussianHMM. Which I assume should be the standard HMM model.

USHMM = UnSupervisedIOHMM(num_states=2, max_EM_iter=200, EM_tol=1e-6)

USHMM.set_models(model_emissions = [OLS()],  
                                    model_transition=CrossEntropyMNL(solver='lbfgs'), 
                                    model_initial=CrossEntropyMNL(solver='lbfgs'))

USHMM.set_inputs(covariates_initial = [], covariates_transition = [], covariates_emissions = [[]])
USHMM.set_outputs([['feature1'],['feature2'],['feature3'],['feature4']] ) #multiple univariate emissions
USHMM.set_data([train_df])

The above gives me almost same results to

import hmmlearn as hmm

model = hmm.GaussianHMM(n_components = 2, covariance_type = "diag")
model.fit(train_df)

This is expected since "diag" implies that all observations are independent and simply individual emissions. The IOHMM model in this case treats it as 4 independent emissions so the results are not too different and are able to reconcile.

However, if I switch to a full multivariate covariate emission

USHMM = UnSupervisedIOHMM(num_states=2, max_EM_iter=200, EM_tol=1e-6)

USHMM.set_models(model_emissions = [OLS()],  
                                    model_transition=CrossEntropyMNL(solver='lbfgs'), 
                                    model_initial=CrossEntropyMNL(solver='lbfgs'))

USHMM.set_inputs(covariates_initial = [], covariates_transition = [], covariates_emissions = [[]])
USHMM.set_outputs([['feature1','feature2','feature3','feature4']])  #multivariate emission
USHMM.set_data([train_df])

The gives me very different results compared to

model = hmm.GaussianHMM(n_components = 2, covariance_type = "full")
model.fit(train_df)

Where "full" means there would be a pxp covariance matrix under each state which is used as emissions to characterise the multivariate gaussian distribution. Is this expected or did I set-up the model incorrectly? Thanks

Best Regards,

Tutorial?

Is there any tutorial or manual which can guide me as a user to be able to implement a function IOHMM and produce outputs? I'm thinking about using this in a bachelor's thesis.

Using CrossEntropyMNL as model_emissions parameter

(UnSupervisedIOHMM)
When setting models as follows:

SHMM.set_models(model_emissions = [CrossEntropyMNL(solver='lbfgs')], model_transition=CrossEntropyMNL(solver='lbfgs'), model_initial=CrossEntropyMNL(solver='lbfgs'))

It throws me back this error:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

It does not happen when I use instead:

SHMM.set_models(model_emissions = [OLS(est_stderr=True)], model_transition=CrossEntropyMNL(solver='lbfgs'), model_initial=CrossEntropyMNL(solver='lbfgs'))

Could you tell me how to face this problem?

Thanks in advanced!

P.D.: entire model adjustment code here:

SHMM = UnSupervisedIOHMM(num_states=3, max_EM_iter=200, EM_tol=1e-4)

SHMM.set_models(model_emissions = [CrossEntropyMNL(solver='lbfgs')], model_transition=CrossEntropyMNL(solver='lbfgs'), model_initial=CrossEntropyMNL(solver='lbfgs'))

SHMM.set_inputs(covariates_initial = [], covariates_transition = ['var'], covariates_emissions = [['var']])

SHMM.set_outputs([['out']])

SHMM.set_data([df])

SHMM.train()

ImportError: cannot import name 'logsumexp'

When using IOHMM with python 3.5.2 I get the following error:

Traceback (most recent call last):
File "", line 1, in
File "/home/tas/.local/lib/python3.5/site-packages/IOHMM/init.py", line 1, in
from .IOHMM import (UnSupervisedIOHMM,
File "/home/tas/.local/lib/python3.5/site-packages/IOHMM/IOHMM.py", line 38, in
from .forward_backward import forward_backward
File "/home/tas/.local/lib/python3.5/site-packages/IOHMM/forward_backward.py", line 21, in
from scipy.misc import logsumexp
ImportError: cannot import name 'logsumexp'

Regarding Transition Probabilities

Hello

I am using a few covariates for transition, but when I am using the sample code provided to view transition probabilities, it is throwing an error.
For Example : covariates_transition = ['a','b','c','d','e', 'f','g','h','i']

When using print(np.exp(SHMM.model_transition[0].predict_log_proba(np.array([[]])))),
I am getting an error : X has 1 features per sample; expecting 10. How can I get the transition probabilities for the two hidden state model with transition covariates.

Also, how can I get the most likely state for each observation? In hmmlearn ,
remodel = hmm.GaussianHMM(n_components=2).fit(X, lengths)
Z3 = remodel.predict(X) provides the hidden states, how can i get that in this model.

Thank you for all the help.

Handling latent observations in IOHMM

Hi,

I need to modify your code to be able to handle missing observations controlled by a binary vector R that controls missed or provided observations by considering probability 1(log probability 0 ) for missed observations such as suggested solution on
yeh2012 (1).pdf

can you guide me on this?
thnx

Constraints

Is it possible to impose constraints on the parameters?

ModuleNotFoundError: No module named 'sklearn.linear_model.base'

When using IOHMM with python 3.9.1 I get the following error:

Traceback (most recent call last):
File "c:\users...\appdata\local\programs\python\python39\lib\site-packages\IOHMM_init_.py", line 1, in
from .IOHMM import (UnSupervisedIOHMM,
File "c:\users...\appdata\local\programs\python\python39\lib\site-packages\IOHMM\IOHMM.py", line 39, in
from .linear_models import (GLM, OLS, DiscreteMNL, CrossEntropyMNL)
File "c:\users...\appdata\local\programs\python\python39\lib\site-packages\IOHMM\linear_models.py", line 56, in
from sklearn.linear_model.base import _rescale_data
ModuleNotFoundError: No module named 'sklearn.linear_model.base'

I managed to get it working by changing sklearn.linear_model.base to sklearn.linear_model._base in IOHMM\linear_models.py

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.