Search

Search papers and lessons

Total Variation Distance

Level 2 · 12 min read

Total variation distance is the largest gap that any event can show between two probability distributions, which equals half the sum of the absolute differences between their probabilities.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Information theory
  • ReadReady to readNot yet

TL;DR

Total variation distance measures how different two probability distributions are. Take any event, a set of outcomes, and compare the probability each distribution gives it. The largest such gap is the total variation distance, and it always equals half of the sum of the absolute differences between the two distributions. It solves the problem of asking "how far apart are these two models of the world?" with a single number between 0 and 1.

Builds on: Probability Mass Function, Probability Density Function, Supremum · Leads to: Kullback–Leibler Divergence (a different way to compare distributions, with different strengths)

Where it's used

Mixing of random walks. A random walk on a graph, such as a shuffled deck or a Markov chain used by a sampler, starts in one state and slowly forgets it. The standard way to measure how far it still is from its final, stationary behaviour is the total variation distance after tt steps. The mixing time is the first step tt at which that distance drops below 1/41/4. Sampling methods such as Markov Chain Monte Carlo are only trustworthy after they have mixed, so this number decides how long to run them.

Privacy. A privacy mechanism takes a dataset and releases a noisy answer. Consider two datasets that differ in one person's record. The total variation distance between the two output distributions measures how well an observer who sees one output can tell which dataset produced it. A small distance means the one person's record barely changes what anyone can see.

Comparing two models of data. Suppose a team has a simulator that generates synthetic transactions and wants to know how far its output distribution is from the real transactions. Each category, such as "card present" or "online", has a probability under each model. The total variation distance says what fraction of probability mass would have to move to turn one distribution into the other, which is easy to explain to a non-specialist.

How it works

Take two distributions over three outcomes, aa, bb and cc. Let PP be the first distribution and QQ the second:

OutcomePPQQ
aa0.50.2
bb0.30.3
cc0.20.5
  1. List the differences. For each outcome subtract QQ from PP: aa gives 0.5−0.2=0.30.5 - 0.2 = 0.3, bb gives 0.3−0.3=00.3 - 0.3 = 0, and cc gives 0.2−0.5=−0.30.2 - 0.5 = -0.3. Check: the differences add up to 0, because both distributions add up to 1.

  2. Take absolute values. Ignore the sign, since the question is how far apart the numbers are, not which is bigger: 0.30.3, 00, 0.30.3. Check: no value is negative now.

  3. Add them. The sum is 0.3+0+0.3=0.60.3 + 0 + 0.3 = 0.6. This is the "total" in total variation, and on its own it is twice the answer we want.

  4. Halve it. 0.6/2=0.30.6 / 2 = 0.3. So the total variation distance is 0.30.3.

  5. Check it against an event. Take the event A={a}A = \{a\}. The gap in probability is P(A)−Q(A)=0.5−0.2=0.3P(A) - Q(A) = 0.5 - 0.2 = 0.3. That matches the answer. The event {a,c}\{a, c\} gives 0.7−0.7=00.7 - 0.7 = 0, and no event gives a larger gap, which is what the definition promises.

The plot shows the same idea for two continuous distributions. Each curve is a normal distribution with standard deviation 1, one centred at 0 and one at 1. The total variation distance is half the area between the curves, because the shaded gap is what the two distributions disagree about.

−3−2−10123400.10.20.30.4xdensity
  • P: mean 0
  • Q: mean 1
The two curves overlap in the middle. Their total variation distance is about 0.383, half the area between them.

The math

Definition: Total variation distance

For two probability distributions PP and QQ on the same set of outcomes Ω\Omega, the total variation distance is

TV(P,Q)=sup⁡A⊆Ω∣P(A)−Q(A)∣,\mathrm{TV}(P, Q) = \sup_{A \subseteq \Omega} \big| P(A) - Q(A) \big|,

where the supremum runs over all events AA. For discrete distributions this equals

TV(P,Q)=12∑x∈Ω∣P(x)−Q(x)∣.\mathrm{TV}(P, Q) = \frac{1}{2} \sum_{x \in \Omega} \big| P(x) - Q(x) \big|.

For continuous distributions with densities pp and qq, replace the sum by an integral: TV(P,Q)=12∫∣p(x)−q(x)∣ dx\mathrm{TV}(P, Q) = \tfrac{1}{2} \int |p(x) - q(x)|\, dx.

Step 1 in symbols: the largest event gap

The definition takes a supremum over events. An event AA is any subset of outcomes, so P(A)P(A) is the sum of P(x)P(x) over the outcomes in AA. For the table above, the event {a}\{a\} achieves a gap of 0.30.3, and so does {c}\{c\} with the opposite sign. Either one attains the maximum.

Step 2 in symbols: the sum form

Split Ω\Omega into the outcomes where PP is larger, S+S_{+}, and the outcomes where QQ is larger, S−S_{-}. Since both distributions add to 1, the total gap on S+S_{+} equals the total gap on S−S_{-}:

∑x∈S+(P(x)−Q(x))=∑x∈S−(Q(x)−P(x)).\sum_{x \in S_{+}} \big(P(x) - Q(x)\big) = \sum_{x \in S_{-}} \big(Q(x) - P(x)\big).

Adding the two sides gives ∑x∣P(x)−Q(x)∣=2∑x∈S+(P(x)−Q(x))\sum_x |P(x) - Q(x)| = 2 \sum_{x \in S_{+}} (P(x) - Q(x)). The event S+S_{+} achieves the largest gap, so the supremum equals ∑x∈S+(P(x)−Q(x))\sum_{x \in S_{+}} (P(x) - Q(x)), which is half the absolute sum. This is the reason for the factor 12\tfrac{1}{2}. Step 1 and the sum form agree.

Step 3 in symbols: the Gaussian shift

For two normal distributions with the same standard deviation σ\sigma and means μ1\mu_1 and μ2\mu_2, the total variation distance depends only on the gap between the means. Writing δ=∣μ1−μ2∣\delta = |\mu_1 - \mu_2| and Φ\Phi for the cumulative distribution function of the standard normal:

TV=2 Φ ⁣(δ2σ)−1.\mathrm{TV} = 2\,\Phi\!\left(\frac{\delta}{2\sigma}\right) - 1.

With δ=1\delta = 1 and σ=1\sigma = 1 this gives 2 Φ(0.5)−1=2(0.6915)−1=0.3832\,\Phi(0.5) - 1 = 2(0.6915) - 1 = 0.383, which is the value in the plot.

Worked examples

Example
Level 1: a fair coin against a biased coin

Let PP be a fair coin, P=(0.5,0.5)P = (0.5, 0.5) for (heads, tails), and let QQ be a biased coin, Q=(0.7,0.3)Q = (0.7, 0.3).

  1. Differences: heads 0.5−0.7=−0.20.5 - 0.7 = -0.2, tails 0.5−0.3=0.20.5 - 0.3 = 0.2.
  2. Absolute values: 0.20.2 and 0.20.2.
  3. Sum: 0.40.4.
  4. Halve: 0.4/2=0.20.4 / 2 = 0.2.
  5. Event check: A={heads}A = \{\text{heads}\} gives ∣P(A)−Q(A)∣=∣0.5−0.7∣=0.2|P(A) - Q(A)| = |0.5 - 0.7| = 0.2. It matches.

Interpretation: the two coins disagree about a fifth of the probability mass. A distance of 0 would mean the coins are identical, and 1 would mean they never land on the same outcome.

Example
Level 2: uniform distributions on different intervals

Let PP be uniform on [0,1][0, 1], with density p(x)=1p(x) = 1 on [0,1][0, 1] and 0 elsewhere. Let QQ be uniform on [0,2][0, 2], with density q(x)=0.5q(x) = 0.5 on [0,2][0, 2] and 0 elsewhere.

  1. On [0,1][0, 1]: ∣p(x)−q(x)∣=∣1−0.5∣=0.5|p(x) - q(x)| = |1 - 0.5| = 0.5.
  2. On (1,2](1, 2]: ∣p(x)−q(x)∣=∣0−0.5∣=0.5|p(x) - q(x)| = |0 - 0.5| = 0.5.
  3. Outside [0,2][0, 2] both densities are 0, so the difference is 0.
  4. Integrate: ∫010.5 dx+∫120.5 dx=0.5+0.5=1\int_0^1 0.5\,dx + \int_1^2 0.5\,dx = 0.5 + 0.5 = 1.
  5. Halve: 12⋅1=0.5\tfrac{1}{2} \cdot 1 = 0.5.
  6. Event check: A=[0,1]A = [0, 1] gives P(A)=1P(A) = 1 and Q(A)=0.5Q(A) = 0.5, so the gap is 0.50.5. It matches.

Interpretation: half of QQ's probability sits where PP has none, and the two densities differ by 0.50.5 at every point of [0,2][0, 2]. The distance is 0.50.5, not 11, because the distributions still overlap on [0,1][0, 1].

Example
Level 3: how well one sample can tell two shifted normals apart

Let P=N(0,1)P = \mathcal{N}(0, 1) and Q=N(1,1)Q = \mathcal{N}(1, 1), the curves in the plot. Suppose you are shown a single value and must guess whether it came from PP or QQ, with equal chances that either is the source.

  1. Total variation distance from Step 3: TV=2 Φ(0.5)−1=0.383\mathrm{TV} = 2\,\Phi(0.5) - 1 = 0.383.
  2. The best rule is to guess QQ when the value is above 0.50.5 and PP otherwise, because that is where the two curves cross.
  3. Probability of a correct guess: 12(1+TV)=12(1.383)=0.6915\tfrac{1}{2}(1 + \mathrm{TV}) = \tfrac{1}{2}(1.383) = 0.6915.
  4. Cross-check with the Gaussian: Φ(0.5)=0.6915\Phi(0.5) = 0.6915. The two numbers agree.

Interpretation: a single observation gives you about a 69% chance of guessing right, and total variation distance is exactly the number that controls that accuracy. A distance of 0 would leave you at 50%, which is pure guessing.

Try it: a fair die is compared with a die that shows a 6 with probability 0.4 and each other face with probability 0.12. What is the total variation distance? Answer: the differences are 1/6−0.12≈0.04671/6 - 0.12 \approx 0.0467 for each of faces 1 to 5, and 0.4−1/6≈0.23330.4 - 1/6 \approx 0.2333 for face 6. The sum is 5×0.0467+0.2333≈0.46675 \times 0.0467 + 0.2333 \approx 0.4667, and half of that is about 0.2330.233. Check: the positive gaps (faces 1 to 5, total 0.23330.2333) equal the negative gap (face 6), as Step 2 requires.

Common mistakes and tradeoffs

Misconception: total variation distance is the same as the sum of absolute differences. It is tempting to stop at step 3 of the walkthrough. In fact the sum is twice the distance, because the positive gaps add up to the distance and the negative gaps add up to the same amount, and the sum counts both. Always halve it, or use the event form to check.

Misconception: total variation distance measures the difference in averages. It is tempting to think that two distributions with the same mean are close. In fact they can share a mean and still have total variation distance 1: a point mass at 0 and a coin that lands on −1-1 or +1+1 both have mean 0. The distance looks at the probability of every event, not at one summary. For any function ff with values between 0 and 1, the difference of expectations is bounded by the distance: ∣EPf−EQf∣≤TV(P,Q)|\mathbb{E}_P f - \mathbb{E}_Q f| \le \mathrm{TV}(P, Q).

Misconception: a small distance means a model is good. The distance tells you how far two distributions are from each other, not whether either matches reality. A simulator can be close to a poor model and far from the data.

When to use it. Use it when you need a bounded, symmetric measure with a clear meaning: the best achievable accuracy of a test, the mixing of a random process, or the largest disagreement over any event. It is a metric on distributions: it satisfies the triangle inequality, so it can be chained through intermediate distributions.

When not to use it. Avoid it when you need to detect small changes in the tails, or when you want a quantity that adds up over independent samples. Kullback–Leibler Divergence is more sensitive to differences in rare events and adds across independent samples, but it can be infinite when one distribution gives zero probability to an outcome that the other allows. Total variation distance is always finite.

QuestionTotal variationKL divergence
RangeBetween 0 and 10 to infinity
Symmetric?YesNo
Infinite when a probability is zero?NeverYes, if Q(x)=0Q(x) = 0 while P(x)>0P(x) \gt 0
InterpretationLargest gap over any eventExtra surprise from using QQ instead of PP

History

Total variation distance comes from the mathematics of measures: the name refers to the total variation of a function, which is the total size of its ups and downs. Probability theory adopted the idea for comparing distributions, and it became standard in the study of Markov chains, where it is the usual yardstick for how quickly a process settles down. The link to hypothesis testing, which the Level 3 example shows, has long been part of the same toolkit. The exact history of who first used each form is less clear than the way the idea is used today, so the names are better looked up in a textbook than trusted from memory.

Recap

  • A distribution assigns probabilities to outcomes, and an event is a set of outcomes whose probability is the sum over its members.
  • The total variation distance is the largest difference in probability that any event shows between two distributions.
  • Because both distributions sum to 1, that largest difference is exactly half the sum of absolute differences.
  • For continuous distributions the sum becomes an integral of the absolute difference of densities.
  • It controls the best accuracy of a single-sample test and the speed at which a Markov chain settles.

Read next: Kullback–Leibler Divergence, which measures the same gap in a different way and is the usual choice when rare events matter.

Check yourself

QuestionTwo distributions over three outcomes give the differences 0.10.1, −0.4-0.4 and 0.30.3 for P−QP - Q. What is the total variation distance?Show answer

Take absolute values: 0.10.1, 0.40.4, 0.30.3. The sum is 0.80.8. Half of that is 0.40.4. The check: the event that holds the largest positive difference is the set of outcomes where P>QP \gt Q, which gives 0.1+0.3=0.40.1 + 0.3 = 0.4. The two routes agree.

QuestionTwo distributions have total variation distance 0.25. What is the largest possible probability gap that any single event can show, and what does that mean for a test that tries to tell the two apart from one sample?Show answer

The largest gap over any event is exactly 0.25, since total variation distance is defined as that supremum. Under equal priors the best single-sample test is correct with probability 12(1+0.25)=0.625\tfrac{1}{2}(1 + 0.25) = 0.625. A distance of 0.25 therefore leaves a modest advantage over guessing.

QuestionWhy is the total variation distance always finite, while the KL divergence can be infinite?Show answer

Total variation distance only takes differences of probabilities, and each probability is between 0 and 1, so every term is bounded. KL divergence takes the logarithm of the ratio P(x)/Q(x)P(x)/Q(x). If Q(x)=0Q(x) = 0 while P(x)>0P(x) \gt 0, that ratio is infinite, so the sum is infinite.

QuestionA simulator's output and the real data differ in the proportion of each category, with total variation distance 0.05. A colleague says the simulator matches the data in 95% of cases. Is that right?Show answer

Almost, but the wording is off. A total variation distance of 0.05 means that the two distributions share at least 95% of their probability mass: at most 5% has to move to turn one into the other. It is not a count of individual cases that match, and it says nothing about whether the real data is the right target. Say "the distributions agree on at least 95% of probability mass".

Sources