mathematics25 min

Expectation & Variance

Expected value, variance, and standard deviation for understanding data distributions

0/9Not Started

Why This Matters

A website has an average response time of 200ms. But is that the whole story? If half the requests take 50ms and half take 350ms, users have a very different experience than if every request takes exactly 200ms. The average (the expected value) tells you the center. The variance and standard deviation tell you the spread.

Expected value is the weighted average of all possible outcomes. Variance measures how far values typically stray from that center. Together they give you a complete picture: is your data tightly clustered or wildly spread out? This matters for performance monitoring, ML model evaluation, financial modeling, and any situation where you need to reason about distributions of numbers.

Define Terms

Visual Model

-1 SDLow values
MeanE[X] = 5.0
+1 SDHigh values
68% of data
Variance= 4.0
Std Dev= 2.0
Data Points[2,4,4,4,5,5,7,9]

The full process at a glance. Click Start tour to walk through each step.

Expected value is the center. Variance and standard deviation measure how spread out values are.

Code Example

Code
// Expected value (mean)
function mean(arr) {
  return arr.reduce((sum, x) => sum + x, 0) / arr.length;
}

const data = [2, 4, 4, 4, 5, 5, 7, 9];
console.log("mean:", mean(data)); // 5

// Variance: average of squared differences from mean
function variance(arr) {
  const avg = mean(arr);
  const squaredDiffs = arr.map(x => (x - avg) ** 2);
  return mean(squaredDiffs);
}
console.log("variance:", variance(data)); // 4

// Standard deviation: sqrt of variance
function stdDev(arr) {
  return Math.sqrt(variance(arr));
}
console.log("stdDev:", stdDev(data)); // 2

// Weighted expected value (e.g., dice roll)
// E[X] = sum(value * probability)
const dieOutcomes = [1, 2, 3, 4, 5, 6];
const dieProbability = 1 / 6;
const expected = dieOutcomes.reduce(
  (sum, val) => sum + val * dieProbability, 0
);
console.log("E[die]:", expected.toFixed(2)); // 3.50

// Compare two datasets with same mean but different spread
const consistent = [49, 50, 50, 51, 50];
const variable   = [10, 30, 50, 70, 90];
console.log("consistent mean:", mean(consistent)); // 50
console.log("variable mean:",   mean(variable));   // 50
console.log("consistent std:",  stdDev(consistent).toFixed(2)); // 0.63
console.log("variable std:",    stdDev(variable).toFixed(2));   // 28.28

Interactive Experiment

Try these exercises:

  • Calculate the mean of [10, 20, 30, 40, 50] by hand. Verify with code.
  • Compute the variance and standard deviation of [5, 5, 5, 5]. Why are they 0?
  • Generate 10000 random numbers from a normal distribution with mean 0 and std 1. What fraction falls between -1 and 1? (Should be about 68%.)
  • Create two arrays with the same mean but different standard deviations. Which would you prefer as response times?
  • Roll a die 10000 times. What is the mean and standard deviation of the results?

Quick Quiz

Coding Challenge

Z-Score Calculator

Write a function called `zScores` that takes an array of numbers and returns an array of z-scores. The z-score of a value x is (x - mean) / stdDev. A z-score tells you how many standard deviations a value is from the mean. Return the z-scores rounded to 2 decimal places.

Loading editor...

Real-World Usage

Expectation and variance appear throughout production software:

  • Performance monitoring: Average response time tells you the center; p99 latency and standard deviation tell you about tail risk and consistency.
  • Machine learning: Loss functions compute expected error. Batch normalization uses mean and variance to stabilize training.
  • Financial software: Expected return and variance (risk) drive portfolio optimization and option pricing.
  • Quality assurance: Six Sigma means the defect rate is 6 standard deviations from the mean — extremely unlikely.
  • A/B testing: Statistical tests compare means and use variance to determine if a difference is significant or just noise.

Connections