freealg.sample#

freealg.sample(x, rho, num_pts, method='qmc', seed=None)#

Low-discrepancy sampling from density estimate.

Parameters:
xnumpy.array

Sorted abscissae at which the density has been evaluated. Shape (n,).

rhonumpy.array

Density values corresponding to x. Must be non-negative and define a valid probability density (i.e., integrate to 1 over the support). Shape (n,).

num_ptsint

Number of sample points to generate from the density estimate.

method{'mc', 'qmc'}, default= 'qmc'

Method of drawing samples from uniform distribution:

  • 'mc': Monte Carlo

  • 'qmc': Quasi Monte Carlo

seedint, default=None

Seed for random number generator

Returns:
samplesnumpy.array, shape (num_pts,)

Samples drawn from the estimated density using a one-dimensional Halton sequence mapped through the estimated quantile function.

Notes

The underlying Quasi-Monte Carlo engine uses scipy.stats.qmc.Halton function for generating low-discrepancy points.

Examples

>>> import numpy
>>> from freealg import sample

>>> # density of Beta(3,1) on [0,1]
>>> x = numpy.linspace(0, 1, 200)
>>> rho = 3 * x**2

>>> samples = sample(x, rho, num_pts=1000, method='qmc')
>>> assert samples.shape == (1000,)

>>> # Empirical mean should be close to 3/4
>>> numpy.allclose(samples.mean(), 0.75, atol=0.02)