---
title: Functions of a random variable
heading: What happens to randomness when you square it, rectify it or divide by it
eyebrow: A11 · Lesson 17 · Functions of a random variable
xp: 40
---

::: hook
A voltage averages 2 V but wobbles with noise. The power it delivers to a 1 Ω load, $V^2$, averages more than 4 W. A full-wave rectifier fed pure noise with an average of zero puts out a positive average. And a simulator that can only draw uniform random numbers can still produce exponential lifetimes or Gaussian noise. All of these are about one question: if $X$ is random with a known distribution, what is the distribution of $g(X)$, and what is its mean?
:::

::: play
- $V$ is Gaussian with mean $m$ and standard deviation $s$. The histogram shows the power $P = V^2$ into 1 Ω; the line is the exact density.
- Compare the measured mean power with $m^2$, the power of the average voltage. Which is bigger, and by how much?
- Set $m = 0$: the voltage averages zero but the power doesn't.
:::
:::

```sim plot
view: random
x: P = 0..12 W
slider: m = 0..3 step 0.1 = 2 V
slider: s = 0.1..1.5 step 0.05 = 0.5 V
sample: (m + s*randn(1))^2 | mean: M
trials: 20000
bins: 60
density: (exp(-(sqrt(P) - m)^2/(2*s^2)) + exp(-(sqrt(P) + m)^2/(2*s^2)))/(2*s*sqrt(2*pi)*sqrt(P))
readout: mean power, measured, W = M | digits: 3
readout: m², W = m^2 | digits: 3
readout: m² + s², W = m^2 + s^2 | digits: 3
```

::: predict
## A voltage has mean 2 V and standard deviation 0.5 V. What is the average power it delivers to 1 Ω?

- [ ] 4 W, the power of the average voltage
- [x] 4.25 W
- [ ] 4.5 W
- [ ] 3.75 W

::: reveal
4.25 W. Power goes as the square, and $E[V^2] = \mu^2 + \sigma^2 = 4 + 0.25$. The fluctuations add power whichever way they go, because the square of a negative swing is positive. That's the variance formula from lesson 9 read backward, and it's why noise power adds to signal power.
:::
:::

::: nameit
## The mean of a function

For any function $g$,

$$E[g(X)] = \sum g(x_i)P(x_i) \qquad\text{or}\qquad \int g(x)\,p(x)\,dx$$

You don't need the distribution of $g(X)$ to find its mean. But in general $E[g(X)] \ne g(E[X])$: the average of the squares isn't the square of the average, $E[X^2] = \mu^2 + \sigma^2$. Only for straight lines, $g(x) = ax + b$, do the two agree.

## The distribution of a function

**Through the CDF.** Work with probabilities of ranges, then differentiate:

$$F_Y(y) = P(g(X) \le y)$$

For example, with $X$ uniform on $[0, 1]$ and $Y = X^2$: $P(Y \le y) = P(X \le \sqrt y) = \sqrt y$, so $p_Y = \frac{1}{2\sqrt y}$.

**Monotonic functions.** If $g$ is always increasing or always decreasing, probability in a small range $dx$ maps to a range $dy = |g'(x)|\,dx$, so

$$p_Y(y) = \frac{p_X(x)}{|g'(x)|} \quad\text{at } x = g^{-1}(y)$$

Where $g$ is steep, the probability gets spread thin; where it's flat, it piles up.

**Simulating any distribution.** If $U$ is uniform on $[0, 1]$, then $Y = F^{-1}(U)$ has CDF $F$. For the exponential, $Y = -\frac{\ln(1 - U)}{\lambda}$. That's how this course's sims draw lifetimes.

## Small errors: linearization

If $X$ varies only a little around $\mu$, a function is nearly a straight line there:

$$g(X) \approx g(\mu) + g'(\mu)(X - \mu) \qquad\Rightarrow\qquad \sigma_{g(X)} \approx |g'(\mu)|\,\sigma_X$$

This is the error propagation of A9.6 and of lesson 36, for one variable. In relative terms, a power law $g = cX^k$ has $\frac{\sigma_g}{g} \approx |k|\frac{\sigma_X}{X}$: a resonant frequency $\propto C^{-1/2}$ varies by half the capacitor's percentage spread.
:::

::: example
## Zero-mean Gaussian noise with standard deviation σ goes through an ideal full-wave rectifier. What is the average output?

::: solution
1. **Write the average.** The output is $|X|$, so its mean is
   $$E|X| = \int_{-\infty}^\infty |x|\frac{e^{-x^2/2\sigma^2}}{\sigma\sqrt{2\pi}}dx$$
2. **Fold onto the positive half.** The integrand is even, so
   $$\begin{aligned} E|X| &= 2\int_0^\infty x\frac{e^{-x^2/2\sigma^2}}{\sigma\sqrt{2\pi}}dx \\ &= \frac{2\sigma^2}{\sigma\sqrt{2\pi}} \\ &= \sigma\sqrt{\frac{2}{\pi}} \\ &= 0.798\sigma \end{aligned}$$
3. **Why meters misread noise.** An average-responding meter calibrated for sine waves would misread noise for this reason: the ratio of average to RMS differs between shapes.

**Answer:** the average output is $\sigma\sqrt{2/\pi} = 0.798\sigma$.
:::
:::

::: example
## A simulator draws $U$ uniform on $[0, 1)$ and returns $Y = -\ln(1 - U)/\lambda$. Show that $Y$ is exponential with rate $\lambda$.

::: solution
1. **Write the survival function.** For $y \ge 0$,
   $$\begin{aligned} P(Y > y) &= P(-\ln(1 - U) > \lambda y) \\ &= P(1 - U < e^{-\lambda y}) \\ &= e^{-\lambda y} \end{aligned}$$
   since $1 - U$ is also uniform on $(0, 1]$.
2. **Recognize it.** That's the exponential survival function, so $Y$ is exponential with rate $\lambda$.

**Answer:** $P(Y > y) = e^{-\lambda y}$ for $y \ge 0$, so $Y$ is exponential with rate $\lambda$.
:::
:::

::: example
## Gaussian noise of 1 V RMS appears across 50 Ω. What average power does it deliver?

::: solution
1. **Average the power.** The power is $\frac{V^2}{R}$, a function of the random voltage, so its mean is
   $$\begin{aligned} E[P] &= \frac{E[V^2]}{R} \\ &= \frac{\sigma^2}{R} \\ &= \frac{1}{50}\ \text{W} \\ &= 20\ \text{mW} \end{aligned}$$
2. **The fluctuations.** The instantaneous power fluctuates wildly (it's a scaled chi-square variable with one degree of freedom), but its mean depends only on the variance, whatever the distribution.

**Answer:** the noise delivers 20 mW on average.
:::
:::

::: check
given: m = 1 | 2 | 3; s = 0.5 | 2
answer: m^2 + s^2
tolerance: 0.5%

## $X$ has mean {m} and standard deviation {s}. What is $E[X^2]$?

::: right
Right. $\mu^2 + \sigma^2$ = {answer}.
:::

::: misses
- [m^2] That's the square of the mean. The spread adds to the mean square.
- [m^2 + s] Add the variance, $\sigma^2$.
- [any] Not yet. $\text{Var}(X) = E[X^2] - \mu^2$.
:::
:::

::: check
given: m = 5 | 10; s = 1 | 2; R = 10 | 50
answer: (m^2 + s^2)/R
unit: W
tolerance: 0.5%

## A voltage with mean {m} V and standard deviation {s} V drives a {R} Ω resistor. What is the average power?

::: right
Right. $\frac{E[V^2]}{R} = \frac{\mu^2 + \sigma^2}{R}$ = {answer}.
:::

::: misses
- [m^2/R] That ignores the power in the fluctuations.
- [any] Not yet. Average $V^2/R$.
:::
:::

::: check
given: s = 1 | 2 | 5
answer: s*sqrt(2/pi)
unit: mV
tolerance: 0.5%

## Zero-mean Gaussian noise of RMS {s} mV is full-wave rectified. What is the average of the output?

::: right
Right. $\sigma\sqrt{2/\pi}$ = {answer}.
:::

::: misses
- [s] That's the RMS. The average of $|X|$ is smaller.
- [0] Rectifying makes every value positive, so the average isn't zero.
- [any] Not yet. $E|X|$ for a Gaussian.
:::
:::

::: check
given: c = 2 | 5 | 10
answer: c/2
tolerance: 0.5%

## A capacitor's value has a spread of {c}%. By what percentage does $f_0 = \frac{1}{2\pi\sqrt{LC}}$ spread, from this alone?

::: right
Right. $f_0 \propto C^{-1/2}$, so the relative spread halves: {answer}%.
:::

::: misses
- [c] The square root halves the relative spread.
- [2*c] It's a square root, not a square.
- [any] Not yet. For $g = cX^k$, $\sigma_g/g \approx |k|\sigma_X/X$.
:::
:::

::: check
given: lam = 2 | 4; y = 0.5 | 1
answer: exp(-lam*y)
tolerance: 0.5%

## $U$ is uniform on $[0, 1)$ and $Y = -\ln(1 - U)/\given{lam}$. What is $P(Y > \given{y})$?

::: right
Right. $Y$ is exponential with rate {lam}: $e^{-\lambda y}$ = {answer}.
:::

::: misses
- [1 - exp(-lam*y)] That's $P(Y \le y)$.
- [any] Not yet. $Y > y$ when $1 - U < e^{-\lambda y}$.
:::
:::

::: check
given: y = 0.5 | 0.8 | 0.3
answer: y^2
tolerance: 0.5%

## $U_1$ and $U_2$ are independent and uniform on $[0, 1]$. What is the chance that the larger of the two is below {y}?

::: right
Right. Both must be below {y}: $y^2$ = {answer}.
:::

::: misses
- [y] Both values must be below {y}, and they're independent.
- [1 - (1 - y)^2] That's the chance the smaller one is below {y}.
- [any] Not yet. $P(\max \le y) = P(U_1 \le y)P(U_2 \le y)$.
:::
:::

::: check
kind: parts
given: s = 0.5 | 2; R = 50 | 75

## Gaussian noise of {s} V RMS appears across {R} Ω.

::: part
answer: s^2/R*1000
unit: mW
tolerance: 0.5%

### What average power does it deliver?

::: misses
- [s/R*1000] Power goes as the square of the voltage.
- [any] $\frac{E[V^2]}{R}$.
:::
:::

::: part
answer: s*sqrt(2/pi)
unit: V
tolerance: 0.5%

### What is the average of $|V|$?

::: misses
- [s] The mean absolute value of a Gaussian is $\sigma\sqrt{2/\pi}$.
- [any] $E|V| = \sigma\sqrt{2/\pi}$.
:::
:::
:::

::: check
kind: derivation
vars: l = 0.5..5, u = 0.01..0.99

## A simulator returns $Y = -\ln(1 - U)/l$ for $U$ uniform on $[0, 1)$.

::: step
answer: -ln(1 - u)/l

### What value does $U = u$ give?
:::

::: step
answer: 1 - exp(-l*(-ln(1 - u)/l))

### What is the exponential CDF $1 - e^{-ly}$ at that value?
:::

::: right
Right. It returns $u$: the inverse-CDF method makes $Y$ exponential with rate $l$.
:::
:::

::: explain
## Why is the average of $X^2$ larger than the square of the average of $X$?

::: model
Squaring bends upward: a swing above the mean raises the square by more than an equal swing below the mean lowers it. So the fluctuations, whichever way they go, push the average of the squares up. The amount is exactly the variance: $E[X^2] = \mu^2 + \sigma^2$. Only a straight-line function treats upward and downward swings symmetrically, which is why only for those does the average of $g(X)$ equal $g$ of the average.
:::
:::

::: practice

::: check
given: m = 3 | 6; a = 2 | 5; b = 1 | 4
answer: a*m + b
tolerance: 0.5%

## $X$ has mean {m}. What is the mean of $\given{a}X + \given{b}$?

::: misses
- [any] $aE[X] + b$.
:::
:::

::: check
given: y = 0.25 | 0.49 | 0.64
answer: sqrt(y)
tolerance: 0.5%

## $X$ is uniform on $[0, 1]$ and $Y = X^2$. What is $P(Y \le \given{y})$?

::: misses
- [y] $Y \le y$ means $X \le \sqrt y$.
- [any] $P(X \le \sqrt y)$.
:::
:::

::: check
given: k = 2 | 3; e = 1 | 2
answer: k*e
tolerance: 0.5%

## $Y = X^{\given{k}}$ and $X$ has a relative spread of {e}%. About what is the relative spread of $Y$, in percent?

::: misses
- [any] $|k|$ times the relative spread.
:::
:::
:::
