algorithm.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. from .DataStream import EMchPosmap
  2. import numpy as np
  3. import logging
  4. logger = logging.getLogger('algorithm')
  5. def calc_chratios(data, pos_map, start, end, window, left_len, right_len):
  6. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  7. all = view[:, start:end]
  8. view = np.cumsum(all, axis=1)
  9. succ = view[0, :]
  10. fail = view[1, :]
  11. commit = view[2, :] + 0.0000001
  12. y = succ / commit
  13. y = y.ravel()
  14. if window is not None:
  15. y = np.convolve(y, window, 'same')
  16. y = y[left_len:end - start - right_len]
  17. cur = all[:, left_len:end - start - right_len]
  18. sums = np.sum(cur, axis=1)
  19. succs = int(sums[0])
  20. fails = int(sums[1])
  21. commits = int(sums[1])
  22. return succs, commits, y
  23. def calc_cov_chratios(data, pos_map, start, end, window, left_len, right_len=0):
  24. sum_view = data[:, start + left_len:end - right_len]
  25. sums = np.sum(sum_view, axis=1)
  26. succs = int(sums[pos_map.succ_count])
  27. fails = int(sums[pos_map.fail_count])
  28. commits = int(sums[pos_map.commit_count])
  29. succ_periods = int(sums[pos_map.succ_periods])
  30. fail_periods = int(sums[pos_map.fail_periods])
  31. view = data[:, start:end]
  32. succ = view[pos_map.succ_count, :]
  33. fail = view[pos_map.fail_count, :]
  34. if window is not None:
  35. succ = np.convolve(succ, window, 'same')
  36. fail = np.convolve(fail, window, 'same')
  37. commit = succ + fail + 0.0000001
  38. y = succ / commit
  39. y = y[left_len:end - start - right_len]
  40. return succs, commits, y, int(succ_periods / (succs + 1)), int(fail_periods / (fails + 1))
  41. def calc_count_cdf(data, pos_map, start, end, left_len, right_len=0):
  42. view = data[:, start + left_len:end - right_len]
  43. view = np.cumsum(view, axis=1)
  44. succs = view[pos_map.succ_count, :]
  45. commits = view[pos_map.commit_count, :]
  46. return succs.ravel(), commits.ravel()
  47. def calc_chspeed_ratio(data, pos_map, start, end, period):
  48. dim = pos_map.dim()
  49. view = data[:, start:end]
  50. sum_view = view.reshape((dim, -1, period))
  51. sums = np.sum(sum_view, axis=2)
  52. succs = sums[pos_map.succ_count]
  53. commits = sums[pos_map.commit_count]
  54. ratios = succs / (commits + 0.000001)
  55. return commits, succs, ratios
  56. def calc_cov_chsuccs(data, pos_map, start, end, window, left_len, right_len):
  57. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  58. view = view[:, start:end]
  59. cur = view[:, left_len:end - start - right_len]
  60. sums = np.sum(cur, axis=1)
  61. succs = int(sums[0])
  62. fails = int(sums[1])
  63. succ = view[0, :]
  64. commit = view[2, :]
  65. succ = np.convolve(succ, window, 'same')
  66. commit = np.convolve(commit, window, 'same')
  67. succ = succ[left_len:end - start - right_len]
  68. commit = commit[left_len:end - start - right_len]
  69. return succ, commit, succs, (succs + fails)
  70. def calc_chspeed(data, pos_map, start, end):
  71. view = data[[pos_map.commit_count], :]
  72. view = view[:, start:end]
  73. speed = np.sum(view, axis=1)
  74. return int(speed[0])
  75. def calc_chprice(data, pos_map, start, end):
  76. commits = data[pos_map.commit_count, :]
  77. pos_commit = np.where(commits > 0)
  78. price = None
  79. profit = None
  80. pratio = None
  81. if len(pos_commit[0]) > 0:
  82. view = data[:, pos_commit]
  83. view = view.reshape(pos_map.dim(), -1)
  84. sums = np.sum(view, axis=1)
  85. count = int(sums[pos_map.commit_count])
  86. amounts = sums[pos_map.commit_amounts]
  87. price = round(amounts / count, 4)
  88. else:
  89. return price, profit, pratio
  90. succs = data[pos_map.succ_count, :]
  91. pos_succ = np.where(succs > 0)
  92. if len(pos_succ[0]) > 0:
  93. view = data[:, pos_succ]
  94. view = view.reshape(pos_map.dim(), -1)
  95. sums = np.sum(view, axis=1)
  96. mch_amounts = sums[pos_map.succ_mch_amounts]
  97. ch_amounts = sums[pos_map.succ_amounts]
  98. profit = mch_amounts - ch_amounts
  99. pratio = round(profit / ch_amounts, 4)
  100. return price, profit, pratio
  101. def calc_chratio(data, pos_map, start, end):
  102. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.succ_periods, pos_map.fail_periods, pos_map.commit_count], :]
  103. view = view[:, start:end]
  104. sums = np.sum(view, axis=1)
  105. succs = sums[0]
  106. fails = sums[1]
  107. succ_periods = sums[2]
  108. fail_periods = sums[3]
  109. commit_count = int(sums[4])
  110. all = int(succs + fails)
  111. ratio = succs / (commit_count + 0.0000001)
  112. back_time = (succ_periods + fail_periods) / (succs + fails + 0.0000001)
  113. succ_time = (succ_periods) / (succs + 0.00001)
  114. return round(ratio, 5), commit_count, int(back_time), int(succ_time)
  115. def calc_unback_time(data, pos_map, start, end):
  116. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  117. view = view[:, start:end]
  118. delta = view[2, :] - view[0, :] - view[1, :]
  119. pos = np.where(delta > 0)
  120. if len(pos[0]) > 0:
  121. start = pos[0][0]
  122. count = np.sum(delta)
  123. else:
  124. pass
  125. return delta
  126. def calc_commit(data, pos_map, start, end):
  127. view = data[[pos_map.commit_count], :]
  128. view = view[:, start:end]
  129. sums = np.sum(view, axis=1)
  130. commit_count = int(sums[0])
  131. return commit_count
  132. def calc_mchratios(data, pos_map, start, end, window, left_len, right_len):
  133. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  134. sum_view = view[:, start + left_len:end - right_len]
  135. sums = np.sum(sum_view, axis=1)
  136. succs = int(sums[0])
  137. fails = int(sums[1])
  138. commits = int(sums[2])
  139. view = view[:, start:end]
  140. all = np.cumsum(view, axis=1)
  141. succ = all[0, :]
  142. commit = all[2, :] + 0.0000001
  143. y = succ / commit
  144. y = y.ravel()
  145. if window is not None:
  146. y = np.convolve(y, window, 'same')
  147. y = y[left_len:end - start - right_len]
  148. return succs, commits, y
  149. def calc_mchratios_val(data, pos_map, start, end):
  150. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  151. view = view[:, start:end]
  152. sums = np.sum(view, axis=1)
  153. succs = sums[0]
  154. fails = sums[1]
  155. ratio = succs / (succs + fails + 0.0000001)
  156. return int(succs), int(succs + fails), round(ratio, 5)
  157. def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
  158. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  159. sum_view = view[:, start + left_len:end - right_len]
  160. sums = np.sum(sum_view, axis=1)
  161. succs = sums[0]
  162. fails = sums[1]
  163. view = view[:, start:end]
  164. succ = view[0, :]
  165. fail = view[1, :]
  166. succ = np.convolve(succ, window, 'same')
  167. fail = np.convolve(fail, window, 'same')
  168. commit = succ + fail + 0.0000001
  169. y = succ / commit
  170. y = y[left_len:end - start - right_len]
  171. return int(succs), int(succs + fails), y
  172. def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
  173. view = data[:, start:end]
  174. sums = np.sum(view, axis=1)
  175. all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
  176. ratio = sums[pos_map.succ_mch_amounts] / all_return
  177. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  178. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  179. lack_amounts = send_amounts * ratio
  180. return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
  181. sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
  182. def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
  183. view = data[:, start:end]
  184. sums = np.sum(view, axis=1)
  185. all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
  186. ratio = sums[pos_map.succ_count] / all_return
  187. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  188. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  189. lack_amounts = send_amounts * ratio
  190. logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
  191. return send_amounts, lack_amounts
  192. # 用于计算成功率及利润率
  193. # succ_count, fail_count, succ_ratio, profit,profit_ratio
  194. def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
  195. view = data[:, start:end]
  196. sums = np.sum(view, axis=1)
  197. submit_count = sums[pos_map.submit_count]
  198. succ_count = sums[pos_map.succ_count]
  199. fail_count = sums[pos_map.fail_count]
  200. succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
  201. ch_amounts = sums[pos_map.succ_ch_amounts]
  202. mch_amounts = sums[pos_map.succ_mch_amounts]
  203. profit = mch_amounts - ch_amounts
  204. return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 6), round(profit, 4)
  205. def calc_cov_netfail(data, pos_map, start, end, window):
  206. view = data[[pos_map.succ_count, pos_map.fail_count], :]
  207. view = view[:, start:end]
  208. sums = np.sum(view, axis=1)
  209. succs = int(sums[0])
  210. fails = int(sums[1])
  211. succ = view[0, :]
  212. fail = view[1, :]
  213. succ = np.convolve(succ, window, 'same')
  214. fail = np.convolve(fail, window, 'same')
  215. fail = fail / (fail + succ + 0.0000001)
  216. return fail, fails, (succs + fails)