Bayesian statistics is a fundamentally different approach to probability and inference than the classical (frequentist) methods taught in most introductory courses. Where frequentist statistics asks “What is the probability of observing this data if the null hypothesis is true?”, Bayesian statistics asks “What is the probability the hypothesis is true given the data I’ve observed?” This shift from thinking about data distributions to thinking about parameter distributions has profound implications for how data scientists make decisions under uncertainty.
Bayesian concepts appear throughout modern data science: probabilistic machine learning models, uncertainty quantification in neural networks, Bayesian hyperparameter optimisation (as covered in our Model Evaluation and Hyperparameter Tuning guide), Bayesian A/B testing (discussed in our Data Science Case Study Interview guide), and recommendation systems (see our ML Interview Q&A). This guide gives you the conceptual foundation and practical tools needed for interviews and real-world applications.
Bayes’ Theorem — The Foundation
Bayes’ theorem describes how to update beliefs in light of new evidence. In its simplest form: P(A|B) = P(B|A) × P(A) / P(B). For statistical inference, we rewrite this as:
Posterior ∝ Likelihood × Prior
P(θ|data) ∝ P(data|θ) × P(θ)
Prior P(θ): Your belief about the parameter θ before seeing the data. It encodes domain knowledge or uncertainty. A coin’s probability of heads: if you have no information, a Uniform(0,1) prior says any probability is equally likely. If you know this coin came from a mint and should be fair, a strong prior around 0.5 (e.g., Beta(50, 50)) reflects that knowledge.
Likelihood P(data|θ): The probability of observing the data given a specific parameter value. For coin flips: if θ=0.7 (probability of heads), the likelihood of observing 7 heads in 10 flips is C(10,7) × 0.7⁷ × 0.3³. This is the same quantity used in maximum likelihood estimation (MLE) — MLE finds θ that maximises the likelihood, while Bayesian inference updates the full distribution over θ.
Posterior P(θ|data): Your updated belief about θ after seeing the data. It combines what you knew before (prior) with what the data tells you (likelihood). As you collect more data, the posterior becomes increasingly dominated by the likelihood and less influenced by the prior — with infinite data, any reasonable prior leads to the same posterior. This is Bayesian learning: beliefs are updated continuously as evidence arrives.
The normalising constant P(data): Called the marginal likelihood or evidence. It is the integral of the likelihood over all possible parameter values: P(data) = ∫ P(data|θ) P(θ) dθ. This integral ensures the posterior sums to 1 (a proper probability distribution). It is often intractable for complex models — which is why we use approximate inference methods like MCMC and variational inference.
Conjugate Priors — Analytically Tractable Posteriors
A conjugate prior is a prior distribution that, when combined with a specific likelihood function, produces a posterior of the same distributional family. This makes inference analytically tractable — no MCMC needed. Conjugate priors are computationally convenient and conceptually illuminating.
| Likelihood | Conjugate Prior | Posterior | Application |
|---|---|---|---|
| Binomial (successes in n trials) | Beta(α, β) | Beta(α+successes, β+failures) | Conversion rate, CTR estimation |
| Poisson (count data) | Gamma(α, β) | Gamma(α+sum(counts), β+n) | Event rate estimation |
| Normal (known variance) | Normal(μ₀, σ₀²) | Normal(updated μ, updated σ²) | Revenue, weight estimation |
| Categorical/Multinomial | Dirichlet(α) | Dirichlet(α + counts) | Topic models, text classification |
| Exponential (time between events) | Gamma(α, β) | Gamma(α+n, β+sum(x)) | Inter-arrival times |
Beta-Binomial example (conversion rate): You are estimating the conversion rate θ of a landing page. Prior: Beta(2, 18) — encoding the belief that conversion is probably around 10% (mean = 2/(2+18) = 0.1). You observe 40 conversions in 300 visitors. Posterior: Beta(2+40, 18+260) = Beta(42, 278). Posterior mean = 42/(42+278) = 0.131. The prior has shifted from 10% to 13.1% in light of the data. Crucially, the posterior is a full distribution — you can compute credible intervals, probability of beating a competitor’s rate, and expected future conversions.
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
# Prior: Beta(2, 18) -- conversion rate ~10%
alpha_prior, beta_prior = 2, 18
# Data: 40 conversions in 300 visits
conversions, visits = 40, 300
# Posterior (conjugate update)
alpha_post = alpha_prior + conversions
beta_post = beta_prior + (visits - conversions)
posterior = stats.beta(alpha_post, beta_post)
print(f'Posterior mean: {posterior.mean():.3f}')
print(f'95% Credible Interval: {posterior.interval(0.95)}')
print(f'P(rate > 15%): {1 - posterior.cdf(0.15):.3f}')
x = np.linspace(0, 0.4, 1000)
plt.figure(figsize=(8, 4))
plt.plot(x, stats.beta(alpha_prior, beta_prior).pdf(x), '--', label='Prior')
plt.plot(x, posterior.pdf(x), label='Posterior')
plt.xlabel('Conversion Rate'); plt.ylabel('Density')
plt.title('Bayesian Conversion Rate Estimation')
plt.legend(); plt.tight_layout(); plt.show()
Bayesian vs Frequentist A/B Testing
This is one of the most practically important Bayesian applications in data science, and a common topic in data science case study interviews. Our Statistics Interview Q&A covers frequentist hypothesis testing (p-values, Type I/II errors, power). Here we contrast it with the Bayesian approach.
Frequentist A/B test: Set α=0.05, compute power, determine sample size, run test for predetermined duration, compute p-value, reject or fail to reject H₀. The p-value answers: “If H₀ is true, how likely is data this extreme?” It does NOT tell you the probability that the treatment is better. The test cannot be stopped early (inflates Type I error). The result is binary: significant or not significant.
Bayesian A/B test: Start with priors on conversion rates for control and treatment. As data arrives, update both posteriors. At any time, compute P(treatment > control) by sampling from both posteriors and computing the fraction of samples where treatment wins. Also compute Expected Loss — how much conversion rate you sacrifice by choosing the wrong variant. Stop when P(treatment > control) exceeds a threshold (e.g., 95%) or when Expected Loss falls below a business-acceptable threshold (e.g., 0.1%).
| Dimension | Frequentist | Bayesian |
|---|---|---|
| Question answered | P(data | H₀) | P(treatment wins | data) |
| Early stopping | Not valid (inflates α) | Valid — check any time |
| Multiple comparisons | Bonferroni correction needed | Naturally penalised via priors |
| Interpretability | Requires careful explanation | Intuitive — “95% chance treatment wins” |
| Prior information | Not incorporated | Explicit prior encoding |
| Decision framework | Significance threshold | Expected loss / business value |
MCMC — Markov Chain Monte Carlo
For complex models where the posterior has no closed-form expression (most real-world Bayesian models), we use Markov Chain Monte Carlo (MCMC) to draw samples from the posterior. Instead of computing the posterior analytically, MCMC constructs a Markov chain whose stationary distribution is the target posterior — running the chain long enough produces samples that approximate the posterior.
Metropolis-Hastings algorithm: The fundamental MCMC algorithm. At each step: (1) propose a new parameter value θ’ from a proposal distribution q(θ’|θ). (2) Compute acceptance ratio r = P(data|θ’) P(θ’) / [P(data|θ) P(θ)]. (3) Accept θ’ with probability min(1, r); otherwise keep θ. This ensures the chain visits parameter values proportionally to their posterior probability, without ever computing the normalising constant P(data).
NUTS (No U-Turn Sampler): The modern standard, used by PyMC and Stan. It automatically tunes the step size and path length in Hamiltonian Monte Carlo, producing efficient, low-autocorrelation samples with minimal tuning. For most Bayesian models, PyMC with NUTS is the recommended approach.
import pymc as pm
import numpy as np
# Bayesian linear regression example
# y = alpha + beta*x + epsilon, epsilon ~ Normal(0, sigma)
np.random.seed(42)
x = np.random.randn(100)
true_alpha, true_beta, true_sigma = 2.0, 0.5, 1.0
y = true_alpha + true_beta * x + np.random.normal(0, true_sigma, 100)
with pm.Model() as linear_model:
# Priors
alpha = pm.Normal('alpha', mu=0, sigma=10)
beta = pm.Normal('beta', mu=0, sigma=10)
sigma = pm.HalfNormal('sigma', sigma=5)
# Likelihood
mu = alpha + beta * x
obs = pm.Normal('obs', mu=mu, sigma=sigma, observed=y)
# Inference (NUTS sampler)
trace = pm.sample(2000, tune=1000, return_inferencedata=True,
random_seed=42, progressbar=True)
pm.plot_posterior(trace, var_names=['alpha', 'beta', 'sigma'])
pm.summary(trace)
Bayesian Inference in Machine Learning
Gaussian Processes (GP): A Bayesian non-parametric model that places a prior over functions — instead of learning specific parameters, it maintains a distribution over all possible functions consistent with the data. GPs are used in Bayesian optimisation (our hyperparameter tuning guide covers this), geostatistics, and any regression task where uncertainty quantification is essential. The GP predicts not just a value but a full distribution with a mean (best estimate) and variance (confidence) at each input point.
Naive Bayes classifier: Despite the “naive” independence assumption (features are conditionally independent given the class), Naive Bayes is surprisingly effective for text classification and other high-dimensional sparse problems. It is a Bayesian classifier — it directly estimates P(class|features) via Bayes’ theorem. Gaussian Naive Bayes for continuous features, Multinomial Naive Bayes for word counts, Bernoulli Naive Bayes for binary features.
Bayesian neural networks (BNNs): Instead of learning point estimates of weights, BNNs maintain distributions over weights. This provides principled uncertainty estimates — a BNN can express “I’m 95% confident this is a cat” vs “I’m uncertain — this image is near the decision boundary.” Approximate inference methods: Monte Carlo Dropout (train with dropout, keep it active at test time — multiple forward passes give a distribution of predictions), Deep Ensembles (ensemble of neural networks — simple but effective), Variational Inference (approximate posterior with a simpler distribution).
Interview Questions — Bayesian Statistics
Q: What is a credible interval vs a confidence interval? A 95% Bayesian credible interval (CI) means “given the data, there is a 95% probability the parameter lies in this interval.” This is the intuitive interpretation most people mistakenly apply to frequentist confidence intervals. A frequentist 95% confidence interval means “if we repeat this experiment infinitely, 95% of constructed intervals will contain the true parameter” — the parameter is fixed (not random), so probability statements about it are not directly valid. The distinction matters in practice: credible intervals are narrower and more interpretable for the same data.
Q: What is the prior sensitivity problem in Bayesian inference? If the prior strongly disagrees with the data, the posterior can be misleadingly influenced by the prior, especially with small sample sizes. Solutions: use weakly informative priors (broad enough to not exclude plausible values but regularising enough to provide computational stability), perform prior predictive checks (simulate data from the prior — does it look like plausible data?), and report results with multiple priors to demonstrate robustness.
Q: When would you use Bayesian over frequentist methods? Bayesian: when you have genuine domain knowledge that should be incorporated as a prior; when you need full uncertainty quantification (not just point estimates); when sample sizes are small and the prior regularises effectively; when sequential updating is needed (new data arrives incrementally); when you want to answer “probability that A is better than B” directly. Frequentist: when no informative prior exists and any prior would be arbitrary; when computational cost of MCMC is prohibitive; when regulatory frameworks require p-values (clinical trials, FDA submissions).
For probability distributions that appear in Bayesian priors, see our Probability Distributions guide. For frequentist hypothesis testing fundamentals that Bayesian methods extend, see our Hypothesis Testing guide. Our Statistics Interview Q&A covers 40 questions spanning both frequentist and Bayesian approaches, and our Case Study Interview guide shows how to apply Bayesian reasoning to A/B test design questions.



