---
title: Probability densities
heading: Probability as area under a curve
eyebrow: A6 · Lesson 16 · Probability densities
xp: 40
---

::: hook
An ADC rounds every voltage to the nearest step, and the rounding error could be anything between minus half a step and plus half a step. What's the RMS of that error? A component's lifetime could be anything from an hour to decades. What's the chance it lasts five years? When a quantity can take any value in a range, probabilities come from a density curve, and every question about it is an integral. A11 builds this out fully; this lesson gives you the calculus.
:::

::: play
- The curve is an exponential density, $f(t) = re^{-rt}$, a common model for the time until a part fails. The shaded area between $a$ and $b$ is the probability that the failure falls in that window.
- Change the rate $r$. The total area under the curve stays exactly 1. Why must it?
- Compare the readout with $e^{-ra} - e^{-rb}$.
:::
:::

```sim plot
view: graph
x: t = 0..5
y: f = 0..3
slider: r = 0.3..3 step 0.1 = 1
slider: a = 0..2.5 step 0.1 = 0.5
slider: b = 2.5..5 step 0.1 = 3
plot: r*exp(-r*t) | label: f(t)
plot: r*exp(-r*t) | fill: a..b | color: ch2
readout: probability of the window = exp(-r*a) - exp(-r*b) | digits: 4
readout: mean time = 1/r | digits: 4
```

::: predict
## A density curve has a peak height of 3. Is that a problem?

- [x] No: the height isn't a probability; only areas are
- [ ] Yes: probabilities can't exceed 1
- [ ] Only if the variable is a voltage
- [ ] Yes, unless the curve is very narrow

::: reveal
No. A density is probability **per unit** of the variable, so its height can be anything as long as the total area is 1. A density of 3 per second over a window of 0.01 s gives a probability of about 0.03. Only areas under the curve are probabilities.
:::
:::

::: nameit
## Densities

A **probability density** $f(x)$ describes a quantity $X$ that can take any value in a range. It satisfies

$$f(x) \ge 0 \qquad \int_{-\infty}^{\infty}f(x)\,dx = 1$$

and the probability that $X$ lands between $a$ and $b$ is the area

$$P(a < X < b) = \int_a^b f(x)\,dx$$

## Mean and variance

The **mean** is the density-weighted average, exactly like the center of charge in lesson 14:

$$\mu = \int x\,f(x)\,dx$$

The **variance** measures spread: $\sigma^2 = \int(x - \mu)^2f(x)\,dx$. Its square root $\sigma$ is the standard deviation, which for a zero-mean error is the RMS.

## Three densities you'll meet constantly

| Density | $f(x)$ | Mean | Variance |
|---|---|---|---|
| Uniform on $[a, b]$ | $\frac{1}{b - a}$ | $\frac{a + b}{2}$ | $\frac{(b - a)^2}{12}$ |
| Exponential, rate $r$ | $re^{-rx}$ for $x \ge 0$ | $\frac{1}{r}$ | $\frac{1}{r^2}$ |
| Gaussian | $\frac{1}{\sigma\sqrt{2\pi}}e^{-(x - \mu)^2/2\sigma^2}$ | $\mu$ | $\sigma^2$ |

The exponential gives $P(X > t) = e^{-rt}$, the survival curve. The Gaussian's normalizing constant comes from $\int_{-\infty}^\infty e^{-x^2}dx = \sqrt{\pi}$, which has no antiderivative to evaluate; A9 proves it with a trick in polar coordinates.
:::

::: example
## A component's lifetime is exponential with a mean of 10 years. What's the chance it lasts at least 5 years?

::: solution
1. **Find the rate.** The mean is 10 years, so the rate is $r = 1/10$ per year.
2. **Integrate the tail.**
   $$\begin{aligned} P(X > 5) &= \int_5^\infty 0.1e^{-0.1x}\,dx \\ &= e^{-0.5} \\ &= 0.607 \end{aligned}$$
3. **Interpret.** That's about 61%. Notice the mean is 10 years, yet nearly 40% fail before 5: the exponential has many early failures and a long tail.

**Answer:** the chance is 0.607, about 61%.
:::
:::

::: example
## A 10-bit ADC spans 1 V. What is the RMS of its rounding error?

::: solution
1. **Step size.**
   $$\begin{aligned} q &= \frac{1\ \text{V}}{2^{10}} \\ &= 0.977\ \text{mV} \end{aligned}$$
2. **Variance.** The error is uniform on $[-q/2, q/2]$, so its variance is
   $$\begin{aligned} &\int_{-q/2}^{q/2}x^2\,\frac{1}{q}\,dx \\ &= \frac{1}{q}\cdot\frac{2}{3}\left(\frac{q}{2}\right)^3 \\ &= \frac{q^2}{12} \end{aligned}$$
3. **RMS.**
   $$\begin{aligned} \frac{q}{\sqrt{12}} &= \frac{0.977\ \text{mV}}{\sqrt{12}} \\ &= 0.282\ \text{mV} \end{aligned}$$
   a bit over a quarter of a step. S7 builds an ADC's signal-to-noise ratio from this.

**Answer:** the RMS rounding error is 0.282 mV, a bit over a quarter of a step.
:::
:::

::: example
## A 5% resistor's error is modeled as uniform from $-5\%$ to $+5\%$. What are its mean and standard deviation?

::: solution
1. **Recall the uniform density.** A uniform density on $[a, b]$ has height $\frac{1}{b - a}$, mean $\frac{a + b}{2}$ and variance $\frac{(b - a)^2}{12}$.
2. **Mean.** The range runs from $-5\%$ to $+5\%$, so the mean is 0.
3. **Standard deviation.** The width $b - a$ is 10%:
   $$\begin{aligned} \sigma &= \frac{10\%}{\sqrt{12}} \\ &= 2.89\% \end{aligned}$$
   Most parts are well inside the marked tolerance.

**Answer:** the mean is 0 and the standard deviation is 2.89%.
:::
:::

::: check
given: a = 3 | 4 | 5
answer: 2/a^2
tolerance: 0.5%

## $f(x) = cx$ on $[0, \given{a}]$ (and zero elsewhere) is a probability density. What must $c$ be?

::: right
Right. $\int_0^a cx\,dx = \frac{ca^2}{2} = 1$, so $c = \frac{2}{a^2}$ = {answer}.
:::

::: misses
- [1/a] That would make a uniform density. This one is a triangle.
- [1/a^2] The area of the triangle is $\frac{1}{2}ca^2$; include the half.
- [any] Not yet. Set the total area to 1.
:::
:::

::: check
given: m = 100 | 1000; t = 50 | 500
answer: exp(-t/m)
tolerance: 0.5%

## A part's lifetime is exponential with mean {m} hours. What is the probability it lasts more than {t} hours?

::: right
Right. $e^{-t/m}$ = {answer}.
:::

::: misses
- [1 - exp(-t/m)] That's the chance it fails before {t} hours.
- [any] Not yet. $P(X > t) = e^{-rt}$ with $r = 1/m$.
:::
:::

::: check
given: n = 1 | 2 | 3; let c = n + 1
answer: (n + 1)/(n + 2)
tolerance: 0.5%

## $f(x) = \given{c}x^{\given{n}}$ on $[0, 1]$. What is its mean?

::: right
Right. $\int_0^1 x(n + 1)x^n\,dx = \frac{n + 1}{n + 2}$ = {answer}.
:::

::: misses
- [1/2] That's the uniform density's mean. This one leans toward 1.
- [any] Not yet. $\mu = \int x f(x)\,dx$.
:::
:::

::: check
given: N = 8 | 10 | 12; V = 1 | 5
answer: V/2^N/sqrt(12)*1000
unit: mV
tolerance: 0.5%

## An {N}-bit ADC spans {V} V. What is the RMS of its rounding error?

::: right
Right. $q = V/2^N$, and the RMS of a uniform error is $q/\sqrt{12}$ = {answer}.
:::

::: misses
- [V/2^N/2*1000] That's the largest error, half a step. The RMS is smaller.
- [V/2^N*1000] That's a whole step.
- [any] Not yet. Variance $q^2/12$, so RMS $q/\sqrt{12}$.
:::
:::

::: check
given: r = 0.5 | 2; a = 0.2 | 1; b = 2 | 3
answer: exp(-r*a) - exp(-r*b)
tolerance: 0.5%

## For an exponential density with rate {r}, what is $P(\given{a} < X < \given{b})$?

::: right
Right. $\int_a^b re^{-rx}dx = e^{-ra} - e^{-rb}$ = {answer}.
:::

::: misses
- [exp(-r*b) - exp(-r*a)] The order of the terms: a probability can't be negative.
- [r*(b - a)] That treats the density as constant at its starting height.
- [any] Not yet. Integrate $re^{-rx}$ from {a} to {b}.
:::
:::

::: check
given: a = 2 | 6 | 10
answer: a^2/12
tolerance: 0.5%

## What is the variance of a uniform density on $[0, \given{a}]$?

::: right
Right. $\frac{1}{a}\int_0^a(x - a/2)^2dx = \frac{a^2}{12}$ = {answer}.
:::

::: misses
- [a^2/4] That's the largest squared distance from the mean. Average over all of them.
- [a/sqrt(12)] That's the standard deviation. The variance is its square.
- [any] Not yet. $\int(x - \mu)^2f(x)\,dx$ with $\mu = a/2$.
:::
:::

::: check
kind: parts
given: a = -5 | -1; b = 1 | 5; t = 0.25 | 0.5

## A quantity is uniform on $[\given{a}, \given{b}]$.

::: part
answer: (a + b)/2
tolerance: 0.5%

### What is its mean?

::: misses
- [any] The midpoint.
:::
:::

::: part
answer: (b - a)/sqrt(12)
tolerance: 0.5%

### What is its standard deviation?

::: misses
- [(b - a)/2] That's the half-width. The variance is $(b - a)^2/12$.
- [any] $\sqrt{(b - a)^2/12}$.
:::
:::

::: part
answer: 2*t
tolerance: 0.5%

### What is the probability of landing within $\given{t}(b - a)$ of the mean?

::: misses
- [any] A band of width $2t(b - a)$, out of a total width $b - a$.
:::
:::
:::

::: check
kind: derivation
vars: a = -5..0, b = 1..6

## Derive the variance of a uniform density on $[a, b]$.

::: step
answer: (a^2 + a*b + b^2)/3

### What is the mean of $x^2$, $\frac{1}{b - a}\int_a^b x^2\,dx$?
:::

::: step
answer: (b - a)^2/12
form: simplified

### Subtract the square of the mean, $\left(\frac{a + b}{2}\right)^2$, and simplify.
:::

::: right
Right. The variance is $\frac{(b - a)^2}{12}$, which is where an ADC's quantization noise of $\Delta/\sqrt{12}$ comes from.
:::
:::

::: explain
## Why is the mean of a density computed the same way as the center of charge on a rod?

::: model
Both are weighted averages of position. On a rod, each bit of length $dx$ holds charge $\lambda(x)\,dx$, and the center weights each position by that charge and divides by the total. For a density, each small range $dx$ holds probability $f(x)\,dx$, and the mean weights each value by its probability. The total probability is 1, so there's nothing left to divide by, but the integral $\int xf(x)\,dx$ is the same idea.
:::
:::

::: practice

::: check
given: a = 1 | 3; b = 4 | 7
answer: (a + b)/2

## What is the mean of a uniform density on $[\given{a}, \given{b}]$?

::: misses
- [any] The middle of the interval.
:::
:::

::: check
given: m = 1..20 int
answer: m*ln(2)
tolerance: 0.5%

## An exponential lifetime has mean {m} years. After how many years has half of a batch failed (the median)?

::: misses
- [m] The mean is longer than the median: the long tail pulls it out.
- [any] Solve $e^{-t/m} = \frac{1}{2}$.
:::
:::

::: check
given: c = 0.5 | 1 | 1.5 | 2 | 2.5 | 3
answer: 1 - exp(-c)
tolerance: 0.5%

## For an exponential lifetime, what is the probability of failing before {c} times the mean?

::: misses
- [exp(-c)] That's the chance of surviving past it.
- [any] $1 - e^{-c}$.
:::
:::
:::
