def rejection(model, condition, query, num_samples): samples = [] for _ in range(int(num_samples / 100)): samples += mapp(lambda _: single_rejection(model, condition, query), range(100)) if num_samples % 100: samples += mapp(lambda _: single_rejection(model, condition, query), range(num_samples % 100)) return samples
def test_hierarchical(): ELEMENTS_PER_BAG = 10 observed_samples = { 0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 10, 6: 10, 7: 10, 8: 10, 9: 10 } def model(init_world, tick, data=observed_samples): world = init_world.copy() num_bag_types = church_geometric(world, "num_bag_types", tick, .4) bag_ps = [church_uniform(world, "bag_type_p_%i" % i, tick, 0, 1) for i in range(num_bag_types)] bag_samples = {} for bag in data.keys(): bag_p = church_listdraw(world, "bag_p_%i" % bag, tick, bag_ps) bag_k = church_binomial_fixed(world, "bag_k_%i" % bag, tick, ELEMENTS_PER_BAG, bag_p, fixed_val=data[bag]) bag_samples[bag] = bag_k return (world, {"num_bag_types": num_bag_types, "bag_samples": bag_samples}) def condition(val, data=observed_samples): return val["bag_samples"] == data def query(val, data=observed_samples): return val["num_bag_types"] def getdata(num_clusters): data = {} for i in range(10): data[i] = ((i % num_clusters) + 1) * 20 return data random_seed(10) def runmcmc(): mcmc_results = mcmc(model, condition, query, num_steps=100, runtime=5 * 60) mcmc_samples = mcmc_results["samples"] return ("mcmc", len(mcmc_samples), histogram(mcmc_samples)) all_results = mapp(lambda f: f(), [runmcmc] * 5) for result in all_results: print result
def parallel(self): def parfor(i): self.values[i] = i mapp(parfor, range(self.values.size))
def test_hierarchical(): ELEMENTS_PER_BAG = 10 observed_samples = { 0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 10, 6: 10, 7: 10, 8: 10, 9: 10 } def model(init_world, tick, data=observed_samples): world = init_world.copy() num_bag_types = church_geometric(world, "num_bag_types", tick, .4) bag_ps = [ church_uniform(world, "bag_type_p_%i" % i, tick, 0, 1) for i in range(num_bag_types) ] bag_samples = {} for bag in data.keys(): bag_p = church_listdraw(world, "bag_p_%i" % bag, tick, bag_ps) bag_k = church_binomial_fixed(world, "bag_k_%i" % bag, tick, ELEMENTS_PER_BAG, bag_p, fixed_val=data[bag]) bag_samples[bag] = bag_k return (world, { "num_bag_types": num_bag_types, "bag_samples": bag_samples }) def condition(val, data=observed_samples): return val["bag_samples"] == data def query(val, data=observed_samples): return val["num_bag_types"] def getdata(num_clusters): data = {} for i in range(10): data[i] = ((i % num_clusters) + 1) * 20 return data random_seed(10) def runmcmc(): mcmc_results = mcmc(model, condition, query, num_steps=100, runtime=5 * 60) mcmc_samples = mcmc_results["samples"] return ("mcmc", len(mcmc_samples), histogram(mcmc_samples)) all_results = mapp(lambda f: f(), [runmcmc] * 5) for result in all_results: print result