Example #1
0
 def test_introduced_bias_reversed_sign(self) -> None:
     cbn = CausalBayesianNetwork([("A", "D"), ("A", "Y")])
     cbn.add_cpds(A=discrete_uniform([0, 1]), D=lambda a: 0, Y=lambda a: a)
     assert introduced_total_effect(cbn, "A", "D", "Y") == pytest.approx(-1)
     cbn.add_cpds(Y=lambda a: -a)
     assert introduced_total_effect(
         cbn, "A", "D", "Y", adapt_marginalized=True) == pytest.approx(-1)
Example #2
0
def test_random_cpd_copy() -> None:
    """check that a copy of a random cpd yields the same distribution"""
    cbn = CausalBayesianNetwork([("A", "B")])
    cbn.add_cpds(
        RandomCPD("A"),
        FunctionCPD("B", lambda a: a),
    )
    cbn2 = cbn.copy()
    assert cbn.expected_value(["B"], {}) == cbn2.expected_value(["B"], {})
Example #3
0
 def test_introduced_bias_reversed_sign(self) -> None:
     cbn = CausalBayesianNetwork([("A", "D"), ("A", "Y")])
     cbn.add_cpds(
         UniformRandomCPD("A", [0, 1]),
         FunctionCPD("D", lambda a: 0),
         FunctionCPD("Y", lambda a: a),
     )
     assert introduced_total_effect(cbn, "A", "D", "Y") == pytest.approx(-1)
     cbn.add_cpds(FunctionCPD("Y", lambda a: -a))
     assert introduced_total_effect(
         cbn, "A", "D", "Y", adapt_marginalized=True) == pytest.approx(-1)
Example #4
0
 def test_query_disconnected_components() -> None:
     cbn = CausalBayesianNetwork([("A", "B")])
     cbn.add_cpds(A=RandomCPD(), B=RandomCPD())
     cbn.query(["A"], {}, intervention={
         "B": 0
     })  # the intervention separates A and B into separare components