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
Given a list of values, there are several ways to count the frequency of each value.
You can use a Python dictionary:
You can use a Counter
(which is a dictionary with additional methods):
Or you can use the Hist
object provided by thinkstats2
:
Hist
provides Freq
, which looks up the frequency of a value.
You can also use the bracket operator, which does the same thing.
If the value does not appear, it has frequency 0.
The Values
method returns the values:
So you can iterate the values and their frequencies like this:
Or you can use the Items
method:
thinkplot
is a wrapper for matplotlib
that provides functions that work with the objects in thinkstats2
.
For example Hist
plots the values and their frequencies as a bar graph.
Config
takes parameters that label the x and y axes, among other things.
As an example, I'll replicate some of the figures from the book.
First, I'll load the data from the pregnancy file and select the records for live births.
Here's the histogram of birth weights in pounds. Notice that Hist
works with anything iterable, including a Pandas Series. The label
attribute appears in the legend when you plot the Hist
.
Before plotting the ages, I'll apply floor
to round down:
As an exercise, plot the histogram of pregnancy lengths (column prglngth
).
Hist
provides smallest, which select the lowest values and their frequencies.
Use Largest
to display the longest pregnancy lengths.
From live births, we can select first babies and others using birthord
, then compute histograms of pregnancy length for the two groups.
We can use width
and align
to plot two histograms side-by-side.
Series
provides methods to compute summary statistics:
Here are the mean and standard deviation:
As an exercise, confirm that std
is the square root of var
:
Here's are the mean pregnancy lengths for first babies and others:
And here's the difference (in weeks):
This functon computes the Cohen effect size, which is the difference in means expressed in number of standard deviations:
Compute the Cohen effect size for the difference in pregnancy length for first babies and others.
Exercises
Using the variable totalwgt_lb
, investigate whether first babies are lighter or heavier than others.
Compute Cohen’s effect size to quantify the difference between the groups. How does it compare to the difference in pregnancy length?
For the next few exercises, we'll load the respondent file:
Make a histogram of totincr the total income for the respondent's family. To interpret the codes see the codebook.
Make a histogram of age_r, the respondent's age at the time of interview.
Make a histogram of numfmhh, the number of people in the respondent's household.
Make a histogram of parity, the number of children borne by the respondent. How would you describe this distribution?
Use Hist.Largest to find the largest values of parity.
Let's investigate whether people with higher income have higher parity. Keep in mind that in this study, we are observing different people at different times during their lives, so this data is not the best choice for answering this question. But for now let's take it at face value.
Use totincr to select the respondents with the highest income (level 14). Plot the histogram of parity for just the high income respondents.
Find the largest parities for high income respondents.
Compare the mean parity for high income respondents and others.
Compute the Cohen effect size for this difference. How does it compare with the difference in pregnancy length for first babies and others?