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