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_grid_search_multivariate_normal():
m = MultivariateNormalTransition()
m_grid = GridSearchCV(m, {"scaling": np.logspace(-5, 1.5, 5)}, n_jobs=1)
df, w = data(20)
m_grid.fit(df, w)
def test_gaussian_multiple_populations_adpative_population_size(db_path, sampler):
sigma_x = 1
sigma_y = .5
y_observed = 2
def model(args):
return {"y": st.norm(args['x'], sigma_y).rvs()}
models = [model]
models = list(map(SimpleModel, models))
model_prior = RV("randint", 0, 1)
nr_populations = 4
population_size = AdaptivePopulationStrategy(600, nr_populations)
parameter_given_model_prior_distribution = [Distribution(x=RV("norm", 0, sigma_x))]
parameter_perturbation_kernels = [MultivariateNormalTransition()]
abc = ABCSMC(models, model_prior, ModelPerturbationKernel(1, probability_to_stay=1),
parameter_given_model_prior_distribution, parameter_perturbation_kernels,
PercentileDistanceFunction(measures_to_use=["y"]), MedianEpsilon(.2),
population_size,
sampler=sampler)
options = {'db_path': db_path}
abc.set_data({"y": y_observed}, 0, {}, options)
minimum_epsilon = -1
abc.do_not_stop_when_only_single_model_alive()
history = abc.run(minimum_epsilon)
posterior_x, posterior_weight = history.get_results_distribution(0, "x")
sort_indices = sp.argsort(posterior_x)
f_empirical = sp.interpolate.interp1d(sp.hstack((-200, posterior_x[sort_indices], 200)),
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))
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
@pytest.fixture(params=[LocalTransition, MultivariateNormalTransition])
def transition(request):
return request.param
def test_gaussian_multiple_populations(db_path, sampler):
sigma_x = 1
sigma_y = .5
y_observed = 2
def model(args):
return {"y": st.norm(args['x'], sigma_y).rvs()}
models = [model]
models = list(map(SimpleModel, models))
model_prior = RV("randint", 0, 1)
nr_populations = 4
population_size = ConstantPopulationStrategy(600, nr_populations)
parameter_given_model_prior_distribution = [Distribution(x=RV("norm", 0, sigma_x))]
parameter_perturbation_kernels = [MultivariateNormalTransition()]
abc = ABCSMC(models, model_prior, ModelPerturbationKernel(1, probability_to_stay=1),
parameter_given_model_prior_distribution, parameter_perturbation_kernels,
PercentileDistanceFunction(measures_to_use=["y"]), MedianEpsilon(.2),
population_size,
sampler=sampler)
options = {'db_path': db_path}
abc.set_data({"y": y_observed}, 0, {}, options)
minimum_epsilon = -1
abc.do_not_stop_when_only_single_model_alive()
history = abc.run(minimum_epsilon)
posterior_x, posterior_weight = history.get_results_distribution(0, "x")
sort_indices = sp.argsort(posterior_x)
f_empirical = sp.interpolate.interp1d(sp.hstack((-200, posterior_x[sort_indices], 200)),
def test_beta_binomial_different_priors(db_path, sampler):
binomial_n = 5
def model(args):
return {"result": st.binom(binomial_n, args['theta']).rvs()}
models = [model for _ in range(2)]
models = list(map(SimpleModel, models))
model_prior = RV("randint", 0, 2)
population_size = ConstantPopulationStrategy(800, 3)
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))]
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}
n1 = 2
abc.set_data({"result": n1}, 0, {}, options)
minimum_epsilon = .2
history = abc.run(minimum_epsilon)
mp = history.get_model_probabilities(history.max_t)
def B(a, b):
return gamma(a) * gamma(b) / gamma(a + b)
def test_continuous_non_gaussian(db_path, sampler):
def model(args):
return {"result": sp.rand() * args['u']}
models = [model]
models = list(map(SimpleModel, models))
model_prior = RV("randint", 0, 1)
population_size = ConstantPopulationStrategy(250, 2)
parameter_given_model_prior_distribution = [Distribution(u=RV("uniform", 0, 1))]
parameter_perturbation_kernels = [MultivariateNormalTransition()]
abc = ABCSMC(models, model_prior, ModelPerturbationKernel(1, probability_to_stay=1),
parameter_given_model_prior_distribution, parameter_perturbation_kernels,
PercentileDistanceFunction(measures_to_use=["result"]), MedianEpsilon(.2),
population_size,
sampler=sampler)
options = {'db_path': db_path}
d_observed = .5
abc.set_data({"result": d_observed}, 0, {}, options)
abc.do_not_stop_when_only_single_model_alive()
minimum_epsilon = -1
history = abc.run(minimum_epsilon)
posterior_x, posterior_weight = history.get_results_distribution(0, "u")
sort_indices = sp.argsort(posterior_x)
f_empirical = sp.interpolate.interp1d(sp.hstack((-200, posterior_x[sort_indices], 200)),
def test_beta_binomial_two_identical_models_adaptive(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))
model_prior = RV("randint", 0, 2)
population_size = AdaptivePopulationStrategy(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
@pytest.fixture(params=[LocalTransition, MultivariateNormalTransition])
def transition(request):
return request.param()
rate = pars.rate
arr = sp.rand(4)
return arr
def distance(x, y):
return ((x - y)**2).sum()
mapper = parallel.SGE().map if parallel.sge_available() else map
abc = pyabc.ABCSMC([pyabc.SimpleModel(model_1),
pyabc.SimpleModel(model_2)],
model_prior,
pyabc.ModelPerturbationKernel(2, probability_to_stay=.8),
[rate_prior, rate_prior],
[pyabc.MultivariateNormalTransition(),
pyabc.MultivariateNormalTransition()],
distance,
pyabc.MedianEpsilon(),
population_strategy,
sampler=parallel.sampler.MappingSampler(map=mapper))
abc.stop_if_only_single_model_alive = False
options = {'db_path': "sqlite:///" + sm.output[0]}
abc.set_data(sp.rand(4), 0, {}, options)
history = abc.run(.01)