Skip to content

Instantly share code, notes, and snippets.

@tg12
Created January 16, 2018 12:00
Show Gist options
  • Save tg12/d7efa579ceee4afbeaec97eb442a6b72 to your computer and use it in GitHub Desktop.
Save tg12/d7efa579ceee4afbeaec97eb442a6b72 to your computer and use it in GitHub Desktop.
Markov transition matrix in Python
#the following code takes a list such as
#[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]
#with states labeled as successive integers starting with 0
#and returns a transition matrix, M,
#where M[i][j] is the probability of transitioning from i to j
def transition_matrix(transitions):
n = 1+ max(transitions) #number of states
M = [[0]*n for _ in range(n)]
for (i,j) in zip(transitions,transitions[1:]):
M[i][j] += 1
#now convert to probabilities:
for row in M:
s = sum(row)
if s > 0:
row[:] = [f/s for f in row]
return M
#test:
t = [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]
m = transition_matrix(t)
for row in m: print(' '.join('{0:.2f}'.format(x) for x in row))
#0.67 0.33 0.00 0.00 0.00 0.00 0.00 0.00 0.00
#0.00 0.50 0.12 0.12 0.25 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.50 0.50 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.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 1.00
#0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00
#0.00 0.33 0.00 0.00 0.00 0.33 0.00 0.00 0.33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment