def setUp(self): """ Setup a KMRMarkovMatrix and Compute Stationary Values """ self.P = KMR_Markov_matrix_sequential(self.N, self.p, self.epsilon) self.mc = MarkovChain(self.P) self.stationary = self.mc.stationary_distributions stat_shape = self.stationary.shape if len(stat_shape) == 1: self.n_stat_dists = 1 else: self.n_stat_dists = stat_shape[0]
def test_simulate_for_matrices_with_C_F_orders(): """ Test MarkovChasin.simulate for matrices with C- and F-orders See the issue and fix on Numba: github.com/numba/numba/issues/1103 github.com/numba/numba/issues/1104 """ P_C = np.array([[0.5, 0.5], [0, 1]], order='C') P_F = np.array([[0.5, 0.5], [0, 1]], order='F') init = 1 sample_size = 10 sample_path = np.ones(sample_size, dtype=int) computed_C_and_F = \ MarkovChain(np.array([[1.]])).simulate(init=0, sample_size=sample_size) assert_array_equal(computed_C_and_F, np.zeros(sample_size, dtype=int)) computed_C = MarkovChain(P_C).simulate(init, sample_size) computed_F = MarkovChain(P_F).simulate(init, sample_size) assert_array_equal(computed_C, sample_path) assert_array_equal(computed_F, sample_path)
def test_markovchain_pmatrices(): """ Test the methods of MarkovChain, as well as mc_compute_stationary, with P matrix and known solutions """ # Matrix with two recurrent classes [0, 1] and [3, 4, 5], # which have periods 2 and 3, respectively Q = np.zeros((6, 6)) Q[0, 1], Q[1, 0] = 1, 1 Q[2, [0, 3]] = 1/2 Q[3, 4], Q[4, 5], Q[5, 3] = 1, 1, 1 Q_stationary_dists = \ np.array([[1/2, 1/2, 0, 0, 0, 0], [0, 0, 0, 1/3, 1/3, 1/3]]) testset = [ {'P': np.array([[0.4, 0.6], [0.2, 0.8]]), # P matrix 'stationary_dists': np.array([[0.25, 0.75]]), # Known solution 'comm_classes': [np.arange(2)], 'rec_classes': [np.arange(2)], 'is_irreducible': True, 'period': 1, 'is_aperiodic': True, 'cyclic_classes': [np.arange(2)], }, {'P': np.array([[0, 1], [1, 0]]), 'stationary_dists': np.array([[0.5, 0.5]]), 'comm_classes': [np.arange(2)], 'rec_classes': [np.arange(2)], 'is_irreducible': True, 'period': 2, 'is_aperiodic': False, 'cyclic_classes': [np.array([0]), np.array([1])], }, {'P': np.eye(2), 'stationary_dists': np.array([[1, 0], [0, 1]]), 'comm_classes': [np.array([0]), np.array([1])], 'rec_classes': [np.array([0]), np.array([1])], 'is_irreducible': False, 'period': 1, 'is_aperiodic': True, }, {'P': Q, 'stationary_dists': Q_stationary_dists, 'comm_classes': [np.array([0, 1]), np.array([2]), np.array([3, 4, 5])], 'rec_classes': [np.array([0, 1]), np.array([3, 4, 5])], 'is_irreducible': False, 'period': 6, 'is_aperiodic': False, } ] # Loop Through TestSet # for test_dict in testset: mc = MarkovChain(test_dict['P']) computed = mc.stationary_distributions assert_allclose(computed, test_dict['stationary_dists']) assert(mc.num_communication_classes == len(test_dict['comm_classes'])) assert(mc.is_irreducible == test_dict['is_irreducible']) assert(mc.num_recurrent_classes == len(test_dict['rec_classes'])) list_of_array_equal( sorted(mc.communication_classes, key=lambda x: x[0]), sorted(test_dict['comm_classes'], key=lambda x: x[0]) ) list_of_array_equal( sorted(mc.recurrent_classes, key=lambda x: x[0]), sorted(test_dict['rec_classes'], key=lambda x: x[0]) ) assert(mc.period == test_dict['period']) assert(mc.is_aperiodic == test_dict['is_aperiodic']) try: list_of_array_equal( sorted(mc.cyclic_classes, key=lambda x: x[0]), sorted(test_dict['cyclic_classes'], key=lambda x: x[0]) ) except NotImplementedError: assert(mc.is_irreducible is False) # Test of mc_compute_stationary computed = mc_compute_stationary(test_dict['P']) assert_allclose(computed, test_dict['stationary_dists'])
def test_raises_value_error_non_sum_one(): """Test with input such that some of the rows does not sum to one""" mc = MarkovChain(np.array([[0.4, 0.6], [0.2, 0.9]]))
def test_raises_value_error_non_nonnegative(): """Test with non nonnegative input""" mc = MarkovChain(np.array([[0.4, 0.6], [-0.2, 1.2]]))
def test_raises_value_error_non_sym(): """Test with non symmetric input""" mc = MarkovChain(np.array([[0.4, 0.6]]))
def test_raises_value_error_non_2dim(): """Test with non 2dim input""" mc = MarkovChain(np.array([0.4, 0.6]))