Advertisement
Danila_lipatov

DEA_example

Feb 5th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #https://github.com/araith/pyDEA/tree/master/tests
  2. #https://pystoned.readthedocs.io/en/latest/examples/DEA/dea_io.html
  3. #https://www.mathnet.ru/links/38984a4181dfe157bdcdd6e3f6982187/izkab170.pdf
  4. #https://github.com/metjush/envelopment-py
  5.  
  6.  
  7.  
  8. import numpy as np
  9. from scipy.optimize import fmin_slsqp
  10.  
  11.  
  12. class DEA(object):
  13.  
  14. def __init__(self, inputs, outputs):
  15.  
  16. # supplied data
  17. self.inputs = inputs
  18. self.outputs = outputs
  19.  
  20. # parameters
  21. self.n = inputs.shape[0]
  22. self.m = inputs.shape[1]
  23. self.r = outputs.shape[1]
  24.  
  25. # iterators
  26. self.unit_ = range(self.n)
  27. self.input_ = range(self.m)
  28. self.output_ = range(self.r)
  29.  
  30. # result arrays
  31. self.output_w = np.zeros((self.r, 1), dtype=np.float32()) # output weights
  32. self.input_w = np.zeros((self.m, 1), dtype=np.float32()) # input weights
  33. self.lambdas = np.zeros((self.n, 1), dtype=np.float32()) # unit efficiencies
  34. self.efficiency = np.zeros_like(self.lambdas) # thetas
  35. def __efficiency(self, unit):
  36. # compute efficiency
  37. denominator = np.dot(self.inputs, self.input_w)
  38. numerator = np.dot(self.outputs, self.output_w)
  39.  
  40. return (numerator/denominator)[unit]
  41. def __target(self, x, unit):
  42.  
  43. in_w, out_w, lambdas = x[:self.m], x[self.m:(self.m+self.r)], x[(self.m+self.r):] # unroll the weights
  44. denominator = np.dot(self.inputs[unit], in_w)
  45. numerator = np.dot(self.outputs[unit], out_w)
  46.  
  47. return numerator/denominator
  48.  
  49. def __constraints(self, x, unit):
  50.  
  51. in_w, out_w, lambdas = x[:self.m], x[self.m:(self.m+self.r)], x[(self.m+self.r):] # unroll the weights
  52. constr = [] # init the constraint array
  53.  
  54. # for each input, lambdas with inputs
  55. for input in self.input_:
  56. t = self.__target(x, unit)
  57. lhs = np.dot(self.inputs[:, input], lambdas)
  58. cons = t*self.inputs[unit, input] - lhs
  59. constr.append(cons)
  60.  
  61. # for each output, lambdas with outputs
  62. for output in self.output_:
  63. lhs = np.dot(self.outputs[:, output], lambdas)
  64. cons = lhs - self.outputs[unit, output]
  65. constr.append(cons)
  66.  
  67. # for each unit
  68. for u in self.unit_:
  69. constr.append(lambdas[u])
  70.  
  71. return np.array(constr)
  72.  
  73. def __optimize(self):
  74.  
  75. d0 = self.m + self.r + self.n
  76. # iterate over units
  77. for unit in self.unit_:
  78. # weights
  79. x0 = np.random.rand(d0) - 0.5
  80. x0 = fmin_slsqp(self.__target, x0, f_ieqcons=self.__constraints, args=(unit,))
  81. # unroll weights
  82. self.input_w, self.output_w, self.lambdas = x0[:self.m], x0[self.m:(self.m+self.r)], x0[(self.m+self.r):]
  83. self.efficiency[unit] = self.__efficiency(unit)
  84.  
  85. def fit(self):
  86. self.__optimize() # optimize
  87. return self.efficiency
  88.  
  89.  
  90. if __name__ == "__main__":
  91. X = np.array([
  92. [4., 3., 3.],
  93. [7., 3., 3.],
  94. [8., 1., 7],
  95. [4., 2., 2],
  96. [2., 4., 4],
  97. [10., 1., 7]
  98. ])
  99. y = np.array([
  100. [1., 15.],
  101. [1., 10.],
  102. [1., 4.],
  103. [1., 7.],
  104. [1., 6.],
  105. [1., 9.]
  106. ])
  107. dea = DEA(X,y)
  108. rs = dea.fit()
  109. print(rs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement