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);
     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 std::minstd_rand G;
     37         G g;
     38         D d(5, .25);
     39         const int N = 1000000;
     40         std::vector<D::result_type> u;
     41         for (int i = 0; i < N; ++i)
     42         {
     43             D::result_type v = d(g);
     44             assert(d.min() <= v && v <= d.max());
     45             u.push_back(v);
     46         }
     47         double mean = std::accumulate(u.begin(), u.end(),
     48                                               double(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.k() * (1 - d.p()) / d.p();
     66         double x_var = x_mean / d.p();
     67         double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
     68         double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
     69         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
     70         assert(std::abs((var - x_var) / x_var) < 0.01);
     71         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
     72         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.02);
     73     }
     74     {
     75         typedef std::negative_binomial_distribution<> D;
     76         typedef std::mt19937 G;
     77         G g;
     78         D d(30, .03125);
     79         const int N = 1000000;
     80         std::vector<D::result_type> u;
     81         for (int i = 0; i < N; ++i)
     82         {
     83             D::result_type v = d(g);
     84             assert(d.min() <= v && v <= d.max());
     85             u.push_back(v);
     86         }
     87         double mean = std::accumulate(u.begin(), u.end(),
     88                                               double(0)) / u.size();
     89         double var = 0;
     90         double skew = 0;
     91         double kurtosis = 0;
     92         for (int i = 0; i < u.size(); ++i)
     93         {
     94             double d = (u[i] - mean);
     95             double d2 = sqr(d);
     96             var += d2;
     97             skew += d * d2;
     98             kurtosis += d2 * d2;
     99         }
    100         var /= u.size();
    101         double dev = std::sqrt(var);
    102         skew /= u.size() * dev * var;
    103         kurtosis /= u.size() * var * var;
    104         kurtosis -= 3;
    105         double x_mean = d.k() * (1 - d.p()) / d.p();
    106         double x_var = x_mean / d.p();
    107         double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
    108         double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
    109         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    110         assert(std::abs((var - x_var) / x_var) < 0.01);
    111         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    112         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
    113     }
    114     {
    115         typedef std::negative_binomial_distribution<> D;
    116         typedef std::mt19937 G;
    117         G g;
    118         D d(40, .25);
    119         const int N = 1000000;
    120         std::vector<D::result_type> u;
    121         for (int i = 0; i < N; ++i)
    122         {
    123             D::result_type v = d(g);
    124             assert(d.min() <= v && v <= d.max());
    125             u.push_back(v);
    126         }
    127         double mean = std::accumulate(u.begin(), u.end(),
    128                                               double(0)) / u.size();
    129         double var = 0;
    130         double skew = 0;
    131         double kurtosis = 0;
    132         for (int i = 0; i < u.size(); ++i)
    133         {
    134             double d = (u[i] - mean);
    135             double d2 = sqr(d);
    136             var += d2;
    137             skew += d * d2;
    138             kurtosis += d2 * d2;
    139         }
    140         var /= u.size();
    141         double dev = std::sqrt(var);
    142         skew /= u.size() * dev * var;
    143         kurtosis /= u.size() * var * var;
    144         kurtosis -= 3;
    145         double x_mean = d.k() * (1 - d.p()) / d.p();
    146         double x_var = x_mean / d.p();
    147         double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
    148         double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
    149         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    150         assert(std::abs((var - x_var) / x_var) < 0.01);
    151         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    152         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
    153     }
    154     {
    155         typedef std::negative_binomial_distribution<> D;
    156         typedef std::mt19937 G;
    157         G g;
    158         D d(40, 1);
    159         const int N = 1000;
    160         std::vector<D::result_type> u;
    161         for (int i = 0; i < N; ++i)
    162         {
    163             D::result_type v = d(g);
    164             assert(d.min() <= v && v <= d.max());
    165             u.push_back(v);
    166         }
    167         double mean = std::accumulate(u.begin(), u.end(),
    168                                               double(0)) / u.size();
    169         double var = 0;
    170         double skew = 0;
    171         double kurtosis = 0;
    172         for (int i = 0; i < u.size(); ++i)
    173         {
    174             double d = (u[i] - mean);
    175             double d2 = sqr(d);
    176             var += d2;
    177             skew += d * d2;
    178             kurtosis += d2 * d2;
    179         }
    180         var /= u.size();
    181         double dev = std::sqrt(var);
    182         skew /= u.size() * dev * var;
    183         kurtosis /= u.size() * var * var;
    184         kurtosis -= 3;
    185         double x_mean = d.k() * (1 - d.p()) / d.p();
    186         double x_var = x_mean / d.p();
    187         double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
    188         double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
    189         assert(mean == x_mean);
    190         assert(var == x_var);
    191     }
    192     {
    193         typedef std::negative_binomial_distribution<> D;
    194         typedef std::mt19937 G;
    195         G g;
    196         D d(400, 0.5);
    197         const int N = 1000000;
    198         std::vector<D::result_type> u;
    199         for (int i = 0; i < N; ++i)
    200         {
    201             D::result_type v = d(g);
    202             assert(d.min() <= v && v <= d.max());
    203             u.push_back(v);
    204         }
    205         double mean = std::accumulate(u.begin(), u.end(),
    206                                               double(0)) / u.size();
    207         double var = 0;
    208         double skew = 0;
    209         double kurtosis = 0;
    210         for (int i = 0; i < u.size(); ++i)
    211         {
    212             double d = (u[i] - mean);
    213             double d2 = sqr(d);
    214             var += d2;
    215             skew += d * d2;
    216             kurtosis += d2 * d2;
    217         }
    218         var /= u.size();
    219         double dev = std::sqrt(var);
    220         skew /= u.size() * dev * var;
    221         kurtosis /= u.size() * var * var;
    222         kurtosis -= 3;
    223         double x_mean = d.k() * (1 - d.p()) / d.p();
    224         double x_var = x_mean / d.p();
    225         double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
    226         double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
    227         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    228         assert(std::abs((var - x_var) / x_var) < 0.01);
    229         assert(std::abs((skew - x_skew) / x_skew) < 0.04);
    230         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.05);
    231     }
    232     {
    233         typedef std::negative_binomial_distribution<> D;
    234         typedef std::mt19937 G;
    235         G g;
    236         D d(1, 0.05);
    237         const int N = 1000000;
    238         std::vector<D::result_type> u;
    239         for (int i = 0; i < N; ++i)
    240         {
    241             D::result_type v = d(g);
    242             assert(d.min() <= v && v <= d.max());
    243             u.push_back(v);
    244         }
    245         double mean = std::accumulate(u.begin(), u.end(),
    246                                               double(0)) / u.size();
    247         double var = 0;
    248         double skew = 0;
    249         double kurtosis = 0;
    250         for (int i = 0; i < u.size(); ++i)
    251         {
    252             double d = (u[i] - mean);
    253             double d2 = sqr(d);
    254             var += d2;
    255             skew += d * d2;
    256             kurtosis += d2 * d2;
    257         }
    258         var /= u.size();
    259         double dev = std::sqrt(var);
    260         skew /= u.size() * dev * var;
    261         kurtosis /= u.size() * var * var;
    262         kurtosis -= 3;
    263         double x_mean = d.k() * (1 - d.p()) / d.p();
    264         double x_var = x_mean / d.p();
    265         double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
    266         double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
    267         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
    268         assert(std::abs((var - x_var) / x_var) < 0.01);
    269         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
    270         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.03);
    271     }
    272 }
    273