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

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.pois/rand.dist.pois.weibull/eval.pass.cpp