### Test SBI Installation Source: https://github.com/sbi-dev/sbi/blob/main/README.md Run a minimal example to verify the sbi installation. ```python from sbi.examples.minimal import simple posterior = simple() print(posterior) ``` -------------------------------- ### Setup Prior and Simulator Source: https://github.com/sbi-dev/sbi/blob/main/docs/tutorials/16_implemented_methods.ipynb Defines a simple toy prior and simulator for use in SBI examples. Ensure PyTorch is installed. ```python import torch from sbi.utils import BoxUniform # Define the prior num_dims = 2 num_sims = 1000 num_rounds = 2 prior = BoxUniform(low=torch.zeros(num_dims), high=torch.ones(num_dims)) simulator = lambda theta: theta + torch.randn_like(theta) * 0.1 x_o = torch.tensor([0.5, 0.5]) ``` -------------------------------- ### Setup: Define Simulator and Generate Data Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/24_abstraction_levels.ipynb Sets up a linear Gaussian simulator and generates training data for subsequent examples. Requires torch and sbi libraries. ```python import torch from sbi.inference import NLE, NPE from sbi.utils import BoxUniform # Define a simple linear Gaussian simulator def simulator(theta): """Linear Gaussian simulator with noise.""" return theta + 1.0 + torch.randn_like(theta) * 0.1 # Define prior over 3 parameters num_dim = 3 prior = BoxUniform(low=-2 * torch.ones(num_dim), high=2 * torch.ones(num_dim)) # Generate training data (used for all examples) num_simulations = 2000 theta = prior.sample((num_simulations,)) x = simulator(theta) # Generate a single observation for inference theta_o = prior.sample((1,)) x_o = simulator(theta_o) print(f"Generated {num_simulations} simulations for training") print(f"Parameter shape: {theta.shape}, Data shape: {x.shape}") ``` -------------------------------- ### Execute Complete Training Pipeline Source: https://github.com/sbi-dev/sbi/blob/main/docs/proposals/ep-01-pluggable-training.md A full example demonstrating simulation setup, configuration, logging, early stopping, and training execution. ```python from sbi import utils from sbi.inference import NPE from sbi.training import TrainConfig, LossArgsNPE, LoggingConfig, EarlyStopping # Setup simulation prior = utils.BoxUniform(low=-2*torch.ones(2), high=2*torch.ones(2)) simulator = lambda theta: theta + 0.1 * torch.randn_like(theta) # Configure training with type safety and autocomplete config = TrainConfig( batch_size=100, learning_rate=1e-3, max_epochs=1000 ) # Setup logging and early stopping logging = LoggingConfig(backend="wandb", project="sbi-experiment") early_stop = EarlyStopping.validation_loss(patience=20) # Train with new features inference = NPE(prior=prior) theta, x = utils.simulate_for_sbi(simulator, prior, num_simulations=10000) inference.append_simulations(theta, x) neural_net = inference.train( config, LossArgsNPE(exclude_invalid_x=True), logging=logging, early_stopping=early_stop ) ``` -------------------------------- ### Setup imports for sensitivity analysis example Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/07_sensitivity_analysis.ipynb Import necessary modules for defining the simulator, performing inference, and analyzing subspaces. ```python import torch from torch.distributions import MultivariateNormal from sbi.analysis import ActiveSubspace, pairplot from sbi.inference import NPE from sbi.simulators import linear_gaussian _ = torch.manual_seed(0) ``` -------------------------------- ### Install sbi with pip Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Standard installation method using pip. ```bash python -m pip install sbi ``` -------------------------------- ### Define Minimal Training Setup Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/22_experiment_tracking.ipynb Sets up a basic simulation environment, prior distribution, and neural network components for SBI inference. Ensure PyTorch is installed and configured. ```python import torch from sbi.inference import NPE from sbi.neural_nets import posterior_nn from sbi.neural_nets.embedding_nets import FCEmbedding from sbi.utils import BoxUniform torch.manual_seed(0) def simulator(theta): return theta + 0.1 * torch.randn_like(theta) prior = BoxUniform(low=-2 * torch.ones(2), high=2 * torch.ones(2)) theta = prior.sample((5000,)) x = simulator(theta) embedding_net = FCEmbedding(input_dim=x.shape[1], output_dim=32) density_estimator = posterior_nn( model="nsf", embedding_net=embedding_net, num_transforms=4, ) train_kwargs = dict( max_num_epochs=50, training_batch_size=128, validation_fraction=0.1, show_train_summary=False, ) ``` -------------------------------- ### Install sbi with uv Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Recommended method using uv to create a virtual environment and install the package. ```bash # Create a virtual environment with Python 3.12 uv venv --python 3.12 # Activate the environment (on macOS/Linux) source .venv/bin/activate # On Windows # .venv\Scripts\activate # Install sbi uv pip install sbi ``` -------------------------------- ### Install documentation dependencies Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/README.md Install the required Python packages for building the documentation. ```bash python -m pip install .[doc] ``` -------------------------------- ### Install SBI with Optional Samplers Source: https://github.com/sbi-dev/sbi/blob/main/README.md Install sbi with support for Pyro or PyMC MCMC samplers. ```bash uv pip install sbi ``` ```bash uv pip install "sbi[pyro]" # for Pyro samplers (HMC, NUTS) ``` ```bash uv pip install "sbi[pymc]" # for PyMC samplers (HMC, NUTS, Slice) ``` ```bash uv pip install "sbi[all]" # both Pyro and PyMC ``` -------------------------------- ### Install Build and Twine Source: https://github.com/sbi-dev/sbi/wiki/Release-workflow Install the necessary tools for building and uploading Python packages. Ensure you have the latest versions. ```bash pip install build twine ``` -------------------------------- ### Build and serve documentation locally Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/README.md Convert Jupyter notebooks to markdown and start the local mkdocs development server. ```bash jupyter nbconvert --to markdown ../docs/tutorials/*.ipynb --output-dir docs/tutorials/ jupyter nbconvert --to markdown ../docs/advanced_tutorials/*.ipynb --output-dir docs/tutorials/ jupyter nbconvert --to markdown ../docs/how_to_guide/09_sampler_interface.ipynb --output-dir docs/tutorials/ mkdocs serve ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/sbi-dev/sbi/blob/main/docs/README.md Installs necessary dependencies for building documentation, including Jupyter notebook support, using uv sync. ```bash uv sync --extra doc ``` -------------------------------- ### Install optional dependencies with pip Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Install additional samplers using pip extras. ```bash python -m pip install "sbi[pyro]" # or "sbi[pymc]", or "sbi[all]" ``` -------------------------------- ### Setup Simulation Task for Optuna Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/21_hyperparameter_tuning.ipynb Initializes a toy simulator, prior distribution, and generates training and validation datasets. This setup is required before defining the Optuna objective function. ```python import optuna import torch from sbi.inference import NPE from sbi.neural_nets import posterior_nn from sbi.neural_nets.embedding_nets import FCEmbedding from sbi.utils import BoxUniform torch.manual_seed(0) def simulator(theta): return theta + 0.1 * torch.randn_like(theta) prior = BoxUniform(low=-2 * torch.ones(2), high=2 * torch.ones(2)) theta = prior.sample((6000,)) x = simulator(theta) # Use a separate validation data set for optuna theta_train, x_train = theta[:5000], x[:5000] theta_val, x_val = theta[5000:], x[5000:] ``` -------------------------------- ### Install optional dependencies with uv Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Install additional samplers like Pyro or PyMC using uv extras. ```bash uv pip install "sbi[pyro]" # for Pyro samplers (HMC, NUTS) uv pip install "sbi[pymc]" # for PyMC samplers (HMC, NUTS, Slice) uv pip install "sbi[all]" # for all optional dependencies ``` -------------------------------- ### Install sbi with conda Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Installation method for conda environments using the conda-forge channel. ```bash # Create an environment for sbi (Python 3.10 or higher) conda create -n sbi_env python=3.12 && conda activate sbi_env # Install sbi from conda-forge conda install --channel conda-forge sbi ``` -------------------------------- ### Get Started with SBI Inference Source: https://github.com/sbi-dev/sbi/blob/main/README.md Basic usage for setting up and training an NPE inference model with prior simulations. ```python from sbi.inference import NPE # Given: parameters theta and corresponding simulations x inference = NPE(prior=prior) inference.append_simulations(theta, x).train() posterior = inference.build_posterior() ``` -------------------------------- ### Install and Configure Pre-commit Hooks Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Install pre-commit and set up the git hooks to run code style checks automatically before each commit. ```bash pip install pre-commit ``` ```bash pre-commit install ``` ```bash pre-commit install ``` -------------------------------- ### Install sbi via uv Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/install.md Adds the sbi package to the project using uv. ```bash uv add sbi ``` -------------------------------- ### Setup SBI Inference Workflow Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/19_posterior_parameters.ipynb Initializes the simulator, prior, and trains an NRE inference object. ```python import torch from sbi.inference import NRE from sbi.utils.torchutils import BoxUniform def simulator(theta): return 1.0 + theta + torch.randn(theta.shape, device=theta.device) * 0.1 num_dim = 3 prior = BoxUniform(low=-2 * torch.ones(num_dim), high=2 * torch.ones(num_dim)) theta = prior.sample((300,)) x = simulator(theta) inference = NRE(prior=prior) inference.append_simulations(theta, x) inference.train(); ``` -------------------------------- ### Install sbi with pixi Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Add sbi to a project managed by pixi. ```bash pixi add sbi ``` -------------------------------- ### Start Development Server with Autoreload Source: https://github.com/sbi-dev/sbi/blob/main/docs/README.md Starts a development server using sphinx-autobuild, which automatically rebuilds documentation on changes and serves it locally. Recommended for active development. ```bash sphinx-autobuild . _build/html ``` -------------------------------- ### Install sbi Development Dependencies with UV Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Install development dependencies for sbi using uv if you are using uv for environment management. ```bash uv sync --extra dev ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Install the necessary Python packages for working with the project's documentation. This command ensures all documentation-related tools are available. ```bash pip install -e ".[doc]" ``` -------------------------------- ### Install sbi in editable mode Source: https://github.com/sbi-dev/sbi/blob/main/docs/contributing.md Installs the package with development dependencies, allowing source code changes to reflect immediately. ```bash uv pip install -e ".[dev]" ``` -------------------------------- ### Install optional dependencies with conda Source: https://github.com/sbi-dev/sbi/blob/main/docs/installation.md Install additional samplers as separate conda packages. ```bash conda install --channel conda-forge pyro-ppl # for Pyro samplers conda install --channel conda-forge pymc # for PyMC samplers ``` -------------------------------- ### Install sbi via conda Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/install.md Installs sbi from the conda-forge channel into an active conda environment. ```bash conda install --channel conda-forge sbi ``` -------------------------------- ### Install Documentation Dependencies with UV Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Install documentation dependencies using the 'uv' package manager. This command synchronizes all extra dependencies required for documentation tasks. ```bash uv sync --all-extras ``` -------------------------------- ### Convert Jupyter Notebooks to Markdown and Serve Docs Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Convert tutorial Jupyter notebooks to markdown files and then serve the documentation website locally. This is necessary after updating tutorials or examples. ```bash cd docs jupyter nbconvert --to markdown ../tutorials/*.ipynb --output-dir docs/tutorials/ mkdocs serve ``` -------------------------------- ### Install pre-commit hooks Source: https://github.com/sbi-dev/sbi/blob/main/docs/contributing.md Sets up automated code style checks to run before each commit. ```bash pre-commit install ``` -------------------------------- ### Install dill for Pickling Source: https://github.com/sbi-dev/sbi/blob/main/docs/faq/question_03_pickling_error.md Install the 'dill' library using pip. This is required to set it as the pickler for multiprocessing. ```bash pip install dill ``` -------------------------------- ### Install sbi in Editable Mode Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Install sbi and its development dependencies in editable mode using pip. This allows changes in the source code to be reflected immediately. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Initialize posterior samples Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/17_plotting_functionality.ipynb Setup the posterior object and generate samples for plotting. ```python import torch from toy_posterior_for_07_cc import ExamplePosterior from sbi.analysis import pairplot posterior = ExamplePosterior() posterior_samples = posterior.sample((100,)) ``` -------------------------------- ### Define prior and simulator Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/18_training_interface.ipynb Setup the prior distribution and a simple simulator function for generating training data. ```python seed = 0 torch.manual_seed(seed) prior = BoxUniform(-3 * ones((2,)), 3 * ones((2,))) def simulator(theta): return theta + torch.randn_like(theta) * 0.1 ``` -------------------------------- ### Define simulator and train NPE model Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/15_importance_sampled_posteriors.ipynb Setup of the simulator, prior, and training of the NPE model for posterior estimation. ```python # define prior and simulator class Simulator: def __init__(self): pass def log_likelihood(self, theta, x): return MultivariateNormal(theta, eye(2)).log_prob(x) def sample(self, theta): return theta + torch.randn((theta.shape)) prior = BoxUniform(-5 * ones((2,)), 5 * ones((2,))) sim = Simulator() log_prob_fn = lambda theta, x_o: sim.log_likelihood(theta, x_o) + prior.log_prob(theta) # generate train data _ = torch.manual_seed(3) theta = prior.sample((10,)) x = sim.sample(theta) # train NPE model _ = torch.manual_seed(4) inference = NPE(prior=prior) _ = inference.append_simulations(theta, x).train() posterior = inference.build_posterior() # generate a synthetic observation _ = torch.manual_seed(2) theta_gt = prior.sample((1,)) observation = sim.sample(theta_gt)[0] posterior = posterior.set_default_x(observation) print("observations.shape", observation.shape) # sample from posterior theta_inferred = posterior.sample((10_000,)) # get samples from ground-truth posterior gt_samples = MultivariateNormal(observation, eye(2)).sample((len(theta_inferred) * 5,)) gt_samples = gt_samples[prior.support.check(gt_samples)][:len(theta_inferred)] ``` -------------------------------- ### Run All Mini-SBI Tests Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Execute all mini-sbibm tests with default parameters to get performance metrics. This command is useful for a comprehensive performance check. ```bash pytest --bm ``` -------------------------------- ### Load toy posterior Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/10_diagnostics_posterior_predictive_checks.ipynb Loads a predefined example posterior distribution from a local module for demonstration purposes. ```python from toy_posterior_for_07_cc import ExamplePosterior posterior = ExamplePosterior() ``` -------------------------------- ### Setup LC2ST-NF Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/13_diagnostics_lc2st.ipynb Initialize and train the LC2ST-NF model. This involves setting up the flow's inverse transform and base distribution, and then training the model under the null hypothesis and on observed data. ```python from sbi.diagnostics.lc2st import LC2ST_NF flow_inverse_transform = npe.inverse_transform flow_base_dist = torch.distributions.MultivariateNormal( torch.zeros(2), torch.eye(2) # same as npe.net._distribution ) lc2st_nf = LC2ST_NF( prior_samples=theta_cal, xs=x_cal, posterior_samples=post_samples_cal, flow_inverse_transform=flow_inverse_transform, flow_base_dist=flow_base_dist, num_ensemble=1, ) _ = lc2st_nf.train_under_null_hypothesis() _ = lc2st_nf.train_on_observed_data() ``` -------------------------------- ### Setup CNNEmbedding Network in Python Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/04_embedding_networks.ipynb Initializes a Convolutional Neural Network (CNN) embedding network from SBI. Use this to reduce the dimensionality of image data before feeding it into a neural density estimator. ```python from sbi.neural_nets.embedding_nets import CNNEmbedding embedding_net = CNNEmbedding( input_shape=(32, 32), in_channels=1, out_channels_per_layer=[6], num_conv_layers=1, num_linear_layers=1, output_dim=8, kernel_size=5, pool_kernel_size=8 ) ``` -------------------------------- ### Import SBI analysis and inference modules Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Imports necessary modules from the sbi library for analysis and inference, including FMPE, NPSE, and neural network utilities. Ensure these libraries are installed. ```python import torch from sbi.analysis import pairplot from sbi.inference import FMPE, NPSE from sbi.neural_nets import posterior_flow_nn, posterior_score_nn from sbi.utils import BoxUniform ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Build and serve the project's documentation website locally using MkDocs. This allows for previewing changes in real-time. ```bash mkdocs serve ``` -------------------------------- ### Create and Activate UV Virtual Environment Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/contribute.md Set up a virtual environment using uv. Activate it using the appropriate command for your operating system. ```bash uv venv ``` ```bash source .venv/bin/activate ``` ```bash .venv\Scripts\activate ``` -------------------------------- ### Configure logging backends Source: https://github.com/sbi-dev/sbi/blob/main/docs/proposals/ep-01-pluggable-training.md Demonstrates how to switch between different logging backends using the LoggingConfig interface. ```python from sbi.training import LoggingConfig # Choose your backend - no other code changes needed logging = LoggingConfig(backend="wandb", project="my-experiment") # or: LoggingConfig(backend="tensorboard", log_dir="./runs") # or: LoggingConfig(backend="mlflow", experiment_name="sbi-run") # or: LoggingConfig(backend="stdout") # default inference.train(train_config, loss_args, logging=logging) ``` -------------------------------- ### Import Packages and Configure Environment Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/21_diagnostics_misspecification_checks.ipynb Initializes the environment by importing necessary libraries and setting a random seed for reproducibility. ```python %load_ext autoreload %autoreload 2 # visualization import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import torch # HH simulator from HH_helper_functions import HHsimulator, calculate_summary_statistics, syn_current from sbi import analysis as analysis # sbi from sbi import utils as utils from sbi.inference import simulate_for_sbi from sbi.neural_nets.embedding_nets import FCEmbedding from sbi.utils.user_input_checks import ( check_sbi_inputs, process_prior, process_simulator, ) # set seed seed = 2025 torch.manual_seed(seed) np.random.seed(seed) ``` -------------------------------- ### Initialize Prior and Simulator Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/07_sensitivity_analysis.ipynb Sets up a multivariate normal prior distribution and a linear Gaussian simulator function. This is the initial step before performing inference. ```python prior = MultivariateNormal(0.0 * torch.ones(2), 2 * torch.eye(2)) def simulator(theta): return linear_gaussian(theta, -0.8 * torch.ones(2), torch.eye(2)) ``` -------------------------------- ### Basic SBI Workflow Source: https://github.com/sbi-dev/sbi/blob/main/docs/tutorials/00_getting_started.ipynb This snippet outlines the fundamental steps in using sbi for inference, including initializing NPE, appending simulations, training, and building a posterior. ```python from sbi.inference import NPE from sbi.analysis import pairplot num_simulations = 1000 theta = prior.sample((num_simulations,)) x = simulate(theta) inference = NPE(prior) posterior_net = inference.append_simulations(theta, x).train() posterior = inference.build_posterior() posterior_theta = posterior.sample((100,), x=x_o) pairplot(posterior_theta) ``` -------------------------------- ### Import necessary libraries for SBI and Pyro Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/23_using_pyro_with_sbi.ipynb Imports required libraries for SBI, Pyro, and PyTorch distributions. Ensure Pyro is installed with `pip install "sbi[pyro]"`. ```python import matplotlib.pyplot as plt import pyro import pyro.distributions as dist import torch from pyro.infer import MCMC, RandomWalkKernel from torch.distributions import MultivariateNormal, Normal # SBI imports from sbi.inference import NLE from sbi.utils.pyroutils import to_pyro_distribution pyro.clear_param_store() ``` -------------------------------- ### Configure SBI Prior and Simulator Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/04_embedding_networks.ipynb Prepare the prior distribution and wrap the simulator for compatibility with sbi input requirements. ```python # set prior distribution for the parameters prior = utils.BoxUniform( low=torch.tensor([0.0, 0.0]), high=torch.tensor([1.0, 2 * torch.pi]) ) # make a SBI-wrapper on the simulator object for compatibility prior, num_parameters, prior_returns_numpy = process_prior(prior) simulator_wrapper = process_simulator(simulator_model, prior, prior_returns_numpy) check_sbi_inputs(simulator_wrapper, prior) ``` -------------------------------- ### NPSE training output Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Example console output after training an NPSE model. ```text Output: Neural network successfully converged after 170 epochs. ``` -------------------------------- ### Import SBI diagnostic and inference modules Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/11_diagnostics_simulation_based_calibration.ipynb Initializes the environment by importing necessary modules for SBC, TARP, and NPE inference, and sets the random seed for reproducibility. ```python import torch from torch import eye, ones from torch.distributions import MultivariateNormal from sbi.analysis.plot import sbc_rank_plot from sbi.diagnostics import check_sbc, check_tarp, run_sbc, run_tarp from sbi.inference import NPE # Set random seed _ = torch.manual_seed(42) ``` -------------------------------- ### FMPE training output Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Example console output after training an FMPE model. ```text Output: Neural network successfully converged after 114 epochs. ``` -------------------------------- ### Define Simulator and Prior Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/20_time_series_embedding.ipynb Sets up a simple simulator using torch.randn to generate time-series data and defines a BoxUniform prior for parameter sampling. ```python torch.manual_seed(0) def simulator(theta): return torch.randn(1, 100) prior = BoxUniform(torch.tensor([0.0]), torch.tensor([1.0])) thetas = prior.sample((50,)) xs = simulator(thetas) x_o = simulator(torch.tensor([0.5])) ``` -------------------------------- ### One-Time Documentation Build and Serve Source: https://github.com/sbi-dev/sbi/blob/main/docs/README.md Performs a one-time build of the Sphinx documentation and serves the generated HTML files using Python's built-in HTTP server. ```bash make html && python -m http.server --directory _build/html ``` -------------------------------- ### View TensorBoard Results Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/22_experiment_tracking.ipynb Launches TensorBoard to visualize training results logged to the 'sbi-logs' directory. Run this command in your terminal. ```bash tensorboard --logdir=sbi-logs ``` -------------------------------- ### Define Simulator and Generate Data Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/11_diagnostics_simulation_based_calibration.ipynb Sets up a linear Gaussian simulator with a small smear and generates initial parameter and observation data. Ensure `prior` and `eye` are defined before use. ```python default_likelihood_loc = 0.0 # let's start with 0 shift default_likelihood_scale = 0.01 # let's smear theta only by a little bit def simulator(theta, loc=default_likelihood_loc, scale=default_likelihood_scale): """linear gaussian inspired by sbibm https://github.com/sbi-benchmark/sbibm/blob/15f068a08a938383116ffd92b92de50c580810a3/sbibm/tasks/gaussian_linear/task.py#L74 """ num_dim = theta.shape[-1] cov_ = scale * eye(num_dim) # always positively semi-definite # using validate_args=False disables sanity checks on `covariance_matrix` # for the sake of speed value = MultivariateNormal( loc=(theta + loc), covariance_matrix=cov_, validate_args=False ).sample() return value theta = prior.sample((num_simulations,)) x = simulator(theta) ``` -------------------------------- ### Create uv Virtual Environment Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/install.md Initializes a new virtual environment using uv with Python 3.10. ```bash uv venv -p 3.10 ``` -------------------------------- ### Configure FMPE with Transformer Network Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Use `posterior_flow_nn` to build a transformer network for FMPE. This example shows how to initialize the network builder and then train the FMPE model. ```python # FMPE with a transformer architecture net_builder = posterior_flow_nn( model="transformer", num_layers=2, num_heads=2, hidden_features=64, ) trainer = FMPE(prior, vf_estimator=net_builder) estimator = trainer.append_simulations(theta, x).train( training_batch_size=200, learning_rate=5e-4 ) ``` -------------------------------- ### Initializing NRE with a string classifier Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/03_density_estimators.ipynb Initializes a Neural Likelihood Estimation (NLE) object using a string identifier for the classifier. This example uses a 'resnet' classifier. ```python inference = NRE(prior=prior, classifier="resnet") ``` -------------------------------- ### Use NUTS Sampler with Pyro Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/24_abstraction_levels.ipynb To use the No-U-Turn Sampler (NUTS) with Pyro, specify `mcmc_method="nuts_pyro"`. This requires installing `sbi` with the `pyro` extra. ```python posterior_nuts = inference_nle.build_posterior( sample_with="mcmc", mcmc_method="nuts_pyro" ) samples_nuts = posterior_nuts.sample((1000,), x=x_o) print("Sampling Level 3: Using NUTS from Pyro") ``` -------------------------------- ### simulate_for_sbi Helper Source: https://github.com/sbi-dev/sbi/blob/main/mkdocs/docs/reference/inference.md Documentation for the simulate_for_sbi helper function, used for simulation within SBI. ```APIDOC ## simulate_for_sbi Helper ### Description Documentation for the simulate_for_sbi helper function, used for simulation within SBI. ### Function sbi.inference.trainers.base.simulate_for_sbi ``` -------------------------------- ### Train with TensorBoard Tracker Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/22_experiment_tracking.ipynb Trains an SBI inference model using the default TensorBoard tracker. Additional parameters can be logged using `tracker.log_params()`. Ensure `torch.utils.tensorboard` is installed. ```python from torch.utils.tensorboard.writer import SummaryWriter from sbi.utils.tracking import TensorBoardTracker tracker = TensorBoardTracker(SummaryWriter("sbi-logs")) tracker.log_params({"embedding_dim": 32, "num_transforms": 4}) inference = NPE(prior=prior, tracker=tracker) inference.append_simulations(theta, x) estimator = inference.train(**train_kwargs) posterior = inference.build_posterior(estimator) ``` -------------------------------- ### Import sbi modules Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/15_importance_sampled_posteriors.ipynb Required imports for setting up the simulator, prior, and inference models. ```python import torch from torch import eye, ones from torch.distributions import MultivariateNormal from sbi.analysis import marginal_plot from sbi.inference import NPE, ImportanceSamplingPosterior from sbi.utils import BoxUniform ``` -------------------------------- ### Configure NPSE with MLP Network Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Use `posterior_score_nn` to build an MLP network for NPSE. This example demonstrates configuring the network with specific SDE types and training parameters. ```python # NPSE with custom network configuration via posterior_score_nn net_builder = posterior_score_nn( model="mlp", sde_type="ve", hidden_features=128, num_layers=6, ) trainer = NPSE(prior, vf_estimator=net_builder) estimator = trainer.append_simulations(theta, x).train() ``` -------------------------------- ### SBI Main Syntax with Embedding Network Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/04_embedding_networks.ipynb Demonstrates the core sbi syntax for setting up a neural posterior with a pre-configured embedding network (e.g., CNN) and training an NPE inference procedure. Ensure prior and simulator are defined before use. ```Python from sbi.neural_nets import posterior_nn from sbi.neural_nets.embedding_nets import ( FCEmbedding, CNNEmbedding, PermutationInvariantEmbedding ) embedding_net = CNNEmbedding(input_shape=(32, 32)) neural_posterior = posterior_nn(model="maf", embedding_net=embedding_net) inferer = NPE(prior=prior, density_estimator=neural_posterior) density_estimator = inference.append_simulations(theta, x).train() posterior = inference.build_posterior(density_estimator) ``` -------------------------------- ### Train and sample with FMPE Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Demonstrates the minimal workflow for initializing, training, and sampling from an FMPE posterior. ```python # Minimal FMPE example fmpe_trainer = FMPE(prior) fmpe_trainer.append_simulations(theta, x).train() fmpe_posterior = fmpe_trainer.build_posterior() samples_fmpe = fmpe_posterior.sample((num_posterior_samples,), x=x_o) fig, ax = pairplot( samples_fmpe, limits=[[-2, 2]] * 3, figsize=(5, 5), labels=labels, points=theta_o, ) ``` -------------------------------- ### Apply and Invert Parameter Transform Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/09_sampler_interface.ipynb Applies a PyTorch parameter transform to map parameters to an unconstrained space and then inverts it to get the original parameters. Useful for improving MCMC performance. ```python theta_tf = parameter_transform(torch.zeros(1, num_dim)) theta_original = parameter_transform.inv(theta_tf) print(theta_original) # -> tensor([[0.0]]) ``` -------------------------------- ### Process prior and simulator for sbi Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/18_training_interface.ipynb Prepare the prior and simulator objects to ensure compatibility with sbi's internal processing requirements. ```python from sbi.utils.user_input_checks import ( process_prior, process_simulator, ) prior, num_parameters, prior_returns_numpy = process_prior(prior) # Check simulator, returns PyTorch simulator able to simulate batches. simulator = process_simulator(simulator, prior, prior_returns_numpy) ``` -------------------------------- ### Train and sample with NPSE Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/19_vector_field_methods.ipynb Demonstrates the minimal workflow for initializing, training, and sampling from an NPSE posterior using the 've' SDE type. ```python # Minimal NPSE example npse_trainer = NPSE(prior, sde_type="ve") npse_trainer.append_simulations(theta, x).train() npse_posterior = npse_trainer.build_posterior() samples_npse = npse_posterior.sample((num_posterior_samples,), x=x_o) fig, ax = pairplot( samples_npse, limits=[[-2, 2]] * 3, figsize=(5, 5), labels=labels, points=theta_o, ) ``` -------------------------------- ### Import necessary libraries for SBI Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/12_iid_data_and_permutation_invariant_embeddings.ipynb Imports essential libraries for SBI, including plotting, PyTorch, and specific SBI modules for inference, analysis, and utilities. This setup is required for most SBI operations. ```python import matplotlib.pyplot as plt import torch from torch import eye, zeros from torch.distributions import MultivariateNormal from sbi.analysis import pairplot from sbi.inference import NLE, NPE, simulate_for_sbi from sbi.inference.posteriors.posterior_parameters import MCMCPosteriorParameters from sbi.simulators.linear_gaussian import ( linear_gaussian, true_posterior_linear_gaussian_mvn_prior, ) from sbi.utils.metrics import c2st from sbi.utils.user_input_checks ( check_sbi_inputs, process_prior, process_simulator, ) # Seeding torch.manual_seed(1); ``` -------------------------------- ### Initialize LC2ST and Sample Data Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/13_diagnostics_lc2st.ipynb Initializes the LC2ST class with prior samples, simulator outputs, and posterior samples. Sets up the necessary data for hypothesis testing. Ensure torch and sbi are imported and the prior and simulator are defined. ```python from sbi.diagnostics.lc2st import LC2ST torch.manual_seed(42) # seed for reproducibility # sample calibration data theta_cal = prior.sample((NUM_CAL,)) x_cal = simulator(theta_cal) post_samples_cal = posterior.sample_batched((1,), x=x_cal, max_sampling_batch_size=10)[0] ``` -------------------------------- ### Perform Bayesian Inference Workflow Source: https://github.com/sbi-dev/sbi/blob/main/docs/tutorials/01_Bayesian_workflow.ipynb Demonstrates the core sbi workflow including simulation, neural network training, and SBC diagnostics. ```python from sbi.neural_nets import posterior_nn from sbi.inference import NPE, DirectPosterior from sbi.diagnostics import SBC num_simulations = 1000 theta = prior.sample((num_simulations,)) x = simulate(theta) # Train neural network. inference = NPE(prior) posterior_net = inference.append_simulations(theta, x).train() posterior = inference.build_posterior() # Diagnostics with SBC. thetas = prior.sample((num_sbc_samples,)) xs = simulator(thetas) ranks, dap_samples = run_sbc( thetas, xs, posterior, num_posterior_samples=1_000, ) ``` -------------------------------- ### Sample from true and estimated posteriors for reference observations Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/13_diagnostics_lc2st.ipynb Generates reference observations and samples from both the true posterior and the trained neural posterior estimator for these observations. This is a setup step for comparing posterior estimations. ```python from sbi.simulators.gaussian_mixture import ( samples_true_posterior_gaussian_mixture_uniform_prior, ) # get reference observations torch.manual_seed(0) # seed for reproducibility thetas_star = prior.sample((3,)) xs_star = simulator(thetas_star) # Sample from the true and estimated posterior ref_samples_star = {} for i,x in enumerate(xs_star): ref_samples_star[i] = samples_true_posterior_gaussian_mixture_uniform_prior( x_o=x[None,:], num_samples=1000, ) post_samples_star = posterior.sample_batched((10000,), x=xs_star).permute(1,0,2) ``` -------------------------------- ### Clone the sbi repository Source: https://github.com/sbi-dev/sbi/blob/main/docs/contributing.md Initializes a local copy of the forked repository. ```bash git clone git@github.com:$USERNAME/sbi.git cd sbi ``` -------------------------------- ### Generate and Plot NPE Posterior Samples Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/12_iid_data_and_permutation_invariant_embeddings.ipynb Generates posterior samples for various trial counts using an NPE model and visualizes them in a pairplot with contour plots. Includes setup for plotting legends. ```python # We can easily obtain posteriors for many different x_os, instantly, because # NPE is fully amortized: num_trials = [2, 4, 6, 8, 12, 14, 18] npe_samples = [] for xo in xos: # we need to pad the x_os with NaNs to match the shape of the training data. xoi = torch.ones(1, max_num_trials, x_dim) * float("nan") xoi[0, : len(xo), :] = xo npe_samples.append(posterior.sample(sample_shape=(num_samples,), x=xoi)) # Plot them in one pairplot as contours (obtained via KDE on the samples). fig, ax = pairplot( npe_samples, points=theta_o, diag="kde", upper="contour", diag_kwargs=dict(bins=100), upper_kwargs=dict(levels=[0.95]), fig_kwargs=dict( points_colors=["k"], points_offdiag=dict(marker="*", markersize=10), ), ) plt.sca(ax[1, 1]) plt.legend( [f"{nt} trials" if nt > 1 else f"{nt} trial" for nt in num_trials] + [r"$\theta_o$"], frameon=False, fontsize=12, ); ``` -------------------------------- ### Get True Posterior Samples Source: https://github.com/sbi-dev/sbi/blob/main/docs/advanced_tutorials/12_iid_data_and_permutation_invariant_embeddings.ipynb Defines a function to obtain ground-truth posterior samples for a linear Gaussian model with a multivariate normal prior. Assumes likelihood parameters and prior details are available. ```python def get_true_posterior_samples(x_o, num_samples=1): return true_posterior_linear_gaussian_mvn_prior( x_o, likelihood_shift, likelihood_cov, prior_mean, prior_cov ).sample((num_samples,)) ``` -------------------------------- ### Run Optuna Study and Retrain Model Source: https://github.com/sbi-dev/sbi/blob/main/docs/how_to_guide/21_hyperparameter_tuning.ipynb Initializes an Optuna study with a TPE sampler, enqueues a default trial, optimizes the objective function for a specified number of trials, and then retrains the final model using the best hyperparameters found. ```python sampler = optuna.samplers.TPESampler(n_startup_trials=10) study = optuna.create_study(direction="minimize", sampler=sampler) # Optional: ensure the default config is evaluated study.enqueue_trial({"embedding_dim": 32, "num_transforms": 4}) # This will run the above NPE training up to 25 times study.optimize(objective, n_trials=25) best_params = study.best_params embedding_net = FCEmbedding( input_dim=x_train.shape[1], output_dim=best_params["embedding_dim"], ) density_estimator = posterior_nn( model="nsf", embedding_net=embedding_net, num_transforms=best_params["num_transforms"], ) inference = NPE(prior=prior, density_estimator=density_estimator) inference.append_simulations(theta, x) final_estimator = inference.train(training_batch_size=128) posterior = inference.build_posterior(final_estimator) ``` -------------------------------- ### Obtain Reference Posterior Samples via Analytical Likelihood and MCMC Source: https://github.com/sbi-dev/sbi/blob/main/docs/tutorials/Example_01_DecisionMakingModel.ipynb Generates simulation data and obtains posterior samples using MCMC with an analytical likelihood. Requires prior setup and simulation functions. ```python torch.manual_seed(42) num_trials = 10 num_samples = 1000 theta_o = prior.sample((1,)) x_o = mixed_simulator(theta_o.repeat(num_trials, 1)) ``` ```python mcmc_kwargs = dict( num_chains=100, warmup_steps=100, init_strategy="resample", thin=1, num_workers=4, ) true_posterior = MCMCPosterior( potential_fn=BinomialGammaPotential(prior, x_o), proposal=prior, theta_transform=prior_transform, **mcmc_kwargs, ) true_samples = true_posterior.sample((num_samples,)) ```