-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpfunctions.py
More file actions
322 lines (207 loc) · 8.69 KB
/
Copy pathhelpfunctions.py
File metadata and controls
322 lines (207 loc) · 8.69 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
import pandas as pd
import numpy as np
import scipy.linalg as la
import networkx as nx
import matplotlib.pyplot as plt
import math
import readline
for i in range(readline.get_current_history_length()):
print (readline.get_history_item(i + 1))
"""
Part 1: Loading matrices, and turning them from nominal to real
"""
# drop the ones
# check simulated matrices
def table_eigenvec(eigenvec_result):
df_eigenvec = pd.DataFrame(eigenvec_result.items())
df_eigenvec.columns = ['sector', 'value']
return df_eigenvec
def remove_isolated_sectors(df):
remove_rows = []
for index, row in df.iterrows():
n_non_zero_row = np.count_nonzero(row)
if n_non_zero_row <= 1:
remove_rows.append(index)
df_rows_removed = df.drop(remove_rows, axis = 1).drop(remove_rows, axis = 0)
remove_columns = []
for col in df_rows_removed:
n_non_zero_col = np.count_nonzero(df_rows_removed[col])
if n_non_zero_col <= 1:
remove_columns.append(col)
df_cols_removed = df_rows_removed.drop(remove_columns, axis = 1).drop(remove_columns, axis = 0)
return df_cols_removed
def load_matrix(matrix_type, year):
"""
Parameters
----------
matrix_type : string
'real' or 'nominal'.
year : int
2015-2019.
Returns
-------
matrix_sectors : dataframe
80x80, all sectors.
"""
# define sheet name
sheet_name = str(year) + '_' + matrix_type
# gather data
df_sectors_2015 = pd.read_excel('Data/Input_Output_2015_2019_Nominal_Real_updated.xlsx',
sheet_name=sheet_name,
index_col=0)
# get relevant rows and columns
matrix_sectors = df_sectors_2015.iloc[0:80,0:80]
return matrix_sectors
def calc_price_change_matrix(nominal_matrix, previous_year_price_matrix):
"""
Parameters
----------
nominal_matrix : dataframe
matrix with nominal input-output.
previous_year_price_matrix : dataframe
matrix, same year as 'nominal_matrix', but in previous year prices.
Returns
-------
dataframe
matrix with the price changes between years.
"""
# calc the price change, fill in nan's with zero
price_change = (nominal_matrix - previous_year_price_matrix)/(previous_year_price_matrix)
price_change_formatted = price_change.replace(np.nan,0)
return price_change_formatted + 1
def calc_real_matrices(l_nominal_matrices, l_previous_year_prices_matrices):
"""
Parameters
----------
l_nominal_matrices : list
each element is a dataframe with the input-output matrix in nominal prices.
l_previous_year_prices_matrices : list
each element is a dataframe with the input-output matrix in prices of the previous year.
Returns
-------
real_matrices : list
all the real matrices for the years.
"""
# get n of matrices, list of real matrices
n_matrices = len(l_nominal_matrices)
real_matrices = []
# loop over each
for i in range(0,n_matrices):
# for first, use as base year
if i == 0:
real_matrix = l_nominal_matrices[i]
real_matrices.append(real_matrix)
else:
# calculate for each year the price change
for j in range(0, i ):
previous_year_price_matrix = l_previous_year_prices_matrices[j]
nominal_matrix = l_nominal_matrices[j+1]
price_change_matrix = calc_price_change_matrix(nominal_matrix, previous_year_price_matrix)
# if multiple years, combine the price changes
if i==1:
price_change_matrix_years = price_change_matrix
else:
price_change_matrix_years = price_change_matrix_years * price_change_matrix
# divide the nominal prices by the price change factor, get real matrix
current_nominal_matrix = l_nominal_matrices[i]
real_matrix = current_nominal_matrix.div(price_change_matrix_years)
real_matrices.append(real_matrix)
return real_matrices
"""
Part 2:functions for normalizing the matrix, and calculating the eigenvector centrality
"""
def turn_df_to_networkx(df_sectors):
networkx_sectors = nx.from_pandas_adjacency(df_sectors, nx.DiGraph)
return networkx_sectors
def calc_eigenvec_centrality_sectors(df_sectors, type_centrality, weight='weight'):
# turn df to networkx
networkx_sectors = turn_df_to_networkx(df_sectors)
# by default, python does right vector eigencentrality
if type_centrality == 'left':
networkx_sectors = networkx_sectors.reverse()
# use networkx function to calculate the eigenvector centrality
eigenvector_centrality_sectors = nx.eigenvector_centrality_numpy(networkx_sectors, weight = weight)
return eigenvector_centrality_sectors
def normalize_matrix_rows(df_sectors):
# get row sums
rowSums_sectors = df_sectors.sum(axis=1)
# divide by row sum
df_sectors_normalized = df_sectors.div(rowSums_sectors, axis=0)
df_sectors_normalized= df_sectors_normalized.replace(np.nan,0)
return df_sectors_normalized
"""
Part 3:functions for simulating matrices
"""
def simulate_matrices(base_matrix, sd, n_matrices, p_0):
"""
Parameters
----------
base_matrix : dataframe
dataframe as given - values taken as mean.
sd : float
millions for the sd
Returns
-------
l_simulated_matrices.
list of dataframes, each a simulatedmatrix
"""
# list - each a numpy array of the simulated values for a cell
l_simulated_values = []
# list - each a dataframe
l_simulated_matrices = []
# get number of entries
n_entries = base_matrix.shape[0]
# get name of indeces
indeces = base_matrix.index
# loop over each cell
for rowIndex, row in base_matrix.iterrows(): #iterate over rows
for columnIndex, mean_value in row.items():
sd_simulation = sd
simulated_values_cell = []
for i in range(0, n_matrices):
simulated_value_cell = np.random.normal(mean_value, sd_simulation, None)
if mean_value == 0:
biased_coin_flip = np.random.binomial(1,p_0,None)
if biased_coin_flip ==0:
simulated_value_cell = 0
# accept reject
while simulated_value_cell < 0:
simulated_value_cell = np.random.normal(mean_value, sd_simulation, None)
simulated_values_cell.append(simulated_value_cell)
# simulate values of this cell
l_simulated_values.append(simulated_values_cell)
df_simulations = pd.DataFrame.from_records(l_simulated_values)
# create matrix based on the simulated values
for i in range(0, n_matrices):
simulation_result = df_simulations.iloc[:,i].values.reshape(n_entries,n_entries)
simulated_matrix = pd.DataFrame(simulation_result)
simulated_matrix.index = indeces
simulated_matrix.columns = indeces
l_simulated_matrices.append(simulated_matrix)
return l_simulated_matrices
def get_CI_eigenvector_centrality(l_simulated_matrices, type_centrality, normalize = True):
"""
Parameters
----------
l_simulated_matrices : list
list of simulated matrices from 'simualte matrices'
Returns
-------
df_CI_eigenvector_centrality : dataframe
mean, CI per eigenvector centrality.
"""
l_eigenvec_results = []
for df_matrix in l_simulated_matrices:
if normalize:
df_matrix = normalize_matrix_rows(df_matrix)
eigenvec_centrality = calc_eigenvec_centrality_sectors(df_matrix, type_centrality)
l_eigenvec_results.append(eigenvec_centrality)
df_eigenvec_results = pd.DataFrame(l_eigenvec_results).transpose()
avg = df_eigenvec_results.mean(axis=1)
std = df_eigenvec_results.std(axis=1)
lower_CI = avg - (std*1.96)
upper_CI = avg + (std*1.96)
lower_CI = lower_CI.apply(lambda x: x if x>0 else 0)
df_CI_eigenvector_centrality = pd.concat([avg, lower_CI, upper_CI], axis=1)
df_CI_eigenvector_centrality.columns = ['avg', 'lower_CI', 'upper_CI']
return df_CI_eigenvector_centrality