Class CompositeSamplers.SamplerBuilder<S>
- java.lang.Object
-
- org.apache.commons.rng.sampling.CompositeSamplers.SamplerBuilder<S>
-
- Type Parameters:
S- Type of sampler
- All Implemented Interfaces:
CompositeSamplers.Builder<S>
- Enclosing class:
- CompositeSamplers
private static final class CompositeSamplers.SamplerBuilder<S> extends java.lang.Object implements CompositeSamplers.Builder<S>
Builds a composite sampler.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 Classes Modifier and Type Class Description (package private) static interfaceCompositeSamplers.SamplerBuilder.SamplerFactory<S>A factory for creating composite samplers.(package private) static classCompositeSamplers.SamplerBuilder.SpecialisationThe specialisation of composite sampler to build.private static classCompositeSamplers.SamplerBuilder.WeightedSampler<S>Contains a weighted sampler.
-
Field Summary
Fields Modifier and Type Field Description private CompositeSamplers.SamplerBuilder.SamplerFactory<S>compositeFactoryThe factory to create the composite sampler.private CompositeSamplers.DiscreteProbabilitySamplerFactoryfactoryThe factory to create the discrete probability sampler from the weights.private CompositeSamplers.SamplerBuilder.SpecialisationspecialisationThe specialisation of the sampler.private java.util.List<CompositeSamplers.SamplerBuilder.WeightedSampler<S>>weightedSamplersThe weighted samplers.
-
Constructor Summary
Constructors Constructor Description SamplerBuilder(CompositeSamplers.SamplerBuilder.Specialisation specialisation, CompositeSamplers.SamplerBuilder.SamplerFactory<S> compositeFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompositeSamplers.Builder<S>add(S sampler, double weight)Adds the sampler to the composite.Sbuild(UniformRandomProvider rng)Builds the composite sampler.private DiscreteSamplercreateDiscreteSampler(UniformRandomProvider rng, double[] weights)Creates the discrete sampler of the enumerated probability distribution.private static doublemean(double[] values)Compute the mean of the values.private voidreset()Reset the builder.CompositeSamplers.Builder<S>setFactory(CompositeSamplers.DiscreteProbabilitySamplerFactory samplerFactory)Sets the factory to use to generate the composite's discrete sampler from the sampler weights.intsize()Return the number of samplers in the composite.private static doublesum(double[] values)Compute the sum of the values.private static booleanuniform(double[] values)Check if all the values are the same.
-
-
-
Field Detail
-
specialisation
private final CompositeSamplers.SamplerBuilder.Specialisation specialisation
The specialisation of the sampler.
-
weightedSamplers
private final java.util.List<CompositeSamplers.SamplerBuilder.WeightedSampler<S>> weightedSamplers
The weighted samplers.
-
factory
private CompositeSamplers.DiscreteProbabilitySamplerFactory factory
The factory to create the discrete probability sampler from the weights.
-
compositeFactory
private final CompositeSamplers.SamplerBuilder.SamplerFactory<S> compositeFactory
The factory to create the composite sampler.
-
-
Constructor Detail
-
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 Detail
-
size
public int size()
Description copied from interface:CompositeSamplers.BuilderReturn the number of samplers in the composite. The size must be non-zero before thebuildmethod can create a sampler.- Specified by:
sizein interfaceCompositeSamplers.Builder<S>- Returns:
- the size
-
add
public CompositeSamplers.Builder<S> add(S sampler, double weight)
Description copied from interface:CompositeSamplers.BuilderAdds the sampler to the composite. A sampler with a zero weight is ignored.- Specified by:
addin 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:
setFactoryin interfaceCompositeSamplers.Builder<S>- Parameters:
samplerFactory- Factory.- Returns:
- a reference to this builder
-
build
public S build(UniformRandomProvider rng)
Builds the composite sampler. Therngis 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:
buildin interfaceCompositeSamplers.Builder<S>- Parameters:
rng- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Throws:
java.lang.IllegalStateException- if no samplers have been added to create a composite.- See Also:
CompositeSamplers.Builder.size()
-
reset
private void reset()
Reset the builder.
-
createDiscreteSampler
private DiscreteSampler createDiscreteSampler(UniformRandomProvider rng, double[] weights)
Creates the discrete sampler of the enumerated probability distribution.If the specialisation is a
shared state samplerthe 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
ArrayIndexOutOfBoundsExceptionwill 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
ArrayIndexOutOfBoundsExceptionwill be thrown.- Parameters:
values- the values- Returns:
- the mean
-
-