def setUp(self): super(TestGraphCI, self).setUp() self.g = list(xrange(4)) self.indeps = list(xrange(4)) self.g[0] = UGraph(vertices=['a', 'b', 'c', 'd', 'e'], lines=[('a', 'b'), ('a', 'c'), ('b', 'd'), ('b', 'c'), ('c', 'd'), ('c', 'e')]) self.indeps[0] = { ('b', 'e'): [frozenset(['c'])], ('d', 'e'): [frozenset(['c'])], ('a', 'd'): [frozenset(['c', 'b'])], ('a', 'e'): [frozenset(['c'])] } self.g[1] = ADG(vertices=['a', 'b', 'c', 'd', 'e', 'f'], arrows=[('a', 'b'), ('b', 'c'), ('d', 'a'), ('d', 'e'), ('d', 'f'), ('e', 'c'), ('c', 'f')]) self.indeps[1] = { ('e', 'f'): [frozenset(['c', 'd'])], ('a', 'f'): [frozenset(['b', 'd']), frozenset(['c', 'd'])], ('a', 'e'): [frozenset(['d'])], ('b', 'f'): [frozenset(['a', 'c', 'e']), frozenset(['c', 'd'])], ('c', 'd'): [frozenset(['b', 'e']), frozenset(['a', 'e'])], ('b', 'e'): [frozenset(['a']), frozenset(['d'])], ('a', 'c'): [frozenset(['b', 'd']), frozenset(['b', 'e'])], ('b', 'd'): [frozenset(['a'])] } self.g[2] = ADG(vertices=['a', 'b', 'c', 'd'], arrows=[('a', 'b'), ('b', 'c'), ('c', 'd')]) self.indeps[2] = { ('a', 'd'): [frozenset(['c']), frozenset(['b'])], ('a', 'c'): [frozenset(['b'])], ('b', 'd'): [frozenset(['c'])] } self.g[3] = ADG(vertices=['a', 'b', 'c', 'd'], arrows=[('a', 'c'), ('b', 'c'), ('c', 'd')]) self.indeps[3] = { ('a', 'c'): [frozenset([])], ('b', 'c'): [frozenset([])], ('d', 'c'): [frozenset([])] }
def search(self, data, order): # no need to separate out interventional data ci = PCCI(CBFSeparator(data), constrain_order=order, hill_climb_cond=self._hill_climb_cond) skel = ci.skeleton() g = ADG(vertices=skel.vertices()) for a, b in skel.lines(): if a in ci.potential_ancestors(b): g.add_arrow(a, b) else: g.add_arrow(b, a) return g
def search(self, data, order, score=None): arr = [] parents = {} if score is None: def score(data, child, parents): return data.family_score(child, parents) for i, child in enumerate(order): # candidate parents are those variables occuring before child in # the order candidate_parents = set(order[:i]) parents[child] = self._find_parents(data, child, candidate_parents, score) for parent in parents[child]: arr.append((parent, child)) return ADG(vertices=order, arrows=arr)
def setUp(self): from gPy.Graphs import ADG asia_adg = ADG() asia_adg.add_vertices([ 'VisitAsia', 'Tuberculosis', 'Smoking', 'Cancer', 'TbOrCa', 'XRay', 'Bronchitis', 'Dyspnea' ]) asia_adg.put_arrows( (['VisitAsia', 'Tuberculosis'], ['Tuberculosis', 'TbOrCa'], ['Cancer', 'TbOrCa'], ['Smoking', 'Cancer'], ['Smoking', 'Bronchitis'], ['Bronchitis', 'Dyspnea'], ['TbOrCa', 'Dyspnea'], ['TbOrCa', 'XRay'])) self.asia_adg = asia_adg self.asia_cpts = { 'VisitAsia': (['Visit', 'No_Visit'], [], [((0.01, 0.99), [])]), 'Tuberculosis': (['Present', 'Absent'], ['VisitAsia'], [((0.05, 0.95), ['Visit']), ((0.01, 0.99), ['No_Visit'])]), 'Smoking': (['Smoker', 'NonSmoker'], [], [((0.5, 0.5), [])]), 'Cancer': (['Present', 'Absent'], ['Smoking'], [((0.1, 0.9), ['Smoker']), ((0.01, 0.99), ['NonSmoker'])]), 'TbOrCa': (['True', 'False'], ['Tuberculosis', 'Cancer'], [((1, 0), ['Present', 'Present']), ((1, 0), ['Present', 'Absent']), ((1, 0), ['Absent', 'Present']), ((0, 1), ['Absent', 'Absent'])]), 'XRay': (['Abnormal', 'Normal'], ['TbOrCa'], [((0.98, 0.02), ['True']), ((0.05, 0.95), ['False'])]), 'Bronchitis': (['Present', 'Absent'], ['Smoking'], [((0.6, 0.4), ['Smoker']), ((0.3, 0.7), ['NonSmoker'])]), 'Dyspnea': (['Present', 'Absent'], ['TbOrCa', 'Bronchitis'], [((0.9, 0.1), ['True', 'Present']), ((0.7, 0.3), ['True', 'Absent']), ((0.8, 0.2), ['False', 'Present']), ((0.1, 0.9), ['False', 'Absent'])]) }
""" This is the easy question. To construct the BN a student can either deduce it from the information given or use a brute-force search to find a BN which satistfies all the conditions. The latter option is possible due to the small number of variables. The 'ci' function is a direct implementation of what is discussed in lectures. Students who find the 'ancestral_adg' method in the gPy Python package will find their job easier. """ from gPy.Graphs import ADG _vertices = ['G' + str(i) for i in range(1, 7)] _arrows = (('G1', 'G2'), ('G1', 'G3'), ('G2', 'G4'), ('G3', 'G4'), ('G4', 'G6'), ('G4', 'G5')) bnq1 = ADG(_vertices, _arrows) def ci(adg, a, b, s): """Return whether a is independent of b given s in adg """ return adg.ancestral_adg(a | b | s).moralise().separates(a, b, s)
def setUp(self): from gPy.Graphs import ADG asia_adg = ADG() asia_adg.add_vertices(['VisitAsia', 'Tuberculosis', 'Smoking', 'Cancer', 'TbOrCa', 'XRay', 'Bronchitis', 'Dyspnea']) asia_adg.put_arrows((['VisitAsia','Tuberculosis'], ['Tuberculosis','TbOrCa'], ['Cancer','TbOrCa'], ['Smoking','Cancer'], ['Smoking','Bronchitis'], ['Bronchitis','Dyspnea'], ['TbOrCa','Dyspnea'], ['TbOrCa','XRay'])) self.asia_adg = asia_adg self.asia_cpts = { 'VisitAsia': ( ['Visit', 'No_Visit'], [], [ ((0.01, 0.99), []) ]), 'Tuberculosis': ( ['Present', 'Absent'], ['VisitAsia'], [ ((0.05, 0.95), ['Visit']), ((0.01, 0.99), ['No_Visit']) ]), 'Smoking': ( ['Smoker', 'NonSmoker'], [], [ ((0.5, 0.5), []) ]), 'Cancer': ( ['Present', 'Absent'], ['Smoking'], [ ((0.1, 0.9), ['Smoker']), ((0.01, 0.99), ['NonSmoker']) ]), 'TbOrCa': ( ['True', 'False'], ['Tuberculosis', 'Cancer'], [ ((1, 0), ['Present', 'Present']), ((1, 0), ['Present', 'Absent']), ((1, 0), ['Absent', 'Present']), ((0, 1), ['Absent', 'Absent']) ]), 'XRay': ( ['Abnormal', 'Normal'], ['TbOrCa'], [ ((0.98, 0.02), ['True']), ((0.05, 0.95), ['False']) ]), 'Bronchitis': ( ['Present', 'Absent'], ['Smoking'], [ ((0.6, 0.4), ['Smoker']), ((0.3, 0.7), ['NonSmoker']) ]), 'Dyspnea': ( ['Present', 'Absent'], ['TbOrCa', 'Bronchitis'], [ ((0.9, 0.1), ['True', 'Present']), ((0.7, 0.3), ['True', 'Absent']), ((0.8, 0.2), ['False', 'Present']), ((0.1, 0.9), ['False', 'Absent']) ]) }
best_score = score best_parents = parents except NameError: best_score = score best_parents = parents return best_parents def sublist(candidates,n): if not candidates or n == 0: yield [] else: first = [candidates[0]] for sl in sublist(candidates[1:],n-1): yield first + sl for sl in sublist(candidates[1:],n): yield sl best_adg = ADG(ordering) for i, v in enumerate(ordering): if verbose: print 'Finding best parents for %s' % v parents = best_parents(v,ordering[:i]) for parent in parents: best_adg.add_arrow(parent,v) print 'Best ADG' print '*********' print best_adg
best_score = score best_parents = parents except NameError: best_score = score best_parents = parents return best_parents def sublist(candidates, n): if not candidates or n == 0: yield [] else: first = [candidates[0]] for sl in sublist(candidates[1:], n - 1): yield first + sl for sl in sublist(candidates[1:], n): yield sl best_adg = ADG(ordering) for i, v in enumerate(ordering): if verbose: print 'Finding best parents for %s' % v parents = best_parents(v, ordering[:i]) for parent in parents: best_adg.add_arrow(parent, v) print 'Best ADG' print '*********' print best_adg