Ejemplo n.º 1
0
    def ask(self, query, bayes_net):
        evidence_vars = dict(query.evidence)

        evidence_vars[query.query_variable] = True
        true_probability = self._enumerate_all(bayes_net, bayes_net.get_variables(), evidence_vars)

        evidence_vars[query.query_variable] = False
        false_probability = self._enumerate_all(bayes_net, bayes_net.get_variables(), evidence_vars)

        return normalize((true_probability, false_probability))
Ejemplo n.º 2
0
    def rejection_sample(self, x, evidence, number_of_samples, randomizer=StandardRandomizer()):
        true_values = 0
        false_values = 0

        for i in range(0, number_of_samples):
            sample = self.get_prior_sample(randomizer)
            if self._consistent(sample, evidence):
                query_value = sample[x]

                if query_value:
                    true_values += 1
                else:
                    false_values += 1

        return normalize([true_values, false_values])
Ejemplo n.º 3
0
    def ask(self, query, probability_distribution):
        """

        :param query (Query):
        :param probability_distribution (ProbabilityDistribution):
        :return (float, float):
        """
        h = dict(query.evidence)

        h[query.query_variable] = True
        true_distr = probability_distribution.probability_of(h)

        h[query.query_variable] = False
        false_distr = probability_distribution.probability_of(h)


        return normalize((true_distr, false_distr))
Ejemplo n.º 4
0
    def likelihood_weighting(self, var, evidence, number_of_samples, randomizer=StandardRandomizer()):
        true_probability = 0
        false_probability = 0

        for i in range(number_of_samples):
            x = {}
            w = 1

            for node in self._get_variable_nodes():
                if evidence.get(node.variable) != None:
                    w *= node.probability_of(x)
                    x[node.variable] = evidence[node.variable]
                else:
                    x[node.variable] = node.is_true_for(randomizer.next_double(), x)

            query_value = x[var]

            if query_value:
                true_probability += w
            else:
                false_probability += w

        return normalize([true_probability, false_probability])
Ejemplo n.º 5
0
    def mcmc_ask(self, var, evidence, number_of_values, randomizer=StandardRandomizer()):
        true_values = 0
        false_values = 0

        non_evidence_variables = self._non_evidence_variables(evidence)
        event = self._create_random_event(non_evidence_variables, evidence, randomizer)

        for i in range(number_of_values):
            for non_evidence_var in non_evidence_variables:
                node = self._get_node_of(non_evidence_var)

                markov_blanket = self._create_markov_blanket(node)
                mb = self._create_mb_values(markov_blanket, event)

                true_probability, false_probability = self.rejection_sample(node.variable, mb, 100, randomizer)
                event[node.variable] = self._truth_value(true_probability, randomizer)

                query_value = event[var]
                if query_value:
                    true_values += 1
                else:
                    false_values += 1

        return normalize((true_values, false_values))