Bayes () Function

113 Views Asked by At

Hi can I get an example of what the following code should be using different figures: "Write a bayes() function that can be used to calculate the posterior probability of an event A, given the known outcome of event B and the prior probability of A, of B conditional on A, and of B conditional on not-A using the Bayes theorem.

For example, bayes(.01, .99, .99) should return 0.01."?

I tried the below, but it is incorrect:

def bayes(a, b_given_a, b_given_not_a):
    """Calculates P(a|b) using bayes theorem.
    Bayes theorem states that:
                       P(b|a) * P(a)
    P(a|b) = --------------------------------
              P(b|a) * P(a) + P(b|~a) * P(~a)
    Intuitively this is saying that the probability of b given a is equivalent
    to the number of times both b and a occur over the number of time b occurs.
    """
    not_a = 1-a
    b = b_given_a * a + b_given_not_a * not_a
    a = (b_given_a * a) / a
    return a

    a= .01
    b_given_a= .99
    b_given_not_a= .99
    result = bayes(a, b_given_a, b_given_not_a)
    print('P(b|a) = %,2f%%' % (result *100))
0

There are 0 best solutions below