20.7 C
London
Monday, June 2, 2025
HomePythonDescriptive Statistics in PythonHow to Apply Bayes’ Theorem in Python

How to Apply Bayes’ Theorem in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Bayes’ Theorem states the following for any two events A and B:

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

where:

  • P(A|B): The probability of event A, given event B has occurred.
  • P(B|A): The probability of event B, given event A has occurred.
  • P(A): The probability of event A.
  • P(B): The probability of event B.

For example, suppose the probability of the weather being cloudy is 40%.

Also suppose the probability of rain on a given day is 20%.

Also suppose the probability of clouds on a rainy day is 85%. 

If it’s cloudy outside on a given day, what is the probability that it will rain that day?

Solution:

  • P(cloudy) = 0.40
  • P(rain) = 0.20
  • P(cloudy | rain) = 0.85

Thus, we can calculate:

  • P(rain | cloudy) = P(rain) * P(cloudy | rain) / P(cloudy)
  • P(rain | cloudy) = 0.20 * 0.85 / 0.40
  • P(rain | cloudy) = 0.425

If it’s cloudy outside on a given day, the probability that it will rain that day is 42.5%.

We can create the following simple function to apply Bayes’ Theorem in Python:

def bayesTheorem(pA, pB, pBA):
    return pA * pBA / pB

The following example shows how to use this function in practice.

Example: Bayes’ Theorem in Python

Suppose we know the following probabilities:

  • P(rain) = 0.20
  • P(cloudy) = 0.40
  • P(cloudy | rain) = 0.85

To calculate P(rain | cloudy), we can use the following syntax:

#define function for Bayes' theorem
def bayesTheorem(pA, pB, pBA):
    return pA * pBA / pB

#define probabilities
pRain = 0.2
pCloudy = 0.4
pCloudyRain = 0.85

#use function to calculate conditional probability
bayesTheorem(pRain, pCloudy, pCloudyRain)

0.425

This tells us that if it’s cloudy outside on a given day, the probability that it will rain that day is 0.425 or 42.5%.

This matches the value that we calculated earlier by hand.

Additional Resources

The following tutorials explain how to perform other common tasks in Python:

How to Calculate Conditional Probability in Python
How to Calculate Expected Value in Python
How to Calculate a Trimmed Mean in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories