def test_redis_look_ahead_error(): """Test whether the look-ahead mode fails as expected.""" model, prior, distance, obs = basic_testcase() with tempfile.NamedTemporaryFile(mode='w', suffix='.csv') as fh: sampler = RedisEvalParallelSamplerServerStarter( look_ahead=True, look_ahead_delay_evaluation=False, log_file=fh.name) args_list = [{ 'eps': pyabc.MedianEpsilon() }, { 'distance_function': pyabc.AdaptivePNormDistance() }] for args in args_list: if 'distance_function' not in args: args['distance_function'] = distance try: with pytest.raises(AssertionError) as e: abc = pyabc.ABCSMC(model, prior, sampler=sampler, population_size=10, **args) abc.new(pyabc.create_sqlite_db_id(), obs) abc.run(max_nr_populations=3) assert "cannot be used in look-ahead mode" in str(e.value) finally: sampler.shutdown()
def test_early_stopping(): """Basic test whether an early stopping pipeline works. Heavily inspired by the `early_stopping` notebook. """ prior = pyabc.Distribution(step_size=pyabc.RV("uniform", 0, 10)) n_steps = 30 gt_step_size = 5 gt_trajectory = simulate(n_steps, gt_step_size) model = MyStochasticProcess(n_steps=n_steps, gt_step_size=gt_step_size, gt_trajectory=gt_trajectory) abc = pyabc.ABCSMC( models=model, parameter_priors=prior, distance_function=pyabc.NoDistance(), population_size=30, transitions=pyabc.LocalTransition(k_fraction=0.2), eps=pyabc.MedianEpsilon(300, median_multiplier=0.7), ) # initializing eps manually is necessary as we only have an integrated # model # TODO automatically iniitalizing would be possible, e.g. using eps = inf abc.new(pyabc.create_sqlite_db_id()) abc.run(max_nr_populations=3)
def __init__(self, get_acceptor=None, get_transition=None, get_eps=None, n_acc: int = 1000, n_pop: int = 20, eps_min: float = 0.0, min_acc_rate: float = 0.0, id_=None): if get_acceptor is None: get_acceptor = lambda: pyabc.UniformAcceptor() self.get_acceptor = get_acceptor if get_transition is None: get_transition = lambda: pyabc.MultivariateNormalTransition() self.get_transition = get_transition if get_eps is None: get_eps = lambda: pyabc.MedianEpsilon() self.get_eps = get_eps self.n_acc = n_acc self.n_pop = n_pop self.eps_min = eps_min self.min_acc_rate = min_acc_rate self.id = id_
a=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), k_n_beta=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_n=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), v_n_phi=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), k_phi_beta=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_phi=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), s_beta_n=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_beta=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), s_alpha_phi=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_alpha=pyabc.RV(prior_distribution, lim.lb, lim.interval_length)) # %% Define ABC-SMC model distanceP2 = pyabc.PNormDistance(p=2) # , factors=factors) eps0 = pyabc.MedianEpsilon(60) # eps_fixed = pyabc.epsilon.ListEpsilon([50, 46, 43, 40, 37, 34, 31, 29, 27, 25, # 23, 21, 19, 17, 15, 14, 13, 12, 11, 10]) # transition0 = pyabc.transition.LocalTransition(k=50, k_fraction=None) # sampler0 = pyabc.sampler.MulticoreEvalParallelSampler(n_procs=48) # set model and prior abc = pyabc.ABCSMC( models=solver.ode_model3, parameter_priors=para_prior3, population_size=2000, # sampler=sampler0, distance_function=distanceP2, eps=eps0,
# %% Define ABC-SMC model # distanceP2_adpt = pyabc.AdaptivePNormDistance(p=2, # scale_function=pyabc.distance.root_mean_square_deviation # # factors=factors # ) distanceP2 = pyabc.PNormDistance(p=2)#, factors=factors) # kernel1 = pyabc.IndependentNormalKernel(var=1.0 ** 2) # Measure distance and set it as minimum epsilon # min_eps = distanceP2(obs_data_noisy, obs_data_raw) # acceptor1 = pyabc.StochasticAcceptor() # acceptor_adpt = pyabc.UniformAcceptor(use_complete_history=True) eps0 = pyabc.MedianEpsilon(50) # eps1 = pyabc.Temperature() # eps_fixed = pyabc.epsilon.ListEpsilon([50, 46, 43, 40, 37, 34, 31, 29, 27, 25, # 23, 21, 19, 17, 15, 14, 13, 12, 11, 10]) # transition0 = pyabc.transition.LocalTransition(k=50, k_fraction=None) # transition1 = pyabc.transition.GridSearchCV() # sampler0 = pyabc.sampler.MulticoreEvalParallelSampler(n_procs=1) abc = pyabc.ABCSMC(models=solver.ode_model1, parameter_priors=para_prior1, # acceptor=acceptor_adpt, population_size=2000, # sampler=sampler0, distance_function=distanceP2,
a=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), k_n_beta=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_n=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), v_n_phi=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), k_phi_beta=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_phi=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), s_beta_n=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_beta=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), s_alpha_phi=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), mu_alpha=pyabc.RV(prior_distribution, lim.lb, lim.interval_length)) # %% Define ABC-SMC model distanceP2 = pyabc.PNormDistance(p=2) # , factors=factors) eps0 = pyabc.MedianEpsilon(100) # eps_fixed = pyabc.epsilon.ListEpsilon([50, 46, 43, 40, 37, 34, 31, 29, 27, 25, # 23, 21, 19, 17, 15, 14, 13, 12, 11, 10]) # transition0 = pyabc.transition.LocalTransition(k=50, k_fraction=None) # sampler0 = pyabc.sampler.MulticoreEvalParallelSampler(n_procs=48) abc = pyabc.ABCSMC( models=solver.ode_model2, parameter_priors=para_prior2, population_size=2000, # sampler=sampler0, distance_function=distanceP2, eps=eps0, )
def two_competing_gaussians_multiple_population(db_path, sampler, n_sim): # Define a gaussian model sigma = .5 def model(args): return {"y": st.norm(args['x'], sigma).rvs()} # We define two models, but they are identical so far models = [model, model] models = list(map(pyabc.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 = [ pyabc.Distribution(x=pyabc.RV("norm", mu_x_1, sigma)), pyabc.Distribution(x=pyabc.RV("norm", mu_x_2, sigma)), ] # We plug all the ABC setups together nr_populations = 2 pop_size = pyabc.ConstantPopulationSize(23, nr_samples_per_parameter=n_sim) abc = pyabc.ABCSMC(models, parameter_given_model_prior_distribution, pyabc.PercentileDistance(measures_to_use=["y"]), pop_size, eps=pyabc.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 probabilities mp = history.get_model_probabilities(history.max_t) def p_y_given_model(mu_x_model): res = st.norm(mu_x_model, np.sqrt(sigma**2 + sigma**2)).pdf(y_observed) return res p1_expected_unnormalized = p_y_given_model(mu_x_1) p2_expected_unnormalized = p_y_given_model(mu_x_2) p1_expected = p1_expected_unnormalized / (p1_expected_unnormalized + p2_expected_unnormalized) p2_expected = p2_expected_unnormalized / (p1_expected_unnormalized + p2_expected_unnormalized) assert history.max_t == nr_populations - 1 # the next line only tests if we obtain correct numerical types try: mp0 = mp.p[0] except KeyError: mp0 = 0 try: mp1 = mp.p[1] except KeyError: mp1 = 0 assert abs(mp0 - p1_expected) + abs(mp1 - p2_expected) < np.inf # check that sampler only did nr_particles samples in first round pops = history.get_all_populations() # since we had calibration (of epsilon), check that was saved pre_evals = pops[pops['t'] == pyabc.History.PRE_TIME]['samples'].values assert pre_evals >= pop_size.nr_particles # our samplers should not have overhead in calibration, except batching batch_size = sampler.batch_size if hasattr(sampler, 'batch_size') else 1 max_expected = pop_size.nr_particles + batch_size - 1 if pre_evals > max_expected: # Violations have been observed occasionally for the redis server # due to runtime conditions with the increase of the evaluations # counter. This could be overcome, but as it usually only happens # for low-runtime models, this should not be a problem. Thus, only # print a warning here. logger.warning( f"Had {pre_evals} simulations in the calibration iteration, " f"but a maximum of {max_expected} would have been sufficient for " f"the population size of {pop_size.nr_particles}.")
def test_medianepsilon(): # functionality already covered by quantile epsilon tests eps = pyabc.MedianEpsilon() assert np.isclose(eps.alpha, 0.5)