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 java.lang.Object implements SharedStateDiscreteSampler
Implements the rejection-inversion method for the Zipf distribution.
-
-
Field Summary
Fields Modifier and Type Field Description private doubleexponentExponent parameter of the distribution.private static doubleF_1_21/2.private static doubleF_1_31/3.private static doubleF_1_41/4.private doublehIntegralNumberOfElementshIntegral(numberOfElements + 0.5).private doublehIntegralX1hIntegral(1.5) - 1.private intnumberOfElementsNumber of elements.private doublerhIntegralX1 - hIntegralNumberOfElements.private UniformRandomProviderrngUnderlying source of randomness.private doubles2 - hIntegralInverse(hIntegral(2.5) - h(2).private static doubleTAYLOR_THRESHOLDThreshold below which Taylor series will be used.
-
Constructor Summary
Constructors Modifier Constructor Description (package private)RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, int numberOfElements, double exponent)privateRejectionInversionZipfSamplerImpl(UniformRandomProvider rng, RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl source)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private doubleh(double x)h(x) = 1 / x^exponent.private static doublehelper1(double x)Helper function that calculateslog(1 + x) / x.private static doublehelper2(double x)Helper function to calculate(exp(x) - 1) / x.private doublehIntegral(double x)H(x)is defined as(x^(1 - exponent) - 1) / (1 - exponent), ifexponent != 1log(x), ifexponent == 1H(x) is an integral function of h(x), the derivative of H(x) is h(x).private doublehIntegralInverse(double x)The inverse function ofH(x).intsample()Creates anintsample.java.lang.StringtoString()SharedStateDiscreteSamplerwithUniformRandomProvider(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 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 Detail
-
TAYLOR_THRESHOLD
private static final double TAYLOR_THRESHOLD
Threshold below which Taylor series will be used.- See Also:
- Constant Field Values
-
F_1_2
private static final double F_1_2
1/2.- See Also:
- Constant Field Values
-
F_1_3
private static final double F_1_3
1/3.- See Also:
- Constant Field Values
-
F_1_4
private static final double F_1_4
1/4.- See Also:
- Constant Field Values
-
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 Detail
-
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 Detail
-
sample
public int sample()
Description copied from interface:DiscreteSamplerCreates anintsample.- Specified by:
samplein interfaceDiscreteSampler- Returns:
- a sample.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
withUniformRandomProvider
public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider rng)
Description copied from interface:SharedStateSamplerCreate a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProviderin 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 != 1log(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) / xif x is non-zero, or 1 if x = 0.
-
-