<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numpy as np <EOL> from scipy import sparse <EOL> import itertools <EOL> from numpy . testing import assert_allclose , assert_array_equal , assert_raises <EOL> from nose . tools import eq_ , ok_ , raises <EOL> from quantecon . markov import ( <EOL> MarkovChain , mc_compute_stationary , mc_sample_path <EOL> ) <EOL> def list_of_array_equal ( s , t ) : <EOL> """<STR_LIT>""" <EOL> eq_ ( len ( s ) , len ( t ) ) <EOL> all ( assert_array_equal ( x , y ) for x , y in zip ( s , t ) ) <EOL> def KMR_Markov_matrix_sequential ( N , p , epsilon ) : <EOL> """<STR_LIT>""" <EOL> P = np . zeros ( ( N + <NUM_LIT:1> , N + <NUM_LIT:1> ) , dtype = float ) <EOL> P [ <NUM_LIT:0> , <NUM_LIT:0> ] , P [ <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT:1> - epsilon * ( <NUM_LIT:1> / <NUM_LIT:2> ) , epsilon * ( <NUM_LIT:1> / <NUM_LIT:2> ) <EOL> for n in range ( <NUM_LIT:1> , N ) : <EOL> P [ n , n - <NUM_LIT:1> ] = ( n / N ) * ( epsilon * ( <NUM_LIT:1> / <NUM_LIT:2> ) + <EOL> ( <NUM_LIT:1> - epsilon ) * ( ( ( n - <NUM_LIT:1> ) / ( N - <NUM_LIT:1> ) < p ) + ( ( n - <NUM_LIT:1> ) / ( N - <NUM_LIT:1> ) == p ) * ( <NUM_LIT:1> / <NUM_LIT:2> ) ) <EOL> ) <EOL> P [ n , n + <NUM_LIT:1> ] = ( ( N - n ) / N ) * ( epsilon * ( <NUM_LIT:1> / <NUM_LIT:2> ) + <EOL> ( <NUM_LIT:1> - epsilon ) * ( ( n / ( N - <NUM_LIT:1> ) > p ) + ( n / ( N - <NUM_LIT:1> ) == p ) * ( <NUM_LIT:1> / <NUM_LIT:2> ) ) <EOL> ) <EOL> P [ n , n ] = <NUM_LIT:1> - P [ n , n - <NUM_LIT:1> ] - P [ n , n + <NUM_LIT:1> ] <EOL> P [ N , N - <NUM_LIT:1> ] , P [ N , N ] = epsilon * ( <NUM_LIT:1> / <NUM_LIT:2> ) , <NUM_LIT:1> - epsilon * ( <NUM_LIT:1> / <NUM_LIT:2> ) <EOL> return P <EOL> def test_markovchain_pmatrices ( ) : <EOL> """<STR_LIT>""" <EOL> Q = np . zeros ( ( <NUM_LIT:6> , <NUM_LIT:6> ) ) <EOL> Q [ <NUM_LIT:0> , <NUM_LIT:1> ] , Q [ <NUM_LIT:1> , <NUM_LIT:0> ] = <NUM_LIT:1> , <NUM_LIT:1> <EOL> Q [ <NUM_LIT:2> , [ <NUM_LIT:0> , <NUM_LIT:3> ] ] = <NUM_LIT:1> / <NUM_LIT:2> <EOL> Q [ <NUM_LIT:3> , <NUM_LIT:4> ] , Q [ <NUM_LIT:4> , <NUM_LIT:5> ] , Q [ <NUM_LIT:5> , <NUM_LIT:3> ] = <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> <EOL> Q_stationary_dists = np . array ( [ [ <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] ] ) <EOL> testset = [ <EOL> { '<STR_LIT:P>' : np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> '<STR_LIT>' : np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> } , <EOL> { '<STR_LIT:P>' : sparse . csr_matrix ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> '<STR_LIT>' : np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> } , <EOL> { '<STR_LIT:P>' : np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) , <EOL> '<STR_LIT>' : np . array ( [ [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ] ) , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : [ np . arange ( <NUM_LIT:2> ) ] , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> ] ) , np . array ( [ <NUM_LIT:1> ] ) ] , <EOL> } , <EOL> { '<STR_LIT:P>' : np . eye ( <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] ) , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> ] ) , np . array ( [ <NUM_LIT:1> ] ) ] , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> ] ) , np . array ( [ <NUM_LIT:1> ] ) ] , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> { '<STR_LIT:P>' : np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) , <EOL> '<STR_LIT>' : np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] ] ) , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> ] ) , np . array ( [ <NUM_LIT:1> ] ) ] , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> ] ) ] , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> { '<STR_LIT:P>' : Q , <EOL> '<STR_LIT>' : Q_stationary_dists , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:2> ] ) , np . array ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ] , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ] , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : False , <EOL> } , <EOL> { '<STR_LIT:P>' : sparse . csr_matrix ( Q ) , <EOL> '<STR_LIT>' : Q_stationary_dists , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:2> ] ) , np . array ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ] , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , np . array ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ] , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> ] <EOL> for test_dict in testset : <EOL> mc = MarkovChain ( test_dict [ '<STR_LIT:P>' ] ) <EOL> computed = mc . stationary_distributions <EOL> assert_allclose ( computed , test_dict [ '<STR_LIT>' ] ) <EOL> assert ( mc . num_communication_classes == len ( test_dict [ '<STR_LIT>' ] ) ) <EOL> assert ( mc . is_irreducible == test_dict [ '<STR_LIT>' ] ) <EOL> assert ( mc . num_recurrent_classes == len ( test_dict [ '<STR_LIT>' ] ) ) <EOL> list_of_array_equal ( <EOL> sorted ( mc . communication_classes , key = lambda x : x [ <NUM_LIT:0> ] ) , <EOL> sorted ( test_dict [ '<STR_LIT>' ] , key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> ) <EOL> list_of_array_equal ( <EOL> sorted ( mc . recurrent_classes , key = lambda x : x [ <NUM_LIT:0> ] ) , <EOL> sorted ( test_dict [ '<STR_LIT>' ] , key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> ) <EOL> assert ( mc . period == test_dict [ '<STR_LIT>' ] ) <EOL> assert ( mc . is_aperiodic == test_dict [ '<STR_LIT>' ] ) <EOL> try : <EOL> list_of_array_equal ( <EOL> sorted ( mc . cyclic_classes , key = lambda x : x [ <NUM_LIT:0> ] ) , <EOL> sorted ( test_dict [ '<STR_LIT>' ] , key = lambda x : x [ <NUM_LIT:0> ] ) <EOL> ) <EOL> except NotImplementedError : <EOL> assert ( mc . is_irreducible is False ) <EOL> computed = mc_compute_stationary ( test_dict [ '<STR_LIT:P>' ] ) <EOL> assert_allclose ( computed , test_dict [ '<STR_LIT>' ] ) <EOL> class Test_markovchain_stationary_distributions_KMRMarkovMatrix2 ( ) : <EOL> """<STR_LIT>""" <EOL> N = <NUM_LIT> <EOL> epsilon = <NUM_LIT> <EOL> p = <NUM_LIT:1> / <NUM_LIT:3> <EOL> TOL = <NUM_LIT> <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . P = KMR_Markov_matrix_sequential ( self . N , self . p , self . epsilon ) <EOL> self . mc = MarkovChain ( self . P ) <EOL> self . stationary = self . mc . stationary_distributions <EOL> stat_shape = self . stationary . shape <EOL> if len ( stat_shape ) == <NUM_LIT:1> : <EOL> self . n_stat_dists = <NUM_LIT:1> <EOL> else : <EOL> self . n_stat_dists = stat_shape [ <NUM_LIT:0> ] <EOL> def test_markov_matrix ( self ) : <EOL> "<STR_LIT>" <EOL> mc = self . mc <EOL> assert_allclose ( np . sum ( mc . P , axis = <NUM_LIT:1> ) , np . ones ( mc . n ) ) <EOL> def test_sum_one ( self ) : <EOL> "<STR_LIT>" <EOL> stationary_distributions = self . stationary <EOL> assert_allclose ( np . sum ( stationary_distributions , axis = <NUM_LIT:1> ) , <EOL> np . ones ( self . n_stat_dists ) ) <EOL> def test_nonnegative ( self ) : <EOL> "<STR_LIT>" <EOL> stationary_distributions = self . stationary <EOL> assert ( np . all ( stationary_distributions > - <NUM_LIT> ) ) <EOL> def test_left_eigen_vec ( self ) : <EOL> "<STR_LIT>" <EOL> mc = self . mc <EOL> stationary_distributions = self . stationary <EOL> if self . n_stat_dists == <NUM_LIT:1> : <EOL> assert_allclose ( np . dot ( stationary_distributions , mc . P ) , <EOL> stationary_distributions , atol = self . TOL ) <EOL> else : <EOL> for i in range ( self . n_stat_dists ) : <EOL> curr_v = stationary_distributions [ i , : ] <EOL> assert_allclose ( np . dot ( curr_v , mc . P ) , curr_v , atol = self . TOL ) <EOL> def test_simulate_shape ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> mcs = [ MarkovChain ( P ) , MarkovChain ( sparse . csr_matrix ( P ) ) ] <EOL> for mc in mcs : <EOL> ( ts_length , init , num_reps ) = ( <NUM_LIT:10> , None , None ) <EOL> assert_array_equal ( mc . simulate ( ts_length , init , num_reps ) . shape , <EOL> ( ts_length , ) ) <EOL> ( ts_length , init , num_reps ) = ( <NUM_LIT:10> , [ <NUM_LIT:0> , <NUM_LIT:1> ] , None ) <EOL> assert_array_equal ( mc . simulate ( ts_length , init , num_reps ) . shape , <EOL> ( len ( init ) , ts_length ) ) <EOL> ( ts_length , init , num_reps ) = ( <NUM_LIT:10> , [ <NUM_LIT:0> , <NUM_LIT:1> ] , <NUM_LIT:3> ) <EOL> assert_array_equal ( mc . simulate ( ts_length , init , num_reps ) . shape , <EOL> ( len ( init ) * num_reps , ts_length ) ) <EOL> for ( ts_length , init , num_reps ) in [ ( <NUM_LIT:10> , None , <NUM_LIT:3> ) , ( <NUM_LIT:10> , None , <NUM_LIT:1> ) ] : <EOL> assert_array_equal ( mc . simulate ( ts_length , init , num_reps ) . shape , <EOL> ( num_reps , ts_length ) ) <EOL> def test_simulate_init_array_num_reps ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> mc = MarkovChain ( P ) <EOL> ts_length = <NUM_LIT:10> <EOL> init = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> num_reps = <NUM_LIT:3> <EOL> X = mc . simulate ( ts_length , init , num_reps ) <EOL> assert_array_equal ( X [ : , <NUM_LIT:0> ] , init * num_reps ) <EOL> def test_simulate_init_type ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> mc = MarkovChain ( P ) <EOL> seed = <NUM_LIT:0> <EOL> ts_length = <NUM_LIT:3> <EOL> init = <NUM_LIT:0> <EOL> X = mc . simulate ( ts_length , init = init , random_state = seed ) <EOL> inits_np_int = [ t ( init ) for t in [ np . int32 , np . int64 ] ] <EOL> for init in inits_np_int : <EOL> X_np_int = mc . simulate ( ts_length , init = init , random_state = seed ) <EOL> assert_array_equal ( X_np_int , X ) <EOL> def test_simulate_dense_vs_sparse ( ) : <EOL> n = <NUM_LIT:5> <EOL> a = <NUM_LIT:1> / <NUM_LIT:3> <EOL> b = ( <NUM_LIT:1> - a ) / <NUM_LIT:2> <EOL> P = np . zeros ( ( n , n ) ) <EOL> for i in range ( n ) : <EOL> P [ i , ( i - <NUM_LIT:1> ) % n ] , P [ i , i ] , P [ i , ( i + <NUM_LIT:1> ) % n ] = b , a , b <EOL> mcs = [ MarkovChain ( P ) , MarkovChain ( sparse . csr_matrix ( P ) ) ] <EOL> ts_length = <NUM_LIT:10> <EOL> inits = ( None , <NUM_LIT:0> , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> num_repss = ( None , <NUM_LIT:2> ) <EOL> random_state = <NUM_LIT:0> <EOL> for init , num_reps in itertools . product ( inits , num_repss ) : <EOL> assert_array_equal ( * ( mc . simulate ( ts_length , init , num_reps , <EOL> random_state = random_state ) <EOL> for mc in mcs ) ) <EOL> def test_simulate_ergodicity ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> stationary_dist = [ <NUM_LIT> , <NUM_LIT> ] <EOL> init = <NUM_LIT:0> <EOL> mc = MarkovChain ( P ) <EOL> seed = <NUM_LIT> <EOL> ts_length = <NUM_LIT:100> <EOL> num_reps = <NUM_LIT> <EOL> tol = <NUM_LIT:0.1> <EOL> x = mc . simulate ( ts_length , init = init , num_reps = num_reps , random_state = seed ) <EOL> frequency_1 = x [ : , - <NUM_LIT:1> ] . mean ( ) <EOL> ok_ ( np . abs ( frequency_1 - stationary_dist [ <NUM_LIT:1> ] ) < tol ) <EOL> def test_simulate_for_matrices_with_C_F_orders ( ) : <EOL> """<STR_LIT>""" <EOL> P_C = np . array ( [ [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] , order = '<STR_LIT:C>' ) <EOL> P_F = np . array ( [ [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] , order = '<STR_LIT:F>' ) <EOL> init = <NUM_LIT:1> <EOL> ts_length = <NUM_LIT:10> <EOL> sample_path = np . ones ( ts_length , dtype = int ) <EOL> computed_C_and_F = MarkovChain ( np . array ( [ [ <NUM_LIT:1.> ] ] ) ) . simulate ( ts_length , init = <NUM_LIT:0> ) <EOL> assert_array_equal ( computed_C_and_F , np . zeros ( ts_length , dtype = int ) ) <EOL> computed_C = MarkovChain ( P_C ) . simulate ( ts_length , init ) <EOL> computed_F = MarkovChain ( P_F ) . simulate ( ts_length , init = init ) <EOL> assert_array_equal ( computed_C , sample_path ) <EOL> assert_array_equal ( computed_F , sample_path ) <EOL> def test_mc_sample_path ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> Ps = [ P , sparse . csr_matrix ( P ) ] <EOL> init = <NUM_LIT:0> <EOL> sample_size = <NUM_LIT:10> <EOL> seed = <NUM_LIT> <EOL> for P in Ps : <EOL> expected = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> computed = mc_sample_path ( <EOL> P , init = init , sample_size = sample_size , random_state = seed <EOL> ) <EOL> assert_array_equal ( computed , expected ) <EOL> distribution = ( <NUM_LIT:0.5> , <NUM_LIT:0.5> ) <EOL> expected = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> computed = mc_sample_path ( <EOL> P , init = distribution , sample_size = sample_size , random_state = seed <EOL> ) <EOL> assert_array_equal ( computed , expected ) <EOL> def test_mc_sample_path_lln ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> stationary_dist = [ <NUM_LIT> , <NUM_LIT> ] <EOL> init = <NUM_LIT:0> <EOL> seed = <NUM_LIT> <EOL> sample_size = <NUM_LIT:10> ** <NUM_LIT:4> <EOL> tol = <NUM_LIT> <EOL> frequency_1 = mc_sample_path ( P , init = init , sample_size = sample_size , <EOL> random_state = seed ) . mean ( ) <EOL> ok_ ( np . abs ( frequency_1 - stationary_dist [ <NUM_LIT:1> ] ) < tol ) <EOL> class TestMCStateValues : <EOL> def setUp ( self ) : <EOL> state_values = [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:5> ] ] <EOL> self . state_values = np . array ( state_values ) <EOL> self . mc_reducible_dict = { <EOL> '<STR_LIT>' : MarkovChain ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] , <EOL> state_values = state_values ) , <EOL> '<STR_LIT>' : [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] ] , <EOL> '<STR_LIT>' : [ [ <NUM_LIT:0> ] , [ <NUM_LIT:2> ] ] <EOL> } <EOL> self . mc_periodic_dict = { <EOL> '<STR_LIT>' : MarkovChain ( [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ] , <EOL> state_values = state_values ) , <EOL> '<STR_LIT>' : [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ] , <EOL> '<STR_LIT>' : [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ] , <EOL> '<STR_LIT>' : [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:2> ] ] <EOL> } <EOL> def test_com_rec_classes ( self ) : <EOL> for mc_dict in [ self . mc_reducible_dict , self . mc_periodic_dict ] : <EOL> mc = mc_dict [ '<STR_LIT>' ] <EOL> coms = mc_dict [ '<STR_LIT>' ] <EOL> recs = mc_dict [ '<STR_LIT>' ] <EOL> properties = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> suffix = '<STR_LIT>' <EOL> for prop0 , classes_ind in zip ( properties , [ coms , recs ] ) : <EOL> for return_indices in [ True , False ] : <EOL> if return_indices : <EOL> classes = classes_ind <EOL> prop = prop0 + suffix <EOL> key = lambda x : x [ <NUM_LIT:0> ] <EOL> else : <EOL> classes = [ self . state_values [ i ] for i in classes_ind ] <EOL> prop = prop0 <EOL> key = lambda x : x [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> list_of_array_equal ( <EOL> sorted ( getattr ( mc , prop ) , key = key ) , <EOL> sorted ( classes , key = key ) <EOL> ) <EOL> def test_cyc_classes ( self ) : <EOL> mc = self . mc_periodic_dict [ '<STR_LIT>' ] <EOL> cycs = self . mc_periodic_dict [ '<STR_LIT>' ] <EOL> properties = [ '<STR_LIT>' ] <EOL> suffix = '<STR_LIT>' <EOL> for prop0 , classes_ind in zip ( properties , [ cycs ] ) : <EOL> for return_indices in [ True , False ] : <EOL> if return_indices : <EOL> classes = classes_ind <EOL> prop = prop0 + suffix <EOL> key = lambda x : x [ <NUM_LIT:0> ] <EOL> else : <EOL> classes = [ self . state_values [ i ] for i in classes_ind ] <EOL> prop = prop0 <EOL> key = lambda x : x [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> list_of_array_equal ( <EOL> sorted ( getattr ( mc , prop ) , key = key ) , <EOL> sorted ( classes , key = key ) <EOL> ) <EOL> def test_simulate ( self ) : <EOL> mc = self . mc_periodic_dict [ '<STR_LIT>' ] <EOL> ts_length = <NUM_LIT:6> <EOL> methods = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> init_idx = <NUM_LIT:0> <EOL> inits = [ init_idx , self . state_values [ init_idx ] ] <EOL> path = np . arange ( init_idx , init_idx + ts_length ) % mc . n <EOL> paths = [ path , self . state_values [ path ] ] <EOL> for method , init , X_expected in zip ( methods , inits , paths ) : <EOL> X = getattr ( mc , method ) ( ts_length , init ) <EOL> assert_array_equal ( X , X_expected ) <EOL> init_idx = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> inits = [ init_idx , self . state_values [ init_idx ] ] <EOL> path = np . array ( <EOL> [ np . arange ( i , i + ts_length ) % mc . n for i in init_idx ] <EOL> ) <EOL> paths = [ path , self . state_values [ path ] ] <EOL> for method , init , X_expected in zip ( methods , inits , paths ) : <EOL> X = getattr ( mc , method ) ( ts_length , init ) <EOL> assert_array_equal ( X , X_expected ) <EOL> inits = [ None , None ] <EOL> seed = <NUM_LIT> <EOL> init_idx = <NUM_LIT:2> <EOL> path = np . arange ( init_idx , init_idx + ts_length ) % mc . n <EOL> paths = [ path , self . state_values [ path ] ] <EOL> for method , init , X_expected in zip ( methods , inits , paths ) : <EOL> X = getattr ( mc , method ) ( ts_length , init , random_state = seed ) <EOL> assert_array_equal ( X , X_expected ) <EOL> def test_mc_stationary_distributions_state_values ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] <EOL> state_values = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] <EOL> mc = MarkovChain ( P , state_values = state_values ) <EOL> stationary_dists_expected = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] <EOL> stationary_dists = mc . stationary_distributions <EOL> assert_allclose ( stationary_dists , stationary_dists_expected ) <EOL> def test_get_index ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> mc = MarkovChain ( P ) <EOL> eq_ ( mc . get_index ( <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> eq_ ( mc . get_index ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , mc . get_index , <NUM_LIT:2> ) <EOL> assert_array_equal ( mc . get_index ( [ <NUM_LIT:1> , <NUM_LIT:0> ] ) , [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> assert_raises ( ValueError , mc . get_index , [ [ <NUM_LIT:1> ] ] ) <EOL> mc . state_values = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> eq_ ( mc . get_index ( <NUM_LIT:1> ) , <NUM_LIT:0> ) <EOL> eq_ ( mc . get_index ( <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> assert_raises ( ValueError , mc . get_index , <NUM_LIT:0> ) <EOL> assert_array_equal ( mc . get_index ( [ <NUM_LIT:2> , <NUM_LIT:1> ] ) , [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> assert_raises ( ValueError , mc . get_index , [ [ <NUM_LIT:1> ] ] ) <EOL> mc . state_values = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] <EOL> eq_ ( mc . get_index ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , <NUM_LIT:0> ) <EOL> assert_raises ( ValueError , mc . get_index , <NUM_LIT:1> ) <EOL> assert_array_equal ( mc . get_index ( [ [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) , [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> @ raises ( ValueError ) <EOL> def test_raises_value_error_non_2dim ( ) : <EOL> """<STR_LIT>""" <EOL> mc = MarkovChain ( np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_raises_value_error_non_sym ( ) : <EOL> """<STR_LIT>""" <EOL> P = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_raises ( ValueError , MarkovChain , P ) <EOL> assert_raises ( ValueError , MarkovChain , sparse . csr_matrix ( P ) ) <EOL> def test_raises_value_error_non_nonnegative ( ) : <EOL> """<STR_LIT>""" <EOL> P = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_raises ( ValueError , MarkovChain , P ) <EOL> assert_raises ( ValueError , MarkovChain , sparse . csr_matrix ( P ) ) <EOL> def test_raises_value_error_non_sum_one ( ) : <EOL> """<STR_LIT>""" <EOL> P = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> assert_raises ( ValueError , MarkovChain , P ) <EOL> assert_raises ( ValueError , MarkovChain , sparse . csr_matrix ( P ) ) <EOL> def test_raises_value_error_simulate_init_out_of_range ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> mc = MarkovChain ( P ) <EOL> n = mc . n <EOL> ts_length = <NUM_LIT:3> <EOL> assert_raises ( ValueError , mc . simulate , ts_length , init = n ) <EOL> assert_raises ( ValueError , mc . simulate , ts_length , init = - ( n + <NUM_LIT:1> ) ) <EOL> assert_raises ( ValueError , mc . simulate , ts_length , init = [ <NUM_LIT:0> , n ] ) <EOL> assert_raises ( ValueError , mc . simulate , ts_length , init = [ <NUM_LIT:0> , - ( n + <NUM_LIT:1> ) ] ) <EOL> def test_raises_non_homogeneous_state_values ( ) : <EOL> P = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> state_values = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , <NUM_LIT:2> ] <EOL> assert_raises ( ValueError , MarkovChain , P , state_values = state_values ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import nose <EOL> argv = sys . argv [ : ] <EOL> argv . append ( '<STR_LIT>' ) <EOL> argv . append ( '<STR_LIT>' ) <EOL> nose . main ( argv = argv , defaultTest = __file__ ) </s>
