aneeshnaik / lintsampler

Efficient random sampling via linear interpolation.
https://lintsampler.readthedocs.io
MIT License
1 stars 1 forks source link

Proposed API change #1

Closed michael-petersen closed 1 month ago

michael-petersen commented 2 months ago

This version of lintsampler has a streamlined interface that more closely matches Pythonic patterns. At the simplest, we can now do something like draw 6 samples from a box:

  >>> x = np.linspace(10,20,2)
  >>> y = np.linspace(100,200,2)
  >>> z = np.linspace(1000,2000,2)
  >>> def rndmpdf(X): return np.random.uniform(size=X.shape[0])
  >>> LS = LintSampler(rndmpdf,cells=(x,y,z)).sample(6)
  array([[  12.63103673,  186.7514952 , 1716.6187807 ],
         [  14.67375968,  116.20984414, 1557.59629547],
         [  11.47055697,  178.41650558, 1592.18260186],
         [  12.41780309,  105.28009531, 1436.39525998],
         [  13.44764381,  152.57623376, 1880.55963378],
         [  18.5522151 ,  133.87092063, 1558.85620176]])

which entails constructing a probability density function (here built as a vectorised input to numpy.random.uniform), and defining the cell, before calling for a number of samples (here, 6).

The wrapper class makes both the special case of a grid and the more flexible case of a set of cells fall under one call structure.

michael-petersen commented 2 months ago

It would also be nice to implement the flexibility option for the pdf input, such that the pdf function doesn't need to be vectorised.

michael-petersen commented 1 month ago

The new support for non-vectorised pdfs (which is now the default) means that a minimal example to draw from a 3d prism looks like:

import numpy as np
from lintsampler import LintSampler

x = np.linspace(10,20,2)
y = np.linspace(100,200,2)
z = np.linspace(1000,2000,2)

# default: a pdf that takes the same number of arguments as dimensions
def rndmpdf_nonvec(x,y,z):
    return np.random.uniform()

LintSampler(rndmpdf_nonvec,cells=(x,y,z)).sample()

But the call also works to use a vectorised pdf, for performance, if the vectorizedpdf flag is set:

def rndmpdf(X):
    return np.random.uniform(size=X.shape[0])

LintSampler(rndmpdf,cells=(x,y,z),vectorizedpdf=True).sample()