RajeshDA
RajeshDA

Reputation: 481

Generating Markov transition matrix for continuous data in Python

I am exploring the hidden markov model(HMM) to analyse the sequence of new cases and reproduction rate of covid-19. I have come across a scenarios where I need to generate a transition matrix for the continuous data.

X = [1.324,1.473,1.778,1.626,1.320,1.102,0.905,0.826,0.770,0.770,0.722,0.767,0.707,0.628,0.589,0.558,0.504, ...]

Markovian states (low, medium, high)

low -> 0<=X<=0.5
medium -> 0.5<X<=1.5
high -> 1.5<X<=2.0

How can I generate a Markov transformation matrix for continuous data using python or matlab(preferably python). I think matrix should be 3 be 3,showing the probability of moving from each state to the other 2 states.

I am new to python and finding difficulty to do this. Is there a library that I can use for this purpose. I came across Generating Markov transition matrix in Python which is a similar question but it is for discrete data. I want to do something similar for a continuous data.

Upvotes: 1

Views: 681

Answers (1)

Yuberley Guerrero
Yuberley Guerrero

Reputation: 31

You can implement the following function that calculates the state transitions, receives as a parameter the vector with the numbers, and the little digits that have these numbers, that is: '3826', then they are 4 digits, in order to know how big the output array.

import numpy as np

def TransitionsMatrix( vector, digits ):

    # defined matrix size with zeros
    MatrixResult = np.zeros((10**digits, 10**digits), dtype=float)
    
    for iterator in range( 10**digits ):

        if iterator in vector:

            index = vector.index(iterator)
            amount = vector.count(iterator)

            if ( iterator == vector[ len(vector) - 1 ] ):
                amount = vector.count(iterator) - 1

            for count in range( amount ):

                MatrixResult[ vector[index] ][ vector[index + 1] ] += 1

                if ( count < vector.count(iterator) - 1 ):
                    index = vector.index( iterator, index + 1 )

    return MatrixResult

Example:

states = [1,1,2,6,8,5,5,7,8,8,1,1,4,5,5,0,0,0,1,1,4,4,5,1,3,3,4,5,4,1,1]
print( TransitionsMatrix( states, 1 ) )

Output:

[[2. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 4. 1. 1. 2. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 1. 3. 0. 0. 0. 0.]
 [1. 1. 0. 0. 1. 2. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 1. 0. 0. 0. 1. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

And then implement this function which receives by parameter the matrix of the previous function and returns the matrix of the string of marcov

# return the matrix probability between states
def TransitionProbability(matrixResult, vector):
    sizeMatrix = len(matrixResult)
    for row in range(sizeMatrix):
        for col in range(sizeMatrix):
            if matrixResult[row][col] != 0:
                matrixResult[row][col] = matrixResult[row][col] / vector.count(row)
    return matrixResult

Example:

states = [1,1,2,6,8,5,5,7,8,8,1,1,4,5,5,0,0,0,1,1,4,4,5,1,3,3,4,5,4,1,1]
MatrixResult = TransitionProbability( TransitionsMatrix(states, 1), states )
print( MatrixResult )

Output:

[[0.67 0.33 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00]
 [0.00 0.44 0.11 0.11 0.22 0.00 0.00 0.00 0.00 0.00]
 [0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00]
 [0.00 0.00 0.00 0.50 0.50 0.00 0.00 0.00 0.00 0.00]
 [0.00 0.20 0.00 0.00 0.20 0.60 0.00 0.00 0.00 0.00]
 [0.17 0.17 0.00 0.00 0.17 0.33 0.00 0.17 0.00 0.00]
 [0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00]
 [0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00]
 [0.00 0.33 0.00 0.00 0.00 0.33 0.00 0.00 0.33 0.00]
 [0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00]]

Upvotes: 0

Related Questions