Skip to content

cornucopia.random

min module-attribute

min = MinimumOfSamplers

Alias for [MinimumOfSamplers][]

max module-attribute

max = MaximumOfSamplers

Alias for [MinimumOfSamplers][]

sum module-attribute

sum = SumOfSamplers

Alias for [MinimumOfSamplers][]

exp module-attribute

exp = ExponentiatedSampler

Alias for [ExponentiatedSampler][]

log module-attribute

log = LogarithmOfSampler

Alias for [LogarithmOfSampler][]

pow module-attribute

pow = PowerOfSamplers

Alias for [PowerOfSamplers][]

Sampler

Sampler(**theta)

Bases: ABC

Base class for random samplers, with a bunch of helpers.

Note

Samplers can be combined with determinstic values or between themselves using arithmetic operators:

P + X -> ShiftedSampler(P, X)
P - X -> ShiftedSampler(P, -X)
P * X -> MultipliedSampler(P, X)
P / X -> DividedSampler(P, X)
X / P -> DividedSampler(X, P)
P ** X -> PoweredSampler(P, X)
X ** P -> ExponentSampler(X, P)

P + Q -> SumOfSamplers(P, Q)
P - Q -> DifferenceOfSamplers(P, Q)
P * Q -> ProductOfSamplers(P, Q)
P / Q -> RatioOfSamplers(P, Q)
P ** Q -> PowerOfSamplers(P, Q)

min(P, Q, ...) -> MinimumOfSamplers(P, Q, ...)
max(P, Q, ...) -> MaximumOfSamplers(P, Q, ...)
sum(P, Q, ...) -> SumOfSamplers(P, Q, ...)
exp(P)         -> ExponentiatedSampler(P)
log(P)         -> LogarithmOfSampler(P)

Attributes:

Name Type Description
theta dict

Parameters of the sampler

make classmethod

make(other: Sampler) -> Sampler
make(other: dict) -> Sampler
make(other: tuple) -> Sampler
make(other: Any) -> Sampler

Build a sampler from another sample, or from parameters.

__call__ abstractmethod

__call__() -> Number
__call__(n: int) -> Sequence[Number]
__call__(n: Sequence[int], *, dtype=None, device=None) -> torch.Tensor

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

Other Parameters:

Name Type Description
dtype dtype

Data type of the returned tensor. By default, inferred from the sampler parameters. Only used if n is a list.

device device

Device of the returned tensor. By default, inferred from the sampler parameters. Only used if n is a list.

Returns:

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

Fixed

Fixed(*, mean, std=0)
Fixed(*, mean, var=0)
Fixed(*, mean, fwhm=0)
Fixed(*, min, max=None)

Bases: Sampler

Fixed value.

Attributes:

Name Type Description
value number or sequence[number]

Uniform

Uniform()
Uniform(max)
Uniform(min, max)
Uniform(*, mean, fwhm=1)
Uniform(*, mean, std)
Uniform(*, mean, var)

Bases: Sampler

Continuous uniform sampler.

Attributes:

Name Type Description
min float or sequence[float], default=0

Lower bound (inclusive)

max float or sequence[float], default=1

Upper bound (inclusive or exclusive, depending on rounding)

RandInt

RandInt()
RandInt(max)
RandInt(min, max)

Bases: Sampler

Discrete uniform sampler

Attributes:

Name Type Description
min int or sequence[int], default=0

Lower bound (inclusive)

max int or sequence[int]

Upper bound (inclusive)

RandKFrom

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

Bases: Sampler

Discrete uniform sampler

Attributes:

Name Type Description
range sequence

Values from which to sample

k int, default=None

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

replacement bool, default=False

Whether to sample with replacement

Normal

Normal(mu=0, sigma=1)
Normal(*, mean, std=1)
Normal(*, mean, var)
Normal(*, mean, fwhm)

Bases: Sampler

Gaussian sampler.

Attributes:

Name Type Description
mu float or sequence[float], default=0

Mean

sigma float or sequence[float], default=1

Standard deviation

LogNormal

LogNormal(mu=0, sigma=1)
LogNormal(*, logmean, logstd=1)
LogNormal(*, logmean, logvar)
LogNormal(*, logmean, logfwhm)
LogNormal(*, mean, std=1)
LogNormal(*, mean, var)
LogNormal(*, mean, fwhm)

Bases: Sampler

LogNormal sampler

Attributes:

Name Type Description
mu float or sequence[float], default=0

Mean of the log

sigma float or sequence[float], default=1

Standard deviation of the log

UniformSphere

UniformSphere(ndim=3, **backend)

Bases: Sampler

Uniform distribution on the (d-1)-sphere

Parameters:

Name Type Description Default
ndim int

Number of dimensions of the embedding space. If ndim=3, generate sample that lie on the 2-sphere.

3

TransformedSampler

TransformedSampler(sampler, transform)

Bases: Sampler

A random variable that gets transformed by a deterministic function.

  1. Apply the sampler to get samples
  2. Apply the transform to each sample

Attributes:

Name Type Description
sampler Sampler

A random sampler

transform Callable[[float], float]

A value-wise transformation

CombinedSamplers

CombinedSamplers(samplers, transform)

Bases: Sampler

Random variables that get combined by a deterministic function.

  1. Apply each sampler to get samples
  2. Apply the transform to the samples

Attributes:

Name Type Description
samplers list[Sampler]

A random sampler

transform Callable[[float, ...], float]

A value-wise transformation

MultipliedSampler

MultipliedSampler(sampler, other)

Bases: TransformedSampler

Multiply each sample by a fixed value.

DividedSampler

DividedSampler(sampler, other)

Bases: TransformedSampler

Divide each sample by a fixed value.

DividingSampler

DividingSampler(sampler, other)

Bases: TransformedSampler

Divide a fixed value by each sample.

ShiftedSampler

ShiftedSampler(sampler, other)

Bases: TransformedSampler

Add a fixed value to each sample.

ReverseShiftedSampler

ReverseShiftedSampler(sampler, other)

Bases: TransformedSampler

Subtract each sample from a fixed value.

PoweredSampler

PoweredSampler(sampler, other)

Bases: TransformedSampler

Raise each sample to a fixed power.

ExponentSampler

ExponentSampler(sampler, other)

Bases: TransformedSampler

Raise a fixed value to the power of each sample.

ProductOfSamplers

ProductOfSamplers(*samplers)

Bases: CombinedSamplers

Multiply samples from two samplers.

RatioOfSamplers

RatioOfSamplers(left, right)

Bases: CombinedSamplers

Divide samples from two samplers.

SumOfSamplers

SumOfSamplers(*samplers)

Bases: CombinedSamplers

Add samples from two samplers.

DifferenceOfSamplers

DifferenceOfSamplers(left, right)

Bases: CombinedSamplers

Subtract samples from two samplers.

MinimumOfSamplers

MinimumOfSamplers(*samplers)

Bases: CombinedSamplers

Return the minimum of samples from two samplers.

MaximumOfSamplers

MaximumOfSamplers(*samplers)

Bases: CombinedSamplers

Return the maximum of samples from two samplers.

AverageOfSamplers

AverageOfSamplers(*samplers)

Bases: CombinedSamplers

Return the average of samples from two samplers.

ExponentiatedSampler

ExponentiatedSampler(sampler)

Bases: TransformedSampler

Return the exponentiated value of samples from a sampler.

LogarithmOfSampler

LogarithmOfSampler(sampler)

Bases: TransformedSampler

Return the logarithm of samples from a sampler.

PowerOfSamplers

PowerOfSamplers(base, exponent)

Bases: CombinedSamplers

Raise samples from one sampler to the power of samples from another sampler.

make_range

make_range(max, *, min=0, offset=0) -> Uniform
make_range(min, *, max, offset=0) -> Uniform
make_range(min, max, *, offset=0) -> Uniform

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

Else, build a {lower|upper} range.

Examples

make_range(x, y)           -> (x, y)
make_range(x, y, offset=1) -> (1+x, 1+y)
make_range(x)           -> (-x, x)
make_range(x, offset=1) -> (1-x, 1+x)
make_range(0, x)     -> (0, x)
make_range(x, min=0) -> (0, x)
make_range(x, 1)     -> (x, 1)
make_range(x, max=1) -> (x, 1)