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…
- CourseProbability and Statistics in python
- Lesson8 of 35
- Video11 min
- FormatJupyter notebook · 15 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbWelcome 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)
# Count heads and tails
unique, counts = np.unique(flips, return_counts=True)
results = dict(zip(unique, counts))
print('Counts:', results)
# Calculate probability of heads
prob_heads = results.get('Heads', 0) / len(flips)
print('Estimated probability of heads:', prob_heads)
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()
# Probability of survival overall
prob_survived = titanic['survived'].mean()
print('Probability any passenger survived:', prob_survived)
# 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 the passenger was male
male = titanic[titanic['sex']=='male']
prob_survived_male = male['survived'].mean()
print('Probability of survival given male:', prob_survived_male)
# 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)
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')
# 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()
# 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)
# 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)
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)
# 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)
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.



