How to Calculate Joint and Conditional Probabilities in Python

fimg

Understanding probability basics is essential in many fields, data science, machine learning, and statistics to name a few. Some basic concepts in probability theory are joint probability and conditional probability.

In this tutorial, we’ll explore joint and conditional probabilities, their mathematical definitions and formulas, and go over step-by-step examples using Python.

▶️ Link to the Google Colab notebook

Introduction to Joint and Conditional Probabilities

First, let’s review joint and conditional probabilities.

Conditional Probability

Conditional probability, given by P(X | Y), is the probability of an event X happening given that Y has occurred.

The formula for conditional probability is:
conditional-prob

Where:

  • P(X ∩ Y) is the joint probability of events X and Y
  • P(Y) is the probability of Y

Joint Probability

Joint Probability represents the likelihood that two events, X and Y, occur together. It’s denoted as P(X = x, Y = y), which can be read as the probability that both X = x and Y = y are true.

The formula for calculating joint probability, P(X = x, Y = y), using conditional probabilities, is:
joint-prob

This expression means that the joint probability of X and Y can be calculated by:

  1. Multiplying P(Y = y | X = x) with P(X = x) or
  2. Multiplying P(X = x | Y = y) with P(Y = y).

Calculating Joint and Conditional Probabilities in Python

Let’s use a dataset of individuals categorized by age group and interest in sports and compute joint and conditional probabilities.

Step 1: Creating Sample Data

Let’s create a sample dataset to use for our probability calculations.

import pandas as pd

# Sample data
data = {
    "Age_Group": ["Teen", "Teen", "Teen", "Adult", "Adult", "Senior", "Senior", "Senior"],
    "Sports_Interest": ["Yes", "No", "Yes", "Yes", "No", "Yes", "No", "No"]
}

df = pd.DataFrame(data)

# Display the data
print(df)

Output:

  Age_Group Sports_Interest
0 Teen      Yes
1 Teen      No
2 Teen      Yes
3 Adult     No
4 Adult     No
5 Senior    Yes
6 Senior    Yes
7 Senior    No

Step 2: Calculating Joint Probability

We’ll calculate the joint probability of a person being in the “Teen” age group and having an interest in sports (“Yes”).

  • Define the conditions: We want both Age_Group == “Teen” and Sports_Interest == “Yes”
  • Calculate the total number of observations
  • Count the occurrences that meet both conditions and divide by the total
# Total number of observations
total_count = len(df)

# Count occurrences where Age_Group is "Teen" and Sports_Interest is "Yes"
joint_count = len(df[(df['Age_Group'] == 'Teen') & (df['Sports_Interest'] == 'Yes')])

# Joint probability
joint_probability = joint_count / total_count

print("Joint Probability (Teen and Sports Interest Yes):", joint_probability)

This outputs:

Joint Probability (Teen and Sports Interest Yes): 0.25

Step 3: Calculating Conditional Probability

Now, let’s calculate the conditional probability of a person having an interest in sports, given that they are a “Teen”.

  • Use only “Teen” rows (filter on “Age_Group”).
  • Calculate the probability of “Sports_Interest = Yes” within this filtered subset.
# Filter data for Age_Group = "Teen"
teen_data = df[df['Age_Group'] == 'Teen']

# Count occurrences of Sports_Interest = "Yes" among teens
conditional_count = len(teen_data[teen_data['Sports_Interest'] == 'Yes'])

# Conditional probability
conditional_probability = conditional_count / len(teen_data)

print("Conditional Probability (Sports Interest Yes | Age Group Teen):", conditional_probability)

This outputs:

Conditional Probability (Sports Interest Yes | Age Group Teen): 0.667

Step 4: Defining Reusable Functions

To make these calculations more flexible, we’ll define two functions: one for joint probability and one for conditional probability.

def calculate_joint_probability(df, condition1, condition2):
    total_count = len(df)
    joint_count = len(df[(df[condition1[0]] == condition1[1]) & (df[condition2[0]] == condition2[1])])
    return joint_count / total_count

def calculate_conditional_probability(df, given_condition, target_condition):
    subset = df[df[given_condition[0]] == given_condition[1]]
    conditional_count = len(subset[subset[target_condition[0]] == target_condition[1]])
    return conditional_count / len(subset)

Now let’s use the function calls with the conditions for our example:

# Joint Probability 
joint_prob = calculate_joint_probability(df, ("Age_Group", "Teen"), ("Sports_Interest", "Yes"))
print("Joint Probability (Teen and Sports Interest Yes):", joint_prob)

# Conditional Probability 
conditional_prob = calculate_conditional_probability(df, ("Age_Group", "Teen"), ("Sports_Interest", "Yes"))
print("Conditional Probability (Sports Interest Yes | Age Group Teen):", conditional_prob)

This should give a similar output:

Joint Probability (Teen and Sports Interest Yes): 0.25
Conditional Probability (Sports Interest Yes | Age Group Teen): 0.667

Conclusion

In this tutorial, we defined joint and conditional probabilities with formulas. We also calculated joint and conditional probabilities step-by-step using Python. We generalized the process with functions to make it more reusable for other datasets.

This approach to joint and conditional probabilities can be applied to any real-world examples on discrete probability distributions.

Cover image: Created by Author | Segmind SSD-1B Model

Leave a Reply

Your email address will not be published. Required fields are marked *