Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "absl/random/distributions.h" |
| 16 | |
| 17 | #include <cmath> |
| 18 | #include <cstdint> |
| 19 | #include <random> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "gtest/gtest.h" |
| 23 | #include "absl/random/internal/distribution_test_util.h" |
| 24 | #include "absl/random/random.h" |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | constexpr int kSize = 400000; |
| 29 | |
| 30 | class RandomDistributionsTest : public testing::Test {}; |
| 31 | |
| 32 | TEST_F(RandomDistributionsTest, UniformBoundFunctions) { |
| 33 | using absl::IntervalClosedClosed; |
| 34 | using absl::IntervalClosedOpen; |
| 35 | using absl::IntervalOpenClosed; |
| 36 | using absl::IntervalOpenOpen; |
| 37 | using absl::random_internal::uniform_lower_bound; |
| 38 | using absl::random_internal::uniform_upper_bound; |
| 39 | |
| 40 | // absl::uniform_int_distribution natively assumes IntervalClosedClosed |
| 41 | // absl::uniform_real_distribution natively assumes IntervalClosedOpen |
| 42 | |
| 43 | EXPECT_EQ(uniform_lower_bound(IntervalOpenClosed, 0, 100), 1); |
| 44 | EXPECT_EQ(uniform_lower_bound(IntervalOpenOpen, 0, 100), 1); |
| 45 | EXPECT_GT(uniform_lower_bound<float>(IntervalOpenClosed, 0, 1.0), 0); |
| 46 | EXPECT_GT(uniform_lower_bound<float>(IntervalOpenOpen, 0, 1.0), 0); |
| 47 | EXPECT_GT(uniform_lower_bound<double>(IntervalOpenClosed, 0, 1.0), 0); |
| 48 | EXPECT_GT(uniform_lower_bound<double>(IntervalOpenOpen, 0, 1.0), 0); |
| 49 | |
| 50 | EXPECT_EQ(uniform_lower_bound(IntervalClosedClosed, 0, 100), 0); |
| 51 | EXPECT_EQ(uniform_lower_bound(IntervalClosedOpen, 0, 100), 0); |
| 52 | EXPECT_EQ(uniform_lower_bound<float>(IntervalClosedClosed, 0, 1.0), 0); |
| 53 | EXPECT_EQ(uniform_lower_bound<float>(IntervalClosedOpen, 0, 1.0), 0); |
| 54 | EXPECT_EQ(uniform_lower_bound<double>(IntervalClosedClosed, 0, 1.0), 0); |
| 55 | EXPECT_EQ(uniform_lower_bound<double>(IntervalClosedOpen, 0, 1.0), 0); |
| 56 | |
| 57 | EXPECT_EQ(uniform_upper_bound(IntervalOpenOpen, 0, 100), 99); |
| 58 | EXPECT_EQ(uniform_upper_bound(IntervalClosedOpen, 0, 100), 99); |
| 59 | EXPECT_EQ(uniform_upper_bound<float>(IntervalOpenOpen, 0, 1.0), 1.0); |
| 60 | EXPECT_EQ(uniform_upper_bound<float>(IntervalClosedOpen, 0, 1.0), 1.0); |
| 61 | EXPECT_EQ(uniform_upper_bound<double>(IntervalOpenOpen, 0, 1.0), 1.0); |
| 62 | EXPECT_EQ(uniform_upper_bound<double>(IntervalClosedOpen, 0, 1.0), 1.0); |
| 63 | |
| 64 | EXPECT_EQ(uniform_upper_bound(IntervalOpenClosed, 0, 100), 100); |
| 65 | EXPECT_EQ(uniform_upper_bound(IntervalClosedClosed, 0, 100), 100); |
| 66 | EXPECT_GT(uniform_upper_bound<float>(IntervalOpenClosed, 0, 1.0), 1.0); |
| 67 | EXPECT_GT(uniform_upper_bound<float>(IntervalClosedClosed, 0, 1.0), 1.0); |
| 68 | EXPECT_GT(uniform_upper_bound<double>(IntervalOpenClosed, 0, 1.0), 1.0); |
| 69 | EXPECT_GT(uniform_upper_bound<double>(IntervalClosedClosed, 0, 1.0), 1.0); |
| 70 | |
| 71 | // Negative value tests |
| 72 | EXPECT_EQ(uniform_lower_bound(IntervalOpenClosed, -100, -1), -99); |
| 73 | EXPECT_EQ(uniform_lower_bound(IntervalOpenOpen, -100, -1), -99); |
| 74 | EXPECT_GT(uniform_lower_bound<float>(IntervalOpenClosed, -2.0, -1.0), -2.0); |
| 75 | EXPECT_GT(uniform_lower_bound<float>(IntervalOpenOpen, -2.0, -1.0), -2.0); |
| 76 | EXPECT_GT(uniform_lower_bound<double>(IntervalOpenClosed, -2.0, -1.0), -2.0); |
| 77 | EXPECT_GT(uniform_lower_bound<double>(IntervalOpenOpen, -2.0, -1.0), -2.0); |
| 78 | |
| 79 | EXPECT_EQ(uniform_lower_bound(IntervalClosedClosed, -100, -1), -100); |
| 80 | EXPECT_EQ(uniform_lower_bound(IntervalClosedOpen, -100, -1), -100); |
| 81 | EXPECT_EQ(uniform_lower_bound<float>(IntervalClosedClosed, -2.0, -1.0), -2.0); |
| 82 | EXPECT_EQ(uniform_lower_bound<float>(IntervalClosedOpen, -2.0, -1.0), -2.0); |
| 83 | EXPECT_EQ(uniform_lower_bound<double>(IntervalClosedClosed, -2.0, -1.0), |
| 84 | -2.0); |
| 85 | EXPECT_EQ(uniform_lower_bound<double>(IntervalClosedOpen, -2.0, -1.0), -2.0); |
| 86 | |
| 87 | EXPECT_EQ(uniform_upper_bound(IntervalOpenOpen, -100, -1), -2); |
| 88 | EXPECT_EQ(uniform_upper_bound(IntervalClosedOpen, -100, -1), -2); |
| 89 | EXPECT_EQ(uniform_upper_bound<float>(IntervalOpenOpen, -2.0, -1.0), -1.0); |
| 90 | EXPECT_EQ(uniform_upper_bound<float>(IntervalClosedOpen, -2.0, -1.0), -1.0); |
| 91 | EXPECT_EQ(uniform_upper_bound<double>(IntervalOpenOpen, -2.0, -1.0), -1.0); |
| 92 | EXPECT_EQ(uniform_upper_bound<double>(IntervalClosedOpen, -2.0, -1.0), -1.0); |
| 93 | |
| 94 | EXPECT_EQ(uniform_upper_bound(IntervalOpenClosed, -100, -1), -1); |
| 95 | EXPECT_EQ(uniform_upper_bound(IntervalClosedClosed, -100, -1), -1); |
| 96 | EXPECT_GT(uniform_upper_bound<float>(IntervalOpenClosed, -2.0, -1.0), -1.0); |
| 97 | EXPECT_GT(uniform_upper_bound<float>(IntervalClosedClosed, -2.0, -1.0), -1.0); |
| 98 | EXPECT_GT(uniform_upper_bound<double>(IntervalOpenClosed, -2.0, -1.0), -1.0); |
| 99 | EXPECT_GT(uniform_upper_bound<double>(IntervalClosedClosed, -2.0, -1.0), |
| 100 | -1.0); |
| 101 | |
| 102 | // Edge cases: the next value toward itself is itself. |
| 103 | const double d = 1.0; |
| 104 | const float f = 1.0; |
| 105 | EXPECT_EQ(uniform_lower_bound(IntervalOpenClosed, d, d), d); |
| 106 | EXPECT_EQ(uniform_lower_bound(IntervalOpenClosed, f, f), f); |
| 107 | |
| 108 | EXPECT_GT(uniform_lower_bound(IntervalOpenClosed, 1.0, 2.0), 1.0); |
| 109 | EXPECT_LT(uniform_lower_bound(IntervalOpenClosed, 1.0, +0.0), 1.0); |
| 110 | EXPECT_LT(uniform_lower_bound(IntervalOpenClosed, 1.0, -0.0), 1.0); |
| 111 | EXPECT_LT(uniform_lower_bound(IntervalOpenClosed, 1.0, -1.0), 1.0); |
| 112 | |
| 113 | EXPECT_EQ(uniform_upper_bound(IntervalClosedClosed, 0.0f, |
| 114 | std::numeric_limits<float>::max()), |
| 115 | std::numeric_limits<float>::max()); |
| 116 | EXPECT_EQ(uniform_upper_bound(IntervalClosedClosed, 0.0, |
| 117 | std::numeric_limits<double>::max()), |
| 118 | std::numeric_limits<double>::max()); |
| 119 | } |
| 120 | |
| 121 | struct Invalid {}; |
| 122 | |
| 123 | template <typename A, typename B> |
| 124 | auto InferredUniformReturnT(int) |
| 125 | -> decltype(absl::Uniform(std::declval<absl::InsecureBitGen&>(), |
| 126 | std::declval<A>(), std::declval<B>())); |
| 127 | |
| 128 | template <typename, typename> |
| 129 | Invalid InferredUniformReturnT(...); |
| 130 | |
| 131 | template <typename TagType, typename A, typename B> |
| 132 | auto InferredTaggedUniformReturnT(int) |
| 133 | -> decltype(absl::Uniform(std::declval<TagType>(), |
| 134 | std::declval<absl::InsecureBitGen&>(), |
| 135 | std::declval<A>(), std::declval<B>())); |
| 136 | |
| 137 | template <typename, typename, typename> |
| 138 | Invalid InferredTaggedUniformReturnT(...); |
| 139 | |
| 140 | // Given types <A, B, Expect>, CheckArgsInferType() verifies that |
| 141 | // |
| 142 | // absl::Uniform(gen, A{}, B{}) |
| 143 | // |
| 144 | // returns the type "Expect". |
| 145 | // |
| 146 | // This interface can also be used to assert that a given absl::Uniform() |
| 147 | // overload does not exist / will not compile. Given types <A, B>, the |
| 148 | // expression |
| 149 | // |
| 150 | // decltype(absl::Uniform(..., std::declval<A>(), std::declval<B>())) |
| 151 | // |
| 152 | // will not compile, leaving the definition of InferredUniformReturnT<A, B> to |
| 153 | // resolve (via SFINAE) to the overload which returns type "Invalid". This |
| 154 | // allows tests to assert that an invocation such as |
| 155 | // |
| 156 | // absl::Uniform(gen, 1.23f, std::numeric_limits<int>::max() - 1) |
| 157 | // |
| 158 | // should not compile, since neither type, float nor int, can precisely |
| 159 | // represent both endpoint-values. Writing: |
| 160 | // |
| 161 | // CheckArgsInferType<float, int, Invalid>() |
| 162 | // |
| 163 | // will assert that this overload does not exist. |
| 164 | template <typename A, typename B, typename Expect> |
| 165 | void CheckArgsInferType() { |
| 166 | static_assert( |
| 167 | absl::conjunction< |
| 168 | std::is_same<Expect, decltype(InferredUniformReturnT<A, B>(0))>, |
| 169 | std::is_same<Expect, |
| 170 | decltype(InferredUniformReturnT<B, A>(0))>>::value, |
| 171 | ""); |
| 172 | static_assert( |
| 173 | absl::conjunction< |
| 174 | std::is_same<Expect, decltype(InferredTaggedUniformReturnT< |
| 175 | absl::IntervalOpenOpenTag, A, B>(0))>, |
| 176 | std::is_same<Expect, |
| 177 | decltype(InferredTaggedUniformReturnT< |
| 178 | absl::IntervalOpenOpenTag, B, A>(0))>>::value, |
| 179 | ""); |
| 180 | } |
| 181 | |
| 182 | template <typename A, typename B, typename ExplicitRet> |
| 183 | auto ExplicitUniformReturnT(int) -> decltype( |
| 184 | absl::Uniform<ExplicitRet>(*std::declval<absl::InsecureBitGen*>(), |
| 185 | std::declval<A>(), std::declval<B>())); |
| 186 | |
| 187 | template <typename, typename, typename ExplicitRet> |
| 188 | Invalid ExplicitUniformReturnT(...); |
| 189 | |
| 190 | template <typename TagType, typename A, typename B, typename ExplicitRet> |
| 191 | auto ExplicitTaggedUniformReturnT(int) -> decltype(absl::Uniform<ExplicitRet>( |
| 192 | std::declval<TagType>(), *std::declval<absl::InsecureBitGen*>(), |
| 193 | std::declval<A>(), std::declval<B>())); |
| 194 | |
| 195 | template <typename, typename, typename, typename ExplicitRet> |
| 196 | Invalid ExplicitTaggedUniformReturnT(...); |
| 197 | |
| 198 | // Given types <A, B, Expect>, CheckArgsReturnExpectedType() verifies that |
| 199 | // |
| 200 | // absl::Uniform<Expect>(gen, A{}, B{}) |
| 201 | // |
| 202 | // returns the type "Expect", and that the function-overload has the signature |
| 203 | // |
| 204 | // Expect(URBG&, Expect, Expect) |
| 205 | template <typename A, typename B, typename Expect> |
| 206 | void CheckArgsReturnExpectedType() { |
| 207 | static_assert( |
| 208 | absl::conjunction< |
| 209 | std::is_same<Expect, |
| 210 | decltype(ExplicitUniformReturnT<A, B, Expect>(0))>, |
| 211 | std::is_same<Expect, decltype(ExplicitUniformReturnT<B, A, Expect>( |
| 212 | 0))>>::value, |
| 213 | ""); |
| 214 | static_assert( |
| 215 | absl::conjunction< |
| 216 | std::is_same<Expect, |
| 217 | decltype(ExplicitTaggedUniformReturnT< |
| 218 | absl::IntervalOpenOpenTag, A, B, Expect>(0))>, |
| 219 | std::is_same<Expect, decltype(ExplicitTaggedUniformReturnT< |
| 220 | absl::IntervalOpenOpenTag, B, A, |
| 221 | Expect>(0))>>::value, |
| 222 | ""); |
| 223 | } |
| 224 | |
| 225 | TEST_F(RandomDistributionsTest, UniformTypeInference) { |
| 226 | // Infers common types. |
| 227 | CheckArgsInferType<uint16_t, uint16_t, uint16_t>(); |
| 228 | CheckArgsInferType<uint32_t, uint32_t, uint32_t>(); |
| 229 | CheckArgsInferType<uint64_t, uint64_t, uint64_t>(); |
| 230 | CheckArgsInferType<int16_t, int16_t, int16_t>(); |
| 231 | CheckArgsInferType<int32_t, int32_t, int32_t>(); |
| 232 | CheckArgsInferType<int64_t, int64_t, int64_t>(); |
| 233 | CheckArgsInferType<float, float, float>(); |
| 234 | CheckArgsInferType<double, double, double>(); |
| 235 | |
| 236 | // Explicitly-specified return-values override inferences. |
| 237 | CheckArgsReturnExpectedType<int16_t, int16_t, int32_t>(); |
| 238 | CheckArgsReturnExpectedType<uint16_t, uint16_t, int32_t>(); |
| 239 | CheckArgsReturnExpectedType<int16_t, int16_t, int64_t>(); |
| 240 | CheckArgsReturnExpectedType<int16_t, int32_t, int64_t>(); |
| 241 | CheckArgsReturnExpectedType<int16_t, int32_t, double>(); |
| 242 | CheckArgsReturnExpectedType<float, float, double>(); |
| 243 | CheckArgsReturnExpectedType<int, int, int16_t>(); |
| 244 | |
| 245 | // Properly promotes uint16_t. |
| 246 | CheckArgsInferType<uint16_t, uint32_t, uint32_t>(); |
| 247 | CheckArgsInferType<uint16_t, uint64_t, uint64_t>(); |
| 248 | CheckArgsInferType<uint16_t, int32_t, int32_t>(); |
| 249 | CheckArgsInferType<uint16_t, int64_t, int64_t>(); |
| 250 | CheckArgsInferType<uint16_t, float, float>(); |
| 251 | CheckArgsInferType<uint16_t, double, double>(); |
| 252 | |
| 253 | // Properly promotes int16_t. |
| 254 | CheckArgsInferType<int16_t, int32_t, int32_t>(); |
| 255 | CheckArgsInferType<int16_t, int64_t, int64_t>(); |
| 256 | CheckArgsInferType<int16_t, float, float>(); |
| 257 | CheckArgsInferType<int16_t, double, double>(); |
| 258 | |
| 259 | // Invalid (u)int16_t-pairings do not compile. |
| 260 | // See "CheckArgsInferType" comments above, for how this is achieved. |
| 261 | CheckArgsInferType<uint16_t, int16_t, Invalid>(); |
| 262 | CheckArgsInferType<int16_t, uint32_t, Invalid>(); |
| 263 | CheckArgsInferType<int16_t, uint64_t, Invalid>(); |
| 264 | |
| 265 | // Properly promotes uint32_t. |
| 266 | CheckArgsInferType<uint32_t, uint64_t, uint64_t>(); |
| 267 | CheckArgsInferType<uint32_t, int64_t, int64_t>(); |
| 268 | CheckArgsInferType<uint32_t, double, double>(); |
| 269 | |
| 270 | // Properly promotes int32_t. |
| 271 | CheckArgsInferType<int32_t, int64_t, int64_t>(); |
| 272 | CheckArgsInferType<int32_t, double, double>(); |
| 273 | |
| 274 | // Invalid (u)int32_t-pairings do not compile. |
| 275 | CheckArgsInferType<uint32_t, int32_t, Invalid>(); |
| 276 | CheckArgsInferType<int32_t, uint64_t, Invalid>(); |
| 277 | CheckArgsInferType<int32_t, float, Invalid>(); |
| 278 | CheckArgsInferType<uint32_t, float, Invalid>(); |
| 279 | |
| 280 | // Invalid (u)int64_t-pairings do not compile. |
| 281 | CheckArgsInferType<uint64_t, int64_t, Invalid>(); |
| 282 | CheckArgsInferType<int64_t, float, Invalid>(); |
| 283 | CheckArgsInferType<int64_t, double, Invalid>(); |
| 284 | |
| 285 | // Properly promotes float. |
| 286 | CheckArgsInferType<float, double, double>(); |
| 287 | |
| 288 | // Examples. |
| 289 | absl::InsecureBitGen gen; |
| 290 | EXPECT_NE(1, absl::Uniform(gen, static_cast<uint16_t>(0), 1.0f)); |
| 291 | EXPECT_NE(1, absl::Uniform(gen, 0, 1.0)); |
| 292 | EXPECT_NE(1, absl::Uniform(absl::IntervalOpenOpen, gen, |
| 293 | static_cast<uint16_t>(0), 1.0f)); |
| 294 | EXPECT_NE(1, absl::Uniform(absl::IntervalOpenOpen, gen, 0, 1.0)); |
| 295 | EXPECT_NE(1, absl::Uniform(absl::IntervalOpenOpen, gen, -1, 1.0)); |
| 296 | EXPECT_NE(1, absl::Uniform<double>(absl::IntervalOpenOpen, gen, -1, 1)); |
| 297 | EXPECT_NE(1, absl::Uniform<float>(absl::IntervalOpenOpen, gen, 0, 1)); |
| 298 | EXPECT_NE(1, absl::Uniform<float>(gen, 0, 1)); |
| 299 | } |
| 300 | |
| 301 | TEST_F(RandomDistributionsTest, UniformNoBounds) { |
| 302 | absl::InsecureBitGen gen; |
| 303 | |
| 304 | absl::Uniform<uint8_t>(gen); |
| 305 | absl::Uniform<uint16_t>(gen); |
| 306 | absl::Uniform<uint32_t>(gen); |
| 307 | absl::Uniform<uint64_t>(gen); |
| 308 | } |
| 309 | |
| 310 | // TODO(lar): Validate properties of non-default interval-semantics. |
| 311 | TEST_F(RandomDistributionsTest, UniformReal) { |
| 312 | std::vector<double> values(kSize); |
| 313 | |
| 314 | absl::InsecureBitGen gen; |
| 315 | for (int i = 0; i < kSize; i++) { |
| 316 | values[i] = absl::Uniform(gen, 0, 1.0); |
| 317 | } |
| 318 | |
| 319 | const auto moments = |
| 320 | absl::random_internal::ComputeDistributionMoments(values); |
| 321 | EXPECT_NEAR(0.5, moments.mean, 0.02); |
| 322 | EXPECT_NEAR(1 / 12.0, moments.variance, 0.02); |
| 323 | EXPECT_NEAR(0.0, moments.skewness, 0.02); |
| 324 | EXPECT_NEAR(9 / 5.0, moments.kurtosis, 0.02); |
| 325 | } |
| 326 | |
| 327 | TEST_F(RandomDistributionsTest, UniformInt) { |
| 328 | std::vector<double> values(kSize); |
| 329 | |
| 330 | absl::InsecureBitGen gen; |
| 331 | for (int i = 0; i < kSize; i++) { |
| 332 | const int64_t kMax = 1000000000000ll; |
| 333 | int64_t j = absl::Uniform(absl::IntervalClosedClosed, gen, 0, kMax); |
| 334 | // convert to double. |
| 335 | values[i] = static_cast<double>(j) / static_cast<double>(kMax); |
| 336 | } |
| 337 | |
| 338 | const auto moments = |
| 339 | absl::random_internal::ComputeDistributionMoments(values); |
| 340 | EXPECT_NEAR(0.5, moments.mean, 0.02); |
| 341 | EXPECT_NEAR(1 / 12.0, moments.variance, 0.02); |
| 342 | EXPECT_NEAR(0.0, moments.skewness, 0.02); |
| 343 | EXPECT_NEAR(9 / 5.0, moments.kurtosis, 0.02); |
| 344 | |
| 345 | /* |
| 346 | // NOTE: These are not supported by absl::Uniform, which is specialized |
| 347 | // on integer and real valued types. |
| 348 | |
| 349 | enum E { E0, E1 }; // enum |
| 350 | enum S : int { S0, S1 }; // signed enum |
| 351 | enum U : unsigned int { U0, U1 }; // unsigned enum |
| 352 | |
| 353 | absl::Uniform(gen, E0, E1); |
| 354 | absl::Uniform(gen, S0, S1); |
| 355 | absl::Uniform(gen, U0, U1); |
| 356 | */ |
| 357 | } |
| 358 | |
| 359 | TEST_F(RandomDistributionsTest, Exponential) { |
| 360 | std::vector<double> values(kSize); |
| 361 | |
| 362 | absl::InsecureBitGen gen; |
| 363 | for (int i = 0; i < kSize; i++) { |
| 364 | values[i] = absl::Exponential<double>(gen); |
| 365 | } |
| 366 | |
| 367 | const auto moments = |
| 368 | absl::random_internal::ComputeDistributionMoments(values); |
| 369 | EXPECT_NEAR(1.0, moments.mean, 0.02); |
| 370 | EXPECT_NEAR(1.0, moments.variance, 0.025); |
| 371 | EXPECT_NEAR(2.0, moments.skewness, 0.1); |
| 372 | EXPECT_LT(5.0, moments.kurtosis); |
| 373 | } |
| 374 | |
| 375 | TEST_F(RandomDistributionsTest, PoissonDefault) { |
| 376 | std::vector<double> values(kSize); |
| 377 | |
| 378 | absl::InsecureBitGen gen; |
| 379 | for (int i = 0; i < kSize; i++) { |
| 380 | values[i] = absl::Poisson<int64_t>(gen); |
| 381 | } |
| 382 | |
| 383 | const auto moments = |
| 384 | absl::random_internal::ComputeDistributionMoments(values); |
| 385 | EXPECT_NEAR(1.0, moments.mean, 0.02); |
| 386 | EXPECT_NEAR(1.0, moments.variance, 0.02); |
| 387 | EXPECT_NEAR(1.0, moments.skewness, 0.025); |
| 388 | EXPECT_LT(2.0, moments.kurtosis); |
| 389 | } |
| 390 | |
| 391 | TEST_F(RandomDistributionsTest, PoissonLarge) { |
| 392 | constexpr double kMean = 100000000.0; |
| 393 | std::vector<double> values(kSize); |
| 394 | |
| 395 | absl::InsecureBitGen gen; |
| 396 | for (int i = 0; i < kSize; i++) { |
| 397 | values[i] = absl::Poisson<int64_t>(gen, kMean); |
| 398 | } |
| 399 | |
| 400 | const auto moments = |
| 401 | absl::random_internal::ComputeDistributionMoments(values); |
| 402 | EXPECT_NEAR(kMean, moments.mean, kMean * 0.015); |
| 403 | EXPECT_NEAR(kMean, moments.variance, kMean * 0.015); |
| 404 | EXPECT_NEAR(std::sqrt(kMean), moments.skewness, kMean * 0.02); |
| 405 | EXPECT_LT(2.0, moments.kurtosis); |
| 406 | } |
| 407 | |
| 408 | TEST_F(RandomDistributionsTest, Bernoulli) { |
| 409 | constexpr double kP = 0.5151515151; |
| 410 | std::vector<double> values(kSize); |
| 411 | |
| 412 | absl::InsecureBitGen gen; |
| 413 | for (int i = 0; i < kSize; i++) { |
| 414 | values[i] = absl::Bernoulli(gen, kP); |
| 415 | } |
| 416 | |
| 417 | const auto moments = |
| 418 | absl::random_internal::ComputeDistributionMoments(values); |
| 419 | EXPECT_NEAR(kP, moments.mean, 0.01); |
| 420 | } |
| 421 | |
| 422 | TEST_F(RandomDistributionsTest, Beta) { |
| 423 | constexpr double kAlpha = 2.0; |
| 424 | constexpr double kBeta = 3.0; |
| 425 | std::vector<double> values(kSize); |
| 426 | |
| 427 | absl::InsecureBitGen gen; |
| 428 | for (int i = 0; i < kSize; i++) { |
| 429 | values[i] = absl::Beta(gen, kAlpha, kBeta); |
| 430 | } |
| 431 | |
| 432 | const auto moments = |
| 433 | absl::random_internal::ComputeDistributionMoments(values); |
| 434 | EXPECT_NEAR(0.4, moments.mean, 0.01); |
| 435 | } |
| 436 | |
| 437 | TEST_F(RandomDistributionsTest, Zipf) { |
| 438 | std::vector<double> values(kSize); |
| 439 | |
| 440 | absl::InsecureBitGen gen; |
| 441 | for (int i = 0; i < kSize; i++) { |
| 442 | values[i] = absl::Zipf<int64_t>(gen, 100); |
| 443 | } |
| 444 | |
| 445 | // The mean of a zipf distribution is: H(N, s-1) / H(N,s). |
| 446 | // Given the parameter v = 1, this gives the following function: |
| 447 | // (Hn(100, 1) - Hn(1,1)) / (Hn(100,2) - Hn(1,2)) = 6.5944 |
| 448 | const auto moments = |
| 449 | absl::random_internal::ComputeDistributionMoments(values); |
| 450 | EXPECT_NEAR(6.5944, moments.mean, 2000) << moments; |
| 451 | } |
| 452 | |
| 453 | TEST_F(RandomDistributionsTest, Gaussian) { |
| 454 | std::vector<double> values(kSize); |
| 455 | |
| 456 | absl::InsecureBitGen gen; |
| 457 | for (int i = 0; i < kSize; i++) { |
| 458 | values[i] = absl::Gaussian<double>(gen); |
| 459 | } |
| 460 | |
| 461 | const auto moments = |
| 462 | absl::random_internal::ComputeDistributionMoments(values); |
| 463 | EXPECT_NEAR(0.0, moments.mean, 0.02); |
| 464 | EXPECT_NEAR(1.0, moments.variance, 0.04); |
| 465 | EXPECT_NEAR(0, moments.skewness, 0.2); |
| 466 | EXPECT_NEAR(3.0, moments.kurtosis, 0.5); |
| 467 | } |
| 468 | |
| 469 | TEST_F(RandomDistributionsTest, LogUniform) { |
| 470 | std::vector<double> values(kSize); |
| 471 | |
| 472 | absl::InsecureBitGen gen; |
| 473 | for (int i = 0; i < kSize; i++) { |
| 474 | values[i] = absl::LogUniform<int64_t>(gen, 0, (1 << 10) - 1); |
| 475 | } |
| 476 | |
| 477 | // The mean is the sum of the fractional means of the uniform distributions: |
| 478 | // [0..0][1..1][2..3][4..7][8..15][16..31][32..63] |
| 479 | // [64..127][128..255][256..511][512..1023] |
| 480 | const double mean = (0 + 1 + 1 + 2 + 3 + 4 + 7 + 8 + 15 + 16 + 31 + 32 + 63 + |
| 481 | 64 + 127 + 128 + 255 + 256 + 511 + 512 + 1023) / |
| 482 | (2.0 * 11.0); |
| 483 | |
| 484 | const auto moments = |
| 485 | absl::random_internal::ComputeDistributionMoments(values); |
| 486 | EXPECT_NEAR(mean, moments.mean, 2) << moments; |
| 487 | } |
| 488 | |
| 489 | } // namespace |