def conditional_cow_framework(): language = [ HP, not_HP, HOC, not_HOC, CCA, not_CCA, FM, not_FM, cond_JN, cond_neg_JN, JF, CM ] rules = [ Semantics.Rule(HP, [HOC, CCA, not_FM]), Semantics.Rule(HOC, []), Semantics.Rule(FM, [CM]), Semantics.Rule(CCA, [cond_neg_JN]) ] assumptions = [not_HP, not_HOC, not_CCA, not_FM] contraries = { not_HP: Semantics.Contrary(not_HP, HP), not_HOC: Semantics.Contrary(not_HOC, HOC), not_CCA: Semantics.Contrary(not_CCA, CCA), not_FM: Semantics.Contrary(not_FM, FM) } random_variables = [cond_JN, JF, CM] BN = Bayesian.BayesianNetwork({ cond_JN.symbol: Bayesian.ConditionalProbability(cond_JN.symbol, [JF], { "JF": 0.8, "~JF": 0.0 }), JF.symbol: 0.25, CM.symbol: 0.1 }) return Semantics.BABA(language, rules, assumptions, contraries, random_variables, BN)
def test_extract_conditional_random_variable(self): cond_prob = Bayesian.ConditionalProbability(rv_a, [rv_b], { 'b': 0.4, '~b': 0.3 }) parsed_rv, parsed_probability = Parser.extract_conditional_random_variable( 'myRV(a, [b], [b: 0.4, ~b: 0.3]).') self.assertEqual(Semantics.Sentence('a', random_variable=True), parsed_rv) self.assertTrue(cond_prob == parsed_probability) cond_prob = Bayesian.ConditionalProbability(rv_a, [rv_b, rv_c], { 'bc': 0.2, 'b~c': 0.4, '~bc': 0.1, '~b~c': 0.7 }) parsed_rv, parsed_probability = Parser.extract_conditional_random_variable( 'myRV(a, [b, c], [bc: 0.2, b~c: 0.4, ~bc: 0.1, ~b~c: 0.7]).') self.assertEqual(Semantics.Sentence('a', random_variable=True), parsed_rv) self.assertTrue(cond_prob == parsed_probability) cond_prob = Bayesian.ConditionalProbability(rv_a, [rv_c], { 'c': 0.2, '~c': 0.3 }) parsed_rv, parsed_probability = Parser.extract_conditional_random_variable( 'myRV( a , [ c ] , [~c : 0.3, c: 0.2]).') self.assertEqual(Semantics.Sentence('a', random_variable=True), parsed_rv) self.assertTrue(cond_prob == parsed_probability)
def test_bayesian_network_with_conditional_probabilities(self): cp_map = {"bc": 0.3, "b~c": 0.6, "~bc": 0.5, "~b~c": 0.4} cp = Bayesian.ConditionalProbability(a, [b, c], cp_map) baysnet = Bayesian.BayesianNetwork({'a': cp, 'b': 0.7, 'c': 0.2}) self.assertAlmostEqual(0.7, baysnet.p(b)) self.assertAlmostEqual(0.2, baysnet.p(c)) self.assertAlmostEqual(0.6, baysnet.p(a, [c_neg, b])) self.assertAlmostEqual(0.4, baysnet.p(a, [b_neg, c_neg])) self.assertAlmostEqual(0.6, baysnet.p(a_neg, [b_neg, c_neg])) self.assertAlmostEqual(0.336, baysnet.p_world([a, b, c_neg])) self.assertAlmostEqual(0.096, baysnet.p_world([a, b_neg, c_neg])) self.assertAlmostEqual(0.098, baysnet.p_world([a_neg, b, c])) self.assertAlmostEqual(0.144, baysnet.p_world([a_neg, b_neg, c_neg]))
def cow_framework(): JN = Semantics.Sentence('JN') not_JN = Semantics.Sentence('not_JN') language = [ HP, not_HP, HOC, not_HOC, CCA, not_CCA, FM, not_FM, JN, not_JN, JF, CM ] rules = [ Semantics.Rule(HP, [HOC, CCA, not_FM]), Semantics.Rule(HOC, []), Semantics.Rule(FM, [CM]), Semantics.Rule(CCA, [not_JN]), Semantics.Rule(JN, [JF]) ] assumptions = [not_HP, not_HOC, not_CCA, not_FM, not_JN] contraries = { not_HP: Semantics.Contrary(not_HP, HP), not_HOC: Semantics.Contrary(not_HOC, HOC), not_CCA: Semantics.Contrary(not_CCA, CCA), not_FM: Semantics.Contrary(not_FM, FM), not_JN: Semantics.Contrary(not_JN, JN), HP: Semantics.Contrary(HP, not_HP), HOC: Semantics.Contrary(HOC, not_HOC), CCA: Semantics.Contrary(CCA, not_CCA), FM: Semantics.Contrary(FM, not_FM), JN: Semantics.Contrary(JN, not_JN) } random_variables = [JF, CM] BN = Bayesian.BayesianNetwork({CM.symbol: 0.1, JF.symbol: 0.8}) return Semantics.BABA(language, rules, assumptions, contraries, random_variables, BN)
def test_conditional_probability_request(self): cp_map = {"bc": 0.4, "b~c": 0.3, "~bc": 0.2, "~b~c": 0.1} cp = Bayesian.ConditionalProbability(a, [b, c], cp_map) self.assertEqual(0.4, cp.p([b, c])) self.assertEqual(0.3, cp.p([b, c_neg])) self.assertEqual(0.2, cp.p([b_neg, c])) self.assertEqual(0.1, cp.p([b_neg, c_neg]))
def test_conditional_probability_key_generation(self): cp_map = {"bc": 0.4, "b~c": 0.3, "~bc": 0.2, "~b~c": 0.1} cp = Bayesian.ConditionalProbability(a, [b, c], cp_map) self.assertEqual("bc", cp.conditional_probability_key([c, b])) self.assertEqual("b~c", cp.conditional_probability_key([c_neg, b])) self.assertEqual("~bc", cp.conditional_probability_key([b_neg, c])) self.assertEqual("~b~c", cp.conditional_probability_key([b_neg, c_neg]))
def test_p_raises_exception_for_invalid_random_variable(self): baba = Bayesian.BayesianNetwork({'a': 0.5}) def get_b_probability(): return baba.p(b) self.assertRaises(Bayesian.InvalidRandomVariableException, get_b_probability)
def test_p(self): baba = Bayesian.BayesianNetwork({'a': 0.5, 'b': 0.8, 'c': 0.3}) self.assertAlmostEqual(0.5, baba.p(a)) self.assertAlmostEqual(0.5, baba.p(a_neg)) self.assertAlmostEqual(0.8, baba.p(b)) self.assertAlmostEqual(0.2, baba.p(b_neg)) self.assertAlmostEqual(0.3, baba.p(c)) self.assertAlmostEqual(0.7, baba.p(c_neg))
def test_invalid_conditional_probability_request(self): cp_map = {"bc": 0.4, "b~c": 0.3, "~bc": 0.2, "~b~c": 0.1} cp = Bayesian.ConditionalProbability(a, [b, c], cp_map) def invalid_request(): return cp.p([b]) self.assertRaises(Bayesian.InvalidConditionalProbabilityException, invalid_request)
def extract_conditional_random_variable(text): if not matches_conditional_random_variable_declaration(text): raise ProgramParseException( "Provided conditional random variable does not match required format" ) rv_definition = extract_from_parentheses(text) arguments = rv_definition.split('[') sentence = Semantics.Sentence(arguments.pop(0).split(',')[0].strip(), random_variable=True) conditional_variables = extract_conditional_variables('[' + arguments.pop(0)) conditional_probabilities = extract_conditional_probabilities( '[' + ''.join(arguments)) conditional_probability = Bayesian.ConditionalProbability( sentence, conditional_variables, conditional_probabilities) return sentence, conditional_probability
def r_framework(): language = [a, _a, b, _b, c, _c, j, t, s] rules = [ Semantics.Rule(j, [a]), Semantics.Rule(_a, [b, s]), Semantics.Rule(_b, [c, t]) ] assumptions = [a, b, c] contraries = { a: Semantics.Contrary(a, _a), b: Semantics.Contrary(b, _b), c: Semantics.Contrary(c, _c) } random_variables = [s, t] bayes_net = Bayesian.BayesianNetwork({s.symbol: 0.6, t.symbol: 0.4}) return Semantics.BABA(language, rules, assumptions, contraries, random_variables, bayes_net)
def parse_program(self, program): self.reset_program_elements() rules = [] # Parse rules at the end for line in program: if matches_rule_declaration(line): rules.append(line) elif matches_assumption_declaration(line): assumption = extract_assumption(line) self.assumptions.append(assumption) self.language.append(assumption) elif matches_contrary_declaration(line): contrary = extract_contrary(line) self.contraries[contrary.assumption] = contrary self.language.append(contrary.contrary) elif matches_random_variable_declaration(line): rv, probability = extract_random_variable(line) self.random_variables.append(rv) self.language.append(rv) self.bayesian_network[rv.symbol] = probability elif matches_conditional_random_variable_declaration(line): rv, probability = extract_conditional_random_variable(line) self.random_variables.append(rv) self.language.append(rv) self.bayesian_network[rv.symbol] = probability for rule in rules: extracted_rule = extract_rule(rule, self.random_variables) rule_elements = extracted_rule.body + [extracted_rule.head] for sentence in rule_elements: if sentence not in self.language: self.language.append(sentence) self.rules.append(extracted_rule) return Semantics.BABA(set(self.language), self.rules, set(self.assumptions), self.contraries, set(self.random_variables), Bayesian.BayesianNetwork(self.bayesian_network))
def test_conditional_probability_sum(self): cp_map = {"bc": 0.4, "b~c": 0.3, "~bc": 0.2, "~b~c": 0.1} cp = Bayesian.ConditionalProbability(a, [b, c], cp_map) self.assertAlmostEqual(1.0, cp.sum())
def invalid_network_instantiation(): Bayesian.BayesianNetwork({'a': 0.4, 'b': 1.0001, 'c': -0.4})
import unittest from PythonSemantics import Semantics, Bayesian bn = Bayesian.BayesianNetwork({'a': 0.5, 'b': 0.8, 'c': 0.3}) a = Semantics.Sentence('a', random_variable=True) a_neg = Semantics.Sentence('a', random_variable=True, negation=True) b = Semantics.Sentence('b', random_variable=True) b_neg = Semantics.Sentence('b', random_variable=True, negation=True) c = Semantics.Sentence('c', random_variable=True) c_neg = Semantics.Sentence('c', random_variable=True, negation=True) d = Semantics.Sentence('d', random_variable=True) class TestBayesianNetwork(unittest.TestCase): def test_validates_valid_network(self): bn.validate() def test_validates_invalid_network(self): def invalid_network_instantiation(): Bayesian.BayesianNetwork({'a': 0.4, 'b': 1.0001, 'c': -0.4}) self.assertRaises(Bayesian.InvalidRandomVariableException, invalid_network_instantiation) def test_p(self): baba = Bayesian.BayesianNetwork({'a': 0.5, 'b': 0.8, 'c': 0.3}) self.assertAlmostEqual(0.5, baba.p(a)) self.assertAlmostEqual(0.5, baba.p(a_neg)) self.assertAlmostEqual(0.8, baba.p(b)) self.assertAlmostEqual(0.2, baba.p(b_neg))