Skip to content

Commit 5b452ec

Browse files
committed
Adding world population transition notebook
1 parent 7877410 commit 5b452ec

1 file changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Modeling and Simulation in Python\n",
8+
"\n",
9+
"Project 1 example\n",
10+
"\n",
11+
"Copyright 2018 Allen Downey\n",
12+
"\n",
13+
"License: [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0)\n"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"# Configure Jupyter so figures appear in the notebook\n",
23+
"%matplotlib inline\n",
24+
"\n",
25+
"# Configure Jupyter to display the assigned value after an assignment\n",
26+
"%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'\n",
27+
"\n",
28+
"# import functions from the modsim library\n",
29+
"from modsim import *"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"from pandas import read_html\n",
39+
"\n",
40+
"filename = 'data/World_population_estimates.html'\n",
41+
"tables = read_html(filename, header=0, index_col=0, decimal='M')\n",
42+
"table2 = tables[2]\n",
43+
"table2.columns = ['census', 'prb', 'un', 'maddison', \n",
44+
" 'hyde', 'tanton', 'biraben', 'mj', \n",
45+
" 'thomlinson', 'durand', 'clark']"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"def plot_results(census, un, timeseries, title):\n",
55+
" \"\"\"Plot the estimates and the model.\n",
56+
" \n",
57+
" census: TimeSeries of population estimates\n",
58+
" un: TimeSeries of population estimates\n",
59+
" timeseries: TimeSeries of simulation results\n",
60+
" title: string\n",
61+
" \"\"\"\n",
62+
" plot(census, ':', label='US Census')\n",
63+
" plot(un, '--', label='UN DESA')\n",
64+
" if len(timeseries):\n",
65+
" plot(timeseries, color='gray', label='model')\n",
66+
" \n",
67+
" decorate(xlabel='Year', \n",
68+
" ylabel='World population (billion)',\n",
69+
" title=title)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": null,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"un = table2.un / 1e9\n",
79+
"census = table2.census / 1e9\n",
80+
"empty = TimeSeries()\n",
81+
"plot_results(census, un, empty, 'World population estimates')"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"half = get_first_value(census) / 2"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"init = State(young=half, old=half)"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"system = System(birth_rate1 = 1/18,\n",
109+
" birth_rate2 = 1/25,\n",
110+
" mature_rate = 1/40,\n",
111+
" death_rate = 1/40,\n",
112+
" t_0 = 1950,\n",
113+
" t_end = 2016,\n",
114+
" transition_year = 1970,\n",
115+
" init=init)"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"def update_func1(state, t, system):\n",
125+
" births = system.birth_rate1 * state.young\n",
126+
" \n",
127+
" maturings = system.mature_rate * state.young\n",
128+
" deaths = system.death_rate * state.old\n",
129+
" \n",
130+
" young = state.young + births - maturings\n",
131+
" old = state.old + maturings - deaths\n",
132+
" \n",
133+
" return State(young=young, old=old)"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"state = update_func1(init, system.t_0, system)"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"state = update_func1(state, system.t_0, system)"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": [
160+
"def run_simulation(system, update_func):\n",
161+
" \"\"\"Simulate the system using any update function.\n",
162+
" \n",
163+
" init: initial State object\n",
164+
" system: System object\n",
165+
" update_func: function that computes the population next year\n",
166+
" \n",
167+
" returns: TimeSeries\n",
168+
" \"\"\"\n",
169+
" results = TimeSeries()\n",
170+
" \n",
171+
" state = system.init\n",
172+
" results[system.t_0] = state.young + state.old\n",
173+
" \n",
174+
" for t in linrange(system.t_0, system.t_end):\n",
175+
" state = update_func(state, t, system)\n",
176+
" results[t+1] = state.young + state.old\n",
177+
" \n",
178+
" return results"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": null,
184+
"metadata": {},
185+
"outputs": [],
186+
"source": [
187+
"results = run_simulation(system, update_func1);"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": null,
193+
"metadata": {},
194+
"outputs": [],
195+
"source": [
196+
"plot_results(census, un, results, 'World population estimates')"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"metadata": {},
203+
"outputs": [],
204+
"source": []
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": null,
209+
"metadata": {},
210+
"outputs": [],
211+
"source": []
212+
}
213+
],
214+
"metadata": {
215+
"kernelspec": {
216+
"display_name": "Python 3",
217+
"language": "python",
218+
"name": "python3"
219+
},
220+
"language_info": {
221+
"codemirror_mode": {
222+
"name": "ipython",
223+
"version": 3
224+
},
225+
"file_extension": ".py",
226+
"mimetype": "text/x-python",
227+
"name": "python",
228+
"nbconvert_exporter": "python",
229+
"pygments_lexer": "ipython3",
230+
"version": "3.6.6"
231+
}
232+
},
233+
"nbformat": 4,
234+
"nbformat_minor": 2
235+
}

0 commit comments

Comments
 (0)