Domain Specialization · Local CPULaptop CPULesson 2 of 4 in Phase 11

Phase 11 / Probabilistic modeling

Build a Bayesian regression

Revision v0.18.1 ·

Lesson · about 110 minutes · CPU practice

State the prior, likelihood and observation-noise assumptions Derive posterior precision and check a closed-form special case

Phase study guide: preparation, milestones and project evidence

Before you start

Before this: Distributions and sampling
Hardware: CPU

Setup help: Python, notebook, or Cloud TPU VM

Use the tested CPU packages (the recorded environment uses Python 3.14.3). Other Python versions require separate checks. Hardware receipts apply only to their recorded source, environment, hardware, and cases; they do not qualify every lesson or TPU configuration. See the TPU performance and hardware evidence guide. The optional Cloud TPU specialization explains target-specific execution boundaries. Logical CPU devices practice partitioning; they do not simulate TPU performance.

Download the repository ZIP or use your existing checkout. Extract it and open a terminal in the folder containing requirements-cpu.txt. The beginner ZIP is separate; these lessons use the full repository folder.

macOS / Linux terminal · repository folder
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txt

PowerShell · repository folder
py -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txt

After activation, run the complete worked example from that same folder:

macOS / Linux terminal · activated environment
python phases/11-probability/02-build-a-bayesian-regression/code/main.py

PowerShell command · repository folder
.\.venv\Scripts\python.exe phases/11-probability/02-build-a-bayesian-regression/code/main.py

For an incremental build, create main.py in your own practice folder, append each build block in order, and run python main.py with this environment active. Keep that file separate from the downloaded reference solution.

Check: The posterior mean is approximately (0.9231,1.7778)(0.9231,1.7778). Latent variances at inputs 0,20,2 are approximately 0.3077,2.08550.3077,2.0855; observation variances are each larger by 11.

Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.

Stage 1 of 5 · Understand the MechanismCore mental model, equations & visual walkthrough

What you will be able to do

  • State the prior, likelihood and observation-noise assumptions
  • Derive posterior precision and check a closed-form special case
  • Compute latent and observation predictive variances separately
  • Demonstrate prior strength and extrapolation effects

The problem

You have three noisy measurements and want to predict at a new input. A fitted line gives one answer, but how uncertain should that answer be? We will write a prior, combine it with a likelihood, solve the posterior exactly, and separate uncertainty about the line from noise in a new observation. This exact answer will become our reference when we build an approximate sampler.

The idea

Bayesian regression represents uncertainty about parameters and propagates it to predictions. A latent-function prediction and a future noisy observation are different random quantities, so their intervals generally have different widths.

Separate uncertainty about the function from observation noise

For features xx, posterior parameter covariance Σ\Sigma gives latent predictive variance xTΣxx^T\Sigma x. If independent observation noise has variance σ2\sigma^2, a new observation has variance xTΣx+σ2x^T\Sigma x+\sigma^2. Name each term before combining them.

Moving away from well-observed feature combinations can increase parameter-related uncertainty. It does not require the assumed observation-noise variance to change. Plot the posterior mean with separately labeled latent and observation bands to make this distinction visible.

The saved spread curves show how uncertainty varies with the input. They are conditional on the model, prior and noise assumptions. Check held-out behavior and model adequacy instead of reading any narrow posterior band as a guarantee.

State what is random

The features are fixed inputs. We treat the intercept and slope as uncertain, and condition on the observed targets. The prior scale τ=2\tau=2 expresses uncertainty about each weight before seeing these targets; it is not the sensor noise. The noise scale σ=1\sigma=1 describes repeated readings around the same latent line. We deliberately assume it known. Learning that scale would remove this exact two-parameter shortcut unless we used a richer conjugate model.

w∼N(0,τ2I),y∣w,X∼N(Xw,σ2I)w\sim\mathcal N(0,\tau^2 I),\qquad y\mid w,X\sim\mathcal N(Xw,\sigma^2 I)

Combine information through precision

Precision is inverse covariance: larger precision means less uncertainty. The prior contributes I/τ2I/\tau^2; the observations contribute X⊤X/σ2X^\top X/\sigma^2. The linear term is X⊤y/σ2X^\top y/\sigma^2. Completing the square gives a linear system for the posterior mean. This resembles ridge regression because the posterior mode and mean coincide for this Gaussian model. That equivalence does not turn every regularized optimizer into a complete uncertainty model.

Λ=τ−2I+σ−2X⊤X,Σ=Λ−1,m=Λ−1σ−2X⊤y\Lambda=\tau^{-2}I+\sigma^{-2}X^\top X,\quad \Sigma=\Lambda^{-1},\quad m=\Lambda^{-1}\sigma^{-2}X^\top y

Check the special case by hand

The inputs are −1,0,1-1,0,1, so their sum is zero and X⊤X=diag⁡(3,2)X^\top X=\operatorname{diag}(3,2). The targets give X⊤y=(3,4)X^\top y=(3,4). Adding prior precision 1/41/4 gives diagonal entries 13/413/4 and 9/49/4. Therefore m=(12/13,16/9)m=(12/13,16/9) and Σ=diag⁡(4/13,4/9)\Sigma=\operatorname{diag}(4/13,4/9). These rational numbers provide an independent oracle; comparing two calls to the same posterior function would not.

Two uncertainty bands answer two questions

At a design row x∗x_*, the uncertain latent line value has variance x∗⊤Σx∗x_*^\top\Sigma x_*. A new target adds independent observation variance σ2\sigma^2. At input zero the latent variance is 4/134/13, whereas the observation variance is 17/1317/13. At input two, the slope contribution increases latent variance to about 2.092.09. A nominal Gaussian interval is conditional on this model and known noise scale; it is not proof that future real observations achieve nominal coverage.

E[y∗∣D]=x∗⊤m,Var⁡(y∗∣D)=x∗⊤Σx∗+σ2\mathbb E[y_*\mid D]=x_*^\top m,\qquad \operatorname{Var}(y_*\mid D)=x_*^\top\Sigma x_*+\sigma^2

Use a tiny exact model as an inference test

The next lesson compares approximate samples with this kind of known answer. This is a useful engineering habit: test an inference mechanism on a model you can solve before trusting it on one you cannot. Our design is synthetic and unusually well conditioned. Retain the prior, noise assumption and extrapolation range with your result; a narrow posterior can be confidently wrong when the likelihood is wrong.

Read the figure

The horizontal axis is input value, and the vertical axis is predictive standard deviation in target units. The lower curve is uncertainty in the latent line; the upper curve adds the assumed unit observation-noise variance before taking the square root. At input zero, the executed model gives standard deviations about 4/13=0.555\sqrt{4/13}=0.555 and 17/13=1.144\sqrt{17/13}=1.144. At input two, they are about 1.444 and 1.757 because slope uncertainty grows away from the centered training inputs.

Connect it to the computation

The curves come from the exact Gaussian posterior for fixed inputs −1,0,1-1,0,1, prior scale 2 and known noise scale 1. Variances add before taking the square root; observation noise is not added as a standard deviation. If we changed the assumed noise scale, only the upper predictive variance would receive that additional noise term directly, while refitting would also change the posterior covariance. These are conditional model quantities, not empirical coverage.

Figure data and execution details

Recorded environment: JAX 0.9.2 · 1 CPU device

The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.

Stage 2 of 5 · Build & Run the ReferenceStep-by-step code construction & verified execution output

Your build: Create a separate main.py in your practice folder. Append each block in order and run python main.py in the lesson environment. The downloaded script is the complete reference, so keep it separate from your attempt.

1 Construct a small linear model

Create a fresh main.py. The first feature is an intercept. Targets contain three deliberately simple synthetic observations; the known noise scale is an assumption.

main.py · append build step 1
# Step 1 — 1. Construct a small linear model: The design has three rows and two columns.
import numpy as np
import jax
import jax.numpy as jnp
X = jnp.array([[1.,-1.],[1.,0.],[1.,1.]])
y = jnp.array([-1.,1.,3.])
sigma, prior_scale = 1., 2.

The design has three rows and two columns. The weight vector contains intercept then slope.

Explain the APIs in this step
  • jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.

2 Solve the posterior system

Append the conjugate update. Use a linear solve rather than writing an explicit matrix inverse for the mean.

main.py · append build step 2
# Step 2 — 2. Solve the posterior system
def posterior(X,y,sigma,prior_scale):
    precision = jnp.eye(X.shape[1])/prior_scale**2 + X.T@X/sigma**2
    mean = jnp.linalg.solve(precision,X.T@y/sigma**2)
    covariance = jnp.linalg.solve(precision,jnp.eye(X.shape[1]))
    return mean,covariance
mean,cov = posterior(X,y,sigma,prior_scale)
assert jnp.allclose(mean,jnp.array([12/13,16/9]),atol=1e-6)
assert jnp.allclose(cov,jnp.diag(jnp.array([4/13,4/9])),atol=1e-6)

The independent fractions follow because centered inputs make the precision matrix diagonal.

Explain the APIs in this step
  • jnp.eye(N, M=None, k=0, dtype=...): Creates a square or rectangular identity array: ones lie on diagonal k, and other entries are zero.
  • jnp.allclose(actual, expected, rtol=..., atol=...): Returns a JAX boolean scalar indicating whether corresponding values are within the requested tolerance.
  • jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.

3 Predict both the latent mean and a new observation

Append the prediction calculation, then run python main.py. Retain the two variance components separately.

main.py · append build step 3
# Step 3 — 3. Predict both the latent mean and a new observation
def predictive(design,mean,cov,sigma):
    center = design@mean
    latent_variance = jnp.einsum('ni,ij,nj->n',design,cov,design)
    return center,latent_variance,latent_variance+sigma**2
query = jnp.array([[1.,0.],[1.,2.]])
center,latent_var,observation_var = predictive(query,mean,cov,sigma)
assert jnp.allclose(latent_var,jnp.array([4/13,4/13+16/9]),atol=1e-6)
assert jnp.allclose(observation_var-latent_var,1.)
print('posterior mean:',mean,'covariance:',cov)
print('latent variance:',latent_var,'observation variance:',observation_var)

The variance at the extrapolation input is larger because slope uncertainty contributes more strongly there.

Explain the APIs in this step
  • jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.
  • jnp.allclose(actual, expected, rtol=..., atol=...): Returns a JAX boolean scalar indicating whether corresponding values are within the requested tolerance.
View the cumulative build in main.py

These blocks append in order to your separate practice file.

main.py · cumulative Python build
# Step 1 — 1. Construct a small linear model: The design has three rows and two columns.
import numpy as np
import jax
import jax.numpy as jnp
X = jnp.array([[1.,-1.],[1.,0.],[1.,1.]])
y = jnp.array([-1.,1.,3.])
sigma, prior_scale = 1., 2.

# Step 2 — 2. Solve the posterior system
def posterior(X,y,sigma,prior_scale):
    precision = jnp.eye(X.shape[1])/prior_scale**2 + X.T@X/sigma**2
    mean = jnp.linalg.solve(precision,X.T@y/sigma**2)
    covariance = jnp.linalg.solve(precision,jnp.eye(X.shape[1]))
    return mean,covariance
mean,cov = posterior(X,y,sigma,prior_scale)
assert jnp.allclose(mean,jnp.array([12/13,16/9]),atol=1e-6)
assert jnp.allclose(cov,jnp.diag(jnp.array([4/13,4/9])),atol=1e-6)

# Step 3 — 3. Predict both the latent mean and a new observation
def predictive(design,mean,cov,sigma):
    center = design@mean
    latent_variance = jnp.einsum('ni,ij,nj->n',design,cov,design)
    return center,latent_variance,latent_variance+sigma**2
query = jnp.array([[1.,0.],[1.,2.]])
center,latent_var,observation_var = predictive(query,mean,cov,sigma)
assert jnp.allclose(latent_var,jnp.array([4/13,4/13+16/9]),atol=1e-6)
assert jnp.allclose(observation_var-latent_var,1.)
print('posterior mean:',mean,'covariance:',cov)
print('latent variance:',latent_var,'observation variance:',observation_var)

Run the example

python3 phases/11-probability/02-build-a-bayesian-regression/code/main.py
About this command

This shortcut uses an activated macOS/Linux course environment in the repository folder. For Windows, use the explicit interpreter command in “Setup help” above. The script includes worked examples and reference solutions.

Inspect the complete assembled example
main.py · complete Python example
# Step 1 — 1. Construct a small linear model: The design has three rows and two columns.
import numpy as np
import jax
import jax.numpy as jnp
X = jnp.array([[1.,-1.],[1.,0.],[1.,1.]])
y = jnp.array([-1.,1.,3.])
sigma, prior_scale = 1., 2.

# Step 2 — 2. Solve the posterior system
def posterior(X,y,sigma,prior_scale):
    precision = jnp.eye(X.shape[1])/prior_scale**2 + X.T@X/sigma**2
    mean = jnp.linalg.solve(precision,X.T@y/sigma**2)
    covariance = jnp.linalg.solve(precision,jnp.eye(X.shape[1]))
    return mean,covariance
mean,cov = posterior(X,y,sigma,prior_scale)
assert jnp.allclose(mean,jnp.array([12/13,16/9]),atol=1e-6)
assert jnp.allclose(cov,jnp.diag(jnp.array([4/13,4/9])),atol=1e-6)

# Step 3 — 3. Predict both the latent mean and a new observation
def predictive(design,mean,cov,sigma):
    center = design@mean
    latent_variance = jnp.einsum('ni,ij,nj->n',design,cov,design)
    return center,latent_variance,latent_variance+sigma**2
query = jnp.array([[1.,0.],[1.,2.]])
center,latent_var,observation_var = predictive(query,mean,cov,sigma)
assert jnp.allclose(latent_var,jnp.array([4/13,4/13+16/9]),atol=1e-6)
assert jnp.allclose(observation_var-latent_var,1.)
print('posterior mean:',mean,'covariance:',cov)
print('latent variance:',latent_var,'observation variance:',observation_var)

Expected result: The posterior mean is approximately (0.9231,1.7778)(0.9231,1.7778). Latent variances at inputs 0,20,2 are approximately 0.3077,2.08550.3077,2.0855; observation variances are each larger by 11.

Recorded output and runtime details

Reference output captured from executing the complete lesson script (code/main.py) and its assertions in the course CPU environment.

2026-10-27T17:07:04.106433+00:00 · Python 3.14.3 · JAX 0.9.2 · 1 CPU device

posterior mean: [0.923077  1.7777778] covariance: [[0.30769232 0.        ]
 [0.         0.44444445]]
latent variance: [0.30769232 2.0854702 ] observation variance: [1.3076923 3.0854702]
posterior mean: [0.923077  1.7777778] covariance: [[0.30769232 0.        ]
 [0.         0.44444445]]
latent variance: [0.30769232 2.0854702 ] observation variance: [1.3076923 3.0854702]
[0.16       0.23529412]
PASS: probability-02

Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.

Stage 3 of 5 · Controlled ExperimentsPredict before running & test boundary conditions

Repeat independent observations

Repeat independent observations · Python experiment
# Experiment — Repeat independent observations: The calculation treats rows as independent new evidence.
repeat_mean,repeat_cov = posterior(jnp.tile(X,(2,1)),jnp.tile(y,2),sigma,prior_scale)
assert jnp.all(jnp.diag(repeat_cov)<jnp.diag(cov))
print(jnp.diag(repeat_cov))

Expected: Both posterior marginal variances decrease.

The calculation treats rows as independent new evidence. Duplicating a stored file does not create new evidence in a real dataset.

Check the prior-only boundary

Check the prior-only boundary · Python experiment
# Experiment — Check the prior-only boundary
empty_mean,empty_cov = posterior(jnp.empty((0,2)),jnp.empty((0,)),sigma,prior_scale)
assert jnp.allclose(empty_mean,0.)
assert jnp.allclose(empty_cov,4*jnp.eye(2))

Expected: The mean is zero and covariance is 4I4I.

The posterior recovers the prior when no likelihood information is present.

Stage 4 of 5 · Hands-On Exercise & CheckpointModify the code yourself, solve transfer tasks & test your understanding
Hands-On Exercise · Modify & Verify Run in the declared lesson environment

Make it yours

Where to put your code: Modify your own main.py, or use the Make it yours cell in your downloaded notebook. Run your attempt in the declared environment and verify your prediction before opening the reference solution.

Strengthen the prior by changing its scale from two to one half. Predict the direction of change in the weight norm and check it. Explain why a tighter posterior need not mean a better model.

Show a writing guide and starter scaffold

How to write this exercise code

Learner-only inputs, outputs, checks and hints

Inputs

  • A design matrix with an explicit intercept column, outcomes, known noise scale, and two prior scales.

Outputs

  • Posterior mean and covariance for both prior choices.

Invariants to check

  • The covariance is symmetric positive definite.
  • The stronger zero-centered prior cannot increase the posterior mean norm in this fixed fixture.

Hints

  • Build posterior precision from prior and likelihood precision.
  • Solve linear systems instead of explicitly inverting a matrix.
  • Interpret the changed posterior separately from predictive model quality.
Your attempt · complete the TODOs
def compare_prior_strength(X, y, noise_scale, prior_scales):
    """Return posterior moments under two prior scales."""
    # TODO: solve both Gaussian precision systems and compare their means.
    raise NotImplementedError
Reveal the reference solution
Reference solution
# Exercise solution: Strengthen the prior by changing its scale from two to one half.
strong_mean,strong_cov = posterior(X,y,sigma,.5)
assert jnp.linalg.norm(strong_mean)<jnp.linalg.norm(mean)
assert jnp.all(jnp.diag(strong_cov)<jnp.diag(cov))

Move away from centered inputs

Transfer / diagnosis

Use design rows (1,0),(1,1),(1,2)(1,0),(1,1),(1,2). Compare the JAX result with a host NumPy linear solve and show the posterior covariance is not diagonal.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • The requested design rows, outcomes, known noise scale, and prior scale.

Outputs

  • The JAX posterior mean/covariance, host NumPy solve results, and an off-diagonal covariance check.

Invariants to check

  • The two solves use the same precision system; the shifted design yields a full posterior covariance with a nonzero off-diagonal entry.

Hints

  • Construct the precision matrix in float64 on the host for an independent solve.
  • Check the covariance off-diagonal directly instead of reducing it to a diagonal summary.
Your attempt · complete the TODOs
import numpy as np

def compare_posterior_with_host_solve(posterior, outcomes, noise_scale, prior_scale):
    # TODO: Solve the requested design case with JAX and independent host NumPy arithmetic.
    raise NotImplementedError
Show a hint

The intercept and slope can compensate for each other when features are not centered.

Reveal reference solution and reasoning
Move away from centered inputs · Python solution
# Move away from centered inputs (Transfer / diagnosis)
shift_X = jnp.array([[1.,0.],[1.,1.],[1.,2.]])
shift_m,shift_c = posterior(shift_X,y,sigma,prior_scale)
# Transfer the device result to the host for an independent NumPy comparison.
host_X=np.asarray(shift_X,dtype=np.float64)
host_P=np.eye(2)/4+host_X.T@host_X
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_allclose(shift_m,np.linalg.solve(host_P,host_X.T@np.asarray(y)),rtol=1e-5,atol=1e-6)
assert shift_c[0,1]<0

Nonzero covariance affects prediction uncertainty; keeping only marginal variances would lose the compensating relationship.

Verify total predictive variance

Transfer / diagnosis

Draw posterior weights and independent observation noise. Compare empirical prediction variance at input zero to the analytic observation variance.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Posterior weight mean/covariance, observation noise scale, an explicit random key, and the requested sample count.

Outputs

  • Empirical prediction variance from sampled weights plus observation noise and analytic observation variance at input zero.

Invariants to check

  • The sampled observation variance is compared with the analytic predictive variance, including the noise contribution.

Hints

  • Draw parameter samples from the full posterior covariance.
  • Use a separate subkey for observation noise and evaluate the prediction at input zero.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp

def compare_sampled_and_analytic_observation_variance(mean, covariance, noise_scale, key, sample_count):
    # TODO: Sample posterior weights and independent observation noise, then compare variances at x=0.
    raise NotImplementedError
Show a hint

Use separate keys for weights and noise; sample with a Cholesky factor.

Reveal reference solution and reasoning
Verify total predictive variance · Python solution
# Verify total predictive variance (Transfer / diagnosis)
# Create or split explicit PRNG key(s) (`(kw, kn)`) for reproducible randomness.
kw,kn=jax.random.split(jax.random.key(37))
# Sample deterministic random values into `weights` using an explicit PRNG key.
weights=mean+jax.random.normal(kw,(30000,2))@jnp.linalg.cholesky(cov).T
# Sample deterministic random values into `replicated` using an explicit PRNG key.
replicated=weights[:,0]+sigma*jax.random.normal(kn,(30000,))
assert abs(float(replicated.var())-float(observation_var[0]))<.07

A simulation verifies that the independent noise term belongs in predictions for observed targets.

Check your understanding

Which variance describes a new noisy reading at a given input?

Choose one answer

Diagnose the result

If an interval becomes implausibly narrow, print the latent and noise variance terms separately. If the slope is unexpectedly shrunk, inspect whether the prior scale was confused with prior precision. If targets have an extra column axis, reject the shape before a matrix operation silently changes the mean shape.

Stage 5 of 5 · Keep Your EvidenceRecord your verification receipt & carry takeaways forward

Carry forward

  • Posterior precision combines prior information with likelihood information.
  • Latent uncertainty and future observation noise are separate quantities.
  • An exact small model supplies an independent reference for approximate inference.

Keep your evidence

Download evidence template

Precision derivation, exact fractional reference, host solve and predictive variance plot. Keep the environment, observed outputs and your explanation of the figure.

Save your modified code, its output, and an explanation of your prediction. Checkpoint answers and exercise evidence are tracked separately.

Saved in this browser. Export or back up your portfolio anytime in My learning.

Save work for this lesson

Reference execution: CPU validation only. This worked reference is validated on CPU. Accelerator compatibility requires a separate source review and target execution receipt. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.

Primary references