本书暂未推出简体中文版。现有版本:English。
University Economics — Year 1 · 本科一年级
29Describing Data
In 1978, 2,490 American men of a household panel earned on average 21,554 dollars of 1982, and of them earned less than that average. (The samples of this chapter and the next three measure earnings in dollars of 1982.)
In the United States in 2022, the richest hundredth of the population had an average income 9.37 times the median. A mean is a fine summary of a symmetric distribution and a poor one of a skewed one, and incomes are skewed. This chapter describes data before any model: distributions, their centre and spread, how two variables move together, how means differ across groups — and the traps in each.
On the ordinary axis the bottom four-fifths are squeezed together and the top hundredth stands alone at 629 dollars a day. On the logarithmic axis, equal distances are equal ratios: the median, 67 dollars, is 2.7 times the tenth hundredth and 0.4 times the ninetieth.
You already know
Mean, median, quartiles, variance and standard deviation were met in the mathematics series; the Lorenz curve, the Gini coefficient and top shares at school. Python and pandas are Chapter 28.
How it is measured — Income in a survey
The opening chart’s incomes come from a household survey. Household income is divided by the number of members, which assumes that everyone in the household shares equally; and wealthy households are less likely to answer, so the distribution may have too thin a tail at the top. Tax records, which reach the top better, give larger top shares.
29.1 Histograms and quantiles
Definition 29.1 (Histogram)
A histogram divides the range of a variable into bins and draws over each a bar whose area is the share of observations in the bin: its height is the share divided by the bin’s width, a density, so that the areas sum to 1 whatever the widths.
Definition 29.2 (Empirical distribution function)
The empirical distribution function of a sample is , the share of observations at or below .
Definition 29.3 (Quantile, percentile)
The quantile of order is the smallest value with (or an interpolation between two observations, as pandas does by default); the -th percentile is the quantile of order .
"""Earnings in 1978 of 2,490 American men (PSID sample): a histogram and quantiles."""
import numpy as np
from econdata import nsw
earn = nsw("psid_controls").re78 # dollars of 1982
counts, edges = np.histogram(earn, bins=np.arange(0, 130_001, 10_000))
width = edges[1] - edges[0]
density = counts / (len(earn) * width) # the bars' areas sum to 1
for lo, n, d in zip(edges[:-1], counts, density):
print(f"{lo:>7,.0f} to {lo + width:>7,.0f}: {n:5d} density {d:.2e}")
q = earn.quantile([0.1, 0.25, 0.5, 0.75, 0.9]).round(0)
print(q.to_string())
print(
f"mean {earn.mean():,.0f}; share below the mean {(earn < earn.mean()).mean():.3f}"
)
$ python histogram.py
0 to 10,000: 562 density 2.26e-05
10,000 to 20,000: 623 density 2.50e-05
20,000 to 30,000: 730 density 2.93e-05
30,000 to 40,000: 370 density 1.49e-05
40,000 to 50,000: 108 density 4.34e-06
50,000 to 60,000: 41 density 1.65e-06
60,000 to 70,000: 17 density 6.83e-07
70,000 to 80,000: 20 density 8.03e-07
80,000 to 90,000: 11 density 4.42e-07
90,000 to 100,000: 4 density 1.61e-07
100,000 to 110,000: 1 density 4.02e-08
110,000 to 120,000: 2 density 8.03e-08
120,000 to 130,000: 1 density 4.02e-08
0.10 0.0
0.25 11526.0
0.50 20688.0
0.75 29554.0
0.90 38421.0
mean 21,554; share below the mean 0.514
29.2 Mean, variance and skewness
Definition 29.4 (Skewness)
The skewness of a sample is , with : positive when the right tail is longer, negative when the left one is, zero for a symmetric distribution.
"""Mean, standard deviation and skewness of earnings and of their logarithm."""
import numpy as np
from econdata import nsw
from scipy.stats import skew
earn = nsw("psid_controls").re78
pos = earn[earn > 0] # the logarithm needs positive earnings
print("zero earnings:", (earn == 0).sum())
for name, x in [("earnings", pos), ("log earnings", np.log(pos))]:
print(
f"{name:13s} mean {x.mean():10.3f} sd {x.std():10.3f} skewness {skew(x):6.3f}"
)
$ python moments.py
zero earnings: 286
earnings mean 24350.845 sd 14326.321 skewness 1.645
log earnings mean 9.897 sd 0.746 skewness -1.913
The logarithm pulls in the long right tail, but it stretches the left one: a man with very low positive earnings sits far below the others on a log scale. In Brenland’s survey, where no household has income near zero, the logarithm of disposable income has a skewness of , against for the level (weekend problem). Whether the logarithm makes a distribution symmetric depends on its bottom as much as on its top.
29.3 Covariance and correlation
Definition 29.5 (Sample covariance, correlation coefficient)
The sample covariance of and is ; the correlation coefficient is .
Proposition 29.6 (Range of the correlation)
, with if and only if the points lie on a straight line that is not horizontal or vertical.
Proof. Let and . By the Cauchy–Schwarz inequality, , so and . Equality holds when for some , that is for every . ∎
"""Covariance and correlation: earnings in 1975 and 1978, and a parabola."""
import numpy as np
from econdata import nsw
d = nsw("psid_controls")
x, y = d.re75, d.re78
n = len(d)
cov = ((x - x.mean()) * (y - y.mean())).sum() / (n - 1)
r = cov / (x.std() * y.std()) # pandas' std divides by n - 1 too
print(f"by hand: cov {cov:,.0f} r {r:.4f}")
print(f"with pandas: cov {x.cov(y):,.0f} r {x.corr(y):.4f}")
rng = np.random.default_rng(2026)
u = rng.uniform(-1, 1, 1000)
v = u**2 # v is a function of u, yet ...
print(f"parabola: r {np.corrcoef(u, v)[0, 1]:.4f}")
$ python covariance.py
by hand: cov 154,706,518 r 0.7315
with pandas: cov 154,706,518 r 0.7315
parabola: r 0.0672
A correlation measures how close the points are to a straight line. It says nothing about a relation that is not linear: on a symmetric range, is entirely determined by , and the correlation is near zero.
29.4 Conditional means
Definition 29.7 (Conditional mean (in data))
The conditional mean of given in a sample is the mean of over the observations with ; as a function of , it describes how the average of changes with .
"""Conditional means: earnings in 1978 by years of schooling (PSID sample)."""
from econdata import nsw
d = nsw("psid_controls")
d = d[d.education.between(8, 17)]
g = d.groupby("education").re78.agg(["count", "mean", "median"]).round(0)
print(g.to_string())
$ python group_means.py
count mean median
education
8 148 14888.0 13300.0
9 104 16130.0 13743.0
10 163 17797.0 17733.0
11 162 16153.0 14149.0
12 853 20564.0 20688.0
13 135 20934.0 22166.0
14 184 24465.0 25121.0
15 64 27804.0 25687.0
16 283 30184.0 28816.0
17 211 32285.0 31032.0
The samples were assembled to test whether non-experimental methods can reproduce the result of an experiment (LaLonde 1986; Dehejia and Wahba 1999).
The conditional means describe; whether a year of schooling causes the step from to dollars between 11 and 12 years is the question of Chapter 32.
29.5 Traps
Definition 29.8 (Composition effect, Simpson’s paradox)
A composition effect is a change or difference in an overall mean that comes from the shares of the groups rather than from the means within them. Simpson’s paradox is its extreme case: a comparison that goes one way within every group and the other way overall.
"""Brenland's survey: older and younger households, overall and by work."""
from country import load
h = load("households")
h["older"] = h.age_head >= 50 # head aged 50 or more
h["works"] = h.hours > 0
overall = h.groupby("older").disposable.mean().round(0)
print("overall:", overall.to_dict())
by_work = h.groupby(["works", "older"]).disposable.agg(["count", "mean"]).round(0)
print(by_work.to_string())
print("share working:", h.groupby("older").works.mean().round(3).to_dict())
$ python simpson.py
overall: {False: 25301.0, True: 24519.0}
count mean
works older
False False 57 9353.0
True 504 18370.0
True False 968 26240.0
True 471 31098.0
share working: {False: 0.944, True: 0.483}
On the record — Admissions by department
In 1973 the six largest departments of a graduate school admitted of 2,691 male applicants and of 1,835 female applicants. Department by department the gap largely disappeared: women had applied more often to the departments that admitted a small share of all applicants. The pooled figures showed a pattern that the departments’ own decisions did not (Bickel, Hammel and O’Connell 1975).
Definition 29.9 (Top-coding, censoring)
A variable is top-coded when values above a cap are recorded at the cap. More generally, it is censored when values beyond a limit are known only to lie beyond it.
"""The CPS sample: earnings above a cap were recorded at the cap."""
from econdata import nsw
earn = nsw("cps_controls").re78
cap = earn.max()
at_cap = earn == cap
n_cap = at_cap.sum()
print(f"cap {cap:,.2f}; men at the cap {n_cap} of {len(earn)} ({at_cap.mean():.3f})")
print(f"mean as recorded {earn.mean():10,.0f}")
print(f"mean without the capped {earn[~at_cap].mean():10,.0f}")
print(f"median {earn.median():10,.0f}")
print(
"the true mean is at least", f"{earn.mean():,.0f}"
) # capped values are lower bounds
$ python top_coding.py
cap 25,564.70; men at the cap 4145 of 15992 (0.259)
mean as recorded 14,847
mean without the capped 11,097
median 16,422
the true mean is at least 14,847
The CPS sample’s spike at its cap holds a quarter of the men. Every mean, variance or regression computed on it treats 25,564.70 dollars as their earnings.
Definition 29.10 (Outlier, trimmed mean)
An outlier is an observation far from the bulk of the data. The -trimmed mean drops the fraction of the observations at each end and averages the rest.
"""Mean, median and trimmed mean, with and without the largest earnings."""
from econdata import nsw
from scipy.stats import trim_mean
earn = nsw("nsw_dw").re78 # the 445 men of the experiment
top = earn.idxmax()
for name, x in [("all", earn), ("without the largest", earn.drop(top))]:
print(
f"{name:20s} n {len(x)} mean {x.mean():7,.0f} median {x.median():7,.0f}"
f" 5% trimmed {trim_mean(x, 0.05):7,.0f}"
)
print(f"largest {earn.max():,.0f}")
$ python trimmed.py
all n 445 mean 5,301 median 3,702 5% trimmed 4,538
without the largest n 444 mean 5,177 median 3,673 5% trimmed 4,507
largest 60,308
Exercises
Exercise 29.1 ★
From the opening chart’s table, the median hundredth of Americans had 67.15 dollars a day in 2022 and the ninetieth 169.65. Give the ratio, and say where each lies on the logarithmic panel.
Solution
Solution of Exercise 29.1.
. On the logarithmic panel the ninetieth hundredth lies decades above the median, about as far above it as the tenth hundredth (24.55 dollars) lies below it.
Exercise 29.2 ★
Find the mean and the median of , , , , . Which describes the typical value?
Solution
Solution of Exercise 29.2.
Mean , median . The median: one large value pulls the mean above four of the five numbers.
Exercise 29.3 ★
Two variables have a covariance of 12 and standard deviations of 3 and 5. Find their correlation.
Solution
Solution of Exercise 29.3.
.
Exercise 29.4 ★
A country’s average wage falls in a year in which the wage of every occupation rises. Name the trap and give a mechanism.
Solution
Solution of Exercise 29.4.
A composition effect: employment shifted towards lower-paid occupations, for example because high-paid jobs were lost in a recession or many young workers entered low-paid ones.
Exercise 29.5 ★★
For the PSID men earning at least 5,000 dollars, compute the skewness of earnings and of their logarithm, and compare with Output 29.2.
Solution
Solution of Exercise 29.5.
For the 2,107 men earning at least 5,000 dollars, the skewness is for earnings and for their logarithm: without the lowest earnings, the logarithm is close to symmetric.
Exercise 29.6 ★★
Replace the CPS sample’s capped values by 1.5 times the cap. By how much does the mean change? And the median?
Solution
Solution of Exercise 29.6.
The mean rises from 14,847 to 18,160 dollars, by ; the median stays at 16,422, since it lies below the cap.
Exercise 29.7 ★★
From Output 29.4, give the step in mean earnings from 11 to 12 years of schooling, and the step from 12 to 16 years per year.
Solution
Solution of Exercise 29.7.
dollars from 11 to 12 years; from 12 to 16 years, dollars a year.
Exercise 29.8 ★★
Prove Proposition 29.6 without the Cauchy–Schwarz inequality, using .
Solution
Solution of Exercise 29.8.
With and , expanding gives , so . Equality needs every term zero: , a straight line.
Exercise 29.9 ★★
A billionaire moves into a town of 10,000 people. Predict the change in mean and in median wealth.
Solution
Solution of Exercise 29.9.
The mean rises by about dollars; the median barely moves (it shifts by half a step between two neighbouring values).
Exercise 29.10 ★★★
Department A admits 80 of 100 male and 18 of 20 female applicants; department B admits 10 of 50 male and 30 of 130 female applicants. Compute the admission rates by department and overall, and explain.
Solution
Solution of Exercise 29.10.
Men: in A, in B, overall. Women: in A, in B, overall. Women are admitted at a higher rate in each department and at a lower rate overall, because most of them applied to B, which admits few.
Exercise 29.11 ★★★
Show that minimises over , and that a median minimises .
Solution
Solution of Exercise 29.11.
The derivative of is , zero at , and the function is convex. For , moving up by a small changes the sum by times (the number of observations below minus the number above); the sum falls while more lie above and rises once more lie below, so it is smallest where half lie on each side: at a median.
29.6 Problem: Brenland’s Household Survey
Problem 29.1
Weekend problem — a survey, described
Brenland’s household survey of 2025 (invented) has 2,000 households, with disposable and gross income, the head’s age, schooling and hours, and the household’s size. Each answer comes from a script.
Part I — The distribution.
- Give the counts of disposable income in bins of 10,000 dalens.
- Give the counts of of disposable income in bins of from to .
- Which histogram is closer to symmetric?
- Give the mean and the median.
- What share of households is below the mean?
- Why is the mean above the median?
Part II — Moments and quantiles.
- Give the standard deviation and the skewness.
- Give the skewness of the logarithm.
- Give the 10th and 90th percentiles and their ratio.
- What share of all disposable income goes to the top tenth of households?
- Why may a survey understate that share?
Part III — Income and schooling.
- Give mean gross income for heads with under 12, 12, 13–15 and 16 or more years of schooling.
- Give the correlation of schooling with gross income, and with its logarithm.
- Why might the correlation understate how much schooling and income are related?
- Does the table show that schooling raises income?
Part IV — The paradox. Split households by the head’s age (under 50, 50 or over) and by whether the head works.
- Give mean disposable income of each age group, overall.
- Give the four means within working and non-working households.
- Give the share working in each age group.
- Rebuild each overall mean as a weighted average of the within-group means.
- Compute the older households’ mean if they worked as often as the younger.
- Which comparison answers “do older households have more income when they work?”
- Which answers “do older households have more income?”
- Name the trap.
- What would a reader of the overall means alone conclude?
- State the result.
Solution
Solution of Problem 29.1.
1. 49, 731, 734, 315, 111, 40, 11, 7, 1, 1 (from 0 to 100,000).
2. 30, 249, 926, 598, 163, 15: 1,981 households. The other 19, all at the minimum of 6,000 dalens (), lie below .
3. The logarithmic one: the level has a long right tail.
4. Mean dalens; median .
5. .
6. The distribution is skewed to the right: a few high incomes raise the mean and not the median.
7. dalens; skewness .
8. .
9. 14,789 and 38,991; ratio .
10. .
11. Wealthy households answer less often, and top incomes are under-reported.
12. 22,355; 24,775; 28,367; 35,762 dalens.
13. ; with the logarithm.
14. Income also depends on age, hours and luck, and retired households with high schooling have pensions rather than earnings; the relation may also be steeper at high schooling than a line allows.
15. No: it describes; people with more schooling may differ in other ways that raise income.
16. Under 50: 25,301; 50 or over: 24,519 dalens.
17. Not working: 9,353 (under 50) and 18,370 (50 or over). Working: 26,240 and 31,098.
18. under 50, at 50 or over.
19. ; (with the shares rounded to and the sums are about 25,294 and 24,518).
20. dalens.
21. The within-group comparison.
22. The overall comparison, if the question is about households as they are.
23. Simpson’s paradox, a composition effect.
24. That households get poorer with age at every stage, when the difference comes from retirement.
25. Within working and within non-working households, older heads’ households have more income, yet overall their mean is lower by 782 dalens, because half of them no longer work.