def setUp(self): skel = GraphSkeleton() skel.load("unittestdict.txt") skel.toporder() nodedata = NodeData.load("unittestdict.txt") self.bn = DiscreteBayesianNetwork(nodedata) agg = SampleAggregator() agg.aggregate(self.bn.randomsample(50)) self.rseq = agg.seq self.ravg = agg.avg self.fn = TableCPDFactorization(self.bn) evidence = dict(Letter='weak') agg.aggregate(self.fn.gibbssample(evidence, 51)) self.gseq = agg.seq self.gavg = agg.avg
def GenProbTable(network): #Generates Generic probability table for a Bayesian Network. #Used for initialization of Dyn. Disc. BN agg = SampleAggregator() r = agg.aggregate(network.randomsample(5000)) return r
nd = NodeData() skel = GraphSkeleton() nd.load("../tests/unittestdict.txt") skel.load("../tests/unittestdict.txt") # topologically order graphskeleton skel.toporder() # load bayesian network bn = DiscreteBayesianNetwork(skel, nd) # build aggregator agg = SampleAggregator() # average samples result = agg.aggregate(bn.randomsample(10)) # output - toggle comment to see #print json.dumps(result, indent=2) # (8) -------------------------------------------------------------------------- # Learn the CPDs of a discrete Bayesian network, given data and a structure: # say I have some data data = bn.randomsample(200) # and a graphskeleton skel = GraphSkeleton() skel.load("../tests/unittestdict.txt") # instantiate my learner
def inference(bn, evidence): fn = TableCPDFactorization(bn) result = fn.gibbssample(evidence, GIBBS_ITERATIONS) agg = SampleAggregator() result = agg.aggregate(result) return json.dumps(result, indent=2)