Giter Site home page Giter Site logo

maximtrp / mchmm Goto Github PK

View Code? Open in Web Editor NEW
119.0 119.0 22.0 186 KB

Markov Chains and Hidden Markov Models in Python

Home Page: https://mchmm.readthedocs.io

License: GNU General Public License v3.0

Python 100.00%
baum-welch-algorithm hidden-markov-model hmm hmm-viterbi-algorithm markov-chain markov-model probability python simulation

mchmm's Issues

Attribute Error - 'NoneType' object has no attribute 'ep'

I'd like to run mc.HiddenMarkovModel() from known probabilities however when i try to access arguments I gave the method I experience Attribute Error - 'NoneType' object has no attribute 'states'. I tried to have a list of lists or numpy array as an input for ep, tp, states and obs parameters. Maybe I am not running it in a proper way, the code is below:

importing necessary modules

import numpy as np
import mchmm as mc
import pandas as pd

Definition of matrices and states used for the model

transition_mx = [[0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001],
                 [0.994, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001],
                 [0.001, 0.994, 0.001, 0.001, 0.001, 0.001, 0.001],
                 [0.001, 0.001, 0.597, 0.398, 0.001, 0.001, 0.001],
                 [0.001, 0.001, 0.398, 0.597, 0.001, 0.001, 0.001],
                 [0.001, 0.001, 0.001, 0.001, 0.994, 0.001, 0.001],
                 [0.001, 0.001, 0.001, 0.001, 0.001, 0.994, 0.001]]

base A is the fist column next - T, C, G

emission_mx = [[0.799, 0.199, 0.001, 0.001],
               [0.001, 0.001, 0.799, 0.199],
               [0.799, 0.001, 0.199, 0.001], 
               [0.100, 0.200, 0.400, 0.200],
               [0.997, 0.001, 0.001, 0.001], 
               [0.001, 0.799, 0.001, 0.199],
               [0.001, 0.001, 0.199, 0.799]]

states = [0,1]
obs = ['A', 'T', 'C', 'G']

Creating array from list of lists

emission_mx_a = np.array(emission_mx)
transition_mx_a = np.array(transition_mx)
states_a = np.array(states)                          
obs_a =  np.array(obs)

creating a model from probabilities

a = mc.HiddenMarkovModel().from_prob(transition_mx_a, emission_mx_a, obs_a, states_a)
pd.DataFrame(a.ep, index=a.states, columns=a.observations)

At this step I am getting an Atribute error 'NoneType' object has no attribute 'ep'.

generating a markov simulation starting with a specific sequence

Hi there. Not sure if this is the right place to ask, but here is my problem. I am trying to generate a Markov simulation using a specific sequence as start. Your code is faster that what I have done, but I am not quite sure if I am using correctly, and I am dont know the use of Viterbi and Baum-Welch algorithms in the context of Markov.

To illustrate, I will follow some of the code in your examples.

import mchmm as mc
data = 'AABCABCBAAAACBCBACBABCABCBACBACBABABCBACBBCBBCBCBCBACBABABCBCBAAACABABCBBCBCBCBCBCBAABCBBCBCBCCCBABCBCBBABCBABCABCCABABCB'
a = mc.MarkovChain().from_data(data)

I want a markov simulation based on a 3 states transition matrix, starting with the last 3 characters in the sequence above ("ABC")

start_sequence = data[-3:]
tfm3 = a.n_order_matrix(a.observed_p_matrix, order=3)
ids, states = a.simulate(n=10, tf=tfm3, start=start_sequence)

this returns:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_2552615/2308700615.py in <module>
----> 1 ids, states = a.simulate(n=10, tf=tfm3, start=start_sequence)

~/anaconda3/lib/python3.8/site-packages/mchmm/_mc.py in simulate(self, n, tf, states, start, ret, seed)
    304             _start = np.random.randint(0, len(states))
    305         elif isinstance(start, str):
--> 306             _start = np.argwhere(states == start).item()
    307 
    308         # simulated sequence init

ValueError: can only convert an array of size 1 to a Python scalar

I was expecting to get a sequence of 10 characters, starting with the string 'ABC' (data[-3:]), since I want to constraint the Markov simulation to start with the probabilities implied by that specific sequence, and following a Markov of order 3. Can you help with this? I will post this also in StackOverflow, since maybe an of the users of your code might have had a simular question. Thanks.

Open questions on the usage of mchmm

Hello, I have three questions on the use of the mchmm.

I have previously used the hmmlearn library, and I was wondering if it is possible to

a) set a starting set of initial probabilities for the transition matrix states. For example

start_probs = np.array([0.5, 0.5])

b) Use a different type of hmm (Poisson vs MixtureModel for example)

In hmmlearn you can set a model type (e.g. hmm.MultinomialHMM hmm.Poisson) is it possible to define different model types?

c) Output the probabilities for each predicted state.

In hmm learn you can output the probabilities of the predictions from the Viterbi function. Is there a method to perform this task in mchmm?

Thanks
Jonathan

visualization of Markov chain

Thanks for making mchmm. It is a great package.

As a newcomer to Markov chain, I find it very easy to use. The only question I have is visualization. In the description, there is a very nice image mc.png, showing the transition between three states (A,B,C). I am wondering how to generate such diagram using mchmm's transition matrix and states.

Any pointers will be appreciated!

simulate function seed parameter

Dear Maximtrp,

Thank you for this great offering! I am really finding good use for your module. I have noticed some strange behavior with the seed parameter in your simulate function. When I set the seed= 100 or 1000 or 10**9 or anything at all (a.simulate(500, start='a', seed=100)), I get lots of repetition between states for example: ['a','a','a','a','a','a'.....]

However when the parameter is not included at all (a.simulate(500, start='h')), I get better behavior: ['h' 'l' 'h' 'h' 'l' 'd' 'a' 'e' 'e' 'a' 'e' 'e' 'b' 'g' 'h' 'h' 'h' 'i' 'b' 'h' 'i' 'g' 'l' 'i' 'd'], the states switch to other states and I get something much more useful instead of a string of the same character.

Just letting you know...

Thanks again for some great code :)

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.