-1

I am completely new to the field of Bayesian Networks. For my project, I need to check All the possible d separation conditions existing in a 7 node dag and for that I am looking for some good python code. My knowledge in programming is limited ( a bit of numerical analysis and data structures; but I understand d separation, e separation and other concepts in a dag quite well).

It would be really very helpful if someone could point out where to look for such a specific code. Please note that I want a python codes that checks for All the conditional independences following from d separation in a 7 node dag.

I would be happier with an algorithm checking whether each path is blocked or not etc rather than one built on semi graphoid axioms.

I don't know exactly where should I look or to whom should I ask, so any help would be greatly appreciated.

Shashaank
  • 99
  • 3

2 Answers2

2

I guess you understand that your demand is a very large list. Even if we only consider d-separation between only 2 variables (conditionned by a set of nodes).

Anyway, you can do that quite easily with pyAgrum (https://agrum.org)

import itertools
import pyAgrum as gum

# create a BN
bn=gum.fastBN("A->B<-C->D->E->F;B->E<-G");

# print the indepency model by testing d-separations

# how to iterate for each subset of an interable
def powerset(iterable):
    """
    powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
    """
    xs = list(iterable)
    # note we return an iterator rather than a list
    return itertools.chain.from_iterable(itertools.combinations(xs,n) for n in range(len(xs)+1))

# testing every d-separation
for i in bn.names():
    for j in bn.names()-{i}:
        for k in powerset(bn.names()-{i,j}):
            if bn.isIndependent(i,j,k):
                print(f"{i} indep {j} given {k}")

And the result (in a notebook) :

using pyAgrum for list of d-separations

  • Kindly update your answer in order to also include the first code snippet as *text*. – desertnaut Mar 20 '21 at 22:33
  • I added the 2 lines to create the Bayesian network. Since this snippet is not necessarily intended for notebook, I did not put the 2 lines that display it as a directed graph (in a notebook). Thanks! – Pierre-Henri Wuillemin Mar 22 '21 at 06:39
0

I made a package that accurately implements the function you want. What a coincidence!

pip install cdpi  

from cdpi import pattern
ptn = pattern()
ptn.add_edges([
    ('A', 'B'),
    ('C', 'B'),
    ('B', 'D'),
    ('E', 'D'),
    ('F', 'D'),
    ('D', 'G')
])
ptn.draw()

[out] Graph visualization is also supported! but 10 reputations limit... I can't upload image...


ds = ptn.get_all_d_separation() # ds is 2-d dictionary
ds['F']['A'] #example

[out]

{(),
 ('B',),
 ('B', 'C'),
 ('B', 'D'),
 ('B', 'D', 'C'),
 ('B', 'E'),
 ('B', 'E', 'C'),
 ('B', 'E', 'D'),
 ('B', 'E', 'D', 'C'),
 ('B', 'E', 'D', 'G'),
 ('B', 'E', 'G'),
 ('B', 'E', 'G', 'C'),
 ('B', 'G'),
 ('B', 'G', 'C'),
 ('C',),
 ('E',),
 ('E', 'C'),
 ('G', 'B', 'D'),
 ('G', 'B', 'D', 'C'),
 ('G', 'E', 'C', 'B', 'D')}

If you want to see entire code or how it works, see d-separation part of here

Robster
  • 1
  • 1