1. Sample Spaces, Events, and Probability Basics
Sample Space (S): The set of all possible outcomes of a process.
- Example: If you roll a standard 6-sided die, your sample space is S = 1, 2, 3, 4, 5, 6.
- Real-World ML Example: If a user visits your e-commerce site, the sample space for their immediate action might be S = {Buy, Abandon Cart, Keep Browsing}.
Event (E): A specific outcome (or combination of outcomes) you care about. An event is a subset of the sample space.
- Example: Rolling an even number on a die gives the event E = 2, 4, 6.
Probability (P(E)): A number between 0 and 1 that measures how likely event E is to happen.
- P(E) = 0 means the event is impossible.
- P(E) = 1 means the event is guaranteed to happen.
Kolmogorov's 3 Rules of Probability
- Rule 1 (Non-negativity): Probability can never be negative: P(E) >= 0.
- Rule 2 (Total Certainty): The probability of something in the sample space happening is always 1: P(S) = 1.
- Rule 3 (Additivity): If two events cannot happen at the same time (mutually exclusive), the probability of either happening is the sum of their individual probabilities:
2. Conditional Probability & Bayes' Theorem (The Spam Filter Analogy)
Real-World Example: Detecting Spam
- Event A: The email is Spam.
- Event B: The email contains the word "FREE".
- P(Spam): The general chance an email is spam (our Prior belief).
- P("FREE" | Spam): How often spam emails use the word "FREE" (the Likelihood).
- P("FREE"): How often the word "FREE" appears across all emails (the Evidence).
- P(Spam | "FREE"): The updated probability after seeing the word "FREE" (our Posterior belief).
3. Random Variables: Translating the World into Numbers
A. Discrete Random Variables (Countable Values)
- Examples: The number of customer support tickets received per hour (X = 0, 1, 2, 3, ...), or whether a user clicks an ad (X = 1 for click, X = 0 for no click).
B. Continuous Random Variables (Measurable Ranges)
- Examples: The exact time a user spends on a webpage (e.g., 14.352 seconds), or the exact price of a house.
4. Summarizing Data: Expectation and Variance
The Expected Value (E[X]): The Center of Mass
- For Discrete Variables:
- For Continuous Variables:
Variance (Var(X)): The Measure of Spread
5. The Central Limit Theorem (CLT): Why Bell Curves Rule the World
The Central Limit Theorem: No matter what shape your raw data starts with (skewed, flat, or discrete), the distribution of the sample averages will always approach a Normal Distribution as your sample size (n) gets larger (typically n >= 30).
6. Hands-On Python: Implementing Probability Concepts from Scratch
import random
import math
# ==========================================
# 1. Expected Value and Variance from Scratch
# ==========================================
# Define a discrete random variable (e.g., rolling a biased 6-sided die)
# Values: [1, 2, 3, 4, 5, 6]
outcomes = [1, 2, 3, 4, 5, 6]
probabilities = [0.1, 0.1, 0.1, 0.1, 0.1, 0.5] # Biased towards rolling a 6
def calculate_expected_value(values, probs):
"""Calculates E[X] = sum(x * p(x))"""
return sum(x * p for x, p in zip(values, probs))
def calculate_variance(values, probs):
"""Calculates Var(X) = E[X^2] - (E[X])^2"""
expected_x = calculate_expected_value(values, probs)
expected_x_squared = sum((x ** 2) * p for x, p in zip(values, probs))
return expected_x_squared - (expected_x ** 2)
mean = calculate_expected_value(outcomes, probabilities)
variance = calculate_variance(outcomes, probabilities)
print(f"Biased Die -> Expected Value E[X]: {mean:.2f}")
print(f"Biased Die -> Variance Var(X): {variance:.2f}")
# ==========================================
# 2. Simulating the Central Limit Theorem
# ==========================================
def get_skewed_sample(size):
"""Generates a highly skewed sample (exponential distribution)."""
return [random.expovariate(lambd=0.5) for _ in range(size)]
def simulate_central_limit_theorem(sample_size=35, num_experiments=1000):
"""
Repeatedly calculates the average of random samples
to demonstrate convergence to a normal distribution.
"""
sample_means = []
for _ in range(num_experiments):
sample = get_skewed_sample(sample_size)
sample_mean = sum(sample) / len(sample)
sample_means.append(sample_mean)
return sample_means
# Run simulation
sample_means = simulate_central_limit_theorem(sample_size=40, num_experiments=10000)
overall_mean = sum(sample_means) / len(sample_means)
print(f"\nCLT Simulation (10,000 experiments):")
print(f"Average of all Sample Means: {overall_mean:.2f} (Expected Population Mean: 2.00)")
Key Takeaways
- Probability is the mathematical foundation for handling uncertainty in AI and ML.
- Bayes' Theorem lets us update our prediction when new evidence arrives.
- Random Variables map real-world outcomes into numbers (Discrete for counts, Continuous for measurements).
- The Central Limit Theorem explains why sample averages follow a bell curve, making large-scale statistical inference possible.
Comments (0)
No comments yet. Be the first to share your thoughts.