5 min read min read
Probability Basics
Learn fundamental probability concepts
Probability Basics
What is Probability?
Probability measures how likely something is to happen.
- 0 = impossible
- 1 = certain
- 0.5 = 50% chance
Basic Formula
Probability = Favorable outcomes / Total outcomes
Simple Examples
Coin Flip
code.py
# Probability of heads
favorable = 1 # heads
total = 2 # heads or tails
p_heads = favorable / total
print(p_heads) # 0.5Dice Roll
code.py
# Probability of rolling a 6
favorable = 1 # only one 6
total = 6 # numbers 1-6
p_six = favorable / total
print(p_six) # 0.167Simulating Probability
code.py
import numpy as np
# Simulate 10000 coin flips
flips = np.random.choice(['heads', 'tails'], size=10000)
# Count heads
heads_count = np.sum(flips == 'heads')
probability = heads_count / 10000
print(f"Probability of heads: {probability}") # ~0.5Complement Rule
Probability something does NOT happen:
P(not A) = 1 - P(A)
code.py
p_rain = 0.3
p_no_rain = 1 - p_rain
print(p_no_rain) # 0.7Addition Rule (OR)
Probability of A or B happening:
code.py
# Probability of rolling 1 OR 2
p_one = 1/6
p_two = 1/6
# If events can't happen together (mutually exclusive)
p_one_or_two = p_one + p_two
print(p_one_or_two) # 0.333Multiplication Rule (AND)
Probability of A and B happening:
code.py
# Probability of two heads in a row
p_heads = 0.5
# If events are independent
p_two_heads = p_heads * p_heads
print(p_two_heads) # 0.25Independent Events
Events that don't affect each other:
code.py
# Each coin flip is independent
p_heads = 0.5
# 5 heads in a row
p_five_heads = p_heads ** 5
print(p_five_heads) # 0.03125Conditional Probability
Probability of A given B happened:
P(A|B) = P(A and B) / P(B)
code.py
# 60% of customers are female
# 40% of females buy product
# What's P(buy | female)?
p_female = 0.60
p_buy_and_female = 0.24
p_buy_given_female = p_buy_and_female / p_female
print(p_buy_given_female) # 0.4Bayes' Theorem
P(A|B) = P(B|A) * P(A) / P(B)
code.py
# Disease test example
# P(disease) = 0.01 (1% have disease)
# P(positive | disease) = 0.99 (99% accuracy)
# P(positive | no disease) = 0.05 (5% false positive)
p_disease = 0.01
p_positive_given_disease = 0.99
p_positive_given_no_disease = 0.05
# P(positive)
p_positive = (p_positive_given_disease * p_disease +
p_positive_given_no_disease * (1 - p_disease))
# P(disease | positive)
p_disease_given_positive = (p_positive_given_disease * p_disease) / p_positive
print(f"P(disease|positive): {p_disease_given_positive:.2%}") # ~16.7%Even with 99% accurate test, only 17% chance of actually having disease!
Expected Value
Average outcome over many tries:
code.py
# Dice roll expected value
outcomes = [1, 2, 3, 4, 5, 6]
probability = 1/6
expected = sum(outcome * probability for outcome in outcomes)
print(expected) # 3.5Complete Example
code.py
import numpy as np
# Simulate dice rolls
np.random.seed(42)
rolls = np.random.randint(1, 7, size=10000)
# Calculate probabilities
print("=== Dice Roll Probabilities ===")
for i in range(1, 7):
prob = np.sum(rolls == i) / len(rolls)
print(f"P({i}): {prob:.3f}")
# P(even number)
p_even = np.sum(rolls % 2 == 0) / len(rolls)
print(f"P(even): {p_even:.3f}")
# P(greater than 4)
p_gt_4 = np.sum(rolls > 4) / len(rolls)
print(f"P(>4): {p_gt_4:.3f}")Key Points
- Probability is between 0 and 1
- P(not A) = 1 - P(A)
- P(A or B) = P(A) + P(B) (if mutually exclusive)
- P(A and B) = P(A) × P(B) (if independent)
- Conditional probability considers given info
- Bayes' theorem updates probability with new evidence
What's Next?
Learn about probability distributions.