Class ZigguratSampler.NormalizedGaussian
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.ZigguratSampler
-
- org.apache.commons.rng.sampling.distribution.ZigguratSampler.NormalizedGaussian
-
- All Implemented Interfaces:
ContinuousSampler
,NormalizedGaussianSampler
,SharedStateContinuousSampler
,SharedStateSampler<SharedStateContinuousSampler>
- Enclosing class:
- ZigguratSampler
public static final class ZigguratSampler.NormalizedGaussian extends ZigguratSampler implements NormalizedGaussianSampler, SharedStateContinuousSampler
Modified ziggurat method for sampling from a Gaussian distribution with mean 0 and standard deviation 1.Note: The algorithm is a modification of the
Marsaglia and Tsang "Ziggurat" method
. The modification improves performance of the rejection method used to generate samples at the edge of the ziggurat.- See Also:
NormalizedGaussianSampler
,GaussianSampler
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.commons.rng.sampling.distribution.ZigguratSampler
ZigguratSampler.Exponential, ZigguratSampler.NormalizedGaussian
-
-
Field Summary
Fields Modifier and Type Field Description private static long
CONCAVE_E_MAX
Maximum distance of concave pdf(x) below the hypotenuse value for early exit.private static long
CONVEX_E_MAX
Maximum epsilon distance of convex pdf(x) above the hypotenuse value for early rejection.private SharedStateContinuousSampler
exponential
Exponential sampler used for the long tail.private static int
I_MAX
The number of layers in the ziggurat.private static long[]
IPMF
The alias inverse PMF.private static int
J_INFLECTION
The point where the Gaussian switches from convex to concave.private static byte[]
MAP
The alias map.private static double
ONE_OVER_X_0
1/X_0.private static double[]
X
The precomputed ziggurat lengths, denoted X_i in the main text.private static double
X_0
Beginning of tail.private static double[]
Y
The precomputed ziggurat heights, denoted Y_i in the main text.
-
Constructor Summary
Constructors Modifier Constructor Description private
NormalizedGaussian(UniformRandomProvider rng)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private double
edgeSample(long xx)
Create the sample from the edge of the ziggurat.static ZigguratSampler.NormalizedGaussian
of(UniformRandomProvider rng)
Create a new normalised Gaussian sampler.double
sample()
Creates adouble
sample.private int
selectRegion()
Select the overhang region or the tail using alias sampling.java.lang.String
toString()
ZigguratSampler.NormalizedGaussian
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.ZigguratSampler
interpolate, nextLong, randomInt63, toString
-
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 Detail
-
I_MAX
private static final int I_MAX
The number of layers in the ziggurat. Maximum i value for early exit.- See Also:
- Constant Field Values
-
J_INFLECTION
private static final int J_INFLECTION
The point where the Gaussian switches from convex to concave. This is the largest value of X[j] below 1.- See Also:
- Constant Field Values
-
CONVEX_E_MAX
private static final long CONVEX_E_MAX
Maximum epsilon distance of convex pdf(x) above the hypotenuse value for early rejection. Equal to approximately 0.2460 scaled by 2^63. This is negated on purpose as the distance for a point (x,y) above the hypotenuse is negative:(|d| < max) == (d >= -max)
.- See Also:
- Constant Field Values
-
CONCAVE_E_MAX
private static final long CONCAVE_E_MAX
Maximum distance of concave pdf(x) below the hypotenuse value for early exit. Equal to approximately 0.08244 scaled by 2^63.- See Also:
- Constant Field Values
-
X_0
private static final double X_0
Beginning of tail. Equal to X[0] * 2^63.- See Also:
- Constant Field Values
-
ONE_OVER_X_0
private static final double ONE_OVER_X_0
1/X_0. Used for tail sampling.- See Also:
- Constant Field Values
-
MAP
private static final byte[] MAP
The alias map. An integer in [0, 255] stored as a byte to save space. Contains the alias j for each index. j=0 is the tail; j in [1, N] is the overhang for each layer.
-
IPMF
private static final long[] IPMF
The alias inverse PMF. This is the probability threshold to use the alias for j in-place of j. This has been scaled by 2^64 and offset by -2^63. It represents the numerator of a fraction with denominator 2^64 and can be compared directly to a uniform long deviate. The value probability 0.0 is Long.MIN_VALUE and is used whenj > I_MAX
.
-
X
private static final double[] X
The precomputed ziggurat lengths, denoted X_i in the main text.- X_i = length of ziggurat layer i.
- X_j is the upper-left X coordinate of overhang j (starting from 1).
- X_(j-1) is the lower-right X coordinate of overhang j.
Values have been scaled by 2^-63. Contains
I_MAX + 1
entries as the final value is 0.
-
Y
private static final double[] Y
The precomputed ziggurat heights, denoted Y_i in the main text.- Y_i = height of ziggurat layer i.
- Y_j is the upper-left Y coordinate of overhang j (starting from 1).
- Y_(j-1) is the lower-right Y coordinate of overhang j.
Values have been scaled by 2^-63. Contains
I_MAX + 1
entries as the final value is pdf(x=0).
-
exponential
private final SharedStateContinuousSampler exponential
Exponential sampler used for the long tail.
-
-
Constructor Detail
-
NormalizedGaussian
private NormalizedGaussian(UniformRandomProvider rng)
- Parameters:
rng
- Generator of uniformly distributed random numbers.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
sample
public double sample()
Creates adouble
sample.- Specified by:
sample
in interfaceContinuousSampler
- Returns:
- a sample.
-
edgeSample
private double edgeSample(long xx)
Create the sample from the edge of the ziggurat.This method has been extracted to fit the main sample method within 35 bytes (the default size for a JVM to inline a method).
- Parameters:
xx
- Initial random deviate- Returns:
- a sample
-
selectRegion
private int selectRegion()
Select the overhang region or the tail using alias sampling.- Returns:
- the region
-
withUniformRandomProvider
public ZigguratSampler.NormalizedGaussian 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.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateContinuousSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-
of
public static ZigguratSampler.NormalizedGaussian of(UniformRandomProvider rng)
Create a new normalised Gaussian sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-
-