This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SUTops.py
337 lines (251 loc) · 7.78 KB
/
SUTops.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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 2 12:05:08 2016
Description: Class to perform SUT and IOT transforming and balancing calculations
Scope: MSc research Modelling circular economy policies in EEIOA
@author:Franco Donati
@institution:Leiden University CML, TU Delft TPM
"""
import numpy as np
from numpy import linalg as ln
class SUTops:
def inv(x):
"""
Returns inverse by dividing by 1 and eliminating inf and nan values
"""
x = 1/x
x[x == np.inf ] = 0
x[x == np.nan ] = 0
return(x)
def var(self,V, U, Y, E):
"""
Returns variables that are useful in all calculations
"""
V = np.mat(V)
U = np.mat(U)
Y = np.mat(Y)
E = np.mat(E)
e = E[:9].sum(axis = 0).getA1()
yi = Y.sum(axis = 1).getA1()
yj = Y.sum(axis = 0).getA1()
q = V.sum(axis = 1).getA1()
g = V.sum(axis = 0).getA1()
diag_yi = np.diag(yi)
inv_diag_yi = self.inv(diag_yi)
diag_yj = np.diag(yj)
inv_diag_yj = self.inv(diag_yj)
diag_q = np.diag(q)
inv_diag_q = self.inv(diag_q)
diag_g = np.diag(g)
inv_diag_g = self.inv(diag_g)
p = {"e":e,
"yi":yi,
"yj":yj,
"q":q,
"g":g,
"diag_q":diag_q,
"diag_g":diag_g,
"diag_yi":diag_yi,
"diag_yj":diag_yj,
"inv_diag_q":inv_diag_q,
"inv_diag_g":inv_diag_g,
"inv_diag_yi":inv_diag_yi,
"inv_diag_yj":inv_diag_yj
}
return(p)
class TC_STA:
"""
Model with Transformation Coef.
ProdxProd Industry Technology assumption
"""
def T(inv_diag_g, V):
"""
Transformation matrix
T = inv(diag(g)) * V
"""
T = inv_diag_g @ V
return (T)
def L(U, T, inv_diag_q):
"""
Input coefficients intermediates
A = U * T * inv[diag (q)]
Multiplier matrix
L = (I-A)^-1
"""
A = U @ T @ inv_diag_q # technical coefficient matrix
I = np.identity(len(A))
IA = I - A
L = ln.inv(IA)
return(L)
def R(B, T, inv_diag_q):
"""
Value added and extension requirement matrix
R = E * inv(diag(g))
"""
BT = B @ T
R = BT @ inv_diag_q # Input coefficients
return (R)
def B(R, diag_q):
"""
Extensions and primary input for IO tables
"""
B = R @ diag_q
return (B)
def S(T, U):
"""
Intemediates
S = U * T
"""
S = U @ T
return(S)
class MSC_STA:
"""
Model with Market Share Coef.
Prod x Prod Industry Technology assumption
"""
def Z(U, inv_diag_g):
"""
Input requirements
Z = U * inv(diag(g))
"""
Z = U @ inv_diag_g
return(Z)
def D(V, inv_diag_q):
"""
Market share coefficients
D = V * inv(diag(q))
"""
D = V @ inv_diag_q
return(D)
def A(Z, D):
"""
Total requirement multipliers
A = Z * D
"""
A = Z @ D
return(A)
def L(A):
"""
Leontief inverse
L = (I-A)^-1
"""
I = np.identity(len(A))
IA = I - A
L= ln.inv(IA)
return(L)
def R(B, D, inv_diag_g):
"""
Input coefficients ext_matrix
R = E * inv(diag(g))
"""
R_ = B @ inv_diag_g
R = R_ @ D
return (R)
def B(R, diag_q):
"""
Extensions and primary input for IO tables
"""
B = R @ diag_q
return (B)
def S(Z, D, diag_q):
"""
Intermediates
S = Z * D * diag(q)
"""
S = Z @ D @ diag_q
return (S)
class IOT:
"""
General IOT operations subclass
some methods repeat from other subclasses
but it's good to have them divided for clarity
"""
def q(S,Y):
"""
total product output s the sum of Si and y
"""
q = np.sum(S, axis = 1) + np.sum(Y, axis = 1)
return(q)
def R(B, inv_diag_q):
"""
Primary input and intermediates extensions coefficient matrix
"""
R = B @ inv_diag_q
return(R)
def B(R, diag_q):
"""
Primary input and intermediates extensions matrix
"""
B = R @ diag_q
return(B)
def q_IAy(L, y):
"""
Total product ouput
q = inv(I - A) * yi
"""
q = np.dot(L, y)
return (q)
def S(A, diag_q):
"""
Total product ouput
S = A * diag_q
"""
S = A @ diag_q
return (S)
def A(S, inv_diag_q):
"""
Technical coefficient matrix
A = S * inv(diag(q))
"""
A = S @ inv_diag_q
return(A)
def L(A):
"""
Leontief inverse
L = (I-A)^-1
"""
I = np.identity(len(A))
IA = I - A
L = ln.inv(IA)
return(L)
class fdext:
def RYB(inv_diag_yj, YB):
"""
Method for transformation matrix of YB
(e.g. final demand emissions)
RB = YB * inv(diag(yj))
"""
YRB = YB @ inv_diag_yj
return (YRB)
def YB(YRB, diag_yj):
"""
Extensions and primary input for IO tables
"""
YB = YRB @ diag_yj
return (YB)
class difference:
"""
This class is used to calculate the difference between scenarios
"""
def delta_Y(Y, Yalt):
"""
method to calculate difference in Y
Y = final demand baseline
Yalt = final demand scenario
"""
delta_Y = Y - Yalt
return (delta_Y)
def delta_q(L, Lalt, y):
"""
method to calculate difference in q
L = Leontief of baseline
Lalt = Leontief of scenario
"""
delta_q = (L-Lalt) @ y
return (delta_q)
def verifyIOT(S, Y, E):
q1 = np.sum(S, axis = 1) + np.sum(Y, axis = 1)
q2 = np.sum(S, axis = 0) + np.sum(E[:9], axis = 0)
ver = q1/q2 * 100
ver = ver.fillna(0)
return(ver)