예제 #1
0
    def add_random_delta_distributions(self, k=1):
        """ Convenience method that adds random delta distribution 
		to the Markov chain.

			k: Number of distributions to add. Defaults to 1.
		"""
        self.add_distributions(mkm.random_delta_distributions(self.n, k))
예제 #2
0
	def add_random_delta_distributions(self,k=1):
		""" Convenience method that adds random delta distribution 
		to the Markov chain.

			k: Number of distributions to add. Defaults to 1.
		"""
		self.add_distributions(mkm.random_delta_distributions(self.n, k))
예제 #3
0
def nx_graph_analyze_nbrw(G):  # pragma: no cover
    import networkx as nx
    import matplotlib.pyplot as plt

    mc = mkm.nx_graph_nbrw(G)
    mc.add_distributions(mkm.random_delta_distributions(mc.get_n(), 5))

    mc.compute_tv_mixing()

    plt.figure()
    for i in range(mc.num_distributions()):
        (x, tv) = mc.distribution_tv_mixing(i)
        plt.plot(x, tv)

    plt.xlabel("t")
    plt.ylabel("Distance to stationary distribution in total variation")
    plt.show()
예제 #4
0
def nx_graph_analyze_nbrw(G): # pragma: no cover
	import networkx as nx
	import matplotlib.pyplot as plt

	mc = mkm.nx_graph_nbrw(G)
	mc.add_distributions(mkm.random_delta_distributions(mc.get_n(),5))
	
	mc.compute_tv_mixing()

	plt.figure()
	for i in range(mc.num_distributions()):
		(x,tv) = mc.distribution_tv_mixing(i)
		plt.plot(x, tv)

	plt.xlabel("t")
	plt.ylabel("Distance to stationary distribution in total variation")
	plt.show()	
예제 #5
0
def test_markov_chain():
    #  initialization and initial state (take n>=10000 to challenge the numerics)
    n = 100
    mc = MarkovChain(mkm.line_lazy_transition_matrix(n))

    assert (mc.get_n() == n)
    assert (mc.stationary_distribution_known() == False)
    assert (mc.get_stationary_distribution() == None)
    assert (mc.num_distributions() == 0)

    # distributions
    mc.add_distributions(mkm.delta_distribution(n, 0))
    assert (mc.get_distribution(0) == mkm.delta_distribution(n, 0)).all()

    mc.add_distributions(mkm.random_delta_distributions(n, 2))
    mc.add_distributions(mkm.delta_distribution(n, n - 1))
    assert (mc.num_distributions() == 4)

    # iterations
    assert (mc.last_iteration_time(1) == 0)

    # iterate
    mc.iterate_distributions(
        [0], 2)  # this one will determine the stationary distribution
    assert (mc.last_iteration_time(0) == 2)
    assert (mc.next_iteration_time(0, 1) == 2)

    mc.iterate_distributions([0, 1, 3], 5)

    mc.iterate_distributions_to_stationarity([0, 2])
    mc.iterate_all_distributions_to_stationarity()

    # stationary distribution

    # mixing
    (x, tv) = mc.distribution_tv_mixing(1)
    mc.compute_tv_mixing()

    # print some stuff
    mc.print_info()
예제 #6
0
def test_markov_chain():
	#  initialization and initial state (take n>=10000 to challenge the numerics)
	n = 100
	mc = MarkovChain(mkm.line_lazy_transition_matrix(n))

	assert(mc.get_n() == n)
	assert(mc.stationary_distribution_known() == False)
	assert(mc.get_stationary_distribution() == None)
	assert(mc.num_distributions() == 0)

	# distributions
	mc.add_distributions(mkm.delta_distribution(n,0))
	assert (mc.get_distribution(0) == mkm.delta_distribution(n,0)).all()

	mc.add_distributions(mkm.random_delta_distributions(n,2))
	mc.add_distributions(mkm.delta_distribution(n,n-1))
	assert(mc.num_distributions() == 4)

	# iterations
	assert(mc.last_iteration_time(1) == 0)

	# iterate 
	mc.iterate_distributions([0],2) # this one will determine the stationary distribution
	assert(mc.last_iteration_time(0) == 2)
	assert(mc.next_iteration_time(0,1) == 2)

	mc.iterate_distributions([0,1,3],5)

	mc.iterate_distributions_to_stationarity([0,2])
	mc.iterate_all_distributions_to_stationarity()

	# stationary distribution

	# mixing
	(x,tv) = mc.distribution_tv_mixing(1)
	mc.compute_tv_mixing()

	# print some stuff
	mc.print_info()
예제 #7
0
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 SRW on the graph from the adjacency matrix 
P = mkm.graph_srw_transition_matrix(A)

# Markov chain with the transition marix
mc = mkm.MarkovChain(P)

# stationary distribution of SRW on a graph is deg(x)/2*|E|
mc.set_stationary_distribution(mkm.graph_srw_stationary_distribution(A))

# add a random starting position to the Markov chain
mc.add_distributions(mkm.random_delta_distributions(mc.get_n(),1))

# determine the mixing in total variation 
mc.compute_tv_mixing()

# plot the mixing
(x,tv) = mc.distribution_tv_mixing(0)
plt.plot(x, tv)
plt.xlabel("t")
plt.ylabel("Distance to stationary distribution in total variation")
plt.show()	




G_3_regular = nx.read_sparse6('3_regular.s6')
예제 #8
0
def test_iteration():
	import numpy, time, random

	N = 10000
	k = 10000
	P = mkm.line_lazy_transition_matrix(N)
	P = P.transpose()
	P = P.tocsr()
	
	# single distribution
	x = mkm.delta_distribution(N,0)
	start = time.time()
	for i in xrange(k):
		x = P.dot(x)
	end = time.time()
	print "Python loop:"
	print end - start 
	print x

	x = mkm.delta_distribution(N,0)
	start = time.time()
	x = mkm.matrix_vector_iteration_local(P,x,k)
	end = time.time()
	print "Python local iteration:"
	print end - start 
	print x

	x = mkm.delta_distribution(N,0)
	start = time.time()
	x = mkm.matrix_vector_iteration_by_processes(P,x,k)
	end = time.time()
	print "Python iterating (multiple processes):"
	print end - start 
	print x

	P = P.transpose()
	x = mkm.delta_distribution(N,0)
	start = time.time()
	x = mkm.iterate_distributions(P,x,k)
	end = time.time()
	print "Generic Python iteration:"
	print end - start 
	print x
	P = P.transpose()

	# multiple distributions
	k = 10000
	nd = 10

	random.seed(0)
	x = mkm.random_delta_distributions(N,nd).transpose()
	start = time.time()
	x = mkm.matrix_vector_iteration_local(P,x,k)
	end = time.time()
	print "Python local iteration:"
	print end - start 
	print x

	random.seed(0)
	x = mkm.random_delta_distributions(N,nd).transpose()
	start = time.time()
	x = mkm.matrix_vector_iteration_by_processes(P,x,k)
	end = time.time()
	print "Python iterating (multiple processes):"
	print end - start 
	print x

	random.seed(0)
	P = P.transpose()
	x = mkm.random_delta_distributions(N,nd)
	start = time.time()
	x = mkm.iterate_distributions(P,x,k).transpose()
	end = time.time()
	print "Generic Python iteration:"
	print end - start 
	print x
	P = P.transpose()