Class CompositeSamplers.SamplerBuilder<S>
- Type Parameters:
S
- Type of sampler
- All Implemented Interfaces:
CompositeSamplers.Builder<S>
- Enclosing class:
CompositeSamplers
A single builder can be used to create composites of different implementing classes which support different sampler interfaces. The type of sampler is generic. The individual samplers and their weights can be collected by the builder. The build method creates the discrete probability distribution from the weights. The final composite is created using a factory to create the class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static interface
A factory for creating composite samplers.(package private) static enum
The specialisation of composite sampler to build.private static final class
Contains a weighted sampler. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CompositeSamplers.SamplerBuilder.SamplerFactory
<S> The factory to create the composite sampler.The factory to create the discrete probability sampler from the weights.private final CompositeSamplers.SamplerBuilder.Specialisation
The specialisation of the sampler.private final List
<CompositeSamplers.SamplerBuilder.WeightedSampler<S>> The weighted samplers. -
Constructor Summary
ConstructorsConstructorDescriptionSamplerBuilder
(CompositeSamplers.SamplerBuilder.Specialisation specialisation, CompositeSamplers.SamplerBuilder.SamplerFactory<S> compositeFactory) -
Method Summary
Modifier and TypeMethodDescriptionAdds the sampler to the composite.Builds the composite sampler.private DiscreteSampler
createDiscreteSampler
(UniformRandomProvider rng, double[] weights) Creates the discrete sampler of the enumerated probability distribution.private static double
mean
(double[] values) Compute the mean of the values.private void
reset()
Reset the builder.setFactory
(CompositeSamplers.DiscreteProbabilitySamplerFactory samplerFactory) Sets the factory to use to generate the composite's discrete sampler from the sampler weights.int
size()
Return the number of samplers in the composite.private static double
sum
(double[] values) Compute the sum of the values.private static boolean
uniform
(double[] values) Check if all the values are the same.
-
Field Details
-
specialisation
The specialisation of the sampler. -
weightedSamplers
The weighted samplers. -
factory
The factory to create the discrete probability sampler from the weights. -
compositeFactory
The factory to create the composite sampler.
-
-
Constructor Details
-
SamplerBuilder
SamplerBuilder(CompositeSamplers.SamplerBuilder.Specialisation specialisation, CompositeSamplers.SamplerBuilder.SamplerFactory<S> compositeFactory) - Parameters:
specialisation
- Specialisation of the sampler.compositeFactory
- Factory to create the final composite sampler.
-
-
Method Details
-
size
public int size()Description copied from interface:CompositeSamplers.Builder
Return the number of samplers in the composite. The size must be non-zero before thebuild
method can create a sampler.- Specified by:
size
in interfaceCompositeSamplers.Builder<S>
- Returns:
- the size
-
add
Description copied from interface:CompositeSamplers.Builder
Adds the sampler to the composite. A sampler with a zero weight is ignored.- Specified by:
add
in interfaceCompositeSamplers.Builder<S>
- Parameters:
sampler
- Sampler.weight
- Weight for the composition.- Returns:
- a reference to this builder
-
setFactory
public CompositeSamplers.Builder<S> setFactory(CompositeSamplers.DiscreteProbabilitySamplerFactory samplerFactory) Sets the factory to use to generate the composite's discrete sampler from the sampler weights.Note: If the factory is not explicitly set then a default will be used.
If the weights are uniform the factory is ignored and composite's discrete sampler is a
uniform distribution sampler
.- Specified by:
setFactory
in interfaceCompositeSamplers.Builder<S>
- Parameters:
samplerFactory
- Factory.- Returns:
- a reference to this builder
-
build
Builds the composite sampler. Therng
is the source of randomness for selecting which sampler to use for each sample.Note: When the sampler is created the builder is reset to an empty state. This prevents building multiple composite samplers with the same samplers and their identical underlying source of randomness.
If only one sampler has been added to the builder then the sampler is returned and the builder is reset.
- Specified by:
build
in interfaceCompositeSamplers.Builder<S>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Throws:
IllegalStateException
- if no samplers have been added to create a composite.- See Also:
-
reset
private void reset()Reset the builder. -
createDiscreteSampler
Creates the discrete sampler of the enumerated probability distribution.If the specialisation is a
shared state sampler
the discrete sampler will be an instance ofSharedStateDiscreteSampler
.- Parameters:
rng
- Generator of uniformly distributed random numbers.weights
- Weight associated to each item.- Returns:
- the sampler
-
uniform
private static boolean uniform(double[] values) Check if all the values are the same.Warning: This method assumes there are input values. If the length is zero an
ArrayIndexOutOfBoundsException
will be thrown.- Parameters:
values
- the values- Returns:
- true if all values are the same
-
sum
private static double sum(double[] values) Compute the sum of the values.- Parameters:
values
- the values- Returns:
- the sum
-
mean
private static double mean(double[] values) Compute the mean of the values. Uses a rolling algorithm to avoid overflow of a simple sum. This method can be used to compute the mean of observed counts for normalisation to a probability:double[] values = ...; int n = values.length; double mean = mean(values); for (int i = 0; i < n; i++) { // Two step division avoids using the denominator (mean * n) values[i] = values[i] / mean / n; }
Warning: This method assumes there are input values. If the length is zero an
ArrayIndexOutOfBoundsException
will be thrown.- Parameters:
values
- the values- Returns:
- the mean
-