Class ZipfDistribution
- All Implemented Interfaces:
DiscreteDistribution
The probability mass function of \( X \) is:
\[ f(k; N, s) = \frac{1/k^s}{H_{N,s}} \]
for \( N \in \{1, 2, 3, \dots\} \) the number of elements, \( s \gt 0 \) the exponent characterizing the distribution, \( k \in \{1, 2, \dots, N\} \) the element rank, and \( H_{N,s} \) is the normalizing constant which corresponds to the generalized harmonic number of order N of s.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface DiscreteDistribution
DiscreteDistribution.Sampler -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final doubleExponent parameter of the distribution.private final doubleCached value of the log of the nth generalized harmonic.private final doubleCached value of the nth generalized harmonic.private final intNumber of elements. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateSampler(org.apache.commons.rng.UniformRandomProvider rng) Creates a sampler.doublecumulativeProbability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnsP(X <= x).private static doublegeneralizedHarmonic(int n, double m) Calculates the Nth generalized harmonic number.private static doublegeneralizedHarmonicAscendingSum(int n, double m) Calculates the Nth generalized harmonic number.doubleGets the exponent parameter of this distribution.doublegetMean()Gets the mean of this distribution.intGets the number of elements parameter of this distribution.intGets the lower bound of the support.intGets the upper bound of the support.doubleGets the variance of this distribution.doublelogProbability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnslog(P(X = x)), wherelogis the natural logarithm.static ZipfDistributionof(int numberOfElements, double exponent) Creates a Zipf distribution.doubleprobability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnsP(X = x).doublesurvivalProbability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnsP(X > x).Methods inherited from class AbstractDiscreteDistribution
getMedian, inverseCumulativeProbability, inverseSurvivalProbability, probability
-
Field Details
-
numberOfElements
private final int numberOfElementsNumber of elements. -
exponent
private final double exponentExponent parameter of the distribution. -
nthHarmonic
private final double nthHarmonicCached value of the nth generalized harmonic. -
logNthHarmonic
private final double logNthHarmonicCached value of the log of the nth generalized harmonic.
-
-
Constructor Details
-
ZipfDistribution
private ZipfDistribution(int numberOfElements, double exponent) - Parameters:
numberOfElements- Number of elements.exponent- Exponent.
-
-
Method Details
-
of
Creates a Zipf distribution.- Parameters:
numberOfElements- Number of elements.exponent- Exponent.- Returns:
- the distribution
- Throws:
IllegalArgumentException- ifnumberOfElements <= 0orexponent <= 0.
-
getNumberOfElements
public int getNumberOfElements()Gets the number of elements parameter of this distribution.- Returns:
- the number of elements.
-
getExponent
public double getExponent()Gets the exponent parameter of this distribution.- Returns:
- the exponent.
-
probability
public double probability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnsP(X = x). In other words, this method represents the probability mass function (PMF) for the distribution.- Parameters:
x- Point at which the PMF is evaluated.- Returns:
- the value of the probability mass function at
x.
-
logProbability
public double logProbability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnslog(P(X = x)), wherelogis the natural logarithm.- Parameters:
x- Point at which the PMF is evaluated.- Returns:
- the logarithm of the value of the probability mass function at
x.
-
cumulativeProbability
public double cumulativeProbability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnsP(X <= x). In other, words, this method represents the (cumulative) distribution function (CDF) for this distribution.- Parameters:
x- Point at which the CDF is evaluated.- Returns:
- the probability that a random variable with this distribution
takes a value less than or equal to
x.
-
survivalProbability
public double survivalProbability(int x) For a random variableXwhose values are distributed according to this distribution, this method returnsP(X > x). In other words, this method represents the complementary cumulative distribution function.By default, this is defined as
1 - cumulativeProbability(x), but the specific implementation may be more accurate.- Parameters:
x- Point at which the survival function is evaluated.- Returns:
- the probability that a random variable with this
distribution takes a value greater than
x.
-
getMean
public double getMean()Gets the mean of this distribution.For number of elements \( N \) and exponent \( s \), the mean is:
\[ \frac{H_{N,s-1}}{H_{N,s}} \]
where \( H_{N,k} \) is the generalized harmonic number of order \( N \) of \( k \).
- Returns:
- the mean.
-
getVariance
public double getVariance()Gets the variance of this distribution.For number of elements \( N \) and exponent \( s \), the variance is:
\[ \frac{H_{N,s-2}}{H_{N,s}} - \frac{H_{N,s-1}^2}{H_{N,s}^2} \]
where \( H_{N,k} \) is the generalized harmonic number of order \( N \) of \( k \).
- Returns:
- the variance.
-
generalizedHarmonic
private static double generalizedHarmonic(int n, double m) Calculates the Nth generalized harmonic number. See Harmonic Series.Assumes
exponent > 0to arrange the terms to sum from small to large.- Parameters:
n- Term in the series to calculate (must be larger than 1)m- Exponent (special casem = 1is the harmonic series).- Returns:
- the nth generalized harmonic number.
-
generalizedHarmonicAscendingSum
private static double generalizedHarmonicAscendingSum(int n, double m) Calculates the Nth generalized harmonic number.Checks the value of the
exponentto arrange the terms to sum from from small to large.- Parameters:
n- Term in the series to calculate (must be larger than 1)m- Exponent (special casem = 1is the harmonic series).- Returns:
- the nth generalized harmonic number.
-
getSupportLowerBound
public int getSupportLowerBound()Gets the lower bound of the support. This method must return the same value asinverseCumulativeProbability(0), i.e. \( \inf \{ x \in \mathbb Z : P(X \le x) \gt 0 \} \). By convention,Integer.MIN_VALUEshould be substituted for negative infinity.The lower bound of the support is always 1.
- Returns:
- 1.
-
getSupportUpperBound
public int getSupportUpperBound()Gets the upper bound of the support. This method must return the same value asinverseCumulativeProbability(1), i.e. \( \inf \{ x \in \mathbb Z : P(X \le x) = 1 \} \). By convention,Integer.MAX_VALUEshould be substituted for positive infinity.The upper bound of the support is the number of elements.
- Returns:
- number of elements.
-
createSampler
Creates a sampler.- Specified by:
createSamplerin interfaceDiscreteDistribution- Overrides:
createSamplerin classAbstractDiscreteDistribution- Parameters:
rng- Generator of uniformly distributed numbers.- Returns:
- a sampler that produces random numbers according this distribution.
-