def test_uniform(): d = Uniform(5, 1000, seed=1) assert next(d) == 142 assert next(d) == 587 d = Uniform(5, 1000, seed=1) for x in d: assert x == 142 break
def __init__(self, db, dic, parent_conn, src_dic, ent_size, tgt_dic=None, mode='local'): ''' db : db name dic : the relation dict of the config parent_conn: server conn 1, rel data 2, src nodes: n 3, tgt nodes: m src_dic : src node config dict ent_size: the number of nodes of each entity tgt_dic : tgt node config dict if is None, then tgt_dic == src_dic, e.g. user -> user mode : local (default), dump edge locally : u-level, dump edge and node in parent process ''' self.parent_conn = parent_conn self.rel_dict = dic self.db_name = db self.src_dic = src_dic self.tgt_dic = tgt_dic self.mode = mode self.ent_size = ent_size self.db_handler = Store(db) self.db_handler.connect() # parameters of in/out distributions # TODO: other type of distribution if dic['in']['type'] == 'power_law': mind, maxd, midd = 0, -1, -1 if 'min_degree' in dic['in']: mind = dic['in']['min_degree'] if 'max_degree' in dic['in']: maxd = dic['in']['max_degree'] if 'mid_degree' in dic['in']: midd = dic['in']['mid_degree'] if isinstance(dic['in']['lambd'], list): self.indistr_ins = PWL(0, dic['in']['lambd'][0], dic['in']['lambd'][1], mind, maxd, midd) else: self.indistr_ins = PWL(0, dic['in']['lambd'], min_degree=mind, max_degree=maxd, mid_degree=midd) elif dic['in']['type'] == 'uniform': self.indistr_ins = Uniform(0, dic['in']['min'], dic['in']['max']) if dic['out']['type'] == 'power_law': mind, maxd, midd = 0, -1, -1 if 'min_degree' in dic['out']: mind = dic['out']['min_degree'] if 'max_degree' in dic['out']: maxd = dic['out']['max_degree'] if 'mid_degree' in dic['out']: midd = dic['out']['mid_degree'] if isinstance(dic['out']['lambd'], list): self.outdistr_ins = PWL(0, dic['out']['lambd'][0], dic['out']['lambd'][1], mind, maxd, midd) else: self.outdistr_ins = PWL(0, dic['out']['lambd'], min_degree=mind, max_degree=maxd, mid_degree=midd) elif dic['out']['type'] = 'uniform': self.outdistr_ins = Uniform(0, dic['out']['min'], dic['out']['max'])
def main(): sample_size = 5000 delta = lambda x: chi2.ppf(0.97, x ** 2 - 1) r = 2 s = 2 U = RandomVar(Uniform(-np.pi, np.pi)) x = RandomVar.operation(U, np.cos) y = RandomVar.operation(U, np.sin) xy = Joint(x, y) xy_sample = xy.sample(sample_size) # Adaptive algorithm adaptive_algorithm = AdaptiveAlgorithm(xy_sample, delta, r, s) non_adaptive_part = NonAdaptivePartition(xy_sample, bins=[50, 50]).run() final_partition = adaptive_algorithm.run() print(len(non_adaptive_part)) print(len(final_partition)) fig, ax = plt.subplots() adaptive_algorithm.plot_data(ax, color='red', alpha=0.5, markersize=2) adaptive_algorithm.plot_partition(ax, final_partition) ax.set_xlabel("X") ax.set_ylabel("Y") ax.set_title("Adaptive Partition and samples") plt.show()
def test_triangle(): """ Testing simple sum of simple distributions """ d1 = Triangle(0, 4, 1) d2 = Triangle(10, 18, 11) d1 = Uniform(1, 2) d2 = Uniform(4, 6) T1, f1 = d1.pmf(100) T2, f2 = d2.pmf(100) plt.plot(T1, f1, color='r') plt.plot(T2, f2, color='b') T1, F1 = d1.cmf(100) T2, F2 = d2.cmf(100) plt.plot(T1, F1, color='g') plt.plot(T2, F2, color='c') opsum = SumOp() T, f_T = opsum.sum_num(d1, d2, 100).pmf() plt.plot(T, f_T, color='black', ls='-') plt.savefig("triangle-dist.pdf")
def get_distribution(arguments, keys): if arguments['--zipf'] is not None: print("Using alpha", float(arguments['--zipf'])) distribution = Zipf(keys, float(arguments['--zipf'])) elif arguments['--uniform']: distribution = Uniform(keys) elif arguments['--linear'] is not None: distribution = Linear(keys, float(arguments['--linear'])) else: distribution = Zipf(keys, 1) return distribution
def test_operation(self): u1 = Uniform(1, 2) m1 = Distribution.operation(u1, np.sin) self.seed() ground_truth = np.sin(u1.sample(10)) self.seed() np.testing.assert_almost_equal(m1.sample(10), ground_truth) m1 = Distribution.operation(u1, np.cos) self.seed() ground_truth = np.cos(u1.sample(10)) self.seed() np.testing.assert_almost_equal(m1.sample(10), ground_truth) m1 = Distribution.operation(u1, np.square) self.seed() ground_truth = np.square(u1.sample(10)) self.seed() np.testing.assert_almost_equal(m1.sample(10), ground_truth) cube = lambda x: np.power(x, 3) m1 = Distribution.operation(u1, cube) self.seed() ground_truth = cube(u1.sample(10)) self.seed() np.testing.assert_almost_equal(m1.sample(10), ground_truth)
def test_sum(self): u1 = Uniform(1, 2) u2 = Uniform(4, 5) m1 = Distribution.sum(u1, u2) self.seed() ground_truth = np.sum([u1.sample(10), u2.sample(10)], axis=0) self.seed() np.testing.assert_almost_equal(m1.sample(10), ground_truth)
def test_sample(self): u1 = Uniform(1, 2) u1.sample(5) u1.sample(10)