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 binomial_distribution
14
15 // template<class _URNG> result_type operator()(_URNG& g);
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::binomial_distribution<> D;
34 typedef std::mt19937_64 G;
35 G g;
36 D d(5, .75);
37 const int N = 1000000;
38 std::vector<D::result_type> u;
39 for (int i = 0; i < N; ++i)
40 {
41 D::result_type v = d(g);
42 assert(d.min() <= v && v <= d.max());
43 u.push_back(v);
44 }
45 double mean = std::accumulate(u.begin(), u.end(),
46 double(0)) / u.size();
47 double var = 0;
48 double skew = 0;
49 double kurtosis = 0;
50 for (int i = 0; i < u.size(); ++i)
51 {
52 double d = (u[i] - mean);
53 double d2 = sqr(d);
54 var += d2;
55 skew += d * d2;
56 kurtosis += d2 * d2;
57 }
58 var /= u.size();
59 double dev = std::sqrt(var);
60 skew /= u.size() * dev * var;
61 kurtosis /= u.size() * var * var;
62 kurtosis -= 3;
63 double x_mean = d.t() * d.p();
64 double x_var = x_mean*(1-d.p());
65 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
66 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
67 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
68 assert(std::abs((var - x_var) / x_var) < 0.01);
69 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
70 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.04);
71 }
72 {
73 typedef std::binomial_distribution<> D;
74 typedef std::mt19937 G;
75 G g;
76 D d(30, .03125);
77 const int N = 100000;
78 std::vector<D::result_type> u;
79 for (int i = 0; i < N; ++i)
80 {
81 D::result_type v = d(g);
82 assert(d.min() <= v && v <= d.max());
83 u.push_back(v);
84 }
85 double mean = std::accumulate(u.begin(), u.end(),
86 double(0)) / u.size();
87 double var = 0;
88 double skew = 0;
89 double kurtosis = 0;
90 for (int i = 0; i < u.size(); ++i)
91 {
92 double d = (u[i] - mean);
93 double d2 = sqr(d);
94 var += d2;
95 skew += d * d2;
96 kurtosis += d2 * d2;
97 }
98 var /= u.size();
99 double dev = std::sqrt(var);
100 skew /= u.size() * dev * var;
101 kurtosis /= u.size() * var * var;
102 kurtosis -= 3;
103 double x_mean = d.t() * d.p();
104 double x_var = x_mean*(1-d.p());
105 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
106 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
107 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
108 assert(std::abs((var - x_var) / x_var) < 0.01);
109 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
110 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
111 }
112 {
113 typedef std::binomial_distribution<> D;
114 typedef std::mt19937 G;
115 G g;
116 D d(40, .25);
117 const int N = 100000;
118 std::vector<D::result_type> u;
119 for (int i = 0; i < N; ++i)
120 {
121 D::result_type v = d(g);
122 assert(d.min() <= v && v <= d.max());
123 u.push_back(v);
124 }
125 double mean = std::accumulate(u.begin(), u.end(),
126 double(0)) / u.size();
127 double var = 0;
128 double skew = 0;
129 double kurtosis = 0;
130 for (int i = 0; i < u.size(); ++i)
131 {
132 double d = (u[i] - mean);
133 double d2 = sqr(d);
134 var += d2;
135 skew += d * d2;
136 kurtosis += d2 * d2;
137 }
138 var /= u.size();
139 double dev = std::sqrt(var);
140 skew /= u.size() * dev * var;
141 kurtosis /= u.size() * var * var;
142 kurtosis -= 3;
143 double x_mean = d.t() * d.p();
144 double x_var = x_mean*(1-d.p());
145 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
146 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
147 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
148 assert(std::abs((var - x_var) / x_var) < 0.01);
149 assert(std::abs((skew - x_skew) / x_skew) < 0.03);
150 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.3);
151 }
152 {
153 typedef std::binomial_distribution<> D;
154 typedef std::mt19937 G;
155 G g;
156 D d(40, 0);
157 const int N = 100000;
158 std::vector<D::result_type> u;
159 for (int i = 0; i < N; ++i)
160 {
161 D::result_type v = d(g);
162 assert(d.min() <= v && v <= d.max());
163 u.push_back(v);
164 }
165 double mean = std::accumulate(u.begin(), u.end(),
166 double(0)) / u.size();
167 double var = 0;
168 double skew = 0;
169 double kurtosis = 0;
170 for (int i = 0; i < u.size(); ++i)
171 {
172 double d = (u[i] - mean);
173 double d2 = sqr(d);
174 var += d2;
175 skew += d * d2;
176 kurtosis += d2 * d2;
177 }
178 var /= u.size();
179 double dev = std::sqrt(var);
180 skew /= u.size() * dev * var;
181 kurtosis /= u.size() * var * var;
182 kurtosis -= 3;
183 double x_mean = d.t() * d.p();
184 double x_var = x_mean*(1-d.p());
185 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
186 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
187 assert(mean == x_mean);
188 assert(var == x_var);
189 }
190 {
191 typedef std::binomial_distribution<> D;
192 typedef std::mt19937 G;
193 G g;
194 D d(40, 1);
195 const int N = 100000;
196 std::vector<D::result_type> u;
197 for (int i = 0; i < N; ++i)
198 {
199 D::result_type v = d(g);
200 assert(d.min() <= v && v <= d.max());
201 u.push_back(v);
202 }
203 double mean = std::accumulate(u.begin(), u.end(),
204 double(0)) / u.size();
205 double var = 0;
206 double skew = 0;
207 double kurtosis = 0;
208 for (int i = 0; i < u.size(); ++i)
209 {
210 double d = (u[i] - mean);
211 double d2 = sqr(d);
212 var += d2;
213 skew += d * d2;
214 kurtosis += d2 * d2;
215 }
216 var /= u.size();
217 double dev = std::sqrt(var);
218 skew /= u.size() * dev * var;
219 kurtosis /= u.size() * var * var;
220 kurtosis -= 3;
221 double x_mean = d.t() * d.p();
222 double x_var = x_mean*(1-d.p());
223 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
224 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
225 assert(mean == x_mean);
226 assert(var == x_var);
227 }
228 {
229 typedef std::binomial_distribution<> D;
230 typedef std::mt19937 G;
231 G g;
232 D d(400, 0.5);
233 const int N = 100000;
234 std::vector<D::result_type> u;
235 for (int i = 0; i < N; ++i)
236 {
237 D::result_type v = d(g);
238 assert(d.min() <= v && v <= d.max());
239 u.push_back(v);
240 }
241 double mean = std::accumulate(u.begin(), u.end(),
242 double(0)) / u.size();
243 double var = 0;
244 double skew = 0;
245 double kurtosis = 0;
246 for (int i = 0; i < u.size(); ++i)
247 {
248 double d = (u[i] - mean);
249 double d2 = sqr(d);
250 var += d2;
251 skew += d * d2;
252 kurtosis += d2 * d2;
253 }
254 var /= u.size();
255 double dev = std::sqrt(var);
256 skew /= u.size() * dev * var;
257 kurtosis /= u.size() * var * var;
258 kurtosis -= 3;
259 double x_mean = d.t() * d.p();
260 double x_var = x_mean*(1-d.p());
261 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
262 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
263 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
264 assert(std::abs((var - x_var) / x_var) < 0.01);
265 assert(std::abs(skew - x_skew) < 0.01);
266 assert(std::abs(kurtosis - x_kurtosis) < 0.01);
267 }
268 {
269 typedef std::binomial_distribution<> D;
270 typedef std::mt19937 G;
271 G g;
272 D d(1, 0.5);
273 const int N = 100000;
274 std::vector<D::result_type> u;
275 for (int i = 0; i < N; ++i)
276 {
277 D::result_type v = d(g);
278 assert(d.min() <= v && v <= d.max());
279 u.push_back(v);
280 }
281 double mean = std::accumulate(u.begin(), u.end(),
282 double(0)) / u.size();
283 double var = 0;
284 double skew = 0;
285 double kurtosis = 0;
286 for (int i = 0; i < u.size(); ++i)
287 {
288 double d = (u[i] - mean);
289 double d2 = sqr(d);
290 var += d2;
291 skew += d * d2;
292 kurtosis += d2 * d2;
293 }
294 var /= u.size();
295 double dev = std::sqrt(var);
296 skew /= u.size() * dev * var;
297 kurtosis /= u.size() * var * var;
298 kurtosis -= 3;
299 double x_mean = d.t() * d.p();
300 double x_var = x_mean*(1-d.p());
301 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
302 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
303 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
304 assert(std::abs((var - x_var) / x_var) < 0.01);
305 assert(std::abs(skew - x_skew) < 0.01);
306 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
307 }
308 {
309 typedef std::binomial_distribution<> D;
310 typedef std::mt19937 G;
311 G g;
312 D d(0, 0.005);
313 const int N = 100000;
314 std::vector<D::result_type> u;
315 for (int i = 0; i < N; ++i)
316 {
317 D::result_type v = d(g);
318 assert(d.min() <= v && v <= d.max());
319 u.push_back(v);
320 }
321 double mean = std::accumulate(u.begin(), u.end(),
322 double(0)) / u.size();
323 double var = 0;
324 double skew = 0;
325 double kurtosis = 0;
326 for (int i = 0; i < u.size(); ++i)
327 {
328 double d = (u[i] - mean);
329 double d2 = sqr(d);
330 var += d2;
331 skew += d * d2;
332 kurtosis += d2 * d2;
333 }
334 var /= u.size();
335 double dev = std::sqrt(var);
336 skew /= u.size() * dev * var;
337 kurtosis /= u.size() * var * var;
338 kurtosis -= 3;
339 double x_mean = d.t() * d.p();
340 double x_var = x_mean*(1-d.p());
341 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
342 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
343 assert(mean == x_mean);
344 assert(var == x_var);
345 }
346 {
347 typedef std::binomial_distribution<> D;
348 typedef std::mt19937 G;
349 G g;
350 D d(0, 0);
351 const int N = 100000;
352 std::vector<D::result_type> u;
353 for (int i = 0; i < N; ++i)
354 {
355 D::result_type v = d(g);
356 assert(d.min() <= v && v <= d.max());
357 u.push_back(v);
358 }
359 double mean = std::accumulate(u.begin(), u.end(),
360 double(0)) / u.size();
361 double var = 0;
362 double skew = 0;
363 double kurtosis = 0;
364 for (int i = 0; i < u.size(); ++i)
365 {
366 double d = (u[i] - mean);
367 double d2 = sqr(d);
368 var += d2;
369 skew += d * d2;
370 kurtosis += d2 * d2;
371 }
372 var /= u.size();
373 double dev = std::sqrt(var);
374 skew /= u.size() * dev * var;
375 kurtosis /= u.size() * var * var;
376 kurtosis -= 3;
377 double x_mean = d.t() * d.p();
378 double x_var = x_mean*(1-d.p());
379 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
380 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
381 assert(mean == x_mean);
382 assert(var == x_var);
383 }
384 {
385 typedef std::binomial_distribution<> D;
386 typedef std::mt19937 G;
387 G g;
388 D d(0, 1);
389 const int N = 100000;
390 std::vector<D::result_type> u;
391 for (int i = 0; i < N; ++i)
392 {
393 D::result_type v = d(g);
394 assert(d.min() <= v && v <= d.max());
395 u.push_back(v);
396 }
397 double mean = std::accumulate(u.begin(), u.end(),
398 double(0)) / u.size();
399 double var = 0;
400 double skew = 0;
401 double kurtosis = 0;
402 for (int i = 0; i < u.size(); ++i)
403 {
404 double d = (u[i] - mean);
405 double d2 = sqr(d);
406 var += d2;
407 skew += d * d2;
408 kurtosis += d2 * d2;
409 }
410 var /= u.size();
411 double dev = std::sqrt(var);
412 skew /= u.size() * dev * var;
413 kurtosis /= u.size() * var * var;
414 kurtosis -= 3;
415 double x_mean = d.t() * d.p();
416 double x_var = x_mean*(1-d.p());
417 double x_skew = (1-2*d.p()) / std::sqrt(x_var);
418 double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
419 assert(mean == x_mean);
420 assert(var == x_var);
421 }
422 }
423