Package: sbibm
Simulation-Based Inference BenchmarkThis repository contains a simulation-based inference benchmark framework,sbibm, which we describe in theassociated manuscript "Benchmarking Simulation-based Inference". A short summary of the paper and interactive results can be found on the project website:https://sbi-benchmark.github.ioThe benchmark framework includes tasks, reference posteriors, metrics, plotting, and integrations with SBI toolboxes. The framework is designed to be highly extensible and easily used in new research projects as we show below.In order to emphasize thatsbibmcan be used independently of any particular analysis pipeline, we split the code for reproducing the experiments of the manuscript into a seperate repository hosted atgithub.com/sbi-benchmark/results/. Besides the pipeline to reproduce the manuscripts' experiments, full results including dataframes for quick comparisons are hosted in that repository.If you have questions or comments, please do not hesitateto contact usoropen an issue. Weinvite contributions, e.g., of new tasks, novel metrics, or wrappers for other SBI toolboxes.InstallationAssuming you have a working Python environment, simply installsbibmviapip:$ pip install sbibmODE based models (currently SIR and Lotka-Volterra models) useJuliaviadiffeqtorch. If you are planning to use these tasks, please additionally follow theinstallation instructions ofdiffeqtorch. If you are not planning to simulate these tasks for now, you can skip this step.QuickstartA quick demonstration ofsbibm, see further below for more in-depth explanations:importsbibmtask=sbibm.get_task("two_moons")# See sbibm.get_available_tasks() for all tasksprior=task.get_prior()simulator=task.get_simulator()observation=task.get_observation(num_observation=1)# 10 per task# These objects can then be used for custom inference algorithms, e.g.# we might want to generate simulations by sampling from prior:thetas=prior(num_samples=10_000)xs=simulator(thetas)# Alternatively, we can import existing algorithms, e.g:fromsbibm.algorithmsimportrej_abc# See help(rej_abc) for keywordsposterior_samples,_,_=rej_abc(task=task,num_samples=10_000,num_observation=1,num_simulations=100_000)# Once we got samples from an approximate posterior, compare them to the reference:fromsbibm.metricsimportc2streference_samples=task.get_reference_posterior_samples(num_observation=1)c2st_accuracy=c2st(reference_samples,posterior_samples)# Visualise both posteriors:fromsbibm.visualisationimportfig_posteriorfig=fig_posterior(task_name="two_moons",observation=1,samples=[posterior_samples])# Note: Use fig.show() or fig.save() to show or save the figure# Get results from other algorithms for comparison:fromsbibm.visualisationimportfig_metricresults_df=sbibm.get_results(dataset="main_paper.csv")fig=fig_metric(results_df.query("task == 'two_moons'"),metric="C2ST")TasksYou can then see the list of available tasks by callingsbibm.get_available_tasks(). If we wanted to use, say, thetwo_moonstask, we can load it usingsbibm.get_task, as in:importsbibmtask=sbibm.get_task("slcp")Next, we might want to getpriorandsimulator:prior=task.get_prior()simulator=task.get_simulator()If we callprior()we get a single draw from the prior distribution.num_samplescan be provided as an optional argument. The following would generate 100 samples from the simulator:thetas=prior(num_samples=100)xs=simulator(thetas)xsis atorch.Tensorwith shape(100, 8), since for SLCP the data is eight-dimensional. Note that if required, conversion to and fromtorch.Tensoris very easy: Convert to a numpy array using.numpy(), e.g.,xs.numpy(). For the reverse, usetorch.from_numpy()on a numpy array.Some algorithms might require evaluating the pdf of the prior distribution, which can be obtained as atorch.Distributioninstanceusingtask.get_prior_dist(), which exposeslog_probandsamplemethods. The parameters of the prior can be picked up as a dictionary as parameters usingtask.get_prior_params().For each task, the benchmark contains 10 observations and respective reference posteriors samples. To fetch the first observation and respective reference posterior samples:observation=task.get_observation(num_observation=1)reference_samples=task.get_reference_posterior_samples(num_observation=1)Every tasks has a couple of informative attributes, including:task.dim_data# dimensionality data, here: 8task.dim_parameters# dimensionality parameters, here: 5task.num_observations# number of different observations x_o available, here: 10task.name# name: slcptask.name_display# name_display: SLCPFinally, if you want to have a look at the source code of the task, take a look insbibm/tasks/slcp/task.py. If you wanted to implement a new task, we would recommend modelling them after the existing ones. You will see that each task has a private_setupmethod that was used to generate the reference posterior samples.AlgorithmsAs mentioned in the intro,sbibmwraps a number of third-party packages to run various algorithms. We found it easiest to give each algorithm the same interface: In general, each algorithm specifies arunfunction that getstaskand hyperparameters as arguments, and eventually returns the requirednum_posterior_samples. That way, one can simply import the run function of an algorithm, tune it on any given task, and return metrics on the returned samples. Wrappers for external toolboxes implementing algorithms are in the subfoldersbibm/algorithms. Currently, integrations withsbi,pyabc,pyabcranger, as well as an experimental integration withelfiare provided.MetricsIn order to compare algorithms on the benchmarks, a number of different metrics can be computed. Each task comes with reference samples for each observation. Depending on the benchmark, these are either obtained by making use of an analytic solution for the posterior or a customized likelihood-based approach.A number of metrics can be computed by comparing algorithm samples to reference samples. In order to do so, a number of different two-sample tests can be computed (seesbibm/metrics). These test follow a simple interface, just requiring to pass samples from reference and algorithm.For example, in order to compute C2ST:importtorchfromsbibm.metrics.c2stimportc2stfromsbibm.algorithmsimportrej_abcreference_samples=task.get_reference_posterior_samples(num_observation=1)algorithm_samples,_,_=rej_abc(task=task,num_samples=10_000,num_simulations=100_000,num_observation=1)c2st_accuracy=c2st(reference_samples,algorithm_samples)For more info, seehelp(c2st).Figuressbibmincludes code for plotting results, for instance, to plot metrics on a specific task:fromsbibm.visualisationimportfig_metricresults_df=sbibm.get_results(dataset="main_paper.csv")results_subset=results_df.query("task == 'two_moons'")fig=fig_metric(results_subset,metric="C2ST")# Use fig.show() or fig.save() to show or save the figureIt can also be used to plot posteriors, e.g., to compare the results of an inference algorithm against reference samples:fromsbibm.visualisationimportfig_posteriorfig=fig_posterior(task_name="two_moons",observation=1,samples=[algorithm_samples])Results and ExperimentsWe host results and the code for reproducing the experiments of the manuscript in a seperate repository atgithub.com/sbi-benchmark/results: This includes the pipeline to reproduce the manuscripts' experiments as well as dataframes for new comparisons.CitationThe manuscript isavailable through PMLR:@InProceedings{lueckmann2021benchmarking,title={Benchmarking Simulation-Based Inference},author={Lueckmann, Jan-Matthis and Boelts, Jan and Greenberg, David and Goncalves, Pedro and Macke, Jakob},booktitle={Proceedings of The 24th International Conference on Artificial Intelligence and Statistics},pages={343--351},year={2021},editor={Banerjee, Arindam and Fukumizu, Kenji},volume={130},series={Proceedings of Machine Learning Research},month={13--15 Apr},publisher={PMLR}}SupportThis work was supported by the German Research Foundation (DFG; SFB 1233 PN 276693517, SFB 1089, SPP 2041, Germany’s Excellence Strategy – EXC number 2064/1 PN 390727645) and the German Federal Ministry of Education and Research (BMBF; project ’ADIMEM’, FKZ 01IS18052 A-D).LicenseMIT