Class InverseTransformContinuousSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.SamplerBase
-
- org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler
-
- All Implemented Interfaces:
ContinuousSampler
,SharedStateContinuousSampler
,SharedStateSampler<SharedStateContinuousSampler>
public class InverseTransformContinuousSampler extends SamplerBase implements SharedStateContinuousSampler
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.RealDistribution; import org.apache.commons.math3.distribution.ChiSquaredDistribution; import org.apache.commons.rng.simple.RandomSource; import org.apache.commons.rng.sampling.distribution.ContinuousSampler; import org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler; import org.apache.commons.rng.sampling.distribution.ContinuousInverseCumulativeProbabilityFunction; // Distribution to sample. final RealDistribution dist = new ChiSquaredDistribution(9); // Create the sampler. final ContinuousSampler chiSquareSampler = InverseTransformContinuousSampler.of(RandomSource.XO_RO_SHI_RO_128_PP.create(), new ContinuousInverseCumulativeProbabilityFunction() { public double inverseCumulativeProbability(double p) { return dist.inverseCumulativeProbability(p); } }); // Generate random deviate. double random = chiSquareSampler.sample();
- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description InverseTransformContinuousSampler(UniformRandomProvider rng, ContinuousInverseCumulativeProbabilityFunction function)
Create an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SharedStateContinuousSampler
of(UniformRandomProvider rng, ContinuousInverseCumulativeProbabilityFunction function)
Create a new inverse-transform continuous sampler.double
sample()
Creates adouble
sample.String
toString()
SharedStateContinuousSampler
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.ContinuousSampler
samples, samples
-
-
-
-
Constructor Detail
-
InverseTransformContinuousSampler
public InverseTransformContinuousSampler(UniformRandomProvider rng, ContinuousInverseCumulativeProbabilityFunction function)
Create an instance.- Parameters:
rng
- Generator of uniformly distributed random numbers.function
- Inverse cumulative probability function.
-
-
Method Detail
-
sample
public double sample()
Creates adouble
sample.- Specified by:
sample
in interfaceContinuousSampler
- Returns:
- a sample.
-
toString
public String toString()
- Overrides:
toString
in classSamplerBase
-
withUniformRandomProvider
public SharedStateContinuousSampler 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<SharedStateContinuousSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static SharedStateContinuousSampler of(UniformRandomProvider rng, ContinuousInverseCumulativeProbabilityFunction function)
Create a new inverse-transform continuous 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)
-
-