deabook 0.0.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deabook/CNLSSDFDDF.py +490 -0
- deabook/CNLSSDFDDFweak.py +543 -0
- deabook/CNLSSDFDDFweakmeta.py +567 -0
- deabook/CQERDFDDF.py +1897 -0
- deabook/CQERDFDDFweak.py +1840 -0
- deabook/DDFDUAL.py +38 -0
- deabook/DDFfrontier.py +189 -0
- deabook/DEA.py +1696 -0
- deabook/DEAmb.py +552 -0
- deabook/DEAweak.py +2982 -0
- deabook/DRFDUAL.py +35 -0
- deabook/MB.py +344 -0
- deabook/SBM.py +489 -0
- deabook/StoNED.py +730 -0
- deabook/__init__.py +104 -0
- deabook/cli.py +19 -0
- deabook/common.py +30 -0
- deabook/constant.py +200 -0
- deabook/core.py +903 -0
- deabook/data/41Firm.csv +61 -0
- deabook/data/Ex3.dta +0 -0
- deabook/data/Ex4.dta +0 -0
- deabook/data/Ex5.dta +0 -0
- deabook/data/Lin-ee2015.xlsx +0 -0
- deabook/data/aaa.xls +0 -0
- deabook/data/abatementCost.csv +169 -0
- deabook/data/china data.xlsx +0 -0
- deabook/data/dea_data.csv +5 -0
- deabook/data/dea_data.dta +0 -0
- deabook/data/dea_data.xlsx +0 -0
- deabook/data/electricityFirms.csv +90 -0
- deabook/data/oecd data.xlsx +0 -0
- deabook/data/power plant data.xlsx +0 -0
- deabook/data/province_data.dta +0 -0
- deabook/data/riceProduction.csv +345 -0
- deabook/data/us_electric_plants.csv +20 -0
- deabook/deabook.py +150 -0
- deabook/dual.py +314 -0
- deabook/example/README.md +24 -0
- deabook/example/chap10/chap 10.ipynb +24933 -0
- deabook/example/chap11/chap11.ipynb +3076 -0
- deabook/example/chap12/chap12.ipynb +3419 -0
- deabook/example/chap13/chap13.ipynb +3062 -0
- deabook/example/chap14/chap14.ipynb +705 -0
- deabook/example/chap15/chap15.ipynb +903 -0
- deabook/example/chap2/chap 2.ipynb +1427 -0
- deabook/example/chap3/chap 3.ipynb +2059 -0
- deabook/example/chap4/chap 4.ipynb +3875 -0
- deabook/example/chap5/chap 5.ipynb +2427 -0
- deabook/example/chap6/chap 6.ipynb +2658 -0
- deabook/example/chap7/chap 7.ipynb +15973 -0
- deabook/example/chap8/chap 8.ipynb +1130 -0
- deabook/example/chap9/chap 9.ipynb +2091 -0
- deabook/mqcore.py +370 -0
- deabook/panel.py +393 -0
- deabook/productivity.py +123 -0
- deabook/utils/CNLSDDFG1.py +3 -0
- deabook/utils/CNLSDDFG2.py +1 -0
- deabook/utils/CNLSDDFZG1.py +1 -0
- deabook/utils/CNLSDDFZG2.py +1 -0
- deabook/utils/CQERDDFG1.py +12 -0
- deabook/utils/CQERDDFG2.py +12 -0
- deabook/utils/CQERDDFZG1.py +9 -0
- deabook/utils/CQERDDFZG2.py +9 -0
- deabook/utils/CQERG1.py +12 -0
- deabook/utils/CQERG2.py +12 -0
- deabook/utils/CQERZG1.py +9 -0
- deabook/utils/CQERZG2.py +9 -0
- deabook/utils/CQRDDFweakG1.py +18 -0
- deabook/utils/CQRDDFweakG2.py +14 -0
- deabook/utils/CQRDDFweakZG1.py +9 -0
- deabook/utils/CQRDDFweakZG2.py +9 -0
- deabook/utils/CQRweakG1.py +18 -0
- deabook/utils/CQRweakG2.py +14 -0
- deabook/utils/CQRweakZG1.py +9 -0
- deabook/utils/CQRweakZG2.py +9 -0
- deabook/utils/MB_.py +394 -0
- deabook/utils/MB_extensions.py +211 -0
- deabook/utils/__init__.py +5 -0
- deabook/utils/_directional_efficiency.py +53 -0
- deabook/utils/_g_common.py +108 -0
- deabook/utils/interpolation.py +15 -0
- deabook/utils/sweet.py +59 -0
- deabook/utils/tools.py +2788 -0
- deabook/utils/unihyper.py +36 -0
- deabook/utils/weakCNLSDDFZG1.py +32 -0
- deabook/utils/weakCNLSDDFZG2.py +1 -0
- deabook-0.0.12.dist-info/METADATA +64 -0
- deabook-0.0.12.dist-info/RECORD +93 -0
- deabook-0.0.12.dist-info/WHEEL +5 -0
- deabook-0.0.12.dist-info/entry_points.txt +2 -0
- deabook-0.0.12.dist-info/licenses/LICENSE +22 -0
- deabook-0.0.12.dist-info/top_level.txt +1 -0
deabook/CNLSSDFDDF.py
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
# import dependencies
|
|
2
|
+
from pyomo.environ import ConcreteModel, Set, Var, Objective, minimize, Constraint, log
|
|
3
|
+
from pyomo.core.expr.numvalue import NumericValue
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
|
|
7
|
+
from .constant import CET_ADDI, CET_MULT, FUN_PROD, FUN_COST, OPT_DEFAULT, RTS_CRS, RTS_VRS1, OPT_LOCAL
|
|
8
|
+
from .utils import tools
|
|
9
|
+
from .core import CNLSResultMixin
|
|
10
|
+
|
|
11
|
+
class CNLSSD(CNLSResultMixin):
|
|
12
|
+
"""Convex Nonparametric Least Square for Shephard distance function(CNLSSD)
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, data, sent, z=None, gy=[1], gx=[0], cet=CET_MULT, fun=FUN_PROD, rts=RTS_VRS1):
|
|
16
|
+
"""CNLS model
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
# y (float): output variable.
|
|
20
|
+
# x (float): input variables.
|
|
21
|
+
data
|
|
22
|
+
sent
|
|
23
|
+
z (float, optional): Contextual variable(s). Defaults to None.
|
|
24
|
+
gy (list, optional): output distance vector. Defaults to [1].
|
|
25
|
+
gx (list, optional): input distance vector. Defaults to [0].
|
|
26
|
+
cet (String, optional): CET_ADDI (additive composite error term) or CET_MULT (multiplicative composite error term). Defaults to CET_ADDI.
|
|
27
|
+
fun (String, optional): FUN_PROD (production frontier) or FUN_COST (cost frontier). Defaults to FUN_PROD.
|
|
28
|
+
rts (String, optional): RTS_VRS (variable returns to scale) or RTS_CRS (constant returns to scale). Defaults to RTS_VRS.
|
|
29
|
+
"""
|
|
30
|
+
# TODO(error/warning handling): Check the configuration of the model exist
|
|
31
|
+
self.y, self.x, self.z, self.gy, self.gx, self.basexy= tools.assert_CNLSSD(data, sent, z, gy, gx) ## 注意,这里的数据已经除以x1,或者y1了。
|
|
32
|
+
|
|
33
|
+
self.cet = cet
|
|
34
|
+
self.fun = fun
|
|
35
|
+
self.rts = rts
|
|
36
|
+
|
|
37
|
+
# Initialize the CNLS model
|
|
38
|
+
self.__model__ = ConcreteModel()
|
|
39
|
+
|
|
40
|
+
if type(self.z) != type(None):
|
|
41
|
+
# Initialize the set of z
|
|
42
|
+
self.__model__.M = Set(initialize=range(len(self.z[0])))
|
|
43
|
+
|
|
44
|
+
# Initialize the variables for z variable
|
|
45
|
+
self.__model__.omega = Var(self.__model__.M, doc='z coefficient')
|
|
46
|
+
|
|
47
|
+
# Initialize the sets
|
|
48
|
+
self.__model__.I = Set(initialize=range(len(self.y)))
|
|
49
|
+
self.__model__.J = Set(initialize=range(len(self.x[0])))
|
|
50
|
+
self.__model__.K = Set(initialize=range(len(self.y[0])))
|
|
51
|
+
|
|
52
|
+
# Initialize the variables
|
|
53
|
+
self.__model__.delta = Var(self.__model__.I,self.__model__.J,bounds=(0.0, None),doc='delta')
|
|
54
|
+
self.__model__.gamma = Var(self.__model__.I,self.__model__.K,bounds=(0.0, None),doc='gamma')
|
|
55
|
+
self.__model__.epsilon = Var(self.__model__.I, doc='residual')
|
|
56
|
+
self.__model__.frontier = Var(self.__model__.I,
|
|
57
|
+
bounds=( 0.0, None),
|
|
58
|
+
doc='estimated frontier')
|
|
59
|
+
if self.rts == RTS_VRS1:
|
|
60
|
+
self.__model__.alpha = Var(self.__model__.I, doc='alpha')
|
|
61
|
+
|
|
62
|
+
# Setup the objective function and constraints
|
|
63
|
+
self.__model__.objective = Objective(rule=self.__objective_rule(),sense=minimize,
|
|
64
|
+
doc='objective function')
|
|
65
|
+
self.__model__.regression_rule = Constraint(self.__model__.I,rule=self.__regression_rule(),
|
|
66
|
+
doc='regression equation')
|
|
67
|
+
if self.cet == CET_MULT:
|
|
68
|
+
self.__model__.log_rule = Constraint(self.__model__.I, rule=self.__log_rule(),
|
|
69
|
+
doc='log-transformed regression equation')
|
|
70
|
+
|
|
71
|
+
self.__model__.afriat_rule = Constraint(self.__model__.I,self.__model__.I, rule=self.__afriat_rule(),
|
|
72
|
+
doc='afriat inequality')
|
|
73
|
+
|
|
74
|
+
self.__model__.translation_rule = Constraint(self.__model__.I,
|
|
75
|
+
rule=self.__translation_property(),
|
|
76
|
+
doc='translation property')
|
|
77
|
+
# self.__model__.log_rule.pprint()
|
|
78
|
+
|
|
79
|
+
# Optimize model
|
|
80
|
+
self.optimization_status = 0
|
|
81
|
+
self.problem_status = 0
|
|
82
|
+
|
|
83
|
+
def optimize(self, email=OPT_LOCAL, solver=OPT_DEFAULT):
|
|
84
|
+
"""Optimize the function by requested method
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
email (string): The email address for remote optimization. It will optimize locally if OPT_LOCAL is given.
|
|
88
|
+
solver (string): The solver chosen for optimization. It will optimize with default solver if OPT_DEFAULT is given.
|
|
89
|
+
"""
|
|
90
|
+
self._optimize_cnls_model(email=email, solver=solver, cet=self.cet)
|
|
91
|
+
|
|
92
|
+
def __objective_rule(self):
|
|
93
|
+
"""Return the proper objective function"""
|
|
94
|
+
|
|
95
|
+
def objective_rule(model):
|
|
96
|
+
return sum(model.epsilon[i] ** 2 for i in model.I)
|
|
97
|
+
|
|
98
|
+
return objective_rule
|
|
99
|
+
|
|
100
|
+
def __regression_rule(self):
|
|
101
|
+
"""Return the proper regression constraint"""
|
|
102
|
+
if self.cet == CET_MULT:
|
|
103
|
+
if sum(self.gx) >= 1 and sum(self.gy) ==0:
|
|
104
|
+
if type(self.z) != type(None):
|
|
105
|
+
def regression_rule(model, i):
|
|
106
|
+
return - log(self.basexy[i]) == log(model.frontier[i] +1) \
|
|
107
|
+
+ sum(model.omega[m] * self.z[i][m] for m in model.M) - model.epsilon[i]
|
|
108
|
+
return regression_rule
|
|
109
|
+
|
|
110
|
+
def regression_rule(model, i):
|
|
111
|
+
return - log(self.basexy[i]) == log(model.frontier[i]+1 ) - model.epsilon[i]
|
|
112
|
+
return regression_rule
|
|
113
|
+
elif sum(self.gy) >= 1 and sum(self.gx) ==0:
|
|
114
|
+
if type(self.z) != type(None):
|
|
115
|
+
def regression_rule(model, i):
|
|
116
|
+
return - log(self.basexy[i]) == log(model.frontier[i] +1) \
|
|
117
|
+
+ sum(model.omega[m] * self.z[i][m] for m in model.M) + model.epsilon[i]
|
|
118
|
+
return regression_rule
|
|
119
|
+
|
|
120
|
+
def regression_rule(model, i):
|
|
121
|
+
return - log(self.basexy[i]) == log(model.frontier[i]+1 ) - model.epsilon[i]
|
|
122
|
+
return regression_rule
|
|
123
|
+
raise ValueError("Undefined model parameters.")
|
|
124
|
+
|
|
125
|
+
def __log_rule(self):
|
|
126
|
+
"""Return the proper log constraint"""
|
|
127
|
+
if self.cet == CET_MULT:
|
|
128
|
+
if self.rts == RTS_VRS1:
|
|
129
|
+
def log_rule(model, i):
|
|
130
|
+
return model.frontier[i] == -1 + model.alpha[i] +\
|
|
131
|
+
sum(model.delta[i, j] * self.x[i][j] for j in model.J) -\
|
|
132
|
+
sum(model.gamma[i, k] * self.y[i][k] for k in model.K)
|
|
133
|
+
return log_rule
|
|
134
|
+
|
|
135
|
+
elif self.rts == RTS_CRS:
|
|
136
|
+
def log_rule(model, i):
|
|
137
|
+
return model.frontier[i] == -1 + \
|
|
138
|
+
sum(model.delta[i, j] * self.x[i][j] for j in model.J) -\
|
|
139
|
+
sum(model.gamma[i, k] * self.y[i][k] for k in model.K)
|
|
140
|
+
return log_rule
|
|
141
|
+
|
|
142
|
+
raise ValueError("Undefined model parameters.")
|
|
143
|
+
|
|
144
|
+
def __afriat_rule(self):
|
|
145
|
+
"""Return the proper afriat inequality constraint"""
|
|
146
|
+
if self.fun == FUN_PROD:
|
|
147
|
+
__operator = NumericValue.__le__
|
|
148
|
+
elif self.fun == FUN_COST:
|
|
149
|
+
__operator = NumericValue.__ge__
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
if self.cet == CET_MULT:
|
|
153
|
+
if self.rts == RTS_VRS1:
|
|
154
|
+
|
|
155
|
+
def afriat_rule(model, i, h):
|
|
156
|
+
if i == h:
|
|
157
|
+
return Constraint.Skip
|
|
158
|
+
return __operator(
|
|
159
|
+
(model.frontier[i]+1) ,
|
|
160
|
+
model.alpha[h] + sum(model.delta[h, j] * self.x[i][j]for j in model.J) - \
|
|
161
|
+
sum(model.gamma[h, k] * self.y[i][k]for k in model.K) )
|
|
162
|
+
|
|
163
|
+
return afriat_rule
|
|
164
|
+
elif self.rts == RTS_CRS:
|
|
165
|
+
|
|
166
|
+
def afriat_rule(model, i, h):
|
|
167
|
+
if i == h:
|
|
168
|
+
return Constraint.Skip
|
|
169
|
+
return __operator(
|
|
170
|
+
(model.frontier[i]+1),
|
|
171
|
+
sum(model.delta[h, j] * self.x[i][j]for j in model.J) - \
|
|
172
|
+
sum(model.gamma[h, k] * self.y[i][k]for k in model.K) )
|
|
173
|
+
|
|
174
|
+
return afriat_rule
|
|
175
|
+
|
|
176
|
+
raise ValueError("Undefined model parameters.")
|
|
177
|
+
|
|
178
|
+
def __translation_property(self):
|
|
179
|
+
"""Return the proper translation property
|
|
180
|
+
|
|
181
|
+
Following Chambers, Chung, Färe (1996) and Kuosmanen & Johnson (2017,
|
|
182
|
+
EJOR 262(2):792-801), the translation property normalizes the
|
|
183
|
+
coefficients against the direction vector only — not against data
|
|
184
|
+
values. See also the pyStoNED reference implementation
|
|
185
|
+
(ds2010/pyStoNED, CNLSDDF.__translation_property).
|
|
186
|
+
"""
|
|
187
|
+
def translation_rule(model, i):
|
|
188
|
+
return sum(model.delta[i, j] * self.gx[j] for j in model.J) \
|
|
189
|
+
+ sum(model.gamma[i, k] * self.gy[k] for k in model.K) == 1
|
|
190
|
+
|
|
191
|
+
return translation_rule
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def get_residual(self):
|
|
204
|
+
"""Return residual value by array"""
|
|
205
|
+
tools.assert_optimized(self.optimization_status)
|
|
206
|
+
residual = list( self._single_model().epsilon[:].value)
|
|
207
|
+
if sum(self.gx) >= 1 and sum(self.gy) ==0:
|
|
208
|
+
residual = [+1*v for v in residual]
|
|
209
|
+
elif sum(self.gy) >= 1 and sum(self.gx) ==0:
|
|
210
|
+
residual = [1*v for v in residual]
|
|
211
|
+
# print("aaasss#,",list(self._single_model().epsilon[:].value))
|
|
212
|
+
return np.asarray(residual)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def get_frontier(self):
|
|
216
|
+
"""Return estimated frontier value by array"""
|
|
217
|
+
tools.assert_optimized(self.optimization_status)
|
|
218
|
+
if self.cet == CET_MULT :
|
|
219
|
+
frontier_values = [v.value for v in self._single_model().frontier[:]]
|
|
220
|
+
frontier = [v + 1 for v in frontier_values]
|
|
221
|
+
return frontier
|
|
222
|
+
raise ValueError("CET_ADDI can not be employed")
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# def get_predict(self, x_test):
|
|
228
|
+
# """Return the estimated function in testing sample"""
|
|
229
|
+
# tools.assert_optimized(self.optimization_status)
|
|
230
|
+
# return interpolation.interpolation(self.get_alpha(), self.get_delta(), x_test, fun=self.fun)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class CNLSSDG(CNLSSD):
|
|
235
|
+
"""Sparse-G variant of :class:`CNLSSD`.
|
|
236
|
+
|
|
237
|
+
This class keeps the original CNLSSD Shephard-distance model, residual
|
|
238
|
+
decomposition, translation property and solver path unchanged, but replaces
|
|
239
|
+
the full pairwise Afriat constraints with the Sweet-spot sparse constraint
|
|
240
|
+
set used by the package's G variants.
|
|
241
|
+
"""
|
|
242
|
+
|
|
243
|
+
def __init__(self, data, sent, z=None, gy=[1], gx=[0],
|
|
244
|
+
cet=CET_MULT, fun=FUN_PROD, rts=RTS_VRS1):
|
|
245
|
+
from .utils import sweet
|
|
246
|
+
from .utils._g_common import cg_pairs, replace_constraint
|
|
247
|
+
|
|
248
|
+
super().__init__(data, sent, z=z, gy=gy, gx=gx,
|
|
249
|
+
cet=cet, fun=fun, rts=rts)
|
|
250
|
+
|
|
251
|
+
self.cutactive = sweet.sweet(self.x)
|
|
252
|
+
self.active = np.zeros((len(self.x), len(self.x)))
|
|
253
|
+
self.active2 = np.zeros((len(self.x), len(self.x)))
|
|
254
|
+
|
|
255
|
+
replace_constraint(
|
|
256
|
+
self,
|
|
257
|
+
'afriat_rule',
|
|
258
|
+
cg_pairs(self.cutactive),
|
|
259
|
+
self._CNLSSD__afriat_rule(),
|
|
260
|
+
'active afriat inequality'
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
def optimize(self, email=OPT_LOCAL, solver=OPT_DEFAULT):
|
|
264
|
+
"""Optimize the sparse CNLSSD-G model."""
|
|
265
|
+
self._optimize_cnls_model(email=email, solver=solver, cet=self.cet)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
class CNLSDDF(CNLSSD):
|
|
269
|
+
"""Convex Nonparametric Least Square with directional distance function
|
|
270
|
+
"""
|
|
271
|
+
|
|
272
|
+
def __init__(self, data, sent, z=None, gy=[1], gx=[1], fun=FUN_PROD, rts=RTS_VRS1):
|
|
273
|
+
"""CNLS DDF model
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
# y (float): output variable.
|
|
277
|
+
# x (float): input variables.
|
|
278
|
+
data
|
|
279
|
+
sent
|
|
280
|
+
z (float, optional): Contextual variable(s). Defaults to None.
|
|
281
|
+
gy (list, optional): output directional vector. Defaults to [1].
|
|
282
|
+
gx (list, optional): input directional vector. Defaults to [1].
|
|
283
|
+
fun (String, optional): FUN_PROD (production frontier) or FUN_COST (cost frontier). Defaults to FUN_PROD.
|
|
284
|
+
rts (String, optional): RTS_VRS (variable returns to scale) or RTS_CRS (constant returns to scale). Defaults to RTS_VRS.
|
|
285
|
+
"""
|
|
286
|
+
# TODO(error/warning handling): Check the configuration of the model exist
|
|
287
|
+
self.y, self.x, self.z, self.gy, self.gx, self.basexy = tools.assert_CNLSDDF(data, sent, z, gy, gx)
|
|
288
|
+
for direction in (self.gx, self.gy):
|
|
289
|
+
if not np.isfinite(direction).all() or np.any(np.asarray(direction) < 0):
|
|
290
|
+
raise ValueError("DDF directions must be finite and nonnegative.")
|
|
291
|
+
self.fun = fun
|
|
292
|
+
self.rts = rts
|
|
293
|
+
self.cet = CET_ADDI
|
|
294
|
+
self.distance_type = "directional"
|
|
295
|
+
|
|
296
|
+
# self.data = data
|
|
297
|
+
# self.sent = sent
|
|
298
|
+
# self.z = z
|
|
299
|
+
# self.gy = gy
|
|
300
|
+
# self.gx = gx
|
|
301
|
+
|
|
302
|
+
self.__model__ = ConcreteModel()
|
|
303
|
+
|
|
304
|
+
# Initialize the sets
|
|
305
|
+
self.__model__.I = Set(initialize=range(len(self.y)))
|
|
306
|
+
self.__model__.J = Set(initialize=range(len(self.x[0])))
|
|
307
|
+
self.__model__.K = Set(initialize=range(len(self.y[0])))
|
|
308
|
+
|
|
309
|
+
# Initialize the variables
|
|
310
|
+
if self.rts == RTS_VRS1:
|
|
311
|
+
self.__model__.alpha = Var(self.__model__.I, doc='alpha')
|
|
312
|
+
self.__model__.delta = Var(
|
|
313
|
+
self.__model__.I, self.__model__.J, bounds=(0.0, None), doc='delta')
|
|
314
|
+
self.__model__.epsilon = Var(self.__model__.I, doc='residuals')
|
|
315
|
+
self.__model__.gamma = Var(
|
|
316
|
+
self.__model__.I, self.__model__.K, bounds=(0.0, None), doc='gamma')
|
|
317
|
+
|
|
318
|
+
if type(self.z) != type(None):
|
|
319
|
+
# Initialize the set of z
|
|
320
|
+
self.__model__.M = Set(initialize=range(len(self.z[0])))
|
|
321
|
+
# Initialize the variables for z variable
|
|
322
|
+
self.__model__.omega = Var(self.__model__.M, doc='z coefficient')
|
|
323
|
+
|
|
324
|
+
# Setup the objective function and constraints
|
|
325
|
+
self.__model__.objective = Objective(rule=self._CNLSSD__objective_rule(),
|
|
326
|
+
sense=minimize,
|
|
327
|
+
doc='objective function')
|
|
328
|
+
self.__model__.regression_rule = Constraint(self.__model__.I,
|
|
329
|
+
rule=self.__regression_rule(),
|
|
330
|
+
doc='regression equation')
|
|
331
|
+
self.__model__.afriat_rule = Constraint(self.__model__.I,
|
|
332
|
+
self.__model__.I,
|
|
333
|
+
rule=self.__afriat_rule(),
|
|
334
|
+
doc='afriat inequality')
|
|
335
|
+
|
|
336
|
+
self.__model__.translation_rule = Constraint(self.__model__.I,
|
|
337
|
+
rule=self.__translation_property(),
|
|
338
|
+
doc='translation property')
|
|
339
|
+
# self.__model__.translation_rule.pprint()
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
# Optimize model
|
|
343
|
+
self.optimization_status = 0
|
|
344
|
+
self.problem_status = 0
|
|
345
|
+
|
|
346
|
+
def optimize(self, email=OPT_LOCAL, solver=OPT_DEFAULT):
|
|
347
|
+
"""Optimize the function by requested method
|
|
348
|
+
|
|
349
|
+
Args:
|
|
350
|
+
email (string): The email address for remote optimization. It will optimize locally if OPT_LOCAL is given.
|
|
351
|
+
solver (string): The solver chosen for optimization. It will optimize with default solver if OPT_DEFAULT is given.
|
|
352
|
+
"""
|
|
353
|
+
self._optimize_cnls_model(email=email, solver=solver, cet=CET_ADDI)
|
|
354
|
+
|
|
355
|
+
def __regression_rule(self):
|
|
356
|
+
"""Return the proper regression constraint"""
|
|
357
|
+
if self.rts == RTS_VRS1:
|
|
358
|
+
if type(self.z) != type(None):
|
|
359
|
+
def regression_rule(model, i):
|
|
360
|
+
return model.epsilon[i] + self.basexy[i] \
|
|
361
|
+
== model.alpha[i] \
|
|
362
|
+
+ sum(model.delta[i, j] * self.x[i][j] for j in model.J) \
|
|
363
|
+
- sum(model.gamma[i, k] * self.y[i][k] for k in model.K) \
|
|
364
|
+
- sum(model.omega[m] * self.z[i][m] for m in model.M)
|
|
365
|
+
|
|
366
|
+
return regression_rule
|
|
367
|
+
elif type(self.z) == type(None):
|
|
368
|
+
def regression_rule(model, i):
|
|
369
|
+
return model.epsilon[i] + self.basexy[i] \
|
|
370
|
+
== model.alpha[i] \
|
|
371
|
+
+ sum(model.delta[i, j] * self.x[i][j] for j in model.J) \
|
|
372
|
+
- sum(model.gamma[i, k] * self.y[i][k] for k in model.K)
|
|
373
|
+
|
|
374
|
+
return regression_rule
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
elif self.rts == RTS_CRS:
|
|
379
|
+
if type(self.z) != type(None):
|
|
380
|
+
def regression_rule(model, i):
|
|
381
|
+
return self.basexy[i] \
|
|
382
|
+
== sum(model.delta[i, j] * self.x[i][j] for j in model.J) \
|
|
383
|
+
- sum(model.gamma[i, k] * self.y[i][k] for k in model.K) \
|
|
384
|
+
- sum(model.omega[m] * self.z[i][m] for m in model.M)-model.epsilon[i]
|
|
385
|
+
|
|
386
|
+
return regression_rule
|
|
387
|
+
elif type(self.z) == type(None):
|
|
388
|
+
def regression_rule(model, i):
|
|
389
|
+
return self.basexy[i] \
|
|
390
|
+
== sum(model.delta[i, j] * self.x[i][j] for j in model.J) \
|
|
391
|
+
- sum(model.gamma[i, k] * self.y[i][k] for k in model.K)-model.epsilon[i]
|
|
392
|
+
|
|
393
|
+
return regression_rule
|
|
394
|
+
|
|
395
|
+
raise ValueError("Undefined model parameters.")
|
|
396
|
+
|
|
397
|
+
def __afriat_rule(self):
|
|
398
|
+
"""Return the proper afriat inequality constraint"""
|
|
399
|
+
if self.fun == FUN_PROD:
|
|
400
|
+
__operator = NumericValue.__le__
|
|
401
|
+
elif self.fun == FUN_COST:
|
|
402
|
+
__operator = NumericValue.__ge__
|
|
403
|
+
|
|
404
|
+
if self.rts == RTS_VRS1:
|
|
405
|
+
def afriat_rule(model, i, h):
|
|
406
|
+
if i == h:
|
|
407
|
+
return Constraint.Skip
|
|
408
|
+
return __operator(model.alpha[i]
|
|
409
|
+
+ sum(model.delta[i, j] * self.x[i][j] for j in model.J) \
|
|
410
|
+
- sum(model.gamma[i, k] * self.y[i][k] for k in model.K),
|
|
411
|
+
model.alpha[h]
|
|
412
|
+
+ sum(model.delta[h, j] * self.x[i][j] for j in model.J) \
|
|
413
|
+
- sum(model.gamma[h, k] * self.y[i][k] for k in model.K))
|
|
414
|
+
|
|
415
|
+
return afriat_rule
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
elif self.rts == RTS_CRS:
|
|
420
|
+
def afriat_rule(model, i, h):
|
|
421
|
+
if i == h:
|
|
422
|
+
return Constraint.Skip
|
|
423
|
+
return __operator(sum(model.delta[i, j] * self.x[i][j] for j in model.J) \
|
|
424
|
+
- sum(model.gamma[i, k] * self.y[i][k] for k in model.K),
|
|
425
|
+
sum(model.delta[h, j] * self.x[i][j] for j in model.J) \
|
|
426
|
+
- sum(model.gamma[h, k] * self.y[i][k] for k in model.K))
|
|
427
|
+
|
|
428
|
+
return afriat_rule
|
|
429
|
+
|
|
430
|
+
raise ValueError("Undefined model parameters.")
|
|
431
|
+
|
|
432
|
+
def __translation_property(self):
|
|
433
|
+
"""Return the proper translation property
|
|
434
|
+
|
|
435
|
+
Standard form: coefficients × direction vector, independent of data
|
|
436
|
+
(Chambers, Chung, Färe 1996; Kuosmanen & Johnson 2017, EJOR 262(2):792).
|
|
437
|
+
Matches pyStoNED reference (ds2010/pyStoNED, CNLSDDF).
|
|
438
|
+
"""
|
|
439
|
+
|
|
440
|
+
def translation_rule(model, i):
|
|
441
|
+
return sum(model.delta[i, j] * self.gx[j] for j in model.J) \
|
|
442
|
+
+ sum(model.gamma[i, k] * self.gy[k] for k in model.K) == 1
|
|
443
|
+
|
|
444
|
+
return translation_rule
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def get_frontier(self):
|
|
453
|
+
"""Return estimated frontier value by array"""
|
|
454
|
+
tools.assert_optimized(self.optimization_status)
|
|
455
|
+
|
|
456
|
+
frontier_values = [v.value for v in self.__model__.epsilon[:]]
|
|
457
|
+
frontier = [ i+v for i,v in zip(self.basexy, frontier_values)]
|
|
458
|
+
return frontier
|
|
459
|
+
|
|
460
|
+
def get_residual(self):
|
|
461
|
+
"""Return r = anchor - fitted, independently of the active direction."""
|
|
462
|
+
tools.assert_optimized(self.optimization_status)
|
|
463
|
+
residual = list(self._single_model().epsilon[:].value)
|
|
464
|
+
return -np.asarray(residual)
|
|
465
|
+
|
|
466
|
+
# -----------------------------------------------------------------------------
|
|
467
|
+
# Constraint-generation counterpart
|
|
468
|
+
# -----------------------------------------------------------------------------
|
|
469
|
+
|
|
470
|
+
class CNLSDDFG(CNLSDDF):
|
|
471
|
+
"""CNLSDDF with delayed / sparse Afriat constraints.
|
|
472
|
+
|
|
473
|
+
The class keeps the public DataFrame + ``sent`` interface of ``CNLSDDF``.
|
|
474
|
+
It initializes with the standard CNLSDDF equations and replaces the full
|
|
475
|
+
pairwise Afriat block by the Sweet-spot sparse constraint set. This gives the
|
|
476
|
+
same computational-method interface as the ``G`` family without changing the
|
|
477
|
+
original full-constraint ``CNLSDDF`` class.
|
|
478
|
+
"""
|
|
479
|
+
|
|
480
|
+
def __init__(self, data, sent, z=None, gy=[1], gx=[1], fun=FUN_PROD, rts=RTS_VRS1):
|
|
481
|
+
from .utils import sweet
|
|
482
|
+
from .utils._g_common import cg_pairs, replace_constraint
|
|
483
|
+
super().__init__(data, sent, z=z, gy=gy, gx=gx, fun=fun, rts=rts)
|
|
484
|
+
self.cutactive = sweet.sweet(self.x)
|
|
485
|
+
self.active = np.zeros((len(self.x), len(self.x)))
|
|
486
|
+
self.active2 = np.zeros((len(self.x), len(self.x)))
|
|
487
|
+
replace_constraint(self, 'afriat_rule', cg_pairs(self.cutactive), self._CNLSDDF__afriat_rule(), 'active afriat inequality')
|
|
488
|
+
|
|
489
|
+
def optimize(self, email=OPT_LOCAL, solver=OPT_DEFAULT):
|
|
490
|
+
self._optimize_cnls_model(email=email, solver=solver, cet=CET_ADDI)
|