Home | History | Annotate | Download | only in rand.dist.bern.negbin
      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 IntType = int>
     15 // class negative_binomial_distribution
     16 
     17 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
     18 
     19 #include <random>
     20 #include <numeric>
     21 #include <vector>
     22 #include <cassert>
     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::negative_binomial_distribution<> D;
     36         typedef D::param_type P;
     37         typedef std::minstd_rand G;
     38         G g;
     39         D d(16, .75);
     40         P p(5, .75);
     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 && v <= d.max());
     47             u.push_back(v);
     48         }
     49         double mean = std::accumulate(u.begin(), u.end(),
     50                                               double(0)) / u.size();
     51         double var = 0;
     52         double skew = 0;
     53         double kurtosis = 0;
     54         for (int i = 0; i < u.size(); ++i)
     55         {
     56             double d = (u[i] - mean);
     57             double d2 = sqr(d);
     58             var += d2;
     59             skew += d * 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.k() * (1 - p.p()) / p.p();
     68         double x_var = x_mean / p.p();
     69         double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
     70         double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
     71         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
     72         assert(std::abs((var - x_var) / x_var) < 0.01);
     73         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
     74         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
     75     }
     76     {
     77         typedef std::negative_binomial_distribution<> D;
     78         typedef D::param_type P;
     79         typedef std::mt19937 G;
     80         G g;
     81         D d(16, .75);
     82         P p(30, .03125);
     83         const int N = 1000000;
     84         std::vector<D::result_type> u;
     85         for (int i = 0; i < N; ++i)
     86         {
     87             D::result_type v = d(g, p);
     88             assert(d.min() <= v && v <= d.max());
     89             u.push_back(v);
     90         }
     91         double mean = std::accumulate(u.begin(), u.end(),
     92                                               double(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 = p.k() * (1 - p.p()) / p.p();
    110         double x_var = x_mean / p.p();
    111         double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
    112         double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
    113         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    114         assert(std::abs((var - x_var) / x_var) < 0.01);
    115         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    116         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
    117     }
    118     {
    119         typedef std::negative_binomial_distribution<> D;
    120         typedef D::param_type P;
    121         typedef std::mt19937 G;
    122         G g;
    123         D d(16, .75);
    124         P p(40, .25);
    125         const int N = 1000000;
    126         std::vector<D::result_type> u;
    127         for (int i = 0; i < N; ++i)
    128         {
    129             D::result_type v = d(g, p);
    130             assert(d.min() <= v && v <= d.max());
    131             u.push_back(v);
    132         }
    133         double mean = std::accumulate(u.begin(), u.end(),
    134                                               double(0)) / u.size();
    135         double var = 0;
    136         double skew = 0;
    137         double kurtosis = 0;
    138         for (int i = 0; i < u.size(); ++i)
    139         {
    140             double d = (u[i] - mean);
    141             double d2 = sqr(d);
    142             var += d2;
    143             skew += d * d2;
    144             kurtosis += d2 * d2;
    145         }
    146         var /= u.size();
    147         double dev = std::sqrt(var);
    148         skew /= u.size() * dev * var;
    149         kurtosis /= u.size() * var * var;
    150         kurtosis -= 3;
    151         double x_mean = p.k() * (1 - p.p()) / p.p();
    152         double x_var = x_mean / p.p();
    153         double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
    154         double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
    155         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    156         assert(std::abs((var - x_var) / x_var) < 0.01);
    157         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    158         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
    159     }
    160 }
    161