Skip to content
Machine Learning & Data Science

Probability interview questions and answers

45 questions, 45 with code. About 2 h 27 min to read them all.

By TestLab · Updated

Plan for an interview
45 questions

Track what you know and what to revise.

Sign in to track progress

You can open a few full answers without signing in. Sign in to read all

1

What is probability and how is it used in machine learning?

Probability is the mathematics of uncertainty: it assigns each event a number between 0 and 1 that follows three axioms (probabilities are non-negative, the whole sample space has probability 1, and the probabilities of mutually exclusive events add up), and it can be read as a long-run frequency or as a degree of belief. Machine learning uses it everywhere: data are modelled as samples from probability distributions, models are trained by maximizing likelihood (cross-entropy and mean squared error are negative log-likelihoods), classifiers output class probabilities, Bayesian methods combine priors with data, and evaluation, calibration, A/B testing and uncertainty estimates all rest on probability theory.

Read full answer

Applies togeneral concepts; examples in Python 3.12 with NumPy 2.x and SciPy 1.13+

Where Probability Appears in ML

Area Example
Data modelling Features and noise as random variables; Gaussian noise in regression
Training objectives Maximum likelihood: log loss for classification, MSE for Gaussian regression
Model outputs predict_proba, softmax probabilities
Bayesian methods Priors, posteriors, Naive Bayes, Bayesian optimization
Generative models VAEs, diffusion models, language models predicting next-token probabilities
Evaluation Confidence intervals, hypothesis tests, calibration
Decision making Expected cost, thresholds, reinforcement learning

Code Example: Probability Behind a Classifier

import numpy as np
from scipy import stats
from sklearn.calibration import calibration_curve
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss
from sklearn.model_selection import train_test_split

rng = np.random.default_rng(42)
p_exact = 1 - stats.binom.cdf(5, 10, 0.5)         # P(more than 5 heads in 10 fair flips)
p_sim = np.mean(rng.binomial(10, 0.5, 100_000) > 5)
print(f"exact {p_exact:.4f}, simulated {p_sim:.4f}")   # exact 0.3770 (386/1024)

X, y = make_classification(n_samples=4000, random_state=42)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=42)
proba = LogisticRegression(max_iter=1000).fit(X_tr, y_tr).predict_proba(X_te)[:, 1]
print(f"log loss (negative log-likelihood per sample): {log_loss(y_te, proba):.3f}")
frac_pos, mean_pred = calibration_curve(y_te, proba, n_bins=5)
print(np.c_[mean_pred.round(2), frac_pos.round(2)])   # predicted vs observed rates: close if calibrated

P(more than 5 heads) = Σ from k = 6 to 10 of C(10, k)/2¹⁰ = (210 + 120 + 45 + 10 + 1)/1024 = 386/1024 ≈ 0.377.

Common Pitfalls

  • Treating model scores as probabilities without checking calibration.

  • Confusing P(A | B) with P(B | A) (the base-rate fallacy).

  • Assuming independence where it doesn’t hold.

Interview Tips

  • Give the axioms briefly, then several concrete ML uses.

  • Connect loss functions to likelihood.

2

What are a sample space and an event in probability?

The sample space Ω is the set of all possible outcomes of a random experiment (for one die roll, Ω = {1, 2, 3, 4, 5, 6}), and an event is any subset of the sample space, a collection of outcomes we care about (rolling an even number is the event A = {2, 4, 6}); an event “occurs” when the experiment’s outcome is one of its elements. A probability measure assigns each event a number in [0, 1] with P(Ω) = 1, and events combine with set operations: union (A or B), intersection (A and B) and complement (not A). With equally likely outcomes, P(A) = |A| / |Ω|, so P(even) = 3/6 = 0.5.

Read full answer

Applies togeneral concepts; example in Python 3.12

Key Terms

Term Definition Die example
Outcome A single result 4
Sample space Ω All outcomes {1, …, 6}
Event Subset of Ω Even = {2, 4, 6}
Elementary event One outcome {4}
Complement Aᶜ Outcomes not in A Odd = {1, 3, 5}
Union A ∪ B A or B (or both) Even or > 4 = {2, 4, 5, 6}
Intersection A ∩ B Both A and B Even and > 4 = {6}
Mutually exclusive A ∩ B = ∅ Even and odd
Impossible / certain event ∅ / Ω Rolling 7 / rolling 1–6

Code Example

from fractions import Fraction
from itertools import product

omega = set(product(range(1, 7), repeat=2))          # two dice: 36 equally likely outcomes

def P(event):
    return Fraction(len(event), len(omega))

sum_7 = {o for o in omega if sum(o) == 7}
doubles = {o for o in omega if o[0] == o[1]}
first_even = {o for o in omega if o[0] % 2 == 0}

print(len(omega), P(sum_7), P(doubles))               # 36 1/6 1/6
print(P(sum_7 | doubles), P(sum_7 & doubles))         # 1/3 0: mutually exclusive (a double sums to an even number)
print(P(omega - first_even))                          # 1/2: complement rule 1 - P(A)
print(P(sum_7 | first_even) == P(sum_7) + P(first_even) - P(sum_7 & first_even))   # True: inclusion-exclusion

Types of Sample Spaces

Type Example
Finite Coin flip {H, T}
Countably infinite Number of emails until spam: {1, 2, 3, …}
Continuous Waiting time: [0, ∞)

For continuous sample spaces, single outcomes have probability 0 and probabilities are assigned to intervals.

Common Pitfalls

  • Treating outcomes as equally likely when they aren’t (sums of two dice).

  • Forgetting the overlap when adding probabilities of non-exclusive events.

  • Confusing an outcome with an event.

Interview Tips

  • Define both with a concrete example and show set operations.

  • Mention inclusion-exclusion: P(A ∪ B) = P(A) + P(B) − P(A ∩ B).

3

What is the difference between discrete and continuous probability distributions?

A discrete distribution describes a random variable that takes countable values (0, 1, 2, … or a finite set of categories) and assigns each value a probability through a probability mass function (PMF), with P(X = x) ≥ 0 and the values summing to 1; a continuous distribution describes a variable that can take any value in an interval and is described by a probability density function (PDF), where probabilities come from areas under the curve, P(a ≤ X ≤ b) = ∫ f(x) dx, so any single exact value has probability 0 and the density itself can exceed 1. Both have a cumulative distribution function F(x) = P(X ≤ x): a step function for discrete variables and a smooth curve for continuous ones.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Comparison

Aspect Discrete Continuous
Values Countable (integers, categories) Uncountable (intervals of real numbers)
Described by PMF p(x) = P(X = x) PDF f(x)
P(X = x) Can be > 0 Always 0
Probabilities Sums: Σ p(x) Integrals: ∫ f(x) dx
Normalization Σ p(x) = 1 ∫ f(x) dx = 1
Can exceed 1? No The density can (e.g. narrow normal)
CDF shape Steps Continuous curve
Examples Bernoulli, binomial, Poisson, geometric Uniform, normal, exponential, beta, gamma
ML examples Class labels, word counts, clicks Heights, prices, sensor readings, weights

Code Example

import numpy as np
from scipy import integrate, stats

poisson = stats.poisson(mu=3)                       # discrete: events per hour
print(round(poisson.pmf(2), 4))                     # P(X = 2) = e^-3 * 3^2 / 2! = 0.2240
print(round(sum(poisson.pmf(k) for k in range(100)), 6))   # sums to 1
print(round(poisson.cdf(2), 4))                     # P(X <= 2) = 0.4232

normal = stats.norm(loc=0, scale=0.1)               # continuous with a narrow spread
print(round(normal.pdf(0), 3))                      # 3.989: a density can be above 1
print(round(normal.cdf(0.1) - normal.cdf(-0.1), 4)) # P(-0.1 <= X <= 0.1) = 0.6827 (within 1 sd)
area, _ = integrate.quad(normal.pdf, -np.inf, np.inf)
print(round(area, 6))                               # 1.0
print(normal.cdf(0.05) - normal.cdf(0.05))          # 0.0: a single point has zero probability

Mixed and Practical Cases

  • Rounded measurements (age in whole years) are technically discrete but often modelled as continuous.

  • Mixed distributions occur too, such as rainfall with many exact zeros plus continuous positive amounts (zero-inflated models).

Common Pitfalls

  • Reading a PDF value as a probability.

  • Using a continuous model (normal) for small counts where a Poisson or negative binomial fits better.

Interview Tips

  • Contrast PMF vs PDF and sums vs integrals.

  • Give the “density can exceed 1” and “single point has probability 0” facts.

4

What is the difference between joint, marginal and conditional probability?

Joint probability P(A, B) is the probability that two events happen together; marginal probability P(A) is the probability of one event on its own, obtained by summing (or integrating) the joint probability over all values of the other variable; and conditional probability P(A | B) = P(A, B) / P(B) is the probability of A given that B is known to have happened, which rescales the joint probability to the world where B is true. They are linked by the product rule P(A, B) = P(A | B)·P(B) = P(B | A)·P(A), which leads directly to Bayes’ theorem, and A and B are independent exactly when P(A | B) = P(A).

Read full answer

Applies togeneral concepts; example with pandas 2.x

Worked Example: Email Data

Contains “free” No “free” Marginal
Spam 0.12 0.08 P(spam) = 0.20
Not spam 0.04 0.76 P(not spam) = 0.80
Marginal P(free) = 0.16 0.84 1.00
  • Joint: P(spam, free) = 0.12.

  • Marginal: P(free) = 0.12 + 0.04 = 0.16.

  • Conditional: P(spam | free) = 0.12 / 0.16 = 0.75, while P(free | spam) = 0.12 / 0.20 = 0.60.

Code Example

import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
n = 100_000
spam = rng.random(n) < 0.20
free = np.where(spam, rng.random(n) < 0.60, rng.random(n) < 0.05)   # P(free|spam)=0.6, P(free|not spam)=0.05
df = pd.DataFrame({"spam": spam, "free": free})

joint = pd.crosstab(df["spam"], df["free"], normalize="all")         # joint table
print(joint.round(3))
print("P(free) marginal:", round(joint[True].sum(), 3))               # about 0.16
print("P(spam | free):", round(df.loc[df["free"], "spam"].mean(), 3)) # about 0.75
print("P(free | spam):", round(df.loc[df["spam"], "free"].mean(), 3)) # about 0.60

Exact values: P(free) = 0.2·0.6 + 0.8·0.05 = 0.12 + 0.04 = 0.16, and P(spam | free) = 0.12 / 0.16 = 0.75.

Summary

Type Notation Computed from the joint table
Joint P(A, B) A cell
Marginal P(A) Row or column total
Conditional P(A | B) Cell ÷ column (or row) total

Common Pitfalls

  • Confusing P(A | B) with P(B | A).

  • Forgetting to normalize by P(B) when conditioning.

  • Assuming joint = product of marginals without independence.

Interview Tips

  • Use a 2 × 2 table to show all three.

  • State the product rule and link it to Bayes’ theorem.

5

What does it mean for two events to be independent?

Two events A and B are independent if knowing that one occurred doesn’t change the probability of the other: P(A | B) = P(A), or equivalently P(A ∩ B) = P(A)·P(B). For example, two fair coin flips are independent, so P(both heads) = 0.5 × 0.5 = 0.25, while drawing two cards without replacement is not, because the first draw changes what is left. Independence is different from being mutually exclusive (exclusive events with non-zero probabilities are always dependent, since one rules out the other), and pairwise independence of several events doesn’t guarantee mutual independence. Many ML methods assume independence, such as i.i.d. training samples and Naive Bayes’s conditional independence of features given the class.

Read full answer

Applies togeneral concepts; example in Python 3.12 with NumPy 2.x

Related Ideas

Concept Definition
Independence P(A ∩ B) = P(A)·P(B)
Conditional independence P(A ∩ B | C) = P(A | C)·P(B | C)
Mutually exclusive P(A ∩ B) = 0
Random variables independent Joint distribution = product of marginals for all values
Uncorrelated Covariance 0 (weaker than independence)

Worked Check

One die roll: A = even = {2, 4, 6}, B = at most 4 = {1, 2, 3, 4}.

  • P(A) = 1/2, P(B) = 4/6 = 2/3, P(A ∩ B) = P({2, 4}) = 2/6 = 1/3.

  • P(A)·P(B) = 1/2 · 2/3 = 1/3, so A and B are independent.

  • With C = at most 3 = {1, 2, 3}: P(A ∩ C) = 1/6 but P(A)·P© = 1/4, so A and C are dependent.

Code Example

import numpy as np

rng = np.random.default_rng(42)
rolls = rng.integers(1, 7, 1_000_000)
A, B, C = rolls % 2 == 0, rolls <= 4, rolls <= 3

def check(X, Y):
    return round(np.mean(X & Y), 4), round(np.mean(X) * np.mean(Y), 4)

print("A,B:", check(A, B))           # both about 0.3333: independent
print("A,C:", check(A, C))           # about 0.1667 vs 0.25: dependent

x = rng.normal(size=100_000)
y = x ** 2                           # fully dependent on x
print("corr(x, x^2):", round(float(np.corrcoef(x, y)[0, 1]), 3))   # about 0: uncorrelated but not independent

Common Pitfalls

  • Treating mutually exclusive events as independent.

  • Concluding independence from zero correlation.

  • Assuming i.i.d. data for time series or grouped data, which breaks standard validation.

Interview Tips

  • Give both definitions and a numeric check.

  • Distinguish independence from exclusivity and from zero correlation.

6

What is Bayes' theorem? Give an example of its use.

Bayes’ theorem reverses a conditional probability: P(H | E) = P(E | H)·P(H) / P(E), where P(H) is the prior belief in a hypothesis, P(E | H) the likelihood of the evidence if the hypothesis is true, P(E) the overall probability of the evidence (by total probability, P(E | H)P(H) + P(E | not H)P(not H)), and P(H | E) the updated, posterior belief. The classic example is a medical test: even an accurate test for a rare disease gives mostly false positives, because the low base rate (prior) dominates. In ML, Bayes’ theorem underlies Naive Bayes classifiers, Bayesian inference over model parameters, spam filters and probabilistic reasoning about predictions.

Read full answer

Applies togeneral concepts; example in Python 3.12 with NumPy 2.x

Worked Example: Medical Test

  • Disease prevalence P(D) = 1% (0.01).

  • Sensitivity P(+ | D) = 95%.

  • False positive rate P(+ | not D) = 5%.

P(+) = 0.95·0.01 + 0.05·0.99 = 0.0095 + 0.0495 = 0.059.

P(D | +) = 0.0095 / 0.059 ≈ 0.161: only about 16% of positive results are true cases.

In counts: out of 10,000 people, 100 are sick and 95 test positive; of the 9,900 healthy, 495 test positive. So 95 of 590 positives are sick (16.1%).

Code Example

import numpy as np

def posterior(prior, sensitivity, false_positive_rate):
    evidence = sensitivity * prior + false_positive_rate * (1 - prior)
    return sensitivity * prior / evidence

print(round(posterior(0.01, 0.95, 0.05), 4))           # 0.161
print(round(posterior(0.161, 0.95, 0.05), 4))          # a second positive test: 0.7848

rng = np.random.default_rng(42)
n = 2_000_000
sick = rng.random(n) < 0.01
positive = np.where(sick, rng.random(n) < 0.95, rng.random(n) < 0.05)
print(round(sick[positive].mean(), 4))                 # simulation: about 0.161

for prevalence in [0.001, 0.01, 0.1, 0.5]:            # the prior matters
    print(prevalence, round(posterior(prevalence, 0.95, 0.05), 3))
# 0.001 -> 0.019, 0.01 -> 0.161, 0.1 -> 0.679, 0.5 -> 0.95

Terms

Term Medical example
Prior P(H) 1% prevalence
Likelihood P(E | H) 95% sensitivity
Evidence P(E) 5.9% of all tests are positive
Posterior P(H | E) 16.1% chance of disease after a positive test

Uses in ML

  • Naive Bayes: P(class | features) ∝ P(class)·Π P(feature | class).

  • Bayesian parameter estimation: posterior ∝ likelihood × prior.

  • Spam filtering, diagnosis systems, Bayesian A/B testing, Bayesian optimization.

Common Pitfalls

  • Ignoring the base rate (the prosecutor’s or base-rate fallacy).

  • Confusing sensitivity P(+ | D) with the positive predictive value P(D | +).

Interview Tips

  • Write the formula, then work the medical example with counts.

  • Show how a second test (posterior as new prior) changes the answer.

7

What is a probability density function (PDF)?

A probability density function f(x) describes how probability is spread over the values of a continuous random variable: it is non-negative, its total area is 1 (∫ f(x) dx = 1), and the probability that the variable falls in an interval is the area under the curve over that interval, P(a ≤ X ≤ b) = ∫ₐᵇ f(x) dx. The value f(x) itself is a density, not a probability: it can be larger than 1, and the probability of any single exact value is 0. The PDF is the derivative of the cumulative distribution function, f(x) = F′(x), and in ML densities appear in likelihoods (Gaussian likelihood gives mean squared error), Gaussian Naive Bayes, kernel density estimation, anomaly detection and generative models.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Common PDFs

Distribution PDF Mean Variance
Uniform(a, b) 1/(b − a) on [a, b] (a + b)/2 (b − a)²/12
Normal(μ, σ²) (1/(σ√(2π)))·exp(−(x − μ)²/(2σ²)) μ σ²
Exponential(λ) λe^(−λx), x ≥ 0 1/λ 1/λ²
Beta(α, β) x^(α−1)(1 − x)^(β−1)/B(α, β) on [0, 1] α/(α + β) αβ/((α + β)²(α + β + 1))

Code Example

import numpy as np
from scipy import integrate, stats
from scipy.stats import gaussian_kde

expo = stats.expon(scale=1 / 0.5)                      # exponential with rate 0.5 (mean 2)
area, _ = integrate.quad(expo.pdf, 1, 3)
print(round(area, 4), round(np.exp(-0.5) - np.exp(-1.5), 4))   # 0.3834 both: P(1 <= X <= 3)

narrow = stats.norm(0, 0.2)
print(round(narrow.pdf(0), 3))                         # 1.995: a density above 1 is fine

x, h = 0.1, 1e-5
print(np.isclose((narrow.cdf(x + h) - narrow.cdf(x - h)) / (2 * h), narrow.pdf(x)))   # True: f = F'

rng = np.random.default_rng(42)
samples = np.r_[rng.normal(-2, 0.5, 500), rng.normal(1, 1.0, 500)]   # bimodal data
kde = gaussian_kde(samples)                            # estimate the PDF from data
print(round(float(kde(-2.0)[0]), 3), round(float(kde(0.0)[0]), 3))   # higher density near a peak

P(1 ≤ X ≤ 3) for an exponential with rate 0.5 is e^(−0.5) − e^(−1.5) ≈ 0.6065 − 0.2231 = 0.3834.

Common Pitfalls

  • Reading f(x) as P(X = x).

  • Forgetting that densities change under transformations (a change of variables needs the Jacobian).

  • Comparing densities of variables with different units.

Interview Tips

  • Define the PDF through areas and its relation to the CDF.

  • Mention that densities can exceed 1 and single points have probability 0.

8

What is the role of the cumulative distribution function (CDF)?

The cumulative distribution function F(x) = P(X ≤ x) gives the probability that a random variable is at most x; it exists for every random variable, discrete or continuous, rises from 0 to 1, never decreases, and turns interval questions into simple subtraction: P(a < X ≤ b) = F(b) − F(a). For continuous variables it is the integral of the PDF (and the PDF is its derivative), while for discrete variables it is a step function that jumps at each possible value. Its inverse, the quantile function F⁻¹(p), gives percentiles, critical values and confidence-interval bounds, and lets you generate samples from any distribution (inverse-transform sampling); the empirical CDF of data underlies the Kolmogorov-Smirnov test used to detect data drift.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Uses

Use How the CDF helps
Interval probabilities F(b) − F(a)
Tail probabilities, p-values 1 − F(x) (the survival function)
Percentiles and quantiles F⁻¹(p), e.g. the median is F⁻¹(0.5)
Sampling X = F⁻¹(U) with U ~ Uniform(0, 1)
Comparing distributions KS test: max distance between two CDFs
Model evaluation Calibration and probability integral transform checks

Code Example

import numpy as np
from scipy import stats

norm = stats.norm(loc=100, scale=15)                 # IQ-style scores
print(round(norm.cdf(115) - norm.cdf(85), 4))         # 0.6827: within one standard deviation
print(round(norm.sf(130), 4))                        # 0.0228: P(X > 130) = 1 - F(130)
print(round(norm.ppf(0.95), 2))                      # 124.67: the 95th percentile

binom = stats.binom(n=10, p=0.5)                     # discrete: step-shaped CDF
print(round(binom.cdf(3), 4), round(binom.cdf(3.5), 4))   # 0.1719 0.1719: flat between integers

rng = np.random.default_rng(42)                      # inverse-transform sampling for an exponential
u = rng.random(100_000)
samples = -np.log(1 - u) / 2.0                        # F^-1(u) for rate 2
print(round(samples.mean(), 3))                      # about 0.5 = 1/rate

train = rng.normal(0, 1, 2000)
prod = rng.normal(0.3, 1, 2000)                      # shifted production data
stat, p = stats.ks_2samp(train, prod)                # compares empirical CDFs
print(f"KS statistic {stat:.3f}, p-value {p:.1e}")   # small p-value: distributions differ

For the binomial, F(3) = (1 + 10 + 45 + 120)/1024 = 176/1024 ≈ 0.1719.

Common Pitfalls

  • Mixing up P(X ≤ x) and P(X < x) for discrete variables (they differ at the jump points).

  • Reading the CDF height as a density.

  • Using 1 − F(x) with floating point for extreme tails; use the survival function sf for accuracy.

Interview Tips

  • Define F(x) and show F(b) − F(a).

  • Mention quantiles, inverse-transform sampling and KS tests.

9

What is the Central Limit Theorem and why does it matter in machine learning?

The Central Limit Theorem (CLT) says that if you take the mean of n independent, identically distributed random variables with finite mean μ and finite variance σ², then as n grows the distribution of the sample mean approaches a normal distribution with mean μ and standard deviation σ/√n, whatever the shape of the original distribution; equivalently, √n·(X̄ − μ)/σ converges to the standard normal. This is why normal-based confidence intervals and tests work for averages such as model accuracy, A/B test conversion rates or mean errors, why errors shrink like 1/√n as you collect more data, and why many aggregated quantities look Gaussian. It requires finite variance (it fails for heavy-tailed distributions like the Cauchy), independence (or weak dependence), and a large enough n, which depends on how skewed the data are.

Read full answer

Applies togeneral concepts; example with NumPy 2.x and SciPy 1.13+

Uses in ML

Use How the CLT helps
Confidence intervals for metrics accuracy ± 1.96·√(p(1 − p)/n)
A/B tests z-tests on conversion rate differences
Cross-validation Mean score ± standard error across folds (folds aren’t fully independent, so treat it as approximate)
Mini-batch SGD Gradient noise roughly Gaussian with variance ∝ 1/batch size
Bootstrapping Sampling distributions of statistics
Monte Carlo estimates Error decreases as 1/√n

Code Example

import numpy as np
from scipy import stats

rng = np.random.default_rng(42)
population = rng.exponential(scale=2.0, size=1_000_000)      # strongly skewed, mean 2, sd 2

for n in [2, 10, 50, 200]:
    means = rng.choice(population, size=(20_000, n)).mean(axis=1)
    print(f"n={n:3d}: mean {means.mean():.3f}, sd {means.std():.3f} (theory {2 / np.sqrt(n):.3f}), "
          f"skewness {stats.skew(means):.2f}")
# Skewness falls toward 0 and the sd matches sigma / sqrt(n) as n grows

correct, n_test = 870, 1000                                   # a classifier's test accuracy
p_hat = correct / n_test
se = np.sqrt(p_hat * (1 - p_hat) / n_test)
print(f"accuracy {p_hat:.3f}, 95% CI [{p_hat - 1.96 * se:.3f}, {p_hat + 1.96 * se:.3f}]")   # [0.849, 0.891]

cauchy_means = stats.cauchy.rvs(size=(2000, 1000), random_state=42).mean(axis=1)
print("Cauchy sample means still spread out:", round(float(np.percentile(np.abs(cauchy_means), 90)), 2))

The interval: √(0.87·0.13/1000) ≈ 0.01063, so 0.87 ± 0.0208, about [0.849, 0.891].

Common Pitfalls

  • Saying the CLT makes the data itself normal; it is about the distribution of means (and sums).

  • Applying it to heavy-tailed data without finite variance.

  • Ignoring dependence (time series, grouped data), which makes standard errors too small.

Interview Tips

  • State the theorem with its conditions and σ/√n.

  • Give ML uses such as confidence intervals for accuracy and A/B tests.

10

What is the Law of Large Numbers?

The Law of Large Numbers (LLN) says that the average of many independent, identically distributed observations converges to the expected value as the number of observations grows: if X₁, X₂, … have mean μ, the sample mean X̄ₙ approaches μ as n → ∞. The weak law states convergence in probability (for any tolerance ε, P(|X̄ₙ − μ| > ε) → 0) and the strong law states almost-sure convergence; both need a finite mean, and the simple proof via Chebyshev’s inequality uses finite variance. It justifies estimating probabilities by frequencies, Monte Carlo simulation, using a test-set average as an estimate of true error, and empirical risk minimization; it says nothing about the shape of the error distribution, which is what the Central Limit Theorem adds.

Read full answer

Applies togeneral concepts; example with NumPy 2.x

LLN vs CLT

Law of Large Numbers Central Limit Theorem
Says X̄ₙ → μ X̄ₙ is approximately Normal(μ, σ²/n)
About Where the average goes How the average fluctuates
Needs Finite mean (i.i.d.) Finite variance (i.i.d.)
Gives Consistency Confidence intervals, error rates

Code Example

import numpy as np

rng = np.random.default_rng(42)
rolls = rng.integers(1, 7, 100_000)                       # fair die, expected value 3.5
running_mean = np.cumsum(rolls) / np.arange(1, len(rolls) + 1)
for n in [10, 100, 1_000, 10_000, 100_000]:
    print(f"n={n:6d}: mean {running_mean[n - 1]:.4f}, error {abs(running_mean[n - 1] - 3.5):.4f}")
# The error shrinks roughly like 1/sqrt(n)

inside = rng.random((1_000_000, 2))                       # Monte Carlo estimate of pi
pi_hat = 4 * np.mean((inside ** 2).sum(axis=1) <= 1)
print(f"pi estimate {pi_hat:.4f} vs {np.pi:.4f}")

cauchy = rng.standard_cauchy(1_000_000)                   # no finite mean: the LLN doesn't apply
cauchy_mean = np.cumsum(cauchy) / np.arange(1, len(cauchy) + 1)
print("Cauchy running mean at 10^4, 10^5, 10^6:", np.round(cauchy_mean[[9_999, 99_999, 999_999]], 2))

In ML

  • Test error on n samples estimates true error and improves with more test data.

  • Training minimizes the average loss on samples, which approximates expected loss (empirical risk minimization).

  • Monte Carlo methods (dropout sampling, MCMC, RL returns) rely on averages converging.

Common Pitfalls

  • The gambler’s fallacy: the LLN doesn’t mean outcomes “balance out” in the short run.

  • Applying it to dependent or non-identically distributed data without care.

  • Forgetting that convergence can be slow for high-variance quantities.

Interview Tips

  • State it precisely with the finite-mean condition.

  • Contrast it with the CLT and give a Monte Carlo example.

11

What are expectation, variance and covariance?

Expectation E[X] is the probability-weighted average value of a random variable (Σ x·p(x) for discrete variables, ∫ x·f(x) dx for continuous ones), its long-run mean; variance Var(X) = E[(X − E[X])²] = E[X²] − (E[X])² measures how spread out the values are around that mean, and its square root is the standard deviation; covariance Cov(X, Y) = E[(X − E[X])(Y − E[Y])] measures whether two variables move together (positive), in opposite directions (negative), or show no linear relationship (zero). Correlation is covariance scaled to [−1, 1] by the two standard deviations. Key rules: expectation is linear, E[aX + bY] = aE[X] + bE[Y], even for dependent variables, while Var(aX + bY) = a²Var(X) + b²Var(Y) + 2ab·Cov(X, Y).

Read full answer

Applies togeneral concepts; example with NumPy 2.x

Worked Example: A Fair Die

  • E[X] = (1 + 2 + 3 + 4 + 5 + 6)/6 = 3.5.

  • E[X²] = (1 + 4 + 9 + 16 + 25 + 36)/6 = 91/6 ≈ 15.17.

  • Var(X) = 91/6 − 3.5² = 91/6 − 12.25 = 35/12 ≈ 2.917; standard deviation ≈ 1.708.

Properties

Property Formula
Linearity E[aX + b] = aE[X] + b
Sum E[X + Y] = E[X] + E[Y] (always)
Scaling variance Var(aX + b) = a²Var(X)
Sum variance Var(X + Y) = Var(X) + Var(Y) + 2Cov(X, Y)
Independence Cov(X, Y) = 0 (the converse isn’t true)
Correlation ρ = Cov(X, Y) / (σ_X σ_Y)

Code Example

import numpy as np

faces = np.arange(1, 7)
p = np.full(6, 1 / 6)
mean = float(faces @ p)
var = float((faces ** 2) @ p - mean ** 2)
print(round(mean, 4), round(var, 4))                  # 3.5 2.9167 (= 35/12)

rng = np.random.default_rng(42)
hours = rng.uniform(0, 10, 100_000)                    # study hours
score = 50 + 4 * hours + rng.normal(0, 5, 100_000)     # exam score depends on hours
print(round(float(np.cov(hours, score)[0, 1]), 2))     # about 4 * Var(hours) = 4 * 100/12 = 33.3
print(round(float(np.corrcoef(hours, score)[0, 1]), 3))   # strong positive correlation, about 0.92

total = hours + score                                  # variance of a sum includes 2 * covariance
lhs = total.var()
rhs = hours.var() + score.var() + 2 * np.cov(hours, score, ddof=0)[0, 1]
print(np.isclose(lhs, rhs))                            # True

Var(hours) for Uniform(0, 10) is 10²/12 ≈ 8.33, so Cov(hours, score) = 4·8.33 ≈ 33.3. Var(score) = 16·8.33 + 25 ≈ 158.3, so the correlation is 33.3 / √(8.33·158.3) ≈ 0.918.

In ML

  • Expected loss (risk) is what training approximates.

  • Variance appears in the bias-variance decomposition and in feature scaling.

  • Covariance matrices drive PCA, Gaussian models and multicollinearity checks.

Common Pitfalls

  • Treating zero covariance as independence.

  • Forgetting the covariance term when adding correlated variables.

  • Mixing population (ddof = 0) and sample (ddof = 1) variance.

Interview Tips

  • Give the formulas and the die example.

  • State linearity of expectation and the variance-of-a-sum rule.

12

What are the characteristics of a Gaussian (normal) distribution?

The normal (Gaussian) distribution N(μ, σ²) is a continuous, symmetric, bell-shaped distribution fully described by its mean μ (centre, which equals the median and mode) and variance σ² (spread), with density f(x) = (1/(σ√(2π)))·exp(−(x − μ)²/(2σ²)); it has light tails, so about 68%, 95% and 99.7% of values lie within 1, 2 and 3 standard deviations of the mean. Sums of independent normals are normal, linear transformations stay normal, and the Central Limit Theorem makes averages of many variables approximately normal, which is why it models measurement noise, underlies least-squares regression (Gaussian errors give mean squared error), Gaussian Naive Bayes, Gaussian processes and mixtures, and the weight initialization of neural networks.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Properties

Property Value
Parameters μ (mean), σ² (variance)
Mean = median = mode μ
Skewness, excess kurtosis 0, 0
Within 1σ / 2σ / 3σ 68.27% / 95.45% / 99.73%
Standardization Z = (X − μ)/σ ~ N(0, 1)
Sum of independent normals N(μ₁ + μ₂, σ₁² + σ₂²)
Maximum entropy Among distributions with given mean and variance
Multivariate N(μ, Σ) with covariance matrix Σ

Code Example

import numpy as np
from scipy import stats

dist = stats.norm(loc=170, scale=8)                   # heights in cm
for k in [1, 2, 3]:
    print(k, round(dist.cdf(170 + 8 * k) - dist.cdf(170 - 8 * k), 4))   # 0.6827 0.9545 0.9973
print(round(dist.ppf(0.975), 2))                      # 185.68: mu + 1.96 sigma
print(round((190 - 170) / 8, 2), round(dist.sf(190), 4))   # z = 2.5, P(X > 190) = 0.0062

rng = np.random.default_rng(42)
a = rng.normal(2, 1, 200_000)
b = rng.normal(-1, 2, 200_000)
s = a + b
print(round(s.mean(), 2), round(s.var(), 2))          # about 1 and 5 (= 1 + 4)

data = rng.normal(5, 2, 500)
mu_hat, sigma_hat = stats.norm.fit(data)              # maximum likelihood estimates
print(round(mu_hat, 2), round(sigma_hat, 2))          # close to 5 and 2
print("Shapiro-Wilk p-value:", round(stats.shapiro(data).pvalue, 3))   # large p: no evidence against normality

In ML

  • Linear regression with Gaussian noise: maximum likelihood equals least squares.

  • Gaussian Naive Bayes and Gaussian mixture models.

  • Anomaly detection with z-scores or multivariate Gaussians.

  • Weight initialization (He, Xavier normal) and noise in VAEs and diffusion models.

Common Pitfalls

  • Assuming data are normal without checking (incomes, counts and waiting times usually aren’t).

  • Underestimating extreme events: real data often have heavier tails.

  • Using z-score outlier rules on skewed data.

Interview Tips

  • Give the parameters, symmetry, the 68-95-99.7 rule and standardization.

  • Connect it to least squares and the CLT.

13

How is the binomial distribution useful in machine learning?

The binomial distribution Binomial(n, p) gives the probability of k successes in n independent trials that each succeed with the same probability p: P(X = k) = C(n, k)·pᵏ(1 − p)ⁿ⁻ᵏ, with mean np and variance np(1 − p). In machine learning it models counts of binary outcomes: the number of correct predictions on a test set (so accuracy has a binomial sampling distribution, giving confidence intervals and tests), clicks or conversions out of impressions in A/B tests, defective items in a batch, and the success/failure counts behind logistic regression with grouped data; its conjugate prior, the beta distribution, gives simple Bayesian updates for rates.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Properties

Property Formula
PMF C(n, k)pᵏ(1 − p)ⁿ⁻ᵏ
Mean np
Variance np(1 − p)
Special case n = 1 gives Bernoulli(p)
Normal approximation N(np, np(1 − p)) when np and n(1 − p) are both large (≥ 10)
Conjugate prior Beta(α, β) → posterior Beta(α + k, β + n − k)

Code Example

import numpy as np
from scipy import stats

# 1) Accuracy confidence interval: 92 correct out of 100
result = stats.binomtest(92, 100)
ci = result.proportion_ci(confidence_level=0.95, method="wilson")
print(f"accuracy 0.92, 95% Wilson CI [{ci.low:.3f}, {ci.high:.3f}]")

# 2) Is a model better than a 0.85 baseline? One-sided test
print(f"p-value vs 0.85: {stats.binomtest(92, 100, p=0.85, alternative='greater').pvalue:.4f}")

# 3) A/B test with a Beta-Binomial posterior: conversions out of visitors
rng = np.random.default_rng(42)
post_a = stats.beta(1 + 120, 1 + 2000 - 120)          # uniform prior Beta(1, 1)
post_b = stats.beta(1 + 150, 1 + 2000 - 150)
samples_a = post_a.rvs(200_000, random_state=rng)
samples_b = post_b.rvs(200_000, random_state=rng)
print(f"P(variant B converts better than A) = {np.mean(samples_b > samples_a):.3f}")

# 4) Exact probability with a hand check: at least 9 of 10 correct when p = 0.8
exact = stats.binom.sf(8, 10, 0.8)                    # P(X >= 9)
print(round(exact, 4))                                # 0.3758 = 10 * 0.8^9 * 0.2 + 0.8^10
print(round(np.mean(rng.binomial(10, 0.8, 200_000) >= 9), 4))   # simulation agrees

P(X ≥ 9) = C(10, 9)·0.8⁹·0.2 + 0.8¹⁰ = 10·0.1342·0.2 + 0.1074 = 0.2684 + 0.1074 = 0.3758.

Assumptions

  • Fixed number of trials n.

  • Independent trials.

  • The same success probability p for every trial.

If p varies between trials (different users), counts are overdispersed; a beta-binomial model fits better.

Common Pitfalls

  • Using the normal-approximation interval with small n or p near 0 or 1 (use Wilson or exact intervals).

  • Ignoring dependence, such as repeated visits by the same user.

  • Treating the number of trials as random when it is fixed, or the reverse.

Interview Tips

  • Give the PMF, mean and variance, and an ML use (accuracy intervals, A/B tests).

  • Mention the beta conjugate prior.

14

How does the Poisson distribution differ from the binomial distribution?

The binomial distribution counts successes in a fixed number n of independent trials, each with success probability p, so its values are bounded at n and its variance np(1 − p) is smaller than its mean; the Poisson distribution counts events occurring in a fixed interval of time or space at a constant average rate λ, with no upper limit and variance equal to its mean (λ). The Poisson is the limit of the binomial when n is large and p is small with np = λ fixed, so it approximates rare-event counts such as typos per page, server errors per minute, or purchases per hour. In ML, Poisson regression models count targets, and overdispersed counts (variance much larger than the mean) call for a negative binomial model instead.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Comparison

Aspect Binomial(n, p) Poisson(λ)
Counts Successes in n trials Events in an interval
Parameters n, p λ (rate)
Support 0, 1, …, n 0, 1, 2, … (unbounded)
PMF C(n, k)pᵏ(1 − p)ⁿ⁻ᵏ e^(−λ)λᵏ/k!
Mean np λ
Variance np(1 − p) < mean λ = mean
Example 7 of 20 emails opened 3 support tickets per hour

Code Example

import numpy as np
from scipy import stats

n, p = 1000, 0.003                                   # many trials, rare success
lam = n * p                                          # 3
for k in range(6):
    print(k, round(stats.binom.pmf(k, n, p), 4), round(stats.poisson.pmf(k, lam), 4))
# The two columns nearly match: Poisson approximates the binomial for large n, small p

print(round(stats.poisson.pmf(0, 3), 4))             # P(no tickets in an hour) = e^-3 = 0.0498
print(round(stats.poisson.sf(5, 3), 4))              # P(more than 5) = 0.0839

rng = np.random.default_rng(42)
counts = rng.poisson(3, 100_000)
print(round(counts.mean(), 3), round(counts.var(), 3))     # both about 3

overdispersed = rng.negative_binomial(n=2, p=0.4, size=100_000)
print(round(overdispersed.mean(), 2), round(overdispersed.var(), 2))   # mean 3, variance 7.5: not Poisson

For the negative binomial with n = 2 and p = 0.4: mean = n(1 − p)/p = 2·0.6/0.4 = 3 and variance = n(1 − p)/p² = 1.2/0.16 = 7.5.

Choosing Between Them

  • A known maximum number of trials → binomial.

  • Events in continuous time or space, no fixed maximum → Poisson.

  • Count variance much larger than the mean → negative binomial.

Common Pitfalls

  • Using Poisson when counts are overdispersed (clustered events).

  • Applying Poisson when the rate changes over the interval (use time-varying rates or offsets).

  • Forgetting exposure: compare counts per unit time, not raw counts over different durations.

Interview Tips

  • Contrast fixed trials vs a rate, and the variance-mean relationship.

  • Mention the Poisson limit of the binomial and Poisson regression.

15

Why is the Bernoulli distribution relevant in machine learning?

The Bernoulli distribution models a single binary trial: X = 1 with probability p and X = 0 with probability 1 − p, with mean p and variance p(1 − p). It is the probability model behind binary classification: logistic regression and neural networks with a sigmoid output predict p = P(y = 1 | x), and the binary cross-entropy (log loss) they minimize is exactly the negative log-likelihood of Bernoulli outcomes, −[y·log p + (1 − y)·log(1 − p)]. It also describes binary features in Bernoulli Naive Bayes (a word is present or absent), the random masks of dropout (each unit kept with probability 1 − rate), clicks and conversions, and it is the building block of the binomial (sums of Bernoullis) and geometric distributions.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x and scikit-learn 1.5+

Properties

Property Value
PMF pˣ(1 − p)¹⁻ˣ for x ∈ {0, 1}
Mean p
Variance p(1 − p), largest at p = 0.5
Log-likelihood of data Σ [yᵢ log p + (1 − yᵢ) log(1 − p)]
MLE of p Sample proportion of ones
Sum of n Bernoullis Binomial(n, p)

Code Example

import numpy as np
from sklearn.naive_bayes import BernoulliNB

rng = np.random.default_rng(42)
y = rng.random(10_000) < 0.3                          # Bernoulli(0.3) outcomes
print(round(y.mean(), 3), round(y.var(), 3))          # about 0.3 and 0.21 = 0.3 * 0.7

p_grid = np.linspace(0.01, 0.99, 99)
log_lik = [np.sum(y * np.log(p) + (~y) * np.log(1 - p)) for p in p_grid]
print("MLE of p:", round(float(p_grid[int(np.argmax(log_lik))]), 2))   # 0.3: the sample proportion

def binary_cross_entropy(y_true, p_pred):
    p_pred = np.clip(p_pred, 1e-12, 1 - 1e-12)
    return -np.mean(y_true * np.log(p_pred) + (1 - y_true) * np.log(1 - p_pred))

print(round(binary_cross_entropy(np.array([1, 0, 1]), np.array([0.9, 0.2, 0.6])), 4))
# -(ln 0.9 + ln 0.8 + ln 0.6) / 3 = 0.2798

keep = rng.random((4, 6)) < 0.8                       # dropout mask: keep each unit with prob 0.8
activations = np.ones((4, 6))
print((activations * keep / 0.8).mean().round(2))     # about 1.0: inverted dropout preserves the mean

X_words = rng.random((500, 20)) < 0.2                 # binary word-presence features
labels = (X_words[:, 0] | X_words[:, 1]).astype(int)
print(round(BernoulliNB().fit(X_words, labels).score(X_words, labels), 3))

The cross-entropy value: ln 0.9 ≈ −0.1054, ln 0.8 ≈ −0.2231, ln 0.6 ≈ −0.5108; their sum is −0.8393, so the mean loss is 0.2798.

Common Pitfalls

  • Treating multi-class labels as Bernoulli (use the categorical distribution and softmax).

  • Taking log(0) when probabilities are exactly 0 or 1; clip them.

  • Assuming the same p for every example when it depends on features (that’s what the model learns).

Interview Tips

  • Connect the Bernoulli likelihood to binary cross-entropy.

  • Give several ML uses: classification, Naive Bayes, dropout.

16

How do probabilistic models handle uncertainty in predictions?

Probabilistic models represent uncertainty explicitly by predicting a distribution rather than a single value: a classifier outputs class probabilities, a regression model outputs a predictive distribution or interval (for example a mean and variance, or quantiles), and Bayesian models also keep a distribution over their own parameters, so predictions average over many plausible models. This lets you separate aleatoric uncertainty (noise inherent in the data, which more data won’t remove) from epistemic uncertainty (lack of knowledge, which shrinks with more data and is high far from the training data). Practical tools include calibrated probabilities, Bayesian linear regression and Gaussian processes, ensembles and Monte Carlo dropout, quantile regression and conformal prediction, and the uncertainty is then used for decisions: abstaining or sending cases to a human, setting safety margins, and choosing what to label next.

Read full answer

Applies togeneral concepts; examples with scikit-learn 1.5+

Methods

Method Uncertainty captured Notes
Calibrated classifier probabilities Aleatoric (mostly) Check with reliability diagrams
Gaussian processes Both Exact for small data
Bayesian linear / neural models Both Posterior over weights
Deep ensembles Mostly epistemic Strong, simple, costly
MC dropout Approximate epistemic Dropout active at test time
Quantile regression Aleatoric intervals Predicts percentiles directly
Conformal prediction Distribution-free intervals with coverage guarantees Needs exchangeable data

Code Example

import numpy as np
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, WhiteKernel

rng = np.random.default_rng(42)
X = np.sort(rng.uniform(0, 6, 40))[:, None]
y = np.sin(X).ravel() + rng.normal(0, 0.2, 40)

gp = GaussianProcessRegressor(kernel=RBF(1.0) + WhiteKernel(0.05), random_state=42).fit(X, y)
X_query = np.array([[1.0], [3.0], [9.0]])               # 9.0 is far outside the training range
mean, std = gp.predict(X_query, return_std=True)
for x, m, s in zip(X_query.ravel(), mean, std):
    print(f"x={x}: prediction {m:.2f} ± {1.96 * s:.2f}")
# The interval at x = 9 is much wider: epistemic uncertainty grows away from the data

quantiles = {q: GradientBoostingRegressor(loss="quantile", alpha=q, random_state=42).fit(X, y)
             for q in (0.05, 0.5, 0.95)}
lo, mid, hi = (quantiles[q].predict([[3.0]])[0] for q in (0.05, 0.5, 0.95))
print(f"quantile regression at x=3: median {mid:.2f}, 90% interval [{lo:.2f}, {hi:.2f}]")

# Split conformal interval around the GP mean (distribution-free coverage)
X_cal = rng.uniform(0, 6, 200)[:, None]
y_cal = np.sin(X_cal).ravel() + rng.normal(0, 0.2, 200)
residuals = np.abs(y_cal - gp.predict(X_cal))
q = np.quantile(residuals, np.ceil(0.9 * 201) / 200)
print(f"90% conformal half-width: {q:.2f}")

Using Uncertainty

  • Abstain or route to a human when uncertainty is high.

  • Choose actions by expected cost rather than the most likely class.

  • Active learning: label the most uncertain examples.

  • Monitor rising uncertainty as a sign of data drift.

Common Pitfalls

  • Treating softmax outputs as calibrated uncertainty, especially far from training data.

  • Reporting point predictions alone in high-stakes settings.

  • Confusing confidence intervals for the mean with prediction intervals for new observations.

Interview Tips

  • Distinguish aleatoric from epistemic uncertainty.

  • Name methods (GPs, ensembles, quantile regression, conformal) and how predictions use them.

17

What are Naive Bayes classifiers and why are they called naive?

Naive Bayes classifiers apply Bayes’ theorem to pick the most probable class given the features, P(class | x) ∝ P(class)·Π P(xᵢ | class), and they are “naive” because they assume all features are conditionally independent of each other once the class is known, an assumption that is rarely true (words in an email are correlated) but makes the model extremely simple: each feature’s distribution per class is estimated separately from counts or means. Despite the unrealistic assumption they often classify well, especially for text, because classification only needs the right class to have the highest score, not accurate probabilities; they train in one pass, handle many features, need little data, and come in variants for different feature types: Gaussian (continuous), multinomial (counts such as word frequencies), Bernoulli (binary presence) and complement NB (imbalanced text).

Read full answer

Applies toscikit-learn 1.5+

Variants

Variant Feature model Typical use
GaussianNB Normal per feature and class Continuous features
MultinomialNB Counts (multinomial) Word counts, TF-IDF
BernoulliNB Binary presence Short texts, binary features
ComplementNB Complement-class statistics Imbalanced text
CategoricalNB Categorical per feature Categorical data

Worked Example

Spam prior P(spam) = 0.4. P(“free” | spam) = 0.5, P(“free” | ham) = 0.05; P(“meeting” | spam) = 0.02, P(“meeting” | ham) = 0.2. For an email containing both words:

  • Spam score: 0.4 · 0.5 · 0.02 = 0.004.

  • Ham score: 0.6 · 0.05 · 0.2 = 0.006.

  • P(spam | email) = 0.004 / (0.004 + 0.006) = 0.4, so it is classified as ham.

Code Example

from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline

categories = ["sci.space", "rec.autos", "comp.graphics"]
train = fetch_20newsgroups(subset="train", categories=categories, remove=("headers", "footers", "quotes"))
test = fetch_20newsgroups(subset="test", categories=categories, remove=("headers", "footers", "quotes"))

model = make_pipeline(CountVectorizer(stop_words="english", min_df=2), MultinomialNB(alpha=0.5))
model.fit(train.data, train.target)
print(f"test accuracy: {accuracy_score(test.target, model.predict(test.data)):.3f}")   # high for such a simple model

vectorizer, nb = model.named_steps["countvectorizer"], model.named_steps["multinomialnb"]
words = vectorizer.get_feature_names_out()
for i, name in enumerate(train.target_names):
    top = nb.feature_log_prob_[i].argsort()[::-1][:6]
    print(name, [words[j] for j in top])
print(model.predict_proba(["the rocket launch reached orbit"]).round(3))

Strengths and Weaknesses

Strengths Weaknesses
Very fast training and prediction Independence assumption distorts probabilities
Works with little data and many features Correlated features are double-counted
Simple and interpretable Usually beaten by logistic regression or boosting with enough data
Good text baseline Zero counts need smoothing (alpha)

Common Pitfalls

  • Forgetting Laplace/additive smoothing, so an unseen word gives zero probability.

  • Trusting the predicted probabilities (often too extreme); calibrate if needed.

  • Using GaussianNB on count data or MultinomialNB on negative values.

Interview Tips

  • Write the formula and explain the conditional independence assumption.

  • Explain why it still works for classification and name the variants.

18

How does logistic regression use probability?

Logistic regression models the probability that the outcome is 1 given the features: it computes a linear score z = w·x + b and passes it through the sigmoid function, p = 1 / (1 + e^(−z)), which maps any real number into (0, 1); equivalently, it assumes the log-odds ln(p / (1 − p)) is a linear function of the features. The outcome is modelled as Bernoulli(p), so the weights are fitted by maximum likelihood, which is the same as minimizing binary cross-entropy (log loss); each coefficient is a change in log-odds per unit of its feature (an odds ratio of e^w), and classification comes from thresholding the probability, usually at 0.5 but ideally at a threshold chosen for the costs of each error. Multinomial (softmax) logistic regression extends this to several classes.

Read full answer

Applies toscikit-learn 1.5+, NumPy 2.x

The Probabilistic Pieces

Piece Formula
Linear score z = w·x + b
Probability p = σ(z) = 1/(1 + e^(−z))
Log-odds ln(p/(1 − p)) = z
Likelihood Π pᵢ^yᵢ (1 − pᵢ)^(1 − yᵢ)
Loss −(1/n) Σ [yᵢ ln pᵢ + (1 − yᵢ) ln(1 − pᵢ)]
Gradient (1/n) Xᵀ(p − y)
Multi-class softmax over K linear scores

Hand Example

With w = 0.8 per hour studied and b = −4, a student who studied 6 hours has z = 0.8·6 − 4 = 0.8, so p = 1/(1 + e^(−0.8)) ≈ 0.69. Each extra hour multiplies the odds of passing by e^0.8 ≈ 2.23.

Code Example

import numpy as np
from sklearn.linear_model import LogisticRegression

rng = np.random.default_rng(42)
hours = rng.uniform(0, 10, 2000)
p_true = 1 / (1 + np.exp(-(0.8 * hours - 4)))
passed = (rng.random(2000) < p_true).astype(int)

def fit_logistic(x, y, lr=0.1, steps=5000):
    X = np.column_stack([np.ones_like(x), x])
    w = np.zeros(2)
    for _ in range(steps):
        p = 1 / (1 + np.exp(-(X @ w)))
        w -= lr * X.T @ (p - y) / len(y)              # gradient of the log loss
    return w

b, w = fit_logistic(hours, passed)
print(f"from scratch: b = {b:.2f}, w = {w:.2f}")       # close to -4 and 0.8
sk = LogisticRegression(penalty=None).fit(hours[:, None], passed)
print(f"scikit-learn: b = {sk.intercept_[0]:.2f}, w = {sk.coef_[0, 0]:.2f}")

p6 = 1 / (1 + np.exp(-(b + w * 6)))
print(f"P(pass | 6 hours) = {p6:.2f}, odds ratio per hour = {np.exp(w):.2f}")   # about 0.69 and 2.23

Common Pitfalls

  • Reading coefficients as changes in probability rather than log-odds.

  • Assuming probabilities are well calibrated after heavy regularization or resampling.

  • Perfect separation making weights grow without bound (regularization fixes it).

Interview Tips

  • Explain sigmoid, log-odds and the Bernoulli likelihood.

  • Show that minimizing log loss is maximum likelihood.

19

What is entropy in information theory and how does it relate to ML?

Entropy H(X) = −Σ p(x) log p(x) measures the average uncertainty, or information content, of a random variable: it is 0 when the outcome is certain and largest when all outcomes are equally likely (1 bit for a fair coin, using log base 2). Machine learning uses it and its relatives throughout: decision trees choose splits that maximize information gain (the drop in entropy of the labels), cross-entropy H(p, q) = −Σ p log q is the standard classification loss, KL divergence D_KL(p ‖ q) = H(p, q) − H(p) measures how far a model’s distribution q is from the true distribution p (used in VAEs, knowledge distillation and RL policy constraints), and mutual information measures how much a feature tells you about the target, useful for feature selection.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+ and scikit-learn 1.5+

Related Quantities

Quantity Formula ML use
Entropy H(p) = −Σ p log p Uncertainty of labels or predictions
Information gain H(Y) − H(Y | split) Decision tree splits
Cross-entropy H(p, q) = −Σ p log q Classification loss
KL divergence Σ p log(p/q) VAEs, distillation, policy updates
Mutual information I(X; Y) = H(Y) − H(Y | X) Feature selection
Perplexity 2^H (or e^H) Language model evaluation

Worked Examples

  • Fair coin: H = −(0.5 log₂ 0.5 + 0.5 log₂ 0.5) = 1 bit.

  • Biased coin p = 0.9: H = −(0.9 log₂ 0.9 + 0.1 log₂ 0.1) ≈ 0.137 + 0.332 = 0.469 bits.

  • Information gain: a node with 10 positives and 10 negatives (H = 1) is split into [9+, 1−] and [1+, 9−], each with H ≈ 0.469; gain = 1 − 0.469 = 0.531 bits.

Code Example

import numpy as np
from scipy.stats import entropy
from sklearn.feature_selection import mutual_info_classif

print(round(entropy([0.5, 0.5], base=2), 3), round(entropy([0.9, 0.1], base=2), 3))   # 1.0 0.469

def info_gain(parent, children):
    n = sum(sum(c) for c in children)
    weighted = sum(sum(c) / n * entropy(c, base=2) for c in children)
    return entropy(parent, base=2) - weighted

print(round(info_gain([10, 10], [[9, 1], [1, 9]]), 3))     # 0.531

p_true = np.array([1.0, 0.0, 0.0])                         # one-hot label
q_model = np.array([0.7, 0.2, 0.1])                        # predicted probabilities
cross_entropy = -np.sum(p_true * np.log(q_model))
print(round(cross_entropy, 4))                             # -ln 0.7 = 0.3567

p, q = np.array([0.4, 0.6]), np.array([0.5, 0.5])
print(round(entropy(p, q), 4), round(entropy(q, p), 4))    # 0.0201 0.0204: KL is not symmetric

rng = np.random.default_rng(42)
X = rng.normal(size=(2000, 3))
y = (X[:, 0] > 0).astype(int)                              # only feature 0 matters
print(mutual_info_classif(X, y, random_state=42).round(3)) # high for feature 0, near 0 for the others

KL(p ‖ q) = 0.4·ln(0.8) + 0.6·ln(1.2) ≈ −0.0893 + 0.1094 = 0.0201 nats.

Common Pitfalls

  • Mixing log bases (bits vs nats) between calculations.

  • Treating KL divergence as a symmetric distance.

  • Taking log(0) in cross-entropy; clip predicted probabilities.

Interview Tips

  • Define entropy with the coin examples.

  • Connect it to information gain, cross-entropy loss and KL divergence.

21

How is probability used in Bayesian inference for machine learning?

Bayesian inference treats unknown quantities such as model parameters as random variables with probability distributions: you start with a prior p(θ) encoding what you believe before seeing data, specify a likelihood p(D | θ) for how data are generated, and use Bayes’ theorem to get the posterior p(θ | D) ∝ p(D | θ)·p(θ), which captures both the best estimate and its uncertainty. Predictions then average over the posterior, p(y | x, D) = ∫ p(y | x, θ)·p(θ | D) dθ, which gives honest uncertainty and resists overfitting with small data. In ML this appears as conjugate updates (Beta-Binomial for click rates), Bayesian linear regression and Gaussian processes, Bayesian neural networks, Bayesian optimization for hyperparameters, and as regularization (a MAP estimate with a Gaussian prior is ridge regression); posteriors without closed forms are approximated with MCMC (PyMC, Stan, NumPyro) or variational inference.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x and SciPy 1.13+

Components

Component Meaning Example (click rate)
Prior p(θ) Belief before data Beta(2, 20): rates around 9%
Likelihood p(D | θ) Data model Binomial clicks out of views
Posterior p(θ | D) Updated belief Beta(2 + clicks, 20 + non-clicks)
Posterior predictive Distribution of new data Expected clicks tomorrow
Evidence p(D) Normalizer; used for model comparison Bayes factors

Code Example: Conjugate Update and Bayesian Linear Regression

import numpy as np
from scipy import stats

prior = stats.beta(2, 20)                              # prior mean 2/22 = 0.091
clicks, views = 30, 200
posterior = stats.beta(2 + clicks, 20 + views - clicks)
lo, hi = posterior.ppf([0.025, 0.975])
print(f"prior mean {prior.mean():.3f}, posterior mean {posterior.mean():.3f}, 95% interval [{lo:.3f}, {hi:.3f}]")
# Posterior Beta(32, 190): mean 32/222 = 0.144, between the prior 0.091 and the data rate 0.15

rng = np.random.default_rng(42)
X = np.column_stack([np.ones(20), rng.uniform(-3, 3, 20)])
y = X @ np.array([1.0, 2.0]) + rng.normal(0, 1.0, 20)

alpha, sigma2 = 1.0, 1.0                               # prior w ~ N(0, alpha^-1 I), known noise variance
S_inv = alpha * np.eye(2) + X.T @ X / sigma2
S = np.linalg.inv(S_inv)                               # posterior covariance
m = S @ X.T @ y / sigma2                               # posterior mean (equals ridge with lambda = alpha * sigma2)
print("posterior mean:", m.round(3), "posterior sd:", np.sqrt(np.diag(S)).round(3))

x_new = np.array([1.0, 2.5])
pred_mean = x_new @ m
pred_var = sigma2 + x_new @ S @ x_new                  # noise + parameter uncertainty
print(f"prediction at x=2.5: {pred_mean:.2f} ± {1.96 * np.sqrt(pred_var):.2f}")

Bayesian vs Frequentist Estimates

MLE MAP Full Bayesian
Output Point estimate Point estimate (posterior mode) Whole posterior distribution
Prior None Yes (acts as regularizer) Yes
Uncertainty Via standard errors Not directly Built in
Cost Low Low Higher (MCMC/VI)

Common Pitfalls

  • Strong priors that dominate small datasets without justification; check prior sensitivity.

  • Treating MAP as a full Bayesian answer.

  • Not checking MCMC convergence (R-hat, effective sample size).

Interview Tips

  • Write posterior ∝ likelihood × prior and work a Beta-Binomial update.

  • Mention posterior predictive uncertainty and MAP = regularization.

22

How can independence assumptions in probabilistic models cause errors?

When a model assumes variables are independent but they are not, it multiplies probabilities that should be combined jointly, so correlated evidence gets counted more than once: Naive Bayes treats “free”, “offer” and “free offer” as separate independent signals and becomes overconfident, models that assume i.i.d. samples underestimate uncertainty when data come in clusters or time series, and risk models that assume independent defaults badly underestimate the chance of many failures at once. The effects are miscalibrated, too-extreme probabilities, confidence intervals and p-values that are too narrow, leakage in validation (correlated rows split across train and test), and wrong decisions in the tails; remedies include modelling dependence explicitly (Bayesian networks, copulas, mixed-effects or time-series models), group-aware validation, and calibrating probabilities afterwards.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x and scikit-learn 1.5+

Where It Goes Wrong

Assumption Reality Consequence
Naive Bayes: features independent given class Duplicated or correlated features Overconfident probabilities
i.i.d. samples Several rows per user, time series Standard errors too small; leaky CV
Independent sensor or system failures Common causes Joint failure risk underestimated
Independent tests in multiple testing Correlated tests Wrong error-rate corrections
Independent errors in regression Autocorrelated residuals Invalid confidence intervals

Code Example: Duplicated Evidence in Naive Bayes

import numpy as np
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import brier_score_loss
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

rng = np.random.default_rng(42)
n = 4000
y = rng.integers(0, 2, n)
signal = y + rng.normal(0, 1.0, n)                       # one informative feature
X_single = signal[:, None]
X_copies = np.column_stack([signal + rng.normal(0, 0.05, n) for _ in range(5)])   # 5 near-copies

for name, X in [("1 feature", X_single), ("5 correlated copies", X_copies)]:
    X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=42)
    proba = GaussianNB().fit(X_tr, y_tr).predict_proba(X_te)[:, 1]
    print(f"{name:20s} share of probabilities > 0.99 or < 0.01: {np.mean((proba > 0.99) | (proba < 0.01)):.2f}, "
          f"Brier {brier_score_loss(y_te, proba):.3f}")
# The copies add no information, but NB counts the evidence 5 times and becomes overconfident

X_tr, X_te, y_tr, y_te = train_test_split(X_copies, y, random_state=42)
calibrated = CalibratedClassifierCV(GaussianNB(), method="isotonic", cv=5).fit(X_tr, y_tr)
print(f"after calibration Brier {brier_score_loss(y_te, calibrated.predict_proba(X_te)[:, 1]):.3f}")

# i.i.d. assumption with clustered data: the naive standard error is too small
groups = np.repeat(np.arange(50), 20)                    # 50 users, 20 rows each
values = rng.normal(0, 1, 50)[groups] + rng.normal(0, 0.3, len(groups))
naive_se = values.std(ddof=1) / np.sqrt(len(values))
cluster_se = values.reshape(50, 20).mean(axis=1).std(ddof=1) / np.sqrt(50)
print(f"naive SE {naive_se:.3f} vs cluster-aware SE {cluster_se:.3f}")   # naive SE is far too small

Remedies

  • Remove or combine redundant features; use models that handle correlation (logistic regression, trees).

  • Calibrate probabilities (Platt scaling, isotonic regression).

  • Use group or time-aware splits and cluster-robust standard errors.

  • Model dependence directly: Bayesian networks, hierarchical models, copulas, autoregressive terms.

Common Pitfalls

  • Trusting Naive Bayes probabilities for decisions that need calibrated risk.

  • Computing confidence intervals from correlated samples as if independent.

  • Assuming independent component failures in reliability or risk estimates.

Interview Tips

  • Explain double-counting of evidence and too-narrow uncertainty.

  • Offer concrete fixes for both.

23

How do you update probabilities using a prior, likelihood and posterior?

Bayesian updating combines what you believed before (the prior) with how well each possibility explains the new evidence (the likelihood) to give an updated belief (the posterior): posterior ∝ likelihood × prior, normalized so the posterior probabilities sum (or integrate) to 1. The posterior then becomes the prior for the next piece of evidence, so beliefs update step by step, and the order of independent observations doesn’t matter. With conjugate pairs the update is just arithmetic on parameters (a Beta prior with binomial data gives a Beta posterior), and for discrete hypotheses it is a table: multiply each prior by its likelihood and divide by the total.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x and SciPy 1.13+

Worked Example: Which Coin?

A bag holds three coins: fair (P(H) = 0.5), biased (0.8) and very biased (0.95), picked with equal prior probability 1/3. You flip the chosen coin and get H, H, T.

Coin Prior Likelihood P(HHT) Prior × likelihood Posterior
Fair 1/3 0.5·0.5·0.5 = 0.125 0.04167 0.4193
Biased 1/3 0.8·0.8·0.2 = 0.128 0.04267 0.4294
Very biased 1/3 0.95·0.95·0.05 = 0.045125 0.01504 0.1514
Total 0.09938 (evidence) 1.000

The tail almost rules out the very biased coin, and the fair and biased coins remain nearly tied.

Code Example

import numpy as np
from scipy import stats

coins = np.array([0.5, 0.8, 0.95])
prior = np.full(3, 1 / 3)

def update(prior, flip):
    likelihood = coins if flip == "H" else 1 - coins
    unnormalized = prior * likelihood
    return unnormalized / unnormalized.sum()

belief = prior
for flip in "HHT":
    belief = update(belief, flip)                      # the posterior becomes the next prior
    print(flip, belief.round(4))
print("final:", belief.round(4))                       # [0.4193 0.4294 0.1514]

order_check = prior
for flip in "THH":                                     # same flips, different order
    order_check = update(order_check, flip)
print(np.allclose(belief, order_check))                # True

# Continuous version with a conjugate Beta prior on P(heads)
a, b = 2, 2                                            # mild prior centred on 0.5
for heads, tails in [(7, 3), (15, 5)]:                 # two batches of data
    a, b = a + heads, b + tails
    post = stats.beta(a, b)
    print(f"after batch: Beta({a}, {b}), mean {post.mean():.3f}, 95% CI {np.round(post.ppf([0.025, 0.975]), 3)}")

Key Points

  • The posterior is a compromise between prior and data; more data shifts it toward the data.

  • Strong priors need more evidence to move.

  • Normalizing by the evidence P(D) is what makes the posterior a proper distribution.

Common Pitfalls

  • Forgetting to normalize.

  • Assigning a zero prior to a hypothesis, which can never recover however strong the evidence.

  • Reusing the same data twice in updates (double counting).

Interview Tips

  • Work a small table of prior × likelihood → posterior.

  • Show sequential updating and conjugate Beta updates.

24

How do you handle missing data in probabilistic models?

Probabilistic models handle missing data in a principled way by treating missing values as unknown random variables rather than filling them with a single guess: depending on why data are missing (missing completely at random, MCAR; missing at random given observed variables, MAR; or missing not at random, MNAR), you can marginalize them out of the likelihood, estimate parameters with the EM algorithm, sample them in a Bayesian model, or create several imputed datasets and pool the results (multiple imputation). Simple single imputation with the mean or median underestimates variance and can bias relationships, so it should be used with care, often together with a missing-indicator feature; models such as Naive Bayes, Gaussian mixtures and many Bayesian models can simply skip missing features in the likelihood.

Read full answer

Applies togeneral concepts; examples with scikit-learn 1.5+

Missingness Mechanisms

Mechanism Meaning Example Implication
MCAR Missingness unrelated to any data Random sensor dropouts Complete-case analysis unbiased but wasteful
MAR Depends on observed variables Older users skip the income question Model-based imputation works
MNAR Depends on the missing value itself High earners hide income Needs modelling of the missingness or sensitivity analysis

Approaches

Approach How Notes
Marginalization Integrate missing variables out of the likelihood Natural for Naive Bayes, mixtures, GPs
EM algorithm Alternate expected values of missing data and parameter updates Gaussian models, mixtures
Bayesian imputation Missing values as parameters sampled by MCMC Full uncertainty
Multiple imputation (MICE) Several imputed datasets, combine estimates IterativeImputer(sample_posterior=True)
Missing indicators Add “was missing” features Captures informative missingness
Native handling Models with built-in NaN support Gradient boosting

Code Example

import numpy as np
from sklearn.experimental import enable_iterative_imputer  # noqa: F401
from sklearn.impute import IterativeImputer, SimpleImputer

rng = np.random.default_rng(42)
n = 2000
age = rng.uniform(20, 70, n)
income = 20 + 1.2 * age + rng.normal(0, 8, n)              # income depends on age
data = np.column_stack([age, income])
true_mean_income = income.mean()

missing = rng.random(n) < 1 / (1 + np.exp(-(age - 50) / 5))   # MAR: older people skip income more often
observed = data.copy()
observed[missing, 1] = np.nan
print(f"missing share {missing.mean():.2f}")
print(f"true mean income {true_mean_income:.1f}, complete-case mean {np.nanmean(observed[:, 1]):.1f}")
# Complete-case analysis is biased downward because high-income (older) rows are missing more often

mean_imp = SimpleImputer(strategy="mean").fit_transform(observed)
print(f"mean imputation: mean {mean_imp[:, 1].mean():.1f}, sd {mean_imp[:, 1].std():.1f} (true sd {income.std():.1f})")

estimates = []
for seed in range(5):                                       # multiple imputation with posterior sampling
    imputed = IterativeImputer(sample_posterior=True, random_state=seed).fit_transform(observed)
    estimates.append(imputed[:, 1].mean())
print(f"multiple imputation mean {np.mean(estimates):.1f} (spread across imputations {np.std(estimates):.2f})")
# Using age to impute income removes most of the bias under MAR

Common Pitfalls

  • Dropping rows with missing values when missingness depends on other variables (biased results).

  • Single mean imputation, which shrinks variance and weakens correlations.

  • Imputing with information from the test set.

  • Ignoring MNAR possibilities; run sensitivity analyses when missingness may depend on the value itself.

Interview Tips

  • Name MCAR, MAR and MNAR with examples.

  • Describe marginalization, EM and multiple imputation, and why single imputation understates uncertainty.

25

How do you determine the significance of an observed effect using probability?

To judge whether an observed effect (a difference in conversion rates, a model’s accuracy gain) is real or could be chance, set up a null hypothesis of no effect, choose a test statistic, and compute how probable a result at least as extreme as the observed one would be if the null were true: that probability is the p-value, and a small p-value (below a significance level α chosen in advance, often 0.05) is evidence against the null. Report the effect size and a confidence interval alongside it, because statistical significance says nothing about practical importance; design the test beforehand (sample size from a power analysis, one primary metric), and adjust for multiple comparisons. Permutation tests and the bootstrap give the same answers with fewer assumptions, and Bayesian analysis gives the probability that the effect is positive directly.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Steps

  1. State H₀ (no difference) and H₁ (a difference, or a specific direction).

  2. Choose α (e.g. 0.05) and the test before looking at the data.

  3. Compute the test statistic and its p-value.

  4. Report the effect size with a confidence interval.

  5. Decide, considering practical significance and multiple testing.

Choosing a Test

Data Test
Two proportions (A/B conversion) Two-proportion z-test, chi-squared, Fisher’s exact
Two means Welch’s t-test
Paired measurements (two models on the same test set) Paired t-test, McNemar’s test (classification)
Non-normal, small samples Mann-Whitney U, permutation test
Many folds / bootstrap Bootstrap confidence intervals

Code Example: A/B Test

import numpy as np
from scipy import stats

conv_a, n_a = 200, 4000                       # 5.0% conversion
conv_b, n_b = 248, 4000                       # 6.2% conversion
p_a, p_b = conv_a / n_a, conv_b / n_b
p_pool = (conv_a + conv_b) / (n_a + n_b)
se_pool = np.sqrt(p_pool * (1 - p_pool) * (1 / n_a + 1 / n_b))
z = (p_b - p_a) / se_pool
p_value = 2 * stats.norm.sf(abs(z))
print(f"lift {p_b - p_a:.4f}, z = {z:.2f}, two-sided p = {p_value:.4f}")

se_diff = np.sqrt(p_a * (1 - p_a) / n_a + p_b * (1 - p_b) / n_b)
print(f"95% CI for the difference: [{p_b - p_a - 1.96 * se_diff:.4f}, {p_b - p_a + 1.96 * se_diff:.4f}]")

rng = np.random.default_rng(42)               # permutation test: no distributional assumptions
outcomes = np.r_[np.ones(conv_a), np.zeros(n_a - conv_a), np.ones(conv_b), np.zeros(n_b - conv_b)]
observed = p_b - p_a
diffs = []
for _ in range(10_000):
    perm = rng.permutation(outcomes)
    diffs.append(perm[n_a:].mean() - perm[:n_a].mean())
print(f"permutation p-value: {np.mean(np.abs(diffs) >= abs(observed)):.4f}")   # close to the z-test

effect = 0.01                                  # power analysis: users per group to detect +1 point from 5%
z_a, z_b = stats.norm.ppf(0.975), stats.norm.ppf(0.8)
p_bar = 0.055
n_needed = (z_a + z_b) ** 2 * 2 * p_bar * (1 - p_bar) / effect ** 2
print(f"about {int(np.ceil(n_needed))} users per group for 80% power")

Here z ≈ 2.33 and p ≈ 0.020, so at α = 0.05 the lift is significant; the interval for the difference runs from about 0.2 to 2.2 percentage points.

Common Pitfalls

  • Peeking at results and stopping as soon as p < 0.05 (inflates false positives).

  • Testing many metrics or segments without correction.

  • Treating p > 0.05 as proof of no effect.

  • Ignoring practical significance or the cost of the change.

Interview Tips

  • Walk through hypotheses, test, p-value, confidence interval and decision.

  • Mention power analysis, peeking and multiple comparisons.

26

What are p-values and confidence intervals and how do you interpret them?

A p-value is the probability, assuming the null hypothesis is true, of getting a result at least as extreme as the one observed; a small p-value means the data would be surprising if there were no effect, but it is not the probability that the null hypothesis is true, and it says nothing about the size of the effect. A 95% confidence interval is a range computed by a procedure that, over many repeated samples, contains the true parameter 95% of the time; it shows the plausible effect sizes and their precision, and a 95% interval that excludes the null value corresponds to p < 0.05 in the matching two-sided test. In practice, report both: the interval tells you how big the effect might be, the p-value how surprising the data are under “no effect”.

Read full answer

Applies togeneral concepts; examples with SciPy 1.13+

Correct and Incorrect Interpretations

Statement Correct?
“p = 0.03: if there were no effect, data this extreme would occur 3% of the time.” Yes
“p = 0.03: there is a 3% chance the null hypothesis is true.” No
“p = 0.03: there is a 97% chance the effect is real.” No
“p = 0.2 shows there is no effect.” No: absence of evidence isn’t evidence of absence
“95% of intervals built this way contain the true value.” Yes
“There is a 95% probability the true value is in this specific interval.” Not in the frequentist sense (that is a Bayesian credible interval)
“A narrow interval means a precise estimate.” Yes

Code Example: Coverage Simulation

import numpy as np
from scipy import stats

rng = np.random.default_rng(42)
true_mean, n, trials = 10.0, 30, 10_000
covered, p_values = 0, []
for _ in range(trials):
    sample = rng.normal(true_mean, 3.0, n)
    lo, hi = stats.t.interval(0.95, df=n - 1, loc=sample.mean(), scale=stats.sem(sample))
    covered += lo <= true_mean <= hi
    p_values.append(stats.ttest_1samp(sample, popmean=true_mean).pvalue)   # H0 is true here
print(f"coverage of 95% intervals: {covered / trials:.3f}")                # about 0.95
print(f"share of p < 0.05 when H0 is true: {np.mean(np.array(p_values) < 0.05):.3f}")   # about 0.05

model_a = rng.normal(0.80, 0.02, 10)                 # accuracy over 10 folds
model_b = model_a + rng.normal(0.01, 0.01, 10)       # model B slightly better on each fold
diff = model_b - model_a
t_stat, p = stats.ttest_rel(model_b, model_a)
lo, hi = stats.t.interval(0.95, df=9, loc=diff.mean(), scale=stats.sem(diff))
print(f"mean gain {diff.mean():.4f}, 95% CI [{lo:.4f}, {hi:.4f}], p = {p:.4f}")

Cross-validation folds share training data, so a paired t-test on fold scores is optimistic; corrected tests (such as the Nadeau-Bengio correction) or repeated held-out evaluation are safer for comparing models.

Common Pitfalls

  • Treating p < 0.05 as a magic line for truth.

  • Reporting p-values without effect sizes and intervals.

  • Running many tests and reporting only the significant ones (p-hacking).

  • Confusing statistical significance with practical importance.

Interview Tips

  • Give the precise definitions and one common misinterpretation of each.

  • Mention the duality between intervals and tests and the Bayesian credible interval.

27

How does a probabilistic graphical model work?

A probabilistic graphical model (PGM) represents a joint probability distribution over many variables as a graph: nodes are random variables and edges encode direct dependencies, so the joint distribution factorizes into small local pieces. In a Bayesian network (a directed acyclic graph) each node has a conditional probability table given its parents, and the joint is P(X₁, …, Xₙ) = Π P(Xᵢ | parents(Xᵢ)); in a Markov random field (an undirected graph) the joint is a normalized product of potential functions over cliques. This factorization makes huge joint distributions compact, shows conditional independencies visually, and supports inference (computing posteriors such as P(disease | symptoms) by variable elimination, belief propagation or sampling) and learning parameters or structure from data. Naive Bayes, hidden Markov models, conditional random fields and topic models are all PGMs.

Read full answer

Applies togeneral concepts; examples in Python 3.12 (pgmpy offers a full library)

Types

Type Graph Factorization Examples
Bayesian network Directed acyclic Π P(Xᵢ | parents) Diagnosis networks, Naive Bayes, HMMs
Markov random field Undirected (1/Z) Π ψ_c(X_c) Image denoising, Ising models
Conditional random field Undirected, conditioned on inputs P(y | x) Sequence labelling (NER)
Factor graph Bipartite variables/factors Product of factors Belief propagation, error-correcting codes

Worked Example: The Sprinkler Network

Rain ® → Sprinkler (S), and both R and S → Wet grass (W).

  • P® = 0.2.

  • P(S | R) = 0.01, P(S | not R) = 0.4.

  • P(W | S, R) = 0.99, P(W | S, not R) = 0.9, P(W | not S, R) = 0.8, P(W | not S, not R) = 0.0.

The joint has 2³ = 8 entries but needs only 1 + 2 + 4 = 7 numbers, and the savings grow fast with more variables.

Code Example: Inference by Enumeration

from itertools import product

P_R = {True: 0.2, False: 0.8}
P_S_given_R = {True: 0.01, False: 0.4}
P_W_given_SR = {(True, True): 0.99, (True, False): 0.9, (False, True): 0.8, (False, False): 0.0}

def joint(r, s, w):
    p_s = P_S_given_R[r] if s else 1 - P_S_given_R[r]
    p_w = P_W_given_SR[(s, r)] if w else 1 - P_W_given_SR[(s, r)]
    return P_R[r] * p_s * p_w                     # factorized joint probability

total = sum(joint(r, s, w) for r, s, w in product([True, False], repeat=3))
print(round(total, 6))                            # 1.0

p_wet = sum(joint(r, s, True) for r, s in product([True, False], repeat=2))
p_rain_and_wet = sum(joint(True, s, True) for s in [True, False])
print(f"P(rain | wet grass) = {p_rain_and_wet / p_wet:.4f}")            # 0.3577

p_rain_wet_sprinkler = joint(True, True, True)
p_wet_sprinkler = sum(joint(r, True, True) for r in [True, False])
print(f"P(rain | wet, sprinkler on) = {p_rain_wet_sprinkler / p_wet_sprinkler:.4f}")   # 0.0069: explaining away

Hand check: P(W, R) = 0.2·(0.01·0.99 + 0.99·0.8) = 0.2·0.8019 = 0.16038; P(W, not R) = 0.8·0.4·0.9 = 0.288; so P(R | W) = 0.16038 / 0.44838 ≈ 0.3577. Learning that the sprinkler was on “explains away” the wet grass and makes rain much less likely.

Common Pitfalls

  • Reading directed edges as proven causal links (they are modelling assumptions).

  • Exact inference on large, densely connected graphs (exponential cost; use approximate methods).

  • Too many parents per node, which makes conditional probability tables huge.

Interview Tips

  • Explain factorization and conditional independence with a small network.

  • Mention inference methods and that Naive Bayes and HMMs are PGMs.

28

What are Markov chains and how do they apply to machine learning?

A Markov chain is a sequence of random states where the next state depends only on the current state, not on the earlier history (the Markov property): P(Xₜ₊₁ | Xₜ, …, X₀) = P(Xₜ₊₁ | Xₜ); a finite chain is defined by a transition matrix P whose entry Pᵢⱼ is the probability of moving from state i to state j, and the distribution after k steps is the starting distribution times Pᵏ. Under mild conditions (irreducible and aperiodic) the chain converges to a unique stationary distribution π with π = πP, regardless of where it starts. In ML, Markov chains model sequences (user navigation, weather, text), power PageRank, are the basis of hidden Markov models and Markov decision processes in reinforcement learning, and make MCMC sampling possible, where a chain is designed so its stationary distribution is a Bayesian posterior.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x

Key Concepts

Concept Meaning
State space Possible states (pages, weather types)
Transition matrix P Rows sum to 1
k-step transitions Pᵏ
Stationary distribution π π = πP; long-run share of time in each state
Irreducible Every state reachable from every other
Aperiodic No fixed cycle length
Absorbing state Once entered, never left (e.g. “churned”)

Applications

Area Use
Web and search PageRank: stationary distribution of a random surfer
Text n-gram language models (Markov of order n − 1)
Customer journeys Navigation paths, churn as an absorbing state
Bayesian inference MCMC (Metropolis-Hastings, Gibbs)
Reinforcement learning MDPs: Markov chains with actions and rewards
Sequence labelling Hidden Markov models

Code Example

import numpy as np

states = ["sunny", "cloudy", "rainy"]
P = np.array([[0.7, 0.2, 0.1],
              [0.3, 0.4, 0.3],
              [0.2, 0.4, 0.4]])

today = np.array([1.0, 0.0, 0.0])                       # sunny today
print("in 2 days:", (today @ np.linalg.matrix_power(P, 2)).round(3))   # [0.57 0.26 0.17]

eigvals, eigvecs = np.linalg.eig(P.T)                   # stationary distribution: left eigenvector for 1
pi = np.real(eigvecs[:, np.isclose(eigvals, 1)].ravel())
pi /= pi.sum()
print("stationary:", dict(zip(states, pi.round(4))))
print(np.allclose(pi @ P, pi))                          # True

rng = np.random.default_rng(42)
state, visits = 0, np.zeros(3)
for _ in range(200_000):                                # long simulation matches pi
    state = rng.choice(3, p=P[state])
    visits[state] += 1
print("simulated shares:", (visits / visits.sum()).round(4))

def metropolis(log_target, n=50_000, step=1.0, seed=42):
    """MCMC: a Markov chain whose stationary distribution is the target."""
    r = np.random.default_rng(seed)
    x, samples = 0.0, []
    for _ in range(n):
        proposal = x + r.normal(0, step)
        if np.log(r.random()) < log_target(proposal) - log_target(x):
            x = proposal
        samples.append(x)
    return np.array(samples[5000:])                     # drop burn-in

draws = metropolis(lambda x: -0.5 * ((x - 3) / 2) ** 2)  # unnormalized N(3, 2^2)
print(f"MCMC mean {draws.mean():.2f}, sd {draws.std():.2f}")   # close to 3 and 2

Two-day forecast by hand: the first row of P² is [0.7·0.7 + 0.2·0.3 + 0.1·0.2, 0.7·0.2 + 0.2·0.4 + 0.1·0.4, 0.7·0.1 + 0.2·0.3 + 0.1·0.4] = [0.57, 0.26, 0.17].

Common Pitfalls

  • Assuming the Markov property when longer history matters (use higher-order chains or add history to the state).

  • Rows of the transition matrix that don’t sum to 1.

  • Using MCMC samples before the chain has converged.

Interview Tips

  • State the Markov property and the stationary distribution.

  • Give applications: PageRank, MCMC, HMMs, RL.

29

How do hidden Markov models use probability to model sequential data?

A hidden Markov model (HMM) assumes an unobserved sequence of hidden states follows a Markov chain (each state depends only on the previous one) and that each observation is generated from the current hidden state; it is defined by initial state probabilities π, a transition matrix A (P(stateₜ | stateₜ₋₁)) and emission probabilities B (P(observationₜ | stateₜ)). Probability drives three standard computations: the forward algorithm gives the likelihood of an observation sequence by summing over all hidden paths, the Viterbi algorithm finds the single most probable hidden path, and the Baum-Welch algorithm (EM) learns A, B and π from unlabelled sequences. HMMs were the backbone of speech recognition and part-of-speech tagging and are still used for regime detection in finance, gene finding, activity recognition from sensors and simple sequence segmentation.

Read full answer

Applies togeneral concepts; example with NumPy 2.x (hmmlearn provides a full implementation)

Components

Component Symbol Example (weather and activities)
Hidden states S Rainy, Sunny
Observations O Walk, Shop, Clean
Initial probabilities π P(Rainy) = 0.6
Transitions A P(Sunny → Rainy) = 0.4
Emissions B P(Walk | Sunny) = 0.6

Algorithms

Problem Algorithm Cost
Likelihood of an observation sequence Forward O(T·N²)
Most likely hidden path Viterbi O(T·N²)
Learn parameters Baum-Welch (EM) Iterative
Posterior of each state Forward-backward O(T·N²)

Code Example

import numpy as np

states = ["Rainy", "Sunny"]
obs_names = ["walk", "shop", "clean"]
pi = np.array([0.6, 0.4])
A = np.array([[0.7, 0.3],
              [0.4, 0.6]])
B = np.array([[0.1, 0.4, 0.5],          # Rainy: walk, shop, clean
              [0.6, 0.3, 0.1]])         # Sunny

obs = [0, 1, 2]                          # walk, shop, clean

def forward(obs):
    alpha = pi * B[:, obs[0]]
    for o in obs[1:]:
        alpha = (alpha @ A) * B[:, o]    # sum over previous states, then emit
    return alpha.sum()

def viterbi(obs):
    delta = np.log(pi) + np.log(B[:, obs[0]])
    back = []
    for o in obs[1:]:
        scores = delta[:, None] + np.log(A)          # previous state x next state
        back.append(scores.argmax(axis=0))
        delta = scores.max(axis=0) + np.log(B[:, o])
    path = [int(delta.argmax())]
    for ptr in reversed(back):
        path.append(int(ptr[path[-1]]))
    return [states[s] for s in reversed(path)], float(np.exp(delta.max()))

print(f"P(walk, shop, clean) = {forward(obs):.6f}")   # 0.033612
path, p_path = viterbi(obs)
print(path, round(p_path, 6))                         # ['Sunny', 'Rainy', 'Rainy'] 0.01344

Hand check of the best path: P(Sunny)·P(walk | Sunny) = 0.4·0.6 = 0.24; → Rainy: 0.24·0.4·0.4 (shop) = 0.0384; → Rainy: 0.0384·0.7·0.5 (clean) = 0.01344.

Limitations

  • The Markov assumption limits long-range context.

  • Observations depend only on the current state.

  • For many tasks, CRFs, RNNs and Transformers now perform better, but HMMs stay interpretable and data-efficient.

Common Pitfalls

  • Multiplying many small probabilities without logs (numerical underflow).

  • Too many hidden states for the data, which overfits.

  • Baum-Welch converging to poor local optima; use several initializations.

Interview Tips

  • Define π, A and B and the three classic problems.

  • Walk through Viterbi on a tiny example.

30

What is the Expectation-Maximization algorithm and how does probability play a role?

Expectation-Maximization (EM) is an iterative method for maximum likelihood estimation when the model has hidden (latent) variables or missing data, such as which cluster generated each point in a Gaussian mixture model: the E-step uses the current parameters to compute the posterior probability of each latent value for every data point (the “responsibilities”), and the M-step re-estimates the parameters by maximizing the expected complete-data log-likelihood under those probabilities, which for mixtures means weighted means, variances and mixing proportions. Each iteration never decreases the data’s log-likelihood, so EM converges, though only to a local optimum, which is why it is run from several starting points. It is used for Gaussian mixture clustering, hidden Markov models (Baum-Welch), missing-data imputation and topic models.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x, SciPy 1.13+ and scikit-learn 1.5+

The Steps for a Gaussian Mixture

Step Computation
Initialize Means, variances, weights πₖ (e.g. from k-means)
E-step rᵢₖ = πₖ N(xᵢ | μₖ, σₖ²) / Σⱼ πⱼ N(xᵢ | μⱼ, σⱼ²)
M-step Nₖ = Σᵢ rᵢₖ; πₖ = Nₖ/n; μₖ = Σᵢ rᵢₖxᵢ / Nₖ; σₖ² = Σᵢ rᵢₖ(xᵢ − μₖ)² / Nₖ
Repeat Until the log-likelihood stops improving

Code Example: EM for a 1-D Mixture from Scratch

import numpy as np
from scipy import stats
from sklearn.mixture import GaussianMixture

rng = np.random.default_rng(42)
x = np.r_[rng.normal(-2, 0.8, 300), rng.normal(3, 1.5, 700)]   # true: weights 0.3/0.7, means -2/3

def em_gmm(x, k=2, iters=200, tol=1e-8):
    weights = np.full(k, 1 / k)
    means = np.quantile(x, np.linspace(0.25, 0.75, k))
    sds = np.full(k, x.std())
    prev = -np.inf
    for _ in range(iters):
        dens = weights * stats.norm.pdf(x[:, None], means, sds)    # shape (n, k)
        log_lik = np.log(dens.sum(axis=1)).sum()
        resp = dens / dens.sum(axis=1, keepdims=True)              # E-step: responsibilities
        nk = resp.sum(axis=0)                                      # M-step
        weights = nk / len(x)
        means = (resp * x[:, None]).sum(axis=0) / nk
        sds = np.sqrt((resp * (x[:, None] - means) ** 2).sum(axis=0) / nk)
        if log_lik - prev < tol:
            break
        prev = log_lik
    return weights, means, sds, log_lik

w, mu, sd, ll = em_gmm(x)
order = np.argsort(mu)
print("weights", w[order].round(3), "means", mu[order].round(3), "sds", sd[order].round(3))
# close to [0.3, 0.7], [-2, 3], [0.8, 1.5]

gm = GaussianMixture(n_components=2, random_state=42).fit(x[:, None])
order = np.argsort(gm.means_.ravel())
print("sklearn weights", gm.weights_[order].round(3), "means", gm.means_.ravel()[order].round(3))
print("P(component | x=0.5):", gm.predict_proba([[0.5]]).round(3))   # soft assignment

Why It Works

  • The E-step builds a lower bound on the log-likelihood that touches it at the current parameters.

  • The M-step maximizes that bound, so the true log-likelihood can only go up.

  • Soft assignments let points contribute partially to several components, unlike k-means’ hard assignments (k-means is a limiting case of EM with equal, shrinking variances).

Common Pitfalls

  • Local optima; run several initializations (n_init in scikit-learn).

  • A component collapsing onto a single point (variance → 0); add a variance floor or regularization (reg_covar).

  • Choosing the number of components without BIC/AIC or validation.

Interview Tips

  • Describe the E-step as computing posterior probabilities of latent variables and the M-step as weighted MLE.

  • Mention monotone likelihood increase, local optima and GMM/HMM uses.

31

Write a function for the probability distribution of the sum of two dice

The sum S of two fair six-sided dice takes values 2 to 12, and because all 36 ordered outcomes (d₁, d₂) are equally likely, P(S = s) is the number of pairs that add to s divided by 36: the count rises from 1 (for 2) to 6 (for 7) and falls back to 1 (for 12), so P(S = s) = (6 − |s − 7|) / 36. A clean function enumerates the outcomes (or convolves the two single-die distributions, which generalizes to any number of dice or unfair dice), returns exact fractions, and can be checked against the closed form, the expected value E[S] = 7, the variance Var(S) = 35/6 ≈ 5.83, and a simulation.

Read full answer

Applies toPython 3.12, NumPy 2.x

The Distribution

Sum 2 3 4 5 6 7 8 9 10 11 12
Ways 1 2 3 4 5 6 5 4 3 2 1
P(S) 1/36 2/36 3/36 4/36 5/36 6/36 5/36 4/36 3/36 2/36 1/36

Code Example

from collections import Counter
from fractions import Fraction
from itertools import product

import numpy as np

def dice_sum_distribution(n_dice=2, faces=6):
    """Exact distribution of the sum of n fair dice, as {sum: Fraction}."""
    counts = Counter(sum(roll) for roll in product(range(1, faces + 1), repeat=n_dice))
    total = faces ** n_dice
    return {s: Fraction(c, total) for s, c in sorted(counts.items())}

def sum_distribution_convolve(die_probs, n_dice=2):
    """Same idea by convolution; works for unfair dice. die_probs[i] = P(face i + 1)."""
    dist = np.array([1.0])
    for _ in range(n_dice):
        dist = np.convolve(dist, die_probs)
    return {s + n_dice: float(p) for s, p in enumerate(dist)}

dist = dice_sum_distribution()
for s, p in dist.items():
    assert p == Fraction(6 - abs(s - 7), 36)            # closed form
print({s: str(p) for s, p in dist.items()})
# {2: '1/36', 3: '1/18', 4: '1/12', 5: '1/9', 6: '5/36', 7: '1/6', 8: '5/36', ...}

mean = sum(s * p for s, p in dist.items())
var = sum((s - mean) ** 2 * p for s, p in dist.items())
print(mean, var, round(float(var), 4))                  # 7 35/6 5.8333

conv = sum_distribution_convolve(np.full(6, 1 / 6))
print(all(np.isclose(conv[s], float(p)) for s, p in dist.items()))   # True

rng = np.random.default_rng(42)
rolls = rng.integers(1, 7, size=(100_000, 2)).sum(axis=1)
print(f"P(S=7): exact {float(dist[7]):.4f}, simulated {np.mean(rolls == 7):.4f}")   # 0.1667 vs about 0.167

print(f"P(S >= 10) = {sum(p for s, p in dist.items() if s >= 10)}")    # 1/6 (3 + 2 + 1 = 6 ways)

Common Pitfalls

  • Counting unordered pairs: (1, 2) and (2, 1) are different outcomes, so 3 has 2 ways, not 1.

  • Using floats where exact fractions are wanted for checking.

  • Enumerating 6ⁿ outcomes for many dice; use convolution or dynamic programming instead.

Interview Tips

  • Derive the 1-2-3-4-5-6-5-4-3-2-1 pattern and E[S] = 7.

  • Mention convolution for unfair or many dice and verify with a simulation.

32

Simulate a biased coin and estimate its bias

To simulate a coin with P(heads) = p, draw uniform random numbers and call a flip heads when the number is below p (or use rng.random(n) < p or rng.binomial); to estimate an unknown bias, the maximum likelihood estimate is just the share of heads, p̂ = heads / n, whose standard error is √(p̂(1 − p̂)/n), so the estimate’s uncertainty shrinks like 1/√n. A good answer also gives a confidence interval (the Wilson interval behaves better than p̂ ± 1.96·SE near 0 or 1), a Bayesian alternative with a Beta posterior, and a test of whether the coin is fair. A related classic is von Neumann’s trick for getting fair flips from a biased coin: flip twice, map HT → heads and TH → tails, and discard HH and TT.

Read full answer

Applies toPython 3.12, NumPy 2.x, SciPy 1.13+

Code Example

import numpy as np
from scipy import stats

rng = np.random.default_rng(42)

def flip_biased(p, n, rng):
    return (rng.random(n) < p).astype(int)              # 1 = heads

def estimate_bias(flips, conf=0.95):
    n, heads = len(flips), int(flips.sum())
    p_hat = heads / n
    z = stats.norm.ppf(0.5 + conf / 2)
    centre = (p_hat + z**2 / (2 * n)) / (1 + z**2 / n)   # Wilson score interval
    half = z * np.sqrt(p_hat * (1 - p_hat) / n + z**2 / (4 * n**2)) / (1 + z**2 / n)
    return p_hat, (centre - half, centre + half)

true_p = 0.65
for n in [10, 100, 1000, 10_000]:
    p_hat, (lo, hi) = estimate_bias(flip_biased(true_p, n, rng))
    print(f"n={n:>6}: estimate {p_hat:.3f}, 95% CI [{lo:.3f}, {hi:.3f}]")
# The interval narrows roughly like 1/sqrt(n) and contains 0.65 in about 95% of runs

flips = flip_biased(true_p, 200, rng)
heads = int(flips.sum())
print(f"fair-coin test p-value: {stats.binomtest(heads, 200, 0.5).pvalue:.2e}")   # tiny: coin is biased

posterior = stats.beta(1 + heads, 1 + 200 - heads)      # uniform prior
print(f"posterior mean {posterior.mean():.3f}, P(p > 0.5 | data) = {posterior.sf(0.5):.4f}")

def von_neumann_fair(flips):
    pairs = flips[: len(flips) // 2 * 2].reshape(-1, 2)
    keep = pairs[:, 0] != pairs[:, 1]
    return pairs[keep, 0]                                # HT -> 1, TH -> 0

fair = von_neumann_fair(flip_biased(true_p, 100_000, rng))
print(f"von Neumann output: share of heads {fair.mean():.3f} from {len(fair)} bits")
# about 0.500; about 2 * 0.65 * 0.35 = 45.5% of pairs are kept, so roughly 22,750 bits

Key Formulas

Quantity Formula
MLE p̂ = heads / n
Standard error √(p̂(1 − p̂)/n)
Flips for margin ±m at 95% (worst case p = 0.5) n ≈ 1.96² · 0.25 / m² (≈ 9,604 for m = 0.01)
Beta posterior Beta(a + heads, b + tails)

Common Pitfalls

  • The normal (Wald) interval with small n or p near 0 or 1; it can go outside [0, 1].

  • Reusing one global seed in a way that makes “independent” simulations identical.

  • Concluding a coin is fair from a non-significant test with only a few flips.

Interview Tips

  • Give the MLE, its standard error and the 1/√n behaviour.

  • Mention Wilson intervals, the Beta posterior and von Neumann’s trick.

33

Implement a Gaussian Naive Bayes classifier from scratch

Gaussian Naive Bayes predicts the class y that maximizes P(y)·Π P(xⱼ | y), assuming the features are independent given the class and that each feature follows a normal distribution within each class; training just estimates, for every class, its prior (its share of the training rows) and the mean and variance of each feature, and prediction sums log-priors and Gaussian log-densities so that tiny probabilities don’t underflow. A small variance smoothing term (scikit-learn adds 1e-9 times the largest feature variance) keeps a zero-variance feature from producing infinite densities, and the posterior probabilities come from normalizing with log-sum-exp. A from-scratch version should match sklearn.naive_bayes.GaussianNB.

Read full answer

Applies toPython 3.12, NumPy 2.x, SciPy 1.13+, scikit-learn 1.5+

The Math

Step Formula
Prior P(y = c) = n_c / n
Likelihood per feature N(xⱼ; μ_cj, σ²_cj)
Log posterior (unnormalized) log P© − ½ Σⱼ [log(2πσ²_cj) + (xⱼ − μ_cj)² / σ²_cj]
Normalize Subtract logsumexp over classes, then exponentiate

Code Example

import numpy as np
from scipy.special import logsumexp
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

class GaussianNaiveBayes:
    def __init__(self, var_smoothing=1e-9):
        self.var_smoothing = var_smoothing

    def fit(self, X, y):
        X, y = np.asarray(X, dtype=float), np.asarray(y)
        self.classes_ = np.unique(y)
        epsilon = self.var_smoothing * X.var(axis=0).max()
        self.theta_ = np.array([X[y == c].mean(axis=0) for c in self.classes_])
        self.var_ = np.array([X[y == c].var(axis=0) for c in self.classes_]) + epsilon
        self.log_prior_ = np.log(np.array([np.mean(y == c) for c in self.classes_]))
        return self

    def _joint_log_likelihood(self, X):
        X = np.asarray(X, dtype=float)
        jll = []
        for k in range(len(self.classes_)):
            log_lik = -0.5 * np.sum(np.log(2 * np.pi * self.var_[k])
                                    + (X - self.theta_[k]) ** 2 / self.var_[k], axis=1)
            jll.append(self.log_prior_[k] + log_lik)
        return np.column_stack(jll)                     # shape (n_samples, n_classes)

    def predict_proba(self, X):
        jll = self._joint_log_likelihood(X)
        return np.exp(jll - logsumexp(jll, axis=1, keepdims=True))

    def predict(self, X):
        return self.classes_[np.argmax(self._joint_log_likelihood(X), axis=1)]

X, y = load_iris(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)

mine = GaussianNaiveBayes().fit(X_tr, y_tr)
ref = GaussianNB().fit(X_tr, y_tr)
print(f"from scratch accuracy {np.mean(mine.predict(X_te) == y_te):.3f}")
print(f"sklearn accuracy      {np.mean(ref.predict(X_te) == y_te):.3f}")
print("same predictions:", np.array_equal(mine.predict(X_te), ref.predict(X_te)))      # True
print("same probabilities:", np.allclose(mine.predict_proba(X_te), ref.predict_proba(X_te)))   # True

Iris accuracy for this split is typically above 0.9; the important check is that both implementations agree.

Common Pitfalls

  • Multiplying raw densities instead of adding logs (underflow with many features).

  • Zero variance for a feature within a class (division by zero); add smoothing.

  • Using the sample variance with ddof = 1 and then wondering why results differ slightly from scikit-learn (it uses ddof = 0).

  • Expecting well-calibrated probabilities; correlated features make NB overconfident.

Interview Tips

  • Write the log-posterior and explain the independence assumption.

  • Mention log-sum-exp, variance smoothing and checking against scikit-learn.

34

Simulate the Law of Large Numbers with coin tosses

The Law of Large Numbers says that the average of many independent, identically distributed trials with a finite mean converges to that expected value: for a fair coin, the running share of heads after n tosses approaches 0.5 as n grows, even though any short run can be far from it. A simulation plots the running proportion for several independent sequences and shows them all closing in on 0.5, with the typical deviation shrinking like 0.5/√n (the standard error), which is what the Central Limit Theorem quantifies. Note what LLN does not say: the count of heads minus n/2 does not shrink (it typically grows like √n), and past results do not make future tosses “correct” themselves (the gambler’s fallacy).

Read full answer

Applies toPython 3.12, NumPy 2.x, Matplotlib 3.8+

Code Example

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(42)
n, n_paths = 10_000, 5
tosses = rng.integers(0, 2, size=(n_paths, n))          # 1 = heads
counts = np.arange(1, n + 1)
running_share = tosses.cumsum(axis=1) / counts

for checkpoint in [10, 100, 1000, 10_000]:
    shares = running_share[:, checkpoint - 1]
    print(f"n={checkpoint:>6}: shares {np.round(shares, 3)}  expected spread ±{0.5 / np.sqrt(checkpoint):.3f}")
# At n=10 the shares can be 0.2 or 0.8; by n=10,000 they sit within about ±0.01 of 0.5

excess_heads = tosses.cumsum(axis=1)[:, -1] - n / 2
print("heads minus n/2 at the end:", excess_heads)      # typically tens, not near 0: LLN is about averages

fig, ax = plt.subplots(figsize=(8, 4))
for path in running_share:
    ax.plot(counts, path, linewidth=1)
ax.axhline(0.5, color="black", linestyle="--", label="expected value 0.5")
ax.fill_between(counts, 0.5 - 1.96 * 0.5 / np.sqrt(counts), 0.5 + 1.96 * 0.5 / np.sqrt(counts),
                color="grey", alpha=0.2, label="95% band, ±1.96·0.5/√n")
ax.set(xscale="log", xlabel="number of tosses", ylabel="share of heads", ylim=(0, 1),
       title="Law of Large Numbers: running share of heads")
ax.legend()
fig.tight_layout()
fig.savefig("lln_coin.png", dpi=120)

many = rng.integers(0, 2, size=(2000, 1000)).mean(axis=1)    # 2000 experiments of 1000 tosses
print(f"sd of the share across experiments {many.std():.4f} vs theory {0.5 / np.sqrt(1000):.4f}")   # about 0.0158

Statements and Conditions

Result Statement Conditions
Weak LLN The sample mean converges in probability to μ i.i.d., finite mean
Strong LLN The sample mean converges almost surely to μ i.i.d., finite mean
CLT √n(x̄ − μ)/σ approaches N(0, 1) i.i.d., finite variance

The Cauchy distribution has no mean, so its running average never settles, a useful counterexample.

Common Pitfalls

  • The gambler’s fallacy: after many heads, tails is still 50%.

  • Thinking the absolute difference in counts shrinks.

  • Applying LLN to dependent or non-identically distributed data without checking conditions.

Interview Tips

  • State LLN with its conditions and show the 1/√n shrinkage.

  • Distinguish LLN (convergence of the mean) from CLT (shape of the fluctuations).

35

Estimate the mean and variance of a dataset and plot its fitted Gaussian

For data assumed to come from a normal distribution, the maximum likelihood estimates are the sample mean μ̂ = (1/n)Σxᵢ and the variance σ̂² = (1/n)Σ(xᵢ − μ̂)²; the unbiased sample variance divides by n − 1 instead (ddof=1), which matters only for small samples. To plot the fit, draw a density-normalized histogram and overlay the normal pdf with those parameters; then check the fit rather than assuming it, with a Q-Q plot, skewness and kurtosis, or a normality test, and give a confidence interval for the mean (μ̂ ± t·s/√n) to show its uncertainty.

Read full answer

Applies toPython 3.12, NumPy 2.x, SciPy 1.13+, Matplotlib 3.8+

Code Example

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

rng = np.random.default_rng(42)
data = rng.normal(loc=50, scale=8, size=500)

n = len(data)
mean = data.mean()
var_mle = data.var()                 # divides by n
var_unbiased = data.var(ddof=1)      # divides by n - 1
sd = np.sqrt(var_unbiased)
print(f"mean {mean:.2f}, variance (MLE) {var_mle:.2f}, variance (unbiased) {var_unbiased:.2f}, sd {sd:.2f}")
# close to the true mean 50 and variance 64

mu_fit, sigma_fit = stats.norm.fit(data)             # MLE: same as mean and sqrt(var_mle)
print(np.isclose(mu_fit, mean), np.isclose(sigma_fit, np.sqrt(var_mle)))   # True True

lo, hi = stats.t.interval(0.95, df=n - 1, loc=mean, scale=sd / np.sqrt(n))
print(f"95% CI for the mean: [{lo:.2f}, {hi:.2f}]")
print(f"skew {stats.skew(data):.3f}, excess kurtosis {stats.kurtosis(data):.3f}")   # both near 0
print(f"Shapiro-Wilk p = {stats.shapiro(data).pvalue:.3f}")

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.hist(data, bins=30, density=True, alpha=0.6, label="data")
xs = np.linspace(data.min() - 5, data.max() + 5, 300)
ax1.plot(xs, stats.norm.pdf(xs, mean, sd), linewidth=2, label=f"N({mean:.1f}, {sd:.1f}²)")
ax1.axvline(mean, linestyle="--", color="black")
ax1.set(title="Histogram with fitted Gaussian", xlabel="value", ylabel="density")
ax1.legend()
stats.probplot(data, dist="norm", plot=ax2)          # points on the line = good normal fit
ax2.set_title("Normal Q-Q plot")
fig.tight_layout()
fig.savefig("gaussian_fit.png", dpi=120)

MLE vs Unbiased Variance

Estimator Divisor Bias NumPy
MLE n Biased low by a factor (n − 1)/n x.var()
Unbiased n − 1 Unbiased for σ² x.var(ddof=1)

pandas’ Series.var() defaults to ddof = 1 while NumPy defaults to 0, a common source of small mismatches.

Common Pitfalls

  • Plotting a histogram of counts against a pdf (forgetting density=True).

  • Assuming normality without checking skewed or heavy-tailed data (consider a log transform or another distribution).

  • Relying on a normality test with huge samples, where tiny harmless deviations become “significant”.

Interview Tips

  • Give the MLE formulas and explain n vs n − 1.

  • Show a Q-Q plot as the check of fit.

36

How would you design a probabilistic recommendation system?

A probabilistic recommender models how likely each user is to engage with each item, P(click or purchase | user, item, context), and ranks items by expected value, using models such as Bayesian estimates of item popularity (Beta-Binomial smoothing of click-through rates), probabilistic matrix factorization where user and item vectors have Gaussian priors, logistic or gradient-boosted models trained on interaction features, and topic or mixture models for user interests. Because it outputs calibrated probabilities with uncertainty, it can handle cold start (new items borrow strength from priors), explore uncertain items with Thompson sampling or UCB instead of only showing known winners, combine objectives as expected value (P(click) × value), and be evaluated offline with log-loss and calibration plus ranking metrics, then online with A/B tests.

Read full answer

Applies tosystem design; examples with NumPy 2.x

Architecture

Stage Probabilistic component
Candidate generation Probabilistic matrix factorization, co-occurrence P(item B | item A), embeddings
Scoring Calibrated P(engage | user, item, context) from logistic regression or boosted trees
Cold start Priors from item category or content features; hierarchical models
Exploration Thompson sampling on Beta posteriors
Ranking Expected value = P(engage) × value, with diversity constraints
Evaluation Log-loss, calibration, NDCG and recall@k offline; A/B tests online

Code Example: Smoothed CTRs and Thompson Sampling

import numpy as np

rng = np.random.default_rng(42)
true_ctr = np.array([0.04, 0.05, 0.08, 0.03, 0.06])     # unknown in practice
alpha = np.ones(5)                                       # Beta(1, 1) priors
beta = np.ones(5)

shows = np.zeros(5, dtype=int)
for _ in range(20_000):                                  # each round: show one item
    sampled = rng.beta(alpha, beta)                      # one draw from each posterior
    item = int(np.argmax(sampled))                       # Thompson sampling
    click = rng.random() < true_ctr[item]
    alpha[item] += click
    beta[item] += 1 - click
    shows[item] += 1

print("share of impressions:", (shows / shows.sum()).round(3))   # most go to item 2 (CTR 0.08)
print("posterior mean CTR:", (alpha / (alpha + beta)).round(3))

# Empirical Bayes smoothing for ranking items with few views
clicks = np.array([2, 40, 1])
views = np.array([5, 1000, 3])
prior_a, prior_b = 5, 95                                 # prior mean 5%, worth 100 pseudo-views
raw = clicks / views
smoothed = (clicks + prior_a) / (views + prior_a + prior_b)
print("raw CTR:", raw.round(3), "smoothed:", smoothed.round(3))
# raw [0.4 0.04 0.333] -> smoothed [0.067 0.041 0.058]: a lucky 2 of 5 no longer ranks first

Design Considerations

  • Position bias: users click top slots more; correct with inverse propensity weighting or position features.

  • Feedback loops: the model only learns about what it shows; exploration and logged propensities help.

  • Calibration: probabilities should match observed rates so expected-value ranking works.

  • Diversity and freshness beyond pure P(click).

Common Pitfalls

  • Ranking by raw CTR, which favours items with tiny samples.

  • Evaluating only on offline ranking metrics without calibration or online tests.

  • Never exploring, so new items never get a chance.

Interview Tips

  • Frame it as estimating P(engagement) with uncertainty.

  • Mention Beta smoothing, Thompson sampling, position bias and calibration.

37

How would you build a probabilistic model to predict customer churn?

A probabilistic churn model estimates, for each customer, the probability of leaving within a set window (for example the next 30 days), or the whole survival curve over time, so that the business can rank customers by risk, estimate expected revenue loss (P(churn) × customer value) and decide who gets a retention offer. Typical choices are logistic regression or gradient boosting trained on behavioural features (usage trend, tenure, support tickets, payment failures, plan) with well-calibrated outputs, survival models (Cox proportional hazards, Weibull, or discrete-time hazard models) that handle customers who haven’t churned yet as censored data, and for non-contract businesses “buy till you die” models such as BG/NBD that estimate the probability a customer is still active. Validate with time-based splits, log-loss and calibration plots, then measure the retention campaign with an uplift test, since high risk doesn’t mean an offer will change the outcome.

Read full answer

Applies tosystem design; examples with NumPy 2.x and scikit-learn 1.5+

Building Blocks

Step Detail
Define churn Cancellation, or no activity for N days; fix the prediction window
Features Measured strictly before the prediction date (no leakage)
Model Logistic regression / boosted trees with calibration, or survival model
Validation Train on earlier cohorts, test on later ones
Metrics Log-loss, Brier score, calibration, precision at top-k, AUC
Decision Target where P(churn) × value × uplift exceeds the offer cost

Code Example

import numpy as np
from sklearn.calibration import calibration_curve
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import brier_score_loss, roc_auc_score

rng = np.random.default_rng(42)
n = 6000
tenure = rng.exponential(18, n)                       # months
usage_drop = rng.normal(0, 1, n)                      # standardized change in usage
tickets = rng.poisson(1.0, n)
logit = -2.0 - 0.04 * tenure + 1.1 * usage_drop + 0.4 * tickets
churn = rng.random(n) < 1 / (1 + np.exp(-logit))
X = np.column_stack([tenure, usage_drop, tickets])

signup_order = np.argsort(rng.random(n))              # stand-in for a time-ordered split
train, test = signup_order[:4500], signup_order[4500:]
model = LogisticRegression(max_iter=1000).fit(X[train], churn[train])
p = model.predict_proba(X[test])[:, 1]
print(f"churn rate {churn.mean():.3f}, AUC {roc_auc_score(churn[test], p):.3f}, Brier {brier_score_loss(churn[test], p):.4f}")

frac_pos, mean_pred = calibration_curve(churn[test], p, n_bins=5, strategy="quantile")
print("calibration (predicted vs observed):", list(zip(mean_pred.round(3), frac_pos.round(3))))

value = rng.uniform(20, 200, len(test))               # monthly revenue per customer
expected_loss = p * value * 12
offer_cost, assumed_uplift = 30, 0.25                  # uplift should come from an A/B test
target = expected_loss * assumed_uplift > offer_cost
print(f"customers to target: {target.sum()} of {len(test)}")

Survival View

A survival model gives S(t) = P(still a customer after t months) and handles censoring: customers who are still active only tell you that their churn time is longer than their current tenure. Dropping them or treating them as non-churners biases the estimates.

Common Pitfalls

  • Leakage: features computed after the churn date (such as a cancellation-reason field).

  • Random splits across time; use time-based validation.

  • Targeting high-risk customers who would churn anyway, or would stay anyway; measure uplift.

  • Uncalibrated scores used as probabilities in expected-value calculations.

Interview Tips

  • Define churn and the prediction window first.

  • Mention calibration, survival analysis for censoring, and uplift-based targeting.

38

How would you use probability to detect anomalies in financial transactions?

Probabilistic anomaly detection models what normal transactions look like as a probability distribution, then flags transactions that are very unlikely under that model: a low density p(x), a large negative log-likelihood, or a tail probability below a threshold. For financial data this means per-customer baselines (a customer’s typical amount, merchant types, times and locations), heavy-tailed or log-normal models for amounts, Gaussian mixtures or kernel density estimates for multi-feature behaviour, Poisson models for transaction frequency (five purchases in a minute is unlikely when the rate is two per day), and Bayesian updating as behaviour changes; supervised fraud models then combine these scores with labelled fraud. Thresholds are set from the cost of false alarms vs missed fraud, and because fraud is rare (often under 0.1%), Bayes’ theorem reminds you that even an accurate detector will produce many false positives per real fraud.

Read full answer

Applies tosystem design; examples with NumPy 2.x, SciPy 1.13+ and scikit-learn 1.5+

Methods

Method Idea Good for
Per-user z-score on log amount Distance from the user’s usual spend Simple amount spikes
Poisson rate model P(k or more transactions in a window) Bursts of activity
Gaussian mixture / KDE Low density in feature space Multi-feature behaviour
Bayesian change detection Posterior that behaviour has shifted Account takeover
Supervised classifier P(fraud | features), calibrated When labels exist

Code Example

import numpy as np
from scipy import stats
from sklearn.mixture import GaussianMixture

rng = np.random.default_rng(42)
log_amount = rng.normal(np.log(40), 0.6, 5000)           # typical spend about $40
hour = np.clip(rng.normal(14, 3.5, 5000), 0, 23.99)
normal = np.column_stack([log_amount, hour])

gmm = GaussianMixture(n_components=3, random_state=42).fit(normal)
threshold = np.percentile(gmm.score_samples(normal), 0.5)  # flag the bottom 0.5% of normal density

new = np.array([[np.log(45), 13.0],                       # ordinary
                [np.log(2500), 3.0],                      # huge amount at 3 am
                [np.log(35), 2.5]])                       # usual amount, odd hour
scores = gmm.score_samples(new)
print("log-density:", scores.round(2), "flagged:", scores < threshold)   # expected: [False True True]

daily_rate = 2.0                                          # Poisson burst check
rate_10min = daily_rate * 10 / (24 * 60)
print(f"P(5 or more in 10 minutes) = {stats.poisson.sf(4, rate_10min):.2e}")   # about 4e-12

prevalence, sensitivity, specificity = 0.001, 0.95, 0.99  # base-rate reality check
ppv = sensitivity * prevalence / (sensitivity * prevalence + (1 - specificity) * (1 - prevalence))
print(f"P(fraud | flagged) = {ppv:.3f}")                  # 0.087: about 10 false alarms per real fraud

Production Concerns

  • Concept drift: normal behaviour changes (holidays, new merchants); retrain or update online.

  • Latency: scoring must happen before authorization, so models must be fast.

  • Feedback: analysts’ decisions become labels for supervised models.

  • Explanations: investigators need to see why a transaction was flagged.

Common Pitfalls

  • Global thresholds for all customers; per-user baselines catch much more.

  • Ignoring base rates, which floods analysts with false positives.

  • Evaluating with accuracy on very imbalanced data; use precision-recall and cost.

Interview Tips

  • Describe modelling normal behaviour and flagging low-probability events.

  • Work the base-rate calculation and mention drift and per-user baselines.

39

How would you improve a Bayesian spam classifier?

A Bayesian (Naive Bayes) spam filter computes P(spam | words) from word likelihoods and a prior, and it can be improved at every step: better features (word bigrams, character n-grams that catch “fr3e”, sender and header features, URLs and domains, TF-IDF or binary presence), better smoothing (tuning the Laplace α instead of fixing it at 1), an honest prior matching the real spam rate, handling the independence assumption (dropping redundant features, or calibrating the overconfident probabilities), and a decision threshold chosen from costs because losing a real email is much worse than letting one spam through. Beyond that, keep the model fresh with online updates from user “mark as spam” feedback, add rules and reputation signals for adversarial tricks, compare with logistic regression or gradient boosting on the same features, and measure precision at a very low false-positive rate rather than accuracy.

Read full answer

Applies toexamples with scikit-learn 1.5+

Improvement Checklist

Area Improvement
Tokens Bigrams, character n-grams, URL and domain tokens, normalization of obfuscations
Metadata Sender reputation, SPF/DKIM results, header anomalies, links-to-text ratio
Model Tune α; Complement NB for imbalance; compare with logistic regression
Probabilities Calibrate (isotonic/Platt); NB scores are overconfident
Threshold Pick for target false-positive rate (e.g. ≤ 0.1% of real email)
Updates Incremental partial_fit from user feedback; watch for drift
Adversaries Image spam, word salad, poisoning of feedback

Code Example

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import GridSearchCV
from sklearn.naive_bayes import ComplementNB, MultinomialNB
from sklearn.pipeline import Pipeline

spam = ["win a free prize now", "free money claim now", "cheap meds online offer",
        "you won a free cruise", "claim your free gift card", "fr3e prize click now"] * 10
ham = ["meeting moved to monday", "please review the report", "lunch tomorrow?",
       "project update attached", "can you call me later", "notes from the team meeting"] * 10
texts = spam + ham
y = np.array([1] * len(spam) + [0] * len(ham))

pipe = Pipeline([
    ("tfidf", TfidfVectorizer(analyzer="char_wb", ngram_range=(2, 4), sublinear_tf=True)),
    ("nb", MultinomialNB()),
])
grid = GridSearchCV(pipe, {"nb": [MultinomialNB(), ComplementNB()], "nb__alpha": [0.01, 0.1, 1.0]},
                    cv=5, scoring="average_precision")
grid.fit(texts, y)
print("best:", grid.best_params_)

model = grid.best_estimator_
for msg in ["fre3 pr1ze, claim now", "can we move the meeting"]:
    print(f"{msg!r}: P(spam) = {model.predict_proba([msg])[0, 1]:.3f}")
# Character n-grams still match the obfuscated "fre3 pr1ze"; the meeting note scores low

def classify(p_spam, threshold=0.99):
    """A high threshold keeps real email out of the spam folder."""
    return "spam" if p_spam >= threshold else "inbox"

print(classify(0.95), classify(0.995))                 # inbox spam

The toy data repeats a few messages, so cross-validation is optimistic here; with real data, split by time and sender so near-duplicates don’t leak across folds.

Common Pitfalls

  • Tuning on accuracy rather than precision at a low false-positive rate.

  • A training spam ratio far from reality, which skews the prior.

  • Never updating, so new spam campaigns slip through.

  • Trusting feedback blindly; attackers can mark spam as “not spam”.

Interview Tips

  • Name concrete feature, smoothing, calibration and threshold improvements.

  • Stress asymmetric costs and continual learning.

40

How would you use Markov chains to model user behaviour on a website?

Model each page (or page type such as Home, Search, Product, Cart, Checkout) as a state and estimate a transition matrix from session logs, where P(j | i) = (number of moves from page i to page j) / (moves out of page i), with absorbing states for “Purchase” and “Exit”; the fitted chain then answers questions like the probability that a visitor who lands on a product page eventually buys, the expected number of steps to purchase, where users drop off, and which next page to recommend or prefetch. Absorbing-chain math gives these answers exactly through the fundamental matrix N = (I − Q)⁻¹, and the chain supports attribution (the “removal effect”: how much conversion falls if a page or channel is removed) and simulation of design changes. Caveats are that a first-order chain forgets history, so add states such as “Product after Search” or use higher-order chains when the path matters, and that sparse transitions need smoothing.

Read full answer

Applies toexamples with NumPy 2.x

Steps

  1. Sessionize logs and map URLs to page types; add Start, Purchase and Exit states.

  2. Count transitions and normalize rows (with additive smoothing for rare moves).

  3. Compute absorption probabilities and expected steps.

  4. Validate on later sessions (log-likelihood of held-out paths, predicted vs actual conversion).

  5. Use it: funnel drop-off, next-page prediction, removal-effect attribution.

Code Example

import numpy as np

pages = ["Home", "Search", "Product", "Cart", "Purchase", "Exit"]
idx = {p: i for i, p in enumerate(pages)}
sessions = [
    ["Home", "Search", "Product", "Cart", "Purchase"],
    ["Home", "Product", "Exit"],
    ["Search", "Product", "Product", "Cart", "Exit"],
    ["Home", "Search", "Exit"],
    ["Product", "Cart", "Purchase"],
    ["Home", "Product", "Cart", "Purchase"],
    ["Home", "Exit"],
    ["Search", "Product", "Exit"],
]
counts = np.zeros((6, 6))
for path in sessions:
    for a, b in zip(path, path[1:]):
        counts[idx[a], idx[b]] += 1
counts[idx["Purchase"], idx["Purchase"]] = 1            # absorbing states
counts[idx["Exit"], idx["Exit"]] = 1
P = counts / counts.sum(axis=1, keepdims=True)
print(np.round(P[:4], 2))

transient = [0, 1, 2, 3]
Q = P[np.ix_(transient, transient)]
R = P[np.ix_(transient, [4, 5])]
N = np.linalg.inv(np.eye(4) - Q)                        # fundamental matrix
B = N @ R                                               # absorption probabilities
for page, row, steps in zip(pages[:4], B, N.sum(axis=1)):
    print(f"from {page:8s} P(purchase) = {row[0]:.3f}, expected steps before leaving = {steps:.2f}")
# From Cart: 3 purchases and 1 exit in the data, so P(purchase) = 0.75

def next_page(current):
    return pages[int(np.argmax(P[idx[current]]))]
print("most likely after Search:", next_page("Search"))   # Product

Hand check: Cart has 4 outgoing moves (3 to Purchase, 1 to Exit), so P(purchase | Cart) = 0.75. Product has 7 outgoing moves: 4 to Cart, 1 to itself and 2 to Exit, so P(purchase | Product) = (4/7 · 0.75) / (1 − 1/7) = 0.5.

Common Pitfalls

  • Using raw URLs as states, which creates thousands of sparse states; group into page types.

  • Ignoring history when it matters (first-order assumption).

  • Forgetting the Exit state, which hides drop-off.

  • Estimating rare transitions from a handful of counts without smoothing.

Interview Tips

  • Describe states, the transition matrix and absorbing states.

  • Mention the fundamental matrix, next-page prediction and removal-effect attribution.

41

How do quantum computing and probabilistic algorithms relate?

Quantum computing is inherently probabilistic: a quantum state of n qubits holds complex amplitudes over 2ⁿ basis states, and measuring it returns each outcome with probability equal to the squared magnitude of its amplitude (the Born rule), so quantum algorithms are designed to make interference boost the amplitudes of correct answers and cancel wrong ones, then repeat runs to get the answer with high probability. That makes them close relatives of classical randomized algorithms (Monte Carlo methods, randomized primality tests), with a key difference: amplitudes can be negative or complex and interfere, while classical probabilities only add. For machine learning, the main links are Grover-style amplitude amplification (quadratic speedups for search), quantum amplitude estimation (in principle quadratically fewer samples than Monte Carlo for estimating expectations), sampling from hard distributions (quantum Boltzmann machines, Born machines), and variational quantum circuits trained like small neural networks; practical advantage on real ML problems is still unproven because current hardware is small and noisy.

Read full answer

Applies togeneral concepts; simulation with NumPy 2.x

Classical vs Quantum Probability

Classical randomized algorithm Quantum algorithm
State Probability distribution over states Amplitudes over basis states
Values Non-negative, sum to 1 Complex, squared magnitudes sum to 1
Combination Probabilities add Amplitudes add and can cancel (interference)
Readout Sample Measurement collapses the state
Success High probability, repeat to boost High probability, repeat to boost

Code Example: Measurement Probabilities and Grover Amplification

import numpy as np

rng = np.random.default_rng(42)

H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)          # Hadamard gate
state = H @ np.array([1.0, 0.0])                      # |0> -> (|0> + |1>)/sqrt(2)
probs = np.abs(state) ** 2
print("P(0), P(1):", probs.round(3))                  # [0.5 0.5]
print("measured shares:", np.bincount(rng.choice(2, 10_000, p=probs)) / 10_000)

print("H applied twice:", (H @ state).round(3))       # [1. 0.]: the |1> paths cancel (interference)

N, marked = 16, 11                                    # Grover search over 16 items (4 qubits)
amps = np.full(N, 1 / np.sqrt(N))
iterations = int(np.floor(np.pi / 4 * np.sqrt(N)))    # 3
for _ in range(iterations):
    amps[marked] *= -1                                # oracle flips the sign of the answer
    amps = 2 * amps.mean() - amps                     # inversion about the mean
print(f"after {iterations} iterations P(marked) = {amps[marked] ** 2:.3f} vs random guess {1 / N:.3f}")
# about 0.961 after only 3 oracle calls; a classical search needs about N/2 = 8 on average

Relevance to ML

Idea Probabilistic link Status
Amplitude estimation Estimate expectations with error about 1/n instead of 1/√n Needs fault-tolerant hardware
Quantum sampling (Born machines) Model distributions as p(x) = |ψ(x)|² Research
Variational circuits Measurement outcomes as model outputs, trained by gradients Small-scale experiments
Quantum-inspired classical algorithms Randomized sampling for linear algebra Some “speedups” turned out to be classical

Common Pitfalls

  • Saying quantum computers “try all answers at once” and read out the right one; measurement gives one random outcome, and interference does the work.

  • Assuming exponential speedups for ML in general.

  • Ignoring the cost of loading classical data into quantum states.

Interview Tips

  • Explain the Born rule and interference vs classical probability.

  • Mention Grover and amplitude estimation as quadratic speedups, and be realistic about current hardware.

42

What role does probability play in reinforcement learning?

Reinforcement learning is built on probability: the environment is a Markov decision process with transition probabilities P(s′ | s, a) and (often random) rewards, policies are usually probability distributions over actions π(a | s), and value functions are expectations, V^π(s) = E[Σ γᵗ rₜ | s₀ = s], of the discounted return under that randomness. Probability also drives how agents learn: Monte Carlo and temporal-difference methods estimate those expectations from samples, policy gradient methods follow the gradient of expected return using ∇log π(a | s), exploration uses randomness (ε-greedy, softmax/Boltzmann, entropy bonuses, Thompson sampling with posteriors over values), off-policy learning reweights data with importance ratios π(a | s)/μ(a | s), and partially observable problems keep a belief, a probability distribution over hidden states, updated with Bayes’ rule.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x

Where Probability Appears

Component Probabilistic object
Environment P(s′, r | s, a)
Policy π(a | s), stochastic or deterministic
Value Expectation of discounted return
Learning Sample estimates (Monte Carlo, TD), stochastic gradients
Exploration ε-greedy, softmax, entropy regularization, Thompson sampling
Off-policy correction Importance sampling ratios
Partial observability Belief states b(s) updated by Bayes’ rule
Distributional RL Full distribution of returns, not just its mean

Code Example: Expected Value and Policy Gradient on a Bandit

import numpy as np

rng = np.random.default_rng(42)
p_win = np.array([0.2, 0.5, 0.7])                     # Bernoulli reward per action (unknown to the agent)

theta = np.zeros(3)                                    # softmax policy parameters
lr, baseline = 0.1, 0.0
for step in range(3000):
    pi = np.exp(theta - theta.max())
    pi /= pi.sum()
    a = rng.choice(3, p=pi)                            # sample an action from the policy
    r = float(rng.random() < p_win[a])                 # random reward
    baseline += 0.01 * (r - baseline)                  # running average reduces variance
    grad_log_pi = -pi
    grad_log_pi[a] += 1                                # gradient of log softmax
    theta += lr * (r - baseline) * grad_log_pi         # REINFORCE update
pi = np.exp(theta - theta.max())
pi /= pi.sum()
print("final policy:", pi.round(3))                    # most probability on action 2
print(f"expected reward: {pi @ p_win:.3f} (best possible 0.7)")

# Value of a stochastic policy in a 2-state MDP, solved exactly
P = {0: np.array([[0.9, 0.1], [0.2, 0.8]]),            # P[a][s, s'] for action 0
     1: np.array([[0.5, 0.5], [0.6, 0.4]])}
R = np.array([[1.0, 0.0], [0.0, 2.0]])                 # R[s, a]
policy = np.array([[0.5, 0.5], [0.3, 0.7]])            # pi(a | s)
gamma = 0.9
P_pi = sum(policy[:, [a]] * P[a] for a in (0, 1))      # state-to-state transitions under pi
R_pi = (policy * R).sum(axis=1)
V = np.linalg.solve(np.eye(2) - gamma * P_pi, R_pi)   # Bellman equation V = R + gamma P V
print("V(pi):", V.round(3))

Common Pitfalls

  • High-variance return estimates; use baselines, advantage estimates and more samples.

  • Exploration that is too greedy too early, which locks in a poor policy.

  • Huge importance weights in off-policy learning; clip or use per-decision methods.

Interview Tips

  • Name MDP transitions, stochastic policies and values as expectations.

  • Mention the log-derivative trick, exploration strategies and belief states.

43

How are Monte Carlo methods used in machine learning?

Monte Carlo methods estimate quantities that are hard to compute exactly by averaging over random samples: an expectation E[f(X)] is approximated by (1/n)Σ f(xᵢ) with xᵢ drawn from the distribution, and by the Law of Large Numbers this converges, with error shrinking like σ/√n regardless of the number of dimensions. In machine learning this idea is everywhere: stochastic gradient descent uses minibatches as Monte Carlo estimates of the full gradient, MCMC (Metropolis-Hastings, Gibbs, Hamiltonian Monte Carlo) samples Bayesian posteriors, dropout at test time (MC dropout) estimates predictive uncertainty, variational inference and VAEs use sampled estimates of the ELBO with the reparameterization trick, reinforcement learning estimates returns from rollouts and searches game trees with Monte Carlo tree search, and the bootstrap and permutation tests estimate uncertainty and significance.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x and SciPy 1.13+

Uses in ML

Use What is sampled Estimates
SGD / minibatches Training examples Gradient of the loss
MCMC Parameter values Posterior means and intervals
MC dropout / ensembles Dropout masks or models Predictive uncertainty
Variational inference, VAEs Latent variables ELBO and its gradient
RL and MCTS Episodes, rollouts Returns, action values
Bootstrap Resampled datasets Confidence intervals of metrics
Importance sampling Proposal distribution Expectations under a target, off-policy values

Code Example

import numpy as np
from scipy import stats

rng = np.random.default_rng(42)

for n in [100, 10_000, 1_000_000]:                    # estimate pi: share of points inside the circle
    pts = rng.random((n, 2))
    est = 4 * np.mean((pts ** 2).sum(axis=1) <= 1)
    print(f"n={n:>9}: pi estimate {est:.4f}, error {abs(est - np.pi):.4f}")
# Error shrinks roughly like 1/sqrt(n)

x = rng.normal(0, 1, 200_000)                          # E[max(X, 0)] for X ~ N(0, 1)
print(f"MC {np.maximum(x, 0).mean():.4f} vs exact {1 / np.sqrt(2 * np.pi):.4f}")   # exact 0.3989

threshold = 4.0                                        # rare event: P(X > 4) = 3.17e-5
naive = np.mean(rng.normal(0, 1, 100_000) > threshold)
proposal = rng.normal(threshold, 1, 100_000)           # sample where the event happens
weights = stats.norm.pdf(proposal) / stats.norm.pdf(proposal, loc=threshold)
importance = np.mean((proposal > threshold) * weights)
print(f"exact {stats.norm.sf(threshold):.3e}, naive {naive:.3e}, importance sampling {importance:.3e}")
# Naive sampling sees only a few hits; importance sampling is accurate to a few percent

scores = rng.binomial(1, 0.83, 500)                    # per-example correctness of a model
boot = [rng.choice(scores, len(scores)).mean() for _ in range(5000)]
print(f"accuracy {scores.mean():.3f}, bootstrap 95% CI {np.round(np.percentile(boot, [2.5, 97.5]), 3)}")

Variance Reduction

  • Importance sampling for rare events or off-policy estimates.

  • Control variates and baselines (as in policy gradients).

  • Antithetic and quasi-random (Sobol) samples.

  • Stratified sampling across groups.

Common Pitfalls

  • Too few samples to trust the estimate; always report its standard error.

  • MCMC samples used before burn-in, or without convergence checks.

  • Importance weights with huge variance from a poor proposal distribution.

Interview Tips

  • Give the estimator and its 1/√n error rate.

  • List SGD, MCMC, MC dropout, VAEs, RL and the bootstrap as examples.

44

How do GANs relate to probability theory?

A generative adversarial network learns a probability distribution implicitly: the generator G turns random noise z ~ p(z) (often a standard normal) into samples G(z), which defines a distribution p_g over data space without ever writing its density down, and the discriminator D(x) estimates the probability that x came from the real data distribution p_data rather than from the generator. Training is the minimax game min_G max_D E{x~p_data}[log D(x)] + E{z~p(z)}[log(1 − D(G(z)))]; for a fixed generator the best discriminator is D*(x) = p_data(x) / (p_data(x) + p_g(x)), and plugging it in shows the generator is minimizing 2·JS(p_data ‖ p_g) − log 4, a Jensen-Shannon divergence between the two distributions that is minimal exactly when p_g = p_data. That view explains GAN problems (vanishing gradients when the distributions don’t overlap, mode collapse) and their fixes (the Wasserstein distance in WGAN, f-divergence GANs), and contrasts GANs with explicit-density models such as VAEs, normalizing flows and diffusion models.

Read full answer

Applies togeneral concepts; example with NumPy 2.x and SciPy 1.13+

Probabilistic Pieces

Piece Probabilistic meaning
Noise z ~ p(z) Source of randomness
Generator G Pushes p(z) forward to an implicit p_g
Discriminator D(x) Estimated P(real | x), a density-ratio estimator
Optimal D p_data / (p_data + p_g)
Objective at the optimal D 2·JS(p_data ‖ p_g) − log 4
WGAN critic Estimates the Wasserstein-1 distance

Code Example: Optimal Discriminator and the JS Divergence

import numpy as np
from scipy import stats

xs = np.linspace(-10, 10, 20_001)
dx = xs[1] - xs[0]
p_data = stats.norm.pdf(xs, 2.0, 1.0)

def gan_value_at_optimum(mu_g):
    p_g = stats.norm.pdf(xs, mu_g, 1.0)
    d_star = p_data / (p_data + p_g + 1e-300)          # optimal discriminator
    value = np.sum(p_data * np.log(d_star + 1e-300) + p_g * np.log(1 - d_star + 1e-300)) * dx
    m = 0.5 * (p_data + p_g)
    js = 0.5 * np.sum(p_data * np.log(p_data / m + 1e-300)) * dx \
        + 0.5 * np.sum(p_g * np.log(p_g / m + 1e-300)) * dx
    return value, js, d_star[np.argmin(np.abs(xs - 2.0))]

for mu_g in [-4.0, 0.0, 1.5, 2.0]:
    value, js, d_at_2 = gan_value_at_optimum(mu_g)
    print(f"generator mean {mu_g:>4}: V(D*, G) = {value:.4f}, 2*JS - log 4 = {2 * js - np.log(4):.4f}, D*(2) = {d_at_2:.3f}")
# The two columns match; when the generator mean reaches 2, V = -log 4 = -1.3863 and D* = 0.5 everywhere

When the generator is far away (mean −4), JS is almost at its maximum log 2 and barely changes as the generator moves, which is the vanishing-gradient problem the Wasserstein distance avoids.

GANs vs Other Generative Models

Model Density Training signal
GAN Implicit (samples only) Adversarial classifier
VAE Lower bound on likelihood ELBO
Normalizing flow Exact likelihood Change of variables
Diffusion model Likelihood bound via denoising Denoising score matching

Common Pitfalls

  • Mode collapse: the generator covers only some modes of p_data.

  • A discriminator that wins too easily, giving near-zero generator gradients (use the non-saturating loss or WGAN-GP).

  • Evaluating with likelihood, which GANs don’t provide; use FID or precision/recall for distributions.

Interview Tips

  • Derive D* and the JS divergence connection.

  • Explain the implicit density and how WGAN fixes the non-overlap problem.

45

What are the probabilistic foundations of active learning?

Active learning chooses which unlabelled examples to send for labelling so a model learns the most from the fewest labels, and its standard selection rules are probabilistic: uncertainty sampling picks points where the model’s predicted class distribution is least confident (lowest top probability, smallest margin, or highest entropy H = −Σ p log p), query-by-committee picks points where an ensemble or posterior samples disagree most, and Bayesian methods such as BALD choose points with the highest expected information gain about the model parameters, the mutual information I(y; θ | x, D) = H[y | x, D] − E_θ[H[y | x, θ]]. Expected error or variance reduction picks the label that, in expectation over its possible values, most improves the model. These scores rely on calibrated probabilities, and because the selected data is no longer a random sample, evaluation needs a separate random test set; batch selection also needs diversity so the model doesn’t query many near-duplicate points.

Read full answer

Applies togeneral concepts; examples with NumPy 2.x and scikit-learn 1.5+

Query Strategies

Strategy Score Probabilistic idea
Least confidence 1 − max p(y | x) Low top probability
Margin p₁ − p₂ (small is uncertain) Close call between top classes
Entropy −Σ p log p Total predictive uncertainty
Query by committee Vote entropy / KL across models Disagreement between hypotheses
BALD Mutual information between y and θ Epistemic (reducible) uncertainty only
Expected error reduction Expected future loss after labelling Decision-theoretic

Code Example: Entropy Sampling vs Random Sampling

import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=3000, n_features=10, n_informative=5, random_state=42)
X_pool, X_test, y_pool, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

def entropy(p):
    return -(p * np.log(p + 1e-12)).sum(axis=1)

def run(strategy, n_rounds=20, batch=10, seed=42):
    rng = np.random.default_rng(seed)
    labelled = list(rng.choice(len(X_pool), 20, replace=False))
    for _ in range(n_rounds):
        model = LogisticRegression(max_iter=1000).fit(X_pool[labelled], y_pool[labelled])
        unlabelled = np.setdiff1d(np.arange(len(X_pool)), labelled)
        if strategy == "entropy":
            scores = entropy(model.predict_proba(X_pool[unlabelled]))
            picks = unlabelled[np.argsort(scores)[-batch:]]   # most uncertain points
        else:
            picks = rng.choice(unlabelled, batch, replace=False)
        labelled.extend(picks.tolist())
    model = LogisticRegression(max_iter=1000).fit(X_pool[labelled], y_pool[labelled])
    return len(labelled), model.score(X_test, y_test)

for strategy in ["random", "entropy"]:
    n_labels, acc = run(strategy)
    print(f"{strategy:8s}: {n_labels} labels, test accuracy {acc:.3f}")
# Entropy sampling usually reaches equal or higher accuracy with the same 220 labels

p_members = np.array([[0.9, 0.1], [0.1, 0.9]])          # BALD for one point, two posterior samples
mean_p = p_members.mean(axis=0, keepdims=True)
bald = entropy(mean_p)[0] - entropy(p_members).mean()
print(f"BALD score {bald:.3f}")                          # 0.693 - 0.325 = 0.368: members disagree

Hand check: the mean prediction is [0.5, 0.5] with entropy log 2 = 0.693; each member has entropy −(0.9 ln 0.9 + 0.1 ln 0.1) = 0.325; the difference, 0.368, is the information a label would give about which hypothesis is right.

Common Pitfalls

  • Querying noisy or outlier points that are uncertain because they are unlearnable (aleatoric uncertainty); BALD separates this from epistemic uncertainty.

  • Poorly calibrated probabilities that make uncertainty scores meaningless.

  • Selecting a batch of near-identical points; add diversity (clustering, core-sets).

  • Evaluating on the actively selected data instead of a random test set.

Interview Tips

  • Name uncertainty sampling, query-by-committee and BALD with their formulas.

  • Distinguish aleatoric and epistemic uncertainty and mention sampling bias.

Sign in to keep reading

You've opened 2 full answers. Sign in to read all 45 Probability answers, mark what you know and pick up where you left off.

Report a mistake

What's wrong?

All Machine Learning & Data Science subjects