Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/chap05.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 5
Copyright 2017 Allen Downey
Reading data
Pandas is a library that provides tools for reading and processing data. read_html
reads a web page from a file or the Internet and creates one DataFrame
for each table on the page.
The data directory contains a downloaded copy of https://en.wikipedia.org/wiki/World_population_estimates
The arguments of read_html
specify the file to read and how to interpret the tables in the file. The result, tables
, is a sequence of DataFrame
objects; len(tables)
reports the length of the sequence.
We can select the DataFrame
we want using the bracket operator. The tables are numbered from 0, so tables[2]
is actually the third table on the page.
head
selects the header and the first five rows.
tail
selects the last five rows.
Long column names are awkard to work with, but we can replace them with abbreviated names.
Here's what the DataFrame looks like now.
The first column, which is labeled Year
, is special. It is the index for this DataFrame
, which means it contains the labels for the rows.
Some of the values use scientific notation; for example, 2.544000e+09
is shorthand for or 2.544 billion.
NaN
is a special value that indicates missing data.
Series
We can use dot notation to select a column from a DataFrame
. The result is a Series
, which is like a DataFrame
with a single column.
Like a DataFrame
, a Series
contains an index, which labels the rows.
1e9
is scientific notation for or 1 billion.
From here on, we will work in units of billions.
Here's what these estimates look like.
The following expression computes the elementwise differences between the two series, then divides through by the UN value to produce relative errors, then finds the largest element.
So the largest relative error between the estimates is about 1.3%.
Exercise: Break down that expression into smaller steps and display the intermediate results, to make sure you understand how it works.
Compute the elementwise differences,
census - un
Compute the absolute differences,
abs(census - un)
Compute the relative differences,
abs(census - un) / un
Compute the percent differences,
abs(census - un) / un * 100
max
and abs
are built-in functions provided by Python, but NumPy also provides version that are a little more general. When you import modsim
, you get the NumPy versions of these functions.
Constant growth
We can select a value from a Series
using bracket notation. Here's the first element:
And the last value.
But rather than "hard code" those dates, we can get the first and last labels from the Series
:
And we can get the first and last values:
Then we can compute the average annual growth in billions of people per year.
TimeSeries
Now let's create a TimeSeries
to contain values generated by a linear growth model.
Initially the TimeSeries
is empty, but we can initialize it so the starting value, in 1950, is the 1950 population estimated by the US Census.
After that, the population in the model grows by a constant amount each year.
Here's what the results looks like, compared to the actual data.
The model fits the data pretty well after 1990, but not so well before.
Exercises
Optional Exercise: Try fitting the model using data from 1970 to the present, and see if that does a better job.
Hint:
Copy the code from above and make a few changes. Test your code after each small change.
Make sure your
TimeSeries
starts in 1950, even though the estimated annual growth is based on later data.You might want to add a constant to the starting value to match the data better.