-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathenvelopment.py
181 lines (150 loc) · 5.23 KB
/
envelopment.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
"""
Data Envelopment Analysis implementation
Sources:
Sherman & Zhu (2006) Service Productivity Management, Improving Service Performance using Data Envelopment Analysis (DEA) [Chapter 2]
ISBN: 978-0-387-33211-6
http://deazone.com/en/resources/tutorial
"""
import numpy as np
from scipy.optimize import fmin_slsqp
class DEA(object):
def __init__(self, inputs, outputs):
"""
Initialize the DEA object with input data
n = number of entities (observations)
m = number of inputs (variables, features)
r = number of outputs
:param inputs: inputs, n x m numpy array
:param outputs: outputs, n x r numpy array
:return: self
"""
# supplied data
self.inputs = inputs
self.outputs = outputs
# parameters
self.n = inputs.shape[0]
self.m = inputs.shape[1]
self.r = outputs.shape[1]
# iterators
self.unit_ = range(self.n)
self.input_ = range(self.m)
self.output_ = range(self.r)
# result arrays
self.output_w = np.zeros((self.r, 1), dtype=np.float) # output weights
self.input_w = np.zeros((self.m, 1), dtype=np.float) # input weights
self.lambdas = np.zeros((self.n, 1), dtype=np.float) # unit efficiencies
self.efficiency = np.zeros_like(self.lambdas) # thetas
# names
self.names = []
def __efficiency(self, unit):
"""
Efficiency function with already computed weights
:param unit: which unit to compute for
:return: efficiency
"""
# compute efficiency
denominator = np.dot(self.inputs, self.input_w)
numerator = np.dot(self.outputs, self.output_w)
return (numerator/denominator)[unit]
def __target(self, x, unit):
"""
Theta target function for one unit
:param x: combined weights
:param unit: which production unit to compute
:return: theta
"""
in_w, out_w, lambdas = x[:self.m], x[self.m:(self.m+self.r)], x[(self.m+self.r):] # unroll the weights
denominator = np.dot(self.inputs[unit], in_w)
numerator = np.dot(self.outputs[unit], out_w)
return numerator/denominator
def __constraints(self, x, unit):
"""
Constraints for optimization for one unit
:param x: combined weights
:param unit: which production unit to compute
:return: array of constraints
"""
in_w, out_w, lambdas = x[:self.m], x[self.m:(self.m+self.r)], x[(self.m+self.r):] # unroll the weights
constr = [] # init the constraint array
# for each input, lambdas with inputs
for input in self.input_:
t = self.__target(x, unit)
lhs = np.dot(self.inputs[:, input], lambdas)
cons = t*self.inputs[unit, input] - lhs
constr.append(cons)
# for each output, lambdas with outputs
for output in self.output_:
lhs = np.dot(self.outputs[:, output], lambdas)
cons = lhs - self.outputs[unit, output]
constr.append(cons)
# for each unit
for u in self.unit_:
constr.append(lambdas[u])
return np.array(constr)
def __optimize(self):
"""
Optimization of the DEA model
Use: http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.optimize.linprog.html
A = coefficients in the constraints
b = rhs of constraints
c = coefficients of the target function
:return:
"""
d0 = self.m + self.r + self.n
# iterate over units
for unit in self.unit_:
# weights
x0 = np.random.rand(d0) - 0.5
x0 = fmin_slsqp(self.__target, x0, f_ieqcons=self.__constraints, args=(unit,))
# unroll weights
self.input_w, self.output_w, self.lambdas = x0[:self.m], x0[self.m:(self.m+self.r)], x0[(self.m+self.r):]
self.efficiency[unit] = self.__efficiency(unit)
def name_units(self, names):
"""
Provide names for units for presentation purposes
:param names: a list of names, equal in length to the number of units
:return: nothing
"""
assert(self.n == len(names))
self.names = names
def fit(self):
"""
Optimize the dataset, generate basic table
:return: table
"""
self.__optimize() # optimize
print("Final thetas for each unit:\n")
print("---------------------------\n")
for n, eff in enumerate(self.efficiency):
if len(self.names) > 0:
name = "Unit %s" % self.names[n]
else:
name = "Unit %d" % (n+1)
print("%s theta: %.4f" % (name, eff))
print("\n")
print("---------------------------\n")
if __name__ == "__main__":
X = np.array([
[20., 300.],
[30., 200.],
[40., 100.],
[20., 200.],
[10., 400.]
])
y = np.array([
[1000.],
[1000.],
[1000.],
[1000.],
[1000.]
])
names = [
'Bratislava',
'Zilina',
'Kosice',
'Presov',
'Poprad'
]
dea = DEA(X,y)
dea.name_units(names)
dea.fit()