Beispiel #1
0
    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)
Beispiel #2
0
    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'))
Beispiel #4
0
 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')()
Beispiel #5
0
 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'})
Beispiel #6
0
 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)
    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})
pc_taipei.breed('A2', 'agA', {'ageA': 4})
pc_taipei.breed('B1', 'agB')
print(pc_taipei.list_actors())
b2 = pc_taipei.breed('B2', 'agB', exo={'sdB': 0.5})
b2.get_sibling('B3')
pc.deep_print()
print(b2.list_actors())