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 // <random>
11 
12 // class bernoulli_distribution
13 
14 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
15 
16 #include <random>
17 #include <numeric>
18 #include <vector>
19 #include <cassert>
20 
21 template <class T>
22 inline
23 T
sqr(T x)24 sqr(T x)
25 {
26     return x * x;
27 }
28 
main()29 int main()
30 {
31     {
32         typedef std::bernoulli_distribution D;
33         typedef D::param_type P;
34         typedef std::minstd_rand G;
35         G g;
36         D d(.75);
37         P p(.25);
38         const int N = 100000;
39         std::vector<D::result_type> u;
40         for (int i = 0; i < N; ++i)
41             u.push_back(d(g, p));
42         double mean = std::accumulate(u.begin(), u.end(),
43                                               double(0)) / u.size();
44         double var = 0;
45         double skew = 0;
46         double kurtosis = 0;
47         for (int i = 0; i < u.size(); ++i)
48         {
49             double d = (u[i] - mean);
50             double d2 = sqr(d);
51             var += d2;
52             skew += d * d2;
53             kurtosis += d2 * d2;
54         }
55         var /= u.size();
56         double dev = std::sqrt(var);
57         skew /= u.size() * dev * var;
58         kurtosis /= u.size() * var * var;
59         kurtosis -= 3;
60         double x_mean = p.p();
61         double x_var = p.p()*(1-p.p());
62         double x_skew = (1 - 2 * p.p())/std::sqrt(x_var);
63         double x_kurtosis = (6 * sqr(p.p()) - 6 * p.p() + 1)/x_var;
64         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
65         assert(std::abs((var - x_var) / x_var) < 0.01);
66         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
67         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.02);
68     }
69     {
70         typedef std::bernoulli_distribution D;
71         typedef D::param_type P;
72         typedef std::minstd_rand G;
73         G g;
74         D d(.25);
75         P p(.75);
76         const int N = 100000;
77         std::vector<D::result_type> u;
78         for (int i = 0; i < N; ++i)
79             u.push_back(d(g, p));
80         double mean = std::accumulate(u.begin(), u.end(),
81                                               double(0)) / u.size();
82         double var = 0;
83         double skew = 0;
84         double kurtosis = 0;
85         for (int i = 0; i < u.size(); ++i)
86         {
87             double d = (u[i] - mean);
88             double d2 = sqr(d);
89             var += d2;
90             skew += d * d2;
91             kurtosis += d2 * d2;
92         }
93         var /= u.size();
94         double dev = std::sqrt(var);
95         skew /= u.size() * dev * var;
96         kurtosis /= u.size() * var * var;
97         kurtosis -= 3;
98         double x_mean = p.p();
99         double x_var = p.p()*(1-p.p());
100         double x_skew = (1 - 2 * p.p())/std::sqrt(x_var);
101         double x_kurtosis = (6 * sqr(p.p()) - 6 * p.p() + 1)/x_var;
102         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
103         assert(std::abs((var - x_var) / x_var) < 0.01);
104         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
105         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.02);
106     }
107 }
108