S30 Logo
S30 AI Labwww.thes30.com
Back
#6

Probability Essentials

Easy📐 Math for MLW2 D3

Before You Start

Required knowledge
Fractions, conditional probability, and NumPy basics.
Libraries used
NumPy
Dataset and source
Explicit probability tables and simulated random draws; no downloaded data.
Expected output
Probability and Bayes calculations, standardized samples, and naive Bayes log-odds.

Probability Essentials — Theory & Concepts

Read this before attempting the exercises. Probability is the foundation of ML — every prediction is a probability estimate.


1. Core Concepts

  • ●Random variable — a variable whose value is determined by a random process
  • ●Probability distribution — describes the likelihood of each possible value
  • ●PMF (discrete) — P(X = x) for each value
  • ●PDF (continuous) — f(x) such that P(a ≤ X ≤ b) = ∫f(x)dx

2. Key Distributions

Bernoulli & Binomial

# Bernoulli: single coin flip (p = probability of success)
# Binomial: n independent coin flips
from scipy.stats import binom
P_3_heads_in_10 = binom.pmf(3, n=10, p=0.5)

Gaussian (Normal)

from scipy.stats import norm
# 68-95-99.7 rule: 68% within 1σ, 95% within 2σ, 99.7% within 3σ
P_within_2sigma = norm.cdf(2) - norm.cdf(-2)  # ≈ 0.954

Poisson

Models count of events in a fixed interval: P(X=k) = λᵏe⁻λ/k!

ML Use: Click counts, arrival rates, rare event modeling.


3. Bayes' Theorem

P(A|B) = P(B|A) x P(A) / P(B)

posterior = likelihood x prior / evidence

Example: Disease testing

  • ●P(disease) = 0.001 (prior)
  • ●P(positive | disease) = 0.99 (sensitivity)
  • ●P(positive | no disease) = 0.05 (false positive rate)
  • ●P(disease | positive) = 0.99 × 0.001 / (0.99×0.001 + 0.05×0.999) ≈ 1.9%

Even with a 99% accurate test, a positive result only means ~2% chance of disease when the base rate is low!

Interview Tip: This is a classic FAANG question. The key insight is that base rate matters enormously.


4. Expectation & Variance

E[X] = Σ xᵢ P(xᵢ)           # expected value (mean)
Var(X) = E[(X - E[X])²]      # variance (spread)
       = E[X²] - (E[X])²     # computational formula

Properties:

  • ●E[aX + b] = aE[X] + b
  • ●Var(aX + b) = a²Var(X)
  • ●If X, Y independent: Var(X + Y) = Var(X) + Var(Y)

5. Joint, Marginal, Conditional

Joint:       P(X, Y) — probability of both X and Y
Marginal:    P(X) = Σ_y P(X, Y=y) — sum out the other variable
Conditional: P(X|Y) = P(X, Y) / P(Y) — probability of X given Y

Independence: X and Y are independent iff P(X, Y) = P(X)P(Y)


6. Maximum Likelihood Estimation (MLE)

Given data, find the parameters that maximize the probability of observing that data:

θ_MLE = argmax_θ P(data | θ)
      = argmax_θ Σ log P(xᵢ | θ)    < log-likelihood (easier to optimize)

Example: For Gaussian data, MLE gives μ = sample mean, σ² = sample variance.

ML Connection: Training a logistic regression model = maximizing log-likelihood of the labels.


7. Information Theory Basics

Entropy:         H(X) = -Σ P(x) log P(x)     # uncertainty/surprise
Cross-entropy:   H(P,Q) = -Σ P(x) log Q(x)   # cost of using Q to encode P
KL divergence:   KL(P||Q) = Σ P(x) log(P(x)/Q(x))  # "distance" between distributions

ML Connection: Cross-entropy loss in classification is literally the cross-entropy between true labels and predicted probabilities.


Now you're ready to attempt the exercises. Switch to the Exercise tab and start coding!

Ready to practice?

Python 3 — Notebook
0/6 solvedSubstack Notes
1
Dataset & Setup

Probability Essentials — FAANG-Level Lab

Goal: ML-relevant probability: expectation, variance, Bayes, and simulation checks.

Outcome: You can reason about uncertainty, distributions, and Bayes updates (interview-ready).

Loading editor...
1

Discrete Random Variables

2
Expectation & variance from a PMF
2

Section 1 — Discrete Random Variables

Task 1.1: Expectation & variance from a PMF

Given values x and probabilities p (sum to 1):

  • ●

    implement E[X] and Var(X)

  • ●

    E[X] = sum p_i x_i

  • ●

    Var(X) = E[X^2] - (E[X])^2

Explain: Why is variance not linear, but expectation is?

Explain: Why is variance not linear, but expectation is?
Loading editor...
2

Conditional Probability & Bayes

3
Bayes theorem (classic interview)
1

Section 2 — Conditional Probability & Bayes

Task 2.1: Bayes theorem (classic interview)

Disease test example:

  • ●prevalence P(D)=0.01
  • ●sensitivity P(+|D)=0.99
  • ●false positive rate P(+|~D)=0.05 Compute P(D|+)

P(D|+) = P(+|D)P(D) / (P(+|D)P(D) + P(+|~D)P(~D))

FAANG gotcha: base-rate fallacy.

Loading editor...
4
Simulation check (sanity)
1

Task 2.2: Simulation check (sanity)

Simulate N people and estimate P(D|+) empirically.

  • ●sample disease ~ Bernoulli(P_D)
  • ●sample test result conditional on disease

Explain: Why does simulation converge to the analytic value?

Explain: Why does simulation converge to the analytic value?
Loading editor...
3

Continuous Distributions (Normal)

5
Standardization (z-score)
2

Section 3 — Continuous Distributions (Normal)

Task 3.1: Standardization (z-score)

Given X ~ Normal(mu, sigma^2). Compute standardized Z=(X-mu)/sigma.

  • ●simulate X and check Z mean ~0, std ~1

ML link: standardization shows up in preprocessing and SGD stability.

Loading editor...
4

Naive Bayes Thinking (Optional Mini)

6
Compute log-odds for a toy Naive Bayes
1

Section 4 — Naive Bayes Thinking (Optional Mini)

Task 4.1: Compute log-odds for a toy Naive Bayes

Given word likelihoods for spam vs ham, compute posterior odds for a message.

  • ●work in log space (sum logs)

FAANG gotcha: multiplying small probabilities underflows; use logs.

Loading editor...

Need help? Share feedback