Search code examples
pythonmathprobability

Solving probability using Bayes Theorem in Python


I have the following question:

Suppose that a given coin is known to be either a fair coin or else a biased coin such as that described in part a). Although you do not know which it is, you assign a prior probability of 0.8 to the hypothesis that the coin is fair.

The coin is tossed and lands heads. Use Bayes’ theorem to determine the probability that the coin is fair and the probability that the coin is biased* given this observation.

The Theorem is as follows:

P(A|B) = P(B|A) / P(B) * P(A)

The code below is my attempt but I got an error saying I can't use "|" with floats:

 print(((0.2|0.8) / 0.2) * 0.8)

Solution

  • The operator "|" is called "binary OR" and it is a binary operator, does not work for floats.

    Additionally, P(B|A) is a single probability, read as "Probability of B given condition A happening." It is not the "|" of the single probabilities. In order for Bayes' theorem to work, you need to have 3 inputs, not two.

    Check out Bayes' Theorem on Math is Fun