Skip to content

Instantly share code, notes, and snippets.

@colmex
Last active August 29, 2015 14:25
Show Gist options
  • Save colmex/c559e6d94f5e43efbd15 to your computer and use it in GitHub Desktop.
Save colmex/c559e6d94f5e43efbd15 to your computer and use it in GitHub Desktop.
import scipy.sparse as sp
import numpy as np
def random_walk_transition_probability(n, p):
keys = p.keys()
m = 2*max(max(keys),abs(min(keys)))*n+1 # max(max(keys),abs(min(keys))) finds the largest step in the probability array
A = sp.csr_matrix((m, m))
for key in keys:
A += sp.diags(p[key]*np.ones(m-abs(key)), key)
x = np.zeros((m,1))
x[n] = 1.0
for i in xrange(n):
x = A.dot(x)
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment