예제 #1
0
def test_slogl_dbn():
    variables = ["a", "b", "c", "d"]

    static_bn = GaussianNetwork(["a", "b", "c", "d"], [("a", "c"), ("b", "c"),
                                                       ("c", "d")])
    static_bn = GaussianNetwork(["a", "b", "c", "d"], [("a", "c"), ("b", "c"),
                                                       ("c", "d")])
    gbn = DynamicGaussianNetwork(variables, 2)

    static_bn = gbn.static_bn()
    static_bn.add_arc("a_t_2", "c_t_2")
    static_bn.add_arc("b_t_2", "c_t_2")
    static_bn.add_arc("c_t_2", "d_t_2")
    static_bn.add_arc("a_t_1", "c_t_1")
    static_bn.add_arc("b_t_1", "c_t_1")
    static_bn.add_arc("c_t_1", "d_t_1")

    transition_bn = gbn.transition_bn()
    transition_bn.add_arc("a_t_2", "a_t_0")
    transition_bn.add_arc("b_t_2", "b_t_0")
    transition_bn.add_arc("c_t_2", "c_t_0")
    transition_bn.add_arc("d_t_2", "d_t_0")
    transition_bn.add_arc("a_t_1", "a_t_0")
    transition_bn.add_arc("b_t_1", "b_t_0")
    transition_bn.add_arc("c_t_1", "c_t_0")
    transition_bn.add_arc("d_t_1", "d_t_0")

    gbn.fit(df)
    test_df = util_test.generate_normal_data(100)
    ll = numpy_logl(gbn, test_df)
    assert np.isclose(gbn.slogl(test_df), ll.sum())
예제 #2
0
def gaussian_fit_bytes():
    gaussian = GaussianNetwork(["a", "b", "c", "d"], [("a", "b")])
    lg_a = LinearGaussianCPD("a", [], [0], 0.5)
    lg_b = LinearGaussianCPD("b", ["a"], [1, 2], 2)
    lg_c = LinearGaussianCPD("c", [], [2], 1)
    lg_d = LinearGaussianCPD("d", [], [3], 1.5)
    gaussian.add_cpds([lg_a, lg_b, lg_c, lg_d])
    gaussian.include_cpd = True
    return pickle.dumps(gaussian)
예제 #3
0
def test_create_dbn():
    variables = ["a", "b", "c", "d"]
    gbn = DynamicGaussianNetwork(variables, 2)

    assert gbn.markovian_order() == 2
    assert gbn.variables() == ["a", "b", "c", "d"]
    assert gbn.num_variables() == 4
    assert gbn.type() == pbn.GaussianNetworkType()

    transition_nodes = [v + "_t_0" for v in variables]
    static_nodes = [v + "_t_" + str(m) for v in variables for m in range(1, 3)]

    assert set(gbn.static_bn().nodes()) == set(static_nodes)
    assert set(gbn.transition_bn().interface_nodes()) == set(static_nodes)
    assert set(gbn.transition_bn().nodes()) == set(transition_nodes)

    static_bn = GaussianNetwork(static_nodes)
    transition_bn = ConditionalGaussianNetwork(transition_nodes, static_nodes)

    gbn2 = DynamicGaussianNetwork(variables, 2, static_bn, transition_bn)

    wrong_transition_bn = pbn.ConditionalDiscreteBN(transition_nodes,
                                                    static_nodes)

    with pytest.raises(ValueError) as ex:
        gbn3 = DynamicGaussianNetwork(variables, 2, static_bn,
                                      wrong_transition_bn)
    assert "Static and transition Bayesian networks do not have the same type" in str(
        ex.value)

    wrong_static_bn = pbn.DiscreteBN(static_nodes)
    with pytest.raises(ValueError) as ex:
        gbn4 = DynamicGaussianNetwork(variables, 2, wrong_static_bn,
                                      wrong_transition_bn)
    assert "Bayesian networks are not Gaussian." in str(ex.value)
예제 #4
0
def test_serialization_unfitted_factor(lg_bytes, ckde_bytes, discrete_bytes,
                                       new_bytes, newbis_bytes):
    loaded_lg = pickle.loads(lg_bytes)
    assert loaded_lg.variable() == "c"
    assert set(loaded_lg.evidence()) == set(["a", "b"])
    assert not loaded_lg.fitted()
    assert loaded_lg.type() == pbn.LinearGaussianCPDType()

    loaded_ckde = pickle.loads(ckde_bytes)
    assert loaded_ckde.variable() == "c"
    assert set(loaded_ckde.evidence()) == set(["a", "b"])
    assert not loaded_ckde.fitted()
    assert loaded_ckde.type() == pbn.CKDEType()

    loaded_discrete = pickle.loads(discrete_bytes)
    assert loaded_discrete.variable() == "c"
    assert set(loaded_discrete.evidence()) == set(["a", "b"])
    assert not loaded_discrete.fitted()
    assert loaded_discrete.type() == pbn.DiscreteFactorType()

    loaded_new = pickle.loads(new_bytes)
    assert loaded_new.variable() == "c"
    assert set(loaded_new.evidence()) == set(["a", "b"])
    assert not loaded_new.fitted()
    assert type(loaded_new.type()) == NewType
    nn = NewFactor("a", [])
    assert loaded_new.type() == nn.type()

    from pybnesian import GaussianNetwork
    dummy_network = GaussianNetwork(["a", "b", "c", "d"])
    assert type(loaded_new.type().new_factor(dummy_network, "a",
                                             [])) == NewFactor

    loaded_newbis = pickle.loads(newbis_bytes)
    assert loaded_newbis.variable() == "c"
    assert set(loaded_newbis.evidence()) == set(["a", "b"])
    assert not loaded_newbis.fitted()
    assert type(loaded_newbis.type()) == NewType
    nnbis = NewFactorBis("a", [])
    assert loaded_newbis.type() == nnbis.type()
    assert type(loaded_newbis.type().new_factor(dummy_network, "a",
                                                [])) == NewFactorBis

    assert loaded_lg.type() != loaded_ckde.type()
    assert loaded_lg.type() != loaded_discrete.type()
    assert loaded_lg.type() != loaded_new.type()
    assert loaded_ckde.type() != loaded_discrete.type()
    assert loaded_ckde.type() != loaded_new.type()
    assert loaded_discrete.type() != loaded_new.type()
    assert loaded_newbis.type() == loaded_new.type()
예제 #5
0
def test_bn_type():
    g1 = GaussianNetwork(["a", "b", "c", "d"])
    g2 = GaussianNetwork(["a", "b", "c", "d"])
    g3 = GaussianNetwork(["a", "b", "c", "d"])

    assert g1.type() == pbn.GaussianNetworkType()
    assert g1.type() == g2.type()
    assert g1.type() == g3.type()
    assert g2.type() == g3.type()

    s1 = SemiparametricBN(["a", "b", "c", "d"])
    s2 = SemiparametricBN(["a", "b", "c", "d"])
    s3 = SemiparametricBN(["a", "b", "c", "d"])

    assert s1.type() == pbn.SemiparametricBNType()
    assert s1.type() == s2.type()
    assert s1.type() == s3.type()
    assert s2.type() == s3.type()

    k1 = KDENetwork(["a", "b", "c", "d"])
    k2 = KDENetwork(["a", "b", "c", "d"])
    k3 = KDENetwork(["a", "b", "c", "d"])

    assert k1.type() == pbn.KDENetworkType()
    assert k1.type() == k2.type()
    assert k1.type() == k3.type()
    assert k2.type() == k3.type()

    d1 = DiscreteBN(["a", "b", "c", "d"])
    d2 = DiscreteBN(["a", "b", "c", "d"])
    d3 = DiscreteBN(["a", "b", "c", "d"])

    assert d1.type() == pbn.DiscreteBNType()
    assert d1.type() == d2.type()
    assert d1.type() == d3.type()
    assert d2.type() == d3.type()

    assert g1.type() != s1.type()
    assert g1.type() != k1.type()
    assert g1.type() != d1.type()
    assert s1.type() != k1.type()
    assert s1.type() != d1.type()
    assert k1.type() != d1.type()
예제 #6
0
def gaussian_partial_fit_bytes():
    gaussian = GaussianNetwork(["a", "b", "c", "d"], [("a", "b")])
    lg = pbn.LinearGaussianCPD("b", ["a"], [1, 2], 2)
    gaussian.add_cpds([lg])
    gaussian.include_cpd = True
    return pickle.dumps(gaussian)
예제 #7
0
def gaussian_bytes():
    gaussian = GaussianNetwork(["a", "b", "c", "d"], [("a", "b")])
    return pickle.dumps(gaussian)