Mathew K Analytics

Lesson 8 · Probability and Statistics in python

Understanding Conditional Probability and Independence in Probability Theory

In this hands-on lesson, we will discover conditional probability and independence. You will learn key ideas, see examples, and try simple code. No prior…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Welcome to Probability and Statistics in Python!#

In this hands-on lesson, we will discover conditional probability and independence.

You will learn key ideas, see examples, and try simple code. No prior experience needed.

Let us start by understanding basic probability, then build toward real-world analysis!

What is Probability?#

Probability is the chance that something will happen. For example, if you flip a coin, the chance of getting heads is 0.5 (or 50%). Probabilities are always between 0 and 1.

# import libraries for probability and data manipulation
import warnings; warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
# Simulate 10 coin flips
flips = np.random.choice(['Heads', 'Tails'], size=10)
print('Ten coin flips:', flips)
Ten coin flips: ['Tails' 'Tails' 'Tails' 'Tails' 'Tails' 'Heads' 'Heads' 'Heads' 'Tails'
 'Heads']
# Count heads and tails
unique, counts = np.unique(flips, return_counts=True)
results = dict(zip(unique, counts))
print('Counts:', results)
Counts: {np.str_('Heads'): np.int64(4), np.str_('Tails'): np.int64(6)}
# Calculate probability of heads
prob_heads = results.get('Heads', 0) / len(flips)
print('Estimated probability of heads:', prob_heads)
Estimated probability of heads: 0.4

Conditional Probability: What does it mean?#

Conditional probability answers: What is the chance of event A, given that event B happened? For example: What is the chance a randomly chosen Titanic passenger survived, if they were female? It is written P(A | B).

# Data setup
import seaborn as sns
titanic = sns.load_dataset('titanic')
print('Shape:', titanic.shape)
titanic.head()
Shape: (891, 15)
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
# Probability of survival overall
prob_survived = titanic['survived'].mean()
print('Probability any passenger survived:', prob_survived)
Probability any passenger survived: 0.3838383838383838
# Probability of survival, given the passenger was female
female = titanic[titanic['sex']=='female']
prob_survived_female = female['survived'].mean()
print('Probability of survival given female:', prob_survived_female)
Probability of survival given female: 0.7420382165605095
# Probability of survival, given the passenger was male
male = titanic[titanic['sex']=='male']
prob_survived_male = male['survived'].mean()
print('Probability of survival given male:', prob_survived_male)
Probability of survival given male: 0.18890814558058924
# Compare probabilities for different passenger classes
for pclass in [1,2,3]:
    class_group = titanic[titanic['pclass']==pclass]
    prob = class_group['survived'].mean()
    print(f'Survival chance in class {pclass}:', prob)
    
Survival chance in class 1: 0.6296296296296297
Survival chance in class 2: 0.47282608695652173
Survival chance in class 3: 0.24236252545824846

What is Independence?#

Two events are independent if knowing that one happens does not change the chance of the other. If events are not independent, their probabilities change when you know something. Example: Flipping a fair coin twicethe result of the first flip does not affect the second.

But, on the Titanic, survival and being female are not independent.

# Check independence by comparing probabilities
# If P(Survived|Female) = P(Survived), they are independent
print('P(Survived):', prob_survived)
print('P(Survived|Female):', prob_survived_female)
if np.isclose(prob_survived, prob_survived_female):
    print('Survival and being female are independent')
else:
    print('Survival and being female are not independent')
    
P(Survived): 0.3838383838383838
P(Survived|Female): 0.7420382165605095
Survival and being female are not independent
# Visualize survival by sex
import matplotlib.pyplot as plt
pd.crosstab(titanic['sex'], titanic['survived']).plot(kind='bar', stacked=True)
plt.xlabel('Sex')
plt.ylabel('Count')
plt.title('Titanic Survival by Sex')
plt.legend(['Did not survive', 'Survived'])
plt.show()
No description has been provided for this image
# Challenge! Type a name and see if you would survive, using real probabilities
name = input('Enter your first name: ')
sex = input('Are you male or female? ').strip().lower()
your_prob = prob_survived_female if sex=='female' else prob_survived_male
print(f'Hi {name.title()}, your chance of survival might have been:', your_prob)
Hi Alex, your chance of survival might have been: 0.7420382165605095
# Simulate two dice rolls, test independence
die1 = np.random.randint(1,7, size=1000)
die2 = np.random.randint(1,7, size=1000)
event_A = die1 == 6
event_B = die2 == 6
P_A = np.mean(event_A)
P_B = np.mean(event_B)
P_AB = np.mean(event_A & event_B)
print('P(A):', P_A)
print('P(B):', P_B)
print('P(A and B):', P_AB)
print('If independent, P(A and B) should equal P(A)*P(B):', P_A * P_B)
P(A): 0.171
P(B): 0.165
P(A and B): 0.027
If independent, P(A and B) should equal P(A)*P(B): 0.028215000000000004

Quick Recap#

  • Probability measures how likely events are.
  • Conditional probability asks about chances with context.
  • Independence means one event does not affect another.

Real datasets can help you see these ideas clearly.

# Challenge: What is the conditional probability a third class female survived?
third_females = titanic[(titanic['sex']=='female') & (titanic['pclass']==3)]
prob_third_female_survived = third_females['survived'].mean()
print('Chance a 3rd class female survived:', prob_third_female_survived)
Chance a 3rd class female survived: 0.5
# Experiment: What happens if we treat sex and class as independent?
P_class_3 = (titanic['pclass']==3).mean()
P_female = (titanic['sex']=='female').mean()
P_both_independent = P_class_3 * P_female
P_both_observed = ((titanic['pclass']==3) & (titanic['sex']=='female')).mean()
print('If independent, P(class 3 and female):', P_both_independent)
print('Observed P(class 3 and female):', P_both_observed)
If independent, P(class 3 and female): 0.19420290950406927
Observed P(class 3 and female): 0.16161616161616163

Next Steps and Tips#

  • Always define what your events mean.
  • Use real data to test how events relate.
  • If you are not sure about independence, calculate and compare like we did.

Try sharing this lesson or leave a comment about what you discovered!

You finished!#

  • Review: Try describing conditional probability to someone else.
  • Change code and rerun experiments.
  • Come up with your own real-world example and test it in Python.

Thanks for learning with us!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.