示例#1
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()))
示例#2
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()))
示例#3
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()))
示例#4
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()))
示例#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)
示例#6
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)
示例#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])
    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))
示例#8
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)
示例#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(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)
示例#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))
示例#11
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)
示例#12
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)
示例#13
0
文件: die.py 项目: paulorauber/pgm
def die():
    # Parameters
    # d1_ = [0.2, 0.0, 0.5, 0.1, 0.1, 0.1]
    # d2_ = [0.2, 0.3, 0.1, 0.05, 0.05, 0.3]
    d1_ = [0.1, 0.9]
    d2_ = [0.6, 0.4]
    n_samples = 5000

    n_iterations = 10
    n_restarts = 2
    verbose = 2

    # Model creation
    if len(d1_) != len(d2_):
        raise Exception('The die should have the same cardinality')

    h = RandomVar('h', 2)
    o1 = RandomVar('o1', len(d1_))
    o2 = RandomVar('o2', len(d2_))

    f_h = CPD([h], [0.5, 0.5])
    f_o1_h = Factor([o1, h])
    f_o2_h = Factor([o2, h])

    for i in range(len(f_o1_h.values)):
        o_, h_ = f_o1_h.itoa(i)
        f_o1_h.values[i] = d1_[o_] if h_ == 0 else d2_[o_]
        f_o2_h.values[i] = d2_[o_] if h_ == 0 else d1_[o_]

    f_o1_h = CPD(f_o1_h.scope, f_o1_h.values)
    f_o2_h = CPD(f_o2_h.scope, f_o2_h.values)

    bn = BayesianNetwork([f_h, f_o1_h, f_o2_h])

    # Sampling from true model
    fs = ForwardSampler(bn)
    fs.sample(n_samples)
    scope, X = fs.samples_to_matrix()

    em = ExpectationMaximization(scope, known_cpds=[f_h],
                                 n_iterations=n_iterations,
                                 n_restarts=n_restarts, alpha=10.0,
                                 verbose=verbose)

    print('True log-likelihood (no missing variables):')
    print(em.log_likelihood(X, bn))

    print('Maximum log-likelihood (no missing variables):')
    ls = LikelihoodScore(scope)
    ls.fit(X, bn.graph())
    print(ls.score)

    # Hiding variable
    X[:, scope.index(h)] = -1

    print('True log-likelihood (missing variables):')
    print(em.log_likelihood(X, bn))

    bn_pred = em.fit_predict(X, bn.graph())
    print('Best log-likelihood (missing variables)')
    print(em.log_likelihood(X, bn_pred))

    # Estimation results
    print('Results:')
    f_o1_h = [f for f in bn_pred.factors if f.scope[0] == o1][0]
    f_o2_h = [f for f in bn_pred.factors if f.scope[0] == o2][0]

    d = np.zeros(o1.k)
    d1 = np.zeros(o1.k)
    d2 = np.zeros(o1.k)

    with printoptions(precision=3):
        print('d1: {0}'.format(d1_))

        for i in range(o1.k):
            d[i] = f_o1_h.values[f_o1_h.atoi([i, 0])]
        print('d1 according to o1: {0}'.format(d))
        d1 += d

        for i in range(o2.k):
            d[i] = f_o2_h.values[f_o2_h.atoi([i, 1])]
        print('d1 according to o2: {0}'.format(d))
        d1 += d

        print('d2: {0}'.format(d2_))
        for i in range(o1.k):
            d[i] = f_o1_h.values[f_o1_h.atoi([i, 1])]
        print('d2 according to o1: {0}'.format(d))
        d2 += d

        for i in range(o2.k):
            d[i] = f_o2_h.values[f_o2_h.atoi([i, 0])]
        print('d2 according to o2: {0}'.format(d))
        d2 += d

        print('Average estimate:')
        print('d1: {0}'.format(d1/2.))
        print('d2: {0}'.format(d2/2.))