<jupyter_start><jupyter_text>Chapter 10 *Modeling and Simulation in Python*Copyright 2021 <NAME>License: [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-nc-sa/4.0/)<jupyter_code># download modsim.py if necessary

from os.path import basename, exists

def download(url):
    filename = basename(url)
    if not exists(filename):
        from urllib.request import urlretrieve
        local, _ = urlretrieve(url, filename)
        print('Downloaded ' + local)
    
download('https://raw.githubusercontent.com/AllenDowney/' +
         'ModSimPy/master/modsim.py')
# import functions from modsim

from modsim import *<jupyter_output><empty_output><jupyter_text>[Click here to run this chapter on Colab](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master//chapters/chap10.ipynb) This chapter presents case studies where you can apply the tools we have learned so far to problems involving the growth of human and salmon populations, queueing systems, and a model of tree growth.   Prehistoric World PopulationOn the Wikipedia page about world population growth, the second table contains estimates for prehistoric populations.<jupyter_code>download('https://raw.githubusercontent.com/AllenDowney/' +
         'ModSimPy/master/data/World_population_estimates.html')
from pandas import read_html

filename = 'World_population_estimates.html'
tables = read_html(filename, header=0, index_col=0, decimal='M')
len(tables)<jupyter_output><empty_output><jupyter_text>Select `tables[1]`, which is the second table on the page.<jupyter_code>table1 = tables[1]
table1.head()<jupyter_output><empty_output><jupyter_text>Some of the values are null because not all researchers provide estimates for the same dates.Again, we'll replace the long column names with more convenient abbreviations.<jupyter_code>table1.columns = ['PRB', 'UN', 'Maddison', 'HYDE', 'Tanton', 
                  'Biraben', 'McEvedy & Jones', 'Thomlinson', 'Durand', 'Clark']<jupyter_output><empty_output><jupyter_text>Some of the estimates are in a form Pandas doesn't recognize as numbers, but we can coerce them to be numeric.<jupyter_code>for col in table1.columns:
    table1[col] = pd.to_numeric(table1[col], errors='coerce')<jupyter_output><empty_output><jupyter_text>Here are the results.  Notice that we are working in millions now, not billions.<jupyter_code>table1.plot()
decorate(xlim=[-10000, 2000], xlabel='Year', 
         ylabel='World population (millions)',
         title='Prehistoric population estimates')
plt.legend(fontsize='small');<jupyter_output><empty_output><jupyter_text>We can use `xlim` to zoom in on everything after Year 0. The following figure shows the estimates of several research groups from 1 CE to the near present.<jupyter_code>table1.plot()
decorate(xlim=[0, 2000], xlabel='Year', 
         ylabel='World population (millions)',
         title='CE population estimates')<jupyter_output><empty_output><jupyter_text>See if you can find a model that fits these estimates.How well does your best model predict actual population growth from 1940 to the present?<jupyter_code>tables = read_html(filename, header=0, index_col=0, decimal='M')
table2 = tables[2]
table2.columns = ['census', 'prb', 'un', 'maddison', 
                  'hyde', 'tanton', 'biraben', 'mj', 
                  'thomlinson', 'durand', 'clark']
un = table2.un / 1e9
census = table2.census / 1e9
# Solution goes here
# Solution goes here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10767045454545454, 'fraction_numerical': 0.015625, 'mean_word_length': 5.220848056537102, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '381016', 'license': 'MIT', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1062, 'n_words': 308, 'path': 'chapters/chap10.ipynb', 'repo_name': 'maciejkos/ModSimPy', 'size': '3526'}