示例#1
0
def sample_dag(dag, num):

    #zzz this loses disconnected nodes!!!
    # bayesmod = BayesianModel(dag.edges())
    # bayesmod = BayesianModel(dag)
    bayesmod = BayesianModel()
    bayesmod.add_nodes_from(dag.nodes())
    bayesmod.add_edges_from(dag.edges())

    tab_cpds = []
    cards = {node: len(dag.node[node]['cpd']) for node in dag.nodes()}
    for node in dag.nodes():
        parents = dag.predecessors(node)
        cpd = dag.node[node]['cpd']
        if parents:
            parent_cards = [cards[par] for par in parents]
            logging.debug("TablularCPD({}, {}, {}, {}, {})".format(
                node, cards[node], cpd, parents, parent_cards))
            tab_cpds.append(
                TabularCPD(node, cards[node], cpd, parents, parent_cards))
        else:
            logging.debug("TablularCPD({}, {}, {})".format(
                node, cards[node], cpd))
            tab_cpds.append(TabularCPD(node, cards[node], cpd))

    logging.debug("cpds add: {}".format(tab_cpds))

    print "model variables:", bayesmod.nodes()
    for tab_cpd in tab_cpds:
        print "cpd variables:", tab_cpd.variables

    bayesmod.add_cpds(*tab_cpds)

    logging.debug("cpds get: {}".format(bayesmod.get_cpds()))
    inference = BayesianModelSampling(bayesmod)

    logging.debug("generating data")
    recs = inference.forward_sample(size=num, return_type='recarray')
    return recs
示例#2
0
from pgmpy.models.BayesianModel import BayesianModel
from pgmpy.factors.discrete import TabularCPD
from pgmpy.sampling import BayesianModelSampling
student = BayesianModel([('diff', 'grade'), ('intel', 'grade')])
cpd_d = TabularCPD('diff', 2, [[0.6], [0.4]])
cpd_i = TabularCPD('intel', 2, [[0.7], [0.3]])
cpd_g = TabularCPD('grade', 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25,0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],['intel', 'diff'], [2, 2])
student.add_cpds(cpd_d, cpd_i, cpd_g)
inference = BayesianModelSampling(student)
print inference.forward_sample(size=3, return_type='recarray')
from pgmpy.sampling import BayesianModelSampling
from pgmpy.inference import VariableElimination

burglary = BayesianModel([('Burglary', 'Alarm'), ('Earthquake', 'Alarm'),
                          ('Alarm', 'Johncalls'), ('Alarm', 'Marycalls')])

cpd_burgary = TabularCPD('Burglary', 2, [[0.001], [0.999]])
cpd_earthquake = TabularCPD('Earthquake', 2, [[0.002], [0.998]])
cpd_alarm = TabularCPD('Alarm', 2,
                       [[0.99, 0.71, 0.06, 0.05], [0.01, 0.29, 0.94, 0.95]],
                       ['Burglary', 'Earthquake'], [2, 2])
cpd_johncall = TabularCPD('Johncalls', 2, [[0.95, 0.1], [0.05, 0.90]],
                          ['Alarm'], [2])
cpd_marrycall = TabularCPD('Marycalls', 2, [[0.99, 0.30], [0.01, 0.70]],
                           ['Alarm'], [2])
burglary.add_cpds(cpd_burgary, cpd_earthquake, cpd_alarm, cpd_johncall,
                  cpd_marrycall)

for cpd in burglary.get_cpds():
    print(cpd)
#######################################################################
"""inference using variable elimination"""

inference = VariableElimination(burglary)
alarm = inference.query(['Alarm'], {'Johncalls': 0})
print(alarm['Alarm'])

##############################################################
"""inference using likelihood weighting sampling"""

inference = BayesianModelSampling(burglary)
evidence = [State('Johncalls', 0)]
示例#4
0
    parentsCardList = {}
    for node in nodes:
        if parents.has_key(node):
            tempParentCardList = []
            for parent in parents[node]:
                tempParentCardList.append(len(cpds[parent]))
            parentsCardList[node] = tempParentCardList

    print("Adding Tabular CPDs to model")

    for node in nodes:
        try:
            if parents.has_key(node):
                model.add_cpds(
                    TabularCPD(node, variableCard[node], cpds[node],
                               parents[node], parentsCardList[node]))
            else:
                model.add_cpds(TabularCPD(node, variableCard[node],
                                          cpds[node]))
        except Exception as e:
            tempException2 = 0

    #print("Tabular cpds added to model")

    print "Creating samples using Bayesian model forward Sampling"

    inference = BayesianModelSampling(model)
    normalSamples = inference.forward_sample(size=5000,
                                             return_type='dataframe')
    #print "length ", normalSamples.shape
                   2, [[0.3, 0.4], [0.7, 0.6]],
                   evidence=['A'],
                   evidence_card=[2])
cpd_d = TabularCPD('D',
                   3, [[0.5, 0.3, 0.1], [0.4, 0.4, 0.8], [0.1, 0.3, 0.1]],
                   evidence=['B'],
                   evidence_card=[3])
cpd_e = TabularCPD('E',
                   2, [[0.3, 0.5, 0.2], [0.7, 0.5, 0.8]],
                   evidence=['B'],
                   evidence_card=[3])
cpd_f = TabularCPD('F',
                   3, [[0.3, 0.6], [0.5, 0.2], [0.2, 0.2]],
                   evidence=['C'],
                   evidence_card=[2])
model.add_cpds(cpd_a, cpd_b, cpd_c, cpd_d, cpd_e, cpd_f)

from pgmpy.sampling import BayesianModelSampling

# sample data from BN
inference = BayesianModelSampling(model)
df_data = inference.forward_sample(size=10000, return_type='dataframe')
print(df_data)

from pgmpy.estimators import TreeSearch

# learn graph structure
est = TreeSearch(df_data, root_node="A")
dag = est.estimate(estimator_type="chow-liu")
nx.draw_circular(dag,
                 with_labels=True,