Home / Statistics

Mean, Variance and Standard Deviation

The mean says where the data sits; variance and standard deviation say how far it spreads. Drag points around and watch all three respond — then see why one outlier can wreck them.

Controls

number of points8

Drag any point on the plot to change the data.

Data, Mean and Spread

drag the points

Live Calculation

mean μ
0
variance σ²
0
std dev σ
0
within 1σ
0

Mean, Variance and Standard Deviation

Where the data sits, and how far it wanders — the two numbers behind every normalisation layer.

The problem it solves

Two datasets can share the same average and still look nothing alike. The mean describes the centre; variance and standard deviation describe the spread. You need both to say anything useful about data.

The Three Formulas

Read variance as a recipe: measure how far each point sits from the mean, square those distances, and average them. The table below the plot performs exactly these steps on your live data.

Why Square the Deviations?

Because raw deviations always cancel out. Points above the mean are positive, points below are negative, and by the definition of the mean they sum to exactly zero — every time, for every dataset. Check the Σ row in the table.

Squaring removes the signs so distances can accumulate. It also weights large deviations far more heavily: a point 4 away contributes 16, while four points 1 away contribute 4 in total. This is precisely why a single outlier can dominate the variance — press the Outlier button and watch σ jump.

Standard Deviation Is the Readable One

Variance is in squared units — if your data is in kilograms, the variance is in kg², which means nothing physically. Taking the square root returns you to the original units, so σ can be read directly off the same axis as the data. That is the only reason both quantities exist.

As a rule of thumb for roughly bell-shaped data, about 68% of points fall within ±1σ and 95% within ±2σ. The app counts this for your actual data — try the two-groups preset and watch the rule break down, since it only holds for normal-ish distributions.

n or n−1?

Dividing by n gives the population variance — correct when your data is the entire group. Dividing by n−1 gives the sample variance, used when your data is a sample being used to estimate a larger population. The sample version is slightly larger, correcting a bias that arises because you measured deviations from the sample's own mean rather than the true one. Toggle the checkbox and watch the difference shrink as n grows.

Where It Shows Up in ML

Standardisation — the z-score — rewrites every value as "how many standard deviations from the mean":

This is what feature scaling, batch normalisation and layer normalisation all compute. It puts features measured in wildly different units onto a common footing, which stops the largest-scaled feature from dominating training purely because of its units.

Centre and spread

The mean says where the data sits. Variance and standard deviation say how far it typically strays from there — and without the second number the first one is close to useless.

Two datasets with the same mean of 50:

  • A: 49, 50, 50, 51 — tight.
  • B: 10, 30, 70, 90 — spread out.

Same centre, entirely different situations. "Average delivery time 3 days" means something quite different at ±0.5 days than at ±5.

The calculation, worked through on [2, 4, 4, 4, 5, 5, 7, 9]:

  1. Mean: 40 / 8 = 5.
  2. Deviations: −3, −1, −1, −1, 0, 0, 2, 4.
  3. Square them: 9, 1, 1, 1, 0, 0, 4, 16 — total 32.
  4. Variance: 32 / 8 = 4.
  5. Standard deviation: √4 = 2.

Step 3 is the one that needs justifying: deviations are squared because they would otherwise cancel to exactly zero, and squaring (rather than taking absolute values) keeps the result smooth and differentiable, which matters for everything built on top of it.

Why both variance and standard deviation exist

Variance is in squared units. If the data is in pounds, the variance is in "squared pounds", which nobody can picture.

The standard deviation is its square root, back in the original units, and is therefore the number you report: "average salary £50,000 with a standard deviation of £12,000" is a sentence people can act on.

Variance survives because it is mathematically better behaved. Variances of independent variables add; standard deviations do not. That single property underlies error propagation, the bias-variance decomposition, portfolio theory and the derivation of most statistical tests.

Rule of thumb: compute with variance, communicate with standard deviation.

The n or n−1 question

Dividing by n gives the population variance — correct when your data is the entire group you care about.

Dividing by n−1 gives the sample variance — correct when your data is a sample used to estimate a larger population's variance. The smaller denominator makes the estimate slightly larger, correcting a bias that comes from measuring deviations against the sample's own mean rather than the true one.

In practice: use n−1 for sample data, which is nearly always what you have. NumPy defaults to n (ddof=0); pandas defaults to n−1. That inconsistency is a real source of small discrepancies between two analyses of the same data.

import numpy as np, pandas as pd
x = [2, 4, 4, 4, 5, 5, 7, 9]

np.std(x)                  # 2.0    - population (ddof=0)
np.std(x, ddof=1)          # 2.138  - sample
pd.Series(x).std()         # 2.138  - sample by default

With large n the difference is negligible; with n = 5 it is 12%.

Build variance by hand

Two sets with the same mean and very different spread, with every step of the variance calculation printed rather than asserted.

example_01.pyNumPy
Output

Try it yourself

  1. Drag a point far to the right. The mean drifts toward it and σ grows sharply — both are sensitive to extremes.
  2. Watch the Σ(x−μ) row as you drag. It stays pinned at zero no matter what you do.
  3. Load "Tightly clustered", then "Widely spread". The means can be similar while σ differs several-fold — the average alone hides this completely.
  4. Load "Two groups". The mean lands in the empty gap between the clusters, describing a value that no data point is anywhere near.
  5. Toggle n−1 with only 2 points, then raise n. The correction is large for tiny samples and negligible for big ones.

Worth remembering

The mean locates the data, the standard deviation measures its spread in the same units, and variance is the squared intermediate that makes the maths work. Squaring is what makes both outlier-sensitive — and the z-score built from them is the normalisation step in nearly every model you will train.

Standardisation, and why models need it

Subtracting the mean and dividing by the standard deviation converts any feature into a z-score — centred at 0, with a spread of 1.

z = (x − μ) / σ

The reason this matters is that many algorithms compare features by magnitude. Salary in the tens of thousands and age in the tens are not comparable numbers, so any distance, any regularisation penalty, and any gradient step will be dominated by whichever column happens to use larger units.

Which models need it:

ModelNeeds scaling?Why
KNN, k-means, SVMYesBuilt on distances
Linear/logistic with regularisationYesThe penalty is on raw coefficient size
Neural networksYesGradients and initialisation assume comparable scales
PCAYesIt maximises variance, which is unit-dependent
Decision trees, forests, boostingNoSplits are per-feature comparisons

The rule that keeps it honest: fit the scaler on the training set only, then apply it to validation and test data. Fitting on everything leaks the test set's mean and standard deviation into training. A Pipeline enforces this automatically.

RobustScaler is the variant to reach for when outliers are present: it centres on the median and scales by the interquartile range, so one extreme value cannot distort the whole column.

Reading the spread

  • Coefficient of variation (σ / μ) makes spread comparable across variables with different units — a σ of 10 is large for a mean of 20 and tiny for a mean of 10,000.
  • Interquartile range is the robust alternative, unaffected by the tails.
  • Standard error (σ / √n) is the spread of the mean, not of the data, and is what confidence intervals are built from. Confusing the two makes intervals far too wide or far too narrow.
  • Variance in the bias-variance decomposition is the same idea applied to a model's predictions across different training sets.

Questions people ask

Can variance be negative? No — it is an average of squares. A negative result means a numerical bug, usually in a "shortcut" formula.

What does a standard deviation of zero mean? Every value is identical. As a feature that is useless, and it will break standardisation by dividing by zero.

Which is more robust to outliers? Neither — squaring makes both very sensitive. Use the interquartile range or median absolute deviation instead.

Why square instead of taking absolute values? Squares are differentiable everywhere and they add cleanly across independent variables. Mean absolute deviation is more robust but harder to work with mathematically.

Do I standardise the target too? For neural networks often yes, and remember to invert it before reporting errors. For tree models, no.

What is ddof? The delta degrees of freedom — the amount subtracted from n in the denominator. ddof=1 gives the sample variance.

Recap in one screen

  • The mean gives the centre; variance and standard deviation give the spread, and a centre without a spread says little.
  • Variance is the mean squared deviation; the standard deviation is its square root, in the data's own units.
  • Compute with variance (it adds), report with standard deviation (it is interpretable).
  • Divide by n−1 for samples; check your library's default.
  • Standardisation is required for every distance-based or regularised model, and must be fitted on training data only.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Every value in a dataset is increased by 10. What happens?

  2. Why is standard deviation usually quoted rather than variance?

  3. A single extreme outlier is added to a dataset. Which is affected more?

Cheat sheet

Mean, Variance and Standard Deviation

The mean says where the data sits; variance and standard deviation say how far it spreads. Drag points around and watch all three respond — then see why one outlier can wreck them.

MATHS · vizlearn.in/maths/mean_variance_standard_deviation.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.