---
title: Estimating parameters
heading: Sample statistics, bias and maximum likelihood
eyebrow: A11 · Lesson 35 · Estimation
xp: 40
---

::: hook
Twenty power supplies run a thousand hours each, and four fail. What is their failure rate? Five readings of a reference voltage scatter around 5 V. What is the meter's noise? Until now the course has assumed the parameters were known: $p$, $\lambda$, $\sigma$. In practice you estimate them from data, and you need to know whether an estimate is systematically off, how much it scatters, and how to find a good one when there's no obvious formula. Maximum likelihood answers the last question for almost every model in this course.
:::

::: play
- A reliability test saw $n$ failures in $S$ thousand device-hours, with exponential lifetimes. The curve is the log-likelihood: how well each failure rate $\lambda$ explains that data.
- The peak is the maximum likelihood estimate, $\hat\lambda = n/S$.
- Raise $n$ and $S$ together, keeping their ratio. The peak stays put but gets sharper: more data, less uncertainty.
:::
:::

```sim plot
view: graph
x: lam = 0.02..0.6 per 1000 h
y: L = -8..0.5
slider: n = 1..40 step 1 = 4
slider: S = 5..200 step 5 = 20 thousand h
plot: n*ln(lam) - lam*S - (n*ln(n/S) - n) | label: log-likelihood, relative to its peak
marker: x = n/S | label: estimate
readout: estimated rate per 1000 h = n/S | digits: 3
readout: estimated MTTF, thousand h = S/n | digits: 3
readout: rough relative uncertainty = 1/sqrt(n) | digits: 2
```

::: predict
## Five readings are 4.9, 5.1, 5.0, 5.2 and 4.8 V. What is the best estimate of the meter's noise standard deviation?

- [ ] 0.141 V, dividing the sum of squared deviations by 5
- [x] 0.158 V, dividing by 4
- [ ] 0.4 V, the range
- [ ] 0.071 V

::: reveal
0.158 V. The squared deviations from the sample mean add to 0.10 V². Dividing by $n - 1 = 4$ gives 0.025 V², whose square root is 0.158 V. Dividing by 5 underestimates on average, because the deviations are measured from the sample's own mean, which sits closer to the data than the true mean does. (0.071 V is the standard error of the mean, a different quantity.)
:::
:::

::: nameit
## Estimators

An **estimator** $\hat\theta$ is a formula that turns data into a guess at a parameter $\theta$. Because the data are random, so is $\hat\theta$, and it's judged by its distribution:

- **bias:** $E[\hat\theta] - \theta$, a systematic error;
- **variance**, or its square root, the **standard error**: the scatter from sample to sample;
- **mean squared error:** $E[(\hat\theta - \theta)^2] = \text{bias}^2 + \text{variance}$.

A good estimator has small MSE and is **consistent**: it converges to $\theta$ as the data grow.

## Sample mean and variance

From readings $x_1, \ldots, x_n$:

$$\bar x = \frac{1}{n}\sum x_i \qquad s^2 = \frac{1}{n - 1}\sum (x_i - \bar x)^2$$

$\bar x$ is unbiased with standard error $\frac{\sigma}{\sqrt n}$, estimated as $\frac{s}{\sqrt n}$. $s^2$ is unbiased because of the $n - 1$, **Bessel's correction**; dividing by $n$ gives an estimate whose average is $\frac{n - 1}{n}\sigma^2$, too low. The deviations from $\bar x$ always add to zero, so only $n - 1$ of them are free.

## Maximum likelihood

The **likelihood** $L(\theta) = \prod_i p(x_i; \theta)$ is the chance (or density) of the observed data, viewed as a function of the parameter. The **maximum likelihood estimate** is the $\theta$ that makes the data most probable. In practice maximize $\ln L$, which turns the product into a sum, by setting its derivative to zero.

| Model | Data | MLE |
|---|---|---|
| Bernoulli $p$ | $k$ successes in $n$ | $\hat p = k/n$ |
| Poisson $\lambda$ | counts $x_i$ | $\hat\lambda = \bar x$ |
| exponential rate $\lambda$ | $n$ failures in total time $S$ | $\hat\lambda = n/S$ |
| Gaussian $\mu$, $\sigma^2$ | readings $x_i$ | $\bar x$, and $\frac{1}{n}\sum (x_i - \bar x)^2$ |

The exponential case: $\ln L = n\ln\lambda - \lambda S$, whose derivative $\frac{n}{\lambda} - S$ is zero at $\lambda = n/S$. For lifetime tests with replacement, $S$ is the total device-hours run, whoever failed.

For large samples the MLE is nearly unbiased and nearly Gaussian, with a spread set by how sharply $\ln L$ peaks. For counts of events, that's a relative uncertainty of about $\frac{1}{\sqrt n}$, the Poisson rule again.
:::

::: example
## Twenty supplies each run 1000 hours, with failed units replaced, and 4 failures occur. Estimate the failure rate and MTTF, and their uncertainty.

::: solution
1. **Total time.**
   $$\begin{aligned} S &= 20 \times 1000 \\ &= 20\,000\ \text{h} \end{aligned}$$
2. **Failure rate.**
   $$\begin{aligned} \hat\lambda &= \frac{4}{20\,000} \\ &= 2 \times 10^{-4}\ \text{per hour} \end{aligned}$$
3. **MTTF.** It's one over the rate:
   $$\widehat{\text{MTTF}} = 5000\ \text{h}$$
4. **Uncertainty.** With only 4 failures the relative uncertainty is about
   $$\frac{1}{\sqrt4} = 50\%$$
   so the true MTTF could easily be 3000 or 10,000 h.
5. **Why tests run hot.** Reliability tests need many failures, which is why they run hot to speed failures up.

**Answer:** the failure rate is $2 \times 10^{-4}$ per hour and the MTTF 5000 h, each uncertain by about 50%.
:::
:::

::: example
## For the readings 4.9, 5.1, 5.0, 5.2, 4.8 V, find $\bar x$, $s$ and the standard error of the mean.

::: solution
1. **Mean.**
   $$\bar x = 5.0\ \text{V}$$
2. **Squared deviations.** They are 0.01, 0.01, 0, 0.04 and 0.04, adding to 0.10 V².
3. **Sample standard deviation.**
   $$\begin{aligned} s^2 &= \frac{0.10}{4} \\ &= 0.025\ \text{V}^2 \end{aligned}$$
   so $s = 0.158$ V.
4. **Standard error of the mean.**
   $$\frac{0.158}{\sqrt5} = 0.071\ \text{V}$$

**Answer:** $\bar x = 5.0$ V, $s = 0.158$ V and a standard error of 0.071 V: report $5.00 \pm 0.07$ V.
:::
:::

::: example
## 7 of 50 sampled boards fail a test. Estimate the failure rate and its standard error.

::: solution
1. **Estimate the rate.**
   $$\begin{aligned} \hat p &= \frac{7}{50} \\ &= 0.14 \end{aligned}$$
2. **Standard error.**
   $$\begin{aligned} \text{SE} &= \sqrt{\frac{\hat p(1 - \hat p)}{n}} \\ &= \sqrt{\frac{0.14 \times 0.86}{50}} \\ &= 0.049 \end{aligned}$$
3. **What it means.** The rate is about $14 \pm 5\%$: fifty boards pin it down only roughly. Halving the uncertainty would take 200 boards.

**Answer:** the failure rate is about 0.14, with a standard error of 0.049.
:::
:::

::: check
given: ss = 0.4 | 1.2; n = 5 | 9
answer: ss/(n - 1)
tolerance: 0.5%

## {n} readings have squared deviations from their mean adding to {ss}. What is the sample variance $s^2$?

::: right
Right. $\frac{\sum(x_i - \bar x)^2}{n - 1}$ = {answer}.
:::

::: misses
- [ss/n] Divide by $n - 1$: the deviations are from the sample's own mean.
- [sqrt(ss/(n - 1))] That's $s$; the question asks for the variance.
- [any] Not yet. Bessel's correction.
:::
:::

::: check
given: v = 4 | 9; n = 4 | 10
answer: (n - 1)/n*v
tolerance: 0.5%

## The true variance is {v}. On average, what does the divide-by-$n$ estimate give, from {n} readings?

::: right
Right. $\frac{n - 1}{n}\sigma^2$ = {answer}, too low.
:::

::: misses
- [v] That's what the $n - 1$ version averages to.
- [any] Not yet. Its expected value is $\frac{n - 1}{n}\sigma^2$.
:::
:::

::: check
given: k = 3 | 8; S = 50 | 200
answer: k/S
tolerance: 0.5%

## A life test runs {S} thousand device-hours in total and sees {k} failures. What is the maximum likelihood estimate of the failure rate, per thousand hours?

::: right
Right. $\hat\lambda = \frac{n}{S}$ = {answer}.
:::

::: misses
- [S/k] That's the MTTF estimate. The rate is its reciprocal.
- [any] Not yet. Failures over total time.
:::
:::

::: check
given: c1 = 3 | 5; c2 = 7 | 4; c3 = 2 | 6
answer: (c1 + c2 + c3)/3
tolerance: 0.5%

## A Geiger counter records {c1}, {c2} and {c3} counts in three equal intervals. What is the maximum likelihood estimate of the Poisson mean per interval?

::: right
Right. The sample mean: {answer}.
:::

::: misses
- [c1 + c2 + c3] That's the total over three intervals; divide by 3.
- [any] Not yet. For a Poisson model, $\hat\lambda = \bar x$.
:::
:::

::: check
given: s = 2 | 5; n = 16 | 25
answer: s/sqrt(n)
unit: mV
tolerance: 0.5%

## {n} readings have sample standard deviation {s} mV. What is the standard error of their mean?

::: right
Right. $\frac{s}{\sqrt n}$ = {answer}.
:::

::: misses
- [s] That's the scatter of single readings.
- [s/n] Divide by $\sqrt n$.
- [any] Not yet. $s/\sqrt n$.
:::
:::

::: check
given: b = 0.1 | 0.3; s = 0.2 | 0.4
answer: b^2 + s^2
tolerance: 0.5%

## An estimator has bias {b} and standard deviation {s}. What is its mean squared error?

::: right
Right. $\text{bias}^2 + \text{variance}$ = {answer}.
:::

::: misses
- [b + s] Square both before adding.
- [b^2 + s] Use the variance, $\sigma^2$.
- [any] Not yet. MSE $= \text{bias}^2 + \sigma^2$.
:::
:::

::: check
kind: parts
given: k = 5 | 7 | 12; n = 50 | 100

## {k} of {n} boards fail a test.

::: part
answer: k/n
tolerance: 0.5%

### What is the estimated failure rate?

::: misses
- [any] $\hat p = k/n$.
:::
:::

::: part
answer: sqrt(k/n*(1 - k/n)/n)
tolerance: 0.5%

### What is its standard error?

::: misses
- [any] $\sqrt{\hat p(1 - \hat p)/n}$.
:::
:::
:::

::: check
kind: derivation
vars: s = 0.01..1, n = 4..100

## Readings have sample standard deviation $s$; there are $n$ of them.

::: step
answer: s/sqrt(n)

### What is the standard error of the mean?
:::

::: step
answer: 1.96*s/sqrt(n)

### What is the half-width of a 95% interval, for large $n$?
:::

::: right
Right. About two standard errors either side of the mean.
:::
:::

::: explain
## Why does dividing by $n$ underestimate the variance?

::: model
The deviations are measured from the sample mean $\bar x$, and $\bar x$ is, by construction, the value that makes the sum of squared deviations as small as possible. The true mean is somewhere else, so deviations from it would be larger on average. Measuring from $\bar x$ therefore shrinks the sum, by exactly one variance's worth on average: $E[\sum(x_i - \bar x)^2] = (n - 1)\sigma^2$. Dividing by $n - 1$ instead of $n$ undoes that.
:::
:::

::: practice

::: check
given: k = 12 | 45; n = 200 | 500
answer: k/n
tolerance: 0.5%

## {k} of {n} sampled boards fail. What is the maximum likelihood estimate of the failure probability?

::: misses
- [any] $k/n$.
:::
:::

::: check
given: k = 9 | 16 | 25
answer: 1/sqrt(k)
tolerance: 0.5%

## A rate estimate is based on {k} observed events. About what is its relative uncertainty?

::: misses
- [any] $1/\sqrt k$.
:::
:::

::: check
given: ss = 2 | 6; n = 3 | 7
answer: sqrt(ss/(n - 1))
tolerance: 0.5%

## {n} readings have squared deviations adding to {ss}. What is the sample standard deviation?

::: misses
- [sqrt(ss/n)] Use $n - 1$.
- [any] $\sqrt{\sum(x_i - \bar x)^2/(n - 1)}$.
:::
:::
:::
