Commit feb6aab8 by Aaron Jacobs Committed by Copybara-Service

distributions: support a zero max value in Zipf.

There is no documentation that says zero isn't okay, and the closed interval
[0, k] described by the documentation is perfectly well-defined even when k is
zero. As far as I can tell, there is no reason *not* to support zero: a random
variable that always returns the same value is still a random variable.
absl::Uniform will happily generate on the interval [0, 1) for the same
reason.
PiperOrigin-RevId: 694649518
Change-Id: Ib940406f762a30e27c19c846c45bd908ae8411c3
parent 89245097
......@@ -470,6 +470,13 @@ TEST_F(RandomDistributionsTest, Zipf) {
EXPECT_NEAR(6.5944, moments.mean, 2000) << moments;
}
TEST_F(RandomDistributionsTest, ZipfWithZeroMax) {
absl::InsecureBitGen gen;
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(0, absl::Zipf(gen, 0));
}
}
TEST_F(RandomDistributionsTest, Gaussian) {
std::vector<double> values(kSize);
......
......@@ -57,7 +57,7 @@ class zipf_distribution {
public:
using distribution_type = zipf_distribution;
// Preconditions: k > 0, v > 0, q > 1
// Preconditions: k >= 0, v > 0, q > 1
// The precondidtions are validated when NDEBUG is not defined via
// a pair of assert() directives.
// If NDEBUG is defined and either or both of these parameters take invalid
......@@ -152,7 +152,7 @@ zipf_distribution<IntType>::param_type::param_type(
: k_(k), q_(q), v_(v), one_minus_q_(1 - q) {
assert(q > 1);
assert(v > 0);
assert(k > 0);
assert(k >= 0);
one_minus_q_inv_ = 1 / one_minus_q_;
// Setup for the ZRI algorithm (pg 17 of the paper).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment