-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDEA_ML_Index.py
More file actions
226 lines (192 loc) · 7.27 KB
/
Copy pathDEA_ML_Index.py
File metadata and controls
226 lines (192 loc) · 7.27 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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import pulp
# In[15]:
class ML_index:
"""
Help on class DEAProblem
ML_index(inputs, outputs, bad_outs, weight_vector, directional_factor=None, returns='CRS',
in_weights=[0, None], out_weights=[0, None],badout_weights=[0, None])
DEAProblem solves DEA model using directional distance function.
Parameters:
inputs: input data, DataFrame data
outputs: output data, DataFrame data
bad_outs: undesirable output data, DataFrame data
weight_vector: weights for individual inputs and outputs. List data
"""
def __init__(
self,
inputs_1,
outputs_1,
bad_outs_1,
inputs_2,
outputs_2,
bad_outs_2,
weight_vector,
directional_factor=None,
returns="CRS",
disp="weak disposability",
in_weights=[0, None],
out_weights=[0, None],
badout_weights=[0, None],
):
self.inputs_1 = inputs_1
self.outputs_1 = outputs_1
self.bad_outs_1 = bad_outs_1
self.inputs_2 = inputs_2
self.outputs_2 = outputs_2
self.bad_outs_2 = bad_outs_2
self.returns = returns
self.weight_vector = (
weight_vector # weight vector in directional distance function
)
self.disp = disp
self.J, self.I = self.inputs_1.shape # no of DMUs, inputs
_, self.R = self.outputs_1.shape # no of outputs
_, self.S = self.bad_outs_1.shape # no of bad outputs
self._i = range(self.I) # inputs
self._r = range(self.R) # outputs
self._s = range(self.S) # bad_output
self._j = range(self.J) # DMUs
if directional_factor == None:
pass
else:
self.gx = directional_factor[: self.I]
self.gy = directional_factor[self.I : (self.I + self.J)]
self.gy = directional_factor[(self.I + self.J) :]
self._in_weights = in_weights # input weight restrictions
self._out_weights = out_weights # output weight restrictions
self._badout_weights = badout_weights # bad output weight restrictions
# creates dictionary of pulp.LpProblem objects for the DMUs
self.dmus = self.solve_problems()
def solve_problems(self):
"""
Iterate over the DMU and create a dictionary of LP problems, one
for each DMU.
"""
dmu_dict_ddf11 = {}
dmu_dict_ddf22 = {}
dmu_dict_ddf12 = (
{}
) # DDF of technology in Period 1 using Period 2 as Reference technology
dmu_dict_ddf21 = (
{}
) # DDF of technology in Period 2 using Period 1 as Reference technology
dmu_dict_MI = {} # MI_index
dmu_dict_EC = {} # efficiency_change
dmu_dict_TC = {} # technology_change
for j0 in self._j:
dmu_dict_ddf11[j0] = self._make_problem(
j0,
self.inputs_1,
self.outputs_1,
self.bad_outs_1,
self.inputs_1,
self.outputs_1,
self.bad_outs_1,
)
dmu_dict_ddf22[j0] = self._make_problem(
j0,
self.inputs_2,
self.outputs_2,
self.bad_outs_2,
self.inputs_2,
self.outputs_2,
self.bad_outs_2,
)
dmu_dict_ddf12[j0] = self._make_problem(
j0,
self.inputs_2,
self.outputs_2,
self.bad_outs_2,
self.inputs_1,
self.outputs_1,
self.bad_outs_1,
)
dmu_dict_ddf21[j0] = self._make_problem(
j0,
self.inputs_1,
self.outputs_1,
self.bad_outs_1,
self.inputs_2,
self.outputs_2,
self.bad_outs_2,
)
# ML_index calculation
numerator = (1 + dmu_dict_ddf12[j0]) * (1 + dmu_dict_ddf11[j0])
denominator = (1 + dmu_dict_ddf22[j0]) * (1 + dmu_dict_ddf21[j0])
dmu_dict_MI[j0] = (numerator / denominator) ** (1 / 2)
# Efficiency change calculation
dmu_dict_EC[j0] = (1 + dmu_dict_ddf11[j0]) / (1 + dmu_dict_ddf22[j0])
# Technological change calculation
numerator_TC = (1 + dmu_dict_ddf12[j0]) * (1 + dmu_dict_ddf22[j0])
denominator_TC = (1 + dmu_dict_ddf11[j0]) * (1 + dmu_dict_ddf21[j0])
dmu_dict_TC[j0] = (numerator_TC / denominator_TC)**(1/2)
return dmu_dict_MI, dmu_dict_EC, dmu_dict_TC
def _make_problem(
self, j0, inputs1, outputs1, bad_outs1, inputs2, outputs2, bad_outs2
):
"""
Create a pulp.LpProblem for a DMU.
"""
# Set up pulp
prob = pulp.LpProblem("".join(["DMU_", str(j0)]), pulp.LpMaximize)
self.weights = pulp.LpVariable.dicts(
"Weight", (self._j), lowBound=self._in_weights[0]
)
self.betax = pulp.LpVariable.dicts(
"scalingFactor_x", (self._i), lowBound=0, upBound=1
)
self.betay = pulp.LpVariable.dicts("scalingFactor_y", (self._r), lowBound=0)
self.betab = pulp.LpVariable.dicts(
"scalingFactor_b", (self._s), lowBound=0, upBound=1
)
# Set up objective function
prob += pulp.lpSum(
[(self.weight_vector[i] * self.betax[i]) for i in self._i]
+ [(self.weight_vector[self.I + r] * self.betay[r]) for r in self._r]
+ [
(self.weight_vector[self.I + self.R + s] * self.betab[s])
for s in self._s
]
)
# Set up constraints
for i in self._i:
prob += (
pulp.lpSum(
[(self.weights[j0] * inputs1.values[j0][i]) for j0 in self._j]
)
<= inputs2.values[j0][i] - self.betax[i] * inputs2.values[j0][i]
)
for r in self._r:
prob += (
pulp.lpSum(
[(self.weights[j0] * outputs1.values[j0][r]) for j0 in self._j]
)
>= outputs2.values[j0][r] + self.betay[r] * outputs2.values[j0][r]
)
if self.disp == "weak disposability":
for s in self._s: # weak disposability
prob += (
pulp.lpSum(
[(self.weights[j0] * bad_outs1.values[j0][s]) for j0 in self._j]
)
== bad_outs2.values[j0][s] - self.betab[s] * bad_outs2.values[j0][s]
)
elif self.disp == "strong disposability":
for s in self._s: # strong disposability
prob += (
pulp.lpSum(
[(self.weights[j0] * bad_outs1.values[j0][s]) for j0 in self._j]
)
>= bad_outs2.values[j0][s] - self.betab[s] * bad_outs2.values[j0][s]
)
# Set returns to scale
if self.returns == "VRS":
prob += sum([self.weights[j] for j in self.weights]) == 1
prob.solve()
Objective_func = pulp.value(prob.objective)
return Objective_func