-
Notifications
You must be signed in to change notification settings - Fork 2
/
08-regional-concentration.py
151 lines (131 loc) · 4.69 KB
/
08-regional-concentration.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
import pandas as pd
import duckdb
mrio_versions = ['72', '62', '62c']
for version in mrio_versions:
ed, flows = f'ed-{version}.parquet', f'flows-{version}.parquet'
output = f'rci-{version}.parquet'
if version == '72':
index = 'mrio'
else:
index = 'mrio62'
grossexports = duckdb.sql(
f"""
SELECT t, s, r, sum(exports) AS exports
FROM 'data/{ed}'
WHERE breakdown='es'
GROUP BY t, s, r
ORDER BY t, s, r
"""
).df()
intlflows = duckdb.sql(
f"""
SELECT t, s, r, SUM(va_flows) AS flows
FROM 'data/{flows}'
WHERE s<>r
GROUP BY t, s, r
ORDER BY t, s, r
"""
).df()
countries = pd.read_excel('dicts/countries.xlsx')
rta = countries[[f'{index}', 'rta_asean', 'rta_carec', 'rta_carec10', 'rta_eaeu', 'rta_eu', 'rta_nafta', 'rta_safta']]
rta = rta.fillna(0)
def compute_rci(method):
if (method == 'gross exports'):
input, name, flow = 'grossexports', 'gross exports', 'exports'
else:
input, name, flow = 'intlflows', 'end-to-end', 'flows'
world = duckdb.sql(f'SELECT t, sum({flow}) AS world FROM {input} GROUP BY t').df()
exports = duckdb.sql(
f'''
SELECT t, rta, sum({flow}) AS exports
FROM (
SELECT *
FROM (
UNPIVOT (
SELECT * EXCLUDE (r, {index})
FROM (SELECT * FROM {input}) AS tbl
JOIN (SELECT * FROM rta) AS rtas ON tbl.s=rtas.{index}
) AS tbl_long
ON COLUMNS(* EXCLUDE (t, s, {flow}))
INTO NAME rta VALUE member
)
WHERE member=1
)
GROUP BY t, rta
'''
).df()
imports = duckdb.sql(
f"""
SELECT t, rta, sum({flow}) AS imports
FROM (
SELECT *
FROM (
UNPIVOT (
SELECT * EXCLUDE (r, {index})
FROM (SELECT * FROM {input}) AS tbl
JOIN (SELECT * FROM rta) AS rtas ON tbl.r=rtas.{index}
) AS tbl_long
ON COLUMNS(* EXCLUDE (t, s, {flow}))
INTO NAME rta VALUE member
)
WHERE member=1
)
GROUP BY t, rta
"""
).df()
within = duckdb.sql(
f"""
WITH aggregate1 AS (
SELECT * EXCLUDE (member)
FROM (
UNPIVOT (
SELECT * EXCLUDE ({index})
FROM (SELECT * FROM {input}) AS tbl
JOIN (SELECT * FROM rta) AS rtas ON tbl.s = rtas.{index}
) AS tbl_long
ON COLUMNS(* EXCLUDE (t, s, r, {flow}))
INTO NAME exporter VALUE member
)
WHERE member=1
),
aggregate AS (
SELECT * EXCLUDE (member)
FROM (
UNPIVOT (
SELECT * EXCLUDE ({index})
FROM (SELECT * FROM aggregate1) AS tbl
JOIN (SELECT * FROM rta) AS rtas ON tbl.r = rtas.{index}
) AS tbl_long
ON COLUMNS(* EXCLUDE (t, s, r, {flow}, exporter))
INTO NAME importer VALUE member
)
WHERE member=1
)
SELECT t, exporter AS rta, sum({flow}) AS within
FROM (SELECT * FROM aggregate WHERE exporter=importer)
GROUP BY t, exporter, importer
"""
).df()
df = duckdb.sql(
f"""
SELECT
t, '{name}' AS method, rta, within, exports, imports, world,
(2 * within) / (exports + imports) AS share_within,
(exports + imports) / (2 * world) AS share_world,
share_within / share_world AS rci
FROM (
SELECT * FROM within
JOIN exports ON within.t=exports.t AND within.rta=exports.rta
JOIN imports ON within.t=imports.t AND within.rta=imports.rta
JOIN world ON within.t=world.t
)
ORDER BY rta, t
"""
).df()
df['rta'] = df['rta'].str.split('_').str[-1]
return(df)
rci = pd.concat([
compute_rci(method='gross exports'),
compute_rci(method='end-to-end')
]).reset_index(drop=True)
rci.to_parquet(f'data/{output}', index=False)