algorithm.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if len(pos_commit[0]) > 0:
  80. view = data[:, pos_commit]
  81. view = view.reshape(pos_map.dim(),-1)
  82. sums = np.sum(view, axis=1)
  83. count = int(sums[pos_map.commit_count])
  84. amounts = sums[pos_map.commit_amounts]
  85. price = round(amounts / count, 4)
  86. else:
  87. return price,None
  88. profit = None
  89. pratio = None
  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.00001)
  112. back_time = (succ_periods + fail_periods) / (succs + fails + 0.00001)
  113. succ_time = (succ_periods) / (succs + 0.00001)
  114. return round(ratio, 5), commit_count, int(back_time),int(succ_time)
  115. def calc_commit(data, pos_map, start, end):
  116. view = data[[pos_map.commit_count], :]
  117. view = view[:, start:end]
  118. sums = np.sum(view, axis=1)
  119. commit_count = int(sums[0])
  120. return commit_count
  121. def calc_mchratios(data, pos_map, start, end, window, left_len, right_len):
  122. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  123. sum_view = view[:, start + left_len:end - right_len]
  124. sums = np.sum(sum_view, axis=1)
  125. succs = int(sums[0])
  126. fails = int(sums[1])
  127. commits = int(sums[2])
  128. view = view[:, start:end]
  129. all = np.cumsum(view, axis=1)
  130. succ = all[0, :]
  131. commit = all[2, :] + 0.0000001
  132. y = succ / commit
  133. y = y.ravel()
  134. if window is not None:
  135. y = np.convolve(y, window, 'same')
  136. y = y[left_len:end - start - right_len]
  137. return succs, commits, y
  138. def calc_mchratios_val(data, pos_map, start, end):
  139. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  140. view = view[:, start:end]
  141. sums = np.sum(view, axis=1)
  142. succs = sums[0]
  143. fails = sums[1]
  144. ratio = succs / (succs + fails + 0.0000001)
  145. return int(succs), int(succs + fails), round(ratio,5)
  146. def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
  147. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  148. sum_view = view[:, start + left_len:end - right_len]
  149. sums = np.sum(sum_view, axis=1)
  150. succs = sums[0]
  151. fails = sums[1]
  152. view = view[:, start:end]
  153. succ = view[0, :]
  154. fail = view[1, :]
  155. succ = np.convolve(succ, window, 'same')
  156. fail = np.convolve(fail, window, 'same')
  157. commit = succ + fail + 0.0000001
  158. y = succ / commit
  159. y = y[left_len:end - start - right_len]
  160. return int(succs), int(succs + fails), y
  161. def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
  162. view = data[:, start:end]
  163. sums = np.sum(view, axis=1)
  164. all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
  165. ratio = sums[pos_map.succ_mch_amounts] / all_return
  166. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  167. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  168. lack_amounts = send_amounts * ratio
  169. return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
  170. sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
  171. def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
  172. view = data[:, start:end]
  173. sums = np.sum(view, axis=1)
  174. all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
  175. ratio = sums[pos_map.succ_count] / all_return
  176. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  177. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  178. lack_amounts = send_amounts * ratio
  179. logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
  180. return send_amounts, lack_amounts
  181. # 用于计算成功率及利润率
  182. # succ_count, fail_count, succ_ratio, profit,profit_ratio
  183. def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
  184. view = data[:, start:end]
  185. sums = np.sum(view, axis=1)
  186. submit_count = sums[pos_map.submit_count]
  187. succ_count = sums[pos_map.succ_count]
  188. fail_count = sums[pos_map.fail_count]
  189. succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
  190. ch_amounts = sums[pos_map.succ_ch_amounts]
  191. mch_amounts = sums[pos_map.succ_mch_amounts]
  192. profit = mch_amounts - ch_amounts
  193. return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 5), round(profit, 3)
  194. def calc_cov_netfail(data, pos_map, start, end, window):
  195. view = data[[pos_map.succ_count, pos_map.fail_count], :]
  196. view = view[:, start:end]
  197. sums = np.sum(view, axis=1)
  198. succs = int(sums[0])
  199. fails = int(sums[1])
  200. succ = view[0, :]
  201. fail = view[1, :]
  202. succ = np.convolve(succ, window, 'same')
  203. fail = np.convolve(fail, window, 'same')
  204. fail = fail / (fail + succ + 0.0000001)
  205. return fail, fails, (succs + fails)