Zum Inhalt springen

Lektion 7 · One Variable

Center and Spread of One Variable

Use mean, median, quantiles, IQR, standard deviation and MAD to describe a numeric variable, and learn why one single number is never enough.

Studying one variable at a time is called univariate analysis. It comes before comparisons, because you cannot compare two columns well if you do not understand each one. For a numeric column, start with four questions: how many values are there, where is the center, how spread out are they and what are the extremes?

Count first

Every statistic should come with its n, the number of values used. An average rating from 7 reviews and an average rating from 7,000 reviews deserve very different levels of trust. Note how many values were missing, too.

Measures of center

  • The mean is the sum divided by the count. Every value affects it, including extreme ones.
  • The median is the middle value after sorting. Extremes have little effect on it.
  • The mode is the most frequent value. It is most useful for categories and for counts.
  • A trimmed mean removes a fixed share from each end and averages the rest.
Python How one value moves the mean

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Adding one value of 400 roughly doubles the mean and barely moves the median. Neither number is wrong. The mean answers "what would each person get if the total were shared equally?" The median answers "what does the person in the middle earn?" Pick the one that fits your question.

With only nine values, a 10% trim removes nothing, because the code rounds the number of values to cut down to zero. Check what a trim really does on small data.

Quantiles and percentiles

A quantile is a cut point that splits sorted data. The 25th percentile has about a quarter of the values below it. The 50th percentile is the median. The values at 25%, 50% and 75% are the quartiles.

The minimum, three quartiles and maximum form the five-number summary. Software offers several ways to interpolate between data points, so two tools can report slightly different quartiles for small datasets. That is normal.

Python Five-number summary and percentiles

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For waiting times, load times and delays, high percentiles often matter as much as the center, because people remember the slow cases. With only 18 values, though, p95 depends on the two largest observations, so treat it with care.

Measures of spread

Two datasets can share a mean and look nothing alike.

Measure How it works Sensitive to extremes?
Range max minus min Very
Standard deviation typical distance from the mean, based on squares Yes
IQR Q3 minus Q1, the width of the middle half Little
MAD median of distances from the median Little
Python Compare spread measures

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

steady and wild have the same mean but very different spread. In spike, one value inflates the standard deviation while the IQR and MAD stay the same as in steady. The robust measures describe the bulk of the data; they also hide the spike. You need both views.

stdev computes the sample standard deviation, which divides by n minus 1 before taking the square root. pstdev is for a complete population. See the statistics module lesson.

describe() in pandas

pandas gives a quick overview of numeric data.

Python describe with extra percentiles

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the mean is clearly above the median, the data often has a long right tail. Treat that as a hint, not a rule, and look at a chart. The next lesson does exactly that.

Reading the numbers

Ask of each result:

  • Are the minimum and maximum possible in the real world?
  • Is the center near what you expected?
  • Is the spread large compared with the center?
  • Do many values sit at one exact number, such as 0 or a limit?
  • Does the unit make sense?

Be careful when you average averages. If one store has 10 orders and another has 1,000, the plain mean of the two store averages is not the average order value. Weight each average by its count, or go back to the rows.

Python Average of averages versus weighted average

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing what to report

For a roughly symmetric column without extremes, the mean with the standard deviation is compact and familiar. For skewed columns or columns with outliers, the median with the quartiles usually describes the typical case better. When totals matter, as with revenue or cost, report the mean or the sum as well, because the median cannot be multiplied back into a total.

If you are unsure, show a small set: n, missing count, minimum, quartiles, maximum and mean. A reader can see much about the shape from those few numbers.

Try it yourself

Change the founder's salary in the first example from 400 to 4,000. Predict the new mean and median before you run it. Then add nine more ordinary salaries and watch how much the influence of the single large value shrinks.

Compare center and spread together

A change in the median means more when you know the spread. A rise of two minutes is striking if waits usually stay within one minute of each other. It is easy to miss in data that swings by thirty minutes every day. Put the difference and a measure of spread side by side, and let the reader compare them.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which measure of center changes least when one huge value is added?

    The median depends on the middle position, not on how large the extreme value is.
  2. 2. What does the interquartile range describe?

    IQR is Q3 minus Q1.
  3. 3. Why report n next to a statistic?

    Results from few values are much less stable.
  4. 4. Two groups have very different sizes. How should you combine their means into an overall mean?

    A weighted mean equals the mean over all the underlying rows.

Summary

  • Always report the count and the number of missing values.
  • Mean, median, mode and trimmed mean answer different questions.
  • Quantiles describe positions; high percentiles show the slow or large cases.
  • IQR and MAD resist extremes, while range and standard deviation react to them.
  • Weight group averages by size, and check results against reality.

Next: Distributions and Histograms

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.