Skip to content

random

cornucopia.random

Sampler

Sampler(**theta)

Base class for random samplers, with a bunch of helpers. This class is for developers of new Sampler classes only.

__call__

__call__(n=None, **backend)

Parameters:

Name Type Description Default
n int or list[int]

Number of values to sample

  • if None, return a scalar
  • if an int, return a list
  • if a list, return a tensor
None

Returns:

Name Type Description
sample number or list[number] or tensor

Fixed

Fixed(value)

Bases: Sampler

Fixed value

Parameters:

Name Type Description Default
value number or sequence[number]
required

Uniform

Uniform(*args, **kwargs)

Bases: Sampler

Continuous uniform sampler

Uniform()
Uniform(max)
Uniform(min, max)

Parameters:

Name Type Description Default
min float or sequence[float]

Lower bound (inclusive)

0
max float or sequence[float]

Upper bound (inclusive or exclusive, depending on rounding)

1

RandInt

RandInt(*args, **kwargs)

Bases: Sampler

Discrete uniform sampler

RandInt(max)
RandInt(min, max)

Parameters:

Name Type Description Default
min float or sequence[float]

Lower bound (inclusive)

0
max float or sequence[float]

Upper bound (inclusive)

required

RandKFrom

RandKFrom(range, k=None, replacement=False)

Bases: Sampler

Discrete uniform sampler

Parameters:

Name Type Description Default
range sequence

Values from which to sample

required
k int

Number of values to sample. Sample random number if None.

None
replacement bool

Whether to sample with replacement

False

Normal

Normal(mu=0, sigma=1)

Bases: Sampler

Gaussian sampler

Parameters:

Name Type Description Default
mu float or sequence[float]

Mean

0
sigma float or sequence[float]

Standard deviation

1

LogNormal

LogNormal(mu=0, sigma=1)

Bases: Sampler

LogNormal sampler

Parameters:

Name Type Description Default
mu float or sequence[float]

Mean of the log

0
sigma float or sequence[float]

Standard deviation of the log

1

TransformedSampler

TransformedSampler(sampler, transform)

Bases: Sampler

A random variable that gets transformed by a deterministic function

Parameters:

Name Type Description Default
sampler Sampler

A random sampler

required
transform callable(float) -> float

A value-wise transformation

required

CombinedSamplers

CombinedSamplers(samplers, transform)

Bases: Sampler

Random variables that get combined by a deterministic function

Parameters:

Name Type Description Default
samplers list[Sampler]

A random sampler

required
transform callable(*float) -> float

A value-wise transformation

required

make_range

make_range(*args, **kwargs)
make_range([min], max, *, offset=0)

If any of the inputs is a Sampler, return the Sampler.

Else, build a (lower, upper) range.

Examples

# full range
make_range(x, y) -> (x, y)
make_range(x, y, offset=1) -> (1+x, 1+y)
# symmetric range
make_range(x) -> (-x, x)
make_range(x, offset=1) -> (1-x, 1+x)
# upper bound
make_range(0, x) -> (0, x)
make_range(x, min=0) -> (0, x)
# lower bound
make_range(x, 1) -> (x, 1)
make_range(x, max=1) -> (x, 1)