In [139]:
%matplotlib inline
import heapq
import matplotlib.pyplot as plt
from IPython.core.debugger import Pdb; pdb = Pdb()
def calc_length_div(p_list):
p_mean = round(sum(p_list) / len(p_list), 2)
return sum([abs(p - p_mean) for p in p_list])
def calc_standard_centre_num(p_list):
std_centre_num = 0
for p in p_list:
if p >= 5:
std_centre_num += 1
return std_centre_num
def calc_price_gap(h, l):
return (h[1] - l[1])
def down_centre_expand_spliter(p_list):
top_nums = (len(p_list) - 8) // 2
ph_start_offset = 3
ph_end_offset = -7
pl_start_offset = 6
pl_end_offset = -4
p = list(enumerate(p_list))
p_max_index = len(p_list) - 1
ph = p[ph_start_offset : ph_end_offset + 1][::2]
pl = p[pl_start_offset : pl_end_offset + 1][::2]
h_tops = heapq.nlargest(top_nums, ph, key=lambda x: x[1])
l_bottoms = heapq.nsmallest(top_nums, pl, key=lambda x: x[1])
options = []
index = 0
h_index = 0
for h in h_tops:
l_index = 0
for l in l_bottoms:
if (l[0] - h[0]) >= 3 and l[1] < h[1]:
div = calc_length_div([h[0], (l[0] - h[0]), (p_max_index - l[0])])
std_centre_num = calc_standard_centre_num([h[0], (l[0] - h[0]), (p_max_index - l[0])])
gap = calc_price_gap(h, l)
options.append([index, h_index, l_index, std_centre_num, gap, h, l])
index += 1
l_index += 1
h_index += 1
options = sorted(options, key = lambda x: ((0 - x[3]), (0 - x[4]), x[2], x[1]))
[print(p) for p in options]
found = False
for opt in options:
hl = opt[5]
lr = opt[6]
# if p_list[0] < hl[1] and hl[1] > lr[1] and lr[1] < p_list[-1]:
if p_list[0] < hl[1] and hl[1] > lr[1]:
found = True
break
if not found:
raise RuntimeError('Not Found')
return [0, hl[0], lr[0], p_max_index], [p_list[0], hl[1], lr[1], p_list[-1]]
# y = [0, 100, 60, 130, 70, 120, 40, 90, 50, 140, 85, 105]
# y = [0, 100, 60, 110, 70, 72, 61, 143, 77, 91, 82, 100, 83, 124, 89, 99]
# y = [0, 100, 60, 110, 70, 115, 75, 120, 80, 125, 85, 130, 90, 135]
# y = [0, 100, 60, 110, 70, 78, 77, 121, 60, 93, 82, 141, 78, 134]
# y = [0, 110, 70, 100, 60, 100, 78, 90, 53, 109, 56, 141, 99, 106, 89, 99, 93, 141]
# y = [0, 110, 70, 100, 60, 79, 77, 117, 61, 145, 72, 139, 62, 150, 81, 132]
# x = list(range(0, len(y)))
# gg = [min(y[1], y[3])] * len(y)
# dd = [max(y[2], y[4])] * len(y)
# plt.figure(figsize=(len(y),4))
# plt.title(y)
# plt.grid()
# plt.plot(x, y)
# plt.plot(x, gg, '--')
# plt.plot(x, dd, '--')
# sx, sy = down_centre_expand_spliter(y)
# plt.plot(sx, sy)
# plt.show()
In [ ]:
# Centre Expand Prototype
%matplotlib inline
import matplotlib.pyplot as plt
y_base = [0, 100, 60, 130, 70, 120, 40, 90, 50, 140, 85, 105, 55, 80]
for i in range(10, len(y_base)):
y = y_base[:(i + 1)]
x = list(range(0, len(y)))
gg = [min(y[1], y[3])] * len(y)
dd = [max(y[2], y[4])] * len(y)
plt.figure(figsize=(i,4))
plt.title(y)
plt.grid()
plt.plot(x, y)
plt.plot(x, gg, '--')
plt.plot(x, dd, '--')
if i % 2 == 1:
sx, sy = down_centre_expand_spliter(y)
plt.plot(sx, sy)
plt.show()
In [ ]:
# Random Centre Generator
%matplotlib inline
import random
import matplotlib.pyplot as plt
y_max = 150
y_min = 50
num_max = 18
def generate_next(y_list, direction):
if direction == 1:
y_list.append(random.randint(max(y_list[2], y_list[4], y_list[-1]) + 1, y_max))
elif direction == -1:
y_list.append(random.randint(y_min, min(y_list[1], y_list[3], y_list[-1]) - 1))
# y_base = [0, 100, 60, 110, 70]
y_base = [0, 110, 70, 100, 60]
# y_base = [0, 100, 60, 90, 70]
# y_base = [0, 90, 70, 100, 60]
direction = 1
for i in range(5, num_max):
generate_next(y_base, direction)
direction = 0 - direction
print(y_base)
for i in range(11, len(y_base), 2):
y = y_base[:(i + 1)]
x = list(range(0, len(y)))
gg = [min(y[1], y[3])] * len(y)
dd = [max(y[2], y[4])] * len(y)
plt.figure(figsize=(i,4))
plt.title(y)
plt.grid()
plt.plot(x, y)
plt.plot(x, gg, '--')
plt.plot(x, dd, '--')
sx, sy = down_centre_expand_spliter(y)
plt.plot(sx, sy)
plt.show()
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
# Group 1
# y_base = [0, 100, 60, 110, 70, 99, 66, 121, 91, 141, 57, 111, 69, 111]
# y_base = [0, 100, 60, 110, 70, 105, 58, 102, 74, 137, 87, 142, 55, 128]
# y_base = [0, 100, 60, 110, 70, 115, 75, 120, 80, 125, 85, 130, 90, 135]
# y_base = [0, 100, 60, 110, 70, 120, 80, 130, 90, 140, 50, 75]
# y_base = [0, 100, 60, 110, 70, 114, 52, 75, 54, 77, 65, 100, 66, 87, 70, 116]
# y_base = [0, 100, 60, 110, 70, 72, 61, 143, 77, 91, 82, 100, 83, 124, 89, 99, 89, 105]
# Group 2
# y_base = [0, 110, 70, 100, 60, 142, 51, 93, 78, 109, 60, 116, 50, 106]
# y_base = [0, 110, 70, 100, 60, 88, 70, 128, 82, 125, 72, 80, 63, 119]
# y_base = [0, 110, 70, 100, 60, 74, 66, 86, 57, 143, 50, 95, 70, 91]
# y_base = [0, 110, 70, 100, 60, 77, 73, 122, 96, 116, 82, 124, 69, 129]
# y_base = [0, 110, 70, 100, 60, 147, 53, 120, 77, 103, 56, 76, 74, 92]
# y_base = [0, 110, 70, 100, 60, 95, 55, 90, 50, 85, 45, 80, 40, 75]
# y_base = [0, 110, 70, 100, 60, 100, 78, 90, 53, 109, 56, 141, 99, 106, 89, 99, 93, 141]
# Group 3
# y_base = [0, 100, 60, 90, 70, 107, 55, 123, 79, 112, 64, 85, 74, 110]
# y_base = [0, 100, 60, 90, 70, 77, 55, 107, 76, 141, 87, 91, 60, 83]
# y_base = [0, 100, 60, 90, 70, 114, 67, 93, 58, 134, 53, 138, 64, 107]
# y_base = [0, 100, 60, 90, 70, 77, 66, 84, 79, 108, 87, 107, 72, 89]
# y_base = [0, 100, 60, 90, 70, 88, 72, 86, 74, 84, 76, 82, 74, 80]
# Group 4
# y_base = [0, 90, 70, 100, 60, 131, 57, 144, 85, 109, 82, 124, 87, 101]
# y_base = [0, 90, 70, 100, 60, 150, 56, 112, 63, 95, 84, 118, 58, 110]
# y_base = [0, 90, 70, 100, 60, 145, 64, 112, 69, 86, 71, 119, 54, 95]
# y_base = [0, 90, 70, 100, 60, 105, 55, 110, 50, 115, 45, 120, 40, 125]
for i in range(11, len(y_base), 2):
y = y_base[:(i + 1)]
x = list(range(0, len(y)))
gg = [min(y[1], y[3])] * len(y)
dd = [max(y[2], y[4])] * len(y)
plt.figure(figsize=(i,4))
plt.title(y)
plt.grid()
plt.plot(x, y)
plt.plot(x, gg, '--')
plt.plot(x, dd, '--')
sx, sy = down_centre_expand_spliter(y)
plt.plot(sx, sy)
plt.show()