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 double
exponent
Exponent parameter of the distribution.private static double
F_1_2
1/2.private static double
F_1_3
1/3.private static double
F_1_4
1/4.private double
hIntegralNumberOfElements
hIntegral(numberOfElements + 0.5)
.private double
hIntegralX1
hIntegral(1.5) - 1
.private int
numberOfElements
Number of elements.private double
r
hIntegralX1 - hIntegralNumberOfElements
.private UniformRandomProvider
rng
Underlying source of randomness.private double
s
2 - hIntegralInverse(hIntegral(2.5) - h(2)
.private static double
TAYLOR_THRESHOLD
Threshold below which Taylor series will be used.
-
Constructor Summary
Constructors Modifier Constructor Description (package private)
RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, int numberOfElements, double exponent)
private
RejectionInversionZipfSamplerImpl(UniformRandomProvider rng, RejectionInversionZipfSampler.RejectionInversionZipfSamplerImpl source)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private 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.java.lang.String
toString()
SharedStateDiscreteSampler
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 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:DiscreteSampler
Creates anint
sample.- Specified by:
sample
in interfaceDiscreteSampler
- Returns:
- a sample.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.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 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.
-
-