Asiman Data School

Module 2, topic 18, week 9, Thursday

Descriptive statistics: mean, median, variance, standard deviation

From data to a few well-chosen numbers: the kinds of data, frequency tables, mean, median and mode, quartiles and the box plot, variance and standard deviation, outliers, shape and rescaling. Worked by hand.

Introduction: from data to a few numbers

The sixth lesson of Module 2, and the first one on statistics. The last two lessons were about probability: the distribution was known, and we calculated what the data would look like. From today the direction is turned around. We start with data, a column of numbers that somebody collected, and ask what it tells us. The first step is always the same: describe the data with a few well-chosen numbers. Where is its centre? How far do the values spread? Is it symmetric or lopsided? Are there values that do not fit? This is descriptive statistics. This guide teaches the measures of centre (mean, median, mode), the measures of spread (range, quartiles, variance, standard deviation), how to read the shape of data, how to find outliers, and how these numbers change when the data is rescaled. Every idea comes as a definition and worked examples calculated by hand. Then the guide walks through 26 solved problems from the easiest to the hardest, and at the end there are 40 practice problems with a hint and a full solution for each.

You need the last lesson only lightly: the mean μ\mu and the standard deviation σ\sigma of a distribution, the z-score and the 68-95-99.7 rule. A calculator with a square root key is enough. As in the whole module, the work is done by hand; Python appears only in one short part near the end.

What you will learn

  1. 1.Why every analysis and every machine learning project starts with descriptive statistics
  2. 2.The kinds of data (categorical and numerical) and which summaries fit each kind
  3. 3.Frequency tables, relative and cumulative frequencies, and histograms
  4. 4.The mean, its properties, the weighted mean and the mean from a frequency table
  5. 5.The median and the mode, and when the mean misleads
  6. 6.The range, quartiles, percentiles, the interquartile range, the five-number summary and the box plot
  7. 7.The variance and the standard deviation, for a population and for a sample, and why a sample divides by n−1n - 1
  8. 8.How to read a standard deviation: z-scores, the 68-95-99.7 rule, Chebyshev's rule and the coefficient of variation
  9. 9.What happens to the summaries when the data is shifted or rescaled; standardising and min-max scaling
  10. 10.The shape of data: skewness, outliers and robust statistics
  11. 11.Data in classes (grouped data)
  12. 12.Where these numbers appear in machine learning
Notation used in this lesson
WrittenMeaning
x1,x2,…,xnx_1, x_2, \dots, x_nthe values of the data
nnthe number of values in a sample; NN for a whole population
∑x\sum xthe sum of all values (∑\sum is the Greek capital letter sigma, "sum")
xˉ\bar{x}the mean of a sample ("x bar")
μ\muthe mean of a population
s2s^2, ssthe variance and the standard deviation of a sample
σ2\sigma^2, σ\sigmathe variance and the standard deviation of a population
Q1Q_1, Q2Q_2, Q3Q_3the first, second and third quartile; Q2Q_2 is the median
IQR\text{IQR}the interquartile range, Q3−Q1Q_3 - Q_1
ffa frequency: how often a value appears
zza z-score: the value minus the mean, divided by the standard deviation

1Why descriptive statistics?

1.1Nobody can read a million rows

A table of 300 orders can still be looked at. A table of a million rows cannot. Yet a decision has to be made from it: is the typical order large or small, are the delivery times reliable, did the new design change anything? Descriptive statistics replaces the column by a handful of numbers that a person can hold in the head:

  • 300 delivery times become: typical 31 minutes, spread about 6 minutes, slowest 58 minutes.

A good summary answers three questions:

  1. Where is the centre? What is a typical value?
  2. How large is the spread? How far are the values from the centre and from each other?
  3. What is the shape? Symmetric or lopsided, one peak or two, with or without unusual values?

1.2Describing and concluding

Statistics has two halves:

The two halves of statistics
Descriptive statisticsInferential statistics
QuestionWhat does this data look like?What does it say about the population behind it?
Toolsmean, median, standard deviation, tables, chartstests, confidence intervals
Topicsthis lessonTopics 19 and 20

The second half is built on the first. A test in Topic 19 compares a sample mean with a claim; a confidence interval in Topic 20 is the sample mean plus or minus a multiple of the standard error sn\dfrac{s}{\sqrt{n}}. Both need the xˉ\bar{x} and the ss of this lesson.

1.3Where you will meet these ideas again

Later topics and what each takes from this lesson
Later in the courseWhat it takes from this lesson
Hypothesis testing (Topic 19)the sample mean and the sample standard deviation
Confidence intervals and correlation (Topic 20)xˉ\bar{x}, ss and the standard error sn\dfrac{s}{\sqrt{n}}
Linear regressionthe mean squared error is a variance; the best constant prediction is the mean
Feature scaling (Topic 24)standardising with the mean and the standard deviation; min-max scaling
Handling outliers (Topic 25)quartiles, the IQR rule, z-scores
Missing valuesfilling a gap with the mean or the median
Decision trees and random forestsa leaf predicts the mean of its rows and splits to reduce the variance
Exploring any new data setdescribe() in pandas prints the numbers of this lesson

2Data and its kinds

2.1Population and sample

The last lesson introduced the words. The population is everything we want to know about; a sample is the part we observed. A number that describes the population is a parameter (μ\mu, σ\sigma); a number calculated from the sample is a statistic (xˉ\bar{x}, ss). Most data is a sample, and the difference matters once in this lesson: in the formula of the variance (Part 7).

2.2Kinds of variables

A column of a table is a variable. What can be calculated with it depends on its kind.

The four kinds of variables
KindWhat its values areExamplesSummaries that make sense
Categorical, nominalnames without an ordercity, colour, payment methodcounts, shares, mode
Categorical, ordinalnames with an orderrating (bad, good, excellent), level of educationcounts, mode, median
Numerical, discretecountsnumber of children, orders per dayeverything
Numerical, continuousmeasurementsheight, price, timeeverything

Two traps:

  • A number is not always numerical. A postal code, a phone number or a shirt number is a name written with digits. The "mean postal code" means nothing.
  • For an ordinal variable the distances are unknown. Is "excellent" as far from "good" as "good" is from "bad"? That is why its mean is doubtful, while its median is fine.

What kind of variable is it?

1 of 8

The city in which a customer lives.

Try it yourself

Name the kind of each variable. (a) The brand of a phone. (b) The number of brothers and sisters of a student. (c) The satisfaction of a customer: low, medium or high. (d) The temperature of a room.

Show a hint
First ask: is it a name or a number? For a name: is there an order? For a number: is it counted or measured?
Show the solution

Answer: (a) nominal, (b) discrete, (c) ordinal, (d) continuous.

(a) Brands are names without an order: nominal. (b) A count of people: discrete. (c) Low, medium, high have an order, but the steps between them are not numbers: ordinal. (d) A measurement that can take every value in an interval: continuous.

3Frequency tables and histograms

3.1Counting the values

Twenty families were asked how many children they have:

21031210214120132102\begin{aligned} & 2 \quad 1 \quad 0 \quad 3 \quad 1 \quad 2 \quad 1 \quad 0 \quad 2 \quad 1 \\ & 4 \quad 1 \quad 2 \quad 0 \quad 1 \quad 3 \quad 2 \quad 1 \quad 0 \quad 2 \end{aligned}

A frequency table counts how often every value appears:

The number of children in 20 families
ChildrenFrequency ffRelative frequencyCumulative frequency
04420=0.20\tfrac{4}{20} = 0.204
17720=0.35\tfrac{7}{20} = 0.3511
26620=0.30\tfrac{6}{20} = 0.3017
32220=0.10\tfrac{2}{20} = 0.1019
41120=0.05\tfrac{1}{20} = 0.0520
Total201
  • The frequency is the count. All frequencies together are nn.
  • The relative frequency is the share, fn\dfrac{f}{n}. All shares together are 1. It is what the last lesson called a probability, now measured from data.
  • The cumulative frequency counts all values up to this one: 17 families have at most 2 children.

3.2Classes and histograms

A continuous variable has too many different values for such a table. Its values are collected in classes (intervals), and the table counts the values in each class. Drawn as bars that touch each other, it is a histogram.

The ages of 30 customers in classes of ten years:

The ages of 30 customers in classes
Age20 to 3030 to 4040 to 5050 to 6060 to 70
Frequency491052

By agreement a value on a border belongs to the class that begins there: a customer of exactly 30 is counted in "30 to 40".

491052203040506070
A histogram of the ages of the 30 customers. Every bar is a class of ten years; its height is the number of customers in the class, written over it. The bars touch, because the classes follow each other without a gap.

3.3What a histogram shows

medianmean

Symmetric

mean and median almost equal

medianmean

Skewed to the right

the tail pulls the mean above the median

medianmean

Skewed to the left

the tail pulls the mean below the median

The three shapes of a histogram, each with its mean and its median. The name follows the tail, not the peak.
  • Symmetric: the left and the right half look alike. The mean and the median are almost equal.
  • Skewed to the right: a long tail of large values (incomes, prices, waiting times). The tail pulls the mean above the median.
  • Skewed to the left: a long tail of small values (marks of an easy exam). The mean lies below the median.

The name follows the tail, not the peak. A histogram also shows whether the data has one peak or two (two peaks often mean two mixed groups) and whether single values stand far away from the rest.

Try it yourself

25 students were asked how many books they read last month: 5 students read none, 8 read one book, 7 read two and 5 read three. Find the relative frequency of "one book", the cumulative frequency of "two books", and the share of students who read at most one book.

Show a hint
The relative frequency is fn\dfrac{f}{n} with n=25n = 25. The cumulative frequency adds all frequencies up to the value.
Show the solution

Answer: 0.320.32, 20 students, and 0.520.52.

one book:825=0.32up to two books:5+8+7=20at most one book:5+825=1325=0.52\begin{aligned} \text{one book:} \quad & \tfrac{8}{25} = 0.32 \\[4pt] \text{up to two books:} \quad & 5 + 8 + 7 = 20 \\[4pt] \text{at most one book:} \quad & \tfrac{5 + 8}{25} = \tfrac{13}{25} = 0.52 \end{aligned}

4The mean

4.1Definition

The mean (the arithmetic average) is the sum of the values divided by their number:

xˉ=∑xn=x1+x2+⋯+xnn\bar{x} = \frac{\sum x}{n} = \frac{x_1 + x_2 + \dots + x_n}{n}

For a whole population the same calculation is written μ=∑xN\mu = \dfrac{\sum x}{N}.

Example. The marks of five students are 6, 9, 4, 10 and 6.

xˉ=6+9+4+10+65=355=7\bar{x} = \frac{6 + 9 + 4 + 10 + 6}{5} = \frac{35}{5} = 7

4.2The mean is the balance point

The distance of a value from the mean, x−xˉx - \bar{x}, is its deviation. For the five marks:

The deviations of the five marks from their mean
xx694106Sum
x−xˉx - \bar{x}−1-1+2+2−3-3+3+3−1-10

The deviations always add up to exactly 0: what lies above the mean balances what lies below. Put the values as equal weights on a ruler, and the ruler balances at the mean.

45678910
The five marks as equal weights on a ruler. The ruler balances at the mean, 7: the distances to the left of it (1 + 1 + 3) are as large as the distances to the right (2 + 3).

Three consequences:

  • The sum of the data is n×xˉn \times \bar{x}. Five marks with the mean 7 add up to 35, whatever they are.
  • Every value takes part. Change one value and the mean changes.
  • One extreme value can pull the mean far away (Part 5).

4.3The weighted mean

Sometimes the values do not count equally. A course grade is made of homework (20 percent), a midterm (30 percent) and a final exam (50 percent). A student has 80, 70 and 90 marks:

weighted mean=0.2×80+0.3×70+0.5×90=16+21+45=82\begin{aligned} \text{weighted mean} &= 0.2 \times 80 + 0.3 \times 70 + 0.5 \times 90 \\ &= 16 + 21 + 45 = 82 \end{aligned}

In general, with the weights ww:

weighted mean=∑(w×x)∑w\text{weighted mean} = \frac{\sum (w \times x)}{\sum w}

When the weights add up to 1, as above, the division disappears. The expected value of the last lesson is a weighted mean with the probabilities as weights.

Combining groups. Class A has 20 students with the mean 70; class B has 30 students with the mean 80. The mean of all 50 is not 75. Weight every mean with the size of its group:

20×70+30×8020+30=1400+240050=76\frac{20 \times 70 + 30 \times 80}{20 + 30} = \frac{1400 + 2400}{50} = 76

4.4The mean from a frequency table

For the 20 families of Part 3 nobody adds twenty numbers. Multiply every value by its frequency:

∑(f×x)=4×0+7×1+6×2+2×3+1×4=0+7+12+6+4=29xˉ=∑(f×x)n=2920=1.45\begin{aligned} \sum (f \times x) &= 4 \times 0 + 7 \times 1 + 6 \times 2 \\ &\quad + 2 \times 3 + 1 \times 4 \\ &= 0 + 7 + 12 + 6 + 4 = 29 \\[6pt] \bar{x} &= \frac{\sum (f \times x)}{n} = \frac{29}{20} = 1.45 \end{aligned}

This is the weighted mean again, with the frequencies as weights.

Try it yourself

(a) Find the mean of 5, 8, 11, 6 and 10, and the sum of the five deviations from it. (b) A shop sold 30 items at the price 4 and 10 items at the price 8. Find the mean price of an item that was sold.

Show a hint
For (b), weight the two prices with the numbers of items. The simple mean of 4 and 8 is not the answer.
Show the solution

Answer: (a) The mean is 8 and the deviations add up to 0. (b) The mean price is 5.

(a)xˉ=5+8+11+6+105=405=8deviations: −3,  0,  3,  −2,  2their sum: 0(b)30×4+10×830+10=20040=5\begin{aligned} \text{(a)} \quad & \bar{x} = \frac{5 + 8 + 11 + 6 + 10}{5} = \frac{40}{5} = 8 \\[4pt] & \text{deviations: } -3,\; 0,\; 3,\; -2,\; 2 \\[2pt] & \text{their sum: } 0 \\[8pt] \text{(b)} \quad & \frac{30 \times 4 + 10 \times 8}{30 + 10} = \frac{200}{40} = 5 \end{aligned}

The deviations of any data from its own mean add up to 0. In (b) three quarters of the items cost 4, so the mean lies much nearer to 4 than to 8.

5The median and the mode

5.1The median

The median is the value in the middle of the sorted data: half of the values lie below it and half above.

  1. Sort the values.
  2. If nn is odd, the median is the middle value, number n+12\dfrac{n + 1}{2} in the list.
  3. If nn is even, the median is the mean of the two middle values, numbers n2\dfrac{n}{2} and n2+1\dfrac{n}{2} + 1.

An odd number of values. 12, 7, 3, 15, 9:

sorted:3791215median=9\begin{aligned} \text{sorted:} \quad & 3 \quad 7 \quad \boxed{9} \quad 12 \quad 15 \\[4pt] \text{median} &= 9 \end{aligned}

It is value number 3 of 5.

An even number of values. 8, 3, 10, 5, 12, 7:

sorted:35781012median=7+82=7.5\begin{aligned} \text{sorted:} \quad & 3 \quad 5 \quad \boxed{7 \quad 8} \quad 10 \quad 12 \\[4pt] \text{median} &= \frac{7 + 8}{2} = 7.5 \end{aligned}

5.2When the mean misleads

Five employees of a small company earn 800, 900, 1000, 1100 and 1200 a month.

mean=50005=1000median=1000\begin{aligned} \text{mean} &= \frac{5000}{5} = 1000 \\[4pt] \text{median} &= 1000 \end{aligned}

Now add the owner, who earns 7000:

mean=12,0006=2000median=1000+11002=1050\begin{aligned} \text{mean} &= \frac{12{,}000}{6} = 2000 \\[4pt] \text{median} &= \frac{1000 + 1100}{2} = 1050 \end{aligned}

"The mean salary is 2000" is true and useless: five of the six people earn far less. The median hardly moved. One extreme value can move the mean as far as it likes; it cannot move the median, because the median only asks which value stands in the middle, not how far away the others are.

Five employees earn 800, 900, 1000, 1100 and 1200. Move the salary x of the owner.

meanmedian02,0004,0006,0008,00010,000

Mean: (5,000 + 7,000) / 6 = 2,000

Median: (1,000 + 1,100) / 2 = 1,050

The mean follows the one large value. The median only asks which values stand in the middle, so it does not move at all.

A statistic that extreme values hardly change is called robust. The median is robust; the mean is not.

The mean and the median side by side
MeanMedian
Usesevery valueonly the order of the values
Extreme valuespull it stronglyhardly change it
Best forsymmetric data without outliersskewed data, data with outliers
Typical usemarks, heights, measurement errorsincomes, prices of flats, waiting times

Mean and median together show the shape. Mean above the median: a tail to the right. Mean below the median: a tail to the left. Almost equal: roughly symmetric.

5.3The mode

The mode is the value that appears most often. Shoe sizes sold in a shop: 38, 40, 40, 41, 42, 40, 39, 42. The mode is 40, which appears three times.

  • Data can have two modes (bimodal) or more, or none when every value appears once.
  • The mode is the only measure of centre for a nominal variable: the most frequent city, the most popular colour.
  • For continuous data single values rarely repeat; there the modal class is used, the highest bar of the histogram.

5.4A compromise: the trimmed mean

The trimmed mean removes a fixed share of the smallest and of the largest values and takes the mean of the rest. Judges' scores in some sports work this way: the highest and the lowest score are dropped. For the six salaries, dropping the smallest and the largest value leaves 900, 1000, 1100, 1200 with the mean 1050.

Try it yourself

The waiting times of six customers in minutes: 3, 9, 4, 30, 6, 8. Find the mean and the median. Which of the two describes a typical waiting time better?

Show a hint
Sort first. With six values the median is the mean of the 3rd and the 4th.
Show the solution

Answer: The mean is 10, the median is 7. The median describes a typical wait better.

mean=3+9+4+30+6+86=606=10sorted:3468930median=6+82=7\begin{aligned} \text{mean} &= \frac{3 + 9 + 4 + 30 + 6 + 8}{6} \\[2pt] &= \frac{60}{6} = 10 \\[6pt] \text{sorted:} \quad & 3 \quad 4 \quad \boxed{6 \quad 8} \quad 9 \quad 30 \\[4pt] \text{median} &= \frac{6 + 8}{2} = 7 \end{aligned}

Five of the six customers waited less than 10 minutes. The one wait of 30 minutes pulls the mean up, so the median is the better description.

6Spread by position: range, quartiles and the box plot

6.1Why the centre is not enough

Two machines fill bags of 500 g. Both have the mean 500 g. The bags of machine A weigh between 498 and 502 g; those of machine B between 470 and 530 g. Nobody would call them equally good. A summary needs a number for the spread.

mean 500470480490500510520530

Machine A

range 4

mean 500470480490500510520530

Machine B

range 60

The bags of two machines. Both have the mean 500 g, the dashed line. The bags of machine A lie close together; those of machine B are spread from 470 to 530 g.

6.2The range

range=largest value−smallest value\text{range} = \text{largest value} - \text{smallest value}

Machine A: 502−498=4502 - 498 = 4. Machine B: 530−470=60530 - 470 = 60. The range is the simplest measure and the most fragile one: it uses only two values, and a single unusual value changes it completely.

6.3Quartiles

The quartiles cut the sorted data into four parts with the same number of values:

  • Q1Q_1, the first quartile: a quarter of the values lie below it.
  • Q2Q_2, the second quartile: the median.
  • Q3Q_3, the third quartile: three quarters of the values lie below it.

How to find them. Sort the data and number the values from 1 to nn. The quartile for the share pp (0.25, 0.5 or 0.75) stands at the position

position=1+(n−1)×p\text{position} = 1 + (n - 1) \times p

If the position is a whole number, the quartile is that value. If it lies between two values, go the same fraction of the way from the lower to the upper one.

Example. Eleven values, already sorted:

Eleven sorted values and their positions
Position1234567891011
Value35789111315161821
Q1: position 1+10×0.25=3.5halfway between 7 and 8:Q1=7.5Q2: position 1+10×0.50=6the 6th value:Q2=11Q3: position 1+10×0.75=8.5halfway between 15 and 16:Q3=15.5\begin{aligned} & Q_1\text{: position } 1 + 10 \times 0.25 = 3.5 \\ & \quad \text{halfway between 7 and 8:} \quad Q_1 = 7.5 \\[6pt] & Q_2\text{: position } 1 + 10 \times 0.50 = 6 \\ & \quad \text{the 6th value:} \quad Q_2 = 11 \\[6pt] & Q_3\text{: position } 1 + 10 \times 0.75 = 8.5 \\ & \quad \text{halfway between 15 and 16:} \quad Q_3 = 15.5 \end{aligned}

A position such as 2.75 means: start at the 2nd value and go 0.75 of the way to the 3rd.

6.4Percentiles

Quartiles are special percentiles. The 90th percentile is the value below which 90 percent of the data lie; its position is 1+(n−1)×0.901 + (n - 1) \times 0.90. Q1Q_1, the median and Q3Q_3 are the 25th, 50th and 75th percentile. "Your result is in the 90th percentile" means: you did better than 90 percent of the group.

6.5The interquartile range

IQR=Q3−Q1\text{IQR} = Q_3 - Q_1

It is the width of the middle half of the data. For the eleven values: IQR=15.5−7.5=8\text{IQR} = 15.5 - 7.5 = 8. Like the median, the IQR is robust: the smallest and the largest quarter of the data can do what they like.

6.6The five-number summary and the box plot

Five numbers describe centre, spread and shape at once:

The five-number summary of the eleven values
MinimumQ1Q_1MedianQ3Q_3Maximum
37.51115.521

The box plot draws them: a box from Q1Q_1 to Q3Q_3 with a line at the median, and "whiskers" out to the smallest and the largest value that are not outliers.

36912151821minimumQ1medianQ3maximum36912151821
The eleven values and their box plot. The box reaches from Q1 to Q3 and holds the middle half of the data; its width is the IQR. The line inside is the median, and the whiskers go out to the smallest and the largest value.

How to read it: a median in the middle of the box and whiskers of equal length mean symmetric data. A median near the left edge and a long right whisker mean a tail to the right.

6.7Outliers: the 1.5 IQR rule

A value far away from the rest is an outlier. The usual rule builds two fences:

lower fence=Q1−1.5×IQRupper fence=Q3+1.5×IQR\begin{aligned} \text{lower fence} &= Q_1 - 1.5 \times \text{IQR} \\[4pt] \text{upper fence} &= Q_3 + 1.5 \times \text{IQR} \end{aligned}

A value outside the fences is called an outlier and is drawn as a single point in a box plot.

For the eleven values: 7.5−1.5×8=−4.57.5 - 1.5 \times 8 = -4.5 and 15.5+1.5×8=27.515.5 + 1.5 \times 8 = 27.5. All values lie inside, so there is no outlier. If the largest value were 35 instead of 21, it would be one.

20304050lower fenceupper fence20304050
Nine delivery times: 21, 23, 24, 25, 26, 27, 28, 30 and 55 minutes. The fences stand at 18 and 34. The value 55 lies outside and is drawn as a single circle; the right whisker ends at 30, the largest value inside the fences.

An outlier is a question, not a verdict. It may be a typing mistake (an age of 250), a fault of an instrument, or a real and important case (a fraud, a very loyal customer). Find out which before you remove anything.

Choose a data set. The quartiles are found with position = 1 + (n − 1) × p.

The sorted values with their positions
position1234567891011
value35789111315161821
36912151821

Q1: position 1 + 10 × 0.25 = 3.5: halfway from the 3rd value (7) to the next (8). Q1 = 7.5

Median: position 1 + 10 × 0.5 = 6: the 6th value. Median = 11

Q3: position 1 + 10 × 0.75 = 8.5: halfway from the 8th value (15) to the next (16). Q3 = 15.5

IQR = 15.5 − 7.5 = 8. Fences: 7.5 − 1.5 × 8 = −4.5 and 15.5 + 1.5 × 8 = 27.5

Outliers: none

Try it yourself

The numbers of pages of nine reports, already sorted: 5, 8, 9, 11, 14, 16, 17, 20, 38. Find Q1Q_1, the median, Q3Q_3, the IQR and the upper fence. Is 38 an outlier?

Show a hint
With 9 values the positions are 3, 5 and 7, so the quartiles are values of the data.
Show the solution

Answer: Q1=9Q_1 = 9, median 14, Q3=17Q_3 = 17, IQR 8, upper fence 29. Yes, 38 is an outlier.

Q1: position 1+8×0.25=3,Q1=9Q2: position 1+8×0.50=5,Q2=14Q3: position 1+8×0.75=7,Q3=17IQR=17−9=8upper fence=17+1.5×8=29\begin{aligned} & Q_1\text{: position } 1 + 8 \times 0.25 = 3, \quad Q_1 = 9 \\[2pt] & Q_2\text{: position } 1 + 8 \times 0.50 = 5, \quad Q_2 = 14 \\[2pt] & Q_3\text{: position } 1 + 8 \times 0.75 = 7, \quad Q_3 = 17 \\[6pt] & \text{IQR} = 17 - 9 = 8 \\[4pt] & \text{upper fence} = 17 + 1.5 \times 8 = 29 \end{aligned}

The value 38 lies above 29, so it is an outlier. The lower fence is 9−12=−39 - 12 = -3, and no value lies below it.

7The variance and the standard deviation

7.1Spread as distance from the mean

Range and IQR look at positions. The most used measure of spread asks another question: how far are the values from the mean, on average?

The deviations x−xˉx - \bar{x} cannot simply be averaged, because they add up to 0 (Part 4). So they are squared first: every square is positive, and large deviations count more.

7.2The variance of a population

When the data is the whole population (all NN values, mean μ\mu):

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

The variance is the mean of the squared deviations. Its square root is the standard deviation σ\sigma, which has the unit of the data again.

Example. Eight values: 2, 4, 4, 4, 5, 5, 7, 9. Their mean is 408=5\tfrac{40}{8} = 5.

The eight values, their deviations and the squares
xx24445579
x−μx - \mu−3-3−1-1−1-1−1-10024
(x−μ)2(x - \mu)^2911100416
∑(x−μ)2=9+1+1+1+0+0+4+16=32σ2=328=4σ=4=2\begin{aligned} \sum (x - \mu)^2 &= 9 + 1 + 1 + 1 + 0 + 0 + 4 + 16 \\ &= 32 \\[6pt] \sigma^2 &= \frac{32}{8} = 4 \\[6pt] \sigma &= \sqrt{4} = 2 \end{aligned}

"The values lie about 2 away from the mean 5."

mean 5123456789
The eight values with their mean, 5. The filled strip reaches one standard deviation, 2, to each side of the mean: from 3 to 7. Most values lie inside it.

7.3The variance of a sample

Almost always the data is a sample, and we use it to estimate the spread of the population. Then the sum of squares is divided by n−1n - 1:

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

If the eight values are a sample:

s2=327≈4.571s≈2.138\begin{aligned} s^2 &= \frac{32}{7} \approx 4.571 \\[4pt] s &\approx 2.138 \end{aligned}

7.4Why n − 1?

The deviations are measured from the sample mean xˉ\bar{x}, and xˉ\bar{x} was calculated from the same values. It sits right in the middle of them, closer than the unknown μ\mu would. So the squared deviations come out a little too small, and dividing by nn would estimate a spread that is too small. Dividing by the smaller number n−1n - 1 repairs this exactly.

Another way to see it: the nn deviations add up to 0. Once n−1n - 1 of them are known, the last one is fixed. Only n−1n - 1 of them carry free information. This number is called the degrees of freedom.

Population and sample side by side
PopulationSample
Meanμ=∑xN\mu = \dfrac{\sum x}{N}xˉ=∑xn\bar{x} = \dfrac{\sum x}{n}
Varianceσ2=∑(x−μ)2N\sigma^2 = \dfrac{\sum (x - \mu)^2}{N}s2=∑(x−xˉ)2n−1s^2 = \dfrac{\sum (x - \bar{x})^2}{n - 1}
Standard deviationσ\sigmass
Use whenthe data is everything there isthe data stands for something larger (the usual case)

For large nn the two hardly differ. For small samples they do: with n=5n = 5 the sample variance is 25 percent larger. Unless a problem says that the data is the whole population, use n−1n - 1. Calculators have two keys (often σn\sigma_n and σn−1\sigma_{n-1}, or σ\sigma and ss), and programs have a switch (Part 17).

7.5The steps

  1. Calculate the mean.
  2. Subtract the mean from every value: the deviations (check: their sum is 0).
  3. Square the deviations.
  4. Add the squares.
  5. Divide by n−1n - 1 for a sample (by NN for a population): the variance.
  6. Take the square root: the standard deviation.

Five values. Move them and follow the six steps to the standard deviation.

The values, their distances from the mean and the squares of the distances
value48657sum 30
value − mean−220−11sum 0
squared44011sum 10
mean024681012

Mean: 30 / 5 = 6. Sum of the squares: 10

Variance: 10 / 4 = 2.5. Standard deviation: √2.5 = 1.581

The distances from the mean always add up to 0. Move one value far away from the others: its square takes over the sum. The filled strip is the mean plus and minus one standard deviation.

7.6A shortcut

When the mean is not a round number, the deviations are awkward. The same sum of squares comes without them:

∑(x−xˉ)2=∑x2−n×xˉ2\sum (x - \bar{x})^2 = \sum x^2 - n \times \bar{x}^2

For the eight values:

∑x2=4+16+16+16+25+25+49+81=232∑x2−n×xˉ2=232−8×52=232−200=32\begin{aligned} & \sum x^2 = 4 + 16 + 16 + 16 + 25 + 25 + 49 + 81 \\ & \quad = 232 \\[6pt] & \sum x^2 - n \times \bar{x}^2 = 232 - 8 \times 5^2 \\ & \quad = 232 - 200 = 32 \end{aligned}

The same sum of squares.

For a population this gives σ2=∑x2N−μ2\sigma^2 = \dfrac{\sum x^2}{N} - \mu^2, "the mean of the squares minus the square of the mean", which is the formula E(X2)−μ2E(X^2) - \mu^2 of the last lesson.

7.7From a frequency table

For the 20 families of Part 3 (mean 1.45), multiply by the frequencies:

∑(f×x2)=4×0+7×1+6×4+2×9+1×16=0+7+24+18+16=65sum of squares=65−20×1.452=65−42.05=22.95s2=22.9519≈1.208s≈1.099\begin{aligned} \sum (f \times x^2) &= 4 \times 0 + 7 \times 1 + 6 \times 4 \\ &\quad + 2 \times 9 + 1 \times 16 \\ &= 0 + 7 + 24 + 18 + 16 = 65 \\[6pt] \text{sum of squares} &= 65 - 20 \times 1.45^2 \\ &= 65 - 42.05 = 22.95 \\[6pt] s^2 &= \frac{22.95}{19} \approx 1.208 \\[4pt] s &\approx 1.099 \end{aligned}

Try it yourself

The sample 3, 5, 6, 10 is given. Find its mean, the sum of the squared deviations, the variance and the standard deviation.

Show a hint
The mean is 6. It is a sample, so divide the sum of squares by n−1=3n - 1 = 3.
Show the solution

Answer: Mean 6, sum of squares 26, s2≈8.667s^2 \approx 8.667 and s≈2.944s \approx 2.944.

The four values, their deviations and the squares
xx35610Sum
x−xˉx - \bar{x}−3-3−1-1040
(x−xˉ)2(x - \bar{x})^29101626
xˉ=244=6s2=263≈8.667s=8.667≈2.944\begin{aligned} \bar{x} &= \frac{24}{4} = 6 \\[4pt] s^2 &= \frac{26}{3} \approx 8.667 \\[4pt] s &= \sqrt{8.667} \approx 2.944 \end{aligned}

Of the 26 in the sum of squares, 16 come from the single value 10: the value farthest from the mean counts most.

8Reading a standard deviation

8.1Small and large

A standard deviation is a typical distance from the mean, in the unit of the data. Whether it is "large" depends on the mean and on the purpose: 5 minutes are a lot for a bus that comes every 10 minutes and nothing for a flight of 9 hours.

  • s=0s = 0 means that all values are equal.
  • The standard deviation is never negative.
  • Like the mean, it is not robust: an outlier is squared and makes it much larger.

8.2z-scores for data

The z-score of the last lesson works for data too:

z=x−xˉsz = \frac{x - \bar{x}}{s}

It says how many standard deviations a value lies above (++) or below (−-) the mean. In a class with the mean 64 and the standard deviation 8, a mark of 80 has z=80−648=2z = \dfrac{80 - 64}{8} = 2.

8.3The 68-95-99.7 rule

If the histogram of the data is roughly a bell, the rule of the last lesson holds for it:

  • about 68 percent of the values lie within 1 standard deviation of the mean
  • about 95 percent within 2
  • about 99.7 percent within 3

So for bell-shaped data a value with a z-score beyond 3 or −3-3 is very unusual, and this is the second common rule for outliers.

8.4Chebyshev's rule: for every shape

When the data is not bell-shaped, a weaker rule still holds, for any data whatever. For every k>1k > 1, at least this share of the values lies within kk standard deviations of the mean:

1−1k21 - \frac{1}{k^2}
Chebyshev's rule and the rule for a bell
kk234
At least75 percentabout 89 percentabout 94 percent
For a bell (for comparison)about 95 percentabout 99.7 percentalmost all

The words "at least" matter: usually far more values lie inside. But the rule can never fail.

8.5The coefficient of variation

Is a standard deviation of 5 kg large? For the weights of adults it is small; for the weights of cats it is huge. The coefficient of variation compares the spread with the mean:

CV=sxˉ(often given in percent)\text{CV} = \frac{s}{\bar{x}} \qquad \text{(often given in percent)}

Adults with the mean 75 kg and s=5s = 5 kg have CV=575≈6.7\text{CV} = \tfrac{5}{75} \approx 6.7 percent. Cats with the mean 4 kg and s=1s = 1 kg have CV=25\text{CV} = 25 percent: relative to their size, cats differ far more. The CV has no unit, so it can compare columns in different units. It makes sense only for quantities that cannot be negative and have a real zero (weights, prices, times), not for temperatures in degrees Celsius.

Try it yourself

The times that the runners of a club need for a lap have the mean 60 seconds and the standard deviation 5 seconds. (a) Find the z-score of a time of 72 seconds. (b) Nothing is known about the shape of the data. At least which share of the times lies between 50 and 70 seconds? (c) Find the coefficient of variation.

Show a hint
For (b): 50 and 70 are two standard deviations from the mean, so use Chebyshev's rule with k=2k = 2.
Show the solution

Answer: (a) 2.42.4, (b) at least 75 percent, (c) about 8.3 percent.

(a)z=72−605=2.4(b)k=2:1−122=0.75(c)CV=560≈0.083\begin{aligned} \text{(a)} \quad & z = \frac{72 - 60}{5} = 2.4 \\[6pt] \text{(b)} \quad & k = 2\text{:} \quad 1 - \frac{1}{2^2} = 0.75 \\[6pt] \text{(c)} \quad & \text{CV} = \frac{5}{60} \approx 0.083 \end{aligned}

If the times were bell-shaped, about 95 percent would lie between 50 and 70 seconds. Without that knowledge only the 75 percent are certain.

9Changing the data: shifting, rescaling and standardising

9.1What happens to the summaries

Every value is changed in the same way: y=a×x+by = a \times x + b.

What a change of the data does to its summaries
ChangeMean, median, quartilesRange, IQR, standard deviationVariance
Add bb to every valuebb is addedunchangedunchanged
Multiply every value by aamultiplied by aamultiplied by ∣a∣\len{a}multiplied by a2a^2
Both: y=a×x+by = a \times x + ba×(old value)+ba \times (\text{old value}) + bmultiplied by ∣a∣\len{a}multiplied by a2a^2

A shift moves the whole picture without changing its width. A factor stretches everything, the centre and the width.

mean05101520

The data

mean 5

mean05101520

Every value plus 10

mean 15: the same picture, moved

mean05101520

Every value times 2

mean 10: the picture is stretched

A shift moves the dots without changing the distances between them, so no measure of spread changes. A factor stretches the distances: range, IQR and standard deviation are multiplied by it.

Example. A teacher adds 5 marks to every result. The mean rises by 5; the standard deviation stays as it was. Another teacher converts marks out of 50 to marks out of 100 by doubling: the mean and the standard deviation both double, and the variance becomes four times as large.

Every value x becomes y = a × x + b. What happens to the summaries?

mean of x−20−1001020mean of y−20−1001020

Mean: 2 × 5 + 0 = 10. Median: 2 × 4.5 + 0 = 9

Standard deviation: 2 × 2.191 = 4.382. Variance: 4 × 4.8 = 19.2

The shift b appears only in the mean and the median. The spread feels only the size of a: a negative a turns the picture round without changing its width.

9.2Standardising

Replace every value by its z-score:

z=x−xˉsz = \frac{x - \bar{x}}{s}

This is a shift by −xˉ-\bar{x} followed by a division by ss. By the table above, the new column has the mean 0 and the standard deviation 1, whatever the old one looked like. The shape does not change: skewed data stays skewed.

Example. The values 2, 4, 6 have the mean 4 and, as a population, the standard deviation 83≈1.633\sqrt{\tfrac{8}{3}} \approx 1.633:

2−41.633≈−1.2254−41.633=06−41.633≈1.225\begin{aligned} \frac{2 - 4}{1.633} &\approx -1.225 \\[4pt] \frac{4 - 4}{1.633} &= 0 \\[4pt] \frac{6 - 4}{1.633} &\approx 1.225 \end{aligned}

9.3Min-max scaling

Another rescaling brings every column into the interval from 0 to 1:

x′=x−minimummaximum−minimumx' = \frac{x - \text{minimum}}{\text{maximum} - \text{minimum}}

The smallest value becomes 0, the largest becomes 1. It is simple, but one outlier squeezes all other values into a corner.

Standardising and min-max scaling side by side
StandardisingMin-max scaling
Resultmean 0, standard deviation 1from 0 to 1
Usesmean and standard deviationminimum and maximum
Outliersdisturb itdisturb it strongly

Both are used in machine learning so that a column in metres and a column in thousands count equally (Part 12).

The values 10, 20, 25, 30 and one largest value x, rescaled in two ways.

The five values, their z-scores and their min-max values
value1020253050
standardised−1.15−0.47−0.130.21.55
min-max00.250.380.51
00.250.50.751

Standardising: mean 27, standard deviation 14.83. The z-scores have the mean 0 and the standard deviation 1.

Min-max: (value − 10) / (50 − 10). The four smaller values lie between 0 and 0.5.

The picture shows the min-max values. Make x very large: the other four values are pressed together near 0. Standardising suffers too, because x pulls the mean and the standard deviation up.

Try it yourself

The prices of a shop have the mean 40, the median 35 and the standard deviation 8. All prices are raised by 10 percent, and then a delivery fee of 2 is added to each: y=1.1×x+2y = 1.1 \times x + 2. Find the new mean, the new median and the new standard deviation.

Show a hint
The mean and the median follow the whole formula. The standard deviation feels only the factor 1.1.
Show the solution

Answer: Mean 46, median 40.5, standard deviation 8.8.

mean:1.1×40+2=46median:1.1×35+2=40.5standard deviation:1.1×8=8.8\begin{aligned} \text{mean:} \quad & 1.1 \times 40 + 2 = 46 \\[4pt] \text{median:} \quad & 1.1 \times 35 + 2 = 40.5 \\[4pt] \text{standard deviation:} \quad & 1.1 \times 8 = 8.8 \end{aligned}

The delivery fee moves every price by the same 2, so it leaves the spread alone.

10Shape, outliers and choosing the summary

10.1Skewness

Part 3 showed the three shapes. Without a picture, the summaries reveal the shape:

The signs of the three shapes
SignSymmetricSkewed to the rightSkewed to the left
Mean and medianabout equalmean above the medianmean below the median
Box plotmedian in the middle, equal whiskerslong right whiskerlong left whisker
QuartilesQ3−Q2≈Q2−Q1Q_3 - Q_2 \approx Q_2 - Q_1Q3−Q2>Q2−Q1Q_3 - Q_2 > Q_2 - Q_1Q3−Q2<Q2−Q1Q_3 - Q_2 < Q_2 - Q_1

Skewed to the right is the common case for quantities with a lower limit and no upper limit: incomes, prices, waiting times, the sizes of cities.

10.2Two families of summaries

The two families of summaries
Uses every value, not robustUses positions, robust
Centremeanmedian
Spreadstandard deviationIQR
Report it forsymmetric data without outliersskewed data or data with outliers

The pairs belong together: "mean and standard deviation" or "median and IQR". When you are unsure, give both, or better: look at the histogram first.

10.3Summaries can hide the truth

The data sets 1, 1, 1, 9, 9, 9 and 1, 3, 5, 5, 7, 9 have the same mean (5) and the same median (5), and the first has no value anywhere near 5. A summary is a loss of information by design. Always look at a picture of the data (a histogram or a box plot) before you trust its numbers.

mean and median13579

1, 1, 1, 9, 9, 9

mean and median13579

1, 3, 5, 5, 7, 9

Two data sets with the same mean and the same median, 5. In the first one no value lies anywhere near 5. A summary cannot show this; a picture can.

Try it yourself

The monthly incomes of the households of a town have the mean 3,000 and the median 2,200. (a) Which shape does the data have? (b) Which pair of summaries should a report give?

Show a hint
Compare the mean with the median: the tail pulls the mean towards itself.
Show the solution

Answer: (a) Skewed to the right. (b) The median and the IQR.

The mean lies far above the median, so a tail of large incomes pulls it up: the data is skewed to the right. For skewed data the robust pair, median and IQR, describes a typical household better. "A typical household has 2,200" is closer to the truth than "3,000".

11Data in classes

Sometimes only the table of classes is available, not the single values. The mean can still be estimated: let every value of a class be represented by the midpoint of the class.

The ages of the 30 customers of Part 3:

The ages in classes, with midpoints
Class20 to 3030 to 4040 to 5050 to 6060 to 70
Midpoint mm2535455565
Frequency ff491052
f×mf \times m100315450275130
xˉ≈∑(f×m)n=100+315+450+275+13030=127030≈42.3\begin{aligned} \bar{x} &\approx \frac{\sum (f \times m)}{n} \\[2pt] &= \frac{100 + 315 + 450 + 275 + 130}{30} \\[2pt] &= \frac{1270}{30} \approx 42.3 \end{aligned}

The modal class is "40 to 50", the class with the highest frequency. The median is the mean of the 15th and the 16th value; the cumulative frequencies are 4, 13, 23, so both lie in the class "40 to 50", the median class.

The result is an estimate: the true values inside a class are unknown. The narrower the classes, the better it is.

Try it yourself

The times that 20 customers spent in a shop, in classes: 0 to 4 minutes: 3 customers, 4 to 8 minutes: 9 customers, 8 to 12 minutes: 6 customers, 12 to 16 minutes: 2 customers. Estimate the mean time. Which class is the modal class?

Show a hint
The midpoints are 2, 6, 10 and 14. Multiply each by its frequency.
Show the solution

Answer: The mean is about 7.4 minutes. The modal class is "4 to 8".

∑(f×m)=3×2+9×6+6×10+2×14=6+54+60+28=148xˉ≈14820=7.4\begin{aligned} \sum (f \times m) &= 3 \times 2 + 9 \times 6 \\ &\quad + 6 \times 10 + 2 \times 14 \\ &= 6 + 54 + 60 + 28 = 148 \\[6pt] \bar{x} &\approx \frac{148}{20} = 7.4 \end{aligned}

The class "4 to 8" has the highest frequency, 9 customers, so it is the modal class.

12Descriptive statistics in machine learning

The first look at any data. In pandas, df.describe() prints for every numerical column the count, the mean, the standard deviation ss (with n−1n - 1), the minimum, Q1Q_1, the median, Q3Q_3 and the maximum: the summaries of this lesson. A mean far from the median tells you about skew before any chart is drawn; a maximum of 250 in a column of ages tells you about a mistake.

Filling missing values. A gap in a column is often filled with the mean or the median of the column. For a skewed column or one with outliers the median is the safer choice, for the reason of Part 5. For a categorical column the mode is used.

Feature scaling. Many models (k-nearest neighbours, support vector machines, neural networks, anything trained with gradient descent) work badly when one column has values in the thousands and another between 0 and 1. Standardising or min-max scaling (Part 9) makes the columns comparable.

Learn the numbers from the training data only. The mean and the standard deviation used for scaling, and the median used for filling gaps, are calculated from the training set and then applied unchanged to the test set. Calculating them from all the data lets information about the test set leak into the training, and the measured quality becomes too good to be true. This mistake is called data leakage.

Outliers. The 1.5 IQR rule and the z-score rule are the standard first checks. A few wrong extreme values can pull a regression line or a mean prediction far away.

Errors of a model. The mean squared error, MSE=∑(true−predicted)2n\text{MSE} = \dfrac{\sum (\text{true} - \text{predicted})^2}{n}, is built like a variance; its square root, the RMSE, like a standard deviation. The mean absolute error is the more robust relative. The model "always predict the mean" has an MSE equal to the variance of the target; a useful model must beat that.

The mean and the median as predictions. If you must predict one number for everybody, the mean makes the squared errors smallest and the median makes the absolute errors smallest. A regression tree does exactly this in each of its leaves.

Try it yourself

A column of a training set has the mean 50 and the standard deviation 10. In the test set the same column has the mean 54 and the standard deviation 12. A test row has the value 65. (a) Which numbers are used to standardise it? (b) What is its standardised value?

Show a hint
The numbers for scaling are learned once, from the training data.
Show the solution

Answer: (a) Those of the training set. (b) 1.51.5.

z=65−5010=1.5z = \frac{65 - 50}{10} = 1.5

The mean 54 and the standard deviation 12 of the test set are not used at all. With them the value would become 65−5412≈0.917\tfrac{65 - 54}{12} \approx 0.917, and the model would see numbers on another scale than the one it was trained on.

13Common mistakes

Forgetting to sort before taking the median. The median is the middle of the sorted data.

Taking the middle position as the median. For 9 values the median is the 5th value, not the number 5.

Averaging averages. The mean of two groups is the weighted mean of their means, not the simple one, unless the groups have the same size.

Reporting the mean for skewed data. A few very large values pull it up. Give the median, or both.

Dividing by nn for a sample. Sample variance: n−1n - 1.

Forgetting the square root. The variance has the unit squared. The number to interpret is the standard deviation.

Squaring the sum instead of summing the squares. ∑x2\sum x^2 means: square every value, then add. (∑x)2\left(\sum x\right)^2 is something else.

Adding standard deviations, or expecting a shift to change them. Adding 5 to every value changes the mean and not the spread.

Calling every outlier a mistake. An outlier may be the most interesting row of the table. Check before removing it.

Using the 68-95-99.7 rule for data that is not bell-shaped. For other shapes only Chebyshev's rule is safe.

Calculating the mean of a categorical variable. Codes such as 1 = Baku, 2 = Ganja are names. Their mean means nothing.

Scaling with the numbers of the whole data set. The mean and the standard deviation for scaling come from the training set only.

Common mistakes and what is right
WrongRight
median of 7, 2, 9 is 2sort first: 2, 7, 9, median 7
groups of 20 and 30 with means 70 and 80: mean 7520×70+30×8050=76\dfrac{20 \times 70 + 30 \times 80}{50} = 76
sample variance ∑(x−xˉ)2n\dfrac{\sum (x - \bar{x})^2}{n}∑(x−xˉ)2n−1\dfrac{\sum (x - \bar{x})^2}{n - 1}
"the spread is 16" (the variance)"the standard deviation is 4"
adding 10 to all values adds 10 to ssss does not change
doubling all values doubles the variancethe variance is multiplied by 4
mean 2000, so "a typical salary is 2000"look at the median when the data is skewed

14Solved problems (from the easiest to the hardest)

Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.

Level 1: The centre

Problem 1: A mean

A seller made 12, 18, 9, 21 and 15 sales on five days. Find the mean number of sales per day.

Idea. Add the values and divide by their number (Part 4).

xˉ=12+18+9+21+155=755=15\bar{x} = \frac{12 + 18 + 9 + 21 + 15}{5} = \frac{75}{5} = 15

Why: the five days together brought 75 sales; the mean spreads them evenly over the days.

Problem 2: A median, odd number of values

Find the median of 14, 6, 21, 9, 17, 11, 30.

Idea. Sort first. With 7 values the median is value number 7+12=4\dfrac{7 + 1}{2} = 4 (Part 5).

sorted:691114172130median=14\begin{aligned} \text{sorted:} \quad & 6 \quad 9 \quad 11 \quad \boxed{14} \quad 17 \quad 21 \quad 30 \\[4pt] \text{median} &= 14 \end{aligned}

Why: three values lie below 14 and three above it.

Problem 3: A median, even number of values

Find the median of 8, 3, 15, 6, 12, 10, 4, 9.

Idea. With 8 values the median is the mean of the 4th and the 5th sorted value (Part 5).

sorted:34689101215median=8+92=8.5\begin{aligned} \text{sorted:} \quad & 3 \quad 4 \quad 6 \quad \boxed{8 \quad 9} \quad 10 \quad 12 \quad 15 \\[4pt] \text{median} &= \frac{8 + 9}{2} = 8.5 \end{aligned}

Why: the median need not be one of the values. Four values lie below 8.5 and four above it.

Problem 4: The mode

A café sold these numbers of cups of tea on ten days: 24, 30, 27, 30, 22, 27, 30, 25, 27, 30. Find the mode.

Idea. Count how often every value appears (Part 5).

How often every value appears
Value2224252730
Frequency11134

The mode is 30.

Why: the mode is the most frequent value, not the largest frequency. The answer is 30, not 4.

Problem 5: Mean or median?

Six flats in a street were sold for 90, 95, 100, 105, 110 and 400 thousand. Find the mean and the median. Which describes a typical flat better?

Idea. One extreme value pulls the mean; the median stays (Part 5).

mean=90+95+100+105+110+4006=9006=150median=100+1052=102.5\begin{aligned} \text{mean} &= \frac{90 + 95 + 100 + 105 + 110 + 400}{6} \\[2pt] &= \frac{900}{6} = 150 \\[6pt] \text{median} &= \frac{100 + 105}{2} = 102.5 \end{aligned}

The median. Five of the six flats cost less than the mean.

Why: the mean is above the median, which is the sign of a tail to the right. Prices are almost always skewed like this, which is why reports on housing use the median.

Problem 6: A weighted mean

A final grade is made of a project (weight 25 percent), a midterm (25 percent) and a final exam (50 percent). A student has 88, 72 and 80. Find the final grade.

Idea. Multiply every value by its weight; the weights add up to 1 (Part 4).

0.25×88+0.25×72+0.50×80=22+18+40=80\begin{aligned} & 0.25 \times 88 + 0.25 \times 72 + 0.50 \times 80 \\ & \quad = 22 + 18 + 40 = 80 \end{aligned}

Why: the simple mean of the three marks is also 80 here only by chance of the numbers; with a final exam of 60 the weighted grade would be 70 and the simple mean 73.3.

Problem 7: Mean, median and mode from a frequency table

The number of goals in 25 matches is given in the table below. Find the mean, the median and the mode.

The number of goals in 25 matches
Goals01234
Matches38752

Idea. Multiply by the frequencies for the mean. For the median use the cumulative frequencies (Parts 3 to 5).

∑(f×x)=3×0+8×1+7×2+5×3+2×4=0+8+14+15+8=45xˉ=4525=1.8\begin{aligned} \sum (f \times x) &= 3 \times 0 + 8 \times 1 + 7 \times 2 \\ &\quad + 5 \times 3 + 2 \times 4 \\ &= 0 + 8 + 14 + 15 + 8 = 45 \\[6pt] \bar{x} &= \frac{45}{25} = 1.8 \end{aligned}

The median is value number 25+12=13\dfrac{25 + 1}{2} = 13. The cumulative frequencies are 3, 11, 18, so the 13th value is a 2: the median is 2. The mode is 1, with 8 matches.

Why: the first 3 sorted values are 0, values 4 to 11 are 1, values 12 to 18 are 2. So number 13 is a 2.

Problem 8: A missing value

Four values have the mean 13. Three of them are 12, 15 and 9. Find the fourth.

Idea. The sum of the data is n×xˉn \times \bar{x} (Part 4).

sum of all four=4×13=52fourth value=52−(12+15+9)=52−36=16\begin{aligned} \text{sum of all four} &= 4 \times 13 = 52 \\[4pt] \text{fourth value} &= 52 - (12 + 15 + 9) \\ &= 52 - 36 = 16 \end{aligned}

Why: a mean fixes the total. This idea solves most "missing value" problems.

Problem 9: Combining two groups

A company has 40 workers with a mean salary of 900 and 10 managers with a mean salary of 1900. Find the mean salary of all 50 employees.

Idea. A weighted mean, with the group sizes as weights (Part 4).

40×900+10×190050=36,000+19,00050=55,00050=1100\begin{aligned} \frac{40 \times 900 + 10 \times 1900}{50} &= \frac{36{,}000 + 19{,}000}{50} \\[2pt] &= \frac{55{,}000}{50} = 1100 \end{aligned}

Why: the simple mean of 900 and 1900 would be 1400, but there are four times as many workers as managers, so the result lies much nearer to 900.

Level 2: The spread

Problem 10: Quartiles on whole positions

Find the five-number summary and the IQR of 4, 7, 8, 10, 12, 13, 15, 18, 21.

Idea. position=1+(n−1)×p\text{position} = 1 + (n - 1) \times p with n=9n = 9 (Part 6).

Q1: position 1+8×0.25=3⇒Q1=8Q2: position 1+8×0.50=5⇒Q2=12Q3: position 1+8×0.75=7⇒Q3=15IQR=15−8=7\begin{aligned} & Q_1\text{: position } 1 + 8 \times 0.25 = 3 \quad \Rightarrow \quad Q_1 = 8 \\[2pt] & Q_2\text{: position } 1 + 8 \times 0.50 = 5 \quad \Rightarrow \quad Q_2 = 12 \\[2pt] & Q_3\text{: position } 1 + 8 \times 0.75 = 7 \quad \Rightarrow \quad Q_3 = 15 \\[6pt] & \text{IQR} = 15 - 8 = 7 \end{aligned}

The five-number summary is 4, 8, 12, 15, 21.

Why: with 9, 13, 17, ... values all three positions are whole numbers, and the quartiles are values of the data.

Problem 11: Quartiles between two values

Find Q1Q_1, the median, Q3Q_3 and the IQR of 2, 5, 6, 9, 11, 14, 15, 20.

Idea. With n=8n = 8 the positions fall between values: go the fraction of the way (Part 6).

Q1Q_1 stands at the position 1+7×0.25=2.751 + 7 \times 0.25 = 2.75: from the 2nd value (5), 0.75 of the way to the 3rd (6).

Q1=5+0.75×1=5.75Q_1 = 5 + 0.75 \times 1 = 5.75

Q2Q_2 stands at the position 1+7×0.50=4.51 + 7 \times 0.50 = 4.5: halfway between the 4th value (9) and the 5th (11).

Q2=10Q_2 = 10

Q3Q_3 stands at the position 1+7×0.75=6.251 + 7 \times 0.75 = 6.25: from the 6th value (14), 0.25 of the way to the 7th (15).

Q3=14+0.25×1=14.25IQR=14.25−5.75=8.5\begin{aligned} Q_3 &= 14 + 0.25 \times 1 = 14.25 \\[4pt] \text{IQR} &= 14.25 - 5.75 = 8.5 \end{aligned}

Why: the whole part of the position names the value to start from, the decimal part says how far to go towards the next one.

Problem 12: Finding an outlier

The delivery times of nine orders in minutes: 21, 23, 24, 25, 26, 27, 28, 30, 55. Is there an outlier by the 1.5 IQR rule?

Idea. Quartiles, IQR, then the two fences (Part 6).

Q1=3rd value=24Q3=7th value=28IQR=28−24=4lower fence=24−1.5×4=18upper fence=28+1.5×4=34\begin{aligned} Q_1 &= \text{3rd value} = 24 \\[2pt] Q_3 &= \text{7th value} = 28 \\[2pt] \text{IQR} &= 28 - 24 = 4 \\[6pt] \text{lower fence} &= 24 - 1.5 \times 4 = 18 \\[2pt] \text{upper fence} &= 28 + 1.5 \times 4 = 34 \end{aligned}

55 lies above the upper fence: it is an outlier. All other values are inside.

Why: the fences are built from the middle half of the data, which the value 55 cannot influence. That is what makes the rule reliable.

Problem 13: The variance of a population

A family has five children aged 3, 5, 7, 9 and 11. Find the variance and the standard deviation of the ages. (The five children are the whole population.)

Idea. All values are there, so divide by NN (Part 7).

The ages, their deviations from the mean 7 and the squares
xx357911Sum
x−μx - \mu−4-4−2-20240
(x−μ)2(x - \mu)^2164041640
μ=355=7σ2=405=8σ=8≈2.828\begin{aligned} \mu &= \frac{35}{5} = 7 \\[4pt] \sigma^2 &= \frac{40}{5} = 8 \\[4pt] \sigma &= \sqrt{8} \approx 2.828 \end{aligned}

Why: nothing is estimated here. The five ages are all there is, so n−1n - 1 is not needed.

Problem 14: The variance of a sample

A sample of five parcels weighs 4, 8, 6, 5 and 7 kg. Find the sample variance and the sample standard deviation.

Idea. The six steps of Part 7, with n−1n - 1.

The weights, their deviations from the mean 6 and the squares
xx48657Sum
x−xˉx - \bar{x}−2-220−1-110
(x−xˉ)2(x - \bar{x})^24401110
xˉ=305=6s2=104=2.5s=2.5≈1.581\begin{aligned} \bar{x} &= \frac{30}{5} = 6 \\[4pt] s^2 &= \frac{10}{4} = 2.5 \\[4pt] s &= \sqrt{2.5} \approx 1.581 \end{aligned}

Why: the parcels stand for all parcels of the company, so this is a sample. Dividing by 5 would give 2, an estimate that is too small.

Problem 15: The shortcut

For the sample 1, 3, 4, 6, 6 find the variance with the shortcut formula.

Idea. ∑(x−xˉ)2=∑x2−n×xˉ2\sum (x - \bar{x})^2 = \sum x^2 - n \times \bar{x}^2 (Part 7).

∑x=20xˉ=4∑x2=1+9+16+36+36=98sum of squares=98−5×42=98−80=18s2=184=4.5s≈2.121\begin{aligned} \sum x &= 20 \qquad \bar{x} = 4 \\[4pt] \sum x^2 &= 1 + 9 + 16 + 36 + 36 = 98 \\[4pt] \text{sum of squares} &= 98 - 5 \times 4^2 = 98 - 80 = 18 \\[6pt] s^2 &= \frac{18}{4} = 4.5 \\[4pt] s &\approx 2.121 \end{aligned}

Why: check with the deviations −3-3, −1-1, 0, 2, 2: their squares 9, 1, 0, 4, 4 also add up to 18.

Problem 16: The standard deviation from a frequency table

In a sample of 10 households, 2 have one phone, 5 have two phones and 3 have three phones. Find the mean and the sample standard deviation of the number of phones.

Idea. Multiply by the frequencies, then the shortcut (Part 7).

∑(f×x)=2×1+5×2+3×3=21xˉ=2110=2.1∑(f×x2)=2×1+5×4+3×9=49sum of squares=49−10×2.12=49−44.1=4.9s2=4.99≈0.544s≈0.738\begin{aligned} \sum (f \times x) &= 2 \times 1 + 5 \times 2 + 3 \times 3 = 21 \\[4pt] \bar{x} &= \frac{21}{10} = 2.1 \\[6pt] \sum (f \times x^2) &= 2 \times 1 + 5 \times 4 + 3 \times 9 = 49 \\[4pt] \text{sum of squares} &= 49 - 10 \times 2.1^2 \\ &= 49 - 44.1 = 4.9 \\[6pt] s^2 &= \frac{4.9}{9} \approx 0.544 \\[4pt] s &\approx 0.738 \end{aligned}

Why: a frequency table is only a shorter way of writing the ten values 1, 1, 2, 2, 2, 2, 2, 3, 3, 3.

Problem 17: z-scores

In an exam the mean is 64 and the standard deviation is 8. Find the z-scores of the marks 80 and 52. Which of the two is more unusual?

Idea. z=x−xˉsz = \dfrac{x - \bar{x}}{s} (Part 8). The size of zz tells how unusual a value is.

80:z=80−648=252:z=52−648=−1.5\begin{aligned} 80\text{:} \quad & z = \frac{80 - 64}{8} = 2 \\[4pt] 52\text{:} \quad & z = \frac{52 - 64}{8} = -1.5 \end{aligned}

The mark 80: it is 2 standard deviations away from the mean, and 52 only 1.5.

Why: "unusual" is about the distance from the mean, in either direction. The sign only says on which side the value lies.

Problem 18: Comparing spreads

Product A is sold for a mean price of 50 with a standard deviation of 5 in different shops. Product B has a mean price of 200 and a standard deviation of 12. Which price varies more, relative to its size?

Idea. The coefficient of variation (Part 8).

A:CV=550=0.10=10%B:CV=12200=0.06=6%\begin{aligned} \text{A:} \quad & \text{CV} = \frac{5}{50} = 0.10 = 10\% \\[4pt] \text{B:} \quad & \text{CV} = \frac{12}{200} = 0.06 = 6\% \end{aligned}

Product A.

Why: in absolute numbers B varies more (12 against 5). Relative to the price, A does. Which answer matters depends on the question.

Level 3: Rescaling, shape and putting ideas together

Problem 19: A linear change of the data

A column has the mean 20, the median 18 and the standard deviation 4. Every value is replaced by y=3×x+5y = 3 \times x + 5. Find the new mean, median, standard deviation and variance.

Idea. The table of Part 9: centres follow the whole formula, spreads only the factor.

new mean=3×20+5=65new median=3×18+5=59new s=3×4=12new variance=32×42=9×16=144\begin{aligned} \text{new mean} &= 3 \times 20 + 5 = 65 \\[2pt] \text{new median} &= 3 \times 18 + 5 = 59 \\[2pt] \text{new } s &= 3 \times 4 = 12 \\[2pt] \text{new variance} &= 3^2 \times 4^2 = 9 \times 16 = 144 \end{aligned}

Why: the shift +5+5 moves all values together, so no distance between them changes.

Problem 20: Standardising

Standardise the values 10, 20, 30, 40 as a population. Check the mean of the result.

Idea. Subtract the mean, divide by the standard deviation (Part 9).

μ=1004=25σ2=225+25+25+2254=5004=125σ≈11.18\begin{aligned} \mu &= \frac{100}{4} = 25 \\[4pt] \sigma^2 &= \frac{225 + 25 + 25 + 225}{4} = \frac{500}{4} = 125 \\[4pt] \sigma &\approx 11.18 \end{aligned}
The four values and their z-scores
xx10203040
x−μx - \mu−15-15−5-5515
zz−1.342-1.342−0.447-0.4470.4471.342

The mean of the z-scores is −1.342−0.447+0.447+1.3424=0\dfrac{-1.342 - 0.447 + 0.447 + 1.342}{4} = 0.

Why: the z-scores always have the mean 0 and the standard deviation 1. The even spacing of the data is kept: standardising does not change the shape.

Problem 21: Min-max scaling

Scale the values 10, 20, 25, 50 to the interval from 0 to 1.

Idea. x′=x−minimummaximum−minimumx' = \dfrac{x - \text{minimum}}{\text{maximum} - \text{minimum}} (Part 9).

The minimum is 10, and the maximum minus the minimum is 40.

10−1040=020−1040=0.2525−1040=0.37550−1040=1\begin{aligned} \frac{10 - 10}{40} &= 0 \qquad & \frac{20 - 10}{40} &= 0.25 \\[6pt] \frac{25 - 10}{40} &= 0.375 \qquad & \frac{50 - 10}{40} &= 1 \end{aligned}

Why: the value 50 lies far from the others, so the other three are pressed into the lower 40 percent of the interval. An outlier of 500 would press them below 0.04.

Problem 22: Chebyshev's rule

The waiting times at a counter have the mean 50 seconds and the standard deviation 5 seconds. Nothing is known about their shape. At least which share of the waiting times lies between 40 and 60 seconds? And between 35 and 65?

Idea. At least 1−1k21 - \dfrac{1}{k^2} within kk standard deviations (Part 8).

40 to 60:k=21−14=0.7535 to 65:k=31−19≈0.889\begin{aligned} \text{40 to 60:} \quad & k = 2 \qquad 1 - \tfrac{1}{4} = 0.75 \\[4pt] \text{35 to 65:} \quad & k = 3 \qquad 1 - \tfrac{1}{9} \approx 0.889 \end{aligned}

At least 75 percent, and at least 88.9 percent.

Why: without the shape, the 68-95-99.7 rule may not be used. If the data were bell-shaped, the shares would be about 95 and 99.7 percent, well above the guaranteed minimum.

Problem 23: The mean of data in classes

The daily sales of a shop on 30 days are given in classes in the table below. Estimate the mean, and name the modal class and the median class.

The daily sales on 30 days, in classes
Sales0 to 1010 to 2020 to 3030 to 40
Midpoint5152535
Days51285

Idea. Midpoints stand for the classes (Part 11).

∑(f×m)=5×5+12×15+8×25+5×35=25+180+200+175=580xˉ≈58030≈19.33\begin{aligned} \sum (f \times m) &= 5 \times 5 + 12 \times 15 + 8 \times 25 + 5 \times 35 \\ &= 25 + 180 + 200 + 175 = 580 \\[6pt] \bar{x} &\approx \frac{580}{30} \approx 19.33 \end{aligned}

The modal class is "10 to 20" (12 days). The median is the mean of the 15th and the 16th value; the cumulative frequencies are 5 and 17, so both lie in "10 to 20", the median class.

Why: the single values are lost, so 19.33 is an estimate. The true mean could be a little different.

Problem 24: What one outlier does

For the sample 5, 6, 7, 8, 9, 25 find the mean, the median and the standard deviation. Then remove the value 25 and calculate them again.

Idea. Compare the robust and the non-robust summaries (Parts 5, 7 and 10).

With the value 25 (the squared deviations are 25, 16, 9, 4, 1 and 225):

xˉ=606=10median=7+82=7.5s2=2805=56s≈7.48\begin{aligned} \bar{x} &= \frac{60}{6} = 10 \qquad \text{median} = \frac{7 + 8}{2} = 7.5 \\[4pt] s^2 &= \frac{280}{5} = 56 \qquad s \approx 7.48 \end{aligned}

Without it (the squared deviations are 4, 1, 0, 1 and 4):

xˉ=355=7median=7s2=104=2.5s≈1.58\begin{aligned} \bar{x} &= \frac{35}{5} = 7 \qquad \text{median} = 7 \\[4pt] s^2 &= \frac{10}{4} = 2.5 \qquad s \approx 1.58 \end{aligned}

Why: one value moved the mean from 7 to 10 and made the standard deviation almost five times as large. The median moved from 7 to 7.5. Of the 280 in the sum of squares, 225 come from the single value 25.

Problem 25: Adding a value

Nine values have the mean 12. A tenth value, 22, is added. Find the new mean. Which tenth value would have left the mean unchanged?

Idea. Work with the total, n×xˉn \times \bar{x} (Part 4).

old total=9×12=108new mean=108+2210=13010=13\begin{aligned} \text{old total} &= 9 \times 12 = 108 \\[4pt] \text{new mean} &= \frac{108 + 22}{10} = \frac{130}{10} = 13 \end{aligned}

A tenth value of 12 would leave the mean at 12.

Why: a new value pulls the mean towards itself. A value equal to the mean pulls nowhere.

Problem 26: A complete description

The numbers of days that 11 patients stayed in a hospital: 2, 3, 3, 4, 5, 5, 5, 6, 7, 8, 18. Describe the data: mean, median, mode, quartiles, IQR, outliers and shape. Which summary would you report?

Idea. Everything together (Parts 4 to 6 and 10).

xˉ=6611=6median=6th value=5mode=5\begin{aligned} \bar{x} &= \frac{66}{11} = 6 \\[4pt] \text{median} &= \text{6th value} = 5 \\[4pt] \text{mode} &= 5 \end{aligned}

Q1Q_1 stands at the position 1+10×0.25=3.51 + 10 \times 0.25 = 3.5, halfway between 3 and 4. Q3Q_3 stands at the position 1+10×0.75=8.51 + 10 \times 0.75 = 8.5, halfway between 6 and 7.

Q1=3.5Q3=6.5IQR=6.5−3.5=3lower fence=3.5−1.5×3=−1upper fence=6.5+1.5×3=11\begin{aligned} Q_1 &= 3.5 \qquad Q_3 = 6.5 \\[4pt] \text{IQR} &= 6.5 - 3.5 = 3 \\[6pt] \text{lower fence} &= 3.5 - 1.5 \times 3 = -1 \\[2pt] \text{upper fence} &= 6.5 + 1.5 \times 3 = 11 \end{aligned}

The value 18 is an outlier. The mean (6) is above the median (5): the data is skewed to the right. Report the median 5 and the IQR 3, and mention the one long stay of 18 days.

Why: without the patient who stayed 18 days the mean would be 4810=4.8\tfrac{48}{10} = 4.8, close to the median. A single patient changes the "average stay" by more than a day, so the mean alone would mislead a hospital that plans its beds.

15Practice problems

Forty problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own. Unless a problem says "population", treat the data as a sample. Find quartiles with the rule position=1+(n−1)×p\text{position} = 1 + (n - 1) \times p. Give results that are not whole numbers with three decimals.

Level 1: The centre

  1. 1.Find the mean of 7, 10, 4, 9, 5.

    Show a hint
    Add and divide by 5.
    Show one possible solution

    Answer: 77.

    7+10+4+9+55=355=7\frac{7 + 10 + 4 + 9 + 5}{5} = \frac{35}{5} = 7
  2. 2.Find the median of 15, 8, 22, 11, 19.

    Show a hint
    Sort first. The median is the 3rd of the 5 values.
    Show one possible solution

    Answer: 1515.

    sorted:811151922\text{sorted:} \quad 8 \quad 11 \quad \boxed{15} \quad 19 \quad 22

    The middle value is 15.

  3. 3.Find the median of 6, 2, 9, 4.

    Show a hint
    With an even number of values, take the mean of the two middle ones.
    Show one possible solution

    Answer: 55.

    sorted:2469median=4+62=5\begin{aligned} \text{sorted:} \quad & 2 \quad \boxed{4 \quad 6} \quad 9 \\[4pt] \text{median} &= \frac{4 + 6}{2} = 5 \end{aligned}
  4. 4.Find the mode of 3, 5, 5, 7, 8, 5, 3.

    Show a hint
    Which value appears most often?
    Show one possible solution

    Answer: 55.

    3 appears twice, 5 three times, 7 and 8 once. The mode is 5.

  5. 5.Find the mean and the median of 10, 12, 14, 16, 98. Which of the two describes a typical value better?

    Show a hint
    One value is far away from the others.
    Show one possible solution

    Answer: Mean 30, median 14. The median.

    mean=1505=30\text{mean} = \frac{150}{5} = 30

    The data is already sorted, and the middle value is 14. Four of the five values are far below 30, so the median describes the data better.

  6. 6.A grade is made of a quiz with the weight 0.4 and an exam with the weight 0.6. A student has 60 in the quiz and 85 in the exam. Find the grade.

    Show a hint
    Every mark times its weight.
    Show one possible solution

    Answer: 7575.

    0.4×60+0.6×85=24+51=750.4 \times 60 + 0.6 \times 85 = 24 + 51 = 75
  7. 7.The number of cars in 20 households: 2 households have no car, 5 have one, 8 have two and 5 have three. Find the mean, the median and the mode.

    Show a hint
    Multiply the values by their frequencies. For the median, find the 10th and the 11th value with the cumulative frequencies.
    Show one possible solution

    Answer: Mean 1.8, median 2, mode 2.

    ∑(f×x)=0+5+16+15=36xˉ=3620=1.8\begin{aligned} \sum (f \times x) &= 0 + 5 + 16 + 15 = 36 \\[4pt] \bar{x} &= \frac{36}{20} = 1.8 \end{aligned}

    The cumulative frequencies are 2, 7, 15. The 10th and the 11th value are both 2, so the median is 2. The most frequent value is 2 (8 households).

  8. 8.Five values have the mean 20. Four of them are 18, 25, 16 and 21. Find the fifth.

    Show a hint
    The total is 5×205 \times 20.
    Show one possible solution

    Answer: 2020.

    The total is 100. The four known values add up to 80, so the fifth is 100−80=20100 - 80 = 20.

  9. 9.A group of 10 people has the mean age 50, and a group of 30 people has the mean age 70. Find the mean age of all 40.

    Show a hint
    Weight the two means with the group sizes.
    Show one possible solution

    Answer: 6565.

    10×50+30×7040=500+210040=260040=65\begin{aligned} \frac{10 \times 50 + 30 \times 70}{40} &= \frac{500 + 2100}{40} \\[2pt] &= \frac{2600}{40} = 65 \end{aligned}
  10. 10.25 values have the mean 8.4. What is their sum?

    Show a hint
    The sum is nn times the mean.
    Show one possible solution

    Answer: 210210.

    25×8.4=21025 \times 8.4 = 210
  11. 11.The column "blood group" of a table has the values A, B, AB and 0. What kind of variable is it?

    Show a hint
    Are the values numbers? Do they have an order?
    Show one possible solution

    Answer: Categorical, nominal.

    The values are names without an order. Counts, shares and the mode make sense for it; a mean or a median does not.

  12. 12.In a survey 12 of 80 people chose the answer "never". Find the relative frequency of this answer.

    Show a hint
    fn\dfrac{f}{n}.
    Show one possible solution

    Answer: 0.150.15.

    1280=0.15\frac{12}{80} = 0.15

    That is 15 percent.

  13. 13.Find the range of 14, 9, 27, 18, 11.

    Show a hint
    Largest minus smallest.
    Show one possible solution

    Answer: 1818.

    27−9=1827 - 9 = 18
  14. 14.Four values have the mean 10. A fifth value, 20, is added. Find the new mean.

    Show a hint
    First the old total.
    Show one possible solution

    Answer: 1212.

    The old total is 4×10=404 \times 10 = 40.

    40+205=605=12\frac{40 + 20}{5} = \frac{60}{5} = 12

Level 2: The spread

  1. 15.Find Q1Q_1, the median, Q3Q_3 and the IQR of 2, 4, 5, 7, 9, 10, 12, 15, 20.

    Show a hint
    With 9 values the positions are 3, 5 and 7.
    Show one possible solution

    Answer: Q1=5Q_1 = 5, median 9, Q3=12Q_3 = 12, IQR 7.

    The positions are 1+8×0.25=31 + 8 \times 0.25 = 3, 1+8×0.5=51 + 8 \times 0.5 = 5 and 1+8×0.75=71 + 8 \times 0.75 = 7. The 3rd, 5th and 7th values are 5, 9 and 12.

    IQR=12−5=7\text{IQR} = 12 - 5 = 7
  2. 16.Find Q1Q_1, the median and Q3Q_3 of 3, 6, 7, 10, 14, 18.

    Show a hint
    The positions are 2.25, 3.5 and 4.75.
    Show one possible solution

    Answer: Q1=6.25Q_1 = 6.25, median 8.58.5, Q3=13Q_3 = 13.

    Q1Q_1: position 1+5×0.25=2.251 + 5 \times 0.25 = 2.25, from 6 a quarter of the way to 7. Median: position 3.5, halfway between 7 and 10. Q3Q_3: position 4.75, from 10 three quarters of the way to 14.

    Q1=6+0.25×1=6.25Q2=7+102=8.5Q3=10+0.75×4=13\begin{aligned} Q_1 &= 6 + 0.25 \times 1 = 6.25 \\[2pt] Q_2 &= \frac{7 + 10}{2} = 8.5 \\[2pt] Q_3 &= 10 + 0.75 \times 4 = 13 \end{aligned}
  3. 17.A data set has Q1=20Q_1 = 20 and Q3=30Q_3 = 30. Find the two fences of the 1.5 IQR rule. Is the value 48 an outlier?

    Show a hint
    IQR=10\text{IQR} = 10.
    Show one possible solution

    Answer: The fences are 5 and 45. Yes.

    IQR=30−20=10lower fence=20−15=5upper fence=30+15=45\begin{aligned} \text{IQR} &= 30 - 20 = 10 \\[2pt] \text{lower fence} &= 20 - 15 = 5 \\[2pt] \text{upper fence} &= 30 + 15 = 45 \end{aligned}

    The value 48 lies above 45, so it is an outlier.

  4. 18.The ages of nine members of a club: 10, 12, 13, 14, 15, 16, 17, 19, 40. Find Q1Q_1, Q3Q_3 and the upper fence. Is 40 an outlier?

    Show a hint
    The quartiles are the 3rd and the 7th value.
    Show one possible solution

    Answer: Q1=13Q_1 = 13, Q3=17Q_3 = 17, upper fence 23. Yes.

    Q1=13Q_1 = 13 and Q3=17Q_3 = 17, so IQR=4\text{IQR} = 4.

    upper fence=17+1.5×4=23\text{upper fence} = 17 + 1.5 \times 4 = 23

    The value 40 lies above it and is an outlier.

  5. 19.The values 2, 4, 6 and 8 are a whole population. Find the variance and the standard deviation.

    Show a hint
    Divide the sum of squares by N=4N = 4.
    Show one possible solution

    Answer: σ2=5\sigma^2 = 5 and σ≈2.236\sigma \approx 2.236.

    The mean is μ=204=5\mu = \tfrac{20}{4} = 5. The deviations are −3-3, −1-1, 1, 3, and their squares 9, 1, 1, 9 add up to 20.

    σ2=204=5σ=5≈2.236\begin{aligned} \sigma^2 &= \frac{20}{4} = 5 \\[4pt] \sigma &= \sqrt{5} \approx 2.236 \end{aligned}
  6. 20.Find the variance and the standard deviation of the sample 5, 7, 9, 11, 13.

    Show a hint
    The mean is 9. Divide by n−1=4n - 1 = 4.
    Show one possible solution

    Answer: s2=10s^2 = 10 and s≈3.162s \approx 3.162.

    The deviations are −4-4, −2-2, 0, 2, 4, and their squares 16, 4, 0, 4, 16 add up to 40.

    s2=404=10s=10≈3.162\begin{aligned} s^2 &= \frac{40}{4} = 10 \\[4pt] s &= \sqrt{10} \approx 3.162 \end{aligned}
  7. 21.Find the variance and the standard deviation of the sample 2, 3, 5, 6.

    Show a hint
    The mean is 4.
    Show one possible solution

    Answer: s2≈3.333s^2 \approx 3.333 and s≈1.826s \approx 1.826.

    The deviations are −2-2, −1-1, 1, 2, and their squares 4, 1, 1, 4 add up to 10.

    s2=103≈3.333s≈1.826\begin{aligned} s^2 &= \frac{10}{3} \approx 3.333 \\[4pt] s &\approx 1.826 \end{aligned}
  8. 22.For a sample of 5 values, ∑x=30\sum x = 30 and ∑x2=200\sum x^2 = 200. Find the mean, the variance and the standard deviation.

    Show a hint
    The shortcut: ∑x2−n×xˉ2\sum x^2 - n \times \bar{x}^2.
    Show one possible solution

    Answer: Mean 6, s2=5s^2 = 5, s≈2.236s \approx 2.236.

    xˉ=305=6sum of squares=200−5×62=20s2=204=5s=5≈2.236\begin{aligned} \bar{x} &= \frac{30}{5} = 6 \\[4pt] \text{sum of squares} &= 200 - 5 \times 6^2 = 20 \\[4pt] s^2 &= \frac{20}{4} = 5 \\[4pt] s &= \sqrt{5} \approx 2.236 \end{aligned}
  9. 23.In a sample of 10 days a machine stopped 0 times on 3 days, once on 4 days and twice on 3 days. Find the mean and the standard deviation of the number of stops per day.

    Show a hint
    ∑(f×x)=10\sum (f \times x) = 10 and ∑(f×x2)=16\sum (f \times x^2) = 16.
    Show one possible solution

    Answer: Mean 1, s≈0.816s \approx 0.816.

    ∑(f×x)=0+4+6=10xˉ=1∑(f×x2)=0+4+12=16sum of squares=16−10×12=6s2=69≈0.667s≈0.816\begin{aligned} \sum (f \times x) &= 0 + 4 + 6 = 10 \qquad \bar{x} = 1 \\[4pt] \sum (f \times x^2) &= 0 + 4 + 12 = 16 \\[4pt] \text{sum of squares} &= 16 - 10 \times 1^2 = 6 \\[4pt] s^2 &= \frac{6}{9} \approx 0.667 \\[4pt] s &\approx 0.816 \end{aligned}
  10. 24.A data set has the mean 120 and the standard deviation 15. Find the z-scores of the values 150 and 105.

    Show a hint
    The value minus the mean, divided by ss.
    Show one possible solution

    Answer: 22 and −1-1.

    150−12015=2105−12015=−1\begin{aligned} \frac{150 - 120}{15} &= 2 \\[4pt] \frac{105 - 120}{15} &= -1 \end{aligned}
  11. 25.A data set has the mean 40 and the standard deviation 6. Which value has the z-score −1.5-1.5?

    Show a hint
    x=xˉ+z×sx = \bar{x} + z \times s.
    Show one possible solution

    Answer: 3131.

    x=40+(−1.5)×6=40−9=31x = 40 + (-1.5) \times 6 = 40 - 9 = 31
  12. 26.The daily sales of a shop have the mean 80 and the standard deviation 12. Find the coefficient of variation.

    Show a hint
    sxˉ\dfrac{s}{\bar{x}}.
    Show one possible solution

    Answer: 0.150.15, or 15 percent.

    CV=1280=0.15\text{CV} = \frac{12}{80} = 0.15
  13. 27.The weights of bags are roughly bell-shaped with the mean 500 g and the standard deviation 20 g. About which share of the bags weighs between 460 and 540 g?

    Show a hint
    460 and 540 are two standard deviations from the mean.
    Show one possible solution

    Answer: About 95 percent.

    500±2×20500 \pm 2 \times 20 gives 460 and 540. By the 68-95-99.7 rule about 95 percent of bell-shaped data lie within two standard deviations.

  14. 28.Nothing is known about the shape of a data set. At least which share of its values lies within 2.5 standard deviations of the mean?

    Show a hint
    Chebyshev's rule with k=2.5k = 2.5.
    Show one possible solution

    Answer: At least 84 percent.

    1−12.52=1−16.25=1−0.16=0.841 - \frac{1}{2.5^2} = 1 - \frac{1}{6.25} = 1 - 0.16 = 0.84

Level 3: Rescaling, shape and putting ideas together

  1. 29.A column has the mean 12 and the standard deviation 3. Every value is replaced by y=2×x−4y = 2 \times x - 4. Find the new mean, standard deviation and variance.

    Show a hint
    The shift changes only the mean.
    Show one possible solution

    Answer: Mean 20, standard deviation 6, variance 36.

    mean:2×12−4=20standard deviation:2×3=6variance:62=36\begin{aligned} \text{mean:} \quad & 2 \times 12 - 4 = 20 \\[2pt] \text{standard deviation:} \quad & 2 \times 3 = 6 \\[2pt] \text{variance:} \quad & 6^2 = 36 \end{aligned}

    The variance also comes as 22×92^2 \times 9.

  2. 30.The temperatures of a month have the mean 25 and the standard deviation 4 in degrees Celsius. Find the mean and the standard deviation in degrees Fahrenheit (F=1.8×C+32F = 1.8 \times C + 32).

    Show a hint
    Only the factor 1.8 touches the standard deviation.
    Show one possible solution

    Answer: Mean 77, standard deviation 7.2.

    mean:1.8×25+32=77standard deviation:1.8×4=7.2\begin{aligned} \text{mean:} \quad & 1.8 \times 25 + 32 = 77 \\[2pt] \text{standard deviation:} \quad & 1.8 \times 4 = 7.2 \end{aligned}
  3. 31.The values 4, 6, 8, 10 and 12 are a population. Find the z-score of the value 12.

    Show a hint
    First μ=8\mu = 8 and σ2=8\sigma^2 = 8.
    Show one possible solution

    Answer: About 1.4141.414.

    The mean is μ=405=8\mu = \tfrac{40}{5} = 8. The squared deviations 16, 4, 0, 4, 16 add up to 40.

    σ2=405=8σ≈2.828z=12−82.828≈1.414\begin{aligned} \sigma^2 &= \frac{40}{5} = 8 \qquad \sigma \approx 2.828 \\[4pt] z &= \frac{12 - 8}{2.828} \approx 1.414 \end{aligned}
  4. 32.The values 20, 30, 50 and 100 are scaled to the interval from 0 to 1 with min-max scaling. What do 30 and 50 become?

    Show a hint
    Subtract 20 and divide by 80.
    Show one possible solution

    Answer: 0.1250.125 and 0.3750.375.

    30−2080=0.12550−2080=0.375\begin{aligned} \frac{30 - 20}{80} &= 0.125 \\[4pt] \frac{50 - 20}{80} &= 0.375 \end{aligned}
  5. 33.The times that 20 students needed for a task, in classes: 0 to 20 minutes: 6 students, 20 to 40 minutes: 10 students, 40 to 60 minutes: 4 students. Estimate the mean time.

    Show a hint
    The midpoints are 10, 30 and 50.
    Show one possible solution

    Answer: About 2828 minutes.

    ∑(f×m)=6×10+10×30+4×50=60+300+200=560xˉ≈56020=28\begin{aligned} \sum (f \times m) &= 6 \times 10 + 10 \times 30 + 4 \times 50 \\ &= 60 + 300 + 200 = 560 \\[4pt] \bar{x} &\approx \frac{560}{20} = 28 \end{aligned}
  6. 34.For the data 3, 4, 5, 6, 32 find the mean and the median. Then remove the value 32 and find them again.

    Show a hint
    Four values are left; their median is the mean of the two middle ones.
    Show one possible solution

    Answer: With 32: mean 10, median 5. Without: mean 4.5, median 4.5.

    with 32:xˉ=505=10median=5without:xˉ=184=4.5median=4+52=4.5\begin{aligned} \text{with 32:} \quad & \bar{x} = \frac{50}{5} = 10 \\[2pt] & \text{median} = 5 \\[6pt] \text{without:} \quad & \bar{x} = \frac{18}{4} = 4.5 \\[2pt] & \text{median} = \frac{4 + 5}{2} = 4.5 \end{aligned}

    The mean fell by 5.5, the median by 0.5.

  7. 35.A class of 30 students has the mean mark 72. The 20 girls have the mean 75. Find the mean of the 10 boys.

    Show a hint
    Work with totals.
    Show one possible solution

    Answer: 6666.

    total of the class=30×72=2160total of the girls=20×75=1500mean of the boys=2160−150010=66\begin{aligned} \text{total of the class} &= 30 \times 72 = 2160 \\[2pt] \text{total of the girls} &= 20 \times 75 = 1500 \\[2pt] \text{mean of the boys} &= \frac{2160 - 1500}{10} = 66 \end{aligned}
  8. 36.Ten values have the mean 15. Then a mistake is found: one value was written as 25 but should be 52. Find the correct mean.

    Show a hint
    How much does the total change?
    Show one possible solution

    Answer: 17.717.7.

    The old total is 150. The correction adds 52−25=2752 - 25 = 27.

    150+2710=17710=17.7\frac{150 + 27}{10} = \frac{177}{10} = 17.7
  9. 37.The marks of an exam have the mean 48 and the median 60. Is the distribution skewed to the left, skewed to the right, or symmetric?

    Show a hint
    The tail pulls the mean towards itself.
    Show one possible solution

    Answer: Skewed to the left.

    The mean lies below the median, so a tail of low values pulls it down: the data is skewed to the left. Most students did well and a few did very badly.

  10. 38.The sample 1, 2, 3, 4, 5 has the standard deviation s≈1.581s \approx 1.581. Without a new calculation, give the standard deviation of 101, 102, 103, 104, 105 and of 10, 20, 30, 40, 50.

    Show a hint
    The first set is shifted by 100, the second is multiplied by 10.
    Show one possible solution

    Answer: About 1.5811.581 and 15.81115.811.

    A shift does not change the spread: s≈1.581s \approx 1.581. A factor 10 multiplies it by 10: s≈15.811s \approx 15.811.

  11. 39.The numbers of mistakes in seven reports: 1, 2, 2, 3, 4, 5, 11. Find the mean, the median, Q1Q_1, Q3Q_3, the IQR and the upper fence. Is 11 an outlier?

    Show a hint
    The positions of the quartiles are 2.5 and 5.5.
    Show one possible solution

    Answer: Mean 4, median 3, Q1=2Q_1 = 2, Q3=4.5Q_3 = 4.5, IQR 2.5, upper fence 8.25. Yes.

    The mean is 287=4\tfrac{28}{7} = 4, and the median is the 4th value, 3. Q1Q_1 stands at the position 2.5, between 2 and 2. Q3Q_3 stands at the position 5.5, halfway between 4 and 5.

    Q1=2Q3=4.5IQR=4.5−2=2.5upper fence=4.5+1.5×2.5=8.25\begin{aligned} Q_1 &= 2 \qquad Q_3 = 4.5 \\[2pt] \text{IQR} &= 4.5 - 2 = 2.5 \\[2pt] \text{upper fence} &= 4.5 + 1.5 \times 2.5 = 8.25 \end{aligned}

    The value 11 lies above it and is an outlier.

  12. 40.The true values of a target are 2, 4, 6 and 8. A model predicts the mean, 5, for every row. Find its mean squared error. Then find the mean squared error of a model that predicts 4 for every row.

    Show a hint
    MSE=∑(true−predicted)2n\text{MSE} = \dfrac{\sum (\text{true} - \text{predicted})^2}{n}.
    Show one possible solution

    Answer: 55 and 66.

    Predicting 5, the errors are −3-3, −1-1, 1, 3. Predicting 4, they are −2-2, 0, 2, 4.

    predicting 5:9+1+1+94=204=5predicting 4:4+0+4+164=244=6\begin{aligned} \text{predicting 5:} \quad & \frac{9 + 1 + 1 + 9}{4} = \frac{20}{4} = 5 \\[4pt] \text{predicting 4:} \quad & \frac{4 + 0 + 4 + 16}{4} = \frac{24}{4} = 6 \end{aligned}

    The first value, 5, is the population variance of the target. No constant prediction has a smaller mean squared error than the mean.

16Check yourself

Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.

Question 1

1.What is the mean of 2, 4 and 9?

Question 2

2.What is the median of 9, 1, 5 and 3?

Question 3

3.Which of these summaries hardly changes when one value of the data becomes extremely large?

Question 4

4.A group of 10 people has the mean 20, and a group of 30 people has the mean 40. What is the mean of all 40 people?

Question 5

5.What is the interquartile range?

Question 6

6.By which number is the sum of the squared deviations divided to get the variance of a sample of nn values?

Question 7

7.What is the variance s2s^2 of the sample 1, 2, 3?

Question 8

8.Ten is added to every value of a data set. What happens to the standard deviation?

Question 9

9.Every value of a data set is multiplied by 3. By which factor is the variance multiplied?

Question 10

10.A data set has the mean 80 and the median 60. What does that say about its shape?

Question 11

11.A data set has Q1=10Q_1 = 10 and Q3=18Q_3 = 18. Where is the upper fence of the 1.5 IQR rule?

Question 12

12.A model is trained on a training set and tested on a test set. From which data come the mean and the standard deviation that are used to standardise the test set?

0 of 12 answered.

17Checking your work with Python

This is the only code of the lesson. NumPy and Python's statistics module calculate every summary of this lesson.

Python
import statistics
import numpy as np

data = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(data.mean(), np.median(data), statistics.mode(data.tolist()))
print(data.var(), data.std())
print(round(data.var(ddof=1), 3), round(data.std(ddof=1), 3))

values = np.array([3, 5, 7, 8, 9, 11, 13, 15, 16, 18, 21])
q1, q2, q3 = np.percentile(values, [25, 50, 75])
print(q1, q2, q3, q3 - q1)

z = (data - data.mean()) / data.std()
print(round(z.mean(), 3), round(z.std(), 3))

Output

5.0 4.5 4
4.0 2.0
4.571 2.138
7.5 11.0 15.5 8.0
0.0 1.0

Change it and run: put the data of a problem you solved by hand into data and compare. Then change ddof=1 to ddof=0 in the third print and see which line of the output it now repeats.

The first line prints the mean, the median and the mode of the eight values of Part 7. The second line is the population variance and standard deviation: NumPy divides by nn unless told otherwise. The third line uses ddof=1, which means "divide by n−1n - 1": the sample variance and standard deviation. The fourth line gives the quartiles and the IQR of the eleven values of Part 6, with the same rule as by hand. The last line standardises the data and shows that the result has the mean 0 and the standard deviation 1.

By hand and in Python
By handIn Python
meannp.mean(x) or x.mean()
mediannp.median(x)
modestatistics.mode(list)
population variance and standard deviationnp.var(x), np.std(x)
sample variance and standard deviationnp.var(x, ddof=1), np.std(x, ddof=1)
quartiles and percentilesnp.percentile(x, [25, 50, 75])
all summaries of a tabledf.describe() in pandas (its std uses n−1n - 1)

18Quick summary

Every idea of the lesson with its definition or rule
IdeaDefinition or rule
Kinds of datacategorical (nominal, ordinal) and numerical (discrete, continuous)
Frequency, relative, cumulativethe count; the share fn\dfrac{f}{n}; the count up to a value
Meanxˉ=∑xn\bar{x} = \dfrac{\sum x}{n}; the balance point; the deviations add up to 0
Weighted mean∑(w×x)∑w\dfrac{\sum (w \times x)}{\sum w}; from a table: ∑(f×x)n\dfrac{\sum (f \times x)}{n}
Medianthe middle of the sorted data; robust
Modethe most frequent value
Mean against medianmean above the median: tail to the right; mean below the median: tail to the left
Rangelargest minus smallest
Quartilesposition=1+(n−1)×p\text{position} = 1 + (n - 1) \times p in the sorted data, for p=0.25p = 0.25, 0.50.5, 0.750.75
IQRQ3−Q1Q_3 - Q_1, the width of the middle half; robust
Five-number summaryminimum, Q1Q_1, median, Q3Q_3, maximum: the box plot
Outlier fencesQ1−1.5×IQRQ_1 - 1.5 \times \text{IQR} and Q3+1.5×IQRQ_3 + 1.5 \times \text{IQR}
Population varianceσ2=∑(x−μ)2N\sigma^2 = \dfrac{\sum (x - \mu)^2}{N}
Sample variances2=∑(x−xˉ)2n−1s^2 = \dfrac{\sum (x - \bar{x})^2}{n - 1}
Standard deviationthe square root of the variance; the unit of the data
Shortcut∑(x−xˉ)2=∑x2−n×xˉ2\sum (x - \bar{x})^2 = \sum x^2 - n \times \bar{x}^2
z-scorez=x−xˉsz = \dfrac{x - \bar{x}}{s}
68-95-99.7 ruleonly for bell-shaped data
Chebyshev's ruleat least 1−1k21 - \dfrac{1}{k^2} within kk standard deviations, for any data
Coefficient of variationCV=sxˉ\text{CV} = \dfrac{s}{\bar{x}}
y=a×x+by = a \times x + bcentres: a×(old)+ba \times (\text{old}) + b; spreads: times ∣a∣\len{a}; variance: times a2a^2
Standardisingz=x−xˉsz = \dfrac{x - \bar{x}}{s}: mean 0, standard deviation 1
Min-max scalingx−minimummaximum−minimum\dfrac{x - \text{minimum}}{\text{maximum} - \text{minimum}}: from 0 to 1
Data in classesuse the midpoints: xˉ≈∑(f×m)n\bar{x} \approx \dfrac{\sum (f \times m)}{n}