Skip to main content

This package implements exact HMC sampling for truncated multivariate gaussians with quadratic constraints

Project description

tmg-hmc

PyPI Version Python License Tests

Exact Hamiltonian Monte Carlo sampling for truncated multivariate Gaussians with quadratic constraints

This package implements the exact HMC algorithm from Pakman and Paninski (2014) for sampling from truncated multivariate Gaussian distributions.

How It Works

The algorithm uses Hamiltonian Monte Carlo with

  1. Analytic Hamiltonian Dynamics: Particles follow deterministic Hamiltonian trajectories that are analytically computable
  2. Exact Bounces: When a trajectory hits a constraint boundary, the algorithm computes the exact bounce time by solving the quartic equation for the hit time analytically
  3. Perfect Acceptance Probability: Unlike standard HMC, there's no integration error to solve the Hamiltonian dynamics. This means the acceptance probability is always 1.

See Pakman & Paninski (2014) for mathematical details.

Features

  • Flexible constraints - Supports linear and quadratic inequality constraints
  • Efficient - Uses optimized compiled C++ hit time calculation for efficient sampling
  • GPU acceleration - Optional PyTorch backend for large-scale problems
  • Well-tested - Comprehensive test suite ensuring correctness

Installation

From PyPI

Base package install

pip install tmg-hmc

Optional GPU support

pip install tmg-hmc[gpu]

From Source

Base package

git clone https://github.com/erik-a-bensen/tmg_hmc.git
cd tmg_hmc 
pip install .

Optional GPU support

git clone https://github.com/erik-a-bensen/tmg_hmc.git
cd tmg_hmc 
pip install .[gpu]

Optional Testing Dependencies

git clone https://github.com/erik-a-bensen/tmg_hmc.git
cd tmg_hmc 
pip install .[test] # Or .[all] for test + gpu

Requirements:

  • Python 3.10+
  • numpy
  • scipy

Optional GPU Requirements

  • torch

Quick Start

Linearly Constrained Gaussian

Sample a 2D standard normal with the y-component restricted to be positive:

import numpy as np
from tmg_hmc import TMGSampler 

# Define the mean and covariance of the untruncated distribution
mu = np.zeros((2, 1))
Sigma = np.identity(2)
sampler = TMGSampler(mu, Sigma)

# Define the constraint y >= 0
# This corresponds to the constraint: f^T x + c >= 0
# where f = [0, 1] and c = 0
f = np.array([[0], [1]])
sampler.add_constraint(f=f, c=0)

# Sample 100 samples with 100 burn-in iterations
x0 = np.array([[1], [1]])  # Initial point (must satisfy constraints)
samples = sampler.sample(x0, n_samples=100, burn_in=100)

Quadratically Constrained Gaussian

Sample from a Gaussian constrained to a circular region:

import numpy as np
from tmg_hmc import TMGSampler

# 2D standard normal
mu = np.zeros((2, 1))
Sigma = np.identity(2)
sampler = TMGSampler(mu, Sigma)

# Add constraint: x^2 + y^2 <= 4 (inside circle of radius 2)
# Quadratic constraint: x^T A x + f^T x + c <= 0
# For x^2 + y^2 - 4 <= 0, we have A = I, f = 0, c = -4
A = np.identity(2)
c = -4
sampler.add_constraint(A=A, c=c)

# Sample
x0 = np.array([[0.5], [0.5]])
samples = sampler.sample(x0, n_samples=1000, burn_in=100)

Multiple Constraints

Combine multiple constraints (e.g., box constraints):

import numpy as np
from tmg_hmc import TMGSampler

mu = np.zeros((2, 1))
Sigma = np.identity(2)
sampler = TMGSampler(mu, Sigma)

# Box constraint: -1 <= x, y <= 1
# x >= -1  =>  [1,0]^T x + 1 >= 0
sampler.add_constraint(f=np.array([[1], [0]]), c=1)
# x <= 1   =>  [-1,0]^T x + 1 >= 0
sampler.add_constraint(f=np.array([[-1], [0]]), c=1)
# y >= -1  =>  [0,1]^T x + 1 >= 0
sampler.add_constraint(f=np.array([[0], [1]]), c=1)
# y <= 1   =>  [0,-1]^T x + 1 >= 0
sampler.add_constraint(f=np.array([[0], [-1]]), c=1)

x0 = np.array([[0], [0]])
samples = sampler.sample(x0, n_samples=1000, burn_in=100)

Examples

See the examples/ directory for:

  • Linear constraint examples
  • Quadratic constraint examples
  • Product constraint examples
  • Truncated Gaussian process examples

Testing

Quick Start

Install test dependencies:

pip install -e .[test]

Run all tests:

pytest

Running Specific Tests

pytest tests/test_sampler.py  # Run specific test file
pytest -v                     # Verbose output
pytest -m "not gpu"           # Skip GPU tests
pytest -m "gpu"               # GPU tests only

Test Organization

  • CPU tests run automatically in CI on Python 3.10, 3.11, 3.12, 3.13, 3.14
  • GPU tests are automatically skipped if CUDA is not available
  • Tests run on Ubuntu, Windows, and macOS

See the Actions tab for CI status.

Documentation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Citation

If you use this package in your research, please cite:

Software:

Bensen, E. A., & Kuusela, M. (2026). tmg_hmc: A Python package for Exact HMC Sampling for Truncated Multivariate Gaussians with Linear and Quadratic Constraints. Journal of Open Source Software. [In Review]

Methodology:

Pakman, A., & Paninski, L. (2014). Exact Hamiltonian Monte Carlo for Truncated Multivariate Gaussians. Journal of Computational and Graphical Statistics, 23(2), 518-542. https://doi.org/10.1080/10618600.2013.788448

BibTeX
@article{Bensen2026tmghmc,
  title={tmg\_hmc: A Python package for Exact HMC Sampling for Truncated Multivariate Gaussians with Linear and Quadratic Constraints},
  author={Bensen, Erik A. and Kuusela, Mikael},
  journal={Journal of Open Source Software},
  year={2026},
  note={[In Review]}
}

@article{PakmanPaninski2014,
  title={Exact Hamiltonian Monte Carlo for Truncated Multivariate Gaussians},
  author={Pakman, Ari and Paninski, Liam},
  journal={Journal of Computational and Graphical Statistics},
  volume={23},
  number={2},
  pages={518--542},
  year={2014},
  publisher={Taylor \& Francis},
  doi={10.1080/10618600.2013.788448}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tmg_hmc-1.0.3.tar.gz (29.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tmg_hmc-1.0.3-cp313-cp313-win_amd64.whl (386.0 kB view details)

Uploaded CPython 3.13Windows x86-64

tmg_hmc-1.0.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

tmg_hmc-1.0.3-cp313-cp313-macosx_11_0_arm64.whl (346.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

tmg_hmc-1.0.3-cp313-cp313-macosx_10_13_x86_64.whl (629.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

tmg_hmc-1.0.3-cp312-cp312-win_amd64.whl (386.0 kB view details)

Uploaded CPython 3.12Windows x86-64

tmg_hmc-1.0.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

tmg_hmc-1.0.3-cp312-cp312-macosx_11_0_arm64.whl (346.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tmg_hmc-1.0.3-cp312-cp312-macosx_10_13_x86_64.whl (629.3 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

tmg_hmc-1.0.3-cp311-cp311-win_amd64.whl (383.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tmg_hmc-1.0.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

tmg_hmc-1.0.3-cp311-cp311-macosx_11_0_arm64.whl (344.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tmg_hmc-1.0.3-cp311-cp311-macosx_10_9_x86_64.whl (626.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

tmg_hmc-1.0.3-cp310-cp310-win_amd64.whl (383.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tmg_hmc-1.0.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

tmg_hmc-1.0.3-cp310-cp310-macosx_11_0_arm64.whl (343.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

tmg_hmc-1.0.3-cp310-cp310-macosx_10_9_x86_64.whl (625.1 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file tmg_hmc-1.0.3.tar.gz.

File metadata

  • Download URL: tmg_hmc-1.0.3.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tmg_hmc-1.0.3.tar.gz
Algorithm Hash digest
SHA256 dc68d2e4d650c454d9e8e850888d8172579d4f907b2c0a663c90bd8517690be6
MD5 875cf45e94437e4fb2fd3515795fde7c
BLAKE2b-256 1adbf0c359772f61a0bc7824e9c0a0b6aed37701f7b1f9507f0bc7af56a9e853

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: tmg_hmc-1.0.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 386.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tmg_hmc-1.0.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d93c3c4150366e72c36ac9a44139ab20c461400a535577df97c072589b56efdd
MD5 c06ac9411ef7a2b987a94a156f82651b
BLAKE2b-256 0be5e8486caa693338943b600f2792086444d4bf9163c5b8c56352ef513d8b27

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f650f381d0c90ceeec9f082115daec1fc9bc30a20d048e16114b442a990ee277
MD5 779d54dc976c788d2a47331b87ba0f1d
BLAKE2b-256 f5ba01f2c7feced229cad99d2c988d1e978d0507914f54800085b162e03c03bc

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 71c20e09563e64340d6c7a1813186efc9e387ddabfd1bff1113f0afae76306f4
MD5 2259c499710914f64b8d1c2d4c6cefd7
BLAKE2b-256 80fb3fd5afd576a3d7082393a4a6fab5ea36752b0075aff77024497e7d0e0a55

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 102ef1ae8e15ea27007979ff60c079b52cdd621a7716bae510d1141a3c644239
MD5 d61ee8d9e19cd92bf1c1d4e1be0c2c62
BLAKE2b-256 4c2f8e98e29723ec35606537c09a35bc8ee17f32608dd65ef680e87c168d4826

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: tmg_hmc-1.0.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 386.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tmg_hmc-1.0.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0d3bfd1d7e2a12ee890bb25f35975722bdc56142395f8ab74e27eba2e5c186c5
MD5 e49fc407b581d91aabefe432ea033306
BLAKE2b-256 d72d988205567a832994e9461e7dc82eafe2234968facc48c6a53a61a8c6dcae

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9f7c86b9bd20c8dbc58fb59b6f534397490e835eca2ef443c0ef0ab8a8d3a4b0
MD5 3f3283dbdb1c289a1f0f1521f81d6f53
BLAKE2b-256 98092534bf8c76c0f16ca06690ea6d2a01d65b97d9c412a105242d72602627fa

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4257483113659971cce32a8da66afc36e272105f5c8493069b3439795e8b74be
MD5 ffdab5e6fba9055c910f3134a5f9b5bf
BLAKE2b-256 833d3bda78bf9bdf0b12d9fd7599091c8d16db7d9d146bfd833f07239a3d3037

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 8300ec294fe8933d2d27c5d54617702fdb8ee9dee128ab30c56d1c126b3dcd73
MD5 dba79d30e8503b87987c3aa27b7505e9
BLAKE2b-256 aa1e7e8073c38abc234097aba8d068691dd73bd666dabbf1fe742aa21c7eef72

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: tmg_hmc-1.0.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 383.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tmg_hmc-1.0.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 da77967964604eddbed8198db62447ce80bbe7c68930ebd7806646aaa0b5c3eb
MD5 812f0915adb10b5f6f9e39e9c68186c7
BLAKE2b-256 d97cb00941fd86e3730e2199bbcb35c8adfdb72f9c890dda00a209141322bf0b

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 508c3fb54e03c8bda0494da6f239ee1693bfc7627b5895f828f9047493b65476
MD5 4ed007c4f22ed029c00771b99c4a5c19
BLAKE2b-256 d24c2209b58b7cdd64b8646a09fdf930ed26b2642e852f8742ad4731df7f805f

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e8802ca0767a27aaa8387b80a6f79e46849e33dbb50e0304143a820da9484a7c
MD5 490484ae72bb856fac075b776fbdbc54
BLAKE2b-256 99cbeceb6fb7e94fc5b63604866bdf8bf0aa84e99ab01e3202e5e2653d55c2d0

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7691ce7ffef702047e0ad9021a14e62173084cd76762bd3d6a8213582c585c3f
MD5 0b4edba1da854057e57d368945f8ce6f
BLAKE2b-256 d590eb1acc0db0e25c2e52515cc31a2b4fbec8124693bdf09341f759c4c3c40d

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: tmg_hmc-1.0.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 383.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tmg_hmc-1.0.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4ee1bf029479de3f452d32334d3148fd802081d7d625356a170946381b85abd5
MD5 f7e9f1d888a94adfb192559ba2fd1ab3
BLAKE2b-256 22bfc1098a75348fc1e067131c3a6236310ad0ba23def3367d88841c7e752ea5

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ffbe55ee64a418a9ec4044dbe9f6c9e4d8b93656a4250ba37f41ea6d2de30f6b
MD5 14fc62fa7dbb41e539af26bf5870048d
BLAKE2b-256 6bf6ed0cd177ed993e96659e63057081425784bcee2f35d95a5839b361b804e5

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10ddcfeda20e387bf0e69c905500e9dcd6a931a4eea65b33c179f3c338ff45da
MD5 e5cf7232521211fe23d29cfe15f2170b
BLAKE2b-256 4a5d107c8f92b6a254245371467b641377b335ae560ac3af2ee72df3c9f9316b

See more details on using hashes here.

File details

Details for the file tmg_hmc-1.0.3-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tmg_hmc-1.0.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0a8cba9c8905cec452ab16809ca92d5c69c8df48a45a66ace2fcad1f36536cd0
MD5 73cb74f97833df31a979c16991144e2c
BLAKE2b-256 00a9c2ebfc10a75962bd9eb7f88dec5265da652ab290bdd8dbb20f25b8504d0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page