def test_set_points(pts, t): test_set = [] rand_pts = random.sample(pts, min(int(math.sqrt(2 * t) + 1), len(pts))) for p1, p2 in itertools.combinations(rand_pts, 2): if len(test_set) >= t: break test_set.append(St.to_line(p1, p2)) return test_set
def gen(): for p1, p2 in itertools.combinations(net_sample, 2): l = to_line(p1, p2) r = sum(w for p, w in zip(red_points, red_weights) if l.pt_eq_below(p)) b = sum(w for p, w in zip(blue_points, blue_weights) if l.pt_eq_below(p)) yield stat(r / nr, b / nb), l
def __init__(self, pts): self.angle_orders = [] self.pts = pts self.lines = [] for i in range(len(pts) - 1): pt = pts[i] pt_order = pts[:i] + pts[(i + 1):] pt_order.sort(key=lambda x: order_function(pt, x)) angle_order = [] lines = [] for j in range(len(pt_order)): try: l = to_line(pt, pt_order[j]) except ZeroDivisionError: continue angle_order.append(order_function(pt, pt_order[j])) lines.append(l) self.angle_orders.append(angle_order) self.lines.append(lines)
def test_set_lines(pts, t): test_set = [] t = min(t, int(len(pts) / 2)) rand_pts = random.sample(pts, 2 * t) for p1, p2 in zip(rand_pts[:t], rand_pts[t:]): test_set.append(St.to_line(p1, p2)) return test_set # # import matplotlib.pyplot as plt # # pts = [(random.random(), random.random()) for i in range(10000)] # # lines = test_set_dual_exact_t(pts, 100) # print(len(lines)) # f, ax = plt.subplots() # for l in lines: # l.visualize(ax, -1, 1) # ax.set_xlim([-1, 1]) # ax.set_ylim([-1, 1]) # plt.show()
def random_test_set(pts, t, c=10): test_set = [] for i in range(int(t * t) * c): p1, p2 = random.sample(pts, 2) test_set.append(to_line(p1, p2)) return test_set
def line_discrepancy(net_sample, red_points, red_weights, blue_points, blue_weights, disc=stat): """ :param big_samp: :param small_samp: :param weights: :param pt_num: :param n: The sub-sampling size of the small sample. :return: """ #net_sample = random.sample(blue_points, n // 2) + random.sample(red_points, n // 2) max_discrepancy = -math.inf max_line = Line(0, 0) a = 0 b = 0 if red_weights: a = 1.0 / sum(red_weights) if blue_weights: b = 1.0 / sum(blue_weights) for i in range(len(net_sample) - 1): sample_part = net_sample[i + 1:] p_0 = net_sample[i] order_f = lambda x: order_function(p_0, x) red_delta = [0] * len(sample_part) blue_delta = [0] * len(sample_part) sample_part.sort(key=lambda x: order_f(x)) angles = [order_f(p) for p in sample_part] try: l1 = to_line(p_0, sample_part[0]) for p_1, w in zip(red_points, red_weights): insertion_pt = bisect.bisect_right(angles, order_f(p_1)) - 1 if insertion_pt == -1: pass elif l1.pt_eq_below_exact(p_1): red_delta[insertion_pt] += -w else: red_delta[insertion_pt] += w for p_1, w in zip(blue_points, blue_weights): insertion_pt = bisect.bisect_right(angles, order_f(p_1)) - 1 if insertion_pt == -1: pass elif l1.pt_eq_below_exact(p_1): blue_delta[insertion_pt] += -w else: blue_delta[insertion_pt] += w red_curr = sum(w for p, w in zip(red_points, red_weights) if l1.pt_eq_below_exact(p)) blue_curr = sum(w for p, w in zip(blue_points, blue_weights) if l1.pt_eq_below_exact(p)) # red_curr += red_delta[0] # blue_curr += blue_delta[0] for db, ds, p_1 in zip(red_delta, blue_delta, sample_part): if max_discrepancy <= disc(red_curr * a, blue_curr * b): max_line = to_line(p_0, p_1) max_discrepancy = disc(red_curr * a, blue_curr * b) red_curr += db blue_curr += ds except ZeroDivisionError: continue return max_discrepancy, max_line