Part 5 · 1 chapters · ~8 min
Regression
Correlation and covariance, least-squares linear regression, interpreting slope and intercept, R² and residuals, multiple regression, log transforms for skewed data, confounding and why observational data cannot show causation, regression to the mean, and using regression for capacity models.
6
Fit, check, do not over-read
code
// least squares in a few lines
function fit(xs: number[], ys: number[]) {
const n = xs.length, mx = xs.reduce((a, b) => a + b) / n, my = ys.reduce((a, b) => a + b) / n;
let sxy = 0, sxx = 0; for (let i = 0; i < n; i++) { sxy += (xs[i] - mx) * (ys[i] - my); sxx += (xs[i] - mx) ** 2; }
const b = sxy / sxx, a = my - b * mx;
const ssRes = xs.reduce((s, x, i) => s + (ys[i] - (a + b * x)) ** 2, 0), ssTot = ys.reduce((s, y) => s + (y - my) ** 2, 0);
return { a, b, r2: 1 - ssRes / ssTot };
}
// fit(payloadKb, latencyMs) → { a: 12.1, b: 0.84, r2: 0.71 } "each extra KB costs ~0.84 ms" (illustrative)Regression to the mean: the worst-performing week is usually followed by a better one regardless of what you changed, because extreme values are partly noise. Any "fix" applied after a bad week looks effective. Compare against a control or a long baseline.
REGRESSION, CAREFULLY
fitting a line, and what it can and cannot tell you
swipe the figure sideways, or tap expand for full screen
1/4
fit
Least squares fits latency = a + b × payload size. The slope b is the marginal cost: useful for capacity models.
y = a + bxmarginal cost