First Principles
Math & Statistics

Variance and Standard Deviation

An explanation of variance and standard deviation — the math, population vs sample variance, a JS implementation, and how to interpret spread in data.

Carries across: A/B tests, latency reports, sprint estimates, any average that hides the spread.

By Fadjar Rafi. Published 21 Oct 2025. 4 min read.

Contents
  1. Mathematical Definition
  2. Programming Implementation
  3. Why Square the Differences?
  4. Interpreting Variance and Standard Deviation
  5. Types of Variance
  6. When to Use Population vs. Sample Variance?
  7. Relationship with Standard Deviation

Variance measures how spread out a set of numbers is from their mean. Picture it as the average distance between each data point and the center — except those distances are squared, which turns out to matter quite a bit.

Say you're measuring tree heights in two forests. In Forest A most trees sit around 30 meters, give or take. In Forest B they run all over the place, from 10 meters to 50. Both forests average 30 meters, but the spread is clearly different — and that difference is exactly what variance captures.

Two number lines with the same mean of 30: Forest A's values cluster tightly (low variance), Forest B's spread far apart (high variance)

Mathematical Definition#

Here's the formula, one step at a time:

  1. Find the mean (μ) of the data.
  2. For each value, subtract the mean (its distance from the center) and square the result (to drop the sign and weight larger gaps more heavily).
  3. Average all those squared differences.
σ2=∑(x−μ)2N\sigma^2 = \frac{\sum (x - \mu)^2}{N}

Where:

SymbolMeaning
σ2\sigma^2Variance
xxEach value in the dataset
μ\muMean of the dataset, calculated as μ=∑xN\mu = \frac{\sum x}{N}
NNTotal number of values in the dataset
∑\sumSigma notation, meaning "sum up everything that follows"

Programming Implementation#

Here's the same math in JavaScript:

// Function to calculate the mean of an array
const calculateMean = (numbers) => {
    return numbers.reduce((sum, num) => sum + num, 0) / numbers.length;
};

// Main function to calculate variance
const calculateVariance = (numbers) => {
    const mean = calculateMean(numbers);
    const squaredDifferences = numbers.map(num => Math.pow(num - mean, 2));
    return calculateMean(squaredDifferences);
};

// Function to calculate standard deviation
const calculateStandardDeviation = (numbers) => {
    return Math.sqrt(calculateVariance(numbers));
};

// Example usage
const exploreVariance = (datasetName, numbers) => {
    console.log(`\n${datasetName} Analysis:`);
    console.log(`Data: [${numbers.join(', ')}]`);
    console.log(`Mean: ${calculateMean(numbers).toFixed(2)}`);
    console.log(`Variance: ${calculateVariance(numbers).toFixed(2)}`);
    console.log(`Standard Deviation: ${calculateStandardDeviation(numbers).toFixed(2)}`);
};

// Example datasets with different variances
const forestA = [28, 29, 30, 31, 32];
const forestB = [10, 20, 30, 40, 50];

exploreVariance('Forest A (Uniform Heights)', forestA);
exploreVariance('Forest B (Varying Heights)', forestB);
// Output will show that Forest B has a higher variance than Forest A

Why Square the Differences?#

Squaring does three useful things:

  1. Handles negative values. Distances can be positive or negative; squaring makes them all positive so they don't cancel out.
  2. Emphasizes outliers. A value far from the mean produces a large squared difference, so variance reacts strongly to points that are way out there.
  3. Keeps the math smooth. Squaring gives a differentiable function, which is handy for the optimization problems that build on variance.

Interpreting Variance and Standard Deviation#

These aren't just abstract numbers — they tell you about the shape of your data.

  1. Small variance and standard deviation. Values cluster tightly around the mean. In car-parts manufacturing, a small standard deviation means the parts come out consistently sized.
  2. Large variance and standard deviation. Values spread far from the mean, signaling high variability. In investing, a large standard deviation means high volatility — prices swing a lot.
  3. Comparison between datasets. Two datasets can share the same mean but have different variances. If two schools post the same average test score but one has a higher standard deviation, that school's results are more uneven.
  4. Relationship with the normal distribution. In a normal distribution, about 68% of the data falls within one standard deviation of the mean, 95% within two, and 99.7% within three — a quick way to spot outliers.

Types of Variance#

There are two, depending on whether you have the whole population or just a sample of it:

  1. Population Variance (σ²) — used when you have every value in the population.

    σ2=∑(x−μ)2N\sigma^2 = \frac{\sum (x - \mu)^2}{N}

    For example, weight data for all residents of a city.

  2. Sample Variance (s²) — used when you only have part of the population (a sample).

    s2=∑(x−xˉ)2n−1s^2 = \frac{\sum (x - \bar{x})^2}{n - 1}

    The difference is dividing by (n − 1) instead of N. This is the degrees of freedom correction, and it keeps the estimate from being biased too low. For example, weight data from just 100 people in the city rather than everyone.

When to Use Population vs. Sample Variance?#

  • Use population variance when you have all the data for the group you're analyzing.
  • Use sample variance when you have part of the data and want to estimate the variance of the whole population.

Relationship with Standard Deviation#

Standard deviation (σ) is just the square root of variance. Variance is convenient for the math, but standard deviation is easier to read because it's back in the same units as the original data.