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

Example notebook support

pip install tmg-hmc[examples]

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 .[dev] for test + examples + 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)

Running the Example Notebooks

See the examples/ directory for:

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

Install notebook dependencies first: From PyPI

pip install tmg-hmc[examples]

Or locally

pip install -e .[examples]

Then launch Jupyter:

jupyter notebook examples/

Documentation

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.

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

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.4.tar.gz (32.9 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.4-cp313-cp313-win_amd64.whl (390.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tmg_hmc-1.0.4-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.4-cp313-cp313-macosx_11_0_arm64.whl (351.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

tmg_hmc-1.0.4-cp313-cp313-macosx_10_13_x86_64.whl (634.0 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

tmg_hmc-1.0.4-cp312-cp312-win_amd64.whl (390.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tmg_hmc-1.0.4-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.4-cp312-cp312-macosx_11_0_arm64.whl (351.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tmg_hmc-1.0.4-cp312-cp312-macosx_10_13_x86_64.whl (634.0 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

tmg_hmc-1.0.4-cp311-cp311-win_amd64.whl (388.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tmg_hmc-1.0.4-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.4-cp311-cp311-macosx_11_0_arm64.whl (349.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tmg_hmc-1.0.4-cp311-cp311-macosx_10_9_x86_64.whl (631.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

tmg_hmc-1.0.4-cp310-cp310-win_amd64.whl (388.8 kB view details)

Uploaded CPython 3.10Windows x86-64

tmg_hmc-1.0.4-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.4-cp310-cp310-macosx_11_0_arm64.whl (348.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

tmg_hmc-1.0.4-cp310-cp310-macosx_10_9_x86_64.whl (629.9 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tmg_hmc-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e27c0df5a8f497b0b6e4a21cc5706d2753b55612eaf077876928992d975fbba3
MD5 ddbc118fe5f569b403452d95d907e120
BLAKE2b-256 a129b2e6169f7ae09ea5cdb5996802ad81b94eb328d74f5805b3d57e5f9b0e9d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tmg_hmc-1.0.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 387fb3862b4f77c57ec0678032c56cac79e6b12f27e53c568a51a490800319d2
MD5 e0c421d3f204a517e13e796d5df287ec
BLAKE2b-256 cf34c2bbdd09625a5b5eff8bc057822f947da57d5aea047c0ced28a74b0981ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2236d47a98796b984a6d3b4d8b75d57f8ffd448ea0fcc411c60dab0b861739f8
MD5 b54bc10d7657b9ca647913a022a9af27
BLAKE2b-256 402fc537a3e7aec063d4d9661bd9b4999633b01b42d943404ad8477b60cadf39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a98fdbf871aac8859e80e03d96b196dc8dc542abee40722d5dc3221279ccdb24
MD5 c48c2d2a5f06e31ed3bde2f840abf868
BLAKE2b-256 b28f5b99055e2e6531914e22ca263c39a65dd9eaec4096fabc4b131ed80ce588

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 03009fd0f9d0f8c1280481bbf8ce41710c3fd4fe91241bfa31c7e9057d4a77b8
MD5 8aeaca9b81e0dcb7adfa1e65fb1a7e31
BLAKE2b-256 1cfa03c7b4ffe4b8873730ba1110fb25738ab27de0ab613358621974f6ea7a66

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tmg_hmc-1.0.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f8318349003a362851ff4bf8777db2db2436da81a0d0ff1957a847979c27b35a
MD5 3be193ae0bdf86e52b12306e7b9a24a8
BLAKE2b-256 2dbf469f5c5f3d81bde7bd7713ba6f498973a2cafc12c1a09e9cbf595818dff3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f64059fd0d022c62b5006e5214b2475c7f93a884bec9863fa490d2d40c7c6dd
MD5 1fc5ce42a82204da5c4331e2de609b36
BLAKE2b-256 9926f6725e3f9eafd8a47a7bc63a94ed59460bce61f2de1414f43e2b2292cdf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ac9549a28417bede8fbdcf62e67f0151b9f123c1915647ef9b52b02c5d0bc757
MD5 09dacac45211b4fa16bcfb6e1fefd686
BLAKE2b-256 7c09bbf78f6b031d53d6019ae8fc0b5ad48a62d8991a08ad3f9e07a5131c9bba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 74615b0d104bec41d97dfa09e88ccea9e4a9e2b7f10663969cefba6938696ae1
MD5 c2deab8aa9c46cc669ef0cee42fb455f
BLAKE2b-256 d8913237dd72b357a64602b99dd1c201d3d9ffc9e48f586921e53dc560f8a309

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tmg_hmc-1.0.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ee4177ba08783dbb74585d9323192a9da7e1eb3868cb47f3c834290e4c0aa9fc
MD5 943d878939148f4bf9e83635af636311
BLAKE2b-256 f53dc0f184940a10696d7f759b5223fbae6219dd2491c949f6b309373280d8c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d4780c1ba007f4b9f62ced6747cc35967691b96ce5e7a08ec68c7cc3a3654582
MD5 3c2fcb4e7959b0ba27ab6d5fe4a306e9
BLAKE2b-256 d8795ae93ab07c1cc934ec883c355cd10fe329ba58ba96a2213c848480c0e671

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ecfd8f4a2916b9cdbcdefc007511dbfe06ba3d2860ec6c876be0750a19d6eae
MD5 39cb5a70c8cb499b5e490326757f11fb
BLAKE2b-256 69a729c9370e0c836e90cf4f5fc67b24e1c883d599e883df75d700771d6eec50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a3eafcb85d2ddc60de568653b1c8cce52eeb853b91c10a779af2af69835596f4
MD5 a756dd60fcf601d1606f3953ec014561
BLAKE2b-256 000b4e029e0fad80eaa99a764f73faa5b9ff79134f0cc50449fd70052c017aae

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tmg_hmc-1.0.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2c2dd644b18602fbf433f6cdd908c1e8406ae5817c8c0228b4cfec57088e54ff
MD5 a6446f1ee62c59828c6139b7afe09270
BLAKE2b-256 365f6f9ce5adc54fd8cccf715567c10af2760eefa9af778ae2af028cb5ee30bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5f6f750192d38249d74d8d0c78c80e347efec58c4e1fafd092b6bf921327f1e4
MD5 3fed378d17fc19c0d7c5db2de2202845
BLAKE2b-256 8a43931c318d7e3788346c9fccc42e958afe329f831c62054aa23d3ac120f55b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 29404d63a7d56df1201e639969468436b6437ad4f2c710b2483facd0ef8a25a6
MD5 e7de48fc78d8e94492bb76d4869c5a50
BLAKE2b-256 7e2d49fbabd458a12afd79559acbfad7f57a191e9d8b7f06d40b6c481866b8df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tmg_hmc-1.0.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7a3a4796517cec8976028f5ee6e819b26c3c295ad7e7796041ff0df97a9f928b
MD5 4f50816506432dc728654def9d859a80
BLAKE2b-256 73a96154871b2cc921c8500f03fbc406ce09db5e192dd9d2264157bafee804bc

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