Example #1
0
def test_reachable_observed_vars_indirect_evidential(simple_model):
    observed_vars = {'13': np.array([1,0])}
    infer = BeliefPropagation(simple_model)
    infer.query(evidence=observed_vars)

    expected = {'13'}
    observed = simple_model.reachable_observed_variables(
        source='9',
        observed=set(observed_vars.keys())
    )
    assert expected == observed
Example #2
0
def test_reachable_observed_vars_blocked_causal(simple_model):
    observed_vars = {'1': np.array([0,1]), '2': np.array([1,0]), '3': np.array([0,1])}
    infer = BeliefPropagation(simple_model)
    infer.query(evidence=observed_vars)

    expected = {'3'}
    observed = simple_model.reachable_observed_variables(
        source='9',
        observed=set(observed_vars.keys())
    )
    assert expected == observed
Example #3
0
def test_reachable_observed_vars_common_cause(simple_model):
    observed_vars = {'10': np.array([0,1])}
    infer = BeliefPropagation(simple_model)
    infer.query(evidence=observed_vars)

    expected = {'10'}
    observed = simple_model.reachable_observed_variables(
        source='9',
        observed=set(observed_vars.keys())
    )
    assert expected == observed
Example #4
0
def test_reachable_observed_vars_direct_common_effect(simple_model):
    observed_vars = {'14': np.array([1,0]), 'x': np.array([1,0])}
    infer = BeliefPropagation(simple_model)
    infer.query(evidence=observed_vars)

    expected = {'x', '14'}
    observed = simple_model.reachable_observed_variables(
        source='9',
        observed=set(observed_vars.keys())
    )
    assert expected == observed
def test_virtual_evidence_for_node_x_five_node_model(five_node_model):
    """Virtual evidence for node x."""
    expected = {
        'x': 0.967741935483871,
        'y': 0.967741935483871,
        'z': 0.967741935483871,
        'u': 0.6451612903225806,
        'v': 0.6451612903225806
    }
    infer = BeliefPropagation(five_node_model)
    query_result = infer.query(evidence={'x': np.array([1, 10])})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
Example #6
0
def test_get_reachable_obs_vars_for_inferred(simple_model):
    observed_vars = {'6': np.array([1,0]), '7': np.array([1,0]), '10': np.array([1,0])}
    infer = BeliefPropagation(simple_model)
    infer.query(evidence=observed_vars)

    print(set(simple_model.get_unobserved_variables_in_definite_state(observed_vars.keys())))
    print(simple_model._get_ancestors_of(set(observed_vars.keys())))
    expected = {'4': {'10'}, '1': {'10'}, '11': {'7', '6', '10'}, '2': {'10'},
                '8': {'7', '6'}, '5': {'10'}, '3': {'10'}, '9': {'7', '6', '10'}}

    observed = get_reachable_observed_variables_for_inferred_variables(
        model=simple_model,
        observed=set(observed_vars.keys())
    )
    assert expected == observed
def test_positive_evidence_node_5(simple_model):
    expected = {
        '1': 0.5714285714285714,
        '5': 1,
        '3': 0.8571428571428571,
        '10': 1.0,
        '8': 0.75,
        '2': 0.5714285714285714,
        '4': 0.5714285714285714,
        '6': 0.5,
        '7': 0.5,
        '14': 0.5,
        '12': 1.0,
        '13': 1.0,
        '11': 1.0,
        '9': 1.0,
        'x': 1.0
    }
    infer = BeliefPropagation(simple_model)
    query_result = infer.query(evidence={'5': np.array([0, 1])})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
def test_positive_evidence_node_13(simple_model):
    expected = {
        '6': 0.50793650793650791,
        '3': 0.76190476190476186,
        '9': 0.98412698412698407,
        '8': 0.76190476190476186,
        'x': 1.0,
        '4': 0.50793650793650791,
        '11': 0.98412698412698407,
        '1': 0.50793650793650791,
        '5': 0.88888888888888884,
        '2': 0.50793650793650791,
        '12': 1.0,
        '14': 0.50793650793650791,
        '13': 1,
        '10': 0.88888888888888884,
        '7': 0.50793650793650791
    }
    infer = BeliefPropagation(simple_model)
    query_result = infer.query(evidence={'13': np.array([0, 1])})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
def test_no_evidence_simple_model(simple_model):
    expected = {
        'x': 0.984375,
        '14': 0.5,
        '7': 0.5,
        '2': 0.5,
        '3': 0.75,
        '13': 0.984375,
        '6': 0.5,
        '4': 0.5,
        '8': 0.75,
        '10': 0.875,
        '1': 0.5,
        '9': 0.96875,
        '12': 0.984375,
        '5': 0.875,
        '11': 0.96875
    }
    infer = BeliefPropagation(simple_model)
    query_result = infer.query(evidence={})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
def test_belief_propagation_no_modify_model_inplace(simple_model):
    assert not simple_model.all_nodes_are_fully_initialized
    infer = BeliefPropagation(simple_model, inplace=False)
    _ = infer.query(evidence={})
    # after belief propagation, model node values should be unchanged
    assert not simple_model.all_nodes_are_fully_initialized
def test_no_evidence_mixed_cpd_model(mixed_cpd_model):
    expected = {'x': 1 - 0.5**2, 'z': 0.5 * (1 - 0.5**2)}
    infer = BeliefPropagation(mixed_cpd_model)
    query_result = infer.query(evidence={})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
def test_one_parent_true_five_node_and_model(five_node_and_model):
    expected = {'x': 0.5}
    infer = BeliefPropagation(five_node_and_model)
    query_result = infer.query(evidence={'u': np.array([0, 1])})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
def test_no_evidence_five_node_and_model(five_node_and_model):
    expected = {'x': 0.5**2}
    infer = BeliefPropagation(five_node_and_model)
    query_result = infer.query(evidence={})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)
def test_NO_evidence_one_node_model(one_node_model):
    expected = {'x': 0}
    infer = BeliefPropagation(one_node_model)
    query_result = infer.query(evidence={'x': np.array([1, 0])})
    result = get_label_mapped_to_positive_belief(query_result)
    compare_dictionaries(expected, result)