Skip to main content

The mean

The mean is the balance point of a dot plot. See why the deviations from it cancel, prove that it is the constant with the smallest squared error, rebuild it from group means with a weighted mean, and watch one penthouse drag it.
S01.3.1L1 Core25 minIn review

Before this lesson

S01.1 Data types and measurement S01.2 Sampling and bias M01.7 Sums, products and sequences

After this lesson you can

  • Compute the mean of a column by hand and in code, and read it as the balance point of a dot plot.
  • Prove that the deviations from the mean add up to zero.
  • Show by algebra that the mean is the constant with the smallest sum of squared deviations, and link this to the baseline prediction of a regression model.
  • Rebuild an overall mean from group means and group shares as a weighted mean.
  • Explain why a single extreme value can move the mean as far as it likes.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

Nine flats rent for 3, 4, 7, 7, 8, 9, 10, 12 and 30 hundred euros a month. Asked for one number that stands for “the rent here”, most people add them up and divide by nine: 10. Yet six of the nine flats pay less than 10, and only two pay more. What is the mean really measuring, why does machine learning lean on it so heavily, and what can one expensive flat do to it?

Intuition

The nine rents come from the flats table of this course, an invented example: nine flats to let, three from each district. Flat F5, the penthouse, pays 30. Put each rent on a number line as a dot of the same weight, and think of the line as a plank. The mean is the point where you could rest the plank on a single support and it would balance.

The nine rents as blue dots on a number line from 0 to 32: 3, 4, two at 7, 8, 9, 10, 12, and the penthouse alone at 30. A vermillion triangle under the line at 10 is labelled mean equals 10. Below, a dashed vermillion line drops from 10, and from it nine purple arrows run out to each rent, one per row, labelled minus 7, minus 6, minus 3, minus 3, minus 2, minus 1, 0, plus 2 and plus 20. Text on the right reads: distances to the left, 7 plus 6 plus 3 plus 3 plus 2 plus 1 equals 22; distances to the right, 0 plus 2 plus 20 equals 22; the deviations sum to zero.
S01.3.1-F1

The nine rents balance on a support at 10. Each arrow is one flat’s deviation, its distance from the mean with a sign. The six on the left add to 22, and so do the three on the right.

Why 10 and not 8, where the bulk of the dots sit? Because a plank balances when the pulls on each side cancel, and a dot far out pulls hard. The penthouse sits 20 to the right of 10; it alone outweighs the five closest flats on the left put together. Six small distances on one side, one huge one on the other: the support has to sit where they cancel, and that is 10.

So the mean is not “the typical value”. It is the centre of mass of the values, and every value counts in proportion to how far out it is.

Build it up

Step 1. A sum over a count. You have used the mean since S01.2 as “add them up, divide by how many”. For the rents the sum is \(90\) and the count is \(9\), so the mean is \({90/9 = 10}\), in hundreds of euros.

Step 2. Measure every value from the mean. Subtract 10 from each rent. The results −7, −6, −3, −3, −2, −1, 0, 2, 20 are the deviations of the figure above. They are the arrows.

Step 3. The deviations cancel. Add them: \({-22 + 22 = 0}\). That is what balance means, and it is no accident of these numbers: the formal section proves it for any list.

Step 4. Score a guess. Suppose you must predict every rent with one number \(c\), and each miss costs its square. The total cost is the sum of squared distances from the rents to \(c\). Try a few values of \(c\) and plot the cost.

A purple U-shaped curve of the sum of squared distances from the nine rents to a constant c, for c from 2 to 18. Its lowest point, a vermillion dot at c equals 10, is labelled c equals 10, the mean: 512. A dashed line marks the floor at 512. At c equals 8 the curve is at 548, labelled 512 plus 9 times 2 squared, and at c equals 14 it is at 656, labelled 512 plus 9 times 4 squared, with orange arrows showing the gap above the floor. A note reads: every other c adds 9 times (10 minus c) squared.
S01.3.1-F2

The cost of predicting every rent with one constant \(c\). It is lowest at the mean, and every other \(c\) pays the same floor of 512 plus an extra \({9\,(10 - c)^2}\).

The curve has its floor at \({c = 10}\). Even the median, 8, the middle of the sorted rents, costs more: \(548\) against \(512\). The formal section shows that the extra is always \(n\) times the squared distance from \(c\) to the mean.

Step 5. Pull on one end. Move the penthouse further out. The other eight dots stay where they are, but the support has to slide right to keep the plank level.

Three dot plots of the nine rents, one above another. In each, eight dots sit between 3 and 12 and the penthouse is at 30, then 48, then 66. A vermillion triangle under each line marks the mean, at 10, 12 and 14, and green arrows run from each mean to the next. A green note reads: the penthouse moves 18, the mean moves 18 over 9, which is 2.
S01.3.1-F5

Moving one value by 18 moves the mean by \({18/9 = 2}\), every time. There is no limit: push the penthouse far enough and the mean goes wherever you like.

The formal statement

NoteDefinition: the mean

For values \({x_1, \dots, x_n}\), the mean is

\[ \bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i . \]

The deviation of value \(i\) is \({x_i - \bar{x}}\). When the values are a sample from a population (S01.2.1), \(\bar{x}\) is a statistic; the population mean is written \(\mu\).

Result 1: the deviations add to zero. Split the sum of deviations into two sums, then use the definition, which says that \({\sum x_i = n\bar{x}}\):

\[ \begin{aligned} \sum_{i=1}^{n} (x_i - \bar{x}) &= \sum_{i=1}^{n} x_i - \sum_{i=1}^{n} \bar{x} \\ &= n\bar{x} - n\bar{x} \\ &= 0 . \end{aligned} \]

The second sum adds the same number \(\bar{x}\) to itself \(n\) times. This is the balance of the anchor figure: the negative deviations exactly cancel the positive ones.

Result 2: the mean is the best constant under squared error. For any number \(c\),

\[ \begin{aligned} \sum_{i=1}^{n} (x_i - c)^2 &= \sum_{i=1}^{n} (x_i - \bar{x})^2 \\ &\quad + n\,(\bar{x} - c)^2 . \end{aligned} \]

Why. Write each distance to \(c\) as a distance to the mean plus a fixed step: \({x_i - c} = {(x_i - \bar{x})} + {(\bar{x} - c)}\). Square it, using \({(a + b)^2} = {a^2 + 2ab + b^2}\):

\[ \begin{aligned} (x_i - c)^2 &= (x_i - \bar{x})^2 \\ &\quad + 2\,(x_i - \bar{x})(\bar{x} - c) \\ &\quad + (\bar{x} - c)^2 . \end{aligned} \]

Now add over \(i\). The first terms give \({\sum (x_i - \bar{x})^2}\). In the middle terms the factor \({2(\bar{x} - c)}\) is the same for every \(i\), so it comes out of the sum, leaving \({2(\bar{x} - c)\sum (x_i - \bar{x})}\), which is zero by Result 1. The last term is the same for every \(i\), so it adds up to \({n(\bar{x} - c)^2}\). That proves the identity.

The first part on the right does not depend on \(c\). The second part is a square times \(n\), so it is never negative, and it is zero only when \({c = \bar{x}}\). So no constant has a smaller sum of squared deviations than the mean. For the rents, \({\sum (x_i - 10)^2 = 512}\), and any other \(c\) adds \({9\,(10 - c)^2}\): the curve of Step 4. No calculus was needed.

This is why the mean is the baseline of regression. A model that ignores its inputs and predicts one number, scored by mean squared error, does best by predicting the mean of its training targets; lesson ML01.7.4 uses that constant as the yardstick every model must beat.

Result 3: the overall mean from group means. Split the \(n\) values into groups \(g\), of sizes \(n_g\) and means \(\bar{x}_g\). The total of group \(g\) is \(n_g \bar{x}_g\), and the overall total is the sum of the group totals. Divide by \(n\):

\[ \begin{aligned} \bar{x} &= \frac{1}{n}\sum_g n_g\,\bar{x}_g \\ &= \sum_g \cparam{w_g}\,\bar{x}_g , \end{aligned} \]

where \({\cparam{w_g} = n_g / n}\).

The overall mean is the weighted mean of the group means, each weighted by its group’s share \(\cparam{w_g}\) of the rows. The shares add to 1. This is the formula S01.2.3 used for strata, \({\mu = \sum_g W_g \mu_g}\), now read the other way: from the parts back to the whole.

Result 4: one value moves the mean by its change over \(n\). Replace \(x_k\) by \({x_k + \Delta}\). The sum grows by \(\Delta\), so the mean grows by \({\Delta / n}\). Since \(\Delta\) can be as large as you like, so can the change in the mean. A summary that one bad value can carry anywhere is called not resistant; the median of the next lesson is the resistant one.

Go deeper: why the balance picture is exact, not a metaphor

A plank with equal weights at \({x_1, \dots, x_n}\) balances on a support at \(c\) when the turning effects on the two sides cancel. The turning effect of a weight is its weight times its signed distance from the support, so the plank balances exactly when \({\sum (x_i - c) = 0}\). Solve for \(c\): \({\sum x_i - nc = 0}\), so \({c = \bar{x}}\). Result 1 is the same statement read backwards. With unequal weights \(w_i\), the balance point is \({\sum w_i x_i / \sum w_i}\), the weighted mean of Result 3.

Worked example

The overall mean rebuilt from the three districts. The nine flats are three from each district. Their district means, from the flats table, are:

  • South: F3, F6, F8 pay 3, 8 and 4, so the mean is \({15 / 3 = 5}\).
  • North: F1, F4, F9 pay 7, 10 and 7, so the mean is \({24 / 3 = 8}\).
  • Centre: F2, F5, F7 pay 12, 30 and 9, so the mean is \({51 / 3 = 17}\).

In the table each district holds one third of the rows, so by Result 3 the overall mean is \({\tfrac13 \cdot 5} + {\tfrac13 \cdot 8} + {\tfrac13 \cdot 17} = 10\), as before.

The city those flats came from (S01.2.3) has 500 flats to let: 250 in South, 150 in North and 100 in Centre. Its shares are 0.5, 0.3 and 0.2, and with the same district means:

\[ \begin{aligned} \mu &= 0.5 \cdot 5 + 0.3 \cdot 8 + 0.2 \cdot 17 \\ &= 2.5 + 2.4 + 3.4 \\ &= 8.3 . \end{aligned} \]

Two horizontal bars built from three vermillion boxes each. Top bar, titled The table: 3 flats per district, shares one third each: South 5 times one third equals 1.67, North 8 times one third equals 2.67, Centre 17 times one third equals 5.67, with an arrow to mean 10. Bottom bar, titled The city: 250, 150, 100 of 500 flats, shares 0.5, 0.3, 0.2: South 5 times 0.5 equals 2.5, North 8 times 0.3 equals 2.4, Centre 17 times 0.2 equals 3.4, with an arrow to mean 8.3. A note reads: each width is a district mean times its share.
S01.3.1-F3

The same three district means, weighted two ways. Each box’s width is one district’s contribution. The table gives Centre a third of the weight; the city gives it a fifth.

The two answers differ only in the weights. The table over-represents Centre, the dearest district, so its mean of 10 overstates the city’s 8.3. Same means, different shares, different answer.

Code

First from scratch, with a loop, so that nothing is hidden.

rents = [7, 12, 3, 10, 30, 8, 9, 4, 7]           # F1 to F9, hundreds of euros a month

total = 0
for x in rents:
    total += x
x_bar = total / len(rents)
deviations = [x - x_bar for x in rents]

def squared_cost(c, values):
    """The sum of squared distances from the values to the constant c."""
    return sum((x - c) ** 2 for x in values)

print("mean:", x_bar, "  sum of deviations:", sum(deviations))
for c in (7, 8, 10, 14):
    print(f"c = {c:>2}: cost {squared_cost(c, rents):.0f}")
mean: 10.0   sum of deviations: 0.0
c =  7: cost 593
c =  8: cost 548
c = 10: cost 512
c = 14: cost 656

Every cost matches \({512 + 9\,(10 - c)^2}\). Now the group means, the weighted means and the penthouse, with NumPy.

import numpy as np

districts = ["North", "Centre", "South", "North", "Centre", "South", "Centre", "South", "North"]
x = np.array(rents, dtype=float)
group_means = {g: x[[d == g for d in districts]].mean() for g in ("South", "North", "Centre")}
means = np.array(list(group_means.values()))

table_mean = np.average(means, weights=[3, 3, 3])          # counts work as weights too
city_mean = np.average(means, weights=[250, 150, 100])
print(group_means, " table:", table_mean, " city:", round(city_mean, 2))

for penthouse in (30, 48, 66):
    moved = x.copy()
    moved[4] = penthouse                                   # F5 is the fifth flat
    print(f"penthouse {penthouse}: mean {np.mean(moved):.2f}")
print("without the penthouse:", np.mean(np.delete(x, 4)))
{'South': np.float64(5.0), 'North': np.float64(8.0), 'Centre': np.float64(17.0)}  table: 10.0  city: 8.3
penthouse 30: mean 10.00
penthouse 48: mean 12.00
penthouse 66: mean 14.00
without the penthouse: 7.5

numpy.average divides by the sum of the weights, so counts and shares give the same answer. Without the penthouse the mean falls from 10 to 7.5: one flat out of nine moved it by 2.5.

Explore

This module’s interactive, Balance and middle, lives in lesson S01.3.3. There you can drag any rent and watch the balance point slide by one ninth of every move.

Pitfalls

Two panels of three blue bars whose heights are the district mean rents, South 5, North 8 and Centre 17. Left panel, marked with a cross and titled Equal weights: 10: the bars have equal widths, a note reads each district counted as one third of the city, and a vermillion line at height 10 is labelled overall 10. Right panel, marked with a tick and titled City shares: 8.3: the bars have widths 0.5, 0.3 and 0.2, a note reads each width is the district's share, and a green line at height 8.3 is labelled overall 8.3.
S01.3.1-F4

Wrong beside right. Averaging the three district means treats the districts as equal in size. Weighting each by its share of the city gives the city’s mean. The overall mean is the height of a flat bar with the same total area.

“The overall mean is the mean of the group means.”

Only when the groups are the same size. In the city, South has two and a half times as many flats as Centre, so the plain average of 5, 8 and 17 (which is 10) overstates the city mean of 8.3. Weight each group mean by its share. S01.7 shows that getting the weights wrong can even reverse a comparison.

“The mean is the typical value.”

It is the balance point, and a long tail drags it. Six of the nine flats pay less than the mean of 10, and only two pay more. When a column has a long tail (incomes, house prices, waiting times), quote the median of the next lesson beside the mean, or instead of it.

“The mean is the best single guess, whatever the scoring.”

It is the best guess when misses are scored by their squares. Score them by their size instead (mean absolute error) and the mean loses: the total distance from the rents to 10 is 44, but to the median, 8, it is only 40. The next lesson shows why the median wins that contest. Pick the summary that matches the loss.

“Ratings are coded 1 to 4, so their mean is the average rating.”

A mean adds distances, and an ordinal column such as the energy rating has an order but no distances (S01.1.2). The mean of the codes depends on which codes you chose. Use the median or the mode for such a column.

Check yourself

Recall

1. What is the mean of the nine rents, and how many flats pay less than it?

Show answer

\({90 / 9 = 10}\) hundred euros. Six flats pay less (3, 4, 7, 7, 8 and 9), one pays exactly 10 and two pay more.

2. What do the deviations from the mean always add up to, and why?

Show answer

Zero. Their sum is \({\sum x_i - n\bar{x}}\), and \({\sum x_i = n\bar{x}}\) by the definition of the mean.

3. Which constant \(c\) makes \({\sum (x_i - c)^2}\) as small as possible, and by how much does any other \(c\) exceed that minimum?

Show answer

The mean, \(\bar{x}\). Any other \(c\) adds \({n(\bar{x} - c)^2}\).

4. Three groups have means 5, 8 and 17 and shares 0.5, 0.3 and 0.2. What is the overall mean?

Show answer

\({0.5 \cdot 5} + {0.3 \cdot 8} + {0.2 \cdot 17} = 8.3\).

5. One of nine values rises by 18. How far does the mean move?

Show answer

By \({18 / 9 = 2}\), in the same direction.

Exercises

1. ★ Find the mean of the eight rents left when the penthouse is removed, list their deviations, and check that the deviations add to zero.

Show solution

The eight rents 3, 4, 7, 7, 8, 9, 10, 12 add to 60, so the mean is \({60 / 8 = 7.5}\). The deviations are −4.5, −3.5, −0.5, −0.5, 0.5, 1.5, 2.5, 4.5. The negative ones add to \({-9}\) and the positive ones to \({+9}\), so the total is 0.

2. ★★ (a) A tenth flat joins the nine. What rent must it have for the mean of all ten to be 11? (b) Without adding up nine squares, find \({\sum (x_i - 7)^2}\) for the nine rents, then check it directly.

Show solution

(a) Ten flats with mean 11 have a total of 110. The nine pay 90, so the tenth must pay \({110 - 90 = 20}\).

(b) By Result 2 with \({c = 7}\): \({512 + 9\,(10 - 7)^2} = {512 + 81} = 593\). Directly, the distances from 7 are −4, −3, 0, 0, 1, 2, 3, 5, 23, whose squares are 16, 9, 0, 0, 1, 4, 9, 25 and 529, and these add to 593.

3. ★★★ Prove that if every value is changed to \({y_i = a x_i + b}\), then \({\bar{y} = a\bar{x} + b}\). Use it to give the mean rent in euros, and the mean after a service charge of 50 euros is added to every rent.

Show solution

Split the sum and take the constants out:

\[ \begin{aligned} \bar{y} &= \frac{1}{n}\sum_{i=1}^{n} (a x_i + b) \\ &= \frac{a}{n}\sum_{i=1}^{n} x_i + \frac{1}{n}\cdot nb \\ &= a\bar{x} + b . \end{aligned} \]

Rents are in hundreds of euros, so in euros \({a = 100}\), \({b = 0}\) and the mean is 1,000 euros. A service charge of 50 euros is \({b = 0.5}\) hundred euros, so the mean becomes 10.5 hundred euros, or 1,050 euros. The mean follows every change of unit and every shift exactly.

Summary card

The mean is the balance point: the deviations from it cancel.

The anchor figure again: the nine rents as blue dots balanced on a vermillion triangle at 10, with purple deviation arrows that add to 22 on each side.
S01.3.1-F1

The nine rents balance at 10.

  • Definition. \({\bar{x} = \frac{1}{n}\sum x_i}\), and \({\sum (x_i - \bar{x}) = 0}\).
  • Best constant. \({\sum (x_i - c)^2} = {\sum (x_i - \bar{x})^2} + {n(\bar{x} - c)^2}\).
  • From groups. \({\bar{x} = \sum_g \cparam{w_g}\,\bar{x}_g}\) with \({\cparam{w_g} = n_g / n}\).
  • Not resistant. Changing one value by \(\Delta\) moves the mean by \({\Delta / n}\).

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • David Diez, Mine Cetinkaya-Rundel and Christopher Barr, OpenIntro Statistics, 4th edition (free PDF). Section 2.1.2, Dot plots and the mean, defines the sample mean \(\bar{x}\) as the sum of the observations over their number and describes it as the balancing point of the distribution, drawn as a triangle under a dot plot.
  • NIST/SEMATECH, e-Handbook of Statistical Methods, Section 1.3.5.1, Measures of location: the mean as the sum of the data points over their number, and the warning that extreme values in the tails distort the mean, for which the median is the better estimate of location.
  • NumPy documentation, numpy.average: the weighted average is the sum of the values times the weights over the sum of the weights, so the weights need not add to 1.

Next. S01.3.2 The median, quartiles and the five-number summary finds the resistant middle that the penthouse cannot drag.

This lesson unlocks

S01.3.2 The median, quartiles and the five-number summary

Your progress is saved in this browser only. No account, no tracking.
Back to top