From 2bc36fcff3de1ace5c74bb7c1459def41a67e862 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Mon, 17 May 2010 18:31:53 +0000 Subject: [PATCH] [rand.dist.norm.lognormal] git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@103957 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/random | 162 +++++++++++- .../rand.dist.norm.lognormal/assign.pass.cpp | 34 +++ .../rand.dist.norm.lognormal/copy.pass.cpp | 32 +++ .../ctor_double_double.pass.cpp | 40 +++ .../ctor_param.pass.cpp | 30 +++ .../rand.dist.norm.lognormal/eq.pass.cpp | 37 +++ .../rand.dist.norm.lognormal/eval.pass.cpp | 242 +++++++++++++++++ .../eval_param.pass.cpp | 248 ++++++++++++++++++ .../get_param.pass.cpp | 29 ++ .../rand.dist.norm.lognormal/io.pass.cpp | 41 +++ .../rand.dist.norm.lognormal/max.pass.cpp | 28 ++ .../rand.dist.norm.lognormal/min.pass.cpp | 27 ++ .../param_assign.pass.cpp | 32 +++ .../param_copy.pass.cpp | 31 +++ .../param_ctor.pass.cpp | 44 ++++ .../param_eq.pass.cpp | 37 +++ .../param_types.pass.cpp | 28 ++ .../set_param.pass.cpp | 30 +++ .../rand.dist.norm.lognormal/types.pass.cpp | 34 +++ 19 files changed, 1185 insertions(+), 1 deletion(-) create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/assign.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/copy.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_double_double.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eq.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/get_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/io.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/max.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/min.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_assign.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_copy.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_ctor.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_eq.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_types.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/set_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/types.pass.cpp diff --git a/include/random b/include/random index afed5295..a1278fe1 100644 --- a/include/random +++ b/include/random @@ -1124,7 +1124,62 @@ public: }; template - class lognormal_distribution; +class lognormal_distribution +{ +public: + // types + typedef RealType result_type; + + class param_type + { + public: + typedef lognormal_distribution distribution_type; + + explicit param_type(result_type m = 0, result_type s = 1); + + result_type m() const; + result_type s() 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 lognormal_distribution(result_type m = 0, result_type s = 1); + explicit lognormal_distribution(const param_type& parm); + void reset(); + + // generating functions + template result_type operator()(URNG& g); + template result_type operator()(URNG& g, const param_type& parm); + + // property functions + result_type m() const; + result_type s() const; + + param_type param() const; + void param(const param_type& parm); + + result_type min() const; + result_type max() const; + + friend bool operator==(const lognormal_distribution& x, + const lognormal_distribution& y); + friend bool operator!=(const lognormal_distribution& x, + const lognormal_distribution& y); + + template + friend + basic_ostream& + operator<<(basic_ostream& os, + const lognormal_distribution& x); + + template + friend + basic_istream& + operator>>(basic_istream& is, + lognormal_distribution& x); +}; template class chi_squared_distribution @@ -3796,6 +3851,111 @@ operator>>(basic_istream<_CharT, _Traits>& __is, return __is; } +// lognormal_distribution + +template +class lognormal_distribution +{ +public: + // types + typedef _RealType result_type; + + class param_type + { + normal_distribution __nd_; + public: + typedef lognormal_distribution distribution_type; + + explicit param_type(result_type __m = 0, result_type __s = 1) + : __nd_(__m, __s) {} + + result_type m() const {return __nd_.mean();} + result_type s() const {return __nd_.stddev();} + + friend bool operator==(const param_type& __x, const param_type& __y) + {return __x.__nd_ == __y.__nd_;} + friend bool operator!=(const param_type& __x, const param_type& __y) + {return !(__x == __y);} + friend class lognormal_distribution; + + template + friend + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const lognormal_distribution<_RT>& __x); + + template + friend + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>& __is, + lognormal_distribution<_RT>& __x); + }; + +private: + param_type __p_; + +public: + // constructor and reset functions + explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) + : __p_(param_type(__m, __s)) {} + explicit lognormal_distribution(const param_type& __p) + : __p_(__p) {} + void reset() {__p_.__nd_.reset();} + + // generating functions + template result_type operator()(_URNG& __g) + {return (*this)(__g, __p_);} + template result_type operator()(_URNG& __g, const param_type& __p) + {return _STD::exp(const_cast&>(__p.__nd_)(__g));} + + // property functions + result_type m() const {return __p_.m();} + result_type s() const {return __p_.s();} + + param_type param() const {return __p_;} + void param(const param_type& __p) {return __p_ = __p;} + + result_type min() const {return 0;} + result_type max() const {return numeric_limits::infinity();} + + friend bool operator==(const lognormal_distribution& __x, + const lognormal_distribution& __y) + {return __x.__p_ == __y.__p_;} + friend bool operator!=(const lognormal_distribution& __x, + const lognormal_distribution& __y) + {return !(__x == __y);} + + template + friend + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const lognormal_distribution<_RT>& __x); + + template + friend + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>& __is, + lognormal_distribution<_RT>& __x); +}; + +template +inline +basic_ostream<_CharT, _Traits>& +operator<<(basic_ostream<_CharT, _Traits>& __os, + const lognormal_distribution<_RT>& __x) +{ + return __os << __x.__p_.__nd_; +} + +template +inline +basic_istream<_CharT, _Traits>& +operator>>(basic_istream<_CharT, _Traits>& __is, + lognormal_distribution<_RT>& __x) +{ + return __is >> __x.__p_.__nd_; +} + // poisson_distribution template diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/assign.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/assign.pass.cpp new file mode 100644 index 00000000..01e897bf --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// lognormal_distribution& operator=(const lognormal_distribution&); + +#include +#include + +void +test1() +{ + typedef std::lognormal_distribution<> D; + D d1(20, 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.norm/rand.dist.norm.lognormal/copy.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/copy.pass.cpp new file mode 100644 index 00000000..574e8871 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// lognormal_distribution(const lognormal_distribution&); + +#include +#include + +void +test1() +{ + typedef std::lognormal_distribution<> D; + D d1(20, 1.75); + D d2 = d1; + assert(d1 == d2); +} + +int main() +{ + test1(); +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_double_double.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_double_double.pass.cpp new file mode 100644 index 00000000..1d2eafac --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_double_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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// explicit lognormal_distribution(result_type mean = 0, result_type stddev = 1); + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + D d; + assert(d.m() == 0); + assert(d.s() == 1); + } + { + typedef std::lognormal_distribution<> D; + D d(14.5); + assert(d.m() == 14.5); + assert(d.s() == 1); + } + { + typedef std::lognormal_distribution<> D; + D d(14.5, 5.25); + assert(d.m() == 14.5); + assert(d.s() == 5.25); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/ctor_param.pass.cpp new file mode 100644 index 00000000..1011fdb9 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// explicit lognormal_distribution(const param_type& parm); + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + P p(0.25, 10); + D d(p); + assert(d.m() == 0.25); + assert(d.s() == 10); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eq.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eq.pass.cpp new file mode 100644 index 00000000..bb253c18 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// bool operator=(const lognormal_distribution& x, +// const lognormal_distribution& y); +// bool operator!(const lognormal_distribution& x, +// const lognormal_distribution& y); + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + D d1(2.5, 4); + D d2(2.5, 4); + assert(d1 == d2); + } + { + typedef std::lognormal_distribution<> D; + D d1(2.5, 4); + D d2(2.5, 4.5); + assert(d1 != d2); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval.pass.cpp new file mode 100644 index 00000000..4779eb6c --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval.pass.cpp @@ -0,0 +1,242 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// template result_type operator()(_URNG& g); + +#include +#include +#include +#include + +template +inline +T +sqr(T x) +{ + return x * x; +} + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d(-1./8192, 0.015625); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(d.m() + sqr(d.s())/2); + double x_var = (std::exp(sqr(d.s())) - 1) * std::exp(2*d.m() + sqr(d.s())); + double x_skew = (std::exp(sqr(d.s())) + 2) * + std::sqrt((std::exp(sqr(d.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(d.s())) + 2*std::exp(3*sqr(d.s())) + + 3*std::exp(2*sqr(d.s())) - 6; + 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.05); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.25); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d(-1./32, 0.25); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(d.m() + sqr(d.s())/2); + double x_var = (std::exp(sqr(d.s())) - 1) * std::exp(2*d.m() + sqr(d.s())); + double x_skew = (std::exp(sqr(d.s())) + 2) * + std::sqrt((std::exp(sqr(d.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(d.s())) + 2*std::exp(3*sqr(d.s())) + + 3*std::exp(2*sqr(d.s())) - 6; + 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::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d(-1./8, 0.5); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(d.m() + sqr(d.s())/2); + double x_var = (std::exp(sqr(d.s())) - 1) * std::exp(2*d.m() + sqr(d.s())); + double x_skew = (std::exp(sqr(d.s())) + 2) * + std::sqrt((std::exp(sqr(d.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(d.s())) + 2*std::exp(3*sqr(d.s())) + + 3*std::exp(2*sqr(d.s())) - 6; + 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.02); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.05); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d; + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(d.m() + sqr(d.s())/2); + double x_var = (std::exp(sqr(d.s())) - 1) * std::exp(2*d.m() + sqr(d.s())); + double x_skew = (std::exp(sqr(d.s())) + 2) * + std::sqrt((std::exp(sqr(d.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(d.s())) + 2*std::exp(3*sqr(d.s())) + + 3*std::exp(2*sqr(d.s())) - 6; + assert(std::abs(mean - x_mean) / x_mean < 0.01); + assert(std::abs(var - x_var) / x_var < 0.02); + assert(std::abs(skew - x_skew) / x_skew < 0.08); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.4); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d(-0.78125, 1.25); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(d.m() + sqr(d.s())/2); + double x_var = (std::exp(sqr(d.s())) - 1) * std::exp(2*d.m() + sqr(d.s())); + double x_skew = (std::exp(sqr(d.s())) + 2) * + std::sqrt((std::exp(sqr(d.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(d.s())) + 2*std::exp(3*sqr(d.s())) + + 3*std::exp(2*sqr(d.s())) - 6; + assert(std::abs(mean - x_mean) / x_mean < 0.01); + assert(std::abs(var - x_var) / x_var < 0.04); + assert(std::abs(skew - x_skew) / x_skew < 0.2); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.7); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval_param.pass.cpp new file mode 100644 index 00000000..27909b4f --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/eval_param.pass.cpp @@ -0,0 +1,248 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// template result_type operator()(_URNG& g, const param_type& parm); + +#include +#include +#include +#include + +template +inline +T +sqr(T x) +{ + return x * x; +} + +int main() +{ + + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d; + P p(-1./8192, 0.015625); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, p); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(p.m() + sqr(p.s())/2); + double x_var = (std::exp(sqr(p.s())) - 1) * std::exp(2*p.m() + sqr(p.s())); + double x_skew = (std::exp(sqr(p.s())) + 2) * + std::sqrt((std::exp(sqr(p.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(p.s())) + 2*std::exp(3*sqr(p.s())) + + 3*std::exp(2*sqr(p.s())) - 6; + 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.05); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.25); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d; + P p(-1./32, 0.25); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, p); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(p.m() + sqr(p.s())/2); + double x_var = (std::exp(sqr(p.s())) - 1) * std::exp(2*p.m() + sqr(p.s())); + double x_skew = (std::exp(sqr(p.s())) + 2) * + std::sqrt((std::exp(sqr(p.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(p.s())) + 2*std::exp(3*sqr(p.s())) + + 3*std::exp(2*sqr(p.s())) - 6; + 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::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d; + P p(-1./8, 0.5); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, p); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(p.m() + sqr(p.s())/2); + double x_var = (std::exp(sqr(p.s())) - 1) * std::exp(2*p.m() + sqr(p.s())); + double x_skew = (std::exp(sqr(p.s())) + 2) * + std::sqrt((std::exp(sqr(p.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(p.s())) + 2*std::exp(3*sqr(p.s())) + + 3*std::exp(2*sqr(p.s())) - 6; + 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.02); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.05); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d(3, 4); + P p; + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, p); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(p.m() + sqr(p.s())/2); + double x_var = (std::exp(sqr(p.s())) - 1) * std::exp(2*p.m() + sqr(p.s())); + double x_skew = (std::exp(sqr(p.s())) + 2) * + std::sqrt((std::exp(sqr(p.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(p.s())) + 2*std::exp(3*sqr(p.s())) + + 3*std::exp(2*sqr(p.s())) - 6; + assert(std::abs(mean - x_mean) / x_mean < 0.01); + assert(std::abs(var - x_var) / x_var < 0.02); + assert(std::abs(skew - x_skew) / x_skew < 0.08); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.4); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + typedef std::mt19937 G; + G g; + D d; + P p(-0.78125, 1.25); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, p); + assert(v > 0); + u.push_back(v); + } + double mean = std::accumulate(u.begin(), u.end(), 0.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 = std::exp(p.m() + sqr(p.s())/2); + double x_var = (std::exp(sqr(p.s())) - 1) * std::exp(2*p.m() + sqr(p.s())); + double x_skew = (std::exp(sqr(p.s())) + 2) * + std::sqrt((std::exp(sqr(p.s())) - 1)); + double x_kurtosis = std::exp(4*sqr(p.s())) + 2*std::exp(3*sqr(p.s())) + + 3*std::exp(2*sqr(p.s())) - 6; + assert(std::abs(mean - x_mean) / x_mean < 0.01); + assert(std::abs(var - x_var) / x_var < 0.04); + assert(std::abs(skew - x_skew) / x_skew < 0.2); + assert(std::abs(kurtosis - x_kurtosis) / x_kurtosis < 0.7); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/get_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/get_param.pass.cpp new file mode 100644 index 00000000..e8737bb8 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// param_type param() const; + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + P p(.125, .5); + D d(p); + assert(d.param() == p); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/io.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/io.pass.cpp new file mode 100644 index 00000000..ed690d5b --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// template +// basic_ostream& +// operator<<(basic_ostream& os, +// const lognormal_distribution& x); + +// template +// basic_istream& +// operator>>(basic_istream& is, +// lognormal_distribution& x); + +#include +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + D d1(7, 5); + 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.norm/rand.dist.norm.lognormal/max.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/max.pass.cpp new file mode 100644 index 00000000..6cb7ed2c --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/max.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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// result_type max() const; + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + D d(5, .25); + D::result_type m = d.max(); + assert(m == INFINITY); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/min.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/min.pass.cpp new file mode 100644 index 00000000..cd565ac3 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution + +// result_type min() const; + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + D d(.5, .5); + assert(d.min() == 0); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_assign.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_assign.pass.cpp new file mode 100644 index 00000000..ac387424 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p0(.75, 6); + param_type p; + p = p0; + assert(p.m() == .75); + assert(p.s() == 6); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_copy.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_copy.pass.cpp new file mode 100644 index 00000000..cd639342 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p0(10, .125); + param_type p = p0; + assert(p.m() == 10); + assert(p.s() == .125); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_ctor.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_ctor.pass.cpp new file mode 100644 index 00000000..58be96e8 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p; + assert(p.m() == 0); + assert(p.s() == 1); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p(10); + assert(p.m() == 10); + assert(p.s() == 1); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p(10, 5); + assert(p.m() == 10); + assert(p.s() == 5); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_eq.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_eq.pass.cpp new file mode 100644 index 00000000..2beb5782 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p1(0.75, .5); + param_type p2(0.75, .5); + assert(p1 == p2); + } + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + param_type p1(0.75, .5); + param_type p2(0.5, .5); + assert(p1 != p2); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_types.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/param_types.pass.cpp new file mode 100644 index 00000000..1c5a4429 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution +// { +// class param_type; + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type param_type; + typedef param_type::distribution_type distribution_type; + static_assert((std::is_same::value), ""); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/set_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/set_param.pass.cpp new file mode 100644 index 00000000..e0f7660c --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution; + +// void param(const param_type& parm); + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::param_type P; + P p(0.25, 5.5); + D d(0.75, 4); + d.param(p); + assert(d.param() == p); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/types.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/types.pass.cpp new file mode 100644 index 00000000..25504f03 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.norm/rand.dist.norm.lognormal/types.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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class lognormal_distribution +// { +// public: +// // types +// typedef RealType result_type; + +#include +#include + +int main() +{ + { + typedef std::lognormal_distribution<> D; + typedef D::result_type result_type; + static_assert((std::is_same::value), ""); + } + { + typedef std::lognormal_distribution D; + typedef D::result_type result_type; + static_assert((std::is_same::value), ""); + } +}