| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // REQUIRES: long_tests |
| 10 | |
| 11 | // <random> |
| 12 | |
| 13 | // template<class RealType = double> |
| 14 | // class exponential_distribution |
| 15 | |
| 16 | // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm); |
| 17 | |
| 18 | #include <random> |
| 19 | #include <cassert> |
| 20 | #include <cmath> |
| 21 | #include <cstddef> |
| 22 | #include <numeric> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | template <class T> |
| 28 | inline |
| 29 | T |
| 30 | sqr(T x) |
| 31 | { |
| 32 | return x * x; |
| 33 | } |
| 34 | |
| 35 | int main(int, char**) |
| 36 | { |
| 37 | { |
| 38 | typedef std::exponential_distribution<> D; |
| 39 | typedef D::param_type P; |
| 40 | typedef std::mt19937 G; |
| 41 | G g; |
| 42 | D d(.75); |
| 43 | P p(2); |
| 44 | const int N = 1000000; |
| 45 | std::vector<D::result_type> u; |
| 46 | for (int i = 0; i < N; ++i) |
| 47 | { |
| 48 | D::result_type v = d(g, p); |
| 49 | assert(d.min() < v); |
| 50 | u.push_back(x: v); |
| 51 | } |
| 52 | double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); |
| 53 | double var = 0; |
| 54 | double skew = 0; |
| 55 | double kurtosis = 0; |
| 56 | for (std::size_t i = 0; i < u.size(); ++i) |
| 57 | { |
| 58 | double dbl = (u[i] - mean); |
| 59 | double d2 = sqr(dbl); |
| 60 | var += d2; |
| 61 | skew += dbl * d2; |
| 62 | kurtosis += d2 * d2; |
| 63 | } |
| 64 | var /= u.size(); |
| 65 | double dev = std::sqrt(x: var); |
| 66 | skew /= u.size() * dev * var; |
| 67 | kurtosis /= u.size() * var * var; |
| 68 | kurtosis -= 3; |
| 69 | double x_mean = 1/p.lambda(); |
| 70 | double x_var = 1/sqr(p.lambda()); |
| 71 | double x_skew = 2; |
| 72 | double x_kurtosis = 6; |
| 73 | assert(std::abs((mean - x_mean) / x_mean) < 0.01); |
| 74 | assert(std::abs((var - x_var) / x_var) < 0.01); |
| 75 | assert(std::abs((skew - x_skew) / x_skew) < 0.01); |
| 76 | assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03); |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 |
