Esempio n. 1
0
def one_predictive_var():
    Y = RandomVar('Y', 2)

    X1 = RandomVar('X1', 2)
    X2 = RandomVar('X2', 2)
    X3 = RandomVar('X3', 2)

    f_X1_Y = CPD([X1, Y], [1.0, 0.0, 0.0, 1.0])
    f_X2_Y = CPD([X2, Y], [0.5, 0.5, 0.5, 0.5])
    f_X3_Y = CPD([X3, Y], [0.5, 0.5, 0.5, 0.5])

    f_Y = CPD([Y], [0.5, 0.5])

    bn = BayesianNetwork([f_Y, f_X1_Y, f_X2_Y, f_X3_Y])

    # Training the model
    fs = ForwardSampler(bn)
    fs.sample(1000)
    scope, X = fs.samples_to_matrix()

    y = X[:, -1]
    X = X[:, 0:-1]

    nb = NaiveBayes()
    nb.fit(X, y)

    # Evaluating the model

    fs = ForwardSampler(bn)
    fs.sample(10)
    _, X = fs.samples_to_matrix()

    print(nb.score(X[:, 0:-1], X[:, -1]))
    print(nb.predict_proba(X[:, 0:-1]))
Esempio n. 2
0
def main():
    parents = {'alice': [], 'bob': [], 'eve': ['bob', 'alice']}
    allele_freqs = [0.9, 0.1]
    prob_trait_genotype = [[0.0, 0.0], [0.0, 1.0]]

    bn = build_genetic_network(parents, allele_freqs, prob_trait_genotype)

    # Examples
    alice_trait = RandomVar('alice_trait', 2)
    bob_trait = RandomVar('bob_trait', 2)
    eve_trait = RandomVar('eve_trait', 2)

    #    alice_allele_1 = RandomVar('alice_allele_1', 2)
    #    alice_allele_2 = RandomVar('alice_allele_2', 2)
    #
    #    bob_allele_1 = RandomVar('bob_allele_1', 2)
    #    bob_allele_2 = RandomVar('bob_allele_2', 2)
    #
    #    eve_allele_1 = RandomVar('eve_allele_1', 2)
    #    eve_allele_2 = RandomVar('eve_allele_2', 2)

    ve = JointMarginalization(bn)

    print(ve.posterior([eve_trait]))
    print(ve.posterior([eve_trait], [(bob_trait, 1)]))
    print(ve.posterior([alice_trait, bob_trait], [(eve_trait, 1)]))

    fs = ForwardSampler(bn)
    fs.sample(1000)
    print(
        fs.posterior([(alice_trait, 0), (bob_trait, 0),
                      (eve_trait, 1)]) / fs.posterior([(eve_trait, 1)]))

    print(bn.graph())
Esempio n. 3
0
def gn():
    parents = {'alice': [], 'bob': [], 'eve': ['bob', 'alice']}
    allele_freqs = [0.9, 0.1]
    prob_trait_genotype = [[0.0, 0.0], [0.0, 1.0]]

    bn = genetic_network.build_genetic_network(parents, allele_freqs,
                                               prob_trait_genotype)

    graph = bn.graph()

    fs = ForwardSampler(bn)
    fs.sample(20000)
    scope, X = fs.samples_to_matrix()

    score_l = LikelihoodScore(scope).fit(X, graph).score
    print(score_l)
    score_bic = BICScore(scope).fit(X, graph).score
    print(score_bic)
    score_b = BayesianScore(scope).fit(X, graph).score
    print(score_b)

    #    scorer = LikelihoodScore(scope)
    #    scorer = BayesianScore(scope)
    scorer = BICScore(scope)
    best_graph, best_score = restarting_local_search(X,
                                                     scope,
                                                     scorer,
                                                     restarts=1,
                                                     iterations=100,
                                                     epsilon=0.1,
                                                     verbose=1)

    print(best_graph)
    print(best_score)
Esempio n. 4
0
def main():
    x1 = RandomVar('X1', 2)
    x2 = RandomVar('X2', 2)
    x3 = RandomVar('X3', 2)

    fx1 = CPD([x1], [0.11, 0.89])
    fx2_x1 = CPD([x2, x1], [0.59, 0.22, 0.41, 0.78])
    fx3_x2 = CPD([x3, x2], [0.39, 0.06, 0.61, 0.94])

    bn = BayesianNetwork([fx1, fx2_x1, fx3_x2])
    #    mn = MarkovNetwork([fx1, fx2_x1, fx3_x2])

    ve = VariableElimination(bn)
    jm = JointMarginalization(bn)

    print(ve.posterior([x1, x2], [(x3, 0)]))
    print(jm.posterior([x1, x2], [(x3, 0)]))

    print(ve.posterior([x1, x2, x3]))
    print(jm.posterior([x1, x2, x3]))

    print(ve.maximum_a_posteriori(evidence=[(x3, 0)]))
    print(jm.maximum_a_posteriori([x1, x2], [(x3, 0)]))

    fs = ForwardSampler(bn)
    fs.sample(10000)

    for c in itertools.product(range(2), repeat=3):
        print('{0}: {1}'.format(c, fs.posterior(zip([x1, x2, x3], c))))

    px3_0 = fs.posterior([(x3, 0)])
    for c in itertools.product(range(2), repeat=2):
        assg = list(zip([x1, x2], c)) + [(x3, 0)]

        print('{0}: {1}'.format(c, fs.posterior(assg) / px3_0))

    gs = GibbsSampler(bn)
    gs.sample(burn_in=1000, n=2000)

    for c in itertools.product(range(2), repeat=3):
        print('{0}: {1}'.format(c, gs.posterior(zip([x1, x2, x3], c))))

    gs.reset()
    gs.sample(burn_in=1000, n=1000, evidence=[(x3, 0)])

    for c in itertools.product(range(2), repeat=2):
        print('{0}: {1}'.format(c, gs.posterior(zip([x1, x2], c))))
Esempio n. 5
0
def earthquake():
    B = RandomVar('B', 2)
    E = RandomVar('E', 2)
    A = RandomVar('A', 2)
    R = RandomVar('R', 2)

    a_be = CPD([A, B, E],
               [0.999, 0.01, 0.01, 0.0001, 0.001, 0.99, 0.99, 0.9999])
    r_e = CPD([R, E], [1.0, 0.0, 0.0, 1.0])
    b = CPD([B], [0.99, 0.01])
    e = CPD([E], [0.999, 0.001])

    bn = BayesianNetwork([a_be, r_e, b, e])

    fs = ForwardSampler(bn)
    fs.sample(1000)
    scope, X = fs.samples_to_matrix()

    graph = bn.graph()
    #    graph = {B : set(), E: set(), A: set(), R: set()}

    score_l = LikelihoodScore(scope).fit(X, graph).score
    print(score_l)
    score_bic = BICScore(scope).fit(X, graph).score
    print(score_bic)
    score_b = BayesianScore(scope).fit(X, graph).score
    print(score_b)

    #    scorer = LikelihoodScore(scope)
    #    scorer = BICScore(scope)
    scorer = BayesianScore(scope)
    best_graph, best_score = restarting_local_search(X,
                                                     scope,
                                                     scorer,
                                                     restarts=1,
                                                     iterations=100,
                                                     epsilon=0.2,
                                                     verbose=1)
    print('Best:')
    print(best_score)
    print(best_graph)
Esempio n. 6
0
def main():
    B = RandomVar('B', 2)
    E = RandomVar('E', 2)
    A = RandomVar('A', 2)
    R = RandomVar('R', 2)

    a_be = CPD([A, B, E],
               [0.999, 0.01, 0.01, 0.0001, 0.001, 0.99, 0.99, 0.9999])
    r_e = CPD([R, E], [1.0, 0.0, 0.0, 1.0])
    b = CPD([B], [0.99, 0.01])
    e = CPD([E], [0.999, 0.001])

    bn = BayesianNetwork([a_be, r_e, b, e])

    ve = VariableElimination(bn)
    jm = JointMarginalization(bn)

    print(ve.posterior([B, E, A, R]) == jm.posterior([B, E, A, R]))

    fs = ForwardSampler(bn)
    fs.sample(1000)
Esempio n. 7
0
def simple_chain():
    x1 = RandomVar('X1', 2)
    x2 = RandomVar('X2', 2)
    x3 = RandomVar('X3', 2)

    fx1 = CPD([x1], [0.11, 0.89])
    fx2_x1 = CPD([x2, x1], [0.59, 0.22, 0.41, 0.78])
    fx3_x2 = CPD([x3, x2], [0.39, 0.06, 0.61, 0.94])

    bn = BayesianNetwork([fx1, fx2_x1, fx3_x2])

    fs = ForwardSampler(bn)
    fs.sample(2000)

    scope, X = fs.samples_to_matrix()

    graph = bn.graph()
    #    graph = {x1 : set(), x2: set(), x3: set()}

    score_l = LikelihoodScore(scope).fit(X, graph).score
    print(score_l)
    score_bic = BICScore(scope).fit(X, graph).score
    print(score_bic)
    score_b = BayesianScore(scope).fit(X, graph).score
    print(score_b)

    #    scorer = LikelihoodScore(scope)
    scorer = BICScore(scope)
    #    scorer = BayesianScore(scope)
    best_graph, best_score = restarting_local_search(X,
                                                     scope,
                                                     scorer,
                                                     restarts=5,
                                                     iterations=50,
                                                     epsilon=0.2,
                                                     verbose=1)
    print('Best:')
    print(best_score)
    print(best_graph)
Esempio n. 8
0
def traffic():
    A = RandomVar('A', 2)
    T = RandomVar('T', 2)
    P = RandomVar('P', 2)

    fP = CPD([P], [0.99, 0.01])
    fA = CPD([A], [0.9, 0.1])

    fT_AP = CPD([T, P, A], [0.9, 0.5, 0.4, 0.1, 0.1, 0.5, 0.6, 0.9])

    bn = BayesianNetwork([fP, fA, fT_AP])
    #    print(bn)

    fs = ForwardSampler(bn)
    fs.sample(2000)
    scope, X = fs.samples_to_matrix()

    graph = bn.graph()

    score_l = LikelihoodScore(scope).fit(X, graph).score
    print(score_l)
    score_bic = BICScore(scope).fit(X, graph).score
    print(score_bic)
    score_b = BayesianScore(scope).fit(X, graph).score
    print(score_b)

    #    scorer = LikelihoodScore(scope)
    scorer = BICScore(scope)
    #    scorer = BayesianScore(scope)
    best_graph, best_score = restarting_local_search(X,
                                                     scope,
                                                     scorer,
                                                     restarts=5,
                                                     iterations=50,
                                                     epsilon=0.2,
                                                     verbose=1)
    print('Best:')
    print(best_score)
    print(best_graph)
Esempio n. 9
0
def traffic():
    A = RandomVar('A', 2)
    T = RandomVar('T', 2)
    P = RandomVar('P', 2)

    fP = CPD([P], [0.99, 0.01])
    fA = CPD([A], [0.9, 0.1])

    fT_AP = CPD([T, P, A], [0.9, 0.5, 0.4, 0.1, 0.1, 0.5, 0.6, 0.9])

    bn = BayesianNetwork([fP, fA, fT_AP])
    print(bn)

    fs = ForwardSampler(bn)
    fs.sample(1000)
    scope, X = fs.samples_to_matrix()

    mle = MaximumLikelihood(scope)
    print(mle.fit_predict(X, bn.graph()))

    ud = UniformDirichlet(scope, alpha=1.0)
    print(ud.fit_predict(X, bn.graph()))
Esempio n. 10
0
def simple_chain():
    x1 = RandomVar('X1', 2)
    x2 = RandomVar('X2', 2)
    x3 = RandomVar('X3', 2)

    fx1 = CPD([x1], [0.11, 0.89])
    fx2_x1 = CPD([x2, x1], [0.59, 0.22, 0.41, 0.78])
    fx3_x2 = CPD([x3, x2], [0.39, 0.06, 0.61, 0.94])

    bn = BayesianNetwork([fx1, fx2_x1, fx3_x2])
    graph = bn.graph()
    print(bn)

    fs = ForwardSampler(bn)
    fs.sample(1000)
    scope, X = fs.samples_to_matrix()

    mle = MaximumLikelihood(scope)
    print(mle.fit_predict(X, graph))

    ud = UniformDirichlet(scope, alpha=1.0)
    print(ud.fit_predict(X, graph))
Esempio n. 11
0
def earthquake():
    B = RandomVar('B', 2)
    E = RandomVar('E', 2)
    A = RandomVar('A', 2)
    R = RandomVar('R', 2)

    a_be = CPD([A, B, E],
               [0.999, 0.01, 0.01, 0.0001, 0.001, 0.99, 0.99, 0.9999])
    r_e = CPD([R, E], [1.0, 0.0, 0.0, 1.0])
    b = CPD([B], [0.99, 0.01])
    e = CPD([E], [0.999, 0.001])

    bn = BayesianNetwork([a_be, r_e, b, e])
    print(bn)

    fs = ForwardSampler(bn)
    fs.sample(1000)
    scope, X = fs.samples_to_matrix()

    mle = MaximumLikelihood(scope)
    print(mle.fit_predict(X, bn.graph()))

    ud = UniformDirichlet(scope, alpha=1.0)
    print(ud.fit_predict(X, bn.graph()))