示例#1
0
文件: bn.py 项目: miguelbarao/ProbPy
    def sample(self, pre_inst=None):
        """
        Returns a random sample of the network in the form of an Event. An
        event will contain a list of tuples. Each tuple is a pair between a
        random variable and it's instance, in the form:

            >>> ret = [(X, "T"), (Y, "F")]

        :param pre_inst: Default=None. Should be a list of tuples. Each tuple
                         is a pair between a random variable and a value of
                         it's domain. That value will be the value in the
                         sample instead of leaving it to random chance.
        """

        net = self.network[:]
        inst_vars = Event()

        for i in net:
            insted = i.factor.instVar(inst_vars)
            var = i.node

            if pre_inst is not None:
                val = pre_inst.value(i.node)

            if pre_inst is None or val is None:
                val = self.pickRandomValue(var.domain, insted.values,
                                           random.random())

            inst_vars.setValue(var, val)

        return inst_vars
示例#2
0
文件: bn.py 项目: Python3pkg/ProbPy
    def sample(self, pre_inst=None):
        """
        Returns a random sample of the network in the form of an Event. An
        event will contain a list of tuples. Each tuple is a pair between a
        random variable and it's instance, in the form:

            >>> ret = [(X, "T"), (Y, "F")]

        :param pre_inst: Default=None. Should be a list of tuples. Each tuple
                         is a pair between a random variable and a value of
                         it's domain. That value will be the value in the
                         sample instead of leaving it to random chance.
        """

        net = self.network[:]
        inst_vars = Event()

        for i in net:
            insted = i.factor.instVar(inst_vars)
            var = i.node

            if pre_inst is not None:
                val = pre_inst.value(i.node)

            if pre_inst is None or val is None:
                val = self.pickRandomValue(var.domain, insted.values,
                                           random.random())

            inst_vars.setValue(var, val)

        return inst_vars
示例#3
0
    def eventSetValue_test_6(self):
        """
        Test setValue method
        """

        event = Event(var=self.X, val=self.X.domain[0])
        event.setValue(self.X, self.X.domain[1])
        event.setValue(self.Y, self.Y.domain[1])

        assert (len(event.event.keys()) == 2)
        assert (event.value(self.X) == self.X.domain[1])
        assert (event.value(self.Y) == self.Y.domain[1])
示例#4
0
    def eventSetValue_test_6(self):
        """
        Test setValue method
        """

        event = Event(var=self.X, val=self.X.domain[0])
        event.setValue(self.X, self.X.domain[1])
        event.setValue(self.Y, self.Y.domain[1])

        assert(len(event.event.keys()) == 2)
        assert(event.value(self.X) == self.X.domain[1])
        assert(event.value(self.Y) == self.Y.domain[1])
示例#5
0
    # distribution. Note that the nodes were place in the list at random
    network = [
        (earthq,   factor_earthq),
        (alarm,    factor_alarm),
        (john,     factor_john),
        (burglary, factor_burglary),
        (mary,     factor_mary)
    ]

    # The network object
    BN = bn.BayesianNetwork(network)

    # An event with the observations In this example the variable john was
    # observed being true. Same for variable mary
    observed = Event()
    observed.setValue(john, "True")
    observed.setValue(mary, "True")

    # Run the elimination ask algorithm (Variable Elimination) for the example.
    # Result should be approximately [0.284, 0.716]
    burglary_k_john_mary = BN.eliminationAsk(burglary, observed)

    print("P(Burglary | John=true, Mary=true)")
    print(burglary_k_john_mary)

    # Another example with the same network. The observation and query variable
    # changed, but the order of the distributions is the same. The result
    # should be approximately [0.849, 0.150]
    observed = Event(var=burglary, val="True")
    john_k_burglary = BN.eliminationAsk(john, observed)