Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 · Random Variables Review

Conditional Probability and Conditional Expectation

Master conditioning: Bayes rule, the law of total probability, the law of total expectation and random sums, with Python simulations that confirm each formula.

Conditioning is the single most used technique in stochastic processes. To analyze a process, you split on what happens first, solve each case, and recombine. The Markov property is itself a statement about conditional probability. This lesson builds the tools.

Conditional probability

The probability of A given B (with P(B) > 0) is

Text
P(A | B) = P(A and B) / P(B)

It answers: among the outcomes where B happens, how often does A also happen? Rearranged, P(A and B) = P(A | B) P(B), which is the multiplication rule that chains together to describe processes step by step.

The law of total probability

If B_1, ..., B_k are disjoint events covering all possibilities (a partition), then

Text
P(A) = P(A | B_1) P(B_1) + ... + P(A | B_k) P(B_k)

You split by cases and add up. Example: a machine is in good condition with probability 0.7 and worn with probability 0.3. A good machine fails a job with probability 0.02; a worn one with probability 0.2. Then

Text
P(fail) = 0.02 × 0.7 + 0.2 × 0.3 = 0.014 + 0.06 = 0.074
Python Law of total probability by simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Bayes' rule

Reading the same numbers backwards gives Bayes' rule:

Text
P(B_j | A) = P(A | B_j) P(B_j) / P(A)

Given that the job failed, the probability the machine was worn is 0.06 / 0.074 ≈ 0.811. The simulation above computed the same quantity by filtering to the failures.

Conditional expectation

E[X | Y = y] is the average of X over the outcomes where Y = y. If you do not fix y, E[X | Y] is itself a random variable: its value depends on what Y turns out to be.

The law of total expectation (also called the tower property) says averaging the conditional averages gives the overall average:

Text
E[X] = E[ E[X | Y] ] = Σ_y E[X | Y = y] P(Y = y)

This is the engine of first-step analysis: condition on the first move of a process, express the answer in terms of itself, and solve.

Example: expected rolls to get a six

Let T be the number of rolls of a fair die until the first six. Condition on the first roll: with probability 1/6 you are done after 1 roll; with probability 5/6 you have used one roll and start afresh. So

Text
E[T] = (1/6)(1) + (5/6)(1 + E[T])
E[T] = 1 + (5/6) E[T]
E[T] / 6 = 1  →  E[T] = 6

A neat way to find an expectation without summing an infinite series.

Python First-step analysis: expected rolls to a six

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Random sums

Often the number of terms is itself random: the number of customers in a day N, each spending an amount X_i. The total is S = X_1 + ... + X_N. If N is independent of the X_i, which all have mean μ, then conditioning on N gives

Text
E[S | N = n] = n μ        so        E[S] = E[N] μ

For the variance, with σ² = Var(X_i):

Text
Var(S) = E[N] σ² + Var(N) μ²

The first term is the variability of the amounts; the second is the variability caused by the random count.

Python A random sum: customers and their spending

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Conditioning and independence

If X and Y are independent, then E[X | Y] = E[X]: knowing Y tells you nothing. The Markov property uses the same idea one level up: given the present, the past is independent of the future.

flowchart TD
    A["Want E[X]"] --> B["Choose Y to condition on"]
    B --> C["Compute E[X | Y = y] for each y"]
    C --> D["Average: sum of E[X | Y = y] P(Y = y)"]

The recipe for total expectation.

Choosing what to condition on is the creative step. Condition on whatever makes the remaining problem easy: the first step, the first arrival time, the value of a hidden variable.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. The law of total probability expresses P(A) as...

    You split on a partition and add the weighted conditional probabilities.
  2. 2. For a random sum S = X_1 + ... + X_N with N independent of the X_i, E[S] equals...

    Condition on N: E[S | N] = N E[X], so E[S] = E[N] E[X].
  3. 3. Using first-step analysis, the expected number of fair coin flips until the first head is...

    E = (1/2)(1) + (1/2)(1 + E), so E = 2.

Summary

  • P(A | B) = P(A and B) / P(B); Bayes' rule reverses the conditioning.
  • The law of total probability and the law of total expectation split a problem by cases.
  • First-step analysis conditions on the first move and solves an equation for the unknown.
  • For a random sum, E[S] = E[N] μ and Var(S) = E[N] σ² + Var(N) μ².

Next: Generating Functions