Skip to main content

Until it runs twice

Building and checking models
Law or aphorism
Advanced
I don’t trust anything until it runs. In fact, I don’t trust anything until . . .
Published

November 9, 2026

Modified

November 13, 2026

Entry #129 in Andrew Gelman’s Handy Statistical Lexicon. Original discussion: Where does this quote come from? “I don’t trust anything until it runs. In fact, I don’t trust anything until it runs twice.”

Filed under Building and checking models · Law or aphorism · Advanced.

Related terms: R-hat, multimodal posterior, multiple chains, reproducibility.

Source check: ✓ The explanation was checked against the source. The post is about where the quote comes from; the reading of running twice is ours.

What it is

Gelman found this saying attributed to him online and couldn’t remember saying it, so he asked his readers. A commenter traced it to his own talk “Theoretical Statistics is the Theory of Applied Statistics: How to Think About What We Do” at the 2017 New York R Conference: “Computer programing is basically the last bastion of rigor in the modern world. I don’t trust anything until it runs. In fact, until it runs twice on the computer.”

The post doesn’t explain the second run. Our reading: code that runs once might have worked by luck: a lucky random seed, an initial value that happened to fall in the right place, a cached object from an earlier session. Running it a second time, from a fresh start, a different seed or a different initialization, tests whether the result is a property of the problem or of that particular run.

Why it matters

In statistical computing this matters a great deal. MCMC with a single chain can’t tell you whether it explored the whole posterior. An optimizer started once can’t tell you whether it found the global optimum. A notebook run once can’t tell you whether it depends on hidden state. Running more than once, with multiple chains, multiple starting points or a clean environment, is the cheapest diagnostic there is.

How it works

Take a posterior with two separated modes. A sampler started near one mode may stay there for the whole run and report a confident, unimodal answer. A second chain, started elsewhere, lands in the other mode. The disagreement between runs is the diagnostic: R-hat across chains turns “runs twice” into a number.

Simulated example

A Metropolis sampler on a bimodal posterior. Run it once and then twice, from different random starting points, and compare.

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, dnorm, fmt} from "../js/stats.js"

viewof gap = Inputs.range([1, 8], {value: 5, step: 0.25, label: "Distance between the two modes"})
viewof step = Inputs.range([0.1, 3], {value: 0.5, step: 0.1, label: "Proposal step size"})
viewof seed = Inputs.range([1, 500], {value: 1, step: 1, label: "Random seed"})

twice = {
  const r = rng(seed);
  const logp = x => Math.log(0.5 * dnorm(x, -gap / 2, 0.7) + 0.5 * dnorm(x, gap / 2, 0.7) + 1e-300);
  const run = (start, label) => {
    let x = start, lp = logp(x);
    return range(2000).map(t => {
      const y = x + r.normal(0, step), ly = logp(y);
      if (Math.log(r()) < ly - lp) { x = y; lp = ly; }
      return {t, x, label};
    });
  };
  return [...run(r.uniform(-gap, gap), "Run 1"), ...run(r.uniform(-gap, gap), "Run 2")];
}

Plot.plot({
  ariaLabel: "Until it runs twice: simulation chart",
  ariaDescription: "Horizontal axis: Iteration. Vertical axis: Draw. The chart updates with the controls above it, and the summary below the charts states the result in words.",
  height: 280,
  x: {label: "Iteration"},
  y: {label: "Draw", grid: true},
  color: {legend: true, range: ["#2166ac", "#d6604d"]},
  marks: [Plot.line(twice, {x: "t", y: "x", stroke: "label", strokeWidth: 0.7})]
})
# Until it runs twice: two runs of a sampler expose a stuck chain.
1gap <- 5 # distance between the two modes
step <- 0.5 # proposal step size
2set.seed(1)

# A posterior with two well-separated modes; its true mean is 0
3logp <- function(x) {
  log(0.5 * dnorm(x, -gap / 2, 0.7) +
    0.5 * dnorm(x, gap / 2, 0.7) + 1e-300)
}
run <- function(x) { # random-walk Metropolis
  out <- numeric(2000)
  lp <- logp(x)
  for (t in 1:2000) {
    y <- x + rnorm(1, 0, step)
    ly <- logp(y)
    if (log(runif(1)) < ly - lp) {
      x <- y
      lp <- ly
    }
    out[t] <- x
  }
  out
}
4r1 <- run(runif(1, -gap, gap))
r2 <- run(runif(1, -gap, gap))
5m <- c(mean(r1[1001:2000]), mean(r2[1001:2000]))
cat("Second-half means: run 1 ", round(m[1], 2), ", run 2 ", round(m[2], 2),
  " (true mean 0)\n", if (abs(diff(m)) > 1) {
    "The runs disagree."
  } else {
    "The runs agree."
  }, "\n",
  sep = ""
)

6matplot(1:2000, cbind(r1, r2),
  type = "l", lty = 1,
  col = c("#2166ac", "#d6604d"), xlab = "Iteration", ylab = "x"
)
1
The page’s controls: the distance between the posterior’s two modes and the sampler’s step size.
2
Fixes the random numbers so the run is reproducible.
3
A posterior with two well-separated modes.
4
Each run of the sampler starts from a random point and may stay in one mode for the whole run.
5
The second-half means of the two runs. Running it twice is what reveals that each run was confident and wrong.
6
The two chains. If they disagree, neither one can be trusted alone.
# Until it runs twice: two runs of a sampler expose a stuck chain.
from statistics import NormalDist
import numpy as np
import matplotlib.pyplot as plt

gap = 5     # distance between the two modes
step = 0.5  # proposal step size
rng = np.random.default_rng(2)

# A posterior with two well-separated modes; its true mean is 0
left, right = NormalDist(-gap / 2, 0.7), NormalDist(gap / 2, 0.7)


def logp(x):
    return np.log(0.5 * left.pdf(x) + 0.5 * right.pdf(x) + 1e-300)


def run(x):  # random-walk Metropolis
    out, lp = [], logp(x)
    for _ in range(2000):
        y = x + rng.normal(0, step)
        ly = logp(y)
        if np.log(rng.uniform()) < ly - lp:
            x, lp = y, ly
        out.append(x)
    return np.array(out)


r1, r2 = run(rng.uniform(-gap, gap)), run(rng.uniform(-gap, gap))
m1, m2 = r1[1000:].mean(), r2[1000:].mean()
print(f"Second-half means: run 1 {m1:.2f}, run 2 {m2:.2f} (true mean 0)")
print("The runs disagree." if abs(m1 - m2) > 1 else "The runs agree.")

plt.plot(r1, color="#2166ac")
plt.plot(r2, color="#d6604d")
plt.xlabel("Iteration")
plt.ylabel("x")
plt.show()

Key takeaways

  • One successful run proves little. Run again from a different starting point.
  • Multiple MCMC chains, multiple initializations and clean environments are cheap and catch a lot.
  • If the answer changes between runs, the answer isn’t known yet.

Source

Back to top