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);
18
19#include <random>
20#include <cassert>
21#include <vector>
22#include <numeric>
23
24template <class T>
25inline
26T
27sqr(T x)
28{
29    return x * x;
30}
31
32int main()
33{
34    {
35        typedef std::weibull_distribution<> D;
36        typedef D::param_type P;
37        typedef std::mt19937 G;
38        G g;
39        D d(0.5, 2);
40        const int N = 1000000;
41        std::vector<D::result_type> u;
42        for (int i = 0; i < N; ++i)
43        {
44            D::result_type v = d(g);
45            assert(d.min() <= v);
46            u.push_back(v);
47        }
48        double mean = std::accumulate(u.begin(), u.end(), 0.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 = d.b() * std::tgamma(1 + 1/d.a());
66        double x_var = sqr(d.b()) * std::tgamma(1 + 2/d.a()) - sqr(x_mean);
67        double x_skew = (sqr(d.b())*d.b() * std::tgamma(1 + 3/d.a()) -
68                        3*x_mean*x_var - sqr(x_mean)*x_mean) /
69                        (std::sqrt(x_var)*x_var);
70        double x_kurtosis = (sqr(sqr(d.b())) * std::tgamma(1 + 4/d.a()) -
71                       4*x_skew*x_var*sqrt(x_var)*x_mean -
72                       6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3;
73        assert(std::abs((mean - x_mean) / x_mean) < 0.01);
74        assert(std::abs((var - x_var) / x_var) < 0.01);
75        assert(std::abs((skew - x_skew) / x_skew) < 0.01);
76        assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
77    }
78    {
79        typedef std::weibull_distribution<> D;
80        typedef D::param_type P;
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 (int i = 0; i < u.size(); ++i)
97        {
98            double d = (u[i] - mean);
99            double d2 = sqr(d);
100            var += d2;
101            skew += d * d2;
102            kurtosis += d2 * d2;
103        }
104        var /= u.size();
105        double dev = std::sqrt(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_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_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 D::param_type P;
125        typedef std::mt19937 G;
126        G g;
127        D d(2, 3);
128        const int N = 1000000;
129        std::vector<D::result_type> u;
130        for (int i = 0; i < N; ++i)
131        {
132            D::result_type v = d(g);
133            assert(d.min() <= v);
134            u.push_back(v);
135        }
136        double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
137        double var = 0;
138        double skew = 0;
139        double kurtosis = 0;
140        for (int i = 0; i < u.size(); ++i)
141        {
142            double d = (u[i] - mean);
143            double d2 = sqr(d);
144            var += d2;
145            skew += d * d2;
146            kurtosis += d2 * d2;
147        }
148        var /= u.size();
149        double dev = std::sqrt(var);
150        skew /= u.size() * dev * var;
151        kurtosis /= u.size() * var * var;
152        kurtosis -= 3;
153        double x_mean = d.b() * std::tgamma(1 + 1/d.a());
154        double x_var = sqr(d.b()) * std::tgamma(1 + 2/d.a()) - sqr(x_mean);
155        double x_skew = (sqr(d.b())*d.b() * std::tgamma(1 + 3/d.a()) -
156                        3*x_mean*x_var - sqr(x_mean)*x_mean) /
157                        (std::sqrt(x_var)*x_var);
158        double x_kurtosis = (sqr(sqr(d.b())) * std::tgamma(1 + 4/d.a()) -
159                       4*x_skew*x_var*sqrt(x_var)*x_mean -
160                       6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3;
161        assert(std::abs((mean - x_mean) / x_mean) < 0.01);
162        assert(std::abs((var - x_var) / x_var) < 0.01);
163        assert(std::abs((skew - x_skew) / x_skew) < 0.01);
164        assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
165    }
166}
167