← back

Expected Value and Variance of an n-Sided Die

#179 · Probability · Easy

⊣ Solve on deep-ml.com

Problem

Compute the Expected Value and Variance of an n-sided fair die. For a die with faces numbered 1 through n, calculate E[X] and Var(X).

Solution

1
2
3
4
def die_expected_value_and_variance(n: int) -> tuple:
    expected_value = (n + 1) / 2.0
    variance = (n ** 2 - 1) / 12.0
    return expected_value, variance

Explanation

  1. For a fair n-sided die with faces 1 to n, each face has probability 1/n.
  2. Expected value: E[X] = (1 + 2 + ... + n) / n = n(n+1)/(2n) = (n+1)/2.
  3. E[X^2]: (1^2 + 2^2 + ... + n^2) / n = n(n+1)(2n+1) / (6n) = (n+1)(2n+1)/6.
  4. Variance: Var(X) = E[X^2] - (E[X])^2 = (n+1)(2n+1)/6 - ((n+1)/2)^2 = (n^2-1)/12.

Complexity

  • Time: O(1)
  • Space: O(1)