Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_all_in_one_model(db_path, sampler):
models = [AllInOneModel() for _ in range(2)]
population_size = ConstantPopulationSize(800)
parameter_given_model_prior_distribution = [Distribution(theta=RV("beta",
1, 1))
for _ in range(2)]
abc = ABCSMC(models, parameter_given_model_prior_distribution,
MinMaxDistance(measures_to_use=["result"]),
population_size,
eps=MedianEpsilon(.1),
sampler=sampler)
abc.new(db_path, {"result": 2})
minimum_epsilon = .2
history = abc.run(minimum_epsilon, max_nr_populations=3)
mp = history.get_model_probabilities(history.max_t)
assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08
def model(args):
return {"result": st.binom(binomial_n, args['theta']).rvs()}
models = [model for _ in range(2)]
models = list(map(SimpleModel, models))
population_size = ConstantPopulationSize(800)
a1, b1 = 1, 1
a2, b2 = 10, 1
parameter_given_model_prior_distribution = [Distribution(theta=RV("beta",
a1, b1)),
Distribution(theta=RV("beta",
a2, b2))]
abc = ABCSMC(models, parameter_given_model_prior_distribution,
MinMaxDistance(measures_to_use=["result"]),
population_size,
eps=MedianEpsilon(.1),
sampler=sampler)
n1 = 2
abc.new(db_path, {"result": n1})
minimum_epsilon = .2
history = abc.run(minimum_epsilon, max_nr_populations=3)
mp = history.get_model_probabilities(history.max_t)
def B(a, b):
return gamma(a) * gamma(b) / gamma(a + b)
def expected_p(a, b, n1):
return binom(binomial_n, n1) * B(a + n1, b + binomial_n - n1) / B(a, b)
p1_expected_unnormalized = expected_p(a1, b1, n1)
p2_expected_unnormalized = expected_p(a2, b2, n1)
def test_all_in_one_model(db_path, sampler):
models = [AllInOneModel() for _ in range(2)]
model_prior = RV("randint", 0, 2)
population_size = ConstantPopulationStrategy(800, 3)
parameter_given_model_prior_distribution = [Distribution(theta=RV("beta", 1, 1))
for _ in range(2)]
parameter_perturbation_kernels = [MultivariateNormalTransition() for _ in range(2)]
abc = ABCSMC(models, model_prior, ModelPerturbationKernel(2, probability_to_stay=.8),
parameter_given_model_prior_distribution, parameter_perturbation_kernels,
MinMaxDistanceFunction(measures_to_use=["result"]), MedianEpsilon(.1),
population_size,
sampler=sampler)
options = {'db_path': db_path}
abc.set_data({"result": 2}, 0, {}, options)
minimum_epsilon = .2
history = abc.run(minimum_epsilon)
mp = history.get_model_probabilities(history.max_t)
assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08
models = list(map(SimpleModel, models))
# However, our models' priors are not the same. Their mean differs.
mu_x_1, mu_x_2 = 0, 1
parameter_given_model_prior_distribution = [
Distribution(x=RV("norm", mu_x_1, sigma)),
Distribution(x=RV("norm", mu_x_2, sigma))
]
# We plug all the ABC setup together
nr_populations = 2
pop_size = ConstantPopulationSize(23, nr_samples_per_parameter=n_sim)
abc = ABCSMC(models, parameter_given_model_prior_distribution,
PercentileDistance(measures_to_use=["y"]),
pop_size,
eps=MedianEpsilon(),
sampler=sampler)
# Finally we add meta data such as model names and
# define where to store the results
# y_observed is the important piece here: our actual observation.
y_observed = 1
abc.new(db_path, {"y": y_observed})
# We run the ABC with 3 populations max
minimum_epsilon = .05
history = abc.run(minimum_epsilon, max_nr_populations=nr_populations)
# Evaluate the model probabililties
mp = history.get_model_probabilities(history.max_t)
def p_y_given_model(mu_x_model):
def test_beta_binomial_two_identical_models(db_path, sampler):
binomial_n = 5
def model_fun(args):
return {"result": st.binom(binomial_n, args.theta).rvs()}
models = [model_fun for _ in range(2)]
models = list(map(SimpleModel, models))
population_size = ConstantPopulationSize(800)
parameter_given_model_prior_distribution = [Distribution(theta=st.beta(
1, 1))
for _ in range(2)]
abc = ABCSMC(models, parameter_given_model_prior_distribution,
MinMaxDistance(measures_to_use=["result"]),
population_size,
eps=MedianEpsilon(.1),
sampler=sampler)
abc.new(db_path, {"result": 2})
minimum_epsilon = .2
history = abc.run(minimum_epsilon, max_nr_populations=3)
mp = history.get_model_probabilities(history.max_t)
assert abs(mp.p[0] - .5) + abs(mp.p[1] - .5) < .08
model_prior = pyabc.RV("randint", 0, 1)
population_size = pyabc.AdaptivePopulationStrategy(500, 20,
max_population_size=10000)
mapper = parallel.SGE().map if parallel.sge_available() else map
abc = pyabc.ABCSMC([pyabc.SimpleModel(abc_model)],
model_prior,
pyabc.ModelPerturbationKernel(1, probability_to_stay=.8),
[ABCPrior()],
[pyabc.MultivariateNormalTransition()],
pyabc.PercentileDistanceFunction(measures_to_use=["x", "y"]),
pyabc.MedianEpsilon(),
population_size,
sampler=parallel.sampler.MappingSampler(map=mapper))
abc.stop_if_only_single_model_alive = False
options = {'db_path': "sqlite:///" + sm.output[0]}
abc.set_data({"x": 1, "y": 1}, 0, {}, options)
history = abc.run(.01)