Parameters:
For a random variable X whose values are distributed according to this
distribution, the probability mass function is given by
P(X = k) = H(N,s) * 1 / k^s for k = 1,2,...,N.
H(N,s) is the normalizing constant
which corresponds to the generalized harmonic number of order N of s.
N is the number of elementss is the exponent| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | ZipfDistribution.
Utility class implementing a rejection inversion sampling method for a discrete, bounded Zipf distribution that is based on the method described in 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. |
| Modifier and Type | Field and Description |
|---|---|
| private final double | exponent
Exponent parameter of the distribution. |
| private final int | numberOfElements
Number of elements. |
| private double | numericalMean
Cached numerical mean |
| private boolean | numericalMeanIsCalculated
Whether or not the numerical mean has been calculated |
| private double | numericalVariance
Cached numerical variance |
| private boolean | numericalVarianceIsCalculated
Whether or not the numerical variance has been calculated |
| private transient ZipfDistribution. | sampler
The sampler to be used for the sample() method |
| private static final long | serialVersionUID
Hides org. |
| Access | Constructor and Description |
|---|---|
| public | ZipfDistribution(final int
Number of elements. numberOfElements, final double Exponent. exponent)Create a new Zipf distribution with the given number of elements and exponent. |
| public | ZipfDistribution(RandomGenerator
Random number generator. rng, int Number of elements. numberOfElements, double Exponent. exponent)Creates a Zipf distribution. |
| Modifier and Type | Method and Description |
|---|---|
| protected double | |
| protected double | Returns: the variance of this distributionUsed by |
| public double | cumulativeProbability(final int
the point at which the CDF is evaluated x)Implements org. For a random variable |
| private double | Returns: the nth generalized harmonic number.Term in the series to calculate (must be larger than 1) n, final double Exponent (special case m)m = 1 is the harmonic series).Calculates the Nth generalized harmonic number. |
| public double | |
| public int | Returns: the number of elementsGet the number of elements (e.g. corpus size) for the distribution. |
| public double | getNumericalMean()
Implements org. Use this method to get the numerical value of the mean of this distribution. |
| public double | getNumericalVariance()
Implements org. Use this method to get the numerical value of the variance of this distribution. |
| public int | Returns: lower bound of the support (always 1)Implements org. Access the lower bound of the support. |
| public int | Returns: upper bound of the supportImplements org. Access the upper bound of the support. |
| public boolean | Returns: trueImplements org. Use this method to get information about whether the support is connected, i.e. whether all integers between the lower and upper bound of the support are included in the support. |
| public double | logProbability(int
the point at which the PMF is evaluated x)Overrides org. For a random variable |
| public double | probability(final int
the point at which the PMF is evaluated x)Implements org. For a random variable |
| public int | sample()
Overrides org. Implements org. Generate a random value sampled from this distribution. |
| exponent | back to summary |
|---|---|
| private final double exponent Exponent parameter of the distribution. | |
| numberOfElements | back to summary |
|---|---|
| private final int numberOfElements Number of elements. | |
| numericalMean | back to summary |
|---|---|
| private double numericalMean Cached numerical mean | |
| numericalMeanIsCalculated | back to summary |
|---|---|
| private boolean numericalMeanIsCalculated Whether or not the numerical mean has been calculated | |
| numericalVariance | back to summary |
|---|---|
| private double numericalVariance Cached numerical variance | |
| numericalVarianceIsCalculated | back to summary |
|---|---|
| private boolean numericalVarianceIsCalculated Whether or not the numerical variance has been calculated | |
| sampler | back to summary |
|---|---|
| private transient ZipfDistribution. The sampler to be used for the sample() method | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. Serializable version identifier. | |
| ZipfDistribution | back to summary |
|---|---|
| public ZipfDistribution(final int numberOfElements, final double exponent) Create a new Zipf distribution with the given number of elements and exponent. Note this constructor will implicitly create an instance of
| |
| ZipfDistribution | back to summary |
|---|---|
| public ZipfDistribution(RandomGenerator rng, int numberOfElements, double exponent) throws NotStrictlyPositiveException Creates a Zipf distribution.
| |
| calculateNumericalMean | back to summary |
|---|---|
| protected double calculateNumericalMean() Used by
| |
| calculateNumericalVariance | back to summary |
|---|---|
| protected double calculateNumericalVariance() Used by
| |
| cumulativeProbability | back to summary |
|---|---|
| public double cumulativeProbability(final int x) Implements org. Doc from org. For a random variable
| |
| generalizedHarmonic | back to summary |
|---|---|
| private double generalizedHarmonic(final int n, final double m) Calculates the Nth generalized harmonic number. See Harmonic Series.
| |
| getExponent | back to summary |
|---|---|
| public double getExponent() Get the exponent characterizing the distribution.
| |
| getNumberOfElements | back to summary |
|---|---|
| public int getNumberOfElements() Get the number of elements (e.g. corpus size) for the distribution.
| |
| getNumericalMean | back to summary |
|---|---|
| public double getNumericalMean() Implements org. Doc from org. Use this method to get the numerical value of the mean of this
distribution.
For number of elements
| |
| getNumericalVariance | back to summary |
|---|---|
| public double getNumericalVariance() Implements org. Doc from org. Use this method to get the numerical value of the variance of this
distribution.
For number of elements
| |
| getSupportLowerBound | back to summary |
|---|---|
| public int getSupportLowerBound() Implements org. Doc from org. Access the lower bound of the support. This method must return the same
value as
| |
| getSupportUpperBound | back to summary |
|---|---|
| public int getSupportUpperBound() Implements org. Doc from org. Access the upper bound of the support. This method must return the same
value as
| |
| isSupportConnected | back to summary |
|---|---|
| public boolean isSupportConnected() Implements org. Doc from org. Use this method to get information about whether the support is connected, i.e. whether all integers between the lower and upper bound of the support are included in the support. The support of this distribution is connected.
| |
| logProbability | back to summary |
|---|---|
| public double logProbability(int x) Overrides org. Doc from org. For a random variable
The default implementation simply computes the logarithm of
| |
| probability | back to summary |
|---|---|
| public double probability(final int x) Implements org. Doc from org. For a random variable
| |
| sample | back to summary |
|---|---|
| public int sample() Overrides org. Implements org. Doc from org. Generate a random value sampled from this distribution. The default implementation uses the inversion method.
| |
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 final double | exponent
Exponent parameter of the distribution. |
| private final double | hIntegralNumberOfElements
Constant equal to |
| private final double | hIntegralX1
Constant equal to |
| private final int | numberOfElements
Number of elements. |
| private final double | s
Constant equal to |
| Access | Constructor and Description |
|---|---|
| pack-priv | ZipfRejectionInversionSampler(final int
number of elements numberOfElements, final double exponent parameter of the distribution exponent)Simple constructor. |
| Modifier and Type | Method and Description |
|---|---|
| private double | |
| pack-priv static double | Returns: log(1+x)/xa value larger than or equal to -1 x)Helper function that calculates |
| pack-priv static double | Returns: (exp(x)-1)/x if x is non-zero, or 1 if x=0free parameter x)Helper function to calculate |
| private double | Returns: H(x)free parameter x)
|
| private double | Returns: y for whichH(y) = xfree parameter x)The inverse function of H(x). |
| pack-priv int | Returns: generated integral number in the range [1, numberOfElements]random generator to use random)Generate one integral number in the range [1, numberOfElements]. |
| exponent | back to summary |
|---|---|
| private final double exponent Exponent parameter of the distribution. | |
| hIntegralNumberOfElements | back to summary |
|---|---|
| private final double hIntegralNumberOfElements Constant equal to | |
| hIntegralX1 | back to summary |
|---|---|
| private final double hIntegralX1 Constant equal to | |
| numberOfElements | back to summary |
|---|---|
| private final int numberOfElements Number of elements. | |
| s | back to summary |
|---|---|
| private final double s Constant equal to | |
| ZipfRejectionInversionSampler | back to summary |
|---|---|
| pack-priv ZipfRejectionInversionSampler(final int numberOfElements, final double exponent) Simple constructor.
| |
| h | back to summary |
|---|---|
| private double h(final double x)
| |
| helper1 | back to summary |
|---|---|
| pack-priv static double helper1(final double x) Helper function that calculates A Taylor series expansion is used, if x is close to 0.
| |
| helper2 | back to summary |
|---|---|
| pack-priv static double helper2(final double x) Helper function to calculate A Taylor series expansion is used, if x is close to 0.
| |
| hIntegral | back to summary |
|---|---|
| private double hIntegral(final double x)
| |
| hIntegralInverse | back to summary |
|---|---|
| private double hIntegralInverse(final double x) The inverse function of H(x).
| |
| sample | back to summary |
|---|---|
| pack-priv int sample(final RandomGenerator random) Generate one integral number in the range [1, numberOfElements].
| |