BodyWalk is a python library implementing popular random walk techniques for sampling uniformly over convex bodies. More precisely, this package offers the following functionalities:
- Efficient implementation of several random walk algorithms, including Ball Walk, Hit-and-Run, and Billiard Walk;
- Support for general convex bodies, including rectangles, balls, and polytopes;
- Implementation of rounding techniques for improved mixing time.
from bodywalk.sampling import ball_walk, billiard_walk, hit_and_run
from bodywalk.geometry import Polytope
convex_body = Polytope([[1, 0], [-1, 0], [0, 1], [0, -1]], [0.5]*4) # Square of side 1 centered at (0, 0)
initial_sample = [0, 0] # Initial point to start the Markov Chain
random_state = 42 # RNG seed
chain = ball_walk(convex_body, initial_sample, delta=0.5)
# chain = billiard_walk(convex_body, initial_sample, tau=0.5)
# chain = hit_and_run(convex_body, initial_sample)
for sample in chain.generate(random_state):
# process the samples one-by-one
# generate a batch of 10 samples, ignoring the first 100
samples = chain.sample(n=10, warmup=100, random_state=random_state)
This module was tested using the following dependencies:
- Python >= 3.9
- Numpy >= 1.21.4
- Pytest >= 6.2.5
This project is released under the BSD-3 Clause license.
[1] L. Lovász. An Algorithmic Theory of Numbers, Graphs and Convexity, volume 50 of CBMS-NSF Regional Conference Series in Applied Mathematics. Society for Industrial and Applied Mathematics, 1986. ISBN 9781611970203.
[2] L. Lovász. Hit-and-run mixes fast. Mathematical Programming, 86(3):443–461, 1999. ISSN 0025-5610. doi: 10.1007/s101070050099.
[3] B.T. Polyak and E.N. Gryazina. Billiard walk-a new sampling algorithm for control and optimization. IFAC Proceedings Volumes, 47(3):6123-6128, 2014.