static final class ZipfDistribution.ZipfRejectionInversionSampler
extends java.lang.Object
Wolfgang Hörmann and Gerhard Derflinger "Rejection-inversion to generate variates from monotone discrete distributions." ACM Transactions on Modeling and Computer Simulation (TOMACS) 6.3 (1996): 169-184.
The paper describes an algorithm for exponents larger than 1 (Algorithm ZRI).
The original method uses H(x) := (v + x)^(1 - q) / (1 - q)
as the integral of the hat function. This function is undefined for
q = 1, which is the reason for the limitation of the exponent.
If instead the integral function
H(x) := ((v + x)^(1 - q) - 1) / (1 - q) is used,
for which a meaningful limit exists for q = 1,
the method works for all positive exponents.
The following implementation uses v := 0 and generates integral numbers in the range [1, numberOfElements]. This is different to the original method where v is defined to be positive and numbers are taken from [0, i_max]. This explains why the implementation looks slightly different.
| Modifier and Type | Field and Description |
|---|---|
private double |
exponent
Exponent parameter of the distribution.
|
private double |
hIntegralNumberOfElements
Constant equal to
hIntegral(numberOfElements + 0.5). |
private double |
hIntegralX1
Constant equal to
hIntegral(1.5) - 1. |
private int |
numberOfElements
Number of elements.
|
private double |
s
Constant equal to
2 - hIntegralInverse(hIntegral(2.5) - h(2). |
| Constructor and Description |
|---|
ZipfDistribution.ZipfRejectionInversionSampler(int numberOfElements,
double exponent)
Simple constructor.
|
| Modifier and Type | Method and Description |
|---|---|
private double |
h(double x)
h(x) := 1/x^exponent |
(package private) static double |
helper1(double x)
Helper function that calculates
log(1+x)/x. |
(package private) static double |
helper2(double x)
Helper function to calculate
(exp(x)-1)/x. |
private double |
hIntegral(double x)
H(x) :=
(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). |
private double |
hIntegralInverse(double x)
The inverse function of H(x).
|
(package private) int |
sample(RandomGenerator random)
Generate one integral number in the range [1, numberOfElements].
|
private final double exponent
private final int numberOfElements
private final double hIntegralX1
hIntegral(1.5) - 1.private final double hIntegralNumberOfElements
hIntegral(numberOfElements + 0.5).private final double s
2 - hIntegralInverse(hIntegral(2.5) - h(2).ZipfDistribution.ZipfRejectionInversionSampler(int numberOfElements,
double exponent)
numberOfElements - number of elementsexponent - exponent parameter of the distributionint sample(RandomGenerator random)
random - random generator to useprivate double hIntegral(double x)
H(x) :=
(x^(1-exponent) - 1)/(1 - exponent), if exponent != 1log(x), if exponent == 1x - free parameterH(x)private double h(double x)
h(x) := 1/x^exponentx - free parameterprivate double hIntegralInverse(double x)
x - free parameterH(y) = xstatic double helper1(double x)
log(1+x)/x.
A Taylor series expansion is used, if x is close to 0.
x - a value larger than or equal to -1log(1+x)/xstatic double helper2(double x)
(exp(x)-1)/x.
A Taylor series expansion is used, if x is close to 0.
x - free parameter(exp(x)-1)/x if x is non-zero, or 1 if x=0Copyright (c) 2003-2017 Apache Software Foundation