def create_qmr(nD, nF, density): """ create qmr BN using nD = number of disease nF = number of findings density = proba for disease i to be linked to finding j """ name = "qmr%d-%d-%f" % (nD, nF, density) bn = gum.BayesNet(name) lDis = [ bn.add(gum.LabelizedVariable("D%4d" % i, "D%4d" % i, 2)) for i in range(nD) ] # add noisyOR nodes with random leaks lFind = [ bn.addNoisyOR(gum.LabelizedVariable("F%d" % i, "F%d" % i, 2), random.uniform(0.1, 0.9)) for i in range(nF) ] for node in lDis: prior = random.uniform(0.01, 0.99) bn.cpt(node)[:] = [prior, 1 - prior] for i in lDis: for j in lFind: if random.random() < density: # add new cause i to noisyOR j with random weight bn.addWeightedArc(i, j, random.uniform(0.1, 0.9)) return bn
def createRandomMNTree(nodeNumber, minDomainSize=2, maxDomainSize=2): """ Build a random markov network and return it, the returned markov network is a tree Examples -------- >>> import markovNetworkLearning as mnl >>> mn=mnl.createRandomMNTree(15,2,4) Parameters ---------- nodeNumber : int the number of variables of the markov network minDomainSize : int 2 or above, default value : 2, each variable will have a random domain size randomly selected between minDomainSize and maxDomainSize maxDomainSize : int 2 or above, and more than minDomainSize, default value : 2, each variable will have a random domain size randomly selected between minDomainSize and maxDomainSize Returns ------- pyAgrum.MarkovNet the resulting Markov network """ variablesDomainSize = np.random.randint(minDomainSize, maxDomainSize + 1, size=nodeNumber) tree = gum.MarkovNet() if nodeNumber > 0: tree.add(gum.LabelizedVariable("0", "", int(variablesDomainSize[0]))) for i in range(1, nodeNumber): otherNode = random.choice(list(tree.nodes())) tree.add(gum.LabelizedVariable(str(i), "", int(variablesDomainSize[i]))) tree.addFactor({otherNode, str(i)}) return tree
def testKL(self): v = gum.LabelizedVariable("v", "v", 2) w = gum.LabelizedVariable("w", "w", 2) p = gum.Potential().add(v).fillWith([0.0, 1.0]) q = gum.Potential().add(v).fillWith([0.5, 0.5]) r = gum.Potential().add(v).fillWith([0.7, 0.3]) s = gum.Potential().add(v).add(w).fillWith([0.0, 1.0, 0.2, .08]) self.assertEqual(p.KL(p), 0.0) with self.assertRaises(gum.InvalidArgument): res = p.KL(s) with self.assertRaises(gum.InvalidArgument): res = s.KL(p) res = p.KL(q) self.assertAlmostEqual(res, 0.0 + 1.0 * math.log(1.0 / 0.5, 2)) with self.assertRaises(gum.FatalError): res = q.KL(p) res = p.KL(r) self.assertAlmostEqual(res, 0.0 + 1.0 * math.log(1.0 / 0.3, 2)) with self.assertRaises(gum.FatalError): res = r.KL(p) self.assertAlmostEqual( q.KL(r), 0.5 * math.log(0.5 / 0.7, 2) + 0.5 * math.log(0.5 / 0.3, 2)) self.assertAlmostEqual( r.KL(q), 0.7 * math.log(0.7 / 0.5, 2) + 0.3 * math.log(0.3 / 0.5, 2))
def testLogit(self): bn = gum.BayesNet() age = bn.add(gum.RangeVariable("age", "", 35, 67)) taux = bn.add(gum.RangeVariable("taux", "", 115, 171)) angine = bn.add(gum.LabelizedVariable("angine", "")) vc = gum.LabelizedVariable("coeur", "", 0) vc.addLabel("NON").addLabel("OUI") coeur = bn.addLogit(vc, 14.4937) bn.addWeightedArc(age, coeur, -0.1256) bn.addWeightedArc(taux, coeur, -0.0636) bn.addWeightedArc(angine, coeur, 1.779) witness_age = ("50", "49", "46", "49", "62", "35", "67", "65", "47") witness_taux = ("126", "126", "144", "139", "154", "156", "160", "140", "143") witness_angine = ("1", "0", "0", "0", "1", "1", "0", "0", "0") witness_coeur = ("OUI", "OUI", "OUI", "OUI", "OUI", "OUI", "NON", "NON", "NON") witness_proba = (0.8786, 0.5807, 0.3912, 0.3773, 0.2127, 0.8760, 1 - 0.0163, 1 - 0.0710, 1 - 0.3765) inst = gum.Instantiation(bn.cpt(coeur)) for i in range(len(witness_age)): inst.chgVal(bn.variable(age), bn.variable(age)[witness_age[i]]) inst.chgVal(bn.variable(taux), bn.variable(taux)[witness_taux[i]]) inst.chgVal(bn.variable(angine), bn.variable(angine)[witness_angine[i]]) inst.chgVal(bn.variable(coeur), bn.variable(coeur)[witness_coeur[i]]) self.assertAlmostEqual(bn.cpt(coeur).get(inst), witness_proba[i], places=3)
def testVariableInsertion(self): pot = gum.Potential() self.assertTrue(pot.empty()) pot.add(self.var['c']) self.assertFalse(pot.empty()) self.assertEqual(pot.nbrDim(), 1) pot.add(self.var['s']).add(self.var['r']) self.assertEqual(pot.nbrDim(), 3) for id, var in enumerate([self.var['c'], self.var['s'], self.var['r']]): self.assertTrue(pot.contains(var)) self.assertEqual(pot.variable(id), var) self.assertFalse(pot.contains(gum.LabelizedVariable("a", "", 5))) a = gum.LabelizedVariable("a", "a", 2) other_a = gum.LabelizedVariable("a", "a", 2) p = gum.Potential() p.add(a) with self.assertRaises(gum.DuplicateElement): p.add(a) # once again with self.assertRaises(gum.DuplicateElement): p.add(other_a) # with the same name
def testLabelsOfVars(self): v = gum.LabelizedVariable("a", "a") self.assertEqual(v.labels(), ("0", "1")) self.assertNotEqual(v.labels(), ("1", "0")) v = gum.LabelizedVariable( "b", "b", 0).addLabel("toto").addLabel("titi").addLabel("yes") self.assertEqual(v.labels(), ("toto", "titi", "yes")) v = gum.RangeVariable("c", "c", 0, 5) self.assertEqual(v.labels(), ("0", "1", "2", "3", "4", "5")) v = gum.RangeVariable("d", "d", 3, 5) self.assertEqual(v.labels(), ("3", "4", "5")) v = gum.DiscretizedVariable( "e", "e").addTick(1).addTick(2).addTick(3).addTick(4) self.assertEqual(v.labels(), ("[1;2[", "[2;3[", "[3;4]")) v = gum.DiscretizedVariable( "e", "e").addTick(1).addTick(2).addTick(3).addTick(4) v.setEmpirical(True) self.assertEqual(v.labels(), ("(1;2[", "[2;3[", "[3;4)")) v = gum.DiscretizedVariable("f", "f", [1, 5, 2, 4]) self.assertEqual(v.labels(), ("[1;2[", "[2;4[", "[4;5]")) v = gum.DiscretizedVariable("f", "f", [1, 5, 2, 4]) v.setEmpirical(True) self.assertEqual(v.labels(), ("(1;2[", "[2;4[", "[4;5)"))
def restructure_bn_fenton(self, bn): # could be streamlined bn.erase('constraint') list_of_scn_node_names = ["NA"] exhaustivity_check = 0 for i in bn.nodes(): if 'scn' in bn.variable(i).name(): list_of_scn_node_names.append(bn.variable(i).name()) exhaustivity_check = exhaustivity_check + bn.cpt(i)[0] if exhaustivity_check != 1: # add a leak state scn_3 = bn.add(gum.LabelizedVariable('scn_leak', 'leak', self.truth_values)) bn.cpt(scn_3) [{'scn_leak': self.truth_values[0]}] = 1 - exhaustivity_check bn.cpt(scn_3)[{'scn_leak': self.truth_values[1]}] = exhaustivity_check list_of_scn_node_names.append(bn.variable('scn_leak').name()) n_states_in_constraint_node = len(list_of_scn_node_names) # this is n(scn) + 1 aux = bn.add(gum.LabelizedVariable('aux', 'aux', list_of_scn_node_names)) constraint = bn.add( gum.LabelizedVariable('constraint', 'constraint', self.truth_values)) # aux always has two states # all nodes are added now... Now add links between the nodes. list_of_scn_node_names.remove("NA") self.scenario_nodes = list_of_scn_node_names for node_name in list_of_scn_node_names: bn.addArc(node_name, 'aux') bn.addArc('aux', 'constraint') self.implement_aux(bn) self.implement_constraint(bn)
def setUp(self): self.bn = gum.BayesNet() self.buffer0 = "" self.buffer1 = "" self.var1 = gum.LabelizedVariable("a", "le noeud a", 2) self.var2 = gum.LabelizedVariable("b", "le noeud b", 2) self.var3 = gum.LabelizedVariable("c", "le noeud c", 3)
def testEqualities(self): u = gum.LabelizedVariable("u", "u", 4) v = gum.LabelizedVariable("v", "v", 2) w = gum.LabelizedVariable("w", "w", 3) p = gum.Potential().add(u).add(v).add(w) p.random() q = gum.Potential(p) self.assertEqual(p, q) i = gum.Instantiation(q) i.setLast() q[i] = 0 self.assertNotEqual(p, q) q.fillWith(p) self.assertEqual(p, q) q.fillWith(1) self.assertNotEqual(p, q) q.fillWith(p) self.assertEqual(p, q) x = gum.LabelizedVariable("x", "Unknown", 5) q.add(x) self.assertNotEqual(p, q) q = gum.Potential().add(v).add(w) self.assertNotEqual(p, q)
def setUp(self): self.var1 = gum.LabelizedVariable("var1", "1", 2) self.var2 = gum.LabelizedVariable("var2", "2", 2) self.var3 = gum.LabelizedVariable("var3", "3", 2) self.var4 = gum.LabelizedVariable("var4", "4", 2) self.var5 = gum.LabelizedVariable("var5", "(gum::Size) 5", 3) self.bufferlisten = "" self.bufferecoute = ""
def testZeroParent(self): bn = gum.BayesNet() a = bn.addOR(gum.LabelizedVariable('a', 'a', 2)) self.assertEqual(bn.cpt(a).tolist(), [1.0, 0.0]) b = bn.addAND(gum.LabelizedVariable('b', 'b', 2)) self.assertEqual(bn.cpt(b).tolist(), [0.0, 1.0])
def setUp(self): self.var = {} self.var.update({"c": gum.LabelizedVariable("c", "nuages", 2)}) self.var.update({"s": gum.LabelizedVariable("s", "arrosoir", 2)}) r = gum.LabelizedVariable("r", "pluie", 0).addLabel('bof') \ .addLabel('carrement') self.var.update({"r": r}) self.var.update({"w": gum.LabelizedVariable("w", "herbe mouillée", 2)})
def testOperationWithDifferentVariablesFromMadsLindskou(self): px = gum.Potential() py = gum.Potential() for s in "ab": px.add(gum.LabelizedVariable(s, s, 2)) for s in "bc": py.add(gum.LabelizedVariable(s, s, 2))
def testHashableDiscreteVariable(self): va = gum.LabelizedVariable("a", "a") vb = gum.LabelizedVariable("b", "b", ["toto", "titi", "yes"]) vc = gum.RangeVariable("c", "c", 0, 5) vd = gum.RangeVariable("d", "d", 3, 5) ve = gum.DiscretizedVariable( "e", "e").addTick(1).addTick(2).addTick(3).addTick(4) s = set([va, vb, vc] + [vc, vd, ve] + [va, ve]) self.assertEqual(len(s), 5)
def testRandomPotential(self): u = gum.LabelizedVariable("u", "u", 4) v = gum.LabelizedVariable("v", "v", 2) w = gum.LabelizedVariable("w", "w", 3) p = gum.Potential().add(u).add(v).add(w) I = gum.Instantiation(p) p.random() I.setFirst() while not I.end(): self.assertTrue(p.get(I) <= 1.0) I.inc() p.randomDistribution() I.setFirst() cum = 0 while not I.end(): self.assertTrue(p.get(I) <= 1.0) cum += p.get(I) I.inc() self.assertAlmostEqual(cum, 1.0, 6) p.randomCPT() v = p.variable(0) I.setFirst() while not I.end(): cum = 0.0 I.setFirstVar(v) while not I.end(): self.assertTrue(p.get(I) <= 1.0) cum += p.get(I) I.incVar(v) self.assertAlmostEqual(cum, 1.0, 6) I.unsetEnd() I.incNotVar(v) p.fillWith(1).normalizeAsCPT() I.setFirst() while not I.end(): self.assertAlmostEqual(p.get(I), 0.25, 6) I.inc() alpha = 0.0 while alpha <= 1.0: p.fillWith(1).normalizeAsCPT() p.noising(alpha) min = (1 - alpha) * 0.25 + alpha * 0.0 max = (1 - alpha) * 0.25 + alpha * 1.0 I.setFirst() while not I.end(): self.assertTrue(min <= p.get(I) <= max) I.inc() alpha += 0.1
def testVariableAccessor(self): v = gum.LabelizedVariable("v", "v", 2) w = gum.LabelizedVariable("w", "w", 2) p = gum.Potential().add(v).add(w) self.assertEqual(p.variable(0), p.variable('v')) self.assertEqual(p.variable(1), p.variable('w')) self.assertNotEqual(p.variable(1), p.variable('v')) self.assertNotEqual(p.variable(0), p.variable('w')) with self.assertRaises(gum.NotFound): x = p.variable("zz")
def testInverse(self): u = gum.LabelizedVariable("u", "u", 4) v = gum.LabelizedVariable("v", "v", 2) w = gum.LabelizedVariable("w", "w", 3) p = gum.Potential().add(u).add(v).add(w) p.random() q = gum.Potential(p).inverse() self.assertAlmostEqual((q * p).max(), 1.0, delta=1e-7) self.assertAlmostEqual((q * p).min(), 1.0, delta=1e-7)
def testArgMaxMinFindAll(self): v = gum.LabelizedVariable("v", "v", 2) w = gum.LabelizedVariable("w", "w", 3) p = gum.Potential() self.__test_val_for_set(p, 10, p.findAll(10), 0) p.add(v).add(w) p.fillWith([1, 3, 2, 4, 1, 4]) self.__test_val_for_set(p, 3, p.findAll(3), 1) self.__test_val_for_set(p, 10, p.findAll(10), 0) self.__test_val_for_set(p, 4, p.argmax()[0], 2) self.__test_val_for_set(p, 1, p.argmin()[0], 2)
def testEntropyPotential(self): a = gum.LabelizedVariable("a", "a", 2) p = gum.Potential().add(a) self.assertEqual(p.fillWith([0, 1]).entropy(), 0.0) self.assertEqual(p.fillWith([1, 0]).entropy(), 0.0) self.assertEqual(p.fillWith([0.5, 0.5]).entropy(), 1.0)
def testReorganizePotential(self): a, b, c, d = [gum.LabelizedVariable(s, s, 3) for s in "abcd"] p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) q = gum.Potential() q.add(c).add(d) q.fillWith([4, 5, 6, 3, 2, 1, 4, 3, 2]) self.assertNotEqual(str(p * q), str(q * p)) self.assertEqual(str(p * q), str((q * p).reorganize(['c', 'd', 'a', 'b']))) self.assertNotEqual( str(p * q), str((q * p).reorganize(['c', 'a', 'd', 'b']))) try: q.reorganize(['a']) self.assertTrue(False) except gum.InvalidArgument: self.assertTrue(True) try: q.reorganize(['d']) self.assertTrue(False) except gum.InvalidArgument: self.assertTrue(True)
def testReorganizePotentialWithAccents(self): if sys.version_info >= (3, 0): a, b, c, d = [gum.LabelizedVariable(s, s, 3) for s in "éèàû"] p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) q = gum.Potential() q.add(c).add(d) q.fillWith([4, 5, 6, 3, 2, 1, 4, 3, 2]) self.assertNotEqual(str(p * q), str(q * p)) self.assertEqual( str(p * q), str((q * p).reorganize(['à', 'û', 'é', 'è']))) self.assertNotEqual( str(p * q), str((q * p).reorganize(['à', 'é', 'û', 'è']))) try: q.reorganize(['é']) self.assertTrue(False) except gum.InvalidArgument: self.assertTrue(True) try: q.reorganize(['û']) self.assertTrue(False) except gum.InvalidArgument: self.assertTrue(True)
def read_file(filename): """ Renvoie les variables aléatoires et la probabilité contenues dans le fichier dont le nom est passé en argument. """ Pjointe = gum.Potential() variables = [] fic = open(filename, 'r') # on rajoute les variables dans le potentiel nb_vars = int(fic.readline()) for i in range(nb_vars): name, domsize = fic.readline().split() variable = gum.LabelizedVariable(name, name, int(domsize)) variables.append(variable) Pjointe.add(variable) # on rajoute les valeurs de proba dans le potentiel cpt = [] for line in fic: cpt.append(float(line)) Pjointe.fillWith(np.array(cpt)) fic.close() return np.array(variables), Pjointe
def testEquality(self): a, b, c = [gum.LabelizedVariable(s, s, 3) for s in "abc"] q = gum.Potential() q.add(b).add(c) q.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) p = gum.Potential() # same data, difference dims p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) r = gum.Potential() # same data, same dims r.add(a).add(b) r.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) t = gum.Potential() # same dims, different data t.add(a).add(b) t.fillWith([1, 2, 3, 0, 5, 6, 7, 8, 9]) u = gum.Potential() # same dims, same data, different order u.add(b).add(a) u.fillWith([1, 4, 7, 2, 5, 8, 3, 6, 9]) self.assertTrue(p == p) self.assertFalse(p == q) self.assertTrue(p == r) self.assertFalse(p == t) self.assertTrue(p == u) self.assertFalse(p != p) self.assertTrue(p != q) self.assertFalse(p != r) self.assertTrue(p != t) self.assertFalse(p != u)
def undiGraphToMarkovNetwork(graph, variables=dict(), domainSize=2): """ Build a markov network from an UndiGraph Variables can be either given in parameters or can be created if they are not defined Parameters ---------- graph : pyAgrum.UndiGraph the graph variables : dict<str:pyAgrum.DiscreteVariable> already defined variables, keys are variables name domainSize : int the domain of variables that are not defined Returns ------- pyAgrum.MarkovNet the resulting Markov network """ nodes = graph.nodes() cliques = getAllMaximalCliquesUndiGraph(graph) definedVariables = variables.keys() mn = gum.MarkovNet() for node in nodes: if node in definedVariables: mn.add(variables[node]) else: mn.add(gum.LabelizedVariable(str(node), "", domainSize)) for clique in cliques: mn.addFactor(clique) return mn
def testMargOutOperators(self): a, b, c, d = [gum.LabelizedVariable(s, s, 3) for s in "abcd"] p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) p.normalize() q = gum.Potential() q.add(c).add(d) q.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) q.normalize() joint = p * q margAB = joint.margSumOut(["c", "d"]) self.assertEqual(margAB.names, p.names) self.assertEqual(margAB.tolist(), p.tolist()) margCD = joint.margSumOut( ["b", "a", "x"]) # note the vars in a different order and with one not present in the potential self.assertEqual(margCD.names, q.names) self.assertEqual(margCD.tolist(), q.tolist()) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(p.margProdOut(["a"]).tolist(), [6, 120, 504]) self.assertEqual(p.margProdOut(["b"]).tolist(), [28, 80, 162]) self.assertEqual(p.margMaxOut(["a"]).tolist(), [3, 6, 9]) self.assertEqual(p.margMaxOut(["b"]).tolist(), [7, 8, 9]) self.assertEqual(p.margMinOut(["a"]).tolist(), [1, 4, 7]) self.assertEqual(p.margMinOut(["b"]).tolist(), [1, 2, 3])
def testDimensionIncreasing(self): bn = gum.BayesNet() id_list = [] self.fillBN(bn, id_list) bn.cpt(id_list[3]).fillWith([1, 0, 0.1, 0.9, 0.1, 0.9, 0.01, 0.99]) addvar = gum.LabelizedVariable("d", "rosée", 4) addvar_id = bn.add(addvar) bn.addArc(addvar_id, id_list[3]) list3 = bn.cpt(id_list[3]) self.assertEqual(list3.names, ('w', 's', 'r', 'd')) list3[0, :, :, :] = \ [[[0.99, 0.1], [0.1, 0]], [[0.01, 0.9], [0.9, 1]]] list3[1, :, :, :] = \ [[[0.99, 0.1], [0.1, 0]], [[0.01, 0.9], [0.9, 1]]] list3[2, :, :, :] = \ [[[0.99, 0.1], [0.1, 0]], [[0.01, 0.9], [0.9, 1]]] list3[3, :, :, :] = \ [[[0.99, 0.1], [0.1, 0]], [[0.01, 0.9], [0.9, 1]]] list3[2, :, :, :] = 0.25 self.assertListsAlmostEqual(list3[2, :, :, :].tolist(), [[[0.25, 0.25], [0.25, 0.25]], [[0.25, 0.25], [0.25, 0.25]]]) self.assertListsAlmostEqual(list3[{"d": 1}].tolist(), [[[0.99, 0.1], [0.1, 0]], [[0.01, 0.9], [0.9, 1]]])
def testSimpleInPLaceOperators(self): a, b, c = [gum.LabelizedVariable(s, s, 3) for s in "abc"] q = gum.Potential() q.add(b).add(c) q.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) z = p + q p += q self.assertEqual(z.tolist(), p.tolist()) p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) z = p - q p -= q self.assertEqual(z.tolist(), p.tolist()) p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) z = p * q p *= q self.assertEqual(z.tolist(), p.tolist()) p = gum.Potential() p.add(a).add(b) p.fillWith([1, 2, 3, 4, 5, 6, 7, 8, 9]) z = p / q p /= q self.assertEqual(z.tolist(), p.tolist())
def dag_to_bn(dag, names): # DAG to BN bn = gum.BayesNet() for name in names: bn.add(gum.LabelizedVariable(name)) for arc in dag.arcs(): bn.addArc(arc[0], arc[1]) return bn
def implement_aux_constraint_structure(self, bn): list_of_scn_node_names = ["NA"] for i in bn.nodes(): if 'scn' in bn.variable(i).name(): list_of_scn_node_names.append(bn.variable(i).name()) n_states_in_constraint_node = len(list_of_scn_node_names) # this is n(scn) + 1 aux = bn.add(gum.LabelizedVariable('aux', 'aux', list_of_scn_node_names)) constraint = bn.add( gum.LabelizedVariable('constraint', 'constraint', self.truth_values)) # aux always has two states # all nodes are added now... Now add links between the nodes. list_of_scn_node_names.remove("NA") self.scenario_nodes = list_of_scn_node_names for node_name in list_of_scn_node_names: bn.addArc(node_name, 'aux') bn.addArc('aux', 'constraint') self.implement_aux(bn) self.implement_constraint(bn)
def add_node(self, code): """Add a new node to the network.""" node = self.twodbn.add(gum.LabelizedVariable(code, code, 2), self.code_as_number(code)) self.nodes.append(node) return node