Repository navigation
Expand file tree
/
Copy pathiobalance.py
More file actions
349 lines (286 loc) · 8.87 KB
/
Copy pathiobalance.py
File metadata and controls
349 lines (286 loc) · 8.87 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
"""
## GAMSSOURCE: https://www.gams.com/latest/gamslib_ml/libhtml/gamslib_iobalance.html
## LICENSETYPE: Demo
## MODELTYPE: LP, NLP, QCP
## KEYWORDS: linear programming, nonlinear programming, quadratic constraints, statistics, RAS approach
Updating and Projecting Coefficients: The RAS Approach (IOBALANCE)
The RAS procedure (named after Richard A. Stone) is an iterative procedure to
update matrices. This numerical example is taken from chapter 7.4.2 of Miller
and Blair. Several additional optimization formulations will be applied to
this toy problem.
Miller R E, and Blair P D, Input-Output Analysis: Foundations and Extensions,
Cambridge University Press, New York, 2009.
"""
from __future__ import annotations
import numpy as np
import gamspy.math as gams_math
from gamspy import (
Alias,
Card,
Container,
Equation,
Model,
Parameter,
Problem,
Sense,
Set,
Smax,
Sum,
Variable,
)
def main():
m = Container()
# Sets
i = Set(m, name="i", records=list(range(1, 4)))
j = Alias(m, name="j", alias_with=i)
# Parameters
a0 = Parameter(
m,
name="a0",
domain=[i, j],
records=np.array(
[
[0.120, 0.100, 0.049],
[0.210, 0.247, 0.265],
[0.026, 0.249, 0.145],
]
),
description="known base matrix",
)
z1 = Parameter(
m,
name="z1",
domain=[i, j],
records=np.array(
[
[98, 72, 75],
[65, 8, 63],
[88, 27, 44],
]
),
description="unknown industry flows",
)
x = Parameter(
m,
name="x",
domain=j,
records=np.array([421, 284, 283]),
description="observed total output",
)
u = Parameter(m, name="u", domain=i, description="observed row totals")
v = Parameter(m, name="v", domain=j, description="observed column totals")
a1 = Parameter(m, name="ai", domain=[i, j], description="unknown matrix A")
u[i] = Sum(j, z1[i, j])
v[j] = Sum(i, z1[i, j])
a1[i, j] = z1[i, j] / x[j]
print("Values of u \n", u.records)
print("Values of v \n", v.records)
print("Values of a1 \n", a1.records)
# --- 1: RAS updating
r = Parameter(m, name="r", domain=i, description="row adjustment")
s = Parameter(m, name="s", domain=j, description="column adjustment")
r[i] = 1
s[j] = 1
oldr = Parameter(m, name="oldr", domain=i)
olds = Parameter(m, name="olds", domain=j)
maxdelta = Parameter(m, name="maxdelta")
maxdelta[...] = 1
while True:
oldr[i] = r[i]
olds[j] = s[j]
r[i] = r[i] * u[i] / Sum(j, r[i] * a0[i, j] * x[j] * s[j])
s[j] = s[j] * v[j] / Sum(i, r[i] * a0[i, j] * x[j] * s[j])
maxdelta[...] = gams_math.Max(
Smax(i, gams_math.abs(oldr[i] - r[i])),
Smax(j, gams_math.abs(olds[j] - s[j])),
)
print("In loop maxdelta: ", round(maxdelta.records.values[0][0], 3))
if maxdelta.records.values[0][0] < 0.005:
break
# Parameter
report = Parameter(
m, name="report", domain=["*", i, j], description="summary report"
)
# option report:3:1:2
report["A0", i, j] = a0[i, j]
report["A1", i, j] = a1[i, j]
report["RAS", i, j] = r[i] * a0[i, j] * s[j]
# --- 2: Entropy formulation a*ln(a/a0)
# The RAS procedure gives the solution to the Entropy formulation
# Variable
obj = Variable(m, name="obj", type="free", description="objective value")
a = Variable(
m,
name="a",
type="positive",
domain=[i, j],
description="estimated A matrix",
)
# Equation
rowbal = Equation(m, name="rowbal", domain=i, description="row totals")
colbal = Equation(m, name="colbal", domain=j, description="column totals")
defobjent = Equation(m, name="defobjent", description="entropy definition")
rowbal[i] = Sum(j, a[i, j] * x[j]) == u[i]
colbal[j] = Sum(i, a[i, j] * x[j]) == v[j]
defobjent[...] = obj == Sum(
[i, j], x[j] * a[i, j] * gams_math.log(a[i, j] / a0[i, j])
)
mEntropy = Model(
m,
name="mEntropy",
equations=[rowbal, colbal, defobjent],
problem=Problem.NLP,
sense=Sense.MIN,
objective=obj,
)
# we need to exclude small values to avoid domain violations
a.lo[i, j] = 1e-5
mEntropy.solve()
report["Entropy", i, j] = a.l[i, j]
# --- 3: Entropy with flow variable
# we can balance the flow matrix instead of the A matrix
zv = Variable(
m, name="zv", type="free", domain=[i, j], description="industry flows"
)
rowbalz = Equation(m, name="rowbalz", domain=i, description="row totals")
colbalz = Equation(
m, name="colbalz", domain=j, description="column totals tive"
)
defobjentz = Equation(
m, name="defobjentz", description="entropy objective using flows"
)
rowbalz[i] = Sum(j, zv[i, j]) == u[i]
colbalz[j] = Sum(i, zv[i, j]) == v[j]
zbar = Parameter(
m, name="zbar", domain=[i, j], description="reference flow"
)
zbar[i, j] = a0[i, j] * x[j]
zv.lo[i, j] = 1
defobjentz[...] = obj == Sum(
[i, j], zv[i, j] * gams_math.log(zv[i, j] / zbar[i, j])
)
mEntropyz = Model(
m,
name="mEntropyz",
equations=[rowbalz, colbalz, defobjentz],
problem=Problem.NLP,
sense=Sense.MIN,
objective=obj,
)
mEntropyz.solve()
report["EntropyZ", i, j] = zv.l[i, j] / x[j]
# --- 4. absolute deviation formulations result in LPs
# MAD Mean Absolute Deviations
# MAPE Mean absolute percentage error
# Linf Infinity norm
# Positive Variable
ap = Variable(
m,
name="ap",
type="positive",
domain=[i, j],
description="positive deviation iation",
)
an = Variable(
m,
name="an",
type="positive",
domain=[i, j],
description="negative deviation",
)
amax = Variable(
m, name="amax", type="positive", description="maximum absilute dev"
)
# Equation
defabs = Equation(
m, name="defabs", domain=[i, j], description="absolute definition"
)
defmaxp = Equation(
m, name="defmaxp", domain=[i, j], description="max positive"
)
defmaxn = Equation(
m, name="defmaxn", domain=[i, j], description="max neagtive"
)
defmad = Equation(m, name="defmad", description="MAD definition")
defmade = Equation(
m, name="defmade", description="mean absolute percentage error"
)
deflinf = Equation(m, name="deflinf", description="infinity norm")
defabs[i, j] = a[i, j] - a0[i, j] == ap[i, j] - an[i, j]
defmaxp[i, j] = a[i, j] - a0[i, j] <= amax
defmaxn[i, j] = a[i, j] - a0[i, j] >= -amax
defmad[...] = obj == 1 / gams_math.power(Card(i), 2) * Sum(
[i, j], ap[i, j] + an[i, j]
)
defmade[...] = obj == 100 / gams_math.power(Card(i), 2) * Sum(
[i, j], (ap[i, j] + an[i, j]) / a0[i, j]
)
deflinf[...] = obj == amax
# Model
mMAD = Model(
m,
name="mMAD",
equations=[rowbal, colbal, defabs, defmad],
problem="LP",
sense=Sense.MIN,
objective=obj,
)
mMADE = Model(
m,
name="mMADE",
equations=[rowbal, colbal, defabs, defmade],
problem="LP",
sense=Sense.MIN,
objective=obj,
)
mLinf = Model(
m,
name="mLinf",
equations=[rowbal, colbal, defmaxp, defmaxn, deflinf],
problem="LP",
sense=Sense.MIN,
objective=obj,
)
mMAD.solve()
report["MAD", i, j] = a.l[i, j]
mMADE.solve()
report["MADE", i, j] = a.l[i, j]
mLinf.solve()
report["Linf", i, j] = a.l[i, j]
# --- 5. Squared Deviations can be solved with powerful QP codes
# SD squared deviations
# RSD relative squared deviations
# Equation
defsd = Equation(m, name="defsd")
defrsd = Equation(m, name="defrsd")
defsd[...] = obj == Sum([i, j], gams_math.power(a[i, j] + a0[i, j], 2))
defrsd[...] = obj == Sum(
[i, j], gams_math.power(a[i, j] + a0[i, j], 2) / a0[i, j]
)
# Model
mSD = Model(
m,
name="mSD",
equations=[rowbal, colbal, defsd],
problem=Problem.QCP,
sense=Sense.MIN,
objective=obj,
)
mRSD = Model(
m,
name="mRSD",
equations=[rowbal, colbal, defrsd],
problem=Problem.QCP,
sense=Sense.MIN,
objective=obj,
)
mSD.solve()
report["SD", i, j] = a.l[i, j]
mRSD.solve()
report["RSD", i, j] = a.l[i, j]
import math
assert math.isclose(mRSD.objective_value, 7.6203, rel_tol=0.001)
print()
print("\t SOLUTION REPORT: \n", report.pivot())
if __name__ == "__main__":
main()