def setUp(self): self.BN1 = dag.bayes_net_from_script(''' PCore A { A = 5 B = A * 4 C = B + B2 D = pow(C) } ''') self.BN2 = dag.bayes_net_from_script(''' PCore B2 { C = B D = pow(C) } ''')
def test_clone(self): script = ''' PCore Regression { x = 1 y = x + 1 z = y + 1 } ''' bn = dag.bayes_net_from_script(script) na = dag.NodeSet('a') nb = na.new_child('b', as_fixed=['x']) nb.new_child('c', as_fixed=['z']) sc = dag.as_simulation_core(bn, na) pc_a = sc.generate('A') pc_b = pc_a.breed('B', 'b') pc_c = pc_b.breed('C', 'c') pc_aa = pc_a.clone(copy_sc=True, include_children=True) pc_cc = pc_aa.find_descendant('B@C') self.assertEqual(pc_c['z'], 3) self.assertEqual(pc_cc['z'], 3) pc_aa.impulse({'x': 5}) self.assertEqual(pc_c['z'], 3) self.assertEqual(pc_cc['z'], 7) pc_a.impulse({'x': 7}) self.assertEqual(pc_c['z'], 9) self.assertEqual(pc_cc['z'], 7)
def quick_build_parameter_core(script): bn = dag.bayes_net_from_script(script) flt = [d for d in bn.Leaves if bn.is_rv(d)] ns = NodeSet('Root', as_floating=flt) sm = as_simulation_core(bn, ns) return sm.generate('')
def test_div(self): bn = dag.bayes_net_from_script(script_betabin2) ns = dag.NodeSet('root') ns.new_child('a', as_floating=['x1']) ns.new_child('b', as_floating=['x2']) sc = dag.as_simulation_core(bn, ns) pc = sc.generate("T4") pc_a = pc.breed('A', 'a') pc_b = pc.breed('B', 'b') self.assertIn('x1', pc_a.get_samplers()) self.assertIn('x2', pc_b.get_samplers())
import epidag as dag __author__ = 'TimeWz667' script = ''' PCore BetaBin { al = 1 be = 1 p ~ beta(al, be) x ~ binom(5, p) } ''' bn = dag.bayes_net_from_script(script) ns = dag.NodeSet('root', as_fixed=['al'], as_floating=['p']) ns.new_child('ag', as_floating=['x']) sc = dag.as_simulation_core(bn, ns) sc.deep_print() pc = sc.generate('a') p = pc.breed('x', 'ag') print(p.get_sampler('p'))
def test_no_exo(self): bn = dag.bayes_net_from_script(script_betabin) sc = dag.as_simulation_core(bn) pc = sc.generate("T3") with self.assertRaises(KeyError): pc.get_sampler('x')()
def test_random(self): bn = dag.bayes_net_from_script(script_betabin) ns = dag.NodeSet('Root', as_floating=['p']) sc = dag.as_simulation_core(bn, ns) pc = sc.generate("T2", {'n': 10}) self.assertSetEqual(set(pc.get_samplers().keys()), {'x', 'p'})
def test_simple(self): bn = dag.bayes_net_from_script(script_betabin) sc = dag.as_simulation_core(bn) pc = sc.generate("T1", {'n': 10}) self.assertEqual(pc['n'], 10)
def setUp(self): self.BN = dag.bayes_net_from_script(scr) self.DM = BinBeta(self.BN, data) self.Logger = logging.getLogger(__name__)
BayesianModel.__init__(self, bn, pars=['prob']) self.Data = data @property def has_exact_likelihood(self): return True def evaluate_distance(self, pars): di = 0 for d in self.Data: pars.update(d) di += self.BN['x'].evaluate(pars) return di def evaluate_likelihood(self, pars): return -self.evaluate_distance(pars) data = [{'id': 1, 'n': 10, 'x': 4}, {'id': 2, 'n': 20, 'x': 7}] bn = bayes_net_from_script(''' PCore test { prob ~ beta(1, 1) x ~ binom(n, prob) } ''') model = BinBeta(bn, data) p = model.sample_prior() print(p)
if self.FloatingNodes: print('{}|-Floating {}'.format(ind, list(self.FloatingNodes))) if self.ExoNodes: print('{}|-Exo {}'.format(ind, list(self.ExoNodes))) if self.ListeningNodes: print('{}|-Listening {}'.format(ind, list(self.ListeningNodes))) for ch in self.__children.values(): ch.print(i + 2) if __name__ == '__main__': from epidag import bayes_net_from_script bn1 = bayes_net_from_script(''' PCore Test { a = 1 b = a + 3 c = b * 2 d ~ binom(b, 0. 5) e = d + c } ''') ndr = NodeSet('root', as_fixed=['a']) nds = ndr.new_child('med', as_fixed=['b'], as_floating=['d']) nde = nds.new_child('leaf', as_fixed=['e']) ndr.inject_bn(bn1) ndr.print()