-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanApproach.py
527 lines (393 loc) · 16 KB
/
RomanApproach.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# -*- coding: utf-8 -*-
"""
Created on Fri May 10 14:27:39 2024
@author: regin
"""
#import pandas as pd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#%% Calcualte the Total factor productivity
TFP_2010 = 0.54
TFP_growthrate = 1.055
TFP_2050 = TFP_2010 * (TFP_growthrate**8)
TFP_change = TFP_2050/TFP_2010
Z_change = (1- TFP_2050) / (1 - TFP_2010)
current_pop = 16655799
project_pop = 20610000
Populationgrowth = project_pop / current_pop
#%%
path = r"C:/Industrial_ecology/Thesis/IOT_2011_ixi/"
outputpath = "C:/Industrial_ecology/Thesis/Circularinterventions/Code/Output/"
#"C:\Industrial_ecology\Thesis\Circularinterventions\Code\Input_circular_interventions\newZ.csv"
Anew = pd.read_csv(f"{outputpath}A_full_adjusted.csv", sep=',', header=[0, 2])
Y = pd.read_csv(f'{path}Y.txt' , sep='\t', index_col=[0, 1], header=[0, 1])
A = pd.read_csv(f'{path}A.txt', sep='\t', index_col=[0, 1], header=[0, 1])
Ynew = pd.read_csv(f'{outputpath}Y_full_adjusted.csv' , sep=',', header=[0,2])
#%%
# Import satellite accounts
F_sat = pd.read_csv(f'{path}satellite/F.txt' , sep='\t', index_col=[0], header=[0, 1])
F_sat_hh = pd.read_csv(f'{path}satellite/F_hh.txt' , sep='\t', index_col=[0], header=[0, 1])
# #%% Import impact accounts
# F_imp = pd.read_csv(f'{path}impacts/F.txt' , sep='\t', index_col=[0], header=[0, 1])
# F_imp_hh = pd.read_csv(f'{path}impacts/F_hh.txt' , sep='\t', index_col=[0], header=[0, 1])
#%% Make the Z matrix using the A and Y
I = np.identity(A.shape[0])
L = np.linalg.inv(I - A)
x_org = L @ Y.sum(axis=1)
Z_org = A @ (np.diag(x_org))
# Z_org.NL.loc["NL"]
#%%population growth is based on increasing the final demadn to match the growing population and multiply it with the final dea
Y.NL = Y.NL * Populationgrowth
x = L @ Y
Z = A@(np.diag(x.sum(axis = 1)))
Z.columns = Z.index
gross_output = Z.sum(axis = 1) + Y.sum(axis = 1)
Z_popgrowth = Z.copy()
#%% prepare necessary data
Va = gross_output - Z.sum(axis=1)
#%%Value added
Y_agg = Y.groupby(level=0, axis=1, sort=False).sum()
xdf = pd.DataFrame(x, index= Z.index, columns=["output orginal"])
Y_agg_org = Y_agg.copy()
#%% Apply changes in Final demand, Value added and Z
Va_nl = Va#.NL
Va_nl = Va_nl / TFP_change
Z_nl = Z#.NL
Z_nl = Z_nl * TFP_change
Y_agg_nl = Y_agg#.NL
Y_agg_nl = Y_agg_nl / TFP_change
#%%apply changes back into the full dataset
# Va.NL = Va_nl.values
# Z.NL= Z_nl
# Y_agg.NL = Y_agg_nl
Va = Va_nl.values
Z= Z_nl
Y_agg = Y_agg_nl
new_input = Z.sum(axis = 1) + Va
newY = new_input - Z.sum(axis = 0)
new_output = Z.sum(axis = 0) + Y_agg.sum(axis =1)
newY.loc["NL"].plot()
Y_agg_org.NL.loc["NL"].plot()
difY = newY - Y_agg.sum(axis = 1)
changeY = Y_agg.NL.copy()
changY = changeY - difY
Z_diff = Z_org.values - Z.values
Z_diff = pd.DataFrame(Z_diff, index=Z.index)
Z_diff.columns = Z.index
Z_diff_nl = Z_diff.NL
#%% Changed Input output
# x_new = Z.sum(axis=1) + Va
# xdf["new input"] = x_new
# print(xdf.loc["NL"])
# xdfnl = xdf.loc["NL"]
# print(xdf.sum(axis = 0))
# new_final = x - Va
# new_final = pd.DataFrame(new_final)
# xdfnl.plot()
# #%%final check
# #output
# #input
# new_input = Z.sum(axis = 1) + Va
# new_output = Z.sum(axis = 0) + Y_agg.sum(axis = 1)
resultsdf = pd.DataFrame()
resultsdf["output"] = new_output
resultsdf["input"] = new_input
print(resultsdf.sum(axis = 0))
resultsdf.plot()
print(new_input.sort_values())
print(new_output.sort_values())
print(resultsdf.sum(axis = 0))
#%%
resultsdf["popgrowth"] = gross_output
print(resultsdf.sum(axis = 0))
print(resultsdf.loc["NL"].sum(axis = 0))
resultsdf.loc["NL"].to_excel("wowow.xlsx")
resultsdf["org_output"] = x_org
resultsdf["difference_pop"] = resultsdf.popgrowth - resultsdf.org_output
resultsdf["difference_TFP"] = resultsdf.output - resultsdf.popgrowth
A_labels = A.index
A_labels = A_labels.to_frame(index=None)
sector_labels = A_labels.sector.drop_duplicates().reset_index(drop=True)
sector_labels = sector_labels.values
sector_labels = resultsdf.org_output.groupby(level=1, axis=0, sort=False).sum()
#%% plot the data
# Create a figure and subplots
fig, axs = plt.subplots(4, 1, sharex=True, sharey=True, figsize=(25, 22))
plt.rcParams.update({'font.size': 8}) # Reducing font size
# Plot the data on each subplot
resultsdf.org_output.loc["NL"].plot(ax=axs[0], label="Base gross output")
resultsdf.popgrowth.loc["NL"].plot(ax=axs[1], label="Population growth", color="red")
resultsdf.input.loc["NL"].plot(ax=axs[2], label="Input TFP", color="cyan")
resultsdf.output.loc["NL"].plot(ax=axs[2], label="Output TFP", color="magenta")
resultsdf.difference_pop.loc["NL"].plot(ax=axs[3], label="difference population growth", color="red")
resultsdf.difference_TFP.loc["NL"].plot(ax=axs[3], label="difference TFP output", color="green")
# Add legend to each subplot
axs[0].legend()
axs[1].legend()
axs[2].legend()
axs[3].legend()
for ax in axs:
ax.legend()
tickvalues = range(0,len(sector_labels))
for ax in axs:
ax.xaxis.set_tick_params(which='minor')
ax.tick_params(rotation=90)
# Adjust layout to prevent overlapping
plt.tight_layout(pad=3.0)
plt.xticks(range(0,len(sector_labels.index)), sector_labels.index)
# Show the plot
plt.show()
#%% plot the data 2
# Create a figure and subplots
fig, axs = plt.subplots(4, 1, sharex=True, sharey=True, figsize=(30, 22))
plt.rcParams.update({'font.size': 8}) # Reducing font size
# Plot the data on each subplot
resultsdf.org_output.groupby(level=0, axis=0, sort=False).sum().plot(ax=axs[0], label="Base gross output")
resultsdf.popgrowth.groupby(level=0, axis=0, sort=False).sum().plot(ax=axs[1], label="Population growth", color="red")
resultsdf.input.groupby(level=0, axis=0, sort=False).sum().plot(ax=axs[2], label="Input TFP", color="cyan")
resultsdf.output.groupby(level=0, axis=0, sort=False).sum().plot(ax=axs[2], label="Output TFP", color="magenta")
resultsdf.difference_pop.groupby(level=0, axis=0, sort=False).sum().plot(ax=axs[3], label="difference population growth", color="red")
resultsdf.difference_TFP.groupby(level=0, axis=0, sort=False).sum().plot(ax=axs[3], label="difference TFP output", color="green")
print(resultsdf.difference_pop.groupby(level=0, axis=0, sort=False).sum().sort_values(0))
print(resultsdf.difference_TFP.groupby(level=0, axis=0, sort=False).sum().sort_values(0))
# Add legend to each subplot
axs[0].legend()
axs[1].legend()
axs[2].legend()
axs[3].legend()
for ax in axs:
ax.legend()
for ax in axs:
ax.tick_params(axis='x', rotation=90)
ax.xaxis.set_tick_params(which='both')
A_labels = A.index
A_labels = A_labels.to_frame(index=None)
sector_labels = A_labels.sector.drop_duplicates().reset_index(drop=True)
# Adjust layout to prevent overlapping
plt.tight_layout(pad=3.0)
plt.xticks(sector_labels)
# Show the plot
plt.show()
#%%
#%%
Z_org.columns = Z.index
# Z_org.NL.loc["NL"].plot()
# Z_popgrowth.NL.loc["NL"].plot()
Zdiffpop = Z_popgrowth.NL.loc["NL"] - Z_org.NL.loc["NL"]
# Zdiffpop.sum(axis = 1).plot()
fig, axs = plt.subplots(2, 1, sharex=True, sharey=True, figsize=(30, 22))
plt.rcParams.update({'font.size': 20}) # Reducing font size
# Plot the data on each subplot
Zdiffpop.sum(axis = 0).plot(ax=axs[0], label="difference in output per industry")
Zdiffpop.sum(axis = 1).plot(ax=axs[1], label="difference in input per industry")
# Add legend to each subplot
axs[0].legend()
axs[1].legend()
tickvalues = range(0,len(sector_labels))
for ax in axs:
ax.xaxis.set_tick_params(which='minor')
ax.tick_params(rotation=90)
# Adjust layout to prevent overlapping
plt.tight_layout(pad=3.0)
plt.xticks(range(0,len(sector_labels.index)), sector_labels.index)
# Show the plot
plt.show()
#%%
fig, axs = plt.subplots(2, 1, sharex=True, sharey=True, figsize=(30, 22))
plt.rcParams.update({'font.size': 20}) # Reducing font size
# Plot the data on each subplot
Zdiffpop.sum(axis=0).plot(ax=axs[0], label="difference in output per industry")
Zdiffpop.sum(axis=1).plot(ax=axs[1], label="difference in input per industry")
# Add legend to each subplot
axs[0].legend()
axs[1].legend()
# Define the threshold
threshold = 700 # Example threshold
# Determine which sectors meet the threshold
output_values = Zdiffpop.sum(axis=0)
input_values = Zdiffpop.sum(axis=1)
# Set tick values and labels based on the threshold
tickvalues = range(0, len(sector_labels))
filtered_labels = [label if output_values[i] > threshold or input_values[i] > threshold else '' for i, label in enumerate(sector_labels.index)]
# Apply x-axis labels conditionally
for ax in axs:
ax.xaxis.set_tick_params(which='minor')
ax.tick_params(rotation=90)
ax.set_xticks(tickvalues)
ax.set_xticklabels(filtered_labels)
# Adjust layout to prevent overlapping
plt.tight_layout(pad=3.0)
# Show the plot
plt.show()
#%%
Zdiffpop.sum(axis=1).sort_values()
#%%
A_labels = A.index
A_labels = A_labels.to_frame(index=None)
region_labels = A_labels.region.drop_duplicates().reset_index(drop=True)
#%% plot the data 2
# Create a figure and subplots
fig, axs = plt.subplots(2, 1, sharex=True, sharey=False, figsize=(30, 22))
plt.rcParams.update({'font.size': 20}) # Reducing font size
# Plot the data on each subplot
resultsdf.org_output.groupby(level=0, axis=0, sort=False).sum().plot(kind='bar',ax=axs[0], label="Base gross output", color="#2F94A8")
resultsdf.popgrowth.groupby(level=0, axis=0, sort=False).sum().plot(kind='bar',ax=axs[0], label="Population growth", color="#AD1556")
resultsdf.difference_pop.groupby(level=0, axis=0, sort=False).sum().plot(kind='bar',ax=axs[1], label="difference population growth", color="#512B84")
# Add legend to each subplot
axs[0].legend()
axs[1].legend()
axs[1].set_xticks(range(len(region_labels)))
axs[1].set_xticklabels(region_labels, rotation=90)
for ax in axs:
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_ylabel("In million euro's")
ax.set_xlabel('Regions')
ax.tick_params(axis='x', rotation=0)
ax.xaxis.set_tick_params(which='both')
ax.grid(True) # Add grid lines
# Adjust layout to prevent overlapping
plt.tight_layout(pad=3.0)
plt.xticks(sector_labels)
# Show the plot
plt.show()
#%%
differencepop = resultsdf.popgrowth - resultsdf.org_output
#%% Make a graph that includes the below threshold values so it doesnt dissapear out of the system
threshold = 4000
# Filter the DataFrame to include only values above the threshold
filtered_df = differencepop[np.absolute(differencepop) > threshold].dropna()
# Calculate the sum of values below the threshold
below_threshold_sum = differencepop[np.absolute(differencepop) <= threshold].sum().sum()
# Add the below-threshold sum as a new row
filtered_df[('Below Threshold', 'Sum of below threshold')] = below_threshold_sum
# Choose a color palette (using Set1)
colors = plt.get_cmap('Set1').colors
# Plot the filtered DataFrame with adjusted size and legend placement
ax = filtered_df.unstack().plot(kind="bar", stacked=True, legend=False, figsize=(10, 6), color=colors)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.grid(True) # Add grid lines
# ax.set_title(f'Filtered difference in CBA (Steel - baseline) in {indicator}')
# ax.set_ylabel(f"{indicator} ({unit})")
ax.set_xlabel('Regions')
plt.tight_layout(pad=3.0)
# Show the plot
plt.show()
#%%import pandas as pd
# Create a figure and subplots
fig, axs = plt.subplots(2, 1, sharex=True, sharey=False, figsize=(30, 22))
plt.rcParams.update({'font.size': 25}) # Reducing font size
# Grouping and summing data for plotting
org_output = resultsdf.org_output.groupby(level=0, axis=0, sort=False).sum()
popgrowth = resultsdf.popgrowth.groupby(level=0, axis=0, sort=False).sum()
difference_pop = resultsdf.difference_pop.groupby(level=0, axis=0, sort=False).sum()
# Combine the data into DataFrames for side-by-side plotting
combined_df_1 = pd.DataFrame({'Gross output': org_output, 'Gross output 2050': popgrowth})
combined_df_2 = pd.DataFrame({'Delta gross output': difference_pop})
# Plot the data as bar graphs
combined_df_1.plot(kind='bar', ax=axs[0], color=["#2F94A8", "#AD1556"])
combined_df_2.plot(kind='bar', ax=axs[1], color=["#512B84"])
# Set titles for each subplot
axs[0].set_title('Gross output of 2011 and 2050 model')
axs[1].set_title('Difference due to population growth')
# Customize each subplot
for ax in axs:
ax.legend(loc='best')
ax.set_ylabel("Gross output (million euro's)")
ax.grid(True) # Add grid lines
# Set x-ticks and labels for both subplots
axs[1].set_xticks(range(len(region_labels)))
axs[1].set_xticklabels(region_labels, rotation=90)
axs[1].set_xlabel('Regions')
axs[0].set_xticks(range(len(region_labels)))
axs[0].set_xticklabels(region_labels, rotation=90)
# Adjust layout to prevent overlapping
plt.tight_layout(pad=4.0)
# Show the plot
plt.show()
#%%
fig, ax1 = plt.subplots(figsize=(30, 22))
plt.rcParams.update({'font.size': 25}) # Adjust font size
# Grouping and summing data for plotting
org_output = resultsdf.org_output.groupby(level=0, axis=0, sort=False).sum()
popgrowth = resultsdf.popgrowth.groupby(level=0, axis=0, sort=False).sum()
difference_pop = resultsdf.difference_pop.groupby(level=0, axis=0, sort=False).sum()
# Combine the data into DataFrames for side-by-side plotting
combined_df_1 = pd.DataFrame({'Gross output': org_output* 0.0001, 'Gross output 2050': popgrowth* 0.0001})
combined_df_2 = pd.DataFrame({'Delta gross output': difference_pop* 0.0001})
# Plot the first set of data
combined_df_1.plot(kind='bar', ax=ax1, color=["#2F94A8", "#AD1556"])
# Create a secondary y-axis
ax2 = ax1.twinx()
# Plot the second set of data on the secondary y-axis
combined_df_2.plot(kind='bar', ax=ax2, color=["#512B84"], position=1)
# Set titles and labels
ax1.set_title('Gross output of 2011 and 2050 model & Difference due to population growth')
ax1.set_ylabel("Gross output (million euro's)")
ax2.set_ylabel("Delta gross output (million euro's)")
# Customize the legends
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
# Add grid lines
ax1.grid(True)
ax2.grid(False)
# Set x-ticks and labels
region_labels = org_output.index # Assuming these are the region labels
ax1.set_xticks(range(len(region_labels)))
ax1.set_xticklabels(region_labels, rotation=90)
ax1.set_xlabel('Regions')
# Adjust layout to prevent overlapping
plt.tight_layout(pad=4.0)
# Show the plot
plt.show()
#%%
# Create a figure and subplots
fig, ax = plt.subplots(figsize=(30, 22))
plt.rcParams.update({'font.size': 25}) # Adjust font size
# Grouping and summing data for plotting
difference_pop = resultsdf.difference_pop.groupby(level=0, axis=0, sort=False).sum()
# Create a DataFrame for plotting
combined_df_2 = pd.DataFrame({'Delta gross output': difference_pop/1000})
# Plot the data as a bar graph
combined_df_2.plot(kind='bar', ax=ax, color=["#512B84"])
# Set titles and labels
ax.set_title('Difference due to population growth')
ax.set_ylabel("Delta gross output (million euro's)")
# Add grid lines
ax.grid(True)
# Set x-ticks and labels
region_labels = difference_pop.index # Assuming these are the region labels
ax.set_xticks(range(len(region_labels)))
ax.set_xticklabels(region_labels, rotation=90)
ax.set_xlabel('Regions')
# Adjust layout to prevent overlapping
plt.tight_layout(pad=4.0)
# Show the plot
plt.show()
#%%
# Create a figure and subplot
fig, ax = plt.subplots(figsize=(30, 22))
plt.rcParams.update({'font.size': 25}) # Adjust font size
# Grouping and summing data for plotting
org_output = resultsdf.org_output.groupby(level=0, axis=0, sort=False).sum()
popgrowth = resultsdf.popgrowth.groupby(level=0, axis=0, sort=False).sum()
# Create a DataFrame for plotting
combined_df_1 = pd.DataFrame({'Gross output': org_output/1000, 'Gross output 2050': popgrowth/1000})
# Plot the data as a bar graph
combined_df_1.plot(kind='bar', ax=ax, color=["#2F94A8", "#AD1556"])
# Set titles and labels
ax.set_title('Gross output of 2011 and 2050 model')
ax.set_ylabel("Gross output (billion euro's)")
# Add grid lines
ax.grid(True)
# Set x-ticks and labels
region_labels = org_output.index # Assuming these are the region labels
ax.set_xticks(range(len(region_labels)))
ax.set_xticklabels(region_labels, rotation=90)
ax.set_xlabel('Regions')
# Customize the legend
ax.legend(loc='best')
# Adjust layout to prevent overlapping
plt.tight_layout(pad=4.0)
# Show the plot
plt.show()