Skip to main content

Uncertainty

Building and checking models
Method or technique
Advanced
It greases the wheels of commerce.
Published

November 9, 2026

Modified

November 13, 2026

Entry #134 in Andrew Gelman’s Handy Statistical Lexicon. Original discussion: Hierarchical model golf putting struggle.

Filed under Building and checking models · Method or technique · Advanced.

Related terms: golf putting model, model error term, Stan sampling problems.

Source check: ✓ The explanation was checked against the source.

What it is

The phrase comes from Gelman’s work on a model of golf putting. The data were success rates of putts at different distances from professional tournaments, and the first few distance bins had enormous sample sizes. A binomial model therefore tried to fit those points almost exactly. That pinned down the parameters so tightly that Stan’s sampler couldn’t move through the posterior, and the chains mixed badly. The fix was to add an error term to the model, a little extra uncertainty for modeling error. In Gelman’s words, he “needed to add an error term to grease the wheels of commerce”, and it worked.

Why it matters

Real models are never exactly right. When data are very precise, a slightly wrong model is forced to fit the most precise points as though it were exact. The result is a posterior squeezed into a thin, awkward region that samplers struggle to explore (compare the folk theorem). Admitting some model error makes the computation work. It also makes the inference more honest, because the posterior no longer pretends the model is perfect at the high-precision points.

How it works

Suppose observation \(j\) is modeled as \(y_j \sim \mathrm{N}(f(x_j; \theta), \text{se}_j)\), with tiny \(\text{se}_j\) for the big bins. If \(f\) is slightly misspecified, the likelihood is dominated by those bins and the posterior for \(\theta\) is a narrow ridge. Adding an error term \(\tau\),

\[ y_j \sim \mathrm{N}\big(f(x_j; \theta),\ \sqrt{\text{se}_j^2 + \tau^2}\big), \]

widens the likelihood just enough that no single bin dominates. The sampler can move again, and the fit balances across all the data. In the golf case Gelman notes the independent error term was a “hack” that he later tried to improve, but it did its job.

Simulated example

A straight-line model is fit to data that curve slightly, where the first bins are extremely precise, much like the golf data. Two Metropolis chains sample the slope. Slide the added error term τ up from zero and watch the chains loosen up and the fit change.

Show simulation code

The R version uses base R, and the Python version uses NumPy and Matplotlib. Where the simulation draws random numbers, each language draws its own, so exact numbers differ slightly but show the same pattern. The numbered markers in the R code are explained under it.

import {rng, range, mean, fmt, pct} from "../js/stats.js"

viewof tau = Inputs.range([0, 0.08], {value: 0, step: 0.005, label: "Added error term τ"})
viewof seed = Inputs.range([1, 500], {value: 134, step: 1, label: "Random seed"})

grease = {
  const r = rng(seed);
  const xs = range(19).map(j => 2 + j), se = xs.map((x, j) => 0.003 * (1 + j / 3));
  const y = xs.map((x, j) => 2 - 0.15 * x + 0.004 * x * x + r.normal(0, se[j]));
  const lp = (a, b) => { let s = 0; for (let j = 0; j < 19; j++) s += -0.5 * (y[j] - a - b * xs[j]) ** 2 / (se[j] ** 2 + tau ** 2); return s; };
  const run = (a, b, chain) => {
    let l = lp(a, b), acc = 0;
    const out = [];
    for (let t = 0; t < 3000; t++) {
      const a2 = a + r.normal(0, 0.02), b2 = b + r.normal(0, 0.002), l2 = lp(a2, b2);
      if (Math.log(r()) < l2 - l) { a = a2; b = b2; l = l2; acc++; }
      out.push({t, a, b, chain});
    }
    return {out, acc: acc / 3000};
  };
  const c1 = run(1.5, -0.1, "Chain 1"), c2 = run(2.5, -0.05, "Chain 2");
  const draws = [...c1.out, ...c2.out].filter(d => d.t >= 1500);
  return {xs, y, se, trace: [...c1.out, ...c2.out], acc: (c1.acc + c2.acc) / 2, aHat: mean(draws.map(d => d.a)), bHat: mean(draws.map(d => d.b))};
}

Plot.plot({
  ariaLabel: "Uncertainty: chart 1 of 2",
  ariaDescription: "Horizontal axis: Iteration. Vertical axis: Draws of the slope. The chart updates with the controls above it, and the summary below the charts states the result in words.",
  height: 240,
  x: {label: "Iteration"},
  y: {label: "Draws of the slope", grid: true},
  color: {legend: true, range: ["#2166ac", "#d6604d"]},
  marks: [Plot.line(grease.trace, {x: "t", y: "b", stroke: "chain", strokeWidth: 0.8})]
})

Plot.plot({
  ariaLabel: "Uncertainty: chart 2 of 2",
  ariaDescription: "Horizontal axis: Distance bin. Vertical axis: Outcome. The chart updates with the controls above it, and the summary below the charts states the result in words.",
  height: 240,
  x: {label: "Distance bin", grid: true},
  y: {label: "Outcome", grid: true},
  marks: [
    Plot.ruleX(grease.xs.map((x, j) => ({x, lo: grease.y[j] - 2 * grease.se[j], hi: grease.y[j] + 2 * grease.se[j]})), {x: "x", y1: "lo", y2: "hi", stroke: "#8a8a8a"}),
    Plot.dot(grease.xs.map((x, j) => ({x, y: grease.y[j]})), {x: "x", y: "y", fill: "currentColor", r: 3}),
    Plot.line([2, 20].map(x => ({x, y: grease.aHat + grease.bHat * x})), {x: "x", y: "y", stroke: "#d6604d", strokeWidth: 2})
  ]
})
# Uncertainty: it greases the wheels of commerce. An added error term
# lets a Metropolis sampler move.
1tau <- 0 # added error term; try 0.03
2set.seed(134)

x <- 2:20
3se <- 0.003 * (1 + (0:18) / 3) # very precise data
y <- 2 - 0.15 * x + 0.004 * x^2 + rnorm(19, 0, se) # slightly curved
4lp <- function(a, b) -0.5 * sum((y - a - b * x)^2 / (se^2 + tau^2))

5run <- function(a, b) { # random-walk Metropolis
  l <- lp(a, b)
  acc <- 0
  draws <- matrix(NA, 3000, 2)
  for (t in 1:3000) {
    a2 <- a + rnorm(1, 0, 0.02)
    b2 <- b + rnorm(1, 0, 0.002)
    l2 <- lp(a2, b2)
    if (log(runif(1)) < l2 - l) {
      a <- a2
      b <- b2
      l <- l2
      acc <- acc + 1
    }
    draws[t, ] <- c(a, b)
  }
  list(draws = draws, acc = acc / 3000)
}
c1 <- run(1.5, -0.1)
c2 <- run(2.5, -0.05)
b_hat <- mean(c(c1$draws[1501:3000, 2], c2$draws[1501:3000, 2]))
cat("With tau = ", tau, ", the sampler accepts ",
  round(100 * (c1$acc + c2$acc) / 2, 1), "% of proposals; fitted ",
  "slope ", round(b_hat, 4), "\n",
  sep = ""
)

6matplot(1:3000, cbind(c1$draws[, 2], c2$draws[, 2]),
  type = "l", lty = 1,
  col = c("#2166ac", "#d6604d"), xlab = "Iteration",
  ylab = "Slope b"
)
1
The page’s control: the added error term. With zero, the model claims to be exact.
2
Fixes the random numbers so the run is reproducible.
3
Very precise data, as in the golf-putting bins with huge counts.
4
The log posterior of a straight line fitted to slightly curved data, with the error term added to each bin’s variance. That is the grease.
5
A random-walk Metropolis sampler.
6
Two chains’ draws of the slope. With no error term they crawl; with a little uncertainty they mix, and the line balances all the bins.
# Uncertainty: it greases the wheels of commerce. An added error term
# lets a Metropolis sampler move.
import numpy as np
import matplotlib.pyplot as plt

tau = 0  # added error term; try 0.03
rng = np.random.default_rng(134)

x = np.arange(2, 21)
se = 0.003 * (1 + np.arange(19) / 3)     # very precise data
y = 2 - 0.15 * x + 0.004 * x ** 2 + rng.normal(0, se)  # slightly curved


def lp(a, b):
    return -0.5 * np.sum((y - a - b * x) ** 2 / (se ** 2 + tau ** 2))


def run(a, b):  # random-walk Metropolis
    l, acc, draws = lp(a, b), 0, []
    for _ in range(3000):
        a2, b2 = a + rng.normal(0, 0.02), b + rng.normal(0, 0.002)
        l2 = lp(a2, b2)
        if np.log(rng.uniform()) < l2 - l:
            a, b, l, acc = a2, b2, l2, acc + 1
        draws.append(b)
    return np.array(draws), acc / 3000


(b1, acc1), (b2, acc2) = run(1.5, -0.1), run(2.5, -0.05)
b_hat = np.mean(np.concatenate([b1[1500:], b2[1500:]]))
print(f"With tau = {tau}, the sampler accepts "
      f"{100 * (acc1 + acc2) / 2:.1f}% of proposals; fitted slope "
      f"{b_hat:.4f}")

plt.plot(b1, color="#2166ac")
plt.plot(b2, color="#d6604d")
plt.xlabel("Iteration")
plt.ylabel("Slope b")
plt.show()

Key takeaways

  • Very precise data plus a slightly wrong model can jam the computation.
  • Adding an error term for model misfit often fixes both the sampling and the fit.
  • Treat it as a stopgap: the next step is to work out what the model is missing.

Source

Back to top