| 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 |
// REQUIRES: long_tests |
| 10 |
|
| 11 |
// <random> |
| 12 |
|
| 13 |
// template<class RealType = double> |
| 14 |
// class normal_distribution |
| 15 |
|
| 16 |
// template<class _URNG> result_type operator()(_URNG& g); |
| 17 |
|
| 18 |
#include <random> |
| 19 |
#include <cassert> |
| 20 |
#include <cmath> |
| 21 |
#include <cstddef> |
| 22 |
#include <numeric> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "test_macros.h" |
| 26 |
|
| 27 |
template <class T> |
| 28 |
inline |
| 29 |
T |
| 30 |
sqr(T x) |
| 31 |
{ |
| 32 |
return x * x; |
| 33 |
} |
| 34 |
|
| 35 |
int main(int, char**) |
| 36 |
{ |
| 37 |
{ |
| 38 |
typedef std::normal_distribution<> D; |
| 39 |
typedef std::minstd_rand G; |
| 40 |
G g; |
| 41 |
D d(5, 4); |
| 42 |
const int N = 1000000; |
| 43 |
std::vector<D::result_type> u; |
| 44 |
for (int i = 0; i < N; ++i) |
| 45 |
u.push_back(d(g)); |
| 46 |
double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size(); |
| 47 |
double var = 0; |
| 48 |
double skew = 0; |
| 49 |
double kurtosis = 0; |
| 50 |
for (std::size_t i = 0; i < u.size(); ++i) |
| 51 |
{ |
| 52 |
double dbl = (u[i] - mean); |
| 53 |
double d2 = sqr(dbl); |
| 54 |
var += d2; |
| 55 |
skew += dbl * 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.mean(); |
| 64 |
double x_var = sqr(d.stddev()); |
| 65 |
double x_skew = 0; |
| 66 |
double x_kurtosis = 0; |
| 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) < 0.01); |
| 70 |
assert(std::abs(kurtosis - x_kurtosis) < 0.01); |
| 71 |
} |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |