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_resume(db_path, gt_model):
def model(parameter):
return {"data": parameter["mean"] + np.random.randn()}
prior = Distribution(mean=RV("uniform", 0, 5))
def distance(x, y):
x_data = x["data"]
y_data = y["data"]
return abs(x_data - y_data)
abc = ABCSMC(model, prior, distance, population_size=10)
history = abc.new(db_path, {"data": 2.5}, gt_model=gt_model)
run_id = history.id
print("Run ID:", run_id)
hist_new = abc.run(minimum_epsilon=0, max_nr_populations=1)
assert hist_new.n_populations == 1
abc_continued = ABCSMC(model, prior, distance)
run_id_continued = abc_continued.load(db_path, run_id)
print("Run ID continued:", run_id_continued)
hist_contd = abc_continued.run(minimum_epsilon=0, max_nr_populations=1)
assert hist_contd.n_populations == 2
assert hist_new.n_populations == 2
distance = r.distance("myDistance")
sum_stat = r.summary_statistics("mySummaryStatistics")
data = r.observation("mySumStatData")
prior = pyabc.Distribution(meanX=pyabc.RV("uniform", 0, 10),
meanY=pyabc.RV("uniform", 0, 10))
abc = pyabc.ABCSMC(model, prior, distance,
summary_statistics=sum_stat,
sampler=sampler, population_size=5)
db = pyabc.create_sqlite_db_id(file_="test_external.db")
abc.new(db, data)
history = abc.run(minimum_epsilon=0.9, max_nr_populations=2)
history.get_weighted_sum_stats_for_model(m=0, t=1)[1][0]["cars"].head()
# try load
id_ = history.id
abc = pyabc.ABCSMC(model, prior, distance,
summary_statistics=sum_stat,
sampler=sampler, population_size=6)
# shan't even need to pass the observed data again
abc.load(db, id_)
abc.run(minimum_epsilon=0.1, max_nr_populations=2)
def test_reference_parameter():
def model(parameter):
return {"data": parameter["mean"] + 0.5 * np.random.randn()}
prior = pyabc.Distribution(p0=pyabc.RV("uniform", 0, 5),
p1=pyabc.RV("uniform", 0, 1))
def distance(x, y):
return abs(x["data"] - y["data"])
abc = pyabc.ABCSMC(model, prior, distance, population_size=2)
db_path = ("sqlite:///" +
os.path.join(tempfile.gettempdir(), "test.db"))
observation = 2.5
gt_par = {'p0': 1, 'p1': 0.25}
abc.new(db_path, {"data": observation}, gt_par=gt_par)
history = abc.history
par_from_history = history.get_ground_truth_parameter()
assert par_from_history == gt_par
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)),
sp.hstack((0, sp.cumsum(posterior_weight[sort_indices]), 1)))
def test_stop_acceptance_rate_too_low(db_path):
abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, 10)
abc.new(db_path, {"par": .5})
history = abc.run(-1, 8, min_acceptance_rate=set_acc_rate)
df = history.get_all_populations()
df["acceptance_rate"] = df["particles"] / df["samples"]
assert df["acceptance_rate"].iloc[-1] < set_acc_rate
assert df["acceptance_rate"].iloc[-2] >= set_acc_rate
# We define two models, but they are identical so far
models = [model, model]
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
# We define two models, but they are identical so far
models = [model, model]
# However, our models' priors are not the same. Their mean differs.
mu_x_1, mu_x_2 = 0, 1
parameter_priors = [
Distribution(x=RV("norm", mu_x_1, sigma)),
Distribution(x=RV("norm", mu_x_2, sigma))
]
# We plug all the ABC setup together with 2 populations maximum
population_strategy = ConstantPopulationStrategy(100, 2)
abc = ABCSMC(models, parameter_priors,
PercentileDistanceFunction(measures_to_use=["y"]),
population_strategy)
# Finally we add meta data such as model names
# and define where to store the results
db_path = ("sqlite:///" +
os.path.join(tempfile.gettempdir(), "test.db"))
# y_observed is the important piece here: our actual observation.
y_observed = 1
abc.set_data({"y": y_observed}, db_path)
# We run the ABC
minimum_epsilon = .05
history = abc.run(minimum_epsilon)
# Evaluate the model probabililties
return prior(sp.array([x.theta1, x.theta2]))
def rvs(self):
sample = sp.rand(2) * 2 * MAX_SIZE - MAX_SIZE
return pyabc.Parameter({"theta1": sample[0], "theta2": sample[1]})
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)