コード例 #1
0
    def test_network2(self):
        ve = VariableElimination()
        bn = NetworkExamples.construct_basic_network()

        distrib = ve.query_prob(bn, ["Burglary"],
                                Assignment(["JohnCalls", "MaryCalls"]))

        assert distrib.get_prob(Assignment("Burglary",
                                           False)) == pytest.approx(0.713676,
                                                                    abs=0.0001)
        assert distrib.get_prob(Assignment("Burglary",
                                           True)) == pytest.approx(0.286323,
                                                                   abs=0.0001)

        query2 = ve.query_prob(bn, ["Alarm", "Burglary"],
                               Assignment(["Alarm", "MaryCalls"]))

        assert query2.get_prob(Assignment(["Alarm", "!Burglary"
                                           ])) == pytest.approx(0.623974,
                                                                abs=0.001)
コード例 #2
0
    def test_network2bis(self):
        ve = VariableElimination()
        bn = NetworkExamples.construct_basic_network2()

        query = ve.query_prob(bn, ["Burglary"],
                              Assignment(["JohnCalls", "MaryCalls"]))

        assert query.get_prob(Assignment("Burglary",
                                         False)) == pytest.approx(0.360657,
                                                                  abs=0.0001)
        assert query.get_prob(Assignment("Burglary",
                                         True)) == pytest.approx(0.63934,
                                                                 abs=0.0001)

        query2 = ve.query_prob(bn, ["Alarm", "Burglary"],
                               Assignment(["Alarm", "MaryCalls"]))

        assert query2.get_prob(Assignment(["Alarm", "!Burglary"
                                           ])) == pytest.approx(0.3577609,
                                                                abs=0.001)
コード例 #3
0
    def __init__(self):
        self._variable_elimination = VariableElimination()
        self._sampling_algorithm_1 = SamplingAlgorithm(2000, 200)
        self._sampling_algorithm_2 = SamplingAlgorithm(15000, 1500)
        self._naive_inference = NaiveInference()

        self._timings = dict()
        self._numbers = dict()

        self._timings[self._variable_elimination] = 0
        self._numbers[self._variable_elimination] = 0
        self._timings[self._sampling_algorithm_1] = 0
        self._numbers[self._sampling_algorithm_1] = 0
        self._timings[self._sampling_algorithm_2] = 0
        self._numbers[self._sampling_algorithm_2] = 0
        self._timings[self._naive_inference] = 0
        self._numbers[self._naive_inference] = 0

        self._include_naive = False
コード例 #4
0
    def test_dirichlet(self):
        old_discretisation_settings = Settings.discretization_buckets
        Settings.discretization_buckets = 250

        alphas = list()
        alphas.append(40.0)
        alphas.append(80.0)
        alphas = np.array(alphas)

        dirichlet = DirichletDensityFunction(alphas)
        distrib = ContinuousDistribution("x", dirichlet)
        assert isinstance(distrib.sample(), ArrayVal)

        assert 2 == len(distrib.sample())
        assert distrib.sample().get_array()[0] == pytest.approx(0.33, abs=0.15)

        ##############################################
        # dirichlet distribution 자바 코드에 버그가 있음.
        ##############################################
        # assert distrib.get_prob_density(ArrayVal([1./3, 2./3])) == pytest.approx(8.0, abs=0.5)

        n = ChanceNode("x", distrib)
        network = BNetwork()
        network.add_node(n)

        table = VariableElimination().query_prob(network, "x")

        sum = 0.
        for value in table.get_values():
            if value.get_array()[0] < 0.33333:
                sum += table.get_prob(value)

        assert sum == pytest.approx(0.5, abs=0.1)

        conversion1 = VariableElimination().query_prob(network, "x")

        assert abs(
            len(conversion1.get_posterior(Assignment()).get_values()) -
            Settings.discretization_buckets) < 10
        assert conversion1.get_posterior(Assignment()).get_prob(
            ValueFactory.create("[0.3333,0.6666]")) == pytest.approx(0.02,
                                                                     abs=0.05)

        conversion3 = SamplingAlgorithm(4000, 1000).query_prob(network, "x")

        # DistributionViewer(conversion3)
        # Thread.sleep(3000000)

        # TODO: 아래 테스트 케이스 문제 없는지 확인 필요.
        # assert conversion3.to_continuous().get_prob_density(ValueFactory.create("[0.3333,0.6666]")) == pytest.approx(9.0, abs=1.5)

        assert distrib.get_function().get_mean()[0] == pytest.approx(0.333333,
                                                                     abs=0.01)
        assert distrib.get_function().get_variance()[0] == pytest.approx(
            0.002, abs=0.01)

        assert conversion3.to_continuous().get_function().get_mean(
        )[0] == pytest.approx(0.333333, abs=0.05)
        assert conversion3.to_continuous().get_function().get_variance(
        )[0] == pytest.approx(0.002, abs=0.05)

        Settings.discretization_buckets = old_discretisation_settings
コード例 #5
0
 def __init__(self):
     self._ve = VariableElimination()
     self._lw = SamplingAlgorithm()
コード例 #6
0
class TestNetworkReduction:
    network = NetworkExamples.construct_basic_network2()
    ve = VariableElimination()

    # change the variable name 'is' -> 'iz'
    iz = SamplingAlgorithm(3000, 500)
    naive = NaiveInference()
    sw = SwitchingAlgorithm()

    def test1(self):
        reduced_net = TestNetworkReduction.ve.reduce(
            TestNetworkReduction.network,
            ["Burglary", "Earthquake", "MaryCalls"])
        reduced_net2 = TestNetworkReduction.naive.reduce(
            TestNetworkReduction.network,
            ["Burglary", "Earthquake", "MaryCalls"])
        reduced_net3 = TestNetworkReduction.iz.reduce(
            TestNetworkReduction.network,
            ["Burglary", "Earthquake", "MaryCalls"])
        reduced_net4 = TestNetworkReduction.sw.reduce(
            TestNetworkReduction.network,
            ["Burglary", "Earthquake", "MaryCalls"])

        assert len(reduced_net.get_nodes()) == 3
        assert len(reduced_net2.get_nodes()) == 3
        assert len(reduced_net3.get_nodes()) == 3
        assert len(reduced_net4.get_nodes()) == 3
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network,
            ["MaryCalls"], Assignment("Burglary")).get_prob(
                Assignment("MaryCalls")) == pytest.approx(
                    TestNetworkReduction.ve.query_prob(
                        reduced_net, ["MaryCalls"],
                        Assignment("Burglary")).get_prob(
                            Assignment("MaryCalls")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network,
            ["MaryCalls"], Assignment("Burglary")).get_prob(
                Assignment("MaryCalls")) == pytest.approx(
                    TestNetworkReduction.ve.query_prob(
                        reduced_net2, ["MaryCalls"],
                        Assignment("Burglary")).get_prob(
                            Assignment("MaryCalls")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network,
            ["MaryCalls"], Assignment("Burglary")).get_prob(
                Assignment("MaryCalls")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net3, ["MaryCalls"],
                        Assignment("Burglary")).get_prob(
                            Assignment("MaryCalls")),
                    abs=0.15)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network,
            ["MaryCalls"], Assignment("Burglary")).get_prob(
                Assignment("MaryCalls")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net4, ["MaryCalls"],
                        Assignment("Burglary")).get_prob(
                            Assignment("MaryCalls")),
                    abs=0.15)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Earthquake"],
            Assignment("!MaryCalls")).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.ve.query_prob(
                        reduced_net, ["Earthquake"],
                        Assignment("!MaryCalls")).get_prob(
                            Assignment("Earthquake")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Earthquake"],
            Assignment("!MaryCalls")).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.ve.query_prob(
                        reduced_net2, ["Earthquake"],
                        Assignment("!MaryCalls")).get_prob(
                            Assignment("Earthquake")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Earthquake"],
            Assignment("!MaryCalls")).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net3, ["Earthquake"],
                        Assignment("!MaryCalls")).get_prob(
                            Assignment("Earthquake")),
                    abs=0.05)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Earthquake"],
            Assignment("!MaryCalls")).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net4, ["Earthquake"],
                        Assignment("!MaryCalls")).get_prob(
                            Assignment("Earthquake")),
                    abs=0.05)

    def test2(self):
        reduced_net = TestNetworkReduction.ve.reduce(
            TestNetworkReduction.network, ["Burglary", "MaryCalls"],
            Assignment("!Earthquake"))
        reduced_net2 = TestNetworkReduction.naive.reduce(
            TestNetworkReduction.network, ["Burglary", "MaryCalls"],
            Assignment("!Earthquake"))
        reduced_net3 = TestNetworkReduction.iz.reduce(
            TestNetworkReduction.network, ["Burglary", "MaryCalls"],
            Assignment("!Earthquake"))
        reduced_net4 = TestNetworkReduction.sw.reduce(
            TestNetworkReduction.network, ["Burglary", "MaryCalls"],
            Assignment("!Earthquake"))

        assert len(reduced_net.get_nodes()) == 2
        assert len(reduced_net2.get_nodes()) == 2
        assert len(reduced_net3.get_nodes()) == 2
        assert len(reduced_net4.get_nodes()) == 2
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["MaryCalls"],
            Assignment("!Earthquake")).get_prob(
                Assignment("MaryCalls")) == pytest.approx(
                    TestNetworkReduction.ve.query_prob(
                        reduced_net,
                        ["MaryCalls"]).get_prob(Assignment("MaryCalls")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net,
            ["MaryCalls"]).get_prob(Assignment("MaryCalls")) == pytest.approx(
                TestNetworkReduction.naive.query_prob(
                    reduced_net2,
                    ["MaryCalls"]).get_prob(Assignment("MaryCalls")),
                abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net,
            ["MaryCalls"]).get_prob(Assignment("MaryCalls")) == pytest.approx(
                TestNetworkReduction.iz.query_prob(
                    reduced_net3,
                    ["MaryCalls"]).get_prob(Assignment("MaryCalls")),
                abs=0.05)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net,
            ["MaryCalls"]).get_prob(Assignment("MaryCalls")) == pytest.approx(
                TestNetworkReduction.iz.query_prob(
                    reduced_net4,
                    ["MaryCalls"]).get_prob(Assignment("MaryCalls")),
                abs=0.05)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Burglary"],
            Assignment(["!MaryCalls", "!Earthquake"
                        ])).get_prob(Assignment("Burglary")) == pytest.approx(
                            TestNetworkReduction.ve.query_prob(
                                reduced_net, ["Burglary"],
                                Assignment("!MaryCalls")).get_prob(
                                    Assignment("Burglary")),
                            abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Burglary"],
            Assignment(["!MaryCalls", "!Earthquake"
                        ])).get_prob(Assignment("Burglary")) == pytest.approx(
                            TestNetworkReduction.naive.query_prob(
                                reduced_net2, ["Burglary"],
                                Assignment("!MaryCalls")).get_prob(
                                    Assignment("Burglary")),
                            abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net, ["Burglary"], Assignment("!MaryCalls")).get_prob(
                Assignment("Burglary")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net3, ["Burglary"],
                        Assignment("!MaryCalls")).get_prob(
                            Assignment("Burglary")),
                    abs=0.05)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net, ["Burglary"], Assignment("!MaryCalls")).get_prob(
                Assignment("Burglary")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net4, ["Burglary"],
                        Assignment("!MaryCalls")).get_prob(
                            Assignment("Burglary")),
                    abs=0.05)

    def test3(self):
        reduced_net = TestNetworkReduction.ve.reduce(
            TestNetworkReduction.network, ["Burglary", "Earthquake"],
            Assignment("JohnCalls"))
        reduced_net2 = TestNetworkReduction.naive.reduce(
            TestNetworkReduction.network, ["Burglary", "Earthquake"],
            Assignment("JohnCalls"))
        reduced_net3 = TestNetworkReduction.iz.reduce(
            TestNetworkReduction.network, ["Burglary", "Earthquake"],
            Assignment("JohnCalls"))
        reduced_net4 = TestNetworkReduction.sw.reduce(
            TestNetworkReduction.network, ["Burglary", "Earthquake"],
            Assignment("JohnCalls"))

        assert len(reduced_net.get_nodes()) == 2
        assert len(reduced_net2.get_nodes()) == 2
        assert len(reduced_net3.get_nodes()) == 2
        assert len(reduced_net4.get_nodes()) == 2
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network,
            ["Burglary"], Assignment("JohnCalls")).get_prob(
                Assignment("Burglary")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net,
                        ["Burglary"]).get_prob(Assignment("Burglary")),
                    abs=0.1)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network,
            ["Burglary"], Assignment("JohnCalls")).get_prob(
                Assignment("Burglary")) == pytest.approx(
                    TestNetworkReduction.naive.query_prob(
                        reduced_net2,
                        ["Burglary"]).get_prob(Assignment("Burglary")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net,
            ["Burglary"]).get_prob(Assignment("Burglary")) == pytest.approx(
                TestNetworkReduction.naive.query_prob(
                    reduced_net3,
                    ["Burglary"]).get_prob(Assignment("Burglary")),
                abs=0.08)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net2,
            ["Burglary"]).get_prob(Assignment("Burglary")) == pytest.approx(
                TestNetworkReduction.naive.query_prob(
                    reduced_net4,
                    ["Burglary"]).get_prob(Assignment("Burglary")),
                abs=0.05)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Earthquake"],
            Assignment("JohnCalls")).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.ve.query_prob(
                        reduced_net,
                        ["Earthquake"]).get_prob(Assignment("Earthquake")),
                    abs=0.0001)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net, ["Earthquake"]).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.iz.query_prob(
                        reduced_net2,
                        ["Earthquake"]).get_prob(Assignment("Earthquake")),
                    abs=0.07)
        assert TestNetworkReduction.ve.query_prob(
            TestNetworkReduction.network, ["Earthquake"],
            Assignment("JohnCalls")).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.naive.query_prob(
                        reduced_net3,
                        ["Earthquake"]).get_prob(Assignment("Earthquake")),
                    abs=0.07)
        assert TestNetworkReduction.ve.query_prob(
            reduced_net, ["Earthquake"]).get_prob(
                Assignment("Earthquake")) == pytest.approx(
                    TestNetworkReduction.naive.query_prob(
                        reduced_net4,
                        ["Earthquake"]).get_prob(Assignment("Earthquake")),
                    abs=0.07)

    def test5(self):
        reduced_net = TestNetworkReduction.ve.reduce(
            TestNetworkReduction.network, ["Burglary"],
            Assignment(["JohnCalls", "MaryCalls"]))

        reduced_net2 = TestNetworkReduction.iz.reduce(
            TestNetworkReduction.network, ["Burglary"],
            Assignment(["JohnCalls", "MaryCalls"]))

        reduced_net.add_node(
            copy(TestNetworkReduction.network.get_node("Action")))
        reduced_net.add_node(
            copy(TestNetworkReduction.network.get_node("Util1")))
        reduced_net.add_node(
            copy(TestNetworkReduction.network.get_node("Util2")))
        reduced_net.get_node("Util1").add_input_node(
            reduced_net.get_node("Burglary"))
        reduced_net.get_node("Util1").add_input_node(
            reduced_net.get_node("Action"))
        reduced_net.get_node("Util2").add_input_node(
            reduced_net.get_node("Burglary"))
        reduced_net.get_node("Util2").add_input_node(
            reduced_net.get_node("Action"))

        table1 = TestNetworkReduction.ve.query_util(reduced_net, "Action")
        table2 = TestNetworkReduction.ve.query_util(
            TestNetworkReduction.network, ["Action"],
            Assignment(["JohnCalls", "MaryCalls"]))

        for a in table1.get_table().keys():
            assert table1.get_util(a) == pytest.approx(table2.get_util(a),
                                                       abs=0.01)

        reduced_net2.add_node(
            copy(TestNetworkReduction.network.get_node("Action")))
        reduced_net2.add_node(
            copy(TestNetworkReduction.network.get_node("Util1")))
        reduced_net2.add_node(
            copy(TestNetworkReduction.network.get_node("Util2")))
        reduced_net2.get_node("Util1").add_input_node(
            reduced_net2.get_node("Burglary"))
        reduced_net2.get_node("Util1").add_input_node(
            reduced_net2.get_node("Action"))
        reduced_net2.get_node("Util2").add_input_node(
            reduced_net2.get_node("Burglary"))
        reduced_net2.get_node("Util2").add_input_node(
            reduced_net2.get_node("Action"))

        table3 = TestNetworkReduction.ve.query_util(reduced_net2, ["Action"])

        for a in table1.get_table().keys():
            assert table1.get_util(a) == pytest.approx(table3.get_util(a),
                                                       abs=0.8)

    def test6(self):
        old = copy(TestNetworkReduction.network)

        TestNetworkReduction.network.get_node("Alarm").remove_input_node(
            "Earthquake")
        TestNetworkReduction.network.get_node("Alarm").remove_input_node(
            "Burglary")
        TestNetworkReduction.network.get_chance_node("Alarm").set_distrib(
            SingleValueDistribution("Alarm", "False"))

        self.test1()
        self.test2()
        self.test3()

        TestNetworkReduction.network = old
コード例 #7
0
    def test_network_util(self):
        network = NetworkExamples.construct_basic_network2()
        ve = VariableElimination()
        naive = NaiveInference()
        iz = SamplingAlgorithm(4000, 300)

        assert ve.query_util(
            network, ["Action"],
            Assignment([
                Assignment("JohnCalls"),
                Assignment("MaryCalls")
            ])).get_util(Assignment("Action",
                                    "CallPolice")) == pytest.approx(-0.680,
                                                                    abs=0.001)
        assert naive.query_util(
            network, ["Action"],
            Assignment([
                Assignment("JohnCalls"),
                Assignment("MaryCalls")
            ])).get_util(Assignment("Action",
                                    "CallPolice")) == pytest.approx(-0.680,
                                                                    abs=0.001)
        assert iz.query_util(
            network, ["Action"],
            Assignment([Assignment("JohnCalls"),
                        Assignment("MaryCalls")])).get_util(
                            Assignment("Action",
                                       "CallPolice")) == pytest.approx(-0.680,
                                                                       abs=0.5)
        assert ve.query_util(
            network, ["Action"],
            Assignment([
                Assignment("JohnCalls"),
                Assignment("MaryCalls")
            ])).get_util(Assignment("Action",
                                    "DoNothing")) == pytest.approx(-6.213,
                                                                   abs=0.001)
        assert naive.query_util(
            network, ["Action"],
            Assignment([
                Assignment("JohnCalls"),
                Assignment("MaryCalls")
            ])).get_util(Assignment("Action",
                                    "DoNothing")) == pytest.approx(-6.213,
                                                                   abs=0.001)
        assert iz.query_util(
            network, ["Action"],
            Assignment([Assignment("JohnCalls"),
                        Assignment("MaryCalls")])).get_util(
                            Assignment("Action",
                                       "DoNothing")) == pytest.approx(-6.213,
                                                                      abs=1.5)
        assert ve.query_util(
            network, ["Burglary"],
            Assignment([
                Assignment("JohnCalls"),
                Assignment("MaryCalls")
            ])).get_util(Assignment("!Burglary")) == pytest.approx(-0.1667,
                                                                   abs=0.001)
        assert naive.query_util(
            network, ["Burglary"],
            Assignment([
                Assignment("JohnCalls"),
                Assignment("MaryCalls")
            ])).get_util(Assignment("!Burglary")) == pytest.approx(-0.1667,
                                                                   abs=0.001)
        assert iz.query_util(
            network, ["Burglary"],
            Assignment([Assignment("JohnCalls"),
                        Assignment("MaryCalls")])).get_util(
                            Assignment("!Burglary")) == pytest.approx(-0.25,
                                                                      abs=0.5)
        assert ve.query_util(
            network, ["Burglary"],
            Assignment([Assignment("JohnCalls"),
                        Assignment("MaryCalls")])).get_util(
                            Assignment("Burglary")) == pytest.approx(-3.5,
                                                                     abs=0.001)
        assert naive.query_util(
            network, ["Burglary"],
            Assignment([Assignment("JohnCalls"),
                        Assignment("MaryCalls")])).get_util(
                            Assignment("Burglary")) == pytest.approx(-3.5,
                                                                     abs=0.001)
        assert iz.query_util(
            network, ["Burglary"],
            Assignment([Assignment("JohnCalls"),
                        Assignment("MaryCalls")])).get_util(
                            Assignment("Burglary")) == pytest.approx(-3.5,
                                                                     abs=1.0)