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

Computing Variance from Moments

Use the formula Var(X) = E[X²] - (E[X])² to efficiently calculate variance from raw and squared expectations.

Computing Variance from Moments

You'll learn a shortcut formula that calculates variance using expected values, saving you computation time.

The Efficient Variance Formula

You already know that variance measures how spread out a random variable is. The straightforward definition is Var(X) = E[(X - E[X])²] — the expected value of squared deviations from the mean.

But there's a computationally friendlier way: Var(X) = E[X²] - (E[X])²

This formula uses moments — statistical measures based on powers of X. Here, E[X] is the first moment (the mean), and E[X²] is the second moment (expected value of the squares).

Why This Works

Think of it like comparing two measurements of "spreadiness":

  • E[X²]: How far values are from zero on average (squared)
  • (E[X])²: Where the mean sits, squared

The difference between these tells you how much the values spread around their mean.

A Simple Example

Say you roll a fair die (X = 1,2,3,4,5,6):

Step 1: Calculate E[X]

  • E[X] = (1+2+3+4+5+6)/6 = 3.5

Step 2: Calculate E[X²]

  • E[X²] = (1²+2²+3²+4²+5²+6²)/6 = (1+4+9+16+25+36)/6 = 91/6 ≈ 15.17

Step 3: Apply the formula

  • Var(X) = E[X²] - (E[X])²
  • Var(X) = 15.17 - (3.5)² = 15.17 - 12.25 = 2.92

This is much faster than computing (x - 3.5)² for each outcome and averaging!

Key Takeaway: The formula Var(X) = E[X²] - (E[X])² lets you compute variance efficiently by combining the second moment and squared first moment, avoiding repeated deviation calculations.