Home | History | Annotate | Download | only in rand.dist.pois.weibull
      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 
     24 template <class T>
     25 inline
     26 T
     27 sqr(T x)
     28 {
     29     return x * x;
     30 }
     31 
     32 int 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         P p(1, .5);
     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, p);
     46             assert(d.min() <= v);
     47             u.push_back(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 (int i = 0; i < u.size(); ++i)
     54         {
     55             double d = (u[i] - mean);
     56             double d2 = sqr(d);
     57             var += d2;
     58             skew += d * d2;
     59             kurtosis += d2 * d2;
     60         }
     61         var /= u.size();
     62         double dev = std::sqrt(var);
     63         skew /= u.size() * dev * var;
     64         kurtosis /= u.size() * var * var;
     65         kurtosis -= 3;
     66         double x_mean = p.b() * std::tgamma(1 + 1/p.a());
     67         double x_var = sqr(p.b()) * std::tgamma(1 + 2/p.a()) - sqr(x_mean);
     68         double x_skew = (sqr(p.b())*p.b() * std::tgamma(1 + 3/p.a()) -
     69                         3*x_mean*x_var - sqr(x_mean)*x_mean) /
     70                         (std::sqrt(x_var)*x_var);
     71         double x_kurtosis = (sqr(sqr(p.b())) * std::tgamma(1 + 4/p.a()) -
     72                        4*x_skew*x_var*sqrt(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.01);
     78     }
     79     {
     80         typedef std::weibull_distribution<> D;
     81         typedef D::param_type P;
     82         typedef std::mt19937 G;
     83         G g;
     84         D d(1, .5);
     85         P p(2, 3);
     86         const int N = 1000000;
     87         std::vector<D::result_type> u;
     88         for (int i = 0; i < N; ++i)
     89         {
     90             D::result_type v = d(g, p);
     91             assert(d.min() <= v);
     92             u.push_back(v);
     93         }
     94         double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
     95         double var = 0;
     96         double skew = 0;
     97         double kurtosis = 0;
     98         for (int i = 0; i < u.size(); ++i)
     99         {
    100             double d = (u[i] - mean);
    101             double d2 = sqr(d);
    102             var += d2;
    103             skew += d * d2;
    104             kurtosis += d2 * d2;
    105         }
    106         var /= u.size();
    107         double dev = std::sqrt(var);
    108         skew /= u.size() * dev * var;
    109         kurtosis /= u.size() * var * var;
    110         kurtosis -= 3;
    111         double x_mean = p.b() * std::tgamma(1 + 1/p.a());
    112         double x_var = sqr(p.b()) * std::tgamma(1 + 2/p.a()) - sqr(x_mean);
    113         double x_skew = (sqr(p.b())*p.b() * std::tgamma(1 + 3/p.a()) -
    114                         3*x_mean*x_var - sqr(x_mean)*x_mean) /
    115                         (std::sqrt(x_var)*x_var);
    116         double x_kurtosis = (sqr(sqr(p.b())) * std::tgamma(1 + 4/p.a()) -
    117                        4*x_skew*x_var*sqrt(x_var)*x_mean -
    118                        6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3;
    119         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    120         assert(std::abs((var - x_var) / x_var) < 0.01);
    121         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    122         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
    123     }
    124     {
    125         typedef std::weibull_distribution<> D;
    126         typedef D::param_type P;
    127         typedef std::mt19937 G;
    128         G g;
    129         D d(2, 3);
    130         P p(.5, 2);
    131         const int N = 1000000;
    132         std::vector<D::result_type> u;
    133         for (int i = 0; i < N; ++i)
    134         {
    135             D::result_type v = d(g, p);
    136             assert(d.min() <= v);
    137             u.push_back(v);
    138         }
    139         double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
    140         double var = 0;
    141         double skew = 0;
    142         double kurtosis = 0;
    143         for (int i = 0; i < u.size(); ++i)
    144         {
    145             double d = (u[i] - mean);
    146             double d2 = sqr(d);
    147             var += d2;
    148             skew += d * d2;
    149             kurtosis += d2 * d2;
    150         }
    151         var /= u.size();
    152         double dev = std::sqrt(var);
    153         skew /= u.size() * dev * var;
    154         kurtosis /= u.size() * var * var;
    155         kurtosis -= 3;
    156         double x_mean = p.b() * std::tgamma(1 + 1/p.a());
    157         double x_var = sqr(p.b()) * std::tgamma(1 + 2/p.a()) - sqr(x_mean);
    158         double x_skew = (sqr(p.b())*p.b() * std::tgamma(1 + 3/p.a()) -
    159                         3*x_mean*x_var - sqr(x_mean)*x_mean) /
    160                         (std::sqrt(x_var)*x_var);
    161         double x_kurtosis = (sqr(sqr(p.b())) * std::tgamma(1 + 4/p.a()) -
    162                        4*x_skew*x_var*sqrt(x_var)*x_mean -
    163                        6*sqr(x_mean)*x_var - sqr(sqr(x_mean))) / sqr(x_var) - 3;
    164         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    165         assert(std::abs((var - x_var) / x_var) < 0.01);
    166         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    167         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
    168     }
    169 }
    170