Skip to content

Instantly share code, notes, and snippets.

@luispedro
Last active January 26, 2020 12:08
Show Gist options
  • Save luispedro/8b797b3563f213528289e82ad31af29c to your computer and use it in GitHub Desktop.
Save luispedro/8b797b3563f213528289e82ad31af29c to your computer and use it in GitHub Desktop.
Simulate population
import numpy as np
# FROM
# http://worldpopulationreview.com/countries/china-population/
pyramid0china = np.array(
[16446861., 16821572., 17097250., 17284418., 17390933., 17423080.,
17403550., 17341743., 17247061., 17129365., 16998517., 16861620.,
16727156., 16605907., 16498945., 16404588., 16387661., 16480191.,
16658788., 16890336., 17208221., 17504667., 17731393., 17957759.,
18174382., 18230709., 18872166., 20419719., 22447682., 24344339.,
26393956., 27309041., 26438650., 24441637., 22631937., 20751729.,
19327966., 18757173., 18781901., 18672355., 18483518., 18750087.,
19636105., 20905854., 22146113., 23423377., 24430011., 24965493.,
25141072., 25319552., 25489605., 25252984., 24467257., 23312424.,
22134346., 20891958., 19709826., 18694187., 17813256., 16857969.,
15784894., 15117366., 15067733., 15376031., 15629092., 15980514.,
15802845., 14764696., 13185556., 11711864., 10209011., 8923817.,
8042567., 7433678., 6768742., 6100779., 5519507., 5032721.,
4617640., 4246309., 3927467., 3590802., 3199414., 2782278.,
2406162., 2058786., 1738256., 1450657., 1193422., 929524.,
713716., 579453., 446889., 313169., 221644., 177575.,
142819., 104928., 63902., 37710., 67839.])
# There were 14.6m births in 2019
fr0china = 2*14.6e6/pyramid0china [20:40].mean()
# http://worldpopulationreview.com/countries/united-states-population/
pyramid0us = np.array([3931967., 3919500., 3919461., 3930158., 3903010., 3955644.,
4008192., 4059364., 4107872., 4156677., 4208742., 4241520.,
4245220., 4231306., 4220681., 4208740., 4210781., 4236404.,
4278618., 4316059., 4347272., 4397310., 4474657., 4565701.,
4651027., 4737732., 4788205., 4782769., 4739004., 4695388.,
4645691., 4592419., 4541165., 4490237., 4433909., 4375200.,
4315098., 4254149., 4194587., 4137614., 4082405., 4040406.,
4017264., 4008404., 4003094., 4002870., 4009404., 4022256.,
4040872., 4061465., 4080383., 4112964., 4165027., 4226569.,
4281521., 4332795., 4362769., 4360922., 4333852., 4300884.,
4260806., 4197638., 4106208., 3993650., 3871350., 3735929.,
3602786., 3480392., 3361570., 3234769., 3107225., 2956039.,
2770249., 2564001., 2361344., 2156197., 1973453., 1827535.,
1707062., 1586129., 1469802., 1357365., 1245835., 1137042.,
1035221., 938832., 849231., 767290., 691462., 616131.,
563171., 502660., 421119., 320109., 246668., 212578.,
178289., 135554., 84374., 52727., 89949.])
fr0us = 2*pyramid0us[0]/pyramid0us[20:40].mean()
def simul(pyramid0, fr, emigration = 0.0, change_fr = False, fr_target=1.2):
totals = []
pyramid = pyramid0.copy()
for _ in range(131):
totals.append(pyramid.sum())
pyramid[1:] = pyramid[:-1].copy()
pyramid[0] = pyramid[20:40].mean() * fr /2.
pyramid[25] *= 1 + emigration
# Simulate deaths
pyramid *= .999 # 1/1000 overall
pyramid[70:] *= .97
pyramid[80:] *= .95
pyramid[90:] *= .80
if change_fr and fr > fr_target:
fr *= .98
if change_fr and fr < fr_target:
fr *= 1.02
return np.array(totals)/1e6
print('''
# CHINA
SCENARIO 2050 2100 2120 2150
------------ ---- ----- ----- -----
FR = 1.4 {sc1[30]} {sc1[80]} {sc1[100]} {sc1[130]}
FR -> 1.2 {sc2[30]} {sc2[80]} {sc2[100]} {sc2[130]}
FR -> 1.0 {sc3[30]} {sc3[80]} {sc3[100]} {sc3[130]}
FR -> 0.9 {sc4[30]} {sc4[80]} {sc4[100]} {sc4[130]}
'''.format(
sc1=simul(pyramid0china, fr0china).astype(int),
sc2=simul(pyramid0china, fr0china, change_fr=True, fr_target=1.2).astype(int),
sc3=simul(pyramid0china, fr0china, change_fr=True, fr_target=1.0).astype(int),
sc4=simul(pyramid0china, fr0china, change_fr=True, fr_target=0.9).astype(int),
))
print('''
# US
SCENARIO 2050 2100 2120 2150
------------ ---- ----- ----- -----
FR = 1.8 {sc1[30]} {sc1[80]} {sc1[100]} {sc1[130]}
FR -> 1.9 {sc2[30]} {sc2[80]} {sc2[100]} {sc2[130]}
FR -> 2.0 {sc3[30]} {sc3[80]} {sc3[100]} {sc3[130]}
FR -> 2.2 {sc4[30]} {sc4[80]} {sc4[100]} {sc4[130]}
'''.format(
sc1=simul(pyramid0us, fr0us, emigration=.14).astype(int),
sc2=simul(pyramid0us, fr0us, emigration=.14, change_fr=True, fr_target=1.9).astype(int),
sc3=simul(pyramid0us, fr0us, emigration=.14, change_fr=True, fr_target=2.0).astype(int),
sc4=simul(pyramid0us, fr0us, emigration=.14, change_fr=True, fr_target=2.2).astype(int),
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment