Course contentsShow
← Computer Science
Lesson 131 of 2,8723. Probability for Computer ScienceFree lesson

Expected Value of Functions

Compute expectations of transformed random variables and apply the law of the unconscious statistician.

Expected Value of Functions

What you'll learn: How to compute the expected value of a transformed random variable—like squaring it or plugging it into a formula—without listing every outcome manually.

The Core Idea

You already know how to compute the expected value of a random variable X. But what if you need the expected value of some function of X, like X² or 2X + 3?

The Law of the Unconscious Statistician (LOTUS) gives you a shortcut: you don't need to first figure out the probability distribution of the new random variable. Instead, you can compute E[g(X)] directly using the original distribution of X.

For Discrete Random Variables

If X is discrete with PMF P(X = x), then:

E[g(X)] = Σ g(x) · P(X = x)

Sum over all possible values of X, but apply your function g to each value before weighting by its probability.

Example

Suppose X represents the outcome of rolling a fair six-sided die. What's the expected value of X²?

  • Instead of creating a whole new random variable "X-squared," just compute:
  • E[X²] = (1² × 1/6) + (2² × 1/6) + (3² × 1/6) + (4² × 1/6) + (5² × 1/6) + (6² × 1/6)
  • E[X²] = (1 + 4 + 9 + 16 + 25 + 36) / 6 = 91/6 ≈ 15.17

You applied the function (squaring) to each outcome, then weighted by probabilities.

Why It Matters in CS

This technique appears everywhere: analyzing randomized algorithms (expected runtime of T²), hashing collisions, and variance calculations all rely on computing expectations of functions.

Key Takeaway: Use the Law of the Unconscious Statistician to compute E[g(X)] directly by applying g to each value of X in the original probability distribution.