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]]