Part I: Foundations of Sensory AI
Chapter 4: Probability, Estimation, and Uncertainty Primer

Estimators, bias-variance, and maximum likelihood

"Every number I report is a guess with a track record. My job is to know how good the track record is before you trust the guess."

A Self-Aware AI Agent

The Big Picture

A sensor never hands you the quantity you actually want. A photodiode gives counts, not lux; an accelerometer gives voltage, not acceleration; a thermistor gives resistance, not degrees. Between the raw reading and the number you report sits an estimator: a rule that turns noisy data into a best guess of a hidden parameter. This section is about how to build those rules, how to judge them, and why the single most useful decomposition in all of applied statistics, the split of error into bias and variance, governs everything from how many samples you average to how large a neural network you can afford. We then meet maximum likelihood, the workhorse recipe that produces most of the estimators you will ever deploy, and the Cramer-Rao bound, which tells you when to stop trying to do better.

This section assumes you are comfortable with random variables, expectation, and variance from Section 4.1, and with the sensor noise models introduced in Chapter 2. Everything here is frequentist: the parameter is a fixed unknown and the data is random. The complementary Bayesian view, where the parameter itself carries a distribution, arrives in Section 4.3.

What an estimator is, and how to grade one

An estimator \(\hat\theta\) is any function of the data \(X_1,\dots,X_n\) that we use to guess an unknown parameter \(\theta\). Because the data is random, \(\hat\theta\) is a random variable with its own distribution, called the sampling distribution. We grade an estimator by properties of that distribution, not by any single run.

Bias measures systematic error: \(\operatorname{Bias}(\hat\theta) = \mathbb{E}[\hat\theta] - \theta\). An estimator is unbiased if, averaged over infinitely many experiments, it lands on the truth. Variance measures spread: how far a single estimate typically strays from its own average. The quantity that actually matters for a deployed system combines both, the mean squared error:

$$\operatorname{MSE}(\hat\theta) = \mathbb{E}\big[(\hat\theta - \theta)^2\big] = \operatorname{Bias}(\hat\theta)^2 + \operatorname{Var}(\hat\theta).$$

This identity is the spine of the whole section. It says total error has exactly two ingredients, and they trade against each other. A thermostat that always reads two degrees high has bias but no variance; a cheap sensor that jitters wildly around the true value has variance but no bias. Both are wrong, and MSE counts them on the same scale.

Two more properties matter in practice. An estimator is consistent if \(\hat\theta \to \theta\) as \(n\to\infty\): more data eventually crushes the error. It is efficient if, among unbiased estimators, it has the smallest possible variance. Consistency is the minimum bar for a sensor calibration routine; efficiency tells you whether spending money on a better algorithm can help, or whether you have already hit the physics-imposed floor.

Key Insight: unbiased is not the goal, low MSE is

Beginners chase unbiasedness as if it were sacred. It is not. Because MSE adds squared bias and variance, you can often reduce total error by accepting a little bias in exchange for a large drop in variance. This is exactly why we regularize models, why we shrink noisy estimates toward a prior, and why averaging a slightly-biased-but-stable sensor beats a perfectly-calibrated-but-jittery one. The estimator that wins in the field is the one with the smallest MSE at your actual sample size, not the one that looks purest on a whiteboard.

The bias-variance tradeoff as a design dial

The tradeoff is not an abstraction; it is a knob you turn every day. Consider estimating a slowly-drifting quantity, say the resting heart rate baseline of a wearer, by averaging the last \(n\) beats. A long window (large \(n\)) drives variance down like \(1/n\) because averaging cancels independent noise. But a long window also reaches back into stale data, so if the true baseline is drifting, the average lags behind reality and picks up bias. A short window tracks the drift faithfully (low bias) but barely averages anything (high variance). There is an optimal window length that minimizes MSE, and it depends on the noise level and the drift rate. This same shape recurs in filter bandwidth choices in Chapter 6 and in model capacity choices for the neural sensor models of Chapter 13.

The model-complexity version is identical in spirit. A tiny model is too rigid to capture the signal, so it is biased (underfitting). A huge model bends to fit the noise in your particular training set, so it has high variance and generalizes poorly (overfitting). Every regularization technique, weight decay, dropout, early stopping, is a device for buying variance reduction with a controlled dose of bias.

Practical Example: calibrating a MEMS gas sensor on the line

An industrial air-quality startup ships a low-cost metal-oxide CO2 sensor. Each unit's raw resistance-to-ppm curve varies with manufacturing tolerances, so every device gets a one-point factory calibration against a reference gas. The engineers first tried estimating each device's offset from a single 5-second reference reading. Result: unbiased, but the per-device offset estimate had huge variance because 5 seconds of a noisy sensor is barely any information, and field accuracy was terrible.

The fix was pure bias-variance thinking. They shrank each device's raw offset estimate toward the fleet-average offset, a deliberately biased move, weighting the shrinkage by how noisy that unit's reference reading was. Devices with clean readings kept their own estimate; devices with noisy readings borrowed strength from the fleet. MSE of the deployed calibration dropped by roughly forty percent with zero hardware change. The lesson: a biased estimator that leans on a good prior beat the unbiased one that trusted five noisy seconds.

Maximum likelihood: one recipe for almost every estimator

Where do good estimators come from? Most of the time, from maximum likelihood estimation (MLE). The idea is disarmingly simple. Write down a probabilistic model \(p(x \mid \theta)\) for how your sensor produces data given the parameter. Given the actual data you observed, treat that probability as a function of \(\theta\), the likelihood \(L(\theta) = \prod_i p(x_i \mid \theta)\), and pick the \(\theta\) that makes the observed data most probable:

$$\hat\theta_{\text{MLE}} = \arg\max_\theta \; \sum_{i=1}^n \log p(x_i \mid \theta).$$

We maximize the log-likelihood because sums are numerically kinder than products and because the logarithm turns the exponentials in most noise models into tidy quadratics. Here is the payoff that connects a whole thread of the book: if your sensor noise is Gaussian, maximum likelihood is exactly least squares. Maximizing \(\sum_i \log p(x_i\mid\theta)\) for \(p = \mathcal{N}(\mu(\theta), \sigma^2)\) is the same as minimizing \(\sum_i (x_i - \mu(\theta))^2\). The sample mean, linear regression, and the Kalman filter update of Chapter 9 are all maximum-likelihood estimators under a Gaussian assumption in disguise.

MLE earns its dominance because, under mild conditions, it is asymptotically unbiased, consistent, and efficient: with enough data it reaches the lowest variance any unbiased estimator can achieve. That floor has a name.

Key Insight: the Cramer-Rao bound tells you when to stop optimizing

For any unbiased estimator, the variance cannot go below the reciprocal of the Fisher information \(I(\theta)\): \(\operatorname{Var}(\hat\theta) \ge 1/I(\theta)\). Fisher information measures how sharply the log-likelihood peaks around the true parameter, that is, how much each sample actually tells you. The practical consequence is liberating: compute the Cramer-Rao bound for your problem, and if your current estimator already sits near it, no cleverer algorithm will save you. Your only levers left are more data, a better sensor (higher \(I(\theta)\) per sample), or accepting bias to trade against variance. This is how good sensing teams decide between "write a smarter filter" and "buy a quieter sensor."

Computing a maximum-likelihood estimate

Consider a common sensor-modeling task: your accelerometer sits still, and you want to estimate the noise standard deviation \(\sigma\) of its output so you can feed it to a downstream filter. Assume readings are Gaussian around a fixed (also unknown) bias \(\mu\). The code below fits both by maximum likelihood and compares against the Cramer-Rao lower bound on the variance of \(\hat\sigma\), so you can see whether your estimate is as good as physics allows.

import numpy as np

rng = np.random.default_rng(0)
true_mu, true_sigma, n = 0.02, 0.15, 500          # bias (g), noise std (g), samples
x = rng.normal(true_mu, true_sigma, size=n)        # a still accelerometer axis

# Maximum-likelihood estimates for a Gaussian: closed form.
mu_hat = x.mean()                                  # MLE of the mean
sigma_hat = x.std(ddof=0)                           # MLE of std (divides by n, biased low)
sigma_unbiased = x.std(ddof=1)                      # Bessel-corrected (divides by n-1)

# Cramer-Rao lower bound on Var(sigma_hat) for a Gaussian: sigma^2 / (2n).
crlb_var_sigma = true_sigma**2 / (2 * n)

print(f"mu_hat      = {mu_hat:+.4f}  (true {true_mu:+.4f})")
print(f"sigma_hat   = {sigma_hat:.4f}  MLE, biased low by ~sigma/(2n)")
print(f"sigma_unbi. = {sigma_unbiased:.4f}  (true {true_sigma:.4f})")
print(f"CRLB std of sigma_hat = {np.sqrt(crlb_var_sigma):.4f} g")
Maximum-likelihood estimation of an accelerometer's bias and noise from a still recording. The MLE of \(\sigma\) divides by \(n\) and is slightly biased low; the Bessel-corrected version (divide by \(n-1\)) is the unbiased choice. The printed Cramer-Rao bound is the smallest standard error any unbiased estimator of \(\sigma\) could achieve at this sample size, so it tells you whether collecting more stillness data would meaningfully help.

Notice the concrete appearance of bias-variance in the output: the plain MLE of \(\sigma\) is biased low (it divides by \(n\)), while the Bessel-corrected estimate is unbiased (it divides by \(n-1\)). At \(n=500\) the gap is tiny, which is exactly the point, the bias of MLE vanishes as data grows, so we tolerate it in exchange for the recipe's generality.

Library Shortcut: let SciPy fit the whole distribution

Writing the log-likelihood and its maximizer by hand is instructive but rarely necessary. For any of dozens of noise families, scipy.stats.<dist>.fit(x) returns the maximum-likelihood parameters in one line, handling the optimization, the log-sum-exp stability, and the parameter constraints for you:

from scipy import stats
mu_hat, sigma_hat = stats.norm.fit(x)        # MLE, one line
df, loc, scale   = stats.t.fit(x)            # heavy-tailed? swap the family

That replaces roughly 20 to 40 lines of hand-rolled likelihood, gradient, and bounded-optimizer code per distribution, and switching noise models (Gaussian to Student-t for a sensor with outliers) becomes a one-word edit. Reserve the from-scratch version for when your measurement model is custom enough that no library distribution fits it.

When maximum likelihood misleads you

MLE is powerful, not infallible, and sensor data breaks its assumptions in predictable ways. First, model misspecification: MLE finds the best parameter within the family you assumed. If you assume Gaussian noise but your accelerometer occasionally emits large spike outliers, the fitted \(\sigma\) inflates to cover the spikes and every downstream estimate suffers. The remedy is a heavier-tailed likelihood (Student-t, Huber) so a few outliers no longer dominate. Second, small samples: MLE's beautiful efficiency is asymptotic, and with a handful of points it can be badly biased or even undefined. That is precisely the regime where borrowing strength from a prior pays off, which motivates the Bayesian treatment in Section 4.3. Third, MLE gives you a point estimate; it does not, by itself, tell you how much to trust that point. Quantifying that trust, and separating noise you cannot reduce from ignorance you can, is the subject of Section 4.4 on aleatoric versus epistemic uncertainty.

Exercise

You estimate a wearable's skin-temperature offset by averaging \(n\) readings taken over a window during which the true skin temperature is slowly rising at rate \(r\) degrees per sample. Model the per-sample noise as zero-mean with variance \(\sigma^2\). (a) Write the MSE of the windowed-average estimate as a function of \(n\), \(r\), and \(\sigma^2\), identifying which term is bias and which is variance. (b) Differentiate to find the window length \(n^\star\) that minimizes MSE. (c) Explain in one sentence what happens to \(n^\star\) as the sensor gets noisier, and as the drift gets faster. This is the bias-variance dial made numerical.

Self-Check

  1. An estimator has zero bias but enormous variance, and a second has a small constant bias but tiny variance. Under MSE at a fixed sample size, which can be the better choice, and why does "unbiased" not settle the question?
  2. Your sensor-noise fit sits right at the Cramer-Rao lower bound. A colleague proposes a more elaborate estimation algorithm. What do you tell them, and what are the two remaining ways to actually reduce the error?
  3. Why is maximum-likelihood estimation under Gaussian noise identical to least-squares fitting, and what changes the moment you switch to a Student-t noise model?

What's Next

In Section 4.3, we relax the frequentist assumption that the parameter is a fixed unknown and let it carry a distribution of its own. That single move, treating \(\theta\) as random, turns the shrinkage trick we improvised for the gas sensor into a principled framework, gives us a rigorous way to inject prior knowledge when data is scarce, and hands back not just a point estimate but a full posterior we can propagate through the rest of the sensing pipeline.