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

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.bern/rand.dist.bern.bernoulli/eval_param.pass.cpp