def circle_transition_matrix(n, p = 0.5, lazy = True): """ Returns the transition matrix of the (possibly lazy) biased random walk on the n-cycle. p : probability to go to the higher number not regarding lazyness (defaults to 0.5) lazy: should the walk be lazy? Defaults to True """ P = ssp.lil_matrix((n,n)) P[0,n-1] = (1-p) P[0,1] = p P[n-1,n-2] = (1-p) P[n-1,0] = p for i in range(1,n-1): P[i,i-1] = (1-p) P[i,i+1] = p if lazy: P = mkm.lazy(P) return P.tocsr()
def nx_graph_lazy_srw(G): """Returns the lazy srw on the graph G """ import networkx as nx A = nx.to_scipy_sparse_matrix(G) P = mkm.lazy(mkm.graph_srw_transition_matrix(A)) mc = mkm.MarkovChain(P) mc.set_stationary_distribution(mkm.graph_srw_stationary_distribution(A)) return mc
def hypercube_transition_matrix(n, lazy = True): """ Returns the transition matrix of the (possibly lazy) random walk on the n-dimensional hypercube. n: dimension. The chain has 2^n states lazy: should the walk be lazy? Defaults to True """ k = pow(2,n) P = ssp.lil_matrix((k,k)) p = 1./n # use our infinite wisdom on bitwise operators to swap bit j in the number i for i in range(0,k): for j in range(0,n): P[i, i ^ ( (i ^ (~ (i & (1 << j))) ) & (1 << j) )] = p if lazy: P = mkm.lazy(P) return P.tocsr()