Skip to main content

grapevine

Tests Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Supported Python versions: 3.12 and newer

JAX/Blackjax implementation of the grapevine method for reusing the solutions of guessing problems embedded in Hamiltonian trajectories.

The grapevine method can dramatically speed up MCMC for statistical with embedded equation solving problems.

Installation

pip install grapevine-mcmc

Usage

First make a suitable log density function.

This function should have two arguments: a set of parameters (a Pytree) and a guess (also a Pytree). It should return the log density of these parameters (a number) and the solution it found, which grapevine feeds back in as the next guess. It should also be generally compatible with JAX, and will probalbly involve some differentiable numerical solving, for example using optimistix.

Here is a simple example of such a function:

from functools import partial

import jax

from jax.scipy.stats import norm
from jax.scipy.special import expit
from jax import numpy as jnp

import optimistix as optx

# equation solving problems often need 64 bit floats
jax.config.update("jax_enable_x64", True)

solver = optx.Newton(rtol=1e-8, atol=1e-8)
obs = jnp.array(0.7)


def fn(y, args):
    """Equation defining a root-finding problem."""
    a = args
    return y - jnp.tanh(y * expit(a) + 1)


def joint_logdensity(a, obs, guess):
    """An example log density."""
    sol = optx.root_find(fn, solver, guess, args=a)
    log_prior = norm.logpdf(a, loc=0.0, scale=1.0)
    log_likelihood = norm.logpdf(obs, loc=sol.value, scale=0.5)
    return log_prior + log_likelihood, sol.value


posterior_logdensity = partial(joint_logdensity, obs=obs)
posterior_logdensity(a=0.0, guess=0.01)
# (Array(-1.22095095, dtype=float64), Array(0.8952192, dtype=float64))

Now you can run MCMC on your model using GrapeNUTS, the grapevine version of the NUTS sampler!

grapevine provides the sampler; blackjax-utils runs it, handling chains, initial jitter and position flattening. grapenuts returns the two hooks it needs.

from blackjax_utils import run_sampler
from grapevine import grapenuts

states, info = run_sampler(
    key=jax.random.key(1234),
    log_posterior=posterior_logdensity,
    init_params=jnp.array(0.0),
    init_sd=0.01,
    n_chain=4,
    n_warmup=200,
    n_sample=200,
    warmup_options=dict(initial_step_size=0.01),
    sampler=grapenuts(default_guess=jnp.array(0.01)),
)
jnp.quantile(states.position, jnp.array([0.01, 0.5, 0.99]))
# Array([-1.26712677,  0.12950684,  0.93903677], dtype=float64)

Choosing a guess

grapenuts takes a guess_fn, which turns the previous problem's solution into the next guess. The default, guess_previous, reuses the solution as it is. guess_implicit instead takes an Euler step from it, using the solution's jacobian with respect to the parameters, and guess_implicit_cg does the same without materialising any jacobians:

from functools import partial
from grapevine import grapenuts, guess_implicit

sampler = grapenuts(
    default_guess=jnp.array(0.01),
    guess_fn=partial(guess_implicit, target_function=fn),
)

A guess_fn is called as guess_fn(guess_inputs, position), where guess_inputs has the previous solution, the position at which it was found, and a flag marking the start of a trajectory. Whatever it returns is what the log density gets as its guess, so a log density that accumulates diagnostics across a trajectory can carry them alongside the solution.

Pass solver_info_fn to record something about each iteration's solve, such as the number of steps the solver took. It defaults to recording nothing.

How to run the benchmarks

  1. Install uv
  2. Run these commands
uv run benchmarks/methionine.py
uv run benchmarks/linear.py
uv run benchmarks/test_functions.py
uv run benchmarks/adversarial.py
uv run benchmarks/trajectory.py
uv run benchmarks/analyse_results.py

Alternatively, run this convenient shell script:

bash run_all_benchmarks.sh

Metadata

Release files for grapevine-mcmc 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grapevine-mcmc 0.3.0
File Size Uploaded Provenance Actions
grapevine_mcmc-0.3.0.tar.gz 2.6 MB With provenance Details for grapevine_mcmc-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for grapevine-mcmc 0.3.0
File Interpreter ABI Platform Actions
grapevine_mcmc-0.3.0-py3-none-any.whl Python 3 none any Details for grapevine_mcmc-0.3.0-py3-none-any.whl

Total release size: 2.6 MB

Release files / grapevine_mcmc-0.3.0.tar.gz

Download URL grapevine_mcmc-0.3.0.tar.gz
Size 2.6 MB
Tags Source
SHA-256 checksum
How to use checksums
e8e5a715414c1cd34c6810c3e75c585a00caaf265a5aa7380116a6f2cddcf5cf
BLAKE2b-256 checksum
How to use checksums
8868876ef4cf58a7e2e550095cf0c9101120dc9b51abc9e51cef3167979a66a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Release files / grapevine_mcmc-0.3.0-py3-none-any.whl

Download URL grapevine_mcmc-0.3.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
390c3f7b6b279fb622f91f4c890e6ff8ee7d6917f89fd515cf6e7eb65f1fa6ad
BLAKE2b-256 checksum
How to use checksums
ba4a169262f4341e9be303a34ae8efdd59edf99e57c87d314699192d53984359
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Security for grapevine-mcmc 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for grapevine_mcmc-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page