def test_simple_cid_examples(self) -> None: get_minimal_cid() get_5node_cid() get_5node_cid() get_5node_cid_with_scaled_utility() get_2dec_cid() get_insufficient_recall_cid()
def test_possible_pure_decision_rules(self) -> None: cid = get_minimal_cid() possible_pure_decision_rules = cid.pure_decision_rules('A') self.assertEqual(len(possible_pure_decision_rules), 2) expected_utilities = [] for decision_rule in possible_pure_decision_rules: cid.add_cpds(decision_rule) cid.check_model() expected_utilities.append(cid.expected_utility({})) self.assertEqual(set(expected_utilities), {0, 1}) cid = get_3node_cid() possible_pure_decision_rules = cid.pure_decision_rules('D') self.assertEqual(len(possible_pure_decision_rules), 4) expected_utilities = [] matrices = set() for decision_rule in possible_pure_decision_rules: cid.add_cpds(decision_rule) matrices.add(tuple(cid.get_cpds('D').values.flatten())) cid.check_model() expected_utilities.append(cid.expected_utility({})) self.assertEqual(set(expected_utilities), {-1, 0, 1}) self.assertEqual(len(matrices), 4) five_node = get_5node_cid() possible_pure_decision_rules = five_node.pure_decision_rules('D') self.assertEqual(len(possible_pure_decision_rules), 16) expected_utilities = [] for decision_rule in possible_pure_decision_rules: five_node.add_cpds(decision_rule) five_node.check_model() expected_utilities.append(five_node.expected_utility({})) self.assertEqual(set(expected_utilities), {0.5, 1.0, 1.5})
def test_intervention(self) -> None: cid = get_minimal_cid() cid.impute_random_policy() self.assertEqual(cid.expected_value(['B'], {})[0], 0.5) for a in [0, 1, 2]: cid.intervene({'A': a}) self.assertEqual(cid.expected_value(['B'], {})[0], a) self.assertEqual(cid.expected_value(['B'], {}, intervene={'A': 1})[0], 1)
def test_total_effect(self) -> None: cid = get_minimal_cid() cid.impute_random_policy() self.assertEqual(total_effect(cid, 'A', 'B', 0, 1), 1) cid = get_introduced_bias() cid.impute_random_policy() self.assertEqual(total_effect(cid, 'A', 'X', 0, 1), 0.5) self.assertEqual(total_effect(cid, 'A', 'D', 0, 1), 0) self.assertEqual(total_effect(cid, 'A', 'Y', 0, 1), 0.5)
def test_initialize_uniform_random_cpd(self) -> None: cid = get_minimal_cid() cpd_a = UniformRandomCPD('A', [0, 2]) cpd_a.initialize_tabular_cpd(cid) self.assertTrue((cpd_a.get_values() == np.array([[0.5], [0.5]])).all()) self.assertEqual(cpd_a.get_state_names('A', 1), 2) cpd_b = UniformRandomCPD('B', [0, 1]) cpd_b.initialize_tabular_cpd(cid) self.assertTrue((cpd_a.get_values() == np.array([[0.5, 0.5], [0.5, 0.5]])).all())
def test_intervention(self) -> None: cid = get_minimal_cid() cid.impute_random_policy() self.assertEqual(cid.expected_value(['B'], {})[0], 0.5) for a in [0, 1, 2]: cid.intervene({'A': a}) self.assertEqual(cid.expected_value(['B'], {})[0], a) self.assertEqual(cid.expected_value(['B'], {}, intervene={'A': 1})[0], 1) macid = taxi_competition() macid.impute_fully_mixed_policy_profile() self.assertEqual(macid.expected_value(['U1'], {}, intervene={'D1': 'c', 'D2': 'e'})[0], 3) self.assertEqual(macid.expected_value(['U2'], {}, intervene={'D1': 'c', 'D2': 'e'})[0], 5)
def test_initialize_function_cpd(self) -> None: cid = get_minimal_cid() cpd_a = FunctionCPD('A', lambda: 2, evidence=[]) cpd_a.initialize_tabular_cpd(cid) self.assertTrue(cpd_a.get_values(), np.array([[1]])) self.assertEqual(cpd_a.get_cardinality(['A'])['A'], 1) self.assertEqual(cpd_a.get_state_names('A', 0), 2) cpd_b = FunctionCPD('B', lambda x: x, evidence=['A']) cpd_b.initialize_tabular_cpd(cid) self.assertTrue(cpd_a.get_values(), np.array([[1]])) self.assertEqual(cpd_a.get_cardinality(['A'])['A'], 1) self.assertEqual(cpd_a.get_state_names('A', 0), 2)
def test_optimal_decision_rules(self) -> None: cid = get_minimal_cid() optimal_decision_rules = cid.optimal_pure_decision_rules('A') self.assertEqual(len(optimal_decision_rules), 1) for cpd in optimal_decision_rules: cid.add_cpds(cpd) self.assertEqual(cid.expected_utility({}), 1) cid = get_3node_cid() optimal_decision_rules = cid.optimal_pure_decision_rules('D') self.assertEqual(len(optimal_decision_rules), 1) for cpd in optimal_decision_rules: cid.add_cpds(cpd) self.assertEqual(cid.expected_utility({}), 1) five_node = get_5node_cid() optimal_decision_rules = five_node.optimal_pure_decision_rules('D') self.assertEqual(len(optimal_decision_rules), 4) five_node.impute_optimal_policy() for cpd in optimal_decision_rules: five_node.add_cpds(cpd) self.assertEqual(five_node.expected_utility({}), 1.5)