ML Notes
Core probability concepts for ML including Bayes theorem, conditional probability, joint distributions, and their applications in classification and generative models.
Probability theory is the language of uncertainty — and machine learning is fundamentally about making predictions under uncertainty. From Naive Bayes classifiers to Bayesian neural networks, probability is woven into the fabric of ML.
Core Probability Concepts
import numpy as np
# Basic probability: P(event) = favorable outcomes / total outcomes
# Example: Rolling a die
total_outcomes = 6
p_even = 3 / total_outcomes # P(even) = P(2,4,6) = 3/6
print(f"P(even number on die): {p_even:.4f}")
# Complement: P(not A) = 1 - P(A)
p_odd = 1 - p_even
print(f"P(odd number): {p_odd:.4f}")
# Union: P(A or B) = P(A) + P(B) - P(A and B)
p_even_or_gt4 = 3/6 + 2/6 - 1/6 # Even: {2,4,6}, >4: {5,6}, Both: {6}
print(f"P(even OR >4): {p_even_or_gt4:.4f}")Conditional Probability
# P(A|B) = P(A and B) / P(B)
# "Probability of A given that B happened"
# Example: Email classification
# P(spam) = 0.3, P(contains "free" | spam) = 0.8, P(contains "free" | not spam) = 0.1
p_spam = 0.3
p_free_given_spam = 0.8
p_free_given_not_spam = 0.1
# P(free) using total probability
p_free = p_free_given_spam * p_spam + p_free_given_not_spam * (1 - p_spam)
print(f"P(email contains 'free'): {p_free:.4f}")
# Bayes' Theorem: P(spam | free) = P(free | spam) * P(spam) / P(free)
p_spam_given_free = (p_free_given_spam * p_spam) / p_free
print(f"P(spam | contains 'free'): {p_spam_given_free:.4f}")
print("If an email contains 'free', there's a 77% chance it's spam!")Bayes' Theorem
Bayes in Action: Medical Diagnosis
# Disease testing scenario
# P(disease) = 0.001 (rare disease)
# P(positive test | disease) = 0.99 (sensitivity)
# P(positive test | no disease) = 0.05 (false positive rate)
p_disease = 0.001
p_pos_given_disease = 0.99
p_pos_given_no_disease = 0.05
# P(positive)
p_positive = p_pos_given_disease * p_disease + p_pos_given_no_disease * (1 - p_disease)
# P(disease | positive test)
p_disease_given_pos = (p_pos_given_disease * p_disease) / p_positive
print("Medical Test Scenario:")
print(f" Disease prevalence: {p_disease:.3%}")
print(f" Test sensitivity: {p_pos_given_disease:.1%}")
print(f" False positive rate: {p_pos_given_no_disease:.1%}")
print(f"\n P(disease | positive test): {p_disease_given_pos:.2%}")
print(" Even with a 'positive' test, only ~2% chance of disease!")
print(" This is why rare disease screening needs confirmation tests.")Probability Distributions in ML
Maximum Likelihood Estimation
import numpy as np
from scipy.optimize import minimize_scalar
# MLE: Find parameters that maximize P(data | parameters)
# Example: Estimating mean of normal distribution
np.random.seed(42)
data = np.random.normal(loc=5.0, scale=2.0, size=100)
# Log-likelihood for normal distribution
def neg_log_likelihood(mu, data=data, sigma=2.0):
n = len(data)
ll = -n/2 * np.log(2*np.pi*sigma**2) - np.sum((data - mu)**2) / (2*sigma**2)
return -ll # Negative because we minimize
result = minimize_scalar(neg_log_likelihood, bounds=(0, 10), method='bounded')
print(f"True mean: 5.0")
print(f"MLE estimate: {result.x:.4f}")
print(f"Sample mean: {data.mean():.4f}")
print("MLE for Gaussian mean = sample mean (they're the same!)")Joint and Marginal Probability
Interview Questions
- Explain Bayes' theorem in simple terms.
Bayes' theorem updates our initial belief (prior) about something after seeing new evidence (data). It combines what we knew before with what the data tells us to form an updated belief (posterior).
- Why is the base rate important in probability?
Even a highly accurate test can give misleading results when the base rate is very low (rare events). This is the base rate fallacy — you must consider prevalence when interpreting test results.
- What is the difference between likelihood and probability?
Probability is P(data | parameters) — given known parameters, how likely is the data. Likelihood is the same function but viewed as a function of parameters for fixed data — what parameters best explain the data.
- How does Naive Bayes use probability for classification?
It applies Bayes' theorem assuming features are independent given the class. P(class | features) ∝ P(class) × ∏P(feature_i | class). Despite the naive assumption, it works surprisingly well.
- What is the difference between frequentist and Bayesian approaches?
Frequentists treat parameters as fixed and data as random (probability = long-run frequency). Bayesians treat parameters as random with distributions and update beliefs with data (probability = degree of belief).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Probability Basics for Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, prerequisites, probability, basics, probability basics for machine learning
Related Machine Learning Topics