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