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 Details

    • R

      private static final double R
      Start of tail.
      See Also:
    • ONE_OVER_R

      private static final double ONE_OVER_R
      Inverse of R.
      See Also:
    • LAST

      private static final int LAST
      Index of last entry in the tables (which have a size that is a power of 2).
      See Also:
    • K

      private static final long[] K
      Auxiliary table.
    • W

      private static final double[] W
      Auxiliary table.
    • F

      private static final double[] F
      Auxiliary table.
    • rng

      private final UniformRandomProvider rng
      Underlying source of randomness.
  • Constructor Details

    • ZigguratNormalizedGaussianSampler

      public ZigguratNormalizedGaussianSampler(UniformRandomProvider rng)
      Create an instance.
      Parameters:
      rng - Generator of uniformly distributed random numbers.
  • Method Details

    • sample

      public double sample()
      Creates a double sample.
      Specified by:
      sample in interface ContinuousSampler
      Returns:
      a sample.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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 to hz.
      Returns:
      the requested random value.
    • pdf

      private static double pdf(double x)
      Compute the Gaussian probability density function f(x) = e^-0.5x^2.
      Parameters:
      x - Argument.
      Returns:
      \( e^{-\frac{x^2}{2}} \)
    • 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.
      Specified by:
      withUniformRandomProvider in interface SharedStateSampler<SharedStateContinuousSampler>
      Parameters:
      rng - Generator of uniformly distributed random numbers.
      Returns:
      the sampler
      Since:
      1.3
    • of

      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