Distributions, mean and variance, and the normal distribution

Heights, test scores, measurement errors. Collect enough of them and draw them, and you often get a bell shape: high in the middle, low on both sides. Here you will see which numbers describe that shape and why it shows up so often, by moving things yourself.

Short answer. A probability distribution is a picture of "how often each value comes up". The mean is its middle. The variance and standard deviation say how spread out it is. The normal distribution is a bell-shaped distribution, high in the middle and falling off evenly on both sides. When many small chances are added up, the result usually takes this shape.

Use the sliders to change the mean and the standard deviation. Click or drag on the picture to move the end nearer to where you pressed (a or b). The blue area is the probability that a value falls between them. The purple ticks ±1σ, ±2σ, ±3σ are 1, 2 and 3 standard deviations away from the mean.

Definitions

The text below explains with pictures and experiments. The textbook definitions are as follows. X is a random variable (a number whose value is decided by chance each time), and P(·) is probability.

probability density function
For a continuous random variable X, a function f that satisfies the lines below. The probability of landing in an interval is the area (integral) over that interval, and the probability of any single point is 0.

f(x)≥0,∫−∞∞f(x)dx=1,P(a≤X≤b)=∫abf(x)dx

expected value
The mean of a random variable. For a discrete random variable (values x₁, x₂, …) use the first line. For a continuous random variable with density f use the second. It is usually written μ.

E[X]=∑ixiP(X=xi)

E[X]=∫−∞∞xf(x)dx

variance, standard deviation
The variance is the expected value of the squared distance from the mean μ = E[X]. The standard deviation is the positive square root of the variance.

σ2=Var(X)=E[(X−μ)2],σ=Var(X)

If n values x₁, …, xₙ are the whole population, use the population variance σ². If they are a sample, use the sample variance s², measured from the sample mean x¯.

σ2=1n∑i=1n(xi−μ)2,s2=1n−1∑i=1n(xi−x¯)2

normal distribution
The continuous distribution whose density is the line below. Its parameters are the mean μ and the variance σ² (σ > 0), and it is written X~N(μ,σ2). E[X] = μ and Var(X) = σ².

f(x)=1σ2πe−(x−μ)22σ2,−∞<x<∞

standard normal distribution, z-score
The normal distribution with mean 0 and variance 1, N(0, 1), is the standard normal distribution. The z-score is a value minus the mean, divided by the standard deviation. Turning values into z-scores is called standardization. If X ~ N(μ, σ²), then Z follows N(0, 1).

z=x−μσ,Z=X−μσ~N(0,1)

The cumulative distribution function Φ of the standard normal is written with the error function erf.

Φ(z)=P(Z≤z)=12(1+erf(z2)),erf(x)=2π∫0xe−t2dt

68-95-99.7 rule
If X is normal, then whatever μ and σ are, the chances of landing within 1, 2 and 3 standard deviations of the mean are as below. The rule does not hold for other shapes.

P(|X−μ|≤kσ)≈0.6827,0.9545,0.9973(k=1,2,3)

central limit theorem
Let X₁, …, Xₙ be independent and follow the same distribution, with mean μ and a finite variance σ² greater than 0. Then, as n grows, the distribution of the standardized sample mean X¯n approaches the standard normal. The standard deviation of the sample mean is σ / √n.

limn→∞P(X¯n−μσ/n≤z)=Φ(z)(every z)

1. A distribution: how often does each value come up?

A probability distribution lists every value that can come up and how often it comes up (its probability). You can write it as a table or draw it. For one die there are six values, 1 to 6, each with probability 1/6. The probabilities of all possible values always add up to 1, because something always comes up.

If the values are separate steps, it is a discrete distribution. If any value in between can come up, it is a continuous distribution.

DiscreteContinuous
ValuesSeparate, countable steps (die faces, visitors per day)Endlessly many values in between (height, weight, time waited)
PictureBarsA smooth curve
Probability isThe height of a barThe area under the curve
Probability of one exact valueCan be more than 0 (rolling a 3: 1/6)0 (being exactly 170.000… cm tall)
Everything added upHeights add up to 1Total area = 1

Why is probability an area for continuous values? Nobody is "exactly" 170 cm tall. However precise your ruler, people come out as 170.0001 cm or 169.9999 cm. So you ask about a range, like "between 170 cm and 171 cm", and the area above that range is the probability.

The height of the curve is not a probability. It says how crowded the values are near that spot. This is called the probability density. That is why the height can be more than 1. In the lab above, set the standard deviation to 0.5 and the peak is 0.8 high. With 0.25 it would be about 1.6. The total area is still 1.

2. Mean and expected value: the middle

The mean of some data is the sum divided by the count. For 2, 4, 5, 6, 8 it is (2 + 4 + 5 + 6 + 8) ÷ 5 = 25 ÷ 5 = 5.

The mean of a distribution is called the expected value. Multiply each value by its probability, then add them all up.

E[X]=∑ixiP(X=xi)

Σ (capital sigma) means "add them all up". For one die the expected value is 1×1/6 + 2×1/6 + 3×1/6 + 4×1/6 + 5×1/6 + 6×1/6 = 21 ÷ 6 = 3.5. No die ever shows 3.5. The expected value is not "what you get in one roll". It is "what the average gets close to after a great many rolls". In the lab in section 8 you can watch dice averages gather around 3.5.

The mean tells you the middle, but not how spread out things are. 5, 5, 5, 5, 5 and 1, 3, 5, 7, 9 both have mean 5. So we need one more number.

3. Variance and standard deviation: the spread

To measure spread, look at how far each value is from the mean (its distance). But if you add these distances with their signs, you always get 0. The values above the mean and below it cancel out exactly. So you square each distance first. Squares are never negative.

σ2=1n∑i=1n(xi−μ)2

σ=σ2

n is the number of values. The variance is "the average of the squared distances from the mean". Because of the squaring, its unit is squared too (cm becomes cm²). Taking the square root brings the unit back, and that is the standard deviation. Read it as "the values are about this far from the mean". The mean is often written μ (mu) and the standard deviation σ (small sigma). The variance is σ².

Drag the blue points left or right, or choose a point and type its value. "Remove point" removes the chosen point (the black ring). You can have 2 to 10 points. Each orange line at the top is a point's distance to the mean, and each orange square below uses that distance as its side, so its area is the squared distance. Add up the square areas and divide by the number of points: you get the area of the green square (the variance). The side of the green square is the standard deviation. The squares are drawn smaller so they fit.

Work it out by hand

The variance and standard deviation of the five values 2, 4, 5, 6, 8. This is where the lab above starts.

ValueDistance to the mean (value − 5)Squared distance
2−39
4−11
500
611
839
Sum 25020
  1. Mean: 25 ÷ 5 = 5
  2. Distances: −3, −1, 0, 1, 3. With their signs they add up to 0.
  3. Squared distances: 9, 1, 0, 1, 9. They add up to 20.
  4. Variance: 20 ÷ 5 = 4
  5. Standard deviation: √4 = 2. The five values are about 2 away from the mean of 5.

The same working, written as formulas. n = 5, and x₁, …, x₅ are 2, 4, 5, 6, 8.

μ=15∑i=15xi=2+4+5+6+85=255=5

∑i=15(xi−μ)=−3−1+0+1+3=0

σ2=15∑i=15(xi−μ)2=9+1+0+1+95=205=4

σ=4=2

Try this. ① The first five points match the hand calculation above: variance 4, standard deviation 2. ② Drag point 5 from 8 to 10. The mean moves only a little, to 5.4, but the variance grows to 7.04 and the standard deviation to 2.65. One far point counts a lot because of the squaring. ③ Put all five points on 5. The variance is 0: no spread at all. ④ From the start, press "Add a point". A 3 is added, and you get mean 4.67, variance 3.89, standard deviation 1.97.

Divide by n or by n − 1?

This page divides by the count n. That is the variance when your values are the whole group (like the scores of every student in one class), called the population variance. But when you pick only a few values from a bigger group (a sample) and want to estimate the variance of the whole group, people usually divide by n − 1. That is the sample variance. The sample's own mean fits the sample perfectly, so the sum of squared distances comes out a bit smaller than it would from the true mean. Dividing by n − 1 instead of n makes it a bit bigger, which is right on average. In the example above that gives 20 ÷ 4 = 5 and a standard deviation of √5 ≈ 2.24 (the third line of the lab's readout). When n is large, the two hardly differ. Tools have different defaults, so check. In Python, statistics.pvariance divides by n and statistics.variance divides by n − 1.

4. The normal distribution: a bell shape

The normal distribution is a bell-shaped curve whose whole shape is fixed by two numbers: the mean μ and the standard deviation σ. Its height is written like this:

z=x−μσ

f(x)=e−z2/2σ2π

e is the number 2.718…, and π is 3.14…. The formula looks hard, but it does three things. ① It works out how many standard deviations x is from the mean (z). ② It makes the height drop quickly as you move away (e^(−z² ÷ 2)). This is 1 in the middle, about 0.61 at z = 1, and about 0.011 at z = 3. ③ It divides by σ × √(2π) so the total area is 1. To watch this formula being put together one piece at a time, see 5. How the formula is built.

Change the mean and the curve slides sideways without changing shape. Make the standard deviation bigger and the curve gets wider and, by the same amount, lower. That is because the area is always 1.

The area over a range (the probability) is found by cutting the area under the curve into thin slices and adding them up (integration). For this curve, the answer cannot be written with ordinary operations like adding, multiplying and square roots. So the result is given a name, the error function (erf), and computed with an approximation.

P(X<x)=12(1+erf(z2))

P(a<X<b)=P(X<b)−P(X<a)

The lab on this page computes erf with the approximation 7.1.26 from the book by Abramowitz and Stegun. Compared with Python's math.erf, the biggest difference we measured was about 0.00000014, so the four decimal places shown on screen are never off.

Try this. (The lab at the top.) ① At the start, mean ± 1 standard deviation (−1 to 1) is shaded, and the area is 0.6827 (68.27%). "± 2 standard deviations" gives 95.45% and "± 3 standard deviations" gives 99.73%. ② Keep "Mean ± 1 standard deviation" selected and change the mean to 2 and the standard deviation to 1.5. The area stays at 68.27%, because both ends move with the curve. ③ Raise the standard deviation from 1 to 2. The peak drops from 0.4 to 0.2. ④ With mean 0 and standard deviation 1, set a to 0 and b to 1: you get 0.3413. Set a to 1 and b to 2: you get 0.1359. Both ranges are 1 wide, but the one near the middle is about 2.5 times bigger. Put a and b on the same spot and the area is 0.

5. How the formula is built: the normal curve

Section 4 only gave the finished formula. Here we start from the simplest bell, e^(−x²), and add one piece at a time until we reach f(x) = (1 ÷ (σ√(2π))) × e^(−(x − μ)² ÷ (2σ²)). At every step the area under the curve is measured by cutting it into thin slices and adding them up (numerical integration). Watching the area change shows why the last piece is needed.

Press "Next" to go one step at a time. A piece is added to the formula on top (the orange background marks the new piece), the curve gets wider, slides or is pressed down by that much, and the reason appears below. The green curve is the formula now, and the gray dashed curve is the formula one step earlier (at the last step, the bell you get back through z). The blue area is the area under the curve, the purple line is the mean ± one standard deviation, and the orange arrows show how the curve moved in this step. Arrows that would be very short are not drawn. The μ slider opens at step ③ and the σ slider at step ④.

Written as formulas only, the steps above are:

e−x2

e−x2/2

e−(x−μ)2/2

e−(x−μσ)2/2=e−(x−μ)22σ2,∫−∞∞e−(x−μ)22σ2dx=σ2π

f(x)=1σ2πe−(x−μ)22σ2

  1. The seed: e^(−x²). It is highest at x = 0, where e⁰ = 1, and falls fast: 0.368 at x = ±1, 0.018 at ±2, 0.0001 at ±3. Why a square: (−x)² = x², so x and −x get the same height and both sides of the bell match. The top is also round, not pointed like it would be with |x|. Why a minus sign: e raised to a positive power grows without end. The minus sign makes the height smaller the farther you go from 0. The area under the curve is 1.7725, and the standard deviation (treating the curve as a distribution) is 0.71. Using 2 or 10 instead of e as the base gives the same bell, only with a different width.
  2. Divide the exponent by 2: e^(−x² ÷ 2). The height 0.368 that x = 1 gave on the seed now comes from x = √2 ≈ 1.41. The bell gets 1.41 times wider, and so does the area: 1.7725 × 1.41 ≈ 2.5066. The standard deviation goes from 0.71 to exactly 1, and 68.27% of the area now lies between −1 and 1 (84.27% on the seed). That is the first number of the 68-95-99.7 rule in section 7. Why: we agree to build a bell whose spread is exactly 1, so that the σ we divide by in step ④ is exactly the standard deviation. Without the 2, putting in σ would give a standard deviation of σ ÷ √2.
  3. x − μ in place of x: e^(−(x − μ)² ÷ 2). With μ = 2, (x − 2)² is 0 at x = 2, so the peak moves there. To get the same height, x has to be 2 bigger, so subtracting 2 moves the curve right. It is the same rule as shifting a graph. The shape does not change, so the area stays 2.5066 and the standard deviation stays 1. Why: this piece puts the middle of the bell (the mean) at μ.
  4. Divide x − μ by σ: e^(−((x − μ) ÷ σ)² ÷ 2). With σ = 1.5, you have to go 1.5 times as far from μ to get the same height. The bell becomes 1.5 times as wide around μ, and the standard deviation goes from 1 to 1.5. The heights do not change, so the area is 1.5 times as big too: 2.5066 × 1.5 = 3.7599. Why: this piece sets the spread. Because step ② made the standard deviation 1, σ is exactly the standard deviation. Written out, the exponent is ((x − μ) ÷ σ)² ÷ 2 = (x − μ)² ÷ (2σ²).
  5. Make the area 1: … ÷ (σ√(2π)). The area right now is the area from step ②, √(2π), times σ. So divide every height by σ√(2π) = 1.5 × 2.5066 ≈ 3.76 and the area becomes 1.0000. The peak is pressed down from 1 to 0.27. Why: all probabilities must add up to 1 (section 1). Without this piece the curve is bell-shaped but not a probability distribution. That the area under e^(−x² ÷ 2) is exactly √(2π) ≈ 2.5066 (the Gaussian integral) is not proved here. We only check it by adding up thin slices.
  6. Done: f(x) = (1 ÷ (σ√(2π))) × e^(−(x − μ)² ÷ (2σ²)). This is the same formula as section 4's e^(−z² ÷ 2) ÷ (σ × √(2π)) with z = (x − μ) ÷ σ. Check by hand: with μ = 2 and σ = 1.5, the height at x = 3.5 (one standard deviation above the mean) is e^(−0.5) ÷ 3.7599 ≈ 0.6065 ÷ 3.7599 ≈ 0.1613.

The way back. Standardizing, z = (x − μ) ÷ σ, walks this path backward. Subtracting μ from every value brings the bell that step ③ moved by μ back to 0, and dividing by σ shrinks the bell that step ④ widened by σ back to a standard deviation of 1. That is why every normal distribution ends up as the same bell with mean 0 and standard deviation 1 (the standard normal distribution). The gray dashed curve at the last step is that bell. Doing the same thing to data is normalization and standardization.

Try this. ① Press "Next" five times and watch the area in the top left: 1.7725 → 2.5066 → 2.5066 → 3.7599 → 1.0000. The standard deviation on the purple line goes 0.71 → 1 → 1 → 1.5 → 1.5. ② At step ④, set σ to 2.5. The area grows to 6.2666. Go one step further and the peak is pressed down to 0.16, while the area is back to 1.0000. ③ Set σ to 0.5. At step ④ the bell gets narrower (area 1.2533), and at step ⑤ the peak is 0.8. That matches section 1: "with standard deviation 0.5 the peak is 0.8". ④ At the last step, set μ to 0 and σ to 1. The green curve lands on the gray dashed one, and the peak is 0.4.

6. Standardizing: the z-score

How many standard deviations a value is from the mean is its z-score. Turning values into z-scores is called standardizing.

z=x−μσ

Example: a test has mean 70 and standard deviation 10. A score of 85 has z = (85 − 70) ÷ 10 = 1.5. If the scores follow a normal distribution, the probability of being below z = 1.5 is 0.9332. So 85 is about 93% of the way up from the bottom, or in the top 7% or so. A negative z means below the mean. A score of 60 has z = −1.

After standardizing, every normal distribution turns into the same one, with mean 0 and standard deviation 1 (the standard normal distribution). That lets you compare things with different units (height and weight, scores in different subjects) by "how far from the mean". The readout of the top lab shows the z-scores of a and b too.

7. The 68-95-99.7 rule

In a normal distribution, these shares are the same whatever the mean and standard deviation are.

RangeProbability insideProbability outside
Mean ± 1 standard deviation68.27%31.73% (about 1 in 3)
Mean ± 2 standard deviations95.45%4.55% (about 1 in 22)
Mean ± 3 standard deviations99.73%0.27% (about 1 in 370)

Knowing these by heart makes quick estimates easy. If heights have mean 170 cm and standard deviation 6 cm and follow a normal distribution, about two in three people are between 164 and 176 cm, and about one in twenty is shorter than 158 cm or taller than 182 cm. If you need exactly 95%, use ± 1.96 standard deviations. The rule only holds for the normal distribution. Other shapes give other shares (section 9).

8. Add things up and you get a bell: the central limit theorem

One die gives 1 to 6 equally often. That is nothing like a bell. But roll two dice and take the mean, and 3.5 (a total of 7) comes up most often. There are six ways to make 7 (1+6, 2+5, 3+4, 4+3, 5+2, 6+1), while 2 and 12 have only one way each. Add more dice and the results crowd toward the middle even more, and the shape gets close to a bell.

Each green bar shows how often "the mean of n dice" came out as that value. The ticks along the bottom are mean values (1 to 6). The orange curve is a normal distribution with mean 3.5 and standard deviation 1.71 ÷ √n, scaled to the same number of rolls. Changing n starts over with 5,000 rolls. "Auto" keeps rolling up to 50,000. Random numbers in a computer come from a fixed calculation, so the same seed (starting value) always gives the same sequence. That is why the same seed and the same n always give the same picture. Change the seed to get different luck.

Try this. ① With n = 1 the six bars are almost the same height, between 798 and 875. The shape is flat and does not match the orange curve. ② With n = 2 you get a triangle. 3.5 comes up most, 854 times, and the ends 1 and 6 only 143 and 125 times. ③ Raise n to 10 or 30 and you get a bell that sits on the orange curve. The standard deviation of the means is 0.54 at n = 10 and 0.31 at n = 30, the same as the formula. ④ Press "Auto" and watch the bars hug the curve more and more closely.

This is the central limit theorem. Add up or average many random values that do not affect each other (independent values), and the result gets close to a normal distribution, whatever shape each one had. There are conditions. Each value must be independent of the others, and the distribution must not throw out huge values so often that its variance is endless.

The spread of an average shrinks with √n.

σX¯=σn

For one die, the variance is the average of the squared distances (−2.5, −1.5, −0.5, 0.5, 1.5, 2.5), which is 17.5 ÷ 6 ≈ 2.92, so the standard deviation is about 1.71. For the mean of 30 dice it is 1.71 ÷ √30 ≈ 0.31. This is why measuring many times and averaging gives a more precise answer. But to cut the spread in half you need 4 times as many measurements, and to cut it to a quarter you need 16 times as many.

9. Plenty of things are not normal

The bell shape is common, but not everything has it. When the shape is different, the mean and standard deviation do not tell the whole story, and the 68-95-99.7 rule does not hold.

ExampleShapeWhy
Income, house pricesA long tail to the right. Most values are on the low side, with rare very large onesNo negative values, and no upper limit. A few very large values pull the mean up, so the mean comes out bigger than the middle value (the median)
Waiting time (until the next customer, the next breakdown)Highest near 0, then a long slope down to the right (exponential distribution)No negative values. You get this shape when the event can happen at any moment at the same rate
One die, a random number between 0 and 1Flat (uniform distribution)Every value is equally likely
Number of heads in 10 coin flips11 bars (binomial distribution). With many flips it gets close to a bellA discrete distribution with only whole numbers 0 to 10

If you treat long-tailed data as normal, you will badly underestimate how often rare large values show up. Before you calculate, draw a bar chart of the data and look at its shape.

10. Key properties

PropertyIn plain words
Total area = 1Some value always comes up. For a continuous distribution, read probability as area
The normal distribution is symmetric around the meanMean = middle value (median) = highest point (mode)
Shifting and stretching valuesAdd the same number to every value and only the mean moves; the variance stays the same. Multiply every value by k and the standard deviation becomes k times, the variance k² times
68-95-99.7The chance of landing within 1, 2, 3 standard deviations of the mean in a normal distribution. The same for every mean and standard deviation
Adding two independent values adds the variancesVariances add. Standard deviations do not simply add: the result is √(σ₁² + σ₂²). The sum of two normal values is normal again

Where is it used?

# With Python's standard library only
from statistics import NormalDist, pvariance, variance
NormalDist(0, 1).cdf(1) - NormalDist(0, 1).cdf(-1)   # 0.6827: mean ± 1 standard deviation
NormalDist(70, 10).cdf(85)                            # 0.9332: probability below z = 1.5
pvariance([2, 4, 5, 6, 8]), variance([2, 4, 5, 6, 8]) # (4, 5): divided by n, and by n − 1

Common questions

What is the normal distribution?

It is a bell-shaped probability distribution: highest in the middle (the mean) and falling off evenly on both sides. Two numbers, the mean and the standard deviation, fix its whole shape. A value made by adding up many small chances usually ends up close to this shape.

What is the difference between variance and standard deviation?

Variance is the average of the squared distances from the mean. Standard deviation is the square root of the variance. The standard deviation has the same unit as the data (cm if the data is in cm), so you can read it directly as "the values are about this far from the mean".

Why do people sometimes divide by n − 1 for the variance?

When you use a small sample to estimate the variance of a much bigger group. Distances measured from the sample's own mean give a slightly smaller sum of squares, so dividing by n − 1 instead of n corrects for that. If your values are the whole group, divide by n.

Can the height of the curve (the probability density) be more than 1?

Yes. In a continuous distribution, probability is the area, not the height. A normal distribution with standard deviation 0.25 has a peak about 1.6 high, but the total area under the curve is still 1.

Does all real-world data follow a normal distribution?

No. Many things have a long tail on one side, like income, or are highest near 0, like waiting times. Before you assume a normal distribution, draw a bar chart of the data and look at its shape first.

Words

Words used hereTextbook term
How often each value comes upProbability distribution
Separate steps / any value in betweenDiscrete / continuous
Height of the curve, how crowded it isProbability density, probability density function (PDF). For discrete bars: probability mass function (PMF)
Probability of a value below xCumulative distribution function (CDF)
Mean of a distributionExpected value, mean. Written μ
Distance to the meanDeviation
Average of the squared distancesVariance. Written σ²
Square root of the varianceStandard deviation. Written σ
Variance divided by n / by n − 1Population variance / sample variance
Bell-shaped distributionNormal distribution, Gaussian distribution
Normal distribution with mean 0 and standard deviation 1Standard normal distribution
How many standard deviations from the meanz-score, standard score. Converting is standardization
68-95-99.7 ruleEmpirical rule, three-sigma rule
Adding things up gives a bellCentral limit theorem
The function used to get areas under the normal curveError function (erf)
A long tail on one sideSkewed distribution, long tail
Flat / waiting time / number of headsUniform / exponential / binomial distribution
Not affecting each otherIndependent
Starting value for random numbersSeed

Sources

← Binomial and Poisson distributionsEntropy and cross-entropy →