---
title: Covariance and correlation
heading: When two noises move together
eyebrow: A11 · Lesson 22 · Covariance and correlation
xp: 40
---

::: hook
A differential pair carries a signal as the difference between two wires. Interference couples into both wires, 10 mV of noise on each. If the two noises were unrelated, the difference would carry 14 mV of noise. But the wires run side by side, so the noise on them rises and falls together, and the difference carries much less. How much less depends on one number, the correlation between the two noises. It also explains when adding noisy signals helps and when it hurts.
:::

::: play
- The shading is the joint density of two noise voltages $X$ and $Y$, each with standard deviation 1, and correlation $\rho$.
- At $\rho = 0$ the contours are circles. Raise $\rho$: the density stretches along the line $y = x$, because the two tend to move together.
- The readouts give the spread of the sum and of the difference. Which one a differential receiver uses explains why twisted pairs work.
:::
:::

```sim plot
view: flow
x: x = -3..3
y: y = -3..3
aspect: equal
slider: r = -0.95..0.95 step 0.05 = 0.6
shade: exp(-(x^2 - 2*r*x*y + y^2)/(2*(1 - r^2)))/(2*pi*sqrt(1 - r^2)) | contours: 8
path: -3 + 6*u, -3 + 6*u | over: u = 0..1 | color: ch3
readout: standard deviation of X + Y = sqrt(2 + 2*r) | digits: 3
readout: standard deviation of X − Y = sqrt(2 - 2*r) | digits: 3
```

::: predict
## Each wire of a differential pair picks up noise of 10 mV RMS, with correlation 0.9 between the wires. What noise is left in the difference?

- [ ] 14.1 mV
- [ ] 10 mV
- [x] About 4.5 mV
- [ ] 0 mV

::: reveal
About 4.5 mV. The variance of a difference is $\sigma_X^2 + \sigma_Y^2 - 2\rho\sigma_X\sigma_Y = 100 + 100 - 180 = 20$ mV², so the RMS is $\sqrt{20} = 4.47$ mV. Unrelated noises would give $\sqrt{200} = 14.1$ mV; perfectly correlated ones would cancel completely. Twisting the pair raises the correlation, which is the whole point of twisting.
:::
:::

::: nameit
## Covariance

$$\text{Cov}(X, Y) = E\big[(X - \mu_X)(Y - \mu_Y)\big] = E[XY] - \mu_X\mu_Y$$

It's positive when $X$ and $Y$ tend to be above their means together, negative when one tends to be high while the other is low. $\text{Cov}(X, X) = \text{Var}(X)$.

## Correlation

Covariance depends on units. Dividing by both standard deviations gives a pure number, the **correlation coefficient**:

$$\rho = \frac{\text{Cov}(X, Y)}{\sigma_X\sigma_Y}, \qquad -1 \le \rho \le 1$$

$\rho = \pm 1$ means $Y$ is exactly a straight-line function of $X$. $\rho = 0$ means **uncorrelated**.

## Variance of sums and differences

$$\text{Var}(aX + bY) = a^2\sigma_X^2 + b^2\sigma_Y^2 + 2ab\,\text{Cov}(X, Y)$$

- Sum: $\sigma_X^2 + \sigma_Y^2 + 2\rho\sigma_X\sigma_Y$. Correlated noises add more than in quadrature.
- Difference: $\sigma_X^2 + \sigma_Y^2 - 2\rho\sigma_X\sigma_Y$. Positively correlated noise cancels: **common-mode rejection**.

The root-sum-square rule of lesson 9 is the special case $\rho = 0$.

## Independent versus uncorrelated

Independent variables have $\text{Cov} = 0$, because $E[XY] = E[X]E[Y]$ when the joint density factors. The reverse fails: correlation only measures straight-line association. With $X$ uniform on $[-1, 1]$ and $Y = X^2$, $Y$ is completely determined by $X$, yet $\text{Cov}(X, Y) = E[X^3] - E[X]E[X^2] = 0$. For jointly Gaussian variables, and only then in general, uncorrelated does mean independent.
:::

::: example
## Two sensors read the same temperature. Each has noise of 0.2 °C RMS, and their noises have correlation 0.3 (they share a power supply). How noisy is their average?

::: solution
1. **Variance of the average.**
   $$\begin{aligned} \text{Var}\left(\frac{X + Y}{2}\right) &= \frac{1}{4}(0.04 + 0.04 \\ &\qquad + 2 \times 0.3 \times 0.04) \\ &= \frac{0.104}{4} \\ &= 0.026 \end{aligned}$$
   so the RMS is 0.161 °C.
2. **Compare with uncorrelated sensors.** They would give
   $$\frac{0.2}{\sqrt2} = 0.141\ ^\circ\text{C}$$
3. **Why.** The shared noise doesn't average away.

**Answer:** their average has 0.161 °C RMS of noise, against 0.141 °C for uncorrelated sensors.
:::
:::

::: example
## $X$ has mean 2 and $Y$ has mean 3, with $E[XY] = 7$, $\sigma_X = 1$ and $\sigma_Y = 2$. Find the covariance and correlation.

::: solution
1. **Covariance.** Subtract the product of the means from $E[XY]$:
   $$\begin{aligned} \text{Cov} &= 7 - 2 \times 3 \\ &= 1 \end{aligned}$$
2. **Correlation.** Divide by the two standard deviations:
   $$\begin{aligned} \rho &= \frac{1}{1 \times 2} \\ &= 0.5 \end{aligned}$$

**Answer:** the covariance is 1 and the correlation 0.5.
:::
:::

::: example
## Two sensors each have 0.2 °C of noise, with correlation 0.5. How noisy is the difference of their readings?

::: solution
1. **Variance of the difference.**
   $$\begin{aligned} \text{Var}(X - Y) &= \sigma_X^2 + \sigma_Y^2 \\ &\quad - 2\rho\sigma_X\sigma_Y \\ &= 0.04 + 0.04 - 0.04 \\ &= 0.04 \end{aligned}$$
   so the difference has 0.2 °C of noise, the same as either sensor alone.
2. **Why.** Positive correlation helps differences and hurts sums: common-mode noise cancels when you subtract, which is why differential measurements are used.

**Answer:** the difference has 0.2 °C of noise, the same as either sensor alone.
:::
:::

::: check
given: exy = 7 | 9; mx = 2 | 1; my = 3 | 4
answer: exy - mx*my
tolerance: 0.5%

## $E[XY]$ = {exy}, $E[X]$ = {mx} and $E[Y]$ = {my}. What is $\text{Cov}(X, Y)$?

::: right
Right. $E[XY] - \mu_X\mu_Y$ = {answer}.
:::

::: misses
- [exy] Subtract the product of the means.
- [any] Not yet. $\text{Cov} = E[XY] - E[X]E[Y]$.
:::
:::

::: check
given: c = 0.5 | 1.2; sx = 1 | 2; sy = 1.5 | 3
answer: c/(sx*sy)
tolerance: 0.5%

## $\text{Cov}(X, Y)$ = {c}, $\sigma_X$ = {sx} and $\sigma_Y$ = {sy}. What is the correlation coefficient?

::: right
Right. $\frac{\text{Cov}}{\sigma_X\sigma_Y}$ = {answer}.
:::

::: misses
- [c] Divide by both standard deviations.
- [c/(sx^2*sy^2)] Divide by the standard deviations, not the variances.
- [any] Not yet. $\rho = \text{Cov}/(\sigma_X\sigma_Y)$.
:::
:::

::: check
given: sx = 3 | 4; sy = 2 | 5; r = 0.5 | -0.5
answer: sqrt(sx^2 + sy^2 + 2*r*sx*sy)
tolerance: 0.5%

## Noises with RMS {sx} and {sy} mV and correlation {r} add. What is the RMS of the sum?

::: right
Right. $\sqrt{\sigma_X^2 + \sigma_Y^2 + 2\rho\sigma_X\sigma_Y}$ = {answer} mV.
:::

::: misses
- [sqrt(sx^2 + sy^2)] That's for uncorrelated noise. Include $2\rho\sigma_X\sigma_Y$.
- [sx + sy] Only perfectly correlated noises add directly.
- [any] Not yet. Variances add, plus twice the covariance.
:::
:::

::: check
given: s = 5 | 10; r = 0.8 | 0.95
answer: s*sqrt(2 - 2*r)
tolerance: 0.5%

## Both wires of a differential pair pick up {s} mV RMS of noise, with correlation {r}. How much noise is in the difference?

::: right
Right. $\sigma\sqrt{2 - 2\rho}$ = {answer} mV.
:::

::: misses
- [s*sqrt(2)] That assumes the noises are unrelated; the correlation makes them cancel.
- [s*sqrt(2 + 2*r)] That's the sum. The difference subtracts the covariance.
- [any] Not yet. $\text{Var}(X - Y) = \sigma^2 + \sigma^2 - 2\rho\sigma^2$.
:::
:::

::: check
given: a = 2 | 3; b = 1 | 4; sx = 1 | 2; sy = 1 | 3
answer: sqrt(a^2*sx^2 + b^2*sy^2)
tolerance: 0.5%

## $X$ and $Y$ are independent with standard deviations {sx} and {sy}. What is the standard deviation of $\given{a}X - \given{b}Y$?

::: right
Right. The coefficients are squared, so the minus sign doesn't matter: {answer}.
:::

::: misses
- [a*sx - b*sy] Standard deviations don't subtract; variances add, with squared coefficients.
- [sqrt(a*sx^2 + b*sy^2)] Square the coefficients too.
- [any] Not yet. $\text{Var}(aX + bY) = a^2\sigma_X^2 + b^2\sigma_Y^2$ when independent.
:::
:::

::: check
kind: choice

## $X$ and $Y$ have zero covariance. What follows?

- [ ] They are independent
- [x] The variance of their sum is the sum of their variances
- [ ] $Y$ can't be a function of $X$
- [ ] Their correlation is 1

::: right
Right. That's all zero covariance guarantees. They can still be strongly related, as $X$ and $X^2$ are when $X$ is symmetric about zero.
:::

::: misses
- [1] Uncorrelated doesn't imply independent, except for jointly Gaussian variables.
- [3] $Y = X^2$ with $X$ uniform on $[-1, 1]$ is a function of $X$ with zero covariance.
- [4] Zero covariance means correlation 0.
:::
:::

::: check
kind: parts
given: s = 0.1 | 0.3; r = 0.3 | 0.8

## Two sensors each have noise {s} with correlation {r}.

::: part
answer: s*sqrt(2*(1 - r))
tolerance: 0.5%

### What is the noise of their difference?

::: misses
- [s*sqrt(2)] That ignores the correlation.
- [any] $\sqrt{2\sigma^2(1 - \rho)}$.
:::
:::

::: part
answer: s*sqrt((1 + r)/2)
tolerance: 0.5%

### What is the noise of their average?

::: misses
- [s/sqrt(2)] That's for uncorrelated noise; correlation keeps more of it.
- [any] $\sqrt{\frac{\sigma^2(1 + \rho)}{2}}$.
:::
:::
:::

::: check
kind: derivation
vars: mx = -3..3, my = -3..3, Exy = -10..10

## $X$ and $Y$ have means $m_x$ and $m_y$ and $E[XY] = E_{xy}$.

::: step
answer: Exy - mx*my

### What is their covariance?
:::

::: right
Right. $\text{Cov}(X, Y) = E[XY] - E[X]E[Y]$.
:::
:::

::: explain
## Why does a differential receiver reject noise that couples equally into both wires?

::: model
The receiver outputs the difference of the two wire voltages. Noise that couples into both wires alike shows up as nearly the same voltage on each, so the two noises are strongly positively correlated. The variance of a difference is $\sigma_1^2 + \sigma_2^2 - 2\text{Cov}$, and a large positive covariance cancels most of the two variances. The signal, by contrast, is driven in opposite directions on the two wires, so it doubles in the difference.
:::
:::

::: practice

::: check
given: s = 2 | 4; r = 0.2 | 0.6
answer: s^2*r
tolerance: 0.5%

## $X$ and $Y$ each have standard deviation {s}, with correlation {r}. What is their covariance?

::: misses
- [any] $\rho\sigma_X\sigma_Y$.
:::
:::

::: check
given: s = 1..20 int
answer: 2*s
tolerance: 0.5%

## Two noises of RMS {s} are perfectly correlated ($\rho = 1$). What is the RMS of their sum?

::: misses
- [s*sqrt(2)] With $\rho = 1$ they add directly.
- [any] $\sqrt{2\sigma^2 + 2\sigma^2}$.
:::
:::

::: check
given: s = 1 | 2; n = 4 | 9
answer: s/sqrt(n)
tolerance: 0.5%

## {n} independent readings, each with noise {s}, are averaged. What is the noise of the average?

::: misses
- [s/n] Variances add, so the standard deviation shrinks as $1/\sqrt n$.
- [any] $\sigma/\sqrt n$.
:::
:::
:::
