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 // template<class IntType = int>
13 // class negative_binomial_distribution
14
15 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
16
17 #include <random>
18 #include <numeric>
19 #include <vector>
20 #include <cassert>
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::negative_binomial_distribution<> D;
34 typedef D::param_type P;
35 typedef std::minstd_rand G;
36 G g;
37 D d(16, .75);
38 P p(5, .75);
39 const int N = 1000000;
40 std::vector<D::result_type> u;
41 for (int i = 0; i < N; ++i)
42 {
43 D::result_type v = d(g, p);
44 assert(d.min() <= v && v <= d.max());
45 u.push_back(v);
46 }
47 double mean = std::accumulate(u.begin(), u.end(),
48 double(0)) / u.size();
49 double var = 0;
50 double skew = 0;
51 double kurtosis = 0;
52 for (int i = 0; i < u.size(); ++i)
53 {
54 double d = (u[i] - mean);
55 double d2 = sqr(d);
56 var += d2;
57 skew += d * d2;
58 kurtosis += d2 * d2;
59 }
60 var /= u.size();
61 double dev = std::sqrt(var);
62 skew /= u.size() * dev * var;
63 kurtosis /= u.size() * var * var;
64 kurtosis -= 3;
65 double x_mean = p.k() * (1 - p.p()) / p.p();
66 double x_var = x_mean / p.p();
67 double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
68 double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
69 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
70 assert(std::abs((var - x_var) / x_var) < 0.01);
71 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
72 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
73 }
74 {
75 typedef std::negative_binomial_distribution<> D;
76 typedef D::param_type P;
77 typedef std::mt19937 G;
78 G g;
79 D d(16, .75);
80 P p(30, .03125);
81 const int N = 1000000;
82 std::vector<D::result_type> u;
83 for (int i = 0; i < N; ++i)
84 {
85 D::result_type v = d(g, p);
86 assert(d.min() <= v && v <= d.max());
87 u.push_back(v);
88 }
89 double mean = std::accumulate(u.begin(), u.end(),
90 double(0)) / u.size();
91 double var = 0;
92 double skew = 0;
93 double kurtosis = 0;
94 for (int i = 0; i < u.size(); ++i)
95 {
96 double d = (u[i] - mean);
97 double d2 = sqr(d);
98 var += d2;
99 skew += d * d2;
100 kurtosis += d2 * d2;
101 }
102 var /= u.size();
103 double dev = std::sqrt(var);
104 skew /= u.size() * dev * var;
105 kurtosis /= u.size() * var * var;
106 kurtosis -= 3;
107 double x_mean = p.k() * (1 - p.p()) / p.p();
108 double x_var = x_mean / p.p();
109 double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
110 double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
111 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
112 assert(std::abs((var - x_var) / x_var) < 0.01);
113 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
114 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
115 }
116 {
117 typedef std::negative_binomial_distribution<> D;
118 typedef D::param_type P;
119 typedef std::mt19937 G;
120 G g;
121 D d(16, .75);
122 P p(40, .25);
123 const int N = 1000000;
124 std::vector<D::result_type> u;
125 for (int i = 0; i < N; ++i)
126 {
127 D::result_type v = d(g, p);
128 assert(d.min() <= v && v <= d.max());
129 u.push_back(v);
130 }
131 double mean = std::accumulate(u.begin(), u.end(),
132 double(0)) / u.size();
133 double var = 0;
134 double skew = 0;
135 double kurtosis = 0;
136 for (int i = 0; i < u.size(); ++i)
137 {
138 double d = (u[i] - mean);
139 double d2 = sqr(d);
140 var += d2;
141 skew += d * d2;
142 kurtosis += d2 * d2;
143 }
144 var /= u.size();
145 double dev = std::sqrt(var);
146 skew /= u.size() * dev * var;
147 kurtosis /= u.size() * var * var;
148 kurtosis -= 3;
149 double x_mean = p.k() * (1 - p.p()) / p.p();
150 double x_var = x_mean / p.p();
151 double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
152 double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
153 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
154 assert(std::abs((var - x_var) / x_var) < 0.01);
155 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
156 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
157 }
158 }
159