def nx_graph_nbrw(G): import networkx as nx A = nx.to_scipy_sparse_matrix(G) P = mkm.graph_nbrw_transition_matrix(A) mc = mkm.MarkovChain(P) mc.set_stationary_distribution(mkm.uniform_distribution(mc.get_n())) return mc
def nx_graph_nbrw(G): """Returns the Markov chain for the NBRW on the graph G. """ import networkx as nx A = nx.to_scipy_sparse_matrix(G) P = mkm.graph_nbrw_transition_matrix(A) mc = mkm.MarkovChain(P) mc.set_stationary(mkm.uniform_distribution(mc.get_n())) return mc
def tets_transition_matrix(): assert(mkm.is_transition_matrix(numpy.eye(1))) assert(mkm.is_transition_matrix(numpy.eye(5))) assert(mkm.is_transition_matrix(numpy.ones((1,2,3))) == False) assert(mkm.is_transition_matrix(mkm.line_lazy_transition_matrix(100, p = 0.51))) A = numpy.ones((3,3)) numpy.fill_diagonal(A,0) P = mkm.graph_nbrw_transition_matrix(ssp.dok_matrix(A)) assert(mkm.is_transition_matrix(P)) print P
def tets_transition_matrix(): assert (mkm.is_transition_matrix(numpy.eye(1))) assert (mkm.is_transition_matrix(numpy.eye(5))) assert (mkm.is_transition_matrix(numpy.ones((1, 2, 3))) == False) assert (mkm.is_transition_matrix( mkm.line_lazy_transition_matrix(100, p=0.51))) A = numpy.ones((3, 3)) numpy.fill_diagonal(A, 0) P = mkm.graph_nbrw_transition_matrix(ssp.dok_matrix(A)) assert (mkm.is_transition_matrix(P)) print P
def tets_transition_matrix(): assert (mkm.is_transition_matrix(numpy.eye(1))) assert (mkm.is_transition_matrix(numpy.eye(5))) assert (mkm.is_transition_matrix(numpy.ones((1, 2, 3))) == False) assert (mkm.is_transition_matrix( mkm.line_lazy_transition_matrix(100, p=0.51))) # graph_nbrw_transition_matrix A = numpy.ones((3, 3)) numpy.fill_diagonal(A, 0) P = mkm.graph_nbrw_transition_matrix(A) assert (mkm.is_transition_matrix(P)) print A print P # tree_nbrw_transition_matrix A = numpy.array([[0, 1, 0, 0, 0], [1, 0, 1, 1, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 0]]) P = mkm.tree_nbrw_transition_matrix(A, 0) assert (mkm.is_transition_matrix(P)) print A print P
# plot the total variation mixing mc.plot_tv_mixing(y_tol=0.01, threshold=1e-5) ####################### NBRW EXAMPLE ######################### import matplotlib.pyplot as plt # load a 6-regular graph with 50.000 nodes from file G_6_regular = nx.read_sparse6('6_regular.s6') # get the adjacency matrix A = nx.to_scipy_sparse_matrix(G_6_regular) # transition matrix for NBRW on the graph from the adjacency matrix P = mkm.graph_nbrw_transition_matrix(A) # Markov chain with the transition marix mc = mkm.MarkovChain(P) # the stationary distribution is uniform mc.set_stationary(mkm.uniform_distribution(mc.get_n())) # add a random starting position to the Markov chain mc.add_random_delta_distributions(1) # determine the mixing mc.compute_tv_mixing() # plot the mixing (x,tv) = mc.distribution_tv_mixing(0)