-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation_change.py
160 lines (117 loc) · 5.29 KB
/
population_change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'''This script loads WorldPop population projections and adjusts simulated annual population growth rate accordingly'''
from scipy.optimize import minimize
from scipy.interpolate import interp1d
import pandas as pd
import numpy as np
def objective(
x0: float,
growth_rate: np.ndarray,
proj_pop: float,
pop: list
) -> float:
'''This is the objective function used to optimize population growth to match the simulated population growth to the population growth projected in World population prospects
Args:
x0: initial parameter setting
growth_rate: initial department level growth rate
proj_pop: targetted projected population
pop: population currently residing in the country
Returns:
score: the score of the objective function
'''
adj_growth = growth_rate.copy()
adj_growth[growth_rate > 0] *= 1+x0
adj_growth[growth_rate < 0] *= 1-x0
adj_growth_sum = round(np.sum(adj_growth * pop))
score = (adj_growth_sum - proj_pop)**2
return score
def SSP_population_change(
initial_figures,
SSP_projections,
SSP,
iso3,
population,
admin_keys,
current_year
):
raise NotImplementedError('Only applied in paper 2')
# Filter dataset on iso3 code and SSP
data_filt = SSP_projections[SSP_projections['Region'] == iso3]
data_filt = data_filt[data_filt['Scenario'] == SSP]
years_in_dataframe = np.array([year for year in data_filt.columns if type(year) == int])
population_in_dataframe = np.array(data_filt[years_in_dataframe])[0]
ambient_change = []
if current_year >= np.min(years_in_dataframe):
interpolater = interp1d(x = years_in_dataframe, y = population_in_dataframe)
population_change = (interpolater(current_year+1) - interpolater(current_year)) * 1E6
else:
raise NotImplementedError('include historical change')
for region, pop in zip(admin_keys, population):
ambient_change.append(round(initial_figures.loc[region]['ambient_change'] * 0.01, 6))
growth_rate = np.array(ambient_change)
pop = population
obs_growth_sum = population_change
args = (growth_rate, obs_growth_sum, pop)
x0 = 0.2
# Optimize adjustion factor
res = minimize(objective, x0, method = 'nelder-mead', args = args)
factor = res.x
# Adjust growth figures
adj_growth = growth_rate.copy()
adj_growth[growth_rate > 0] *= 1 + factor
adj_growth[growth_rate < 0] *= 1 - factor
# Calculate pop change
simulated_population_change_scaled = np.int32(adj_growth * population)
# Export
population_change = pd.DataFrame({'change': simulated_population_change_scaled}, index=admin_keys)
return population_change
def WorldPopProspectsChange(
initial_figures: pd.core.frame.DataFrame,
HistWorldPopChange: pd.core.frame.DataFrame,
WorldPopChange: pd.core.frame.DataFrame,
population: list,
admin_keys: list,
year: int
) -> pd.core.frame.DataFrame:
'''This function is used to generate population growth and decline figures based on an optimization of departmental growth rate figures.
Args:
initial_figures: department level population growth rates.
HistWorldPopChange: global historical population figures.
WorldPopChange: global population projections.
population: list of population residingin in each node (coastal and inland)
admin_keys: admin names based on gadm dataset.
year: current year in the simulation timestep.
Returns:
population_change: pandas DataFrame showing the adjusted absolute natural population change in each node'''
# Read Insee net population change
initial_figures = pd.read_csv(r'DataDrive/POPULATION/ambient_population_change_gadm_2.csv', index_col='keys')
ambient_change = []
# Select historical observations or future projections
if year < 2020:
FRA_PopChange = HistWorldPopChange[HistWorldPopChange[HistWorldPopChange.columns[2]] == 'France']
FRA_change = (int(FRA_PopChange[str(year+1)]) - int(FRA_PopChange[str(year)])) * 1E3
elif year >= 2020:
FRA_PopChange = WorldPopChange[WorldPopChange[WorldPopChange.columns[2]] == 'France']
FRA_change = (int(FRA_PopChange[str(year+1)]) - int(FRA_PopChange[str(year)])) * 1E3
for region, pop in zip(admin_keys, population):
ambient_change.append(round(initial_figures.loc[region]['ambient_change'] * 0.01, 6))
# Initialize optimization
growth_rate = np.array(ambient_change)
pop = population
obs_growth_sum = FRA_change
args = (growth_rate, obs_growth_sum, pop)
x0 = 0.2
# Optimize adjustion factor
res = minimize(objective, x0, method = 'nelder-mead', args = args)
factor = res.x
# Adjust growth figures
adj_growth = growth_rate.copy()
adj_growth[growth_rate > 0] *= 1 + factor
adj_growth[growth_rate < 0] *= 1 - factor
# Calculate pop change
simulated_population_change_scaled = np.int32(adj_growth * population)
# Export
population_change = pd.DataFrame()
population_change['keys'] = admin_keys
population_change['change'] = simulated_population_change_scaled
population_change = population_change.set_index('keys')
return population_change