Knowledge / Artificial Intelligence and Data Science / Bayesian statistics

Bayesian statistics

Artificial Intelligence and Data Science (B.E./B.Tech) · Mathematics and statistics for AI · free study notes for exams, placements and interviews

In short

Bayesian statistics treats unknown parameters as random variables with probability distributions: a prior belief is combined with observed data through the likelihood to give a posterior belief, using Bayes' theorem. Unlike the frequentist view where a parameter has one fixed true value and probability describes long-run frequencies, the Bayesian view lets you say "there is a 95% probability that the conversion rate lies between 3.1% and 4.4%" — a statement product teams actually want. It is examined in GATE DA (Data Science and AI), GATE CS, AICTE/CBCS papers on Probability and Statistics and Machine Learning, and in nearly every data-science interview via Bayes' theorem puzzles, naive Bayes and A/B-test questions. Indian employers — Flipkart, Swiggy, Zomato, Razorpay, PhonePe, CRED, Dream11, Fractal, Mu Sigma, Tiger Analytics, ISRO/DRDO labs, ICMR epidemiology groups — use it for spam and fraud filters, recommendation, demand forecasting, clinical trials and sequential experimentation.

Explained

Bayes' theorem and its vocabulary

Priors: types and choice

Conjugate families (the examinable table)

Point estimates, intervals and prediction

Hypothesis comparison and decisions

Computation: when there is no closed form

Bayesian models students must know

Indian standards and regulatory practice

Key points

Formulas and examples

Bayes' theorem (discrete): P(Hᵢ|E) = P(E|Hᵢ)P(Hᵢ) / Σⱼ P(E|Hⱼ)P(Hⱼ) — Hᵢ: hypotheses (mutually exclusive, exhaustive); E: evidence. All terms dimensionless probabilities in [0,1].
Bayes' theorem (continuous): p(θ|D) = p(D|θ)p(θ) / ∫ p(D|θ)p(θ)dθ — θ: parameter; p(·) are densities (units = 1/units of θ).
Odds form: posterior odds = prior odds × likelihood ratio; BF₁₀ = p(D|M₁)/p(D|M₀).
Beta–Binomial: θ ~ Beta(α, β), x | θ ~ Bin(n, θ) ⇒ θ | x ~ Beta(α+x, β+n−x); posterior mean = (α+x)/(α+β+n); mode = (α+x−1)/(α+β+n−2) for α+x>1, β+n−x>1.
Normal–Normal (σ² known): μ|x̄ ~ Normal(μₙ, τₙ²) with 1/τₙ² = 1/τ₀² + n/σ² and μₙ = [μ₀/τ₀² + nx̄/σ²] / [1/τ₀² + n/σ²]. Units of μ, x̄, σ, τ are the units of the measurement.
Gamma–Poisson: λ ~ Gamma(α, β) with rate β, xᵢ ~ Poisson(λ) ⇒ λ | x ~ Gamma(α + Σxᵢ, β + n); posterior mean = (α + Σxᵢ)/(β + n) — units: events per unit time.
MAP: θ̂_MAP = argmax_θ [ log p(D|θ) + log p(θ) ]; Gaussian prior Normal(0, τ²) ⇒ ridge penalty λ‖w‖² with λ = σ²/τ².
Posterior predictive: p(x̃|D) = ∫ p(x̃|θ) p(θ|D) dθ.
Naive Bayes: ĉ = argmax_c [ log P(c) + Σⱼ log P(xⱼ|c) ]; Laplace smoothing P(w|c) = (n_wc + α)/(N_c + αV).
Metropolis–Hastings acceptance: a = min{1, [p(θ*|D) q(θ|θ*)] / [p(θ|D) q(θ*|θ)]} — p(D) cancels.

Example 1 — Medical screening (the classic base-rate question). A disease affects 0.5% of a population. A test has sensitivity 99% and specificity 95%. A randomly screened person tests positive. What is the probability they have the disease?

Example 2 — Beta–Binomial for an app conversion rate. Last quarter's checkout conversion was around 4%, so take a prior Beta(8, 192) (mean 8/200 = 4%, worth 200 pseudo-trials). A new UI gets 30 conversions in 500 sessions.

Example 3 — Normal–Normal update on a measured mean. A sensor's bias is believed to be μ₀ = 2.0 mm with prior SD τ₀ = 1.0 mm. Nine calibration readings average x̄ = 5.0 mm, with known measurement SD σ = 3.0 mm.

Example 4 — Naive Bayes spam filter with Laplace smoothing. Training set: 60 spam, 40 ham. Word "loan" appears 24 times in spam (total 1,200 spam word tokens) and 2 times in ham (total 800 ham tokens). Vocabulary V = 500. Classify a one-word message "loan" (α = 1).

Example 5 — Gamma–Poisson for support-ticket arrivals. A helpdesk historically receives about 20 tickets/hour; encode this as Gamma(α = 40, β = 2) (mean 40/2 = 20, equivalent to 2 hours of past data). Over the next 5 hours, 120 tickets arrive.

AspectFrequentistBayesian
Parameter θFixed unknown constantRandom variable with a distribution
Probability meansLong-run frequencyDegree of belief, updated by data
Prior informationNot formally usedExplicit prior p(θ)
Main outputPoint estimate, CI, p-valueFull posterior distribution
Interval meaning95% of such intervals cover θ95% posterior probability θ is inside
Small samplesRelies on asymptoticsPrior stabilises the estimate
Repeated looks at dataInflates Type I errorNo inflation; sequential updating is natural
ComputationUsually lightMCMC/VI, often heavy
LikelihoodConjugate priorPosteriorPosterior mean
Binomial(n, θ)Beta(α, β)Beta(α+x, β+n−x)(α+x)/(α+β+n)
Poisson(λ)Gamma(α, β)Gamma(α+Σx, β+n)(α+Σx)/(β+n)
Normal, σ² knownNormal(μ₀, τ₀²)Normal(μₙ, τₙ²)precision-weighted average
Normal, μ knownInverse-Gamma(a, b)Inv-Gamma(a+n/2, b+Σ(x−μ)²/2)(b+Σ(x−μ)²/2)/(a+n/2−1)
MultinomialDirichlet(α₁…α_k)Dirichlet(α₁+x₁…α_k+x_k)(αᵢ+xᵢ)/Σ(αⱼ+xⱼ)
Exponential(λ)Gamma(α, β)Gamma(α+n, β+Σx)(α+n)/(β+Σx)
# Beta-Binomial update and a 95% equal-tailed credible interval (not the HDI)
from scipy.stats import beta
a0, b0 = 8, 192            # prior: ~4% conversion, 200 pseudo-trials
x, n   = 30, 500           # observed conversions / sessions
a, b   = a0 + x, b0 + n - x            # Beta(38, 662)
print(a/(a+b), beta.ppf([0.025, 0.975], a, b))
# P(variant B beats A) by Monte Carlo
import numpy as np
A = beta.rvs(8+22, 192+480-22, size=200_000)
B = beta.rvs(a, b, size=200_000)
print("P(B > A) =", (B > A).mean())

Where it is used

Interview questions

Common mistakes

Related topics

Probability distributions · Statistical inference and estimation · Hypothesis testing · Experiment design and A/B testing · Causal inference basics · Optimisation for machine learning

Open in the Knowledge reader →Find jobs →