[rand.dist.bern.negbin]
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@103916 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/random b/include/random
index 6d17c2c..3298cd3 100644
--- a/include/random
+++ b/include/random
@@ -669,7 +669,62 @@
class geometric_distribution;
template<class IntType = int>
- class negative_binomial_distribution;
+class negative_binomial_distribution
+{
+public:
+ // types
+ typedef IntType result_type;
+
+ class param_type
+ {
+ public:
+ typedef negative_binomial_distribution distribution_type;
+
+ explicit param_type(result_type k = 1, double p = 0.5);
+
+ result_type k() const;
+ double p() const;
+
+ friend bool operator==(const param_type& x, const param_type& y);
+ friend bool operator!=(const param_type& x, const param_type& y);
+ };
+
+ // constructor and reset functions
+ explicit negative_binomial_distribution(result_type k = 1, double p = 0.5);
+ explicit negative_binomial_distribution(const param_type& parm);
+ void reset();
+
+ // generating functions
+ template<class URNG> result_type operator()(URNG& g);
+ template<class URNG> result_type operator()(URNG& g, const param_type& parm);
+
+ // property functions
+ result_type k() const;
+ double p() const;
+
+ param_type param() const;
+ void param(const param_type& parm);
+
+ result_type min() const;
+ result_type max() const;
+
+ friend bool operator==(const negative_binomial_distribution& x,
+ const negative_binomial_distribution& y);
+ friend bool operator!=(const negative_binomial_distribution& x,
+ const negative_binomial_distribution& y);
+
+ template <class charT, class traits>
+ friend
+ basic_ostream<charT, traits>&
+ operator<<(basic_ostream<charT, traits>& os,
+ const negative_binomial_distribution& x);
+
+ template <class charT, class traits>
+ friend
+ basic_istream<charT, traits>&
+ operator>>(basic_istream<charT, traits>& is,
+ negative_binomial_distribution& x);
+};
template<class IntType = int>
class poisson_distribution
@@ -4098,6 +4153,122 @@
return __is;
}
+// negative_binomial_distribution
+
+template<class _IntType = int>
+class negative_binomial_distribution
+{
+public:
+ // types
+ typedef _IntType result_type;
+
+ class param_type
+ {
+ result_type __k_;
+ double __p_;
+ public:
+ typedef negative_binomial_distribution distribution_type;
+
+ explicit param_type(result_type __k = 1, double __p = 0.5)
+ : __k_(__k), __p_(__p) {}
+
+ result_type k() const {return __k_;}
+ double p() const {return __p_;}
+
+ friend bool operator==(const param_type& __x, const param_type& __y)
+ {return __x.__k_ == __y.__k_ && __x.__p_ == __y.__p_;}
+ friend bool operator!=(const param_type& __x, const param_type& __y)
+ {return !(__x == __y);}
+ };
+
+private:
+ param_type __p_;
+
+public:
+ // constructor and reset functions
+ explicit negative_binomial_distribution(result_type __k = 1, double __p = 0.5)
+ : __p_(__k, __p) {}
+ explicit negative_binomial_distribution(const param_type& __p) : __p_(__p) {}
+ void reset() {}
+
+ // generating functions
+ template<class _URNG> result_type operator()(_URNG& __g)
+ {return (*this)(__g, __p_);}
+ template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
+
+ // property functions
+ result_type k() const {return __p_.k();}
+ double p() const {return __p_.p();}
+
+ param_type param() const {return __p_;}
+ void param(const param_type& __p) {__p_ = __p;}
+
+ result_type min() const {return 0;}
+ result_type max() const {return numeric_limits<result_type>::max();}
+
+ friend bool operator==(const negative_binomial_distribution& __x,
+ const negative_binomial_distribution& __y)
+ {return __x.__p_ == __y.__p_;}
+ friend bool operator!=(const negative_binomial_distribution& __x,
+ const negative_binomial_distribution& __y)
+ {return !(__x == __y);}
+};
+
+template <class _IntType>
+template<class _URNG>
+_IntType
+negative_binomial_distribution<_IntType>::operator()(_URNG& __urng, const param_type& __pr)
+{
+ result_type __k = __pr.k();
+ double __p = __pr.p();
+ if (__k <= 21 * __p)
+ {
+ bernoulli_distribution __gen(__p);
+ result_type __f = 0;
+ result_type __s = 0;
+ while (__s < __k)
+ {
+ if (__gen(__urng))
+ ++__s;
+ else
+ ++__f;
+ }
+ return __f;
+ }
+ return poisson_distribution<result_type>(gamma_distribution<double>
+ (__k, (1-__p)/__p)(__urng))(__urng);
+}
+
+template <class _CharT, class _Traits, class _IntType>
+basic_ostream<_CharT, _Traits>&
+operator<<(basic_ostream<_CharT, _Traits>& __os,
+ const negative_binomial_distribution<_IntType>& __x)
+{
+ __save_flags<_CharT, _Traits> _(__os);
+ __os.flags(ios_base::dec | ios_base::left);
+ _CharT __sp = __os.widen(' ');
+ __os.fill(__sp);
+ return __os << __x.k() << __sp << __x.p();
+}
+
+template <class _CharT, class _Traits, class _IntType>
+basic_istream<_CharT, _Traits>&
+operator>>(basic_istream<_CharT, _Traits>& __is,
+ negative_binomial_distribution<_IntType>& __x)
+{
+ typedef negative_binomial_distribution<_IntType> _Eng;
+ typedef typename _Eng::result_type result_type;
+ typedef typename _Eng::param_type param_type;
+ __save_flags<_CharT, _Traits> _(__is);
+ __is.flags(ios_base::dec | ios_base::skipws);
+ result_type __k;
+ double __p;
+ __is >> __k >> __p;
+ if (!__is.fail())
+ __x.param(param_type(__k, __p));
+ return __is;
+}
+
// chi_squared_distribution
template<class _RealType = double>
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
index 7ff25d5..71bbf19 100644
--- a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
@@ -14,8 +14,6 @@
// template<class _URNG> result_type operator()(_URNG& g);
-#include <iostream>
-
#include <random>
#include <numeric>
#include <vector>
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/assign.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/assign.pass.cpp
new file mode 100644
index 0000000..edf7436
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/assign.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// negative_binomial_distribution& operator=(const negative_binomial_distribution&);
+
+#include <random>
+#include <cassert>
+
+void
+test1()
+{
+ typedef std::negative_binomial_distribution<> D;
+ D d1(2, 0.75);
+ D d2;
+ assert(d1 != d2);
+ d2 = d1;
+ assert(d1 == d2);
+}
+
+int main()
+{
+ test1();
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/copy.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/copy.pass.cpp
new file mode 100644
index 0000000..d1dadb9
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/copy.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// negative_binomial_distribution(const negative_binomial_distribution&);
+
+#include <random>
+#include <cassert>
+
+void
+test1()
+{
+ typedef std::negative_binomial_distribution<> D;
+ D d1(2, 0.75);
+ D d2 = d1;
+ assert(d1 == d2);
+}
+
+int main()
+{
+ test1();
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/ctor_int_double.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/ctor_int_double.pass.cpp
new file mode 100644
index 0000000..601d575
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/ctor_int_double.pass.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// explicit negative_binomial_distribution(IntType t = 1, double p = 0.5);
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d;
+ assert(d.k() == 1);
+ assert(d.p() == 0.5);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d(3);
+ assert(d.k() == 3);
+ assert(d.p() == 0.5);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d(3, 0.75);
+ assert(d.k() == 3);
+ assert(d.p() == 0.75);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/ctor_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/ctor_param.pass.cpp
new file mode 100644
index 0000000..37bd125
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/ctor_param.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// explicit negative_binomial_distribution(const param_type& parm);
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type P;
+ P p(5, 0.25);
+ D d(p);
+ assert(d.k() == 5);
+ assert(d.p() == 0.25);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eq.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eq.pass.cpp
new file mode 100644
index 0000000..9c264cf
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eq.pass.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// bool operator=(const negative_binomial_distribution& x,
+// const negative_binomial_distribution& y);
+// bool operator!(const negative_binomial_distribution& x,
+// const negative_binomial_distribution& y);
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d1(3, .25);
+ D d2(3, .25);
+ assert(d1 == d2);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d1(3, .28);
+ D d2(3, .25);
+ assert(d1 != d2);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d1(3, .25);
+ D d2(4, .25);
+ assert(d1 != d2);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eval.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eval.pass.cpp
new file mode 100644
index 0000000..96b4c18
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eval.pass.cpp
@@ -0,0 +1,270 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// template<class _URNG> result_type operator()(_URNG& g);
+
+#include <random>
+#include <numeric>
+#include <vector>
+#include <cassert>
+
+template <class T>
+inline
+T
+sqr(T x)
+{
+ return x * x;
+}
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef std::minstd_rand G;
+ G g;
+ D d(5, .25);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = d.k() * (1 - d.p()) / d.p();
+ double x_var = x_mean / d.p();
+ double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
+ double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.01);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef std::mt19937 G;
+ G g;
+ D d(30, .03125);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = d.k() * (1 - d.p()) / d.p();
+ double x_var = x_mean / d.p();
+ double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
+ double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.01);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef std::mt19937 G;
+ G g;
+ D d(40, .25);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = d.k() * (1 - d.p()) / d.p();
+ double x_var = x_mean / d.p();
+ double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
+ double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.03);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef std::mt19937 G;
+ G g;
+ D d(40, 1);
+ const int N = 1000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = d.k() * (1 - d.p()) / d.p();
+ double x_var = x_mean / d.p();
+ double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
+ double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
+ assert(mean == x_mean);
+ assert(var == x_var);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef std::mt19937 G;
+ G g;
+ D d(400, 0.5);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = d.k() * (1 - d.p()) / d.p();
+ double x_var = x_mean / d.p();
+ double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
+ double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.04);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.05);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef std::mt19937 G;
+ G g;
+ D d(1, 0.05);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = d.k() * (1 - d.p()) / d.p();
+ double x_var = x_mean / d.p();
+ double x_skew = (2 - d.p()) / std::sqrt(d.k() * (1 - d.p()));
+ double x_kurtosis = 6. / d.k() + sqr(d.p()) / (d.k() * (1 - d.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.02);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eval_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eval_param.pass.cpp
new file mode 100644
index 0000000..b890672
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/eval_param.pass.cpp
@@ -0,0 +1,158 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
+
+#include <random>
+#include <numeric>
+#include <vector>
+#include <cassert>
+
+template <class T>
+inline
+T
+sqr(T x)
+{
+ return x * x;
+}
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type P;
+ typedef std::minstd_rand G;
+ G g;
+ D d(16, .75);
+ P p(5, .75);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g, p);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = p.k() * (1 - p.p()) / p.p();
+ double x_var = x_mean / p.p();
+ double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
+ double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.01);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type P;
+ typedef std::mt19937 G;
+ G g;
+ D d(16, .75);
+ P p(30, .03125);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g, p);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = p.k() * (1 - p.p()) / p.p();
+ double x_var = x_mean / p.p();
+ double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
+ double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.01);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type P;
+ typedef std::mt19937 G;
+ G g;
+ D d(16, .75);
+ P p(40, .25);
+ const int N = 1000000;
+ std::vector<D::result_type> u;
+ for (int i = 0; i < N; ++i)
+ {
+ D::result_type v = d(g, p);
+ assert(d.min() <= v && v <= d.max());
+ u.push_back(v);
+ }
+ double mean = std::accumulate(u.begin(), u.end(),
+ double(0)) / u.size();
+ double var = 0;
+ double skew = 0;
+ double kurtosis = 0;
+ for (int i = 0; i < u.size(); ++i)
+ {
+ double d = (u[i] - mean);
+ double d2 = sqr(d);
+ var += d2;
+ skew += d * d2;
+ kurtosis += d2 * d2;
+ }
+ var /= u.size();
+ double dev = std::sqrt(var);
+ skew /= u.size() * dev * var;
+ kurtosis /= u.size() * var * var;
+ kurtosis -= 3;
+ double x_mean = p.k() * (1 - p.p()) / p.p();
+ double x_var = x_mean / p.p();
+ double x_skew = (2 - p.p()) / std::sqrt(p.k() * (1 - p.p()));
+ double x_kurtosis = 6. / p.k() + sqr(p.p()) / (p.k() * (1 - p.p()));
+ assert(std::abs(mean - x_mean) / x_mean < 0.01);
+ assert(std::abs(var - x_var) / x_var < 0.01);
+ assert(std::abs(skew - x_skew) / x_skew < 0.01);
+ assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.03);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/get_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/get_param.pass.cpp
new file mode 100644
index 0000000..8ea4770
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/get_param.pass.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// param_type param() const;
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type P;
+ P p(5, .125);
+ D d(p);
+ assert(d.param() == p);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/io.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/io.pass.cpp
new file mode 100644
index 0000000..ed72ff1
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/io.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// template <class charT, class traits>
+// basic_ostream<charT, traits>&
+// operator<<(basic_ostream<charT, traits>& os,
+// const negative_binomial_distribution& x);
+//
+// template <class charT, class traits>
+// basic_istream<charT, traits>&
+// operator>>(basic_istream<charT, traits>& is,
+// negative_binomial_distribution& x);
+
+#include <random>
+#include <sstream>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d1(7, .25);
+ std::ostringstream os;
+ os << d1;
+ std::istringstream is(os.str());
+ D d2;
+ is >> d2;
+ assert(d1 == d2);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/max.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/max.pass.cpp
new file mode 100644
index 0000000..558ea8d
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/max.pass.cpp
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// result_type max() const;
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d(4, .25);
+ assert(d.max() == std::numeric_limits<int>::max());
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/min.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/min.pass.cpp
new file mode 100644
index 0000000..139c51c
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/min.pass.cpp
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// result_type min() const;
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ D d(4, .5);
+ assert(d.min() == 0);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_assign.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_assign.pass.cpp
new file mode 100644
index 0000000..f014852
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_assign.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+// {
+// class param_type;
+
+#include <random>
+#include <limits>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p0(6, .7);
+ param_type p;
+ p = p0;
+ assert(p.k() == 6);
+ assert(p.p() == .7);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_copy.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_copy.pass.cpp
new file mode 100644
index 0000000..a4af47b
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_copy.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+// {
+// class param_type;
+
+#include <random>
+#include <limits>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p0(10, .125);
+ param_type p = p0;
+ assert(p.k() == 10);
+ assert(p.p() == .125);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_ctor.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_ctor.pass.cpp
new file mode 100644
index 0000000..9b657b4
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_ctor.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+// {
+// class param_type;
+
+#include <random>
+#include <limits>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p;
+ assert(p.k() == 1);
+ assert(p.p() == 0.5);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p(10);
+ assert(p.k() == 10);
+ assert(p.p() == 0.5);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p(10, 0.25);
+ assert(p.k() == 10);
+ assert(p.p() == 0.25);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_eq.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_eq.pass.cpp
new file mode 100644
index 0000000..0d1fe9f
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_eq.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+// {
+// class param_type;
+
+#include <random>
+#include <limits>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p1(3, 0.75);
+ param_type p2(3, 0.75);
+ assert(p1 == p2);
+ }
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ param_type p1(3, 0.75);
+ param_type p2(3, 0.5);
+ assert(p1 != p2);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_types.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_types.pass.cpp
new file mode 100644
index 0000000..85ab987
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/param_types.pass.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+// {
+// class param_type;
+
+#include <random>
+#include <type_traits>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type param_type;
+ typedef param_type::distribution_type distribution_type;
+ static_assert((std::is_same<D, distribution_type>::value), "");
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/set_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/set_param.pass.cpp
new file mode 100644
index 0000000..fec8aea
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/set_param.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+
+// void param(const param_type& parm);
+
+#include <random>
+#include <cassert>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::param_type P;
+ P p(10, 0.25);
+ D d(8, 0.75);
+ d.param(p);
+ assert(d.param() == p);
+ }
+}
diff --git a/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/types.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/types.pass.cpp
new file mode 100644
index 0000000..fc4062f
--- /dev/null
+++ b/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.negbin/types.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// template<class IntType = int>
+// class negative_binomial_distribution
+// {
+// typedef bool result_type;
+
+#include <random>
+#include <type_traits>
+
+int main()
+{
+ {
+ typedef std::negative_binomial_distribution<> D;
+ typedef D::result_type result_type;
+ static_assert((std::is_same<result_type, int>::value), "");
+ }
+ {
+ typedef std::negative_binomial_distribution<long> D;
+ typedef D::result_type result_type;
+ static_assert((std::is_same<result_type, long>::value), "");
+ }
+}
diff --git a/test/numerics/rand/rand.util/rand.util.canonical/generate_canonical.pass.cpp b/test/numerics/rand/rand.util/rand.util.canonical/generate_canonical.pass.cpp
index afeeeb2..44cf901 100644
--- a/test/numerics/rand/rand.util/rand.util.canonical/generate_canonical.pass.cpp
+++ b/test/numerics/rand/rand.util/rand.util.canonical/generate_canonical.pass.cpp
@@ -15,8 +15,6 @@
#include <random>
#include <cassert>
-#include <iostream>
-
int main()
{
{