Interactive Python · Machine learning

Bayesian Linear Regression, the game

Instead of one best line, get a whole distribution of plausible lines. Watch your beliefs update live as data arrives, then fit it in Python with PyMC.

~14 minIntermediatePython · PyMC5 missions260 XP
Mission 1+50 XP✓ Complete

Two schools of statistics

FrequentistBayesian
ParametersFixed, unknown numbersRandom variables with distributions
Prior knowledgeNot usedBuilt in through the prior
ResultA point estimate (plus a confidence interval)A full posterior distribution
UncertaintyConfidence intervalCredible interval

Bayesian methods shine with small datasets, when you have expert knowledge to use, and when you need honest uncertainty for decisions (finance, medicine, risk).

⚡Bayesian or frequentist?
1 / 5
correct on the first try
Mission 2+30 XP✓ Complete

Bayes’ theorem

P(θ | D) = P(D | θ) · P(θ) / P(D)posterior = likelihood × prior / evidence

Flip each card to see what the pieces mean for a regression model with parameters θ (the coefficients) and data D.

🃏Decode the formula
0 / 4
Mission 3+60 XP✓ Complete

Update your beliefs

We want the slope β₁ of a line. The true value is 0.8. Set a prior (your guess and how sure you are), then add data and watch the posterior move and narrow.

The data points are simulated from y = 1.8 + 0.8x + noise (σ = 1.5). The intercept is treated as known so you can focus on the slope.

🔄Prior → posterior
+60 XP
–data points seen
–posterior mean of β₁
–95% credible interval
prior (dashed)posteriortrue slope 0.8
  • ✓Add at least 15 data points and watch the posterior narrow+30 XP
  • ✓Make a stubborn prior: spread ≤ 0.15 and a guess at least 1 away from 0.8, then add 15+ points+30 XP
Mission 4+30 XP✓ Complete

Code it in Python

The original article applies PyMC3 to portfolio weights. Here is a direct Bayesian linear regression with the current pymc package (PyMC3 is no longer maintained).

Step 1Make some data

import numpy as np
import pymc as pm
import arviz as az

rng = np.random.default_rng(42)
x = rng.uniform(0, 10, 50)
y = 1.8 + 0.8 * x + rng.normal(0, 1.5, 50)

Step 2Priors, likelihood, sample

with pm.Model() as model:
    b0 = pm.Normal("b0", mu=0, sigma=10)       # priors
    b1 = pm.Normal("b1", mu=0, sigma=10)
    sigma = pm.HalfNormal("sigma", sigma=5)
    pm.Normal("y", mu=b0 + b1 * x, sigma=sigma, observed=y)   # likelihood
    idata = pm.sample(2000, tune=1000, random_seed=42)

print(az.summary(idata, var_names=["b0", "b1", "sigma"]))

The summary shows each coefficient’s posterior mean and a 94% highest-density interval.

Step 3Quick version with scikit-learn

from sklearn.linear_model import BayesianRidge

model = BayesianRidge().fit(x.reshape(-1, 1), y)
mean, std = model.predict([[5.0]], return_std=True)   # prediction + uncertainty
🧠Quick check
+30 XP

In the PyMC model, which line is the likelihood?

Final boss+90 XP✓ Defeated

Defeat the Bayes Boss 🐲

Six questions, three hearts.

Question 1 / 6
❤️❤️❤️

💥 The boss winsReview the missions above, then try again.

You’re a Bayes Master!

Share on LinkedIn
What’s next

Keep levelling up

You learned how the Bayesian and frequentist views differ, the four parts of Bayes’ theorem, how a prior turns into a posterior as data arrives, and how to fit a Bayesian linear regression with PyMC.

Lv 1 Rookie0 / 260 XP