-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcalibration.py
More file actions
452 lines (359 loc) · 17.1 KB
/
calibration.py
File metadata and controls
452 lines (359 loc) · 17.1 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# -*- coding: utf-8 -*-
"""Policy Priority Inference for Sustainable Development - Estimation & Calibration Code
Authors: Omar A. Guerrero & Gonzalo Castañeda
Written in Pyhton 3.7
Acknowledgments: This product was developed through the sponsorship of the
United Nations Development Programme (bureau for Latin America)
and with the support of the National Laboratory for Public Policies (Mexico City),
the Centro de Investigación y Docencia Económica (CIDE, Mexico City),
and The Alan Turing Institute (London).
This file contains all the necesary functions to estimate the growth factors
of the model and to calibrate the number of periods for convergence. The accompanying
data can be obtained from the public repository: https://github.com/oguerrer/PPI4SD.
The two main functions are:
calibrate: finds the optimal number of periods for convergence in terms of
matching the total volatility of the indicators' changes
estimation: given the number of periods for convergence, this function
estimates the growth factors of the model
There are additional support functions that are explained below.c
Example
-------
To run PPI in a Python script, just add the following line:
steps, alphas = estimation(I0, T, A, R, phi, tau, vola_emp, steps, initial_alphas)
This returns the growth factors in the vector 'alphas'.
Rquired external libraries
--------------------------
- Numpy
- Scipy
- joblib: the joblib library takes care of the parallel processing. It installation
is straightforward and the instructions can be found in its
Pypi site: https://pypi.org/project/joblib/
"""
# import necessary libraries
from __future__ import division, print_function
import numpy as np
import copy
from joblib import Parallel, delayed
import scipy.optimize as opt
# the model_final.py file should be in the same folder
from ppi import *
def run_model(I0, T, A, R, phi, tau, alphas, sampleSize):
"""Runs the model for a given number of times and returns a matrix with the
convergence times of each indicator in each simulation.
Parameters
----------
I0: numpy array
Initial values of the development indicators.
T: numpy array
Target values for development indicators. These values represent
the government's goals or aspirations. For a retrospective analysis,
it is usually assumed that the targets correspond to the final values
of the series. They should be higher than I0 or the model will not
converge.
A: 2D numpy array
The adjacency matrix of the spillover network of development
indicators. If not given, the model assumes a zero-matrix, so there
are no spillovers.
alpha: float, optional
A vector of growth factors in (0,1).
R: numpy array, optional
Binary vector indicating which nodes are instrumental (value 1) and
which are not (value 0). If not provided, it is assumed that all
nodes are instrumental (a vector of ones).
phi: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the government's monitoring mechanisms.
tau: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the rule of law.
alphas: numpy array
A vector with the growth factors. Each element shuold be in (0,1).
sampleSize: int
The number of simulations to be performed.
Returns
-------
all_times: numpy array
A matrix with the convergence times of each indicator in each
simulation. The rows correspond to the indicators and the columns
to the simulations.
"""
all_times = []
for intera in range(sampleSize):
outputs = run_ppi(I0, T, A=A, alpha=alphas, R=R, phi=phi, tau=tau, tolerance=1e-3)
tsI, tsC, tsF, tsP, tsD, tsS, times, H = outputs
all_times.append(times)
all_times = np.array(all_times).T
return all_times
def fobj(alpha, node, alphaStar, steps, sampleSize, I0, T, A, R, phi, tau):
"""A wrapper around the 'run_model' function that will be used to perform
the greedy search.
Parameters
----------
alpha: float
The value of the growth factor to be changed. Shuold be in (0,1).
node: int
The index (from 0 to N-1) of the node to which the growth factor
'alpha' corresponds.
alphaStar: numpy array
A vector with the growth factors. Each element shuold be in (0,1).
steps: int
Number of steps to which the simulation should converge. It is used
to evaluate convergence time errors.
sampleSize: integer
The number of simulations to be performed.
I0: numpy array
Initial values of the development indicators.
T: numpy array
Target values for development indicators. These values represent
the government's goals or aspirations. For a retrospective analysis,
it is usually assumed that the targets correspond to the final values
of the series. They should be higher than I0 or the model will not
converge.
A: 2D numpy array
The adjacency matrix of the spillover network of development
indicators. If not given, the model assumes a zero-matrix, so there
are no spillovers.
alpha: float, optional
A vector of growth factors in (0,1).
R: numpy array, optional
Binary vector indicating which nodes are instrumental (value 1) and
which are not (value 0). If not provided, it is assumed that all
nodes are instrumental (a vector of ones).
phi: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the government's monitoring mechanisms.
tau: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the rule of law.
Returns
-------
errors: numpy array
The squared differences between 'steps' and the convergence time of
each indicator.
"""
alphas = copy.deepcopy(alphaStar)
alphas[node] = alpha
all_times = run_model(I0, T, A, R, phi, tau, alphas, sampleSize)
errors = (all_times.mean(axis=1)[node] - steps)**2
return errors
def func(I0, T, A, R, phi, tau, node, alphas, steps, sampleSize):
"""Greedy search of a growth factor for indicator 'node' that minimizes
the difference between its convergence time and 'steps'.
Parameters
----------
I0: numpy array
Initial values of the development indicators.
T: numpy array
Target values for development indicators. These values represent
the government's goals or aspirations. For a retrospective analysis,
it is usually assumed that the targets correspond to the final values
of the series. They should be higher than I0 or the model will not
converge.
A: 2D numpy array
The adjacency matrix of the spillover network of development
indicators. If not given, the model assumes a zero-matrix, so there
are no spillovers.
alpha: float, optional
A vector of growth factors in (0,1).
R: numpy array, optional
Binary vector indicating which nodes are instrumental (value 1) and
which are not (value 0). If not provided, it is assumed that all
nodes are instrumental (a vector of ones).
phi: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the government's monitoring mechanisms.
tau: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the rule of law.
node: int
The index (from 0 to N-1) of the node to which the searched
growth factor corresponds.
alphas: numpy array
A vector with the growth factors. Each element shuold be in (0,1).
steps: int
Number of steps to which the simulation should converge. It is used
to evaluate convergence time errors.
sampleSize: integer
The number of simulations to be performed.
Returns
-------
best_alpha: float
The growth factor that minimizes the difference between the convergence
time of 'node' and 'steps', keeping everything else constant.
"""
sol = opt.minimize_scalar(fobj, args=(node, alphas, steps, sampleSize, I0, T, A, R, phi, tau), bounds=[.01, .99], method='Bounded')
best_alpha = sol.x
return best_alpha
def aver_dev(mean_times, steps):
"""Computes the average mean difference between the average convergence times
and 'steps'.
Parameters
----------
mean_times: numpy array
A vector with the mean convergence time of each indicator.
steps: integer
Number of steps to which the simulation should converge.
Returns
-------
aver_error: float
The average difference between the mean convergence times and 'steps'.
"""
aver_error = np.mean(np.abs(mean_times - steps))
return aver_error
def estimate(I0, T, A, R, phi, tau,
steps, parallel_processes=4, sample_size=1000, alphas=None,
dev_lim=.8):
"""Estimates the growth factors for a given number of periods to convergence.
Parameters
----------
I0: numpy array
Initial values of the development indicators.
T: numpy array
Target values for development indicators. These values represent
the government's goals or aspirations. For a retrospective analysis,
it is usually assumed that the targets correspond to the final values
of the series. They should be higher than I0 or the model will not
converge.
A: 2D numpy array
The adjacency matrix of the spillover network of development
indicators. If not given, the model assumes a zero-matrix, so there
are no spillovers.
R: numpy array, optional
Binary vector indicating which nodes are instrumental (value 1) and
which are not (value 0). If not provided, it is assumed that all
nodes are instrumental (a vector of ones).
phi: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the government's monitoring mechanisms.
tau: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the rule of law.
steps: int
Number of steps to which the simulation should converge. It is used
to evaluate convergence time errors.
parallel_processes: float, optional
Number of processes to be ran in parallel.
sample_size: int, optional
The number of simulations to be ran for each estimation.
alphas: numpy array, optional
Initial values for the growth factors.
dev_lim: float, optional
Tolerance threshold for the difference between the mean convergence
times and 'calib_steps'.
Returns
-------
alphas: list
A list with numpy arrays. Each array contains the growth factors
estimated for each 'calib_steps'.
vola_sim: list
A list with the different 'steps' iterated in the function.
"""
N = len(R)
if alphas is None:
est_alphas = np.ones(N)*.5
else:
est_alphas = copy.deepcopy(alphas)
print('Number of ticks to convergence:', steps)
all_times = run_model(I0, T, A, R, phi, tau, est_alphas, sample_size)
mean_devs = aver_dev(all_times.mean(axis=1), steps)
keep_looking = True
counter = 1
while keep_looking:
print('Running iteration', counter, '...')
above_std = np.where(np.abs(all_times.mean(axis=1)-steps) > dev_lim)[0]
sol = Parallel(n_jobs=parallel_processes, verbose=0)(delayed(func)(I0, T, A, R, phi, tau,
node, est_alphas, steps, sample_size) for node in above_std)
est_alphas[above_std] = sol
all_times = run_model(I0, T, A, R, phi, tau, est_alphas, sample_size)
mean_devs = aver_dev(all_times.mean(axis=1), steps)
if mean_devs < dev_lim:
keep_looking = False
out_times = []
all_tsI = []
for intera in range(sample_size):
outputs = run_ppi(I0, T, A=A, alpha=est_alphas, R=R, phi=phi, tau=tau)
tsI, tsC, tsF, tsP, tsD, times, tsS, H = outputs
out_times.append(times)
all_tsI.append(tsI)
nchs_sim_dist = []
for ts in all_tsI:
chs_sim = ts[:,1:]-ts[:,0:-1]
nchs_sim_dist += chs_sim.flatten().tolist()
est_vola = np.std(nchs_sim_dist)
counter += 1
print('Obtained a mean average convergence time error of', mean_devs)
return est_alphas, est_vola
def find_steps(I0, T, A, R, phi, tau, vola_emp, alphas=None,
parallel_processes=4, sample_size=10, dev_lim=3, steps=10):
"""Iterates over the number of 'steps' to convergence until the total volatility
of the synthetic indicators is lower than the empirical one.
Parameters
----------
I0: numpy array
Initial values of the development indicators.
T: numpy array
Target values for development indicators. These values represent
the government's goals or aspirations. For a retrospective analysis,
it is usually assumed that the targets correspond to the final values
of the series. They should be higher than I0 or the model will not
converge.
A: 2D numpy array
The adjacency matrix of the spillover network of development
indicators. If not given, the model assumes a zero-matrix, so there
are no spillovers.
R: numpy array, optional
Binary vector indicating which nodes are instrumental (value 1) and
which are not (value 0). If not provided, it is assumed that all
nodes are instrumental (a vector of ones).
phi: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the government's monitoring mechanisms.
tau: float, optional
Scalar in [0,1] or numpy array (a vector) with values in [0,1] that
represent the quality of the rule of law.
vola_emp: float
The total standard deviation of the changes of the empirical
indicators.
parallel_processes: float, optional
Number of processes to be ran in parallel.
sample_size: int, optional
The number of simulations to be ran for each estimation.
dev_lim: float, optional
Tolerance threshold for the difference between the mean convergence
times and 'calib_steps'.
steps: int, optional
The minimum number of simulation steps for convergence.
Returns
-------
rec_alphas: list
A list with numpy arrays. Each array contains the growth factors
estimated for each 'calib_steps'.
rec_volas: list
A list with with the total standard deviation of the simulated
indicators. Each standard deviation corresponds to each 'calib_steps'.
rec_steps: list
A list with the different 'calib_steps' iterated in the function.
"""
N = len(R)
if alphas is None:
alphas = np.ones(N)*.5
else:
est_alphas = copy.deepcopy(alphas)
cont_T = True
rec_alphas = []
rec_volas = []
rec_steps = []
while cont_T:
est_alphas, est_vola = estimate(I0=I0, T=T, A=A, R=R, phi=phi, tau=tau,
steps=steps,
parallel_processes=parallel_processes,
sample_size=sample_size, alphas=alphas,
dev_lim=dev_lim,)
rec_alphas.append(est_alphas)
rec_volas.append(est_vola)
rec_steps.append(steps)
steps += 1
print('Difference in volatility:', est_vola - vola_emp)
if est_vola < vola_emp:
cont_T = False
return rec_alphas, rec_volas, rec_steps