Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bayesian Network Models in PyMC3 and NetworkX
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tbsexton

This comment has been minimized.

Copy link
Owner

tbsexton commented Feb 17, 2017

Updated to use tabular look-up for pre-defined categorical composition nodes. Removed need for theano.tensor.switch() in most cases, using a shared numpy slice on the table instead. Runs much faster.

Still uses T.switch() for splitting the continuous node C3

@tbsexton

This comment has been minimized.

Copy link
Owner

tbsexton commented Mar 21, 2017

Added example of programmatically instantiating the PyMC3 random variable objects using NetworkX dicts. In theory, one could now "loop-over" an existing network and build up a pymc3 model to do inference.

@madHatter106

This comment has been minimized.

Copy link

madHatter106 commented Mar 20, 2018

This is really cool. Thanks!

@Nilavro

This comment has been minimized.

Copy link

Nilavro commented Sep 16, 2018

How do you make a query ? For example P(C3 |C2 = 12)

@dynaryu

This comment has been minimized.

Copy link

dynaryu commented Nov 13, 2018

I've got the following error message when I tried the Networkx with PyMC3:


TypeError Traceback (most recent call last)
in
8 BN.node['D2']['dist_obj'] = pm.Categorical('D2',p=BN.node['D2']['prob'])
9
---> 10 BN.node['C1']['dist_obj'] = pm.Normal('C1', mu=10+4*gpm(BN,'C1'),
11 tau = (1./2)*2)
12 BN.node['C2']['dist_obj'] = pm.Normal('C2', mu=6+2
(gpm(BN,'C2'))**2,

in gpm(BN, node, num)
1 # utily to quickly access the parent nodes' pymc3 objects
2 def gpm(BN, node, num=0):
----> 3 return BN.node[BN.predecessors(node)[num]]['dist_obj']
4
5 with pm.Model() as mod2:

TypeError: 'dict_keyiterator' object is not subscriptable

I'm currently using networkx 2.2, pymc3 3.5, and python 3.5 on OS X

@Fangwq

This comment has been minimized.

Copy link

Fangwq commented Jan 2, 2019

I got the following error message:

Traceback (most recent call last):
File "./pmml_test.py", line 126, in
BN.node['C1']['dist_obj'] = pm.Normal('C1', mu=10+4*gpm(BN,'C1'),tau = (1./2)**2)
File "./pmml_test.py", line 118, in gpm
return BN.node[BN.predecessors(node)[num]]['dist_obj']
TypeError: 'dictionary-keyiterator' object has no attribute 'getitem'

I'm currently using networkx 2.2, pymc3 3.5, and python 2.7 on OS X. Can you help me?

@DanielaLaura

This comment has been minimized.

Copy link

DanielaLaura commented Jan 4, 2019

I got the following error message:

Traceback (most recent call last):
File "./pmml_test.py", line 126, in
BN.node['C1']['dist_obj'] = pm.Normal('C1', mu=10+4*gpm(BN,'C1'),tau = (1./2)**2)
File "./pmml_test.py", line 118, in gpm
return BN.node[BN.predecessors(node)[num]]['dist_obj']
TypeError: 'dictionary-keyiterator' object has no attribute 'getitem'

I'm currently using networkx 2.2, pymc3 3.5, and python 2.7 on OS X. Can you help me?

Here, he is using network 1.x. Here is the Migration guide :https://networkx.github.io/documentation/stable/release/migration_guide_from_1.x_to_2.0.html

In :
"# utily to quickly access the parent nodes' pymc3 objects
def gpm(BN, node, num=0):
return BN.node[BN.predecessors(node)[num]]['dist_obj'] "
you have to change BN.predecessors(node) to list(BN.predecessors(node)).

That is why you might get that keyiterator error.

Edit: aslo, you might want to change BN.node to BN.nodes
Hope it helps!

@DanielaLaura

This comment has been minimized.

Copy link

DanielaLaura commented Jan 4, 2019

I've got the following error message when I tried the Networkx with PyMC3:

TypeError Traceback (most recent call last)
in
8 BN.node['D2']['dist_obj'] = pm.Categorical('D2',p=BN.node['D2']['prob'])
9
---> 10 BN.node['C1']['dist_obj'] = pm.Normal('C1', mu=10+4gpm(BN,'C1'),
11 tau = (1./2)
2) 12 BN.node['C2']['dist_obj'] = pm.Normal('C2', mu=6+2(gpm(BN,'C2'))**2,

in gpm(BN, node, num)
1 # utily to quickly access the parent nodes' pymc3 objects
2 def gpm(BN, node, num=0):
----> 3 return BN.node[BN.predecessors(node)[num]]['dist_obj']
4
5 with pm.Model() as mod2:

TypeError: 'dict_keyiterator' object is not subscriptable

I'm currently using networkx 2.2, pymc3 3.5, and python 3.5 on OS X

Here, he is using network 1.x. Here is the Migration guide :https://networkx.github.io/documentation/stable/release/migration_guide_from_1.x_to_2.0.html

In :
"# utily to quickly access the parent nodes' pymc3 objects
def gpm(BN, node, num=0):
return BN.node[BN.predecessors(node)[num]]['dist_obj'] "
you have to change BN.predecessors(node) to list(BN.predecessors(node)).

That is why you might get that keyiterator error.

Edit: aslo, you might want to change BN.node to BN.nodes
Hope it helps!

@tomkovna

This comment has been minimized.

Copy link

tomkovna commented Jan 7, 2019

How do you make a query such as: P(C1_a<C1<C1_b | C3_a<C3<C3_b, D4 = 1)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment