Last active
August 29, 2015 14:25
-
-
Save colmex/c559e6d94f5e43efbd15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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