DiscreteSampler
, SharedStateDiscreteSampler
, SharedStateSampler<SharedStateDiscreteSampler>
public class InverseTransformDiscreteSampler extends SamplerBase implements SharedStateDiscreteSampler
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 =
new InverseTransformDiscreteSampler(RandomSource.create(RandomSource.MT),
new DiscreteInverseCumulativeProbabilityFunction() {
public int inverseCumulativeProbability(double p) {
return dist.inverseCumulativeProbability(p);
}
});
// Generate random deviate.
int random = binomialSampler.sample();
Constructor | Description |
---|---|
InverseTransformDiscreteSampler(org.apache.commons.rng.UniformRandomProvider rng,
DiscreteInverseCumulativeProbabilityFunction function) |
Modifier and Type | Method | Description |
---|---|---|
static SharedStateDiscreteSampler |
of(org.apache.commons.rng.UniformRandomProvider rng,
DiscreteInverseCumulativeProbabilityFunction function) |
Create a new inverse-transform discrete sampler.
|
int |
sample() |
Creates a sample.
|
java.lang.String |
toString() |
|
SharedStateDiscreteSampler |
withUniformRandomProvider(org.apache.commons.rng.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.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
nextDouble, nextInt, nextInt, nextLong
public InverseTransformDiscreteSampler(org.apache.commons.rng.UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function)
rng
- Generator of uniformly distributed random numbers.function
- Inverse cumulative probability function.public int sample()
sample
in interface DiscreteSampler
public java.lang.String toString()
toString
in class SamplerBase
public SharedStateDiscreteSampler withUniformRandomProvider(org.apache.commons.rng.UniformRandomProvider rng)
Note: The new sampler will share the inverse cumulative probability function. This must be suitable for concurrent use to ensure thread safety.
withUniformRandomProvider
in interface SharedStateSampler<SharedStateDiscreteSampler>
rng
- Generator of uniformly distributed random numbers.public static SharedStateDiscreteSampler of(org.apache.commons.rng.UniformRandomProvider rng, DiscreteInverseCumulativeProbabilityFunction function)
To use the sampler to
share state
the function must be
suitable for concurrent use.
rng
- Generator of uniformly distributed random numbers.function
- Inverse cumulative probability function.withUniformRandomProvider(UniformRandomProvider)
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.