Skip to content
AI360Xpert
Beta

Conditional Expectation

Conditional expectation re-takes the average after learning something. A die averages 3.5, but averages 4 once you know the roll was even.

A die averages 3.5 overall, but inside the known cell of even rolls the re-averaged value E[X | even] equals 4
A die averages 3.5 overall, but inside the known cell of even rolls the re-averaged value E[X | even] equals 4

Why Does This Exist?

Models rarely predict in the dark. They predict after seeing features: the expected price given the neighborhood, the expected click given the history. Plain expectation mathbbE[X]\\mathbb{E}[X] averages over everything; conditional expectation mathbbE[XmidmathcalG]\\mathbb{E}[X \\mid \\mathcal{G}] averages after information mathcalG\\mathcal{G} arrives.

Without it there is no regression theory, no Bayes update, and no filtering. The conditional expectation is the least-squares best predictor given what you know, and every "expected value given features" in ML is one. It extends expectation, variance and covariance to the case where knowledge has arrived.

The one idea you need first

Expectation is a weighted average: for a fair die, mathbbE[X]=(1+2+3+4+5+6)/6=3.5\\mathbb{E}[X] = (1+2+3+4+5+6)/6 = 3.5. Conditional expectation repeats that average on a smaller set, reweighted to sum to 1 inside it.

Think of It Like This

Re-grading after the class splits

A teacher averages all exam scores: 72. Then the class splits into two rooms and she re-averages inside each room: 68 in one, 78 in the other. Nobody retook the exam; the information changed.

Conditional expectation is the room average. It is still a random object before you know your room, and a plain number after. The tower rule says averaging the room averages, weighted by room size, returns the class average. Where the analogy stops: rooms are fixed, while sigma\\sigma-algebras can refine continuously as new data streams in.

How It Actually Works

Given information mathcalG\\mathcal{G} (an event, a variable, or a sigma\\sigma-algebra), mathbbE[XmidmathcalG]\\mathbb{E}[X \\mid \\mathcal{G}] is the unique mathcalG\\mathcal{G}-measurable variable satisfying mathbbE[mathbbE[XmidmathcalG]cdot1A]=mathbbE[Xcdot1A]\\mathbb{E}[\\mathbb{E}[X \\mid \\mathcal{G}] \\cdot 1_A] = \\mathbb{E}[X \\cdot 1_A] for every AA in mathcalG\\mathcal{G}. In plain words: it matches XX on every question mathcalG\\mathcal{G} can ask, and it asks nothing mathcalG\\mathcal{G} cannot.

Properties you actually use

  • Tower: mathbbE[mathbbE[XmidmathcalG]midmathcalH]=mathbbE[XmidmathcalH]\\mathbb{E}[\\mathbb{E}[X \\mid \\mathcal{G}] \\mid \\mathcal{H}] = \\mathbb{E}[X \\mid \\mathcal{H}] when mathcalHsubseteqmathcalG\\mathcal{H} \\subseteq \\mathcal{G}. Coarsen in steps or all at once.
  • Taking out what is known: if ZZ is mathcalG\\mathcal{G}-measurable, mathbbE[ZXmidmathcalG]=Z,mathbbE[XmidmathcalG]\\mathbb{E}[ZX \\mid \\mathcal{G}] = Z\\,\\mathbb{E}[X \\mid \\mathcal{G}].
  • Best predictor: mathbbE[XmidmathcalG]\\mathbb{E}[X \\mid \\mathcal{G}] minimizes mathbbE[(X−f)2]\\mathbb{E}[(X - f)^2] over mathcalG\\mathcal{G}-measurable ff. Regression targets it.

Worked example

Fair die, XX the face. Plain mean 3.53.5. Learn the event E=2,4,6E = \\{2, 4, 6\\} ("even"). Re-average inside: mathbbE[XmidE]=(2+4+6)/3=12/3=4\\mathbb{E}[X \\mid E] = (2 + 4 + 6)/3 = 12/3 = 4. Learn instead the exact face X=5X = 5 and the conditional collapses to 55. Tower check: P(E)=1/2P(E) = 1/2, mathbbE[XmidEc]=(1+3+5)/3=3\\mathbb{E}[X \\mid E^c] = (1+3+5)/3 = 3, and tfrac12cdot4+tfrac12cdot3=3.5\\tfrac{1}{2} \\cdot 4 + \\tfrac{1}{2} \\cdot 3 = 3.5. The cells recombine to the whole.

Code

import numpy as np
rng = np.random.default_rng(1)rolls = rng.integers(1, 7, size=60000)print(round(float(rolls.mean()), 3))  # -> 3.499print(round(float(rolls[rolls % 2 == 0].mean()), 3))  # -> 4.004

Sixty thousand rolls give 3.499 overall and 4.004 on the even subset, against exact 3.5 and 4.

Watch Out For

Treating E[X | Y] as a number before Y is known

mathbbE[XmidY]\\mathbb{E}[X \\mid Y] is a function of YY, hence random. People plug in mathbbE[XmidY=y]\\mathbb{E}[X \\mid Y = y] for one yy and treat the whole object as that number. Then the tower rule looks false and downstream variance math breaks.

Keep the two apart: mathbbE[XmidY]\\mathbb{E}[X \\mid Y] varies with YY; mathbbE[XmidY=y]\\mathbb{E}[X \\mid Y = y] is its value at one point. Take the outer expectation to get back to a number.

Conditioning on zero-probability points with the ratio formula

P(AmidB)=P(AcapB)/P(B)P(A \\mid B) = P(A \\cap B)/P(B) needs P(B)>0P(B) > 0. With continuous features, P(X=x)=0P(X = x) = 0 and the ratio is 0/00/0. Filtering code that subsets to exact floats hits this silently.

Condition on intervals or use densities and the measure-theoretic definition. If the subset has fewer than a dozen rows, widen the window before trusting the average.

The Quick Version

  • Conditional expectation is the average after information arrives: same variable, smaller world.
  • A fair die averages 3.5 overall and 4 given even, since (2+4+6)/3=4(2+4+6)/3 = 4.
  • The tower rule recombines cell averages into the whole average.
  • Known factors pull out; the conditional expectation is the least-squares best predictor.
  • Before the information is known it is random; after, it is a number.
  • Never condition on exact continuous values with the elementary ratio formula.