def test_can_get_inconditional_proba_array( initial_information: MarkovComponents, ) -> None: ini, trans_matrix = initial_information mc = MarkovChain(ini, trans_matrix) pi2 = mc.calc_inconditional_probability(n=2) expected_vector = [0.372, 0.31, 0.318] assert pi2 == expected_vector, "Wrong probability vector"
def test_can_calculate_transition_matrix_n_steps( initial_information: MarkovComponents, n: int, expectation: TransitionMatrix) -> None: ini, trans_matrix = initial_information mc = MarkovChain(ini, trans_matrix) p_matrix = mc.get_n_steps_probability_matrix(n) assert p_matrix == expectation, "The expected result was not found."
def test_can_create_markovchain_from_dict() -> None: data_dict: MarkovRepr = { "initial_prob": [0.1, 0.2], "transition_matrix": [ [0.1, 0.9], [0.8, 0.2], ], } MarkovChain.from_dict(data_dict)
def test_p0_matrix_is_identity(initial_information: MarkovComponents) -> None: ini, trans_matrix = initial_information mc = MarkovChain(ini, trans_matrix) p0_matrix = mc.get_n_steps_probability_matrix(n=0) expected_result = [ [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], ] assert (p0_matrix == expected_result ), "non-identity matrix was returned for p^0 operation"
def test_has_proper_representation( initial_information: MarkovComponents) -> None: initial, trans_matrix = initial_information expected_repr = {"initial": initial, "transition_matrix": trans_matrix} mc = MarkovChain(initial, trans_matrix) assert str(expected_repr) == repr( mc), "An incorrect representation was found"
def test_can_calculate_prob_with_initial_state( initial_information: MarkovComponents, ) -> None: ini, trans_matrix = initial_information mc = MarkovChain(ini, trans_matrix) assert mc.get_probability_from_sequence([1, 0], initial_state=0) == 0.18
def test_can_calculate_prob_from_sequence( initial_information: MarkovComponents, ) -> None: ini, trans_matrix = initial_information mc = MarkovChain(ini, trans_matrix) assert mc.get_probability_from_sequence([0, 1, 0, 2, 0]) == 0.00378
def test_can_create_markovchain_from_str() -> None: input_str = "0.1 0.2\n0.1 0.9\n0.8 0.2" MarkovChain.from_str(input_str)
def test_raises_exception_when_passing_invalid_matrix( invalid_trans_matrix: TransitionMatrix, ) -> None: with pytest.raises(InvalidTransitionMatrix): MarkovChain(initial_prob=[0.1, 0.2], transition_matrix=invalid_trans_matrix)