def setup_module(): """Run an analysis and create a database. Called once at the beginning. """ def model(p): return { 'ss0': p['p0'] + 0.1 * np.random.uniform(), 'ss1': p['p1'] + 0.1 * np.random.uniform(), } p_true = {'p0': 3, 'p1': 4} observation = {'ss0': p_true['p0'], 'ss1': p_true['p1']} limits = {'p0': (0, 5), 'p1': (1, 8)} prior = pyabc.Distribution( **{ key: pyabc.RV('uniform', limits[key][0], limits[key][1] - limits[key][0]) for key in p_true.keys() }) distance = pyabc.PNormDistance(p=2) abc = pyabc.ABCSMC(model, prior, distance, population_size=50) abc.new(db_path, observation) abc.run(minimum_epsilon=0.1, max_nr_populations=4)
def setup_module(): """Set up module. Called before all tests here.""" # create and run some model observation = {'ss0': p_true['p0'], 'ss1': p_true['p1']} prior = pyabc.Distribution( **{ key: pyabc.RV('uniform', limits[key][0], limits[key][1] - limits[key][0]) for key in p_true.keys() }) distance = pyabc.PNormDistance(p=2) n_history = 2 sampler = pyabc.sampler.MulticoreEvalParallelSampler(n_procs=2) for _ in range(n_history): abc = pyabc.ABCSMC(model, prior, distance, population_size=100, sampler=sampler) abc.new(db_path, observation) abc.run(minimum_epsilon=.1, max_nr_populations=3) for j in range(n_history): history = pyabc.History(db_path) history.id = j + 1 histories.append(history) labels.append("Some run " + str(j))
def test_export(): """Test database export. Just calls export and does some very basic checks. """ # simple problem def model(p): return {"y": p["p"] + 0.1 * np.random.normal()} prior = pyabc.Distribution(p=pyabc.RV("uniform", -1, 2)) distance = pyabc.PNormDistance() try: # run db_file = tempfile.mkstemp(suffix=".db")[1] abc = pyabc.ABCSMC(model, prior, distance, population_size=100) abc.new("sqlite:///" + db_file, model({"p": 0})) abc.run(max_nr_populations=3) # export history for fmt in ["csv", "json", "html", "stata"]: out_file = tempfile.mkstemp()[1] try: pyabc.storage.export(db_file, out=out_file, out_format=fmt) assert os.path.exists(out_file) assert os.stat(out_file).st_size > 0 finally: if os.path.exists(out_file): os.remove(out_file) finally: if os.path.exists(db_file): os.remove(db_file)
def test_pipeline(db_file): """Test whole pipeline using a learned summary statistic.""" rng = np.random.Generator(np.random.PCG64(0)) def model(p): return {"s0": p["p0"] + 1e-2 * rng.normal(size=2), "s1": rng.normal()} prior = pyabc.Distribution(p0=pyabc.RV("uniform", 0, 1)) distance = pyabc.AdaptivePNormDistance(sumstat=PredictorSumstat( LinearPredictor(), fit_ixs={1, 3}), ) data = {"s0": np.array([0.1, 0.105]), "s1": 0.5} # run a little analysis abc = pyabc.ABCSMC(model, prior, distance, population_size=100) abc.new("sqlite:///" + db_file, data) h = abc.run(max_total_nr_simulations=1000) # first iteration df0, w0 = h.get_distribution(t=0) off0 = abs(pyabc.weighted_mean(df0.p0, w0) - 0.1) # last iteration df, w = h.get_distribution() off = abs(pyabc.weighted_mean(df.p0, w) - 0.1) assert off0 > off # alternative run with simple distance distance = pyabc.PNormDistance() abc = pyabc.ABCSMC(model, prior, distance, population_size=100) abc.new("sqlite:///" + db_file, data) h = abc.run(max_total_nr_simulations=1000) df_comp, w_comp = h.get_distribution() off_comp = abs(pyabc.weighted_mean(df_comp.p0, w_comp) - 0.1) assert off_comp > off # alternative run with info weighting distance = pyabc.InfoWeightedPNormDistance( predictor=LinearPredictor(), fit_info_ixs={1, 3}, ) abc = pyabc.ABCSMC(model, prior, distance, population_size=100) abc.new("sqlite:///" + db_file, data) h = abc.run(max_total_nr_simulations=1000) df_info, w_info = h.get_distribution() off_info = abs(pyabc.weighted_mean(df_info.p0, w_info) - 0.1) assert off_comp > off_info
para_prior3 = pyabc.Distribution( lambda_n=pyabc.RV(prior_distribution, lim.lb, lim.interval_length), 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,
kMB=pyabc.RV("uniform", lim2.lb, lim2.interval_length), muM=pyabc.RV("uniform", lim2.lb, lim2.interval_length), sBN=pyabc.RV("uniform", lim3.lb, lim3.interval_length), iBM=pyabc.RV("uniform", lim3.lb, lim3.interval_length), muB=pyabc.RV("uniform", lim3.lb, lim3.interval_length), sAM=pyabc.RV("uniform", lim.lb, lim.interval_length), muA=pyabc.RV("uniform", lim.lb, lim.interval_length) ) # %% 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, weights=None, 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()
vNM=pyabc.RV("uniform", lim2.lb, lim2.interval_length), lambdaM=pyabc.RV("uniform", lim3.lb, lim3.interval_length), kMB=pyabc.RV("uniform", lim2.lb, lim2.interval_length), muM=pyabc.RV("uniform", lim2.lb, lim2.interval_length), sBN=pyabc.RV("uniform", lim3.lb, lim3.interval_length), iBM=pyabc.RV("uniform", lim3.lb, lim3.interval_length), muB=pyabc.RV("uniform", lim3.lb, lim3.interval_length), sAM=pyabc.RV("uniform", lim.lb, lim.interval_length), muA=pyabc.RV("uniform", lim.lb, lim.interval_length)) # %% Define ABC-SMC model # distanceP2_adaptive = pyabc.AdaptivePNormDistance(p=2, # scale_function=pyabc.distance.root_mean_square_deviation # ) distanceP2 = pyabc.PNormDistance(p=2) # 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() 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) sampler0 = pyabc.sampler.MulticoreEvalParallelSampler(n_procs=48)
def get_distance(self): """ Distance to use for deterministic acceptance. """ return pyabc.PNormDistance(p=2)
def get_distance(self): return pyabc.PNormDistance(p=2)