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 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_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'})
b0r ~ norm(0, .01) ageA # ~ norm(20, 3) ageB ~ norm(30, 2) ps ~ beta(5, 6) sexA ~ cat({'m': ps, 'f': 1-ps}) muA = b0 + b0r + b1*ageA bmiA ~ norm(muA, sd) sdB = sd * 0.5 muB = b0 + b0r + b1*ageB bmiB ~ norm(muB, sdB) } ''' bn = dag.bayes_net_from_script(scr) root = dag.NodeSet('country') node_area = root.new_child('area', as_fixed=['b0r', 'ps'], as_floating=['foodstore']) node_area.new_child('agA', as_fixed=['ageA', 'sexA'], as_floating=['bmiA']) node_area.new_child('agB', as_fixed=['ageB'], as_floating=['bmiB']) sc = dag.as_simulation_core(bn, root) root.print() root.print_samplers() pc = sc.generate('Taiwan', {'sd': 1}) print(pc.list_actors()) pc_taipei = pc.breed('Taipei', 'area') pc_taipei.breed('A1', 'agA', {'ageA': 5})
def fn_mean(sim, data): return -abs(sim - data['X']) d = {'N': 10, 'K': 10, 'X': 200} 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=['p']) ns.new_child('ag', as_floating=['x']) sm = dag.as_simulation_core(bn, ns) sm.deep_print() sdm = dag.as_simulation_data_model(sm, d, fn_sim, fn_mean) fit = dag.fitting.GA(sdm) fit.fit(100) print(fit.BestFit)