Useful Probability facts

This page is a dumping ground for probability related facts for now. I never really studied probability in school and have been picking it up along the way, so I gathered the stuff that comes up most in one place. Useful to me for sure, and hopefully to someone else.

Distributions

Notation and basics

An event $A$ is a subset of the sample space $\Omega$, the set of all possible outcomes. We assign probabilities $P(A) \in [0,1]$ to events.

A random variable $X : \Omega \to \mathbb{R}$ is a function that gives a number to each outcome. It is discrete when it takes values in a countable set $\mathcal{X}$, and continuous when it takes values in $\mathbb{R}^d$. For a die, $\Omega = \{1,2,3,4,5,6\}$ and $X(\omega) = 1$ if $\omega$ is even, $0$ otherwise, maps outcomes to $\{0,1\}$.

The distribution of $X$ is described by one of three objects:

  • PMF (probability mass function), discrete: $\;p_X(x) = \Pr(X = x)$.
  • PDF (probability density function), continuous: $\;p_X(x)$ such that $\Pr(X \in A) = \int_A p_X(x)\, dx$.
  • CDF (cumulative distribution function), both: $\;F_X(x) = \Pr(X \le x)$.

Either way the mass has to sum to one,

$$\sum_x p(x) = 1 \quad \text{(discrete)}, \qquad \int_{-\infty}^{\infty} p(x)\, dx = 1 \quad \text{(continuous)}.$$
Notation. When the variable is clear from context we write $p(x)$ instead of $p_X(x)$. A subscript like $p_X$ is only there to say which variable the distribution is over, it is not a different function of $x$.
The demo below links two of these views: the shaded area under the PDF up to $x$ is exactly the height of the CDF at $x$. The distribution here is a two-mode mixture, so the CDF flattens between the modes where there is little mass.
mode separation 2.50 x 0.90

Joint, marginal, and conditional

With two variables the joint distribution $p(x,y)$ holds everything. From it we recover one variable by summing (or integrating) out the other, which is the marginal,

$$p(x) = \sum_y p(x,y) \qquad \text{or} \qquad p(x) = \int p(x,y)\, dy,$$

and we condition on a known value with the conditional,

$$p(x \mid y) = \frac{p(x,y)}{p(y)}, \qquad p(y) > 0.$$

Dividing by $p(y)$ makes $p(x \mid y)$ a full distribution in $x$ that integrates to 1:

$$\int p(x \mid y)\, dx = \frac{1}{p(y)} \int p(x,y)\, dx = \frac{p(y)}{p(y)} = 1.$$

Rearranging the conditional gives the product rule $p(x,y) = p(x \mid y)\,p(y) = p(y \mid x)\,p(x)$, and equating the two factorings gives Bayes' rule, which flips the conditioning direction:

$$p(x \mid y) = \frac{p(y \mid x)\,p(x)}{p(y)}, \qquad p(y) = \sum_x p(y \mid x)\,p(x).$$

Two variables are independent, written $X \perp Y$, when the joint factors into the marginals, which is the same as saying conditioning on $Y$ tells us nothing about $X$:

$$p(x,y) = p(x)\,p(y) \quad\Longleftrightarrow\quad p(x \mid y) = p(x).$$

They are conditionally independent given $Z$, written $X \perp Y \mid Z$, when the same factoring holds once $Z$ is fixed:

$$p(x,y \mid z) = p(x \mid z)\,p(y \mid z) \quad\Longleftrightarrow\quad p(x \mid y, z) = p(x \mid z).$$
Below, the joint is the heatmap, the marginal $p(x)$ is the fixed curve on top, and the conditional $p(x \mid y)$ is the highlighted row renormalized into a density. Moving $y$ shifts the conditional mean to $\rho y$ while the marginal $p(x)$ does not change.
ρ 0.60 slice y 1.00
Expectation & estimators

Expectation

The expectation of $X$ is its probability-weighted average,

$$\mathbb{E}[X] = \sum_x x\, p(x) \qquad \text{or} \qquad \mathbb{E}[X] = \int x\, p(x)\, dx,$$

and for any function $f$ we average $f$ against the same distribution,

$$\mathbb{E}[f(X)] = \sum_x f(x)\, p(x).$$

A subscript on the expectation says which distribution we are averaging over, so $\mathbb{E}_{X \sim p}[\,\cdot\,]$ or just $\mathbb{E}_X[\,\cdot\,]$ when $p$ is clear. This matters once several distributions are floating around. Expectation is linear, for constants $a, b$,

$$\mathbb{E}[a X + b Y] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y],$$

and this holds whether or not $X$ and $Y$ are independent.

Variance and covariance

The variance measures how far $X$ spreads around its mean $\mu = \mathbb{E}[X]$, as an average squared deviation,

$$\operatorname{Var}(X) = \mathbb{E}\!\big[(X - \mu)^2\big].$$

Expanding the square and using linearity gives the form we actually compute with,

$$\operatorname{Var}(X) = \mathbb{E}[X^2 - 2\mu X + \mu^2] = \mathbb{E}[X^2] - 2\mu\,\mathbb{E}[X] + \mu^2 = \mathbb{E}[X^2] - \mathbb{E}[X]^2.$$

A constant shift moves the mean along with $X$ and leaves the spread alone, while a scale factor pulls out squared, so

$$\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X).$$

The covariance is the same idea for two variables, the average product of their deviations,

$$\operatorname{Cov}(X, Y) = \mathbb{E}\!\big[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])\big] = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y],$$

so $\operatorname{Var}(X) = \operatorname{Cov}(X, X)$. Expanding $\mathbb{E}[((X - \mathbb{E}X) + (Y - \mathbb{E}Y))^2]$ the same way gives the variance of a sum,

$$\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\operatorname{Cov}(X, Y).$$

When $X$ and $Y$ are independent the cross term $\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y]$ cancels, so $\operatorname{Cov}(X, Y) = 0$ and variances simply add. The converse is not true, zero covariance does not imply independence. Dividing the covariance by the two standard deviations rescales it into the correlation $\rho = \operatorname{Cov}(X,Y) / (\sigma_X \sigma_Y) \in [-1, 1]$.

Averaging shrinks variance. For $n$ independent draws $X_1, \dots, X_n$ each with variance $\sigma^2$, the sample mean $\bar{X} = \frac{1}{n}\sum_i X_i$ has variance $\operatorname{Var}(\bar{X}) = \frac{1}{n^2}\sum_i \operatorname{Var}(X_i) = \frac{\sigma^2}{n}$. Averaging $n$ independent estimates cuts the variance by $n$, the reason a Monte Carlo estimate tightens as you draw more samples.
Below, the density of the sample mean $\bar{X}$ narrows as $n$ grows, next to one concrete draw of $n$ samples and their average.
n 8

Central limit theorem (CLT)

The CLT is a powerful result in statistics: the average of a variable drawn from any distribution is itself Gaussian. Put simply, take many independent draws, average them, and treat that average as a variable: that variable is Gaussian no matter what you drew from. For $X_1, \dots, X_n$ independent with mean $\mu$ and variance $\sigma^2$, the sample mean $\bar{X} = \frac{1}{n}\sum_{i} X_i$ is the variable, and

$$\frac{\bar{X} - \mu}{\sigma / \sqrt{n}} \;\xrightarrow{\; n \to \infty \;}\; \mathcal{N}(0, 1).$$

The CLT is one of the main reasons Gaussians are so ubiquitous in nature.

Pick a source distribution (top strip) and a sample size $n$. The histogram is the standardized mean $(\bar{X} - \mu)/(\sigma/\sqrt{n})$ collected over many repeated draws, against the standard normal in amber. At $n = 1$ it is just the source; raise $n$ and any of them settle into the same Gaussian.
n 1

Jensen's inequality

For a convex function $\phi$ and any random variable $X$, the average of the function is at least the function of the average,

$$\phi\big(\mathbb{E}[X]\big) \;\le\; \mathbb{E}\big[\phi(X)\big].$$

If $\phi$ is concave the inequality goes the other way. It is an equality when $\phi$ is linear over the range of $X$, or when $X$ is constant.

Here is why it holds. A convex function lies above each of its tangent lines, so at any point $m$ there is a slope $g$ (a subgradient, the ordinary derivative $\phi'(m)$ when it exists) with

$$\phi(x) \;\ge\; \phi(m) + g\,(x - m) \qquad \text{for all } x.$$

Take $m = \mathbb{E}[X]$ and put $x = X$, so the bound holds for the random variable itself,

$$\phi(X) \;\ge\; \phi\big(\mathbb{E}[X]\big) + g\,\big(X - \mathbb{E}[X]\big).$$

Now take expectations of both sides. The right-hand side is linear in $X$, and $\mathbb{E}\big[X - \mathbb{E}[X]\big] = 0$, so the slope term vanishes and what remains is

$$\mathbb{E}\big[\phi(X)\big] \;\ge\; \phi\big(\mathbb{E}[X]\big).$$
Take a two-point $X$: value $a$ with probability $1-t$, value $b$ with probability $t$, so $\mathbb{E}[X] = (1-t)a + tb$. The chord over that same mix has height $(1-t)\phi(a) + t\phi(b) = \mathbb{E}[\phi(X)]$, so reading the chord at $\mathbb{E}[X]$ gives $\mathbb{E}[\phi(X)]$. A convex curve sits below its chords, so $\phi(\mathbb{E}[X]) \le \mathbb{E}[\phi(X)]$. The $\sqrt{x}$ button shows the concave case, where the curve is above and the inequality reverses.
spread s 1.00 weight t 0.30
Expectation of max. The function $\max(x_1, \dots, x_n)$ is a pointwise maximum of linear functions, so it is convex. Jensen then gives, for any random $X_1, \dots, X_n$, $$\mathbb{E}\big[\max_i X_i\big] \;\ge\; \max_i \mathbb{E}[X_i].$$ Taking the max of noisy estimates overshoots the max of their true means.
Each gray bar is a true value $\mu_i = \mathbb{E}[X_i]$ you cannot see; each dot is a noisy reading $X_i = \mu_i + \varepsilon_i$. We keep the largest reading, $\max_i X_i$ (circled). Over many draws that max settles on the solid line, $\mathbb{E}[\max_i X_i]$. The gap between $\max_i \mathbb{E}[X_i]$ (dashed) and $\mathbb{E}[\max_i X_i]$ is the overestimation exposed by Jensen's inequality. The wider the noise the larger it is.
actions n 8 noise σ 0.60

Law of total expectation

This is used pretty much everywhere and follows directly from the definition of expectation. Start from the average of $f$ over the joint distribution, and split the joint with the product rule $p(x,y) = p(x \mid y)\,p(y)$:

$$\mathbb{E}_{X,Y}\!\big[f(X,Y)\big] = \sum_{x,y} f(x,y)\, p(x,y) = \sum_{y} p(y) \sum_{x} f(x,y)\, p(x \mid y).$$

The inner sum $\sum_x f(x,y)\, p(x \mid y)$ is by definition the conditional expectation $\mathbb{E}_{X \mid Y}[f(X,y) \mid Y = y]$, a function of $y$ alone. The outer sum then averages that over $p(y)$, which is an expectation over $Y$. Reassembled,

$$\mathbb{E}_{X,Y}\!\big[f(X,Y)\big] = \mathbb{E}_{Y}\!\left[\, \mathbb{E}_{X \mid Y}\!\big[\, f(X,Y) \mid Y\,\big] \,\right].$$

Reading the result inside-out:

  • The inner expectation $\mathbb{E}_{X \mid Y}[\,\cdot \mid Y]$ averages over the conditional distribution of $X$ given $Y$, producing a function of $Y$ alone.
  • The outer expectation $\mathbb{E}_{Y}[\,\cdot\,]$ averages that result over the marginal distribution of $Y$.

Written out, the outer average is just a sum (or integral) over $y$ weighted by how likely each $y$ is:

$$\mathbb{E}_{X,Y}\!\big[f(X,Y)\big] = \sum_y \mathbb{E}_{X \mid Y}\!\big[f(X,y) \mid Y = y\big]\, P_Y(Y = y),$$ $$\mathbb{E}_{X,Y}\!\big[f(X,Y)\big] = \int_{-\infty}^{\infty} \mathbb{E}_{X \mid Y}\!\big[f(X,y) \mid Y = y\big]\, p_Y(y)\, dy.$$
Why it helps. Whenever a quantity depends on two sources of randomness, this lets us deal with one at a time: fix $Y$, average over $X$, then average over $Y$.

Bias and variance of an estimator

Suppose $\hat{\theta}$ is an estimate of some fixed quantity $\theta$, computed from random data so $\hat{\theta}$ is itself random. Two things can make it wrong. It can be off on average, and it can be noisy. The bias is the average error $\operatorname{Bias}(\hat{\theta}) = \mathbb{E}[\hat{\theta}] - \theta$, and the variance is the noise around that average.

Write the mean $m = \mathbb{E}[\hat{\theta}]$ and split the error into a noise part $\hat{\theta} - m$ and a bias part $m - \theta$:

$$\hat{\theta} - \theta = (\hat{\theta} - m) + (m - \theta).$$

Squaring gives three terms,

$$(\hat{\theta} - \theta)^2 = (\hat{\theta} - m)^2 + 2(\hat{\theta} - m)(m - \theta) + (m - \theta)^2.$$

Let's take the expectation of each. The first term is $\operatorname{Var}(\hat{\theta})$ by definition. In the middle term $m - \theta$ is a constant, so we can pull it out by linearity, leaving $2(m - \theta)\,\mathbb{E}[\hat{\theta} - m]$. That last factor is zero, since $\mathbb{E}[\hat{\theta} - m] = \mathbb{E}[\hat{\theta}] - m = m - m = 0$, so the cross term vanishes. The last term is already constant. What is left is

$$\mathbb{E}\!\big[(\hat{\theta} - \theta)^2\big] = \underbrace{\operatorname{Var}(\hat{\theta})}_{\text{noise}} + \underbrace{\big(\mathbb{E}[\hat{\theta}] - \theta\big)^2}_{\text{bias}^2}.$$

So the expected squared error is variance plus bias squared, which is a natural tradeoff. It works a bit like a waterbed: pushing one down tends to push the other up. You can think of it as a small Pareto problem, where whoever designs the estimator has to decide how much bias and how much variance they can tolerate.

The bias slider slides the whole cloud of estimates off the target, the noise slider spreads it out, and the bar shows how each one adds up. They move on their own, which is the point: an estimator can be off on average, noisy, or both.
bias ‖b‖ 0.80 noise σ 0.40

Least squares gives the conditional mean

For a squared-error prediction of the form

$$\mathcal{R}(f) = \mathbb{E}_{X,Y}\!\big[\, (Y - f(X))^2 \,\big],$$

the conditional mean $\mathbb{E}[Y \mid X = x]$ is the best predictor.

To see this, look at one input $x$ at a time. There the prediction is just a number, $c = f(x)$, and the question becomes: which number $c$ has the smallest expected squared distance to $Y$? Call $\mu = \mathbb{E}[Y \mid x]$ and expand the error around it:

$$(Y - c)^2 = (Y - \mu)^2 + 2(Y - \mu)(\mu - c) + (\mu - c)^2.$$

Take the expectation over $Y$ with $x$ held fixed. Then $\mu$ and $c$ are both just numbers, so the factor $\mu - c$ pulls out of the middle term, which vanishes because $\mathbb{E}[Y - \mu \mid x] = 0$, leaving

$$\mathbb{E}\!\big[(Y - c)^2 \mid x\big] = \mathbb{E}\!\big[(Y - \mu)^2 \mid x\big] + (\mu - c)^2.$$

Only the last term involves $c$, and it is smallest at zero, so the best number is $c = \mu$. The first term is the spread of $Y$ around its mean at $x$, the noise floor no prediction can remove. And since the full risk is just these per-$x$ risks averaged over $x$,

$$\mathcal{R}(f) = \mathbb{E}_X\!\Big[\, \mathbb{E}\!\big[(Y - f(X))^2 \mid X\big] \,\Big],$$

making the best choice at every $x$ minimizes the whole thing. The minimizer is the regression function

$$f^\star(x) = \mathbb{E}[Y \mid X = x].$$
Below, pick a slice $x_0$ and move the prediction $c$. The conditional risk on the right is a parabola in $c$ with its minimum at $\mathbb{E}[Y \mid x_0]$, on the regression curve, and the leftover risk at the bottom is the noise variance $\sigma^2$ that no prediction can remove.
slice x₀ 0.60 prediction c 0.80
Densities & gradients

Log-density

A lot of the time we work with the log of a density, $\log p(x)$, instead of the density itself. It is the log of the PDF evaluated at a point $x$, so it is a number attached to that point, not the log of a sample. A few reasons it shows up everywhere:

  • Products become sums. Independent variables have $p(x, y) = p(x)\,p(y)$, so $\log p(x,y) = \log p(x) + \log p(y)$. A sum is easier to differentiate, and it does not underflow the way a long product of small probabilities does.
  • Many objectives are written with it: maximum likelihood maximizes $\sum \log p$, entropy is $\mathcal{H}(p) = -\mathbb{E}[\log p]$, the score function is $\nabla_\theta \log p_\theta$, and KL divergences are expectations of log-ratios.
  • $\log$ is monotonic, so maximizing $\log p$ maximizes $p$. We get the easier algebra without moving the optimum.

For a scalar Gaussian the density is $\mathcal{N}(x \mid \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\big(-(x-\mu)^2 / 2\sigma^2\big)$, so its log is a quadratic in $x$ plus a constant:

$$\log \mathcal{N}(x \mid \mu, \sigma^2) = -\frac{(x-\mu)^2}{2\sigma^2} - \log \sigma - \tfrac{1}{2}\log(2\pi).$$

It is an upside-down parabola: largest at $x = \mu$ and falling off quadratically away from it, with the $-\log\sigma - \tfrac{1}{2}\log(2\pi)$ term shifting the curve so the density integrates to one. For a diagonal Gaussian over $\mathbb{R}^d$ (independent coordinates) the log-density is just the sum over dimensions,

$$\log \mathcal{N}(x \mid \mu, \sigma^2) = -\sum_{i=1}^{d} \Big[ \frac{(x_i - \mu_i)^2}{2\sigma_i^2} + \log \sigma_i \Big] - \frac{d}{2}\log(2\pi).$$

Score function estimator

We often need the gradient of an expectation whose distribution itself depends on the parameter,

$$\nabla_\theta\, \mathbb{E}_{x \sim p_\theta}\big[\, g(x) \,\big].$$

We cannot push the gradient inside, because the distribution $p_\theta$ we average over moves with $\theta$. Writing the expectation as an integral and differentiating, only $p_\theta$ depends on $\theta$:

$$\nabla_\theta\, \mathbb{E}_{x \sim p_\theta}\big[g(x)\big] = \nabla_\theta \int p_\theta(x)\, g(x)\, dx = \int \nabla_\theta p_\theta(x)\, g(x)\, dx.$$

The log-derivative trick $\nabla_\theta \log p_\theta(x) = \nabla_\theta p_\theta(x) / p_\theta(x)$ lets us put a $p_\theta(x)$ back in front, which turns the integral into an expectation again:

$$\nabla_\theta p_\theta(x) = p_\theta(x)\, \nabla_\theta \log p_\theta(x),$$ $$\nabla_\theta\, \mathbb{E}_{x \sim p_\theta}\big[g(x)\big] = \int p_\theta(x)\, \nabla_\theta \log p_\theta(x)\, g(x)\, dx = \mathbb{E}_{x \sim p_\theta}\big[\, g(x)\, \nabla_\theta \log p_\theta(x) \,\big].$$

This is the score function estimator, also called the likelihood-ratio estimator, and $\nabla_\theta \log p_\theta(x)$ is the score. It only needs to sample $x$ and to differentiate $\log p_\theta$, so it works even when $g$ is not differentiable in $x$, discrete $x$ included. The price is variance: it sees the value of $g$ at sampled points and the score, not the shape of $g$.

The score has mean zero. $\mathbb{E}_{x \sim p_\theta}[\nabla_\theta \log p_\theta(x)] = \int \nabla_\theta p_\theta(x)\, dx = \nabla_\theta \int p_\theta(x)\, dx = \nabla_\theta 1 = 0$. So subtracting any constant $b$ from $g$ leaves the expected gradient unchanged, $\mathbb{E}[(g(x) - b)\, \nabla_\theta \log p_\theta(x)] = \mathbb{E}[g(x)\, \nabla_\theta \log p_\theta(x)]$. Picking $b$ near the average of $g$ shrinks the variance, which is the baseline idea.

The baseline leaves the mean alone but does change the variance. Write the score as $s(x) = \nabla_\theta \log p_\theta(x)$. Since $\mathbb{E}[s] = 0$, the variance of a single term is

$$\mathrm{Var}\big[(g - b)\,s\big] = \mathbb{E}[g^2 s^2] - 2b\,\mathbb{E}[g\,s^2] + b^2\,\mathbb{E}[s^2] - \big(\mathbb{E}[g\,s]\big)^2,$$

a parabola in $b$ opening upward, since the leading coefficient $\mathbb{E}[s^2]$ is nonnegative (it is the Fisher information). Setting the derivative to zero, the variance-minimizing baseline is

$$b^\star = \frac{\mathbb{E}[g\,s^2]}{\mathbb{E}[s^2]},$$

a score-squared-weighted average of $g$, not the plain mean $\mathbb{E}[g]$. The two agree only when $g$ is uncorrelated with $s^2$. In practice people still use the mean of $g$ (or of the returns) as the baseline, since it is cheap and usually close enough, but it is not the true minimizer.

Here $p_\theta = \mathcal{N}(\theta, 1)$ sampled at $\theta = 0$, so $x \sim \mathcal{N}(0, 1)$. The score is the gradient in the mean parameter, $\nabla_\theta \log p_\theta(x) = x - \theta$, so $s(x) = x$ at $\theta = 0$. Take $g(x) = x^2 + 1$. Moving the baseline changes each term $(g(x) - b)\,x$ but not their average. Using the moments $\mathbb{E}[x^2] = 1$, $\mathbb{E}[x^4] = 3$, $\mathbb{E}[x^6] = 15$, the variance is $22 - 8b + b^2$, a parabola minimized at $b^\star = 4$, while $\mathbb{E}[g] = 2$. The mean of $g$ is the cheap default and it is often close enough, but it is not the true variance minimizer.
baseline b 0.00

Reparametrization trick

Here the goal is to differentiate through a random sample. The expectation $\mathbb{E}_{x \sim p_\theta}[g(x)]$ averages over a stochastic $x$ whose distribution depends on $\theta$, and we want its gradient in $\theta$. As with the score function, we cannot push $\nabla_\theta$ straight inside, because the randomness we average over moves with $\theta$. The reparametrization trick gets around this by moving the $\theta$-dependence out of the sampling step and into a deterministic function. For a Gaussian $p_\theta = \mathcal{N}(\mu_\theta, \sigma_\theta^2)$, write

$$x = \mu_\theta + \sigma_\theta\, \epsilon, \qquad \epsilon \sim \mathcal{N}(0, I).$$

Now the randomness lives entirely in $\epsilon$, drawn from a fixed distribution with no $\theta$ in it, and $\theta$ only enters through the deterministic map $x = \mu_\theta + \sigma_\theta\, \epsilon$. Since the distribution we average over ($\epsilon$'s) no longer depends on $\theta$, the gradient passes straight through the expectation and onto the sample:

$$\nabla_\theta\, \mathbb{E}_{x \sim p_\theta}\big[\, g(x) \,\big] = \mathbb{E}_{\epsilon \sim \mathcal{N}(0, I)}\big[\, \nabla_\theta\, g(\mu_\theta + \sigma_\theta\, \epsilon) \,\big].$$

With one noise sample per term this is a plain average of gradients. By the chain rule each term is $\nabla_x g \cdot \nabla_\theta(\mu_\theta + \sigma_\theta\, \epsilon)$, so it backpropagates through the sampled $x$. The one requirement is that $g$ be differentiable in $x$, so this works for continuous variables, not discrete ones.

Lower variance than the score function. Both are unbiased estimators of the true gradient $\nabla_\theta\, \mathbb{E}[g(x)]$ and share the same mean. They differ in the single-sample term. The score function weights the value of $g$ by the score, $$\nabla_\theta\, \mathbb{E}_{x \sim p_\theta}\big[\, g(x) \,\big] = \mathbb{E}\big[\, g(x)\, \nabla_\theta \log p_\theta(x) \,\big],$$ so a term sees only the height $g(x)$ and how likely the point was. The reparametrized term uses the slope $\nabla_x g$ instead, which is more informative, so it usually has the lower variance.
Here $g(x) = x^2$ with $x \sim \mathcal{N}(\mu, \sigma^2)$ at $\mu = 1$. We want the gradient in the mean $\mu$, with $\sigma$ held fixed, so the true value is $\nabla_\mu \mathbb{E}[x^2] = 2\mu = 2$, where both histograms center. Writing $x = \mu + \sigma\varepsilon$, each score term is $g(x)\,\frac{x - \mu}{\sigma^2} = x^2\,\frac{\varepsilon}{\sigma}$ and each reparametrized term is $\frac{d}{d\mu} g(\mu + \sigma\varepsilon) = 2x$. Both use the same fixed set of $\varepsilon$ draws. The score histogram is wide, the reparametrized one is narrow.
σ 1.00

Change of variables

If $u$ has density $p_u$ and we pass it through a smooth, monotone map $a = f(u)$, what is the density of $a$? The rule comes from conserving probability mass: the chance that $u$ falls in a small interval must equal the chance that $a$ falls in the image of that interval. Writing those two small intervals as $|du|$ and $|da|$,

$$p_a(a)\, |da| = p_u(u)\, |du|.$$

Dividing by $|da|$ gives the density of $a$,

$$p_a(a) = p_u(u) \left| \frac{du}{da} \right| = \frac{p_u(u)}{|f'(u)|}, \qquad u = f^{-1}(a),$$

using $da/du = f'(u)$ for the last step. The $|f'(u)|$ factor accounts for how much $f$ stretches or compresses the line near $u$: where $f$ is steep, the same mass is spread over a wider range of $a$, so the density there drops. In several dimensions $f'(u)$ becomes the Jacobian of $f$ and $|f'(u)|$ its absolute determinant.

Here $u \sim \mathcal{N}(0, 1)$ (bottom) is pushed through the monotone map $a = f(u) = u + c\,\sin(u)$ (curve) into a density over $a$ (left axis). The slope $f'(u) = 1 + c\,\cos(u)$ alternates between stretching and compressing the line. The shaded slice of mass around $u_0$ lands on the shaded slice on the $a$ axis: same mass, different width, so the height rescales by $1/|f'(u_0)|$. At $c = 0$ the map is the identity and the density passes through unchanged.
u₀ 0.80 bend c 0.60

Squashed Gaussian

Sometimes we want a distribution over a bounded interval, say $(-1, 1)$, while keeping the Gaussian's easy reparametrization. The squashed Gaussian samples a Gaussian and then pushes the sample through $\tanh$:

$$u \sim \mathcal{N}(\mu_\theta, \sigma_\theta^2), \qquad a = \tanh(u).$$

Since $\tanh$ maps $\mathbb{R} \to (-1, 1)$, every sample lands in range, and the map is smooth so the reparametrization trick still passes gradients through it. Clipping a Gaussian to the interval would instead give zero gradient wherever it was clipped, which is why the smooth squash is preferred.

Passing $u$ through the nonlinear $\tanh$ stretches the density by different amounts at different points, so we correct for it with the change of variables above. The derivative of $\tanh$ is $f'(u) = 1 - \tanh^2(u)$, which gives

$$p(a) = \frac{p(u)}{|f'(u)|} = \frac{\mathcal{N}(u \mid \mu_\theta, \sigma_\theta^2)}{1 - \tanh^2(u)},$$

where $1 - \tanh^2 u \ge 0$ so the absolute value drops. Taking logs turns the division into a subtraction, and for a multi-dimensional action with independent coordinates the per-coordinate corrections add up:

$$\log p(a) = \log \mathcal{N}(u \mid \mu_\theta, \sigma_\theta^2) - \sum_i \log\big(1 - \tanh^2(u_i)\big),$$

where implementations add a small constant inside the log for numerical stability. This is the density used whenever an entropy or log-probability term is needed.

Below, the Gaussian over $u$ (bottom) is pushed through $\tanh$ (curve) into a density over $a$ (left axis). Increase $\mu$ or $\sigma$ and the mass accumulates near the boundary, where the correction $1 - \tanh^2(u)$ is small and the density grows large.
μ 1.00 σ 1.00
On this page