sbi

repository·main·Indexed 21 days ago

https://github.com/sbi-dev/sbi

A Python package for simulation-based inference (SBI) that enables researchers to estimate posterior distributions over simulator parameters based on observed data. It implements several state-of-the-art algorithms including Neural Posterior Estimation (NPE), Neural Likelihood Estimation (NLE), Neural Ratio Estimation (NRE), Neural Variational Inference (NVI), and Mixed Neural Likelihood Estimation (MNLE). The library also provides tools for learning summary statistics via embedding networks, sensitivity analysis using ActiveSubspace, and local validation via Local Classifier Two-Sample Tests (L-C2ST).

Tokens
28.5K
Snippets
84
Records
102
Agent score
73%

What's inside sbi

  1. Overview of SBI methods in sbi

    main

    The sbi package implements several machine-learning-based methods to address the problem of intractable likelihoods. These methods are categorized by how they learn the relationship between parameters and data:

    • Neural Posterior Estimation (NPE): Directly learns the posterior distribution. Includes amortized (NPE) and sequential (SNPE) versions.
    • Neural Likelihood Estimation (NLE): Learns the likelihood function. Includes amortized (NLE) and sequential (SNLE) versions.
    • Neural Ratio Estimation (NRE): Learns the likelihood ratio. Includes amortized (NRE) and sequential (SNRE) versions.
    • Neural Variational Inference (NVI): Includes amortized (NVI) and sequential (SNVI) versions.
    • Mixed Neural Likelihood Estimation (MNLE)

    Amortized vs. Sequential:

    • Amortized methods (e.g., NPE) return a posterior that can be applied to many different observations without retraining.
    • Sequential methods (e.g., SNPE) focus the inference on one particular observation to be more simulation-efficient.
  2. Overview of sbi inference algorithms

    main

    The sbi package implements several state-of-the-art simulation-based inference algorithms, categorized by their approach:

    Neural Posterior Estimation (NPE)

    Estimates the posterior distribution directly. Includes:

    • (S)NPE_A: Amortized single-round NPE.
    • (S)NPE_B: Flexible statistical inference for mechanistic models.
    • (S)NPE_C (or APT): Automatic Posterior Transformation.
    • TSNPE: Truncated proposals for scalable SBI.
    • FMPE: Flow matching for scalable SBI.
    • NPSE: Compositional score modeling.

    Neural Likelihood Estimation (NLE)

    Estimates the likelihood function. Includes:

    • (S)NLE (or SNL): Sequential Neural Likelihood.

    Neural Ratio Estimation (NRE)

    Estimates the likelihood ratio. Includes:

    • (S)NRE_A (or AALR): Amortized Approximate Likelihood Ratios.
    • (S)NRE_B (or SRE): Contrastive Learning for LFI.
    • (S)NRE_C (or NRE-C): Contrastive Neural Ratio Estimation.
    • BNRE: Balanced Neural Ratio Estimation.

    Neural Variational Inference (NVI)

    Includes:

    • SNVI: Sequential Variational Inference.

    Mixed Neural Likelihood Estimation (MNLE)

    • MNLE: Flexible and efficient inference for decision-making models.
  3. Migrate from old training API to new API

    main

    The new training infrastructure is backward compatible. You can continue using the old method of passing individual keyword arguments to .train(), or you can mix the old style with new features like LoggingConfig or EarlyStopping during a transition period.

    # Old API still supported - no breaking changes
    inference.train(training_batch_size=100, learning_rate=1e-3)
    
    # Mix old and new as needed during migration
    inference.train(
        training_batch_size=100,  # old style
        logging=LoggingConfig(backend="wandb")  # new feature
    )
  4. Install sbi optional dependencies (Pyro and PyMC)

    main

    sbi supports MCMC samplers from Pyro and PyMC. These are optional and must be installed explicitly using extras or separate package names depending on your package manager.

    Using uv:

    • For Pyro samplers (HMC, NUTS): uv pip install "sbi[pyro]"
    • For PyMC samplers (HMC, NUTS, Slice): uv pip install "sbi[pymc]"
    • For all optional dependencies: uv pip install "sbi[all]"

    Using pip:

    • For Pyro samplers: python -m pip install "sbi[pyro]"
    • For PyMC samplers: python -m pip install "sbi[pymc]"
    • For all optional dependencies: python -m pip install "sbi[all]"

    Using conda:

    • For Pyro samplers: conda install --channel conda-forge pyro-ppl
    • For PyMC samplers: conda install --channel conda-forge pymc
    uv pip install "sbi[all]"
  5. Install sbi using uv (Recommended)

    main

    The recommended way to install sbi is using uv. sbi requires Python 3.10 or higher, though Python 3.12 is recommended.

    To set up a new environment and install sbi with uv:

    1. Create a virtual environment with Python 3.12: uv venv --python 3.12
    2. Activate the environment:
      • macOS/Linux: source .venv/bin/activate
      • Windows: .venv\Scripts\activate
    3. Install the package: uv pip install sbi
    # Create a virtual environment with Python 3.12
    uv venv --python 3.12
    
    # Activate the environment (on macOS/Linux)
    source .venv/bin/activate
    
    # Install sbi
    uv pip install sbi
  6. Set up a local development environment for sbi

    main

    To contribute to sbi, follow these steps to set up your local environment using uv and git:

    1. Fork the repository on GitHub.
    2. Clone your fork locally:
      git clone git@github.com:$USERNAME/sbi.git
      cd sbi
    3. Install in editable mode with development dependencies using uv. This ensures changes to the source code are immediately reflected when importing sbi:
      uv pip install -e ".[dev]"
    4. Add the upstream remote to keep your fork synchronized with the main repository:
      git remote add upstream git@github.com:sbi-dev/sbi.git
    5. Install pre-commit to run code style checks automatically:
      pre-commit install
    git clone git@github.com:$USERNAME/sbi.git
    cd sbi
    uv pip install -e ".[dev]"
    git remote add upstream git@github.com:sbi-dev/sbi.git
    pre-commit install
  7. Guidelines for using AI coding assistants

    main

    You are permitted to use AI coding assistants (e.g., GitHub Copilot, ChatGPT) when contributing to sbi, provided you follow these responsibilities:

    • Verification: Do not blindly accept suggestions. You are responsible for the correctness, efficiency, and security of all submitted code.
    • Review: Treat AI-generated code as unverified code; review it line-by-line.
    • Testing: Write comprehensive unit tests to guard against AI hallucinations or subtle bugs.
    • Attribution: If an AI assistant generated a significant portion of the code, mention it in your commit message or pull request description (e.g., "Implemented feature X with assistance from GitHub Copilot").
    • Style: Ensure all AI-generated code adheres to the project's formatting and maintainability standards.
  8. Build and serve documentation locally with autoreload

    main

    To develop documentation with automatic rebuilding and live serving, use sphinx-autobuild. This method is recommended because it automatically runs Jupyter notebooks, collects their output, and incorporates them into the documentation. The server typically runs at http://127.0.0.1:8000.

    # Install documentation dependencies
    uv sync --extra doc
    
    # Start development server with autoreload
    sphinx-autobuild . _build/html
  9. Install sbi via uv

    main

    Install the core sbi package using uv. Note that sbi requires Python 3.10 or higher.

    To install optional MCMC samplers from Pyro or PyMC, use the following extras:

    • sbi[pyro]: For Pyro samplers (e.g., HMC, NUTS).
    • sbi[pymc]: For PyMC samplers (e.g., HMC, NUTS, Slice).
    • sbi[all]: Installs both Pyro and PyMC support.
    uv pip install sbi
    
    # Optional samplers
    uv pip install "sbi[pyro]"
    uv pip install "sbi[pymc]"
    uv pip install "sbi[all]"
  10. Verify sbi installation

    main

    To ensure sbi is installed correctly, run the minimal example provided in the package. This should return a posterior object without errors.

    from sbi.examples.minimal import simple
    posterior = simple()
    print(posterior)