Skip to main content

Efficient random sampling via linear interpolation.

Project description

lintsampler

Animation showing 'lintsampler' rendered in points.

Efficient random sampling via linear interpolation.

Test Coverage Status Documentation Status License: MIT status

When you have a density function, but you would like to create a set of sample points from that density function, you can use linear interpolate sampling. Using the evaluation of the density at the two endpoints of 1D interval, or the four corners of a 2D rectangle, or generally the $2^k$ vertices of a $k$-dimensional hyperbox (or a series of such hyperboxes, e.g., the cells of a $k$-dimensional grid), linear interpolant sampling is a technique to draw random samples within the hyperbox. lintsampler provides a Python implementation of this.

See the documentation for further details.

This package was also reviewed for the Journal of Open Source Software (JOSS). See the paper here and the review thread here.

Installation

Three ways of installing lintsampler:

  • pip:
pip install lintsampler
  • conda:
conda install -c conda-forge lintsampler
  • Simply cloning this repository.

Quickstart Example

If you have a density function, such as this multi-modal 1d pdf with the bulk of the density between -7 and 7,

import numpy as np
from scipy.stats import norm

def gmm_pdf(x):
    mu = np.array([-3.0, 0.5, 2.5])
    sig = np.array([1.0, 0.25, 0.75])
    w = np.array([0.4, 0.25, 0.35])
    return np.sum([w[i] * norm.pdf(x, mu[i], sig[i]) for i in range(3)], axis=0)

lintsampler can efficiently draw samples from it on some defined interval (here a 100-point grid between -7 and 7):

from lintsampler import LintSampler

grid = np.linspace(-7,7,100)
samples = LintSampler(grid,pdf=gmm_pdf).sample(N=10000)

Making a histogram of the resulting samples and comparing to the input density function shows good agreement -- and we can do even better by increasing the resolution. Example 1d pdf with comparative histogram of sampled points.

See this page of the documentation for a more detailed explanation of this example.

Documentation

Complete documentation, including more example notebooks, is available at lintsampler.readthedocs.io/.

Contributing

The lintsampler maintainers welcome contributions to software, examples, and documentation. The maintainers are actively monitoring pull requests and would be happy to collaborate on contributions or ideas. If you have any requests for additional information or find any bugs, please open an issue directly.

Attribution

If using lintsampler for a research publication, please cite our paper.

License

lintsampler is available under the MIT license. See the LICENSE file for specifics.

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

lintsampler-1.0.0.tar.gz (13.8 MB view details)

Uploaded Source

Built Distribution

lintsampler-1.0.0-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file lintsampler-1.0.0.tar.gz.

File metadata

  • Download URL: lintsampler-1.0.0.tar.gz
  • Upload date:
  • Size: 13.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for lintsampler-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7aa51fe0fa06d098cdd8c1ed11d266a907c0cd70abdff22964de77240bb18d0e
MD5 20be8c432597322daf7d39e1fb45f4be
BLAKE2b-256 44cf1d4d3022bf42a39420156248c8eac390b141f419779e047a99514f0322a1

See more details on using hashes here.

File details

Details for the file lintsampler-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: lintsampler-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for lintsampler-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2103459bf040f900381442ca60417ada07af395df23d613ef9523588ad96d3bd
MD5 2a26ea0cdc1588dc6eb205ffd774fc59
BLAKE2b-256 c102de5efd7361661f2d0e54b751c46717c715d3cda5e661d525c030fecbb5e0

See more details on using hashes here.

Supported by

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