Home | History | Annotate | Download | only in rand.dist.pois.exp
      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 exponential_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::exponential_distribution<> D;
     36         typedef D::param_type P;
     37         typedef std::mt19937 G;
     38         G g;
     39         D d(.75);
     40         P p(2);
     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 = 1/p.lambda();
     67         double x_var = 1/sqr(p.lambda());
     68         double x_skew = 2;
     69         double x_kurtosis = 6;
     70         assert(std::abs((mean - x_mean) / x_mean) < 0.01);
     71         assert(std::abs((var - x_var) / x_var) < 0.01);
     72         assert(std::abs((skew - x_skew) / x_skew) < 0.01);
     73         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
     74     }
     75 }
     76