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 class RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl
extends Object
implements SharedStateDiscreteSampler
Implements the rejection-inversion method for the Zipf distribution.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final double
Exponent parameter of the distribution.private static final double
1/2.private static final double
1/3.private static final double
1/4.private final double
hIntegral(numberOfElements + 0.5)
.private final double
hIntegral(1.5) - 1
.private final int
Number of elements.private final double
hIntegralX1 - hIntegralNumberOfElements
.private final UniformRandomProvider
Underlying source of randomness.private final double
2 - hIntegralInverse(hIntegral(2.5) - h(2)
.private static final double
Threshold below which Taylor series will be used. -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
RejectionInversionZipfSamplerImpl
(UniformRandomProvider rng, int numberOfElements, double exponent) private
RejectionInversionZipfSamplerImpl
(UniformRandomProvider rng, RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl source) -
Method Summary
Modifier and TypeMethodDescriptionprivate double
h
(double x) h(x) = 1 / x^exponent
.private static double
helper1
(double x) Helper function that calculateslog(1 + x) / x
.private static double
helper2
(double x) Helper function to calculate(exp(x) - 1) / x
.private double
hIntegral
(double x) H(x)
is defined as(x^(1 - exponent) - 1) / (1 - exponent)
, ifexponent != 1
log(x)
, ifexponent == 1
H(x) is an integral function of h(x), the derivative of H(x) is h(x).private double
hIntegralInverse
(double x) The inverse function ofH(x)
.int
sample()
Creates anint
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.DiscreteSampler
samples, samples
-
Field Details
-
TAYLOR_THRESHOLD
private static final double TAYLOR_THRESHOLDThreshold below which Taylor series will be used.- See Also:
-
F_1_2
private static final double F_1_21/2.- See Also:
-
F_1_3
private static final double F_1_31/3.- See Also:
-
F_1_4
private static final double F_1_41/4.- See Also:
-
numberOfElements
private final int numberOfElementsNumber of elements. -
exponent
private final double exponentExponent parameter of the distribution. -
hIntegralX1
private final double hIntegralX1hIntegral(1.5) - 1
. -
hIntegralNumberOfElements
private final double hIntegralNumberOfElementshIntegral(numberOfElements + 0.5)
. -
r
private final double rhIntegralX1 - hIntegralNumberOfElements
. -
s
private final double s2 - hIntegralInverse(hIntegral(2.5) - h(2)
. -
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 anint
sample.- Specified by:
sample
in interfaceDiscreteSampler
- Returns:
- a sample.
-
toString
-
withUniformRandomProvider
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 interfaceSharedStateSampler<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)
, ifexponent != 1
log(x)
, ifexponent == 1
- 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 ofH(x)
.- Parameters:
x
- Free parameter- Returns:
- y for which
H(y) = x
.
-
helper1
private static double helper1(double x) Helper function that calculateslog(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.
-