A first-principles guide to conditional expectations, signal-to-noise ratios, moments, and the elegant structure of probability
An interactive tutorial building intuition for the mathematical machinery underlying statistics, information theory, and computational neuroscience
This tutorial builds an intuitive and rigorous understanding of probability theory from the ground up. We start with the question “what is a random variable?” and end at cumulants and the Law of Total Cumulants — a powerful generalization that reveals deep structure in how information decomposes.
The central thread is the signal-to-noise ratio: given two random variables X and Y, how much does knowing X tell us about Y? Answering this question requires us to build up conditional expectations, conditional variance, and the Law of Total Variance.
A common misconception is that a random variable is a function that returns probabilities. That’s actually a probability mass function (PMF) or probability density function (PDF). A random variable is something more fundamental.
A probability space together with a random variable has four ingredients:
Sample space: Ω = {⚀, ⚁, ⚂, ⚃, ⚄, ⚅}
X(ω) = number showing
X(⚀) = 1, X(⚁) = 2, …
P({⚀}) = 1/6 for a fair die
The random variable X does not return a probability — it returns a value.
The probability comes from P, which is a separate entity.
You can define multiple random variables on the same sample space, each extracting different information:
The expectation (or expected value) of a discrete random variable X is the probability-weighted average of all its possible values:
Here the sum runs over all possible values x that X can take, and p(x) = P(X = x) is the probability of each value.
More generally, for any function g(X):
This is where things get nuanced. There are two related but distinct concepts sharing very similar notation:
For discrete variables and a value x with P(X=x) > 0, this is the expected value of Y given that X takes that specific, fixed value:
The sum is over all values of y (not x!), weighted by the conditional probability of y given x. This produces a single number.
This is a function of X. Since X is random, E[Y|X] is also random. It takes the value E[Y|X=x] when X = x. As usual with conditional expectations, its values on events of probability zero can be chosen without changing the random variable probabilistically.
Formally, define:
| Notation | Type | Common Name |
|---|---|---|
| E[Y|X=x] | A number | Conditional expectation of Y given X equals x |
| E[Y|X] | A random variable | Conditional expectation of Y given X (or: the conditional expectation function) |
E[X] gives you a number. E[X|Y=y] gives you a number. But E[X|Y] gives you a random variable! The brackets look the same, but one of these is not like the others.
The intuition is about what uncertainty remains:
Think of E[X|Y] as an oracle: “Tell me what Y is, and I’ll tell you E[X|Y=y].” Since Y is random, asking the oracle gives you a random answer.
Suppose X can be 0 or 1, each with equal probability P(X=0) = P(X=1) = 0.5, and:
Step 1: Compute E[Y|X=x] for each x
Step 2: Assemble the random variable E[Y|X]
E[Y|X] is a random variable — a function of X. It takes the value g(X) where g(0) = 2 and g(1) = 5. Since X is random, we ask: what are the probabilities that E[Y|X] takes each of its values?
Since E[Y|X] is a random variable, we can compute its expectation:
From the example above:
Step 1: For each value of x, compute the average of Y:
Step 2: Average those averages, weighted by how likely each x is (not how many y values there are — but how probable that particular x is):
This must give the overall average E[Y], because we’ve accounted for every (x, y) pair exactly once, weighted by its probability.
Class average analogy: Suppose a class has 20 students in Section A (average score 80) and 30 students in Section B (average score 90). The overall class average is not (80 + 90)/2 = 85, because the sections have different sizes. It’s 80 · (20/50) + 90 · (30/50) = 86. The weighting by “how often each x occurs” is the weighting by section size — how many students are in each group.
Variance measures the expected squared deviation from the mean. Think of it as the “expected variance” — how far values typically fall from the center, on average.
where μ = E[X].
Following the same pattern as conditional expectation:
where μY|x = E[Y|X=x].
This measures: “Given that X = x, how spread out is Y around its conditional mean?”
Just like E[Y|X], this is a function of X that returns Var(Y|X=x) when X = x. It is “waiting” for you to tell it what X is.
E[(X − μ)²] — A property of the distribution. This is what you’re predicting.
(1/n) Σ(xi − x̄)² — An estimator computed from data. This is what you observe.
The Law of Total Variance is one of the most powerful decomposition results in probability. It breaks the total variance of Y into two interpretable components:
This decomposes total variance into:
How much the center of prediction moves as X changes. How much does our best guess shift depending on which X we observe?
If this is zero, then E[Y|X] = E[Y] almost surely — knowing X tells us nothing about Y’s conditional expected value, although it may still reveal other features of Y’s conditional distribution.
The average squared spread of the error bars. How uncertain are we about Y, on average, even after knowing X?
If this is zero, then Var(Y|X) = 0 almost surely, so Y = E[Y|X] almost surely. There is no residual randomness: knowing X determines Y exactly, apart from events of probability zero.
For prediction under squared-error loss, a natural signal-to-noise ratio compares variation in the conditional mean with the remaining conditional variation:
| High | Low | |
|---|---|---|
| Numerator (Signal) | Knowing X dramatically shifts your prediction | Knowing X barely changes your prediction |
| Denominator (Noise) | Even knowing X, Y is still noisy | Knowing X pins down Y tightly |
High SNR: shifts in the conditional mean explain most of Y’s variability — the signal dominates.
Low SNR: residual conditional variation dominates shifts in the conditional mean. X may still reveal other features, such as the conditional variance or shape of Y. If the denominator is zero, the ratio is infinite when the numerator is positive and undefined when both terms are zero.
To make the relationship between signal, noise, and dependence precise, we need two more definitions:
The problem with covariance is that its magnitude depends on the units of X and Y. To get a unit-free measure, we normalize:
where σX = √Var(X) and σY = √Var(Y), assuming both variances are finite and nonzero.
When ρ = ±1 (perfect linear relationship), Y is a deterministic linear function of X, and the Law of Total Variance becomes extreme:
In information theory, conditioning never increases uncertainty: H(X|Y) ≤ H(X).
For expectation, there’s no analogous inequality. E[X|Y] can be larger than, smaller than, or equal to E[X] depending on which y you observe. What you do have is the Law of Total Expectation: E[E[X|Y]] = E[X]. Individual conditional expectations can be anywhere, but they average out to E[X].
Something curious has happened. We’ve seen two “total” laws, but they look very different:
One term. Clean. Simple.
Two terms. Why does variance need an extra term that expectation doesn’t?
Expectation is linear: E[X + Y] = E[X] + E[Y], always. But variance involves squared deviations, and squaring produces cross-terms.
For independent X and Y, the 1st moment (mean) adds cleanly:
But the 2nd raw moment does not:
The cross-term 2E[X]E[Y] ruins the additivity. Even though X and Y are independent, the 2nd raw moment of their sum is not the sum of their 2nd raw moments.
This pattern continues: the 3rd, 4th, and higher moments become increasingly messy to decompose. This raises a natural question: is there a systematic framework that explains why each “total law” takes the form it does?
The answer is yes — it comes from the theory of moments,generating functions, and cumulants. Understanding these tools will reveal that the Laws of Total Expectation and Total Variance are the first two cases of a beautiful general pattern: the Law of Total Cumulants.
To understand why the total laws have different forms, we need to formalize the idea of “higher-order” properties of distributions. Moments are the tool for this.
The nth raw moment is simply the expected power:
| n | Raw Moment | What it is |
|---|---|---|
| 1 | E[X] | Mean |
| 2 | E[X²] | Mean of squares |
| 3 | E[X³] | Mean of cubes |
Central moments measure deviations from the mean:
| n | Central Moment | What it captures |
|---|---|---|
| 1 | E[X − μ] = 0 | Always zero! |
| 2 | E[(X − μ)²] | Variance — spread |
| 3 | E[(X − μ)³] | Numerator of skewness — unstandardized asymmetry |
| 4 | E[(X − μ)⁴] | Numerator of kurtosis — unstandardized fourth-moment magnitude |
Matching a finite number of moments does not guarantee similar shapes: a discrete distribution and a continuous one can even share their first four moments. For example, a standard Gaussian and the discrete distribution with probability 2/3 at 0 and probability 1/6 at each of −√3 and +√3 both have first four raw moments 0, 1, 0, and 3. For many moment-determinate distributions, the full infinite moment sequence specifies the distribution, but some distributions, including the lognormal, are not uniquely determined even by all of their moments.
Within the Gaussian (normal) family, a distribution is completely defined by its mean and variance. All higher moments are determined by these two parameters:
We now know that moments characterize distribution shape, but raw moments don’t add cleanly for independent variables. To find quantities that do add, we need a tool that connects moments to derivatives: the Taylor expansion.
Imagine you have a complicated function f(x) — like sin(x) or ex. These are hard to compute by hand. What if you could approximate them using only addition, subtraction, and multiplication (i.e., polynomials)?
For a function analytic around a = 0, its Taylor series there (called a Maclaurin series) is:
Think of it as matching derivatives:
Within the radius of convergence of an analytic function’s Taylor series, its partial sums converge to the function as more terms are included, although the error need not decrease after every individual term. Smoothness alone does not guarantee convergence to the original function.
For f(x) = ex, every derivative is ex, so every derivative at 0 is 1:
Now we connect this to probability. When E[etX] is finite for t in an open interval around 0, define the Moment Generating Function (MGF):
Three powerful reasons:
Apply the Taylor expansion of etX:
Under this existence condition, take expectations term by term:
The moments appear as coefficients! Taking derivatives extracts them:
| Derivative at t = 0 | Equals |
|---|---|
| M′(0) | E[X] |
| M″(0) | E[X²] |
| M′′′(0) | E[X³] |
We can now return to the motivating question: why do the Laws of Total Expectation and Total Variance have different forms? The MGF showed us that moments are derivatives, and that independent variables’ MGFs multiply. The final insight is that taking the logarithm converts products into sums — giving us quantities called cumulants that add for independent variables whenever the required generating functions and derivatives exist.
The nth cumulant κn = K(n)(0).
For independent X and Y:
| n | Cumulant κn | Equals | Same as central moment? |
|---|---|---|---|
| 1 | κ1 | Mean | No — the 1st central moment is 0 |
| 2 | κ2 | Variance | Yes |
| 3 | κ3 | 3rd central moment | Yes |
| 4 | κ4 | 4th central moment − 3·(Var)² | No — κ4/κ2² is excess kurtosis |
We can now finally answer why the two total laws have different forms. They are the first two cases of a general pattern: the Law of Total Cumulance sums over the set partitions of the conditioned arguments. For repeated copies of the same variable, symmetric partition terms can be grouped; the first three orders produce one, two, and three grouped terms, respectively, while the fourth already produces five grouped term types.
An interesting quantity: the variance of the conditional variance. This measures whether the noise level itself depends on X; it is zero exactly when the conditional variance is constant almost surely.
For example, suppose X = 0 and X = 1 are equally likely:
Here Var(Y|X) takes the values 0.25 and 25 with equal probability, so Var(Var(Y|X)) = 153.14. This is heteroscedasticity — the noise level varies with the input.
The 3rd cumulant decomposition can capture one aspect of this: the covariance term Cov(κ1(Y|X), κ2(Y|X)) measures whether the noise level correlates with the prediction. Heteroscedasticity can also occur when this covariance is zero, however. The quantity Var(Var(Y|X)) appears directly in the 4th cumulant decomposition, so changing noise levels show up naturally in higher cumulant decompositions.
Natural and intuitive
Directly interpretable (mean, variance, skewness…)
Raw moments generally don’t add cleanly for independent sums; variance and the 3rd central moment are useful lower-order exceptions
Algebraically elegant
When they exist, add for independent variables
The Law of Total Variance is a special case of a beautiful general pattern