Class ZigguratNormalizedGaussianSampler
java.lang.Object
org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler
- All Implemented Interfaces:
ContinuousSampler
,NormalizedGaussianSampler
,SharedStateContinuousSampler
,SharedStateSampler<SharedStateContinuousSampler>
public class ZigguratNormalizedGaussianSampler
extends Object
implements NormalizedGaussianSampler, SharedStateContinuousSampler
Marsaglia and Tsang "Ziggurat" method for sampling from a Gaussian
distribution with mean 0 and standard deviation 1.
The algorithm is explained in this paper and this implementation has been adapted from the C code provided therein.
Sampling uses:
- Since:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double[]
Auxiliary table.private static final long[]
Auxiliary table.private static final int
Index of last entry in the tables (which have a size that is a power of 2).private static final double
Inverse of R.private static final double
Start of tail.private final UniformRandomProvider
Underlying source of randomness.private static final double[]
Auxiliary table. -
Constructor Summary
ConstructorsConstructorDescriptionCreate an instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate double
fix
(long hz, int iz) Gets the value from the tail of the distribution.static <S extends NormalizedGaussianSampler & SharedStateContinuousSampler>
Sof
(UniformRandomProvider rng) Create a new normalised Gaussian sampler.private static double
pdf
(double x) Compute the Gaussian probability density functionf(x) = e^-0.5x^2
.double
sample()
Creates adouble
sample.toString()
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 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
-
Field Details
-
R
private static final double RStart of tail.- See Also:
-
ONE_OVER_R
private static final double ONE_OVER_RInverse of R.- See Also:
-
LAST
private static final int LASTIndex of last entry in the tables (which have a size that is a power of 2).- See Also:
-
K
private static final long[] KAuxiliary table. -
W
private static final double[] WAuxiliary table. -
F
private static final double[] FAuxiliary table. -
rng
Underlying source of randomness.
-
-
Constructor Details
-
ZigguratNormalizedGaussianSampler
Create an instance.- Parameters:
rng
- Generator of uniformly distributed random numbers.
-
-
Method Details
-
sample
public double sample()Creates adouble
sample.- Specified by:
sample
in interfaceContinuousSampler
- Returns:
- a sample.
-
toString
-
fix
private double fix(long hz, int iz) Gets the value from the tail of the distribution.- Parameters:
hz
- Start random integer.iz
- Index of cell corresponding tohz
.- Returns:
- the requested random value.
-
pdf
private static double pdf(double x) Compute the Gaussian probability density functionf(x) = e^-0.5x^2
.- Parameters:
x
- Argument.- Returns:
- \( e^{-\frac{x^2}{2}} \)
-
withUniformRandomProvider
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateContinuousSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static <S extends NormalizedGaussianSampler & SharedStateContinuousSampler> S of(UniformRandomProvider rng) Create a new normalised Gaussian sampler.- Type Parameters:
S
- Sampler type.- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-