---
title: Estimating one quantity from another
heading: The best guess of X from a noisy Y
eyebrow: A11 · Lesson 24 · Estimation from correlated data
xp: 40
---

::: hook
A sensor reading is the true value plus noise; a temperature at one point is correlated with the temperature at another. When two random quantities are related, observing one tells you something about the other, and probability says exactly how much. The **minimum mean-square-error** estimate is the guess whose squared error is smallest on average. For jointly Gaussian quantities it's a straight line through the data, and it explains three everyday engineering moves: shrinking a noisy reading toward its expected value, combining two sensors by weighting each by how much you trust it, and predicting one signal from a correlated one.
:::

::: play
- A quantity $X$ with spread $\sigma_x$ is measured as $Y = X + N$, with noise of spread $\sigma_n$.
- The best linear estimate is $\hat X = gY$ with gain $g = \frac{\sigma_x^2}{\sigma_x^2 + \sigma_n^2}$. The curve shows $g$ against $\sigma_n$; the readouts give $g$ and the remaining error.
- With little noise, $g \to 1$: trust the reading. With a lot, $g \to 0$: fall back on the expected value, 0.
:::

```sim plot
view: graph
x: sn = 0..5
y: gain = 0..1.05
slider: sx = 0.5..4 step 0.1 = 2
plot: sx^2/(sx^2 + sn^2) | label: estimator gain g | samples: 400
readout: gain at noise 1 = sx^2/(sx^2 + 1) | digits: 4
readout: error std at noise 1 = sqrt(sx^2/(sx^2 + 1)) | digits: 4
```

::: predict
## Two sensors measure the same quantity, one with 1 °C of noise and the other with 2 °C. How should their readings be combined?

- [ ] Average them equally
- [x] Weight the better one four times as heavily
- [ ] Weight the better one twice as heavily
- [ ] Use only the better one

::: reveal
Weight it four times as heavily: weights go as $\frac{1}{\sigma^2}$, so $\frac11$ against $\frac14$, normalized to 0.8 and 0.2. The combined reading has variance $\frac{1}{1 + 1/4} = 0.8$, a standard deviation of 0.89 °C: better than the good sensor alone. An equal average would give $\sqrt{(1 + 4)/4} = 1.12$ °C, worse than the good sensor.
:::
:::

::: nameit
## The linear MMSE estimate

To estimate $X$ from $Y$ with a straight line $\hat X = a + bY$, minimizing $E[(X - \hat X)^2]$ gives

$$\hat X = \mu_X + \frac{\text{Cov}(X, Y)}{\sigma_Y^2}(Y - \mu_Y)$$

with mean-square error $\sigma_X^2(1 - \rho^2)$, where $\rho$ is the correlation. If $X$ and $Y$ are jointly Gaussian, no other function of $Y$ does better.

## A noisy measurement

For $Y = X + N$ with $X$ and $N$ independent, zero mean:

$$\hat X = \frac{\sigma_x^2}{\sigma_x^2 + \sigma_n^2}Y, \qquad \text{error variance} = \frac{\sigma_x^2\sigma_n^2}{\sigma_x^2 + \sigma_n^2}$$

The gain is the fraction of $Y$'s variance that is signal. This is the scalar Wiener filter; applied frequency by frequency it becomes the Wiener filter for signals, and applied step by step in time, the Kalman filter.

## Combining measurements

Two independent unbiased readings with variances $\sigma_1^2$ and $\sigma_2^2$ combine best with weights proportional to $\frac{1}{\sigma_1^2}$ and $\frac{1}{\sigma_2^2}$. The combined variance satisfies $\frac{1}{\sigma^2} = \frac{1}{\sigma_1^2} + \frac{1}{\sigma_2^2}$: information adds.
:::

::: example
## A quantity with spread 2 is measured with noise of spread 1. What is the best estimate, and how good is it?

::: solution
1. **Gain.** The quantity has variance 4 and the noise variance 1:
   $$\frac{4}{4 + 1} = 0.8$$
   so $\hat X = 0.8Y$.
2. **Error variance.**
   $$\frac{4 \times 1}{5} = 0.8$$
   a standard deviation of 0.89: better than the raw reading's 1.
3. **Why shrinking helps.** Shrinking toward the mean trades a little bias in each estimate for less error on average.

**Answer:** the best estimate is $\hat X = 0.8Y$, with an error standard deviation of 0.89.
:::
:::

::: example
## Two temperatures have correlation 0.9. How much of one's spread is left after predicting it from the other?

::: solution
1. **Error variance.**
   $$\begin{aligned} \sigma^2(1 - \rho^2) &= \sigma^2(1 - 0.81) \\ &= 0.19\sigma^2 \end{aligned}$$
2. **Residual spread.** Take the square root:
   $$\sqrt{0.19} = 0.44$$
   of the original.
3. **What it means.** Correlation 0.9 sounds high, but it leaves 44% of the spread unexplained; it takes $\rho = 0.995$ to get below 10%.

**Answer:** 44% of the spread is left.
:::
:::

::: example
## Combine readings with noise 1 °C and 2 °C optimally.

::: solution
1. **Weights.** Weight each reading by one over its variance:
   $$\frac{1/1}{1/1 + 1/4} = 0.8$$
   for the 1 °C reading, and 0.2 for the 2 °C one.
2. **Combined variance.**
   $$\frac{1}{1 + 0.25} = 0.8$$
   a spread of 0.89 °C.
3. **The noisy sensor still helps.** It helps a little: it carries a fifth of the information.

**Answer:** weight the readings 0.8 and 0.2; the combination has a spread of 0.89 °C.
:::
:::

::: check
given: sx = 1 | 2 | 3; sn = 0.5 | 1.5
answer: sx^2/(sx^2 + sn^2)
tolerance: 0.5%

## $Y = X + N$, with $X$ of spread {sx} and independent noise of spread {sn}. What gain $g$ makes $\hat X = gY$ the best estimate?

::: right
Right. The signal's share of $Y$'s variance: {answer}.
:::

::: misses
- [sx/(sx + sn)] Use variances, not standard deviations.
- [1] That ignores the noise; with noise, shrink toward the mean.
- [any] Not yet. $g = \frac{\sigma_x^2}{\sigma_x^2 + \sigma_n^2}$.
:::
:::

::: check
given: s1 = 0.5 | 1; s2 = 2 | 3
answer: 1/sqrt(1/s1^2 + 1/s2^2)
tolerance: 0.5%

## Two independent sensors have noise {s1} and {s2}. What is the noise of their best combination?

::: right
Right. $\frac{1}{\sigma^2} = \frac{1}{\sigma_1^2} + \frac{1}{\sigma_2^2}$, so {answer}.
:::

::: misses
- [sqrt(s1^2 + s2^2)/2] That's the equal-weight average, which is worse than the better sensor alone here.
- [s1] The second sensor still adds information.
- [any] Not yet. Weight by $1/\sigma^2$.
:::
:::

::: check
given: r = 0.5 | 0.8 | 0.95
answer: sqrt(1 - r^2)
tolerance: 0.5%

## Two quantities have correlation {r}. What fraction of one's spread remains after predicting it linearly from the other?

::: right
Right. $\sqrt{1 - \rho^2}$ = {answer}.
:::

::: misses
- [1 - r] Use $\sqrt{1 - \rho^2}$.
- [1 - r^2] That's the variance fraction; take the square root for the spread.
- [any] Not yet. The error variance is $\sigma^2(1 - \rho^2)$.
:::
:::

::: check
kind: parts
given: mx = 20 | 25; my = 18 | 22; c = 3 | 6; vy = 4 | 9

## $X$ has mean {mx}, $Y$ has mean {my} and variance {vy}, and their covariance is {c}. $Y$ is observed to be $\given{my} + 2$.

::: part
answer: c/vy
tolerance: 0.5%

### What is the slope $b$ of the best linear estimate?

::: misses
- [c/sqrt(vy)] Divide by the variance, not the standard deviation.
- [any] $b = \frac{\text{Cov}(X, Y)}{\sigma_Y^2}$.
:::
:::

::: part
answer: mx + 2*c/vy
tolerance: 0.5%

### What is the estimate of $X$?

::: misses
- [any] $\mu_X + b(Y - \mu_Y)$.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.5..4, b = 0.5..4

## $Y = X + N$, with $X$ of variance $a$ and independent noise of variance $b$.

::: step
answer: a/(a + b)

### What is the best gain?
:::

::: step
answer: a*b/(a + b)

### What is the error variance?
:::

::: right
Right. The error variance is smaller than both $a$ and $b$, like resistances in parallel.
:::
:::

::: check
kind: python

## Draw $X$ with spread 2 and independent noise $N$ with spread 1, 10⁵ samples each (`np.random.default_rng(2)`), and form $Y = X + N$. Set `g_data` to $\frac{\text{Cov}(X, Y)}{\text{Var}(Y)}$ from the samples, and `err` to the RMS of $X - g_\text{data}Y$.

```python starter
import numpy as np

g_data = err = 0.0
print(g_data, err)
```

```python solution
import numpy as np

rng = np.random.default_rng(2)
X = rng.normal(0, 2, 10**5)
Y = X + rng.normal(0, 1, 10**5)
g_data = np.cov(X, Y)[0, 1] / np.var(Y, ddof=1)
err = np.sqrt(np.mean((X - g_data * Y)**2))
print(g_data, err)
```

```python tests
assert abs(g_data - 0.8) < 0.01, f"The best gain is 4/(4 + 1) = 0.8; got {g_data}."
assert abs(err - 0.8944) < 0.01, f"The error RMS is sqrt(0.8) = 0.894; got {err}."
```

::: right
Right. The gain measured from data matches the formula, and the error is smaller than the raw noise of 1.
:::
:::

::: explain
## Why does the best estimate shrink a noisy reading toward the expected value, instead of taking the reading at face value?

::: model
A reading $Y = X + N$ is equally likely to be pushed up or down by noise, but $X$ itself is more likely to be near its mean than far from it. So an unusually large reading is more likely to be a moderate $X$ plus positive noise than a huge $X$ with no noise. Shrinking by the gain $\frac{\sigma_x^2}{\sigma_x^2 + \sigma_n^2}$ weighs those two explanations by how large each kind of variation typically is. Each individual estimate is biased toward the mean, but the average squared error is smaller than with the raw reading, which is the criterion that matters when the estimates feed a decision or a control loop.
:::
:::

::: practice

::: check
given: sx = 0.1..10; sn = 0.1..10
answer: sx^2/(sx^2 + sn^2)
tolerance: 0.5%

## Best gain for signal spread {sx}, noise spread {sn}?

::: misses
- [any] $\frac{\sigma_x^2}{\sigma_x^2 + \sigma_n^2}$.
:::
:::

::: check
given: s1 = 0.1..5; s2 = 0.1..5
answer: 1/sqrt(1/s1^2 + 1/s2^2)
tolerance: 0.5%

## Combined noise of sensors with {s1} and {s2}?

::: misses
- [any] $\left(\sigma_1^{-2} + \sigma_2^{-2}\right)^{-1/2}$.
:::
:::
:::
