Think Stats by Allen B. Downey Think Stats is an introduction to Probability and Statistics for Python programmers.
This is the accompanying code for this book.
License: GPL3
Examples and Exercises from Think Stats, 2nd Edition
Copyright 2016 Allen B. Downey
MIT License: https://opensource.org/licenses/MIT
Exponential distribution
Here's what the exponential CDF looks like with a range of parameters.
Here's the distribution of interarrival times from a dataset of birth times.
Here's what the CCDF looks like on a log-y scale. A straight line is consistent with an exponential distribution.
Normal distribution
Here's what the normal CDF looks like with a range of parameters.
I'll use a normal model to fit the distribution of birth weights from the NSFG.
Here's the observed CDF and the model. The model fits the data well except in the left tail.
A normal probability plot is a visual test for normality. The following example shows that if the data are actually from a normal distribution, the plot is approximately straight.
Here's the normal probability plot for birth weights, showing that the lightest babies are lighter than we expect from the normal mode, and the heaviest babies are heavier.
If we suspect that the deviation in the left tail is due to preterm babies, we can check by selecting only full term births.
Now the deviation in the left tail is almost gone, but the heaviest babies are still heavy.
Lognormal model
As an example of a lognormal disrtribution, we'll look at adult weights from the BRFSS.
The following function estimates the parameters of a normal distribution and plots the data and a normal model.
Here's the distribution of adult weights and a normal model, which is not a very good fit.
Here's the distribution of adult weight and a lognormal model, plotted on a log-x scale. The model is a better fit for the data, although the heaviest people are heavier than the model expects.
The following function generates a normal probability plot.
When we generate a normal probability plot with adult weights, we can see clearly that the data deviate from the model systematically.
If we make a normal probability plot with log weights, the model fit the data well except in the tails, where the heaviest people exceed expectations.
Pareto distribution
Here's what the Pareto CDF looks like with a range of parameters.
The distribution of populations for cities and towns is sometimes said to be Pareto-like.
Here's the distribution of population for cities and towns in the U.S., along with a Pareto model. The model fits the data well in the tail.
The lognormal model might be a better fit for this data (as is often the case for things that are supposed to be Pareto).
Here's a normal probability plot for the log-populations. The model fits the data well except in the right tail, where the biggest cities are bigger than expected.
Random variates
When we have an analytic CDF, we can sometimes invert it to generate random values. The following function generates values from an exponential distribution.
We can test it by generating a sample.
And plotting the CCDF on a log-y scale.
A straight line is consistent with an exponential distribution.
As an exercise, write a function that generates a Pareto variate.
Exercises
Exercise: In the BRFSS (see Section 5.4), the distribution of heights is roughly normal with parameters µ = 178 cm and σ = 7.7 cm for men, and µ = 163 cm and σ = 7.3 cm for women.
In order to join Blue Man Group, you have to be male between 5’10” and 6’1” (see http://bluemancasting.com). What percentage of the U.S. male population is in this range? Hint: use scipy.stats.norm.cdf
.
scipy.stats
contains objects that represent analytic distributions
For example scipy.stats.norm represents a normal distribution.
A "frozen random variable" can compute its mean and standard deviation.
It can also evaluate its CDF. How many people are more than one standard deviation below the mean? About 16%
How many people are between 5'10" and 6'1"?
Exercise: To get a feel for the Pareto distribution, let’s see how different the world would be if the distribution of human height were Pareto. With the parameters xm = 1 m and α = 1.7, we get a distribution with a reasonable minimum, 1 m, and median, 1.5 m.
Plot this distribution. What is the mean human height in Pareto world? What fraction of the population is shorter than the mean? If there are 7 billion people in Pareto world, how many do we expect to be taller than 1 km? How tall do we expect the tallest person to be?
scipy.stats.pareto
represents a pareto distribution. In Pareto world, the distribution of human heights has parameters alpha=1.7 and xmin=1 meter. So the shortest person is 100 cm and the median is 150.
What is the mean height in Pareto world?
What fraction of people are shorter than the mean?
Out of 7 billion people, how many do we expect to be taller than 1 km? You could use dist.cdf or dist.sf.
How tall do we expect the tallest person to be?
Exercise: The Weibull distribution is a generalization of the exponential distribution that comes up in failure analysis (see http://wikipedia.org/wiki/Weibull_distribution). Its CDF is
Can you find a transformation that makes a Weibull distribution look like a straight line? What do the slope and intercept of the line indicate?
Use random.weibullvariate
to generate a sample from a Weibull distribution and use it to test your transformation.
Generate a sample from a Weibull distribution and plot it using a transform that makes a Weibull distribution look like a straight line.
thinkplot.Cdf
provides a transform that makes the CDF of a Weibull distribution look like a straight line.
Exercise: For small values of n
, we don’t expect an empirical distribution to fit an analytic distribution exactly. One way to evaluate the quality of fit is to generate a sample from an analytic distribution and see how well it matches the data.
For example, in Section 5.1 we plotted the distribution of time between births and saw that it is approximately exponential. But the distribution is based on only 44 data points. To see whether the data might have come from an exponential distribution, generate 44 values from an exponential distribution with the same mean as the data, about 33 minutes between births.
Plot the distribution of the random values and compare it to the actual distribution. You can use random.expovariate to generate the values.
Worked Example: The distributions of wealth and income are sometimes modeled using lognormal and Pareto distributions. To see which is better, let’s look at some data.
The Current Population Survey (CPS) is a joint effort of the Bureau of Labor Statistics and the Census Bureau to study income and related variables. Data collected in 2013 is available from http://www.census.gov/hhes/www/cpstables/032013/hhinc/toc.htm. I downloaded hinc06.xls
, which is an Excel spreadsheet with information about household income, and converted it to hinc06.csv
, a CSV file you will find in the repository for this book. You will also find hinc.py
, which reads this file.
Extract the distribution of incomes from this dataset. Are any of the analytic distributions in this chapter a good model of the data?
income | freq | cumsum | ps | |
---|---|---|---|---|
0 | 4.999000e+03 | 4204 | 4204 | 0.034330 |
1 | 9.999000e+03 | 4729 | 8933 | 0.072947 |
2 | 1.499900e+04 | 6982 | 15915 | 0.129963 |
3 | 1.999900e+04 | 7157 | 23072 | 0.188407 |
4 | 2.499900e+04 | 7131 | 30203 | 0.246640 |
5 | 2.999900e+04 | 6740 | 36943 | 0.301679 |
6 | 3.499900e+04 | 6354 | 43297 | 0.353566 |
7 | 3.999900e+04 | 5832 | 49129 | 0.401191 |
8 | 4.499900e+04 | 5547 | 54676 | 0.446488 |
9 | 4.999900e+04 | 5254 | 59930 | 0.489392 |
10 | 5.499900e+04 | 5102 | 65032 | 0.531056 |
11 | 5.999900e+04 | 4256 | 69288 | 0.565810 |
12 | 6.499900e+04 | 4356 | 73644 | 0.601382 |
13 | 6.999900e+04 | 3949 | 77593 | 0.633629 |
14 | 7.499900e+04 | 3756 | 81349 | 0.664301 |
15 | 7.999900e+04 | 3414 | 84763 | 0.692180 |
16 | 8.499900e+04 | 3326 | 88089 | 0.719341 |
17 | 8.999900e+04 | 2643 | 90732 | 0.740923 |
18 | 9.499900e+04 | 2678 | 93410 | 0.762792 |
19 | 9.999900e+04 | 2223 | 95633 | 0.780945 |
20 | 1.049990e+05 | 2606 | 98239 | 0.802226 |
21 | 1.099990e+05 | 1838 | 100077 | 0.817235 |
22 | 1.149990e+05 | 1986 | 102063 | 0.833453 |
23 | 1.199990e+05 | 1464 | 103527 | 0.845408 |
24 | 1.249990e+05 | 1596 | 105123 | 0.858441 |
25 | 1.299990e+05 | 1327 | 106450 | 0.869278 |
26 | 1.349990e+05 | 1253 | 107703 | 0.879510 |
27 | 1.399990e+05 | 1140 | 108843 | 0.888819 |
28 | 1.449990e+05 | 1119 | 109962 | 0.897957 |
29 | 1.499990e+05 | 920 | 110882 | 0.905470 |
30 | 1.549990e+05 | 1143 | 112025 | 0.914803 |
31 | 1.599990e+05 | 805 | 112830 | 0.921377 |
32 | 1.649990e+05 | 731 | 113561 | 0.927347 |
33 | 1.699990e+05 | 575 | 114136 | 0.932042 |
34 | 1.749990e+05 | 616 | 114752 | 0.937072 |
35 | 1.799990e+05 | 570 | 115322 | 0.941727 |
36 | 1.849990e+05 | 502 | 115824 | 0.945826 |
37 | 1.899990e+05 | 364 | 116188 | 0.948799 |
38 | 1.949990e+05 | 432 | 116620 | 0.952327 |
39 | 1.999990e+05 | 378 | 116998 | 0.955413 |
40 | 2.499990e+05 | 2549 | 119547 | 0.976229 |
41 | inf | 2911 | 122458 | 1.000000 |
Here's what the CDF looks like on a linear scale.
To check whether a Pareto model describes the data well, I plot the CCDF on a log-log scale.
I found parameters for the Pareto model that match the tail of the distribution.
For the lognormal model I estimate mu and sigma using percentile-based statistics (median and IQR).
Here's what the distribution, and fitted model, look like on a log-x scale.
My conclusions based on these figures are:
The Pareto model might be a reasonable choice for the top 10-20% of incomes.
The lognormal model captures the shape of the distribution better, with some deviation in the left tail. With different choices for sigma, you could match the upper or lower tail, but not both at the same time.
In summary I would say that neither model captures the whole distribution, so you might have to
look for another analytic model,
choose one that captures the part of the distribution that is most relevent, or
avoid using an analytic model altogether.