Web of Trust
Building and checking models
Principle or heuristic
Introductory
In statistics and programming, it’s not enough to get the right answer; you also want to know it’s right.
What it is
A result that happens to be correct is not much use if you can’t tell it’s correct. Gelman’s “web of trust” is the network of checks that gives you confidence: simulations with known answers, alternative computations, comparisons with simpler models, sanity checks against outside knowledge, and agreement among different methods. Each strand may be weak, but together they support the result.
Why it matters
Code fails silently and models fail quietly. One check, such as the sampler converged or the coefficient has the expected sign, catches some errors and misses others. Several independent checks catch far more. Without such a web, a correct result and an incorrect one look the same to the analyst, and so do their published versions.
How it works
If check \(k\) catches a given error with probability \(p_k\), and checks fail independently, the chance that an error gets past all \(K\) checks is
\[ \prod_{k=1}^K (1 - p_k). \]
Five checks that each catch only 40% of errors together miss fewer than 8%. If the checks are correlated, for example because they all reuse the same buggy function, the product rule overstates the protection. That is why the strands of the web should be as different from each other as possible.
Simulated example
An analysis may contain a bug. Add independent checks, each with modest power, and see how often a buggy result gets through. Increase the correlation between checks to see how protection weakens.
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, pct} from "../js/stats.js"
viewof pCatch = Inputs.range([0.1, 0.9], {value: 0.4, step: 0.05, label: "Power of each check"})
viewof shared = Inputs.range([0, 1], {value: 0, step: 0.05, label: "Correlation between checks (shared blind spots)"})
viewof seed = Inputs.range([1, 500], {value: 106, step: 1, label: "Random seed"})
web = {
const r = rng(seed);
return range(9).map(K => {
let missed = 0;
for (let i = 0; i < 4000; i++) {
const blind = r() < shared; // the bug sits in a blind spot common to all checks
let caught = false;
for (let k = 0; k < K; k++) if (!blind && r() < pCatch) caught = true;
if (!caught) missed++;
}
return {K, missed: missed / 4000, theory: (1 - pCatch) ** K};
});
}
Plot.plot({
ariaLabel: "Web of Trust: simulation chart",
ariaDescription: "Horizontal axis: Number of checks. Vertical axis: Share of bugs that get through. The chart updates with the controls above it, and the summary below the charts states the result in words.",
height: 280,
x: {label: "Number of checks"},
y: {label: "Share of bugs that get through", domain: [0, 1], tickFormat: "%", grid: true},
marks: [
Plot.line(web, {x: "K", y: "theory", stroke: "#8a8a8a", strokeDasharray: "4 3"}),
Plot.line(web, {x: "K", y: "missed", stroke: "#2166ac", strokeWidth: 2}),
Plot.dot(web, {x: "K", y: "missed", fill: "#2166ac"})
]
})# Web of Trust: more checks catch more bugs, unless they share blind spots.
1p_catch <- 0.4 # power of each check
shared <- 0 # correlation between checks (shared blind spots)
2set.seed(106)
K <- 0:8
missed <- sapply(K, function(k) {
mean(replicate(4000, {
3 blind <- runif(1) < shared # the bug is in a common blind spot
4 !any(!blind & runif(k) < p_catch)
}))
})
cat("Share of bugs that get through: 1 check ", round(100 * missed[2]),
"%, 5 checks ", round(100 * missed[6]), "%, 8 checks ",
round(100 * missed[9], 1), "%\n",
sep = ""
)
5plot(K, missed,
type = "b", pch = 19, col = "#2166ac", ylim = c(0, 1),
xlab = "Number of independent checks", ylab = "Share of bugs missed"
)
lines(K, (1 - p_catch)^K, lty = 2)- 1
- The page’s controls: the power of each check and how much the checks share blind spots.
- 2
- Fixes the random numbers so the run is reproducible.
- 3
- Some bugs sit in a blind spot that every check shares.
- 4
- A bug gets through if no check catches it.
- 5
- The share of bugs missed against the number of checks, with the independent-checks curve dashed. Each weak strand helps, as long as the strands are independent.
# Web of Trust: more checks catch more bugs, unless they share blind spots.
import numpy as np
import matplotlib.pyplot as plt
p_catch = 0.4 # power of each check
shared = 0 # correlation between checks (shared blind spots)
rng = np.random.default_rng(106)
K = np.arange(9)
missed = []
for k in K:
blind = rng.uniform(size=4000) < shared # bug in a common blind spot
caught = (rng.uniform(size=(4000, k)) < p_catch).any(axis=1) & ~blind
missed.append(np.mean(~caught))
print(f"Share of bugs that get through: 1 check {100 * missed[1]:.0f}%, "
f"5 checks {100 * missed[5]:.0f}%, 8 checks {100 * missed[8]:.1f}%")
plt.plot(K, missed, "o-", color="#2166ac", label="Simulated")
plt.plot(K, (1 - p_catch) ** K, "k--", label="Independent-checks ideal")
plt.ylim(0, 1)
plt.xlabel("Number of independent checks")
plt.ylabel("Share of bugs missed")
plt.legend()
plt.show()Key takeaways
- Getting the right answer isn’t enough. You need reasons to believe it’s right.
- Several weak but independent checks give strong protection.
- Diversify the checks, since checks with shared blind spots give false comfort.