Class CompositeSamplers
java.lang.Object
org.apache.commons.rng.sampling.CompositeSamplers
Factory class to create a sampler that combines sampling from multiple samplers.
The composite sampler is constructed using a builder for the type of samplers
that will form the composite. Each sampler has a weight in the composition.
Samples are returned using a 2 step algorithm:
- Select a sampler based on its weighting
- Return a sample from the selected sampler
The weights used for each sampler create a discrete probability distribution. This is sampled using a discrete probability distribution sampler. The builder provides methods to change the default implementation.
The following example will create a sampler to uniformly sample the border of a triangle using the line segment lengths as weights:
UniformRandomProvider rng = RandomSource.KISS.create();
double[] a = {1.23, 4.56};
double[] b = {6.78, 9.01};
double[] c = {3.45, 2.34};
ObjectSampler<double[]> sampler =
CompositeSamplers.<double[]>newObjectSamplerBuilder()
.add(LineSampler.of(rng, a, b), Math.hypot(a[0] - b[0], a[1] - b[1]))
.add(LineSampler.of(rng, b, c), Math.hypot(b[0] - c[0], b[1] - c[1]))
.add(LineSampler.of(rng, c, a), Math.hypot(c[0] - a[0], c[1] - a[1]))
.build(rng);
- Since:
- 1.4
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceBuilds a composite sampler.private static classA composite sampler.private static final classA factory for creating a composite ContinuousSampler.static enumThe DiscreteProbabilitySampler class defines implementations that sample from a user-defined discrete probability distribution.static interfaceA factory for creating a sampler of a user-defined discrete probability distribution.private static final classA factory for creating a composite DiscreteSampler.private static final classA factory for creating a composite LongSampler.private static final classA factory for creating a composite ObjectSampler.private static final classBuilds a composite sampler.private static final classA factory for creating a composite SharedStateContinuousSampler.private static final classA class to implement the SharedStateDiscreteSampler interface for a discrete probability sampler given a factory and the probability distribution.private static final classA factory for creating a composite SharedStateDiscreteSampler.private static final classA factory for creating a composite SharedStateLongSampler.private static final classA factory for creating a composite SharedStateObjectSampler. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static <T extends SharedStateSampler<T>>
List<T> copy(List<T> samplers, UniformRandomProvider rng) Create a copy instance of each sampler in the list of samplers using the given uniform random provider as the source of randomness.Create a new builder for a compositeContinuousSampler.Create a new builder for a compositeDiscreteSampler.static CompositeSamplers.Builder<LongSampler> Create a new builder for a compositeLongSampler.static <T> CompositeSamplers.Builder<ObjectSampler<T>> Create a new builder for a compositeObjectSampler.Create a new builder for a compositeSharedStateContinuousSampler.Create a new builder for a compositeSharedStateDiscreteSampler.Create a new builder for a compositeSharedStateLongSampler.static <T> CompositeSamplers.Builder<SharedStateObjectSampler<T>> Create a new builder for a compositeSharedStateObjectSampler.
-
Constructor Details
-
CompositeSamplers
private CompositeSamplers()No public instances.
-
-
Method Details
-
newObjectSamplerBuilder
Create a new builder for a compositeObjectSampler.Note: If the compiler cannot infer the type parameter of the sampler it can be specified within the diamond operator
<T>preceding the call tonewObjectSamplerBuilder(), for example:CompositeSamplers.<double[]>newObjectSamplerBuilder()- Type Parameters:
T- Type of the sample.- Returns:
- the builder
-
newDiscreteSamplerBuilder
Create a new builder for a compositeDiscreteSampler.- Returns:
- the builder
-
newContinuousSamplerBuilder
Create a new builder for a compositeContinuousSampler.- Returns:
- the builder
-
newLongSamplerBuilder
Create a new builder for a compositeLongSampler.- Returns:
- the builder
-
copy
private static <T extends SharedStateSampler<T>> List<T> copy(List<T> samplers, UniformRandomProvider rng) Create a copy instance of each sampler in the list of samplers using the given uniform random provider as the source of randomness.- Type Parameters:
T- the type of sampler- Parameters:
samplers- Source to copy.rng- Generator of uniformly distributed random numbers.- Returns:
- the copy
-