Playground / Expectation and Variance of a Random Variable

Drag the bars and watch E[X] move

Expectation and Variance of a Random Variable

Interactive lab

Try it: Expectation and Variance of a Random Variable

A discrete random variable X as values with probabilities: the expectation E[X] = Σ x·p(x) is a probability-weighted average, and the variance is the expected squared distance from that mean, computed two ways.

How it works

  1. Each outcome has a value x and a bar height; probabilities are the heights renormalised, p(x) = height / total, so they always sum to exactly 1.
  2. E[X] = μ is built term by term: every outcome adds x·p(x) to the running sum.
  3. Var[X] = E[(X − μ)²]: centre each value on μ, square the deviation, weight it by p(x) and add.
  4. The same variance again as E[X²] − μ², and the standard deviation √Var[X].
  5. All sums are exact fractions (shown alongside decimals).

Default run (22 steps): X has 6 outcomes with bar heights 10, 10, 10, 10, 10, 10 (total W = 60). … E[X] = μ = 7/2 = 3.5; Var[X] = 35/12 ≈ 2.9167; standard deviation = √Var ≈ 1.7078.

Simplified: Up to 8 outcomes with integer values from −20 to 20 and integer bar heights 0–100. If every bar is 0 the first outcome gets height 1 (a distribution needs total probability 1).

Educational simulation

Loading the simulation…