Class RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl

java.lang.Object
org.apache.commons.rng.sampling.distribution.RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl
All Implemented Interfaces:
DiscreteSampler, SharedStateDiscreteSampler, SharedStateSampler<SharedStateDiscreteSampler>
Enclosing class:
RejectionInversionZipfSampler

private static final class RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl extends Object implements SharedStateDiscreteSampler
Implements the rejection-inversion method for the Zipf distribution.
  • Field Details

    • TAYLOR_THRESHOLD

      private static final double TAYLOR_THRESHOLD
      Threshold below which Taylor series will be used.
      See Also:
    • F_1_2

      private static final double F_1_2
      1/2.
      See Also:
    • F_1_3

      private static final double F_1_3
      1/3.
      See Also:
    • F_1_4

      private static final double F_1_4
      1/4.
      See Also:
    • numberOfElements

      private final int numberOfElements
      Number of elements.
    • exponent

      private final double exponent
      Exponent parameter of the distribution.
    • hIntegralX1

      private final double hIntegralX1
      hIntegral(1.5) - 1.
    • hIntegralNumberOfElements

      private final double hIntegralNumberOfElements
      hIntegral(numberOfElements + 0.5).
    • r

      private final double r
      hIntegralX1 - hIntegralNumberOfElements.
    • s

      private final double s
      2 - hIntegralInverse(hIntegral(2.5) - h(2).
    • rng

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

    • RejectionInversionZipfSamplerImpl

      RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, int numberOfElements, double exponent)
      Parameters:
      rng - Generator of uniformly distributed random numbers.
      numberOfElements - Number of elements (must be > 0).
      exponent - Exponent (must be > 0).
    • RejectionInversionZipfSamplerImpl

      private RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl source)
      Parameters:
      rng - Generator of uniformly distributed random numbers.
      source - Source to copy.
  • Method Details

    • sample

      public int sample()
      Description copied from interface: DiscreteSampler
      Creates an int sample.
      Specified by:
      sample in interface DiscreteSampler
      Returns:
      a sample.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • withUniformRandomProvider

      public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng)
      Description copied from interface: SharedStateSampler
      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<SharedStateDiscreteSampler>
      Parameters:
      rng - Generator of uniformly distributed random numbers.
      Returns:
      the sampler
    • hIntegral

      private double hIntegral(double x)
      H(x) is defined as
      • (x^(1 - exponent) - 1) / (1 - exponent), if exponent != 1
      • log(x), if exponent == 1
      H(x) is an integral function of h(x), the derivative of H(x) is h(x).
      Parameters:
      x - Free parameter.
      Returns:
      H(x).
    • h

      private double h(double x)
      h(x) = 1 / x^exponent.
      Parameters:
      x - Free parameter.
      Returns:
      h(x).
    • hIntegralInverse

      private double hIntegralInverse(double x)
      The inverse function of H(x).
      Parameters:
      x - Free parameter
      Returns:
      y for which H(y) = x.
    • helper1

      private static double helper1(double x)
      Helper function that calculates log(1 + x) / x.

      A Taylor series expansion is used, if x is close to 0.

      Parameters:
      x - A value larger than or equal to -1.
      Returns:
      log(1 + x) / x.
    • helper2

      private static double helper2(double x)
      Helper function to calculate (exp(x) - 1) / x.

      A Taylor series expansion is used, if x is close to 0.

      Parameters:
      x - Free parameter.
      Returns:
      (exp(x) - 1) / x if x is non-zero, or 1 if x = 0.