Class InverseTransformDiscreteSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.SamplerBase
-
- org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler
-
- All Implemented Interfaces:
DiscreteSampler
,SharedStateDiscreteSampler
,SharedStateSampler<SharedStateDiscreteSampler>
public class InverseTransformDiscreteSampler extends SamplerBase implements SharedStateDiscreteSampler
Distribution sampler that uses the inversion method. It can be used to sample any distribution that provides access to its inverse cumulative probability function.Sampling uses
UniformRandomProvider.nextDouble()
.Example:
import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.distribution.BinomialDistribution; import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.rng.sampling.distribution.DiscreteSampler; import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler; import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction; // Distribution to sample. final IntegerDistribution dist = new BinomialDistribution(11, 0.56); // Create the sampler. final DiscreteSampler binomialSampler = InverseTransformDiscreteSampler.of(RandomSource.XO_RO_SHI_RO_128_PP.create(), new DiscreteInverseCumulativeProbabilityFunction() { public int inverseCumulativeProbability(double p) { return dist.inverseCumulativeProbability(p); } }); // Generate random deviate. int random = binomialSampler.sample();
- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description InverseTransformDiscreteSampler(UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function)
Create an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SharedStateDiscreteSampler
of(UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function)
Create a new inverse-transform discrete sampler.int
sample()
Creates anint
sample.String
toString()
SharedStateDiscreteSampler
withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.-
Methods inherited from class org.apache.commons.rng.sampling.distribution.SamplerBase
nextDouble, nextInt, nextInt, nextLong
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.distribution.DiscreteSampler
samples, samples
-
-
-
-
Constructor Detail
-
InverseTransformDiscreteSampler
public InverseTransformDiscreteSampler(UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function)
Create an instance.- Parameters:
rng
- Generator of uniformly distributed random numbers.function
- Inverse cumulative probability function.
-
-
Method Detail
-
sample
public int sample()
Creates anint
sample.- Specified by:
sample
in interfaceDiscreteSampler
- Returns:
- a sample.
-
toString
public String toString()
- Overrides:
toString
in classSamplerBase
-
withUniformRandomProvider
public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.Note: The new sampler will share the inverse cumulative probability function. This must be suitable for concurrent use to ensure thread safety.
- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateDiscreteSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static SharedStateDiscreteSampler of(UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function)
Create a new inverse-transform discrete sampler.To use the sampler to
share state
the function must be suitable for concurrent use.- Parameters:
rng
- Generator of uniformly distributed random numbers.function
- Inverse cumulative probability function.- Returns:
- the sampler
- Since:
- 1.3
- See Also:
withUniformRandomProvider(UniformRandomProvider)
-
-