Conditional Expectation

Conditional expectation E(Y | X = x) is the mean of Y once you know that X = x. As x changes it becomes a function of x, and E(Y | X) is a random variable. The law of total expectation says E(Y) = E[E(Y | X)]: average the conditional means.

Before this lesson: Marginal and Conditional Distributions, Law of Total Probability and Covariance

The mean of Y when X is known

Suppose X is a student’s hours of study and Y is the exam score. The overall mean E(Y) is one number. But if you learn that a student studied 5 hours, the average score among such students, E(Y | X = 5), is a better guess. Conditional expectation is that “best guess of Y once X is known.”

For each value x, E(Y | X = x) is an ordinary number: the mean of the conditional distribution. Collecting these numbers gives a function g(x) = E(Y | X = x). Plugging in the random X gives E(Y | X) = g(X), which is itself a random variable.

Averaging the conditional means, weighted by how likely each x is, gets you back to the overall mean. That is the law of total expectation, the expected-value version of the law of total probability.

Conditional expectation formulas

E(Y \mid X = x) = \sum_y y\,p_{Y|X}(y \mid x) \qquad \text{or} \qquad E(Y \mid X = x) = \int_{-\infty}^{\infty} y\,f_{Y|X}(y \mid x)\,dy

Law of total expectation (also called the tower rule or law of iterated expectations):

E(Y) = E\big[E(Y \mid X)\big] = \sum_x E(Y \mid X = x)\,p_X(x) \quad \text{or} \quad \int_{-\infty}^{\infty} E(Y \mid X = x)\,f_X(x)\,dx

Useful rules

  • Known factors come out: E(h(X)Y | X) = h(X) E(Y | X).
  • If X and Y are independent, then E(Y | X) = E(Y), a constant.
  • Linearity: E(aY + bZ | X) = a E(Y | X) + b E(Z | X).
  • Law of total variance: Var(Y) = E[Var(Y | X)] + Var(E[Y | X]).

Steps

  1. Find the conditional distribution of Y given X = x (joint divided by marginal).
  2. Multiply by y and add or integrate over the range of y for that x.
  3. Write the answer as a function of x; replace x by X to get E(Y | X).
  4. For E(Y), average the conditional means over the distribution of X.

Conditional expectation examples

Example 1: Pastry sales given coffee (word problem)

In the covariance lesson, X = 1 if a café customer buys coffee and Y = 1 if they buy a pastry, with p(0, 0) = 0.4, p(0, 1) = 0.1, p(1, 0) = 0.2 and p(1, 1) = 0.3. Find E(Y | X = 1), E(Y | X = 0), and check the law of total expectation.

E(Y \mid X = 1) = P(Y = 1 \mid X = 1) = \dfrac{0.3}{0.5} = 0.6

Y is 0 or 1, so its conditional mean is a conditional probability; p_X(1) = 0.2 + 0.3.

E(Y \mid X = 0) = P(Y = 1 \mid X = 0) = \dfrac{0.1}{0.5} = 0.2

p_X(0) = 0.4 + 0.1 = 0.5.

E(Y) = 0.6(0.5) + 0.2(0.5) = 0.3 + 0.1 = 0.4

Weight each conditional mean by P(X = x).

E(Y) = P(Y = 1) = 0.1 + 0.3 = 0.4

Check directly from the table.

Answer: E(Y | X = 1) = 0.6, E(Y | X = 0) = 0.2, and both routes give E(Y) = 0.4.

Example 2: E(Y | X) as a random variable

For the laptop and tablet table, p(0, 0) = 0.20, p(0, 1) = 0.10, p(1, 0) = 0.25, p(1, 1) = 0.15, p(2, 0) = 0.20, p(2, 1) = 0.10, with p_X = 0.30, 0.40, 0.30. Find E(Y | X = x) for each x and describe the random variable E(Y | X).

E(Y \mid X = 0) = \dfrac{p(0, 1)}{p_X(0)} = \dfrac{0.10}{0.30} = \dfrac{1}{3}

For a 0–1 variable, E(Y | X = x) = P(Y = 1 | X = x).

E(Y \mid X = 1) = \dfrac{0.15}{0.40} = \dfrac{3}{8}

Same for x = 1.

E(Y \mid X = 2) = \dfrac{0.10}{0.30} = \dfrac{1}{3}

Same for x = 2.

E(Y) = \dfrac{1}{3}(0.30) + \dfrac{3}{8}(0.40) + \dfrac{1}{3}(0.30) = 0.1 + 0.15 + 0.1 = 0.35

Law of total expectation.

Answer: E(Y | X) equals 1/3 when X is 0 or 2 (probability 0.60) and 3/8 when X = 1 (probability 0.40). Its mean is 0.35 = E(Y). Recall that Cov(X, Y) = 0 for this table, yet E(Y | X) is not constant, so X and Y are dependent.

Example 3: Conditional mean on a triangle

X and Y have joint density f(x, y) = 8xy on 0 ≤ y ≤ x ≤ 1. From the marginal and conditional distributions lesson, f_X(x) = 4x³ and f_{Y|X}(y | x) = 2y/x² for 0 ≤ y ≤ x. Find E(Y | X = x) and use it to find E(Y).

E(Y \mid X = x) = \int_0^x y \cdot \dfrac{2y}{x^2}\,dy = \dfrac{2}{x^2} \cdot \dfrac{x^3}{3} = \dfrac{2x}{3}

Integrate y times the conditional density over 0 to x.

E(Y \mid X) = \dfrac{2X}{3}

Replace x by the random X.

E(X) = \int_0^1 x \cdot 4x^3\,dx = \dfrac{4}{5}

Mean of X from its marginal.

E(Y) = E\left(\dfrac{2X}{3}\right) = \dfrac{2}{3} \cdot \dfrac{4}{5} = \dfrac{8}{15}

Law of total expectation, then linearity.

Answer: E(Y | X = x) = 2x/3 and E(Y) = 8/15, the same value the covariance lesson found from the marginal of Y. The line y = 2x/3 in the figure is the regression function of Y on X.

The triangle 0 ≤ y ≤ x ≤ 1, shaded light blue, with the line y = x as its upper edge. A solid orange line y = 2x/3 runs through the triangle from (0, 0) to (1, 2/3). At x = 0.6, a dashed blue vertical slice runs from y = 0 to y = 0.6, and a filled orange dot on it at y = 0.4 marks the conditional mean E(Y | X = 0.6) = 0.4.
For each x, the conditional mean of Y lies on the line y = 2x/3; at x = 0.6 it is 0.4.

Example 4: The stick broken twice, made easy

A 1-meter stick is broken at a uniform point; the left piece has length X. That piece is broken at a uniform point, and Y is the length of its left part. Find E(Y).

E(Y \mid X = x) = \dfrac{x}{2}

Given X = x, Y is uniform on (0, x), whose mean is the midpoint (see uniform distribution).

E(Y) = E\left(\dfrac{X}{2}\right) = \dfrac{1}{2} \cdot \dfrac{1}{2} = \dfrac{1}{4}

Law of total expectation; E(X) = 1/2.

\int_0^1 y(-\ln y)\,dy = \dfrac{1}{4}

Check with the marginal f_Y(y) = −ln y (by integration by parts).

Answer: E(Y) = 1/4 meter. Conditioning on X turns an integration-by-parts problem into one line.

Example 5: A random number of purchases (word problem)

The number of customers N who visit a shop in an hour has a Poisson distribution with mean 20. Each customer spends an amount with mean $12 and standard deviation $5, independently of N and of each other. Let T be the total spent in the hour. Find E(T) and the standard deviation of T.

E(T \mid N = n) = 12n,\qquad \operatorname{Var}(T \mid N = n) = 25n

With n customers, means add and (by independence) variances add.

E(T) = E(12N) = 12 \times 20 = 240

Law of total expectation.

E\big[\operatorname{Var}(T \mid N)\big] = E(25N) = 25 \times 20 = 500

First part of the law of total variance.

\operatorname{Var}\big(E[T \mid N]\big) = \operatorname{Var}(12N) = 144 \times 20 = 2880

Second part; a Poisson variable has variance equal to its mean, 20.

\operatorname{Var}(T) = 500 + 2880 = 3380

Add the two parts.

\sigma_T = \sqrt{3380} \approx 58.14

Take the square root.

Answer: E(T) = $240 and the standard deviation is about $58.14. Most of the variation comes from the random number of customers, not from how much each one spends.

Common mistakes

MistakeExample of the errorFix
Using the marginal instead of the conditionalE(Y | X = 0.5) = ∫ y f_Y(y) dy in Example 3Integrate y times f_{Y|X}(y | x), with x fixed.
Wrong limits for yIntegrating y from 0 to 1 in Example 3Use the range of y for that x: 0 to x.
Treating E(Y | X) as a numberWriting E(Y | X) = 8/15E(Y | X) = 2X/3 is a random variable; its mean is 8/15.
Averaging conditional means without weightsE(Y) = (0.6 + 0.2)/2 in Example 1 by luckWeight each E(Y | X = x) by P(X = x); it only matched because both weights were 0.5.
Dropping a term of the total varianceVar(T) = E[Var(T | N)] = 500 in Example 5Add Var(E[T | N]) as well.

Related ideas

Regression

The function E(Y | X = x) is the best possible prediction of Y from X, in the sense of smallest mean squared error. When it is a straight line, it matches the population version of the least-squares regression line.

Expected value and conditioning

For discrete problems, conditional expectation is the expected value formula applied to a conditional distribution. The same idea splits hard expected values into cases, as in Example 5.

Sums of random variables

Example 5 is a random sum. When the number of terms is fixed instead, the distribution of a sum of independent variables comes from the next lesson, Convolution of Random Variables.

Practice problems

  1. For the table in Example 1, find E(X | Y = 1).
  2. p(0, 0) = 0.3, p(0, 1) = 0.1, p(1, 0) = 0.1 and p(1, 1) = 0.5. Find E(Y | X = 0) and E(Y | X = 1).
  3. For the table in problem 2, check the law of total expectation.
  4. f(x, y) = x + y on the unit square. Find E(Y | X = x) and E(Y | X = 0).
  5. For the density in Example 3, find E(X | Y = y) and E(X | Y = 1/2).
  6. f(x, y) = 2 on the triangle 0 ≤ x ≤ y ≤ 1. Find E(Y | X = x) and use it to find E(Y).
  7. A day’s humidity level X is uniform on [0, 1]. Given X = x, the number of defects Y is Poisson with mean 10x. Find E(Y).
  8. X and Y are independent and E(Y) = 4. Find E(Y | X = 7).
  9. In Example 3, use E(XY | X) = X E(Y | X) to find E(XY).
  10. An insurer expects N claims with E(N) = 6, and each claim has mean $500, independent of N. Find the expected total claim amount.
Show answers

1.

E(X \mid Y = 1) = P(X = 1 \mid Y = 1) = \dfrac{0.3}{0.4} = 0.75

p_Y(1) = 0.1 + 0.3 = 0.4.

Answer: 0.75

2.

E(Y \mid X = 0) = \dfrac{0.1}{0.3 + 0.1} = \dfrac{0.1}{0.4} = 0.25

P(Y = 1 | X = 0).

E(Y \mid X = 1) = \dfrac{0.5}{0.1 + 0.5} = \dfrac{5}{6}

P(Y = 1 | X = 1).

Answer: 0.25 and 5/6

3.

0.25(0.4) + \dfrac{5}{6}(0.6) = 0.1 + 0.5 = 0.6

Weight by p_X(0) = 0.4 and p_X(1) = 0.6.

E(Y) = 0.1 + 0.5 = 0.6

Direct: P(Y = 1).

Answer: both give 0.6

4.

E(Y \mid X = x) = \dfrac{\int_0^1 y(x + y)\,dy}{x + \tfrac{1}{2}} = \dfrac{\tfrac{x}{2} + \tfrac{1}{3}}{x + \tfrac{1}{2}}

Divide by the marginal f_X(x) = x + 1/2.

E(Y \mid X = x) = \dfrac{3x + 2}{6x + 3}

Multiply top and bottom by 6.

E(Y \mid X = 0) = \dfrac{2}{3}

Substitute x = 0.

Answer: (3x + 2)/(6x + 3); 2/3

5.

E(X \mid Y = y) = \int_y^1 x \cdot \dfrac{2x}{1 - y^2}\,dx = \dfrac{2(1 - y^3)}{3(1 - y^2)}

f_{X|Y}(x | y) = 2x/(1 − y²) on [y, 1].

E(X \mid Y = y) = \dfrac{2(1 + y + y^2)}{3(1 + y)}

Cancel 1 − y (see sum and difference of cubes and difference of squares).

E\left(X \mid Y = \tfrac{1}{2}\right) = \dfrac{2(1.75)}{3(1.5)} = \dfrac{3.5}{4.5} = \dfrac{7}{9}

Substitute y = 1/2.

Answer: 2(1 + y + y²)/(3(1 + y)); 7/9

6.

f_{Y|X}(y \mid x) = \dfrac{2}{2(1 - x)} = \dfrac{1}{1 - x},\quad x \le y \le 1

Marginal f_X(x) = 2(1 − x); Y is uniform on [x, 1].

E(Y \mid X = x) = \dfrac{1 + x}{2}

Midpoint of [x, 1].

E(Y) = \dfrac{1 + E(X)}{2} = \dfrac{1 + \tfrac{1}{3}}{2} = \dfrac{2}{3}

Law of total expectation, with E(X) = 1/3.

Answer: (1 + x)/2; E(Y) = 2/3

7.

E(Y) = E\big[E(Y \mid X)\big] = E(10X) = 10 \times \dfrac{1}{2} = 5

The Poisson mean given X is 10X.

Answer: 5

8.

E(Y \mid X = 7) = E(Y) = 4

Independence: knowing X does not change the mean of Y.

Answer: 4

9.

E(XY) = E\big[X \cdot E(Y \mid X)\big] = E\left(\dfrac{2X^2}{3}\right)

Known factors come out.

E(X^2) = \int_0^1 x^2 \cdot 4x^3\,dx = \dfrac{2}{3}

Second moment of X.

E(XY) = \dfrac{2}{3} \cdot \dfrac{2}{3} = \dfrac{4}{9}

Multiply; this matches the covariance lesson.

Answer: 4/9

10.

E(\text{total}) = E(500N) = 500 \times 6 = 3000

Law of total expectation with E(total | N) = 500N.

Answer: $3,000

Conditional expectation FAQ

What is conditional expectation?

It is the mean of one random variable after the value of another is known: E(Y | X = x) is the expected value of the conditional distribution of Y given X = x.

What is the law of total expectation?

E(Y) = E[E(Y | X)]. Find the mean of Y in each case X = x, then average those means using the distribution of X.

Is E(Y | X) a number or a random variable?

E(Y | X = x) for a specific x is a number. E(Y | X) is a random variable, because it is a function of the random X.

How is conditional expectation related to regression?

E(Y | X = x) is the regression function of Y on X: the best prediction of Y from X in mean squared error. Linear regression approximates it with a line.

Summary

  • E(Y | X = x) is the mean of the conditional distribution of Y given X = x.
  • Compute it by summing or integrating y times the conditional PMF or density.
  • E(Y | X) is a random variable: a function of X.
  • Law of total expectation: E(Y) = E[E(Y | X)].
  • Law of total variance: Var(Y) = E[Var(Y | X)] + Var(E[Y | X]).

Previous lesson: Covariance

Next lesson: Convolution of Random Variables

Back to the Probability Theory Index