Sampler

Contains Sampler-classes.

arithmetize(cls)[source]

Add arithmetic operations to Sampler-class.

class Sampler(*args, **kwargs)[source]

Base class Sampler that implements algebra of Samplers.

weight

weight of Sampler self in mixtures.

Type

float

sample(size)[source]

Sampling method of a sampler.

Parameters

size (int) – lentgh of sample to be generated.

Returns

Array of size (len, Sampler’s dimension).

Return type

np.ndarray

apply(transform)[source]

Apply a transformation to the sampler. Build new sampler, which sampling function is given by transform(self.sample(size))`.

Parameters

transform (callable) – function, that takes ndarray of shape (size, dim_sampler) and produces ndarray of shape (size, new_dim_sampler).

Returns

instance of class Sampler with redefined method sample.

Return type

Sampler

truncate(high=None, low=None, expr=None, prob=0.5, max_iters=None, sample_anyways=False)[source]

Truncate a sampler. Resulting sampler produces points satisfying low <= pts <= high. If expr is suplied, the condition is low <= expr(pts) <= high.

Uses while-loop to obtain a sample from the region of interest of needed size. The behaviour of the while loop is controlled by parameters max_iters and sample_anyways-parameters.

Parameters
  • high (ndarray, list, float) – upper truncation-bound.

  • low (ndarray, list, float) – lower truncation-bound.

  • expr (callable, optional.) – Some vectorized function. Accepts points of sampler, returns either bool or float. In case of float, either high or low should also be supplied.

  • prob (float, optional) – estimate of P(truncation-condtion is satisfied). When supplied, can improve the performance of sampling-method of truncated sampler.

  • max_iters (float, optional) – if the number of iterations needed for obtaining the sample exceeds this number, either a warning or error is raised. By default is set to 1e7 (constant of TruncateSampler-class).

  • sample_anyways (bool, optional) – If set to True, when exceeding self.max_iters number of iterations the procedure throws a warning but continues. If set to False, the error is raised.

Returns

new Sampler-instance, truncated version of self.

Return type

Sampler

class OrSampler(left, right, *args, **kwargs)[source]

Class for implementing | (mixture) operation on Sampler-instances.

sample(size)[source]

Sampling procedure of a mixture of two samplers. Samples points with probabilities defined by weights (self.weight-attr) from two samplers invoked (self.bases-attr) and mixes them in one sample of needed size.

class AndSampler(left, right, *args, **kwargs)[source]

Class for implementing & (coordinates stacking) operation on Sampler-instances.

sample(size)[source]

Sampling procedure of a product of two samplers. Check out the docstring of Sampler.__and__ for more info.

class ApplySampler(sampler, transform, *args, **kwargs)[source]

Class for implementing apply (adding transform) operation on Sampler-instances.

sample(size)[source]

Sampling procedure of a sampler subjugated to a transform. Check out the docstring of Sampler.apply for more info.

class TruncateSampler(sampler, high=None, low=None, expr=None, prob=0.5, max_iters=None, sample_anyways=False, *args, **kwargs)[source]

Class for implementing truncate (truncation by a condition) operation on Sampler-instances.

max_iters = 10000000.0
sample(size)[source]

Sampling method of a sampler subjugated to truncation. Check out the docstring of Sampler.truncation for more info.

class BaseOperationSampler(left, right, *args, **kwargs)[source]

Base class for implementing all arithmetic operations on Sampler-instances.

operation = None
sample(size)[source]

Sampling method of a sampler.

Parameters

size (int) – lentgh of sample to be generated.

Returns

Array of size (len, Sampler’s dimension).

Return type

np.ndarray

class AddSampler(left, right, *args, **kwargs)[source]
operation = '__add__'
class MulSampler(left, right, *args, **kwargs)[source]
operation = '__mul__'
class TruedivSampler(left, right, *args, **kwargs)[source]
operation = '__truediv__'
class SubSampler(left, right, *args, **kwargs)[source]
operation = '__sub__'
class PowSampler(left, right, *args, **kwargs)[source]
operation = '__pow__'
class FloordivSampler(left, right, *args, **kwargs)[source]
operation = '__floordiv__'
class ModSampler(left, right, *args, **kwargs)[source]
operation = '__mod__'
class RAddSampler(left, right, *args, **kwargs)[source]
operation = '__radd__'
class RMulSampler(left, right, *args, **kwargs)[source]
operation = '__rmul__'
class RTruedivSampler(left, right, *args, **kwargs)[source]
operation = '__rtruediv__'
class RSubSampler(left, right, *args, **kwargs)[source]
operation = '__rsub__'
class RPowSampler(left, right, *args, **kwargs)[source]
operation = '__rpow__'
class RFloordivSampler(left, right, *args, **kwargs)[source]
operation = '__rfloordiv__'
class RModSampler(left, right, *args, **kwargs)[source]
operation = '__rmod__'
class ConstantSampler(constant, **kwargs)[source]

Sampler of a constant.

Parameters

constant (int, str, float, list) – constant, associated with the Sampler. Can be multidimensional, e.g. list or np.ndarray.

constant

vectorized constant, associated with the Sampler.

Type

np.array

sample(size)[source]

Sampling method of ConstantSampler. Repeats sampler’s constant size times.

Parameters

size (int) – the size of sample to be generated.

Returns

array of shape (size, 1) containing Sampler’s constant.

Return type

np.ndarray

class NumpySampler(name, seed=None, **kwargs)[source]

Sampler based on a distribution from numpy random.

Parameters
  • name (str) – a distribution name (a method from numpy random) or its alias.

  • seed (int) – random seed for setting up sampler’s state (see make_rng()).

  • **kwargs – additional keyword-arguments defining properties of specific distribution (e.g. loc for ‘normal’).

name

a distribution name (a method from numpy random).

Type

str

state

a random number generator

Type

numpy.random.Generator

_params

dict of args for Sampler’s distribution.

Type

dict

sample(size)[source]

Generates random samples from distribution self.name.

Parameters

size (int) – the size of sample to be generated.

Returns

array of shape (size, Sampler’s dimension).

Return type

np.ndarray

class ScipySampler(name, seed=None, **kwargs)[source]

Sampler based on a distribution from scipy.stats.

Parameters
  • name (str) – a distribution name, a class from scipy.stats, or its alias.

  • seed (int) – random seed for setting up sampler’s state (see make_rng()).

  • **kwargs – additional parameters for specification of the distribution. For instance, scale for name=’gamma’.

name

a distribution name (a class from scipy.stats).

Type

str

state

a random number generator

Type

numpy.random.Generator

distr

a distribution class

sample(size)[source]

Sampling method of ScipySampler. Generates random samples from distribution self.name.

Parameters

size (int) – the size of sample to be generated.

Returns

array of shape (size, Sampler’s dimension).

Return type

np.ndarray

class HistoSampler(histo=None, edges=None, seed=None, **kwargs)[source]

Sampler based on a histogram, output of np.histogramdd.

Parameters
  • histo (tuple) – histogram, on which the sampler is based. Make sure that it is unnormalized (normed=False in np.histogramdd).

  • edges (list) – list of len=histo_dimension, contains edges of bins along axes.

  • seed (int) – random seed for setting up sampler’s state (see make_rng()).

bins

bins of base-histogram (see np.histogramdd).

Type

np.ndarray

edges

edges of base-histogram.

Type

list

Notes

The sampler should be based on unnormalized histogram. if histo-arg is supplied, it is used for histo-initilization. Otherwise, edges should be supplied. In this case all bins are empty.

sample(size)[source]

Sampling method of HistoSampler. Generates random samples from distribution, represented by histogram (self.bins, self.edges).

Parameters

size (int) – the size of sample to be generated.

Returns

array of shape (size, histo dimension).

Return type

np.ndarray

update(points)[source]

Update bins of sampler’s histogram by throwing in additional points.

Parameters

points (np.ndarray) – Array of points of shape (n_points, histo_dimension).

cart_prod(*arrs)[source]

Get array of cartesian tuples from arbitrary number of arrays. Faster version of itertools.product. The order of tuples is lexicographic.

Parameters

arrs (tuple, list or ndarray.) – Any sequence of ndarrays.

Returns

2d-array with rows (arr[0][i], arr[2][j],…,arr[n][k]).

Return type

ndarray