]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/numerics/rand/rand.dis/rand.dist.pois/rand.dist.pois.exp/eval_param.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / numerics / rand / rand.dis / rand.dist.pois / rand.dist.pois.exp / eval_param.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // REQUIRES: long_tests
11
12 // <random>
13
14 // template<class RealType = double>
15 // class exponential_distribution
16
17 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
18
19 #include <random>
20 #include <cassert>
21 #include <vector>
22 #include <numeric>
23 #include <cstddef>
24
25 template <class T>
26 inline
27 T
28 sqr(T x)
29 {
30     return x * x;
31 }
32
33 int main()
34 {
35     {
36         typedef std::exponential_distribution<> D;
37         typedef D::param_type P;
38         typedef std::mt19937 G;
39         G g;
40         D d(.75);
41         P p(2);
42         const int N = 1000000;
43         std::vector<D::result_type> u;
44         for (int i = 0; i < N; ++i)
45         {
46             D::result_type v = d(g, p);
47             assert(d.min() < v);
48             u.push_back(v);
49         }
50         double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
51         double var = 0;
52         double skew = 0;
53         double kurtosis = 0;
54         for (std::size_t i = 0; i < u.size(); ++i)
55         {
56             double dbl = (u[i] - mean);
57             double d2 = sqr(dbl);
58             var += d2;
59             skew += dbl * d2;
60             kurtosis += d2 * d2;
61         }
62         var /= u.size();
63         double dev = std::sqrt(var);
64         skew /= u.size() * dev * var;
65         kurtosis /= u.size() * var * var;
66         kurtosis -= 3;
67         double x_mean = 1/p.lambda();
68         double x_var = 1/sqr(p.lambda());
69         double x_skew = 2;
70         double x_kurtosis = 6;
71         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
72         assert(std::abs((var - x_var) / x_var) < 0.01);
73         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
74         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
75     }
76 }