Library implementing hit-and-run methods for sampling open bounded sets.
Project description
anyHR
A collection of hit-and-run Markov Chain Monte Carlo algorithms for sampling of n-dimensional sets defined by arbitrary inequality constraints.
Introduction
This tool implements some variants of the hit and run or mixing algorithms.
Let S be an open bounded set in n dimensions defined by inequality constraints of the form
f(x1,..., xn) < g(x1,..., xn), where f and g are arbitrary functions.
Let all parameters also have parameter ranges defined as intervals, so S is a subset of a hyperrectangle.
(As an example, one could impose x + y < 1
in the two dimensional plane, with x in (0,1) and y in (0,1)).
Hit-and-run algorithms can be used to get a sample uniformly at random inside of this set S.
anyHR parses the parameters and their respective constraints and returns a number of samples that satisfy this spec,
while being distributed uniformly on the set of allowed values.
For more information on mixing algorithms see
Installation
It is necessary to have a working installation of Python 3,
pip and git for the following installation process.
Open the target installation directory in a terminal and type
pip install anyHR
Use
A minimal running example for the above specification can be sampled with the following code:
# Import modules
import numpy as np
import matplotlib.pyplot as plt
from anyHR.constraint.Constraint import Constraints
from anyHR.hit_and_run.hit_and_run import HitAndRun
# Define variables to use
var_names = ['x', 'y']
# Define the set of constraint
c = Constraints(var_names)
c.add_constraint('x+y < 1')
# Define the bounding hyperrectangle
x_bound = [0, 1]
y_bound = [0, 1]
bounds = [x_bound, y_bound]
# build hr object
hr = HitAndRun(constraint=c, bounding_box=bounds)
# generate samples
samples = []
total_rejections = 0
nb_samples = 100
mixing = 10
for i in range(nb_samples * mixing):
sample, rejections = hr.next_sample()
# do some mixing in between samples
if i % mixing == 0:
samples.append(sample)
xs = [sample[0] for sample in samples]
ys = [sample[1] for sample in samples]
plt.scatter(xs,ys)
plt.show()
References
For more information about mixing algorithms, see:
Smith, R. L. (1984). Efficient Monte Carlo procedures for generating points uniformly distributed over bounded regions. Operations Research, 32(6), 1296-1308.
Kiatsupaibul, S., Smith, R. L., & Zabinsky, Z. B. (2011). An analysis of a variation of hit-and-run for uniform sampling from general regions. ACM Transactions on Modeling and Computer Simulation (TOMACS), 21(3), 1-11.
Neal, R. M. (2003). Slice sampling. The annals of statistics, 31(3), 705-767.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file anyHR-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: anyHR-1.0.0-py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9aa48fe1c37e0979b5af0dcf2c2c0618b6cb5665a5fedf8927c78a2c8d6d1008 |
|
MD5 | 01916b1d63ab0ad244e279019af13dce |
|
BLAKE2b-256 | 69ede9edf77658ecbf434bc7cc090fb98c3767f400c2b978e6fff5597344cfa7 |