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
Пример #2
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_known() == False)
	assert(mc.get_stationary() == 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_random_delta_distributions(2)
	mc.add_distributions(mkm.delta_distribution(n,n-1))
	assert(mc.num_distributions() == 4)

	# iterations
	assert(mc.last_iteration_time(1) == 0)
	assert (mc.closest_iteration_time(0,0) == 0)
	assert (mc.closest_iteration_time(0,5) == 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()

	# assert iteration time and prev & next iteration time
	mc.assert_iteration([0], 99)
	mc.assert_iteration([0], 101)

	assert(mc.previous_iteration_time(0,100) == 99)
	assert(mc.next_iteration_time(0,100) == 101)

	# stationary distribution

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

	# path sampling
	path = mc.sample_path(1,10)
	assert(path[0] == 1)
	assert(len(path) == 10)

	# print 
	mc.print_info()
Пример #3
0
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
Пример #4
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()
Пример #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 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
Пример #7
0
####################### 50-CYCLE EXAMPLE #########################
# create a graph
G = nx.cycle_graph(50)
    
# create a MarkovChain that is lazy simple random walk on the graph
mc = mkm.nx_graph_lazy_srw(G)

# plot the total variation mixing
mc.add_random_delta_distributions(1)
mc.plot_tv_mixing(y_tol=0.01, threshold=0.01)  


####################### BIASED LINE EXAMPLE #########################
# create the transition matrix
P = mkm.line_lazy_transition_matrix(1000, p=0.51)

# create the MarkovChain with the given transition matrix
mc = mkm.MarkovChain(P)
    
# add some initial distributions
for i in [0,500,999]:
	mc.add_distributions(mkm.delta_distribution(1000,x=i))
        
# plot the total variation mixing
mc.plot_tv_mixing(y_tol=0.01, threshold=1e-5)


####################### NBRW EXAMPLE #########################
import matplotlib.pyplot as plt
Пример #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()