Example #1
0
 def runTest(self):
     # construct factors of various sizes with no data
     for sz in xrange(6):
         vars = ['V'+str(i) for i in xrange(sz)]
         vals = dict([(v,[0,1]) for v in vars])
         data = (vars,vals,vars,[])
         for v_on in subsetn(vars, sz):
             inst = []
             for v in vars:
                 if v in v_on:
                     inst.append(1)
                 else:
                     inst.append(0)
             data[3].append(tuple(inst+[0]))
         d = CompactFactor(data,domain=Domain())
         x2 = X2Separator(d)
         g2 = G2Separator(d)
         for a,b in pairs(vars):
             for s in powerset(set(vars) - set([a,b])):
                 x2p, x2s, x2d = x2.test_independ(a, b, set(s))
                 g2p, g2s, g2d = g2.test_independ(a, b, set(s))
                 # one degree of freedom
                 self.assertEquals(x2d, 0)
                 self.assertEquals(g2d, 0)
                 # default to independent
                 self.assertEquals(x2p, 1)
                 self.assertEquals(g2p, 1)
                 # zero statistics (no data)
                 self.assertEquals(x2s, 0)
                 self.assertEquals(g2s, 0)
Example #2
0
    def _ic_discovery(self, separator):
        n = 0
        self._skel.complete(self._skel.vertices())
        skel = self._skel

        too_many_adjacent = True
        while too_many_adjacent:
            too_many_adjacent = False
            # find a pair (x,y) such that the cardinality of the neighbourhood
            # exceeds n
            for x, y in pairs(self.variables()):
                if x not in self._must_have and y not in self._must_have:
                    continue

                n_x = set(skel.neighbours(x))
                n_y = set(skel.neighbours(y))

                if y not in n_x:
                    continue

                if x not in n_y:
                    raise RuntimeError,'inconsistent neighbourhoods'

                # separators must be potential ancestors of both variables.
                # constrain the neighbourhood to contain only potential ancestors
                cond  = n_x & self.potential_ancestors(x)
                cond |= n_y & self.potential_ancestors(y)
                cond -= frozenset([x,y])

                # if the neighbourhood is too small, try the next pair
                if n > len(cond):
                    continue

                # find an untested subset s of neighbours of x of cardinality n
                for s in subsetn(tuple(cond), n):
                    s = frozenset(s)
                    self.num_tests += 1
                    # test for x _|_ y | s
                    if separator.separates(x, y, s):
                        # see if we can find a more probable separator
                        s = self.hill_climb_cond(separator,x,y,s,cond)
                        # record independence since found
                        self._add_independence(x,y,s)
                        skel.remove_line(x, y)
                        break

            # increment required neighbourhood minimum size
            n += 1
            # see if we've found all the CIs
            too_many_adjacent = False
            for x in self.variables():
                too_many_adjacent |= len(skel.neighbours(x)) > n
                if too_many_adjacent:
                    break