Why This Matters
Classical (frequentist) statistics asks "how surprising is this data if the null hypothesis is true?" Bayesian inference asks a more natural question: "given this data, what should I believe?" Instead of p-values and confidence intervals, Bayesian statistics produces posterior distributions — full probability distributions over parameters that directly tell you how likely each value is. This is a fundamentally different and increasingly dominant approach to data analysis.
The Bayesian framework has three components: a prior distribution encoding your beliefs before seeing data, a likelihood function describing how probable the data is for each parameter value, and the posterior distribution that combines both. When the prior and likelihood have compatible mathematical forms (called conjugate priors), the posterior has a known closed-form solution, making updates fast and elegant. From spam filtering to recommendation engines to modern AI, Bayesian reasoning is everywhere.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Bayesian inference: combine prior beliefs with observed data via the likelihood to produce an updated posterior distribution.
Code Example
// Bayesian updating with Beta-Binomial conjugate pair
// Prior: Beta(a, b), Likelihood: Binomial
// Posterior: Beta(a + heads, b + tails)
function betaPosterior(priorA, priorB, heads, tails) {
return {
a: priorA + heads,
b: priorB + tails
};
}
// Beta distribution mean: a / (a + b)
function betaMean(a, b) {
return a / (a + b);
}
// Beta distribution variance: ab / ((a+b)^2 * (a+b+1))
function betaVariance(a, b) {
return (a * b) / ((a + b) ** 2 * (a + b + 1));
}
// Start with uniform prior: Beta(1, 1) = "I know nothing"
let a = 1, b = 1;
console.log(`Prior: Beta(${a}, ${b})`);
console.log(`Prior mean: ${betaMean(a, b).toFixed(4)}`);
// Observe 7 heads out of 10 flips
let post = betaPosterior(a, b, 7, 3);
console.log(`\nAfter 7H, 3T: Beta(${post.a}, ${post.b})`);
console.log(`Posterior mean: ${betaMean(post.a, post.b).toFixed(4)}`);
console.log(`Posterior variance: ${betaVariance(post.a, post.b).toFixed(4)}`);
// Sequential update: observe 6 more heads out of 10
let post2 = betaPosterior(post.a, post.b, 6, 4);
console.log(`\nAfter 6 more H, 4 more T: Beta(${post2.a}, ${post2.b})`);
console.log(`Posterior mean: ${betaMean(post2.a, post2.b).toFixed(4)}`);
console.log(`Posterior variance: ${betaVariance(post2.a, post2.b).toFixed(4)}`);
// Strong prior: Beta(100, 100) = "I am very sure p is near 0.5"
let strongPost = betaPosterior(100, 100, 7, 3);
console.log(`\nStrong prior + 7H,3T: Beta(${strongPost.a}, ${strongPost.b})`);
console.log(`Posterior mean: ${betaMean(strongPost.a, strongPost.b).toFixed(4)}`);
console.log("Strong prior barely moves!");Interactive Experiment
Try these exercises:
- Start with Beta(1,1) and update with 1 head, 0 tails. Then update with 10 heads, 0 tails. How does the posterior mean change?
- Compare updating Beta(1,1) with 70 heads and 30 tails all at once versus updating 10 times with 7 heads and 3 tails each time. Are the results the same?
- Try a strong skeptical prior Beta(50, 50) and observe 20 heads in 20 flips. Does the posterior shift much? How many observations would it take to overwhelm this prior?
- Compute the 95% credible interval for the posterior Beta(8, 4). The 2.5th and 97.5th percentiles approximate this.
- What happens as you observe more and more data? Does the prior choice matter less? Verify that Beta(1,1) and Beta(10,10) priors converge after 1000 observations.
Quick Quiz
Coding Challenge
Write a function `updateBeta(priorA, priorB, heads, tails)` that returns the posterior mean of a Beta distribution after observing `heads` heads and `tails` tails. The posterior is Beta(priorA + heads, priorB + tails), and its mean is a / (a + b). Round the result to 4 decimal places.
Real-World Usage
Bayesian statistics powers many modern systems:
- Spam filtering: The original spam filter by Paul Graham used Naive Bayes, computing P(spam|words) by combining prior spam rates with word likelihoods. This is Bayesian inference in action.
- Recommendation systems: Netflix and Spotify use Bayesian methods to estimate user preferences from sparse rating data. Priors help handle new users with few interactions (the cold start problem).
- A/B testing: Bayesian A/B testing provides posterior probabilities like "there is a 95% chance version B is better." This is more intuitive than frequentist p-values for business decisions.
- Clinical trials: Adaptive clinical trials use Bayesian updating to adjust treatment assignments as data accumulates, potentially saving lives by stopping ineffective treatments early.
- Natural language processing: Language models use Bayesian principles. The prior probability of a word sequence combined with the likelihood of observed text gives the posterior for next-word prediction.