14824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 24824e7fdSDimitry Andric // 34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64824e7fdSDimitry Andric // 74824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 84824e7fdSDimitry Andric 94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H 104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H 114824e7fdSDimitry Andric 124824e7fdSDimitry Andric #include <__config> 1381ad6265SDimitry Andric #include <__random/is_valid.h> 144824e7fdSDimitry Andric #include <__random/uniform_real_distribution.h> 154824e7fdSDimitry Andric #include <cmath> 164824e7fdSDimitry Andric #include <iosfwd> 174824e7fdSDimitry Andric 184824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 194824e7fdSDimitry Andric # pragma GCC system_header 204824e7fdSDimitry Andric #endif 214824e7fdSDimitry Andric 224824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS 234824e7fdSDimitry Andric #include <__undef_macros> 244824e7fdSDimitry Andric 254824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 264824e7fdSDimitry Andric 274824e7fdSDimitry Andric template<class _IntType = int> 284824e7fdSDimitry Andric class _LIBCPP_TEMPLATE_VIS binomial_distribution 294824e7fdSDimitry Andric { 30fcaf7f86SDimitry Andric static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type"); 314824e7fdSDimitry Andric public: 324824e7fdSDimitry Andric // types 334824e7fdSDimitry Andric typedef _IntType result_type; 344824e7fdSDimitry Andric 354824e7fdSDimitry Andric class _LIBCPP_TEMPLATE_VIS param_type 364824e7fdSDimitry Andric { 374824e7fdSDimitry Andric result_type __t_; 384824e7fdSDimitry Andric double __p_; 394824e7fdSDimitry Andric double __pr_; 404824e7fdSDimitry Andric double __odds_ratio_; 414824e7fdSDimitry Andric result_type __r0_; 424824e7fdSDimitry Andric public: 434824e7fdSDimitry Andric typedef binomial_distribution distribution_type; 444824e7fdSDimitry Andric 454824e7fdSDimitry Andric explicit param_type(result_type __t = 1, double __p = 0.5); 464824e7fdSDimitry Andric 474824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 484824e7fdSDimitry Andric result_type t() const {return __t_;} 494824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 504824e7fdSDimitry Andric double p() const {return __p_;} 514824e7fdSDimitry Andric 524824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 534824e7fdSDimitry Andric bool operator==(const param_type& __x, const param_type& __y) 544824e7fdSDimitry Andric {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;} 554824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 564824e7fdSDimitry Andric bool operator!=(const param_type& __x, const param_type& __y) 574824e7fdSDimitry Andric {return !(__x == __y);} 584824e7fdSDimitry Andric 594824e7fdSDimitry Andric friend class binomial_distribution; 604824e7fdSDimitry Andric }; 614824e7fdSDimitry Andric 624824e7fdSDimitry Andric private: 634824e7fdSDimitry Andric param_type __p_; 644824e7fdSDimitry Andric 654824e7fdSDimitry Andric public: 664824e7fdSDimitry Andric // constructors and reset functions 674824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG 684824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 694824e7fdSDimitry Andric binomial_distribution() : binomial_distribution(1) {} 704824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 714824e7fdSDimitry Andric explicit binomial_distribution(result_type __t, double __p = 0.5) 724824e7fdSDimitry Andric : __p_(param_type(__t, __p)) {} 734824e7fdSDimitry Andric #else 744824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 754824e7fdSDimitry Andric explicit binomial_distribution(result_type __t = 1, double __p = 0.5) 764824e7fdSDimitry Andric : __p_(param_type(__t, __p)) {} 774824e7fdSDimitry Andric #endif 784824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 794824e7fdSDimitry Andric explicit binomial_distribution(const param_type& __p) : __p_(__p) {} 804824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 814824e7fdSDimitry Andric void reset() {} 824824e7fdSDimitry Andric 834824e7fdSDimitry Andric // generating functions 844824e7fdSDimitry Andric template<class _URNG> 854824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 864824e7fdSDimitry Andric result_type operator()(_URNG& __g) 874824e7fdSDimitry Andric {return (*this)(__g, __p_);} 884824e7fdSDimitry Andric template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); 894824e7fdSDimitry Andric 904824e7fdSDimitry Andric // property functions 914824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 924824e7fdSDimitry Andric result_type t() const {return __p_.t();} 934824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 944824e7fdSDimitry Andric double p() const {return __p_.p();} 954824e7fdSDimitry Andric 964824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 974824e7fdSDimitry Andric param_type param() const {return __p_;} 984824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 994824e7fdSDimitry Andric void param(const param_type& __p) {__p_ = __p;} 1004824e7fdSDimitry Andric 1014824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 1024824e7fdSDimitry Andric result_type min() const {return 0;} 1034824e7fdSDimitry Andric _LIBCPP_INLINE_VISIBILITY 1044824e7fdSDimitry Andric result_type max() const {return t();} 1054824e7fdSDimitry Andric 1064824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 1074824e7fdSDimitry Andric bool operator==(const binomial_distribution& __x, 1084824e7fdSDimitry Andric const binomial_distribution& __y) 1094824e7fdSDimitry Andric {return __x.__p_ == __y.__p_;} 1104824e7fdSDimitry Andric friend _LIBCPP_INLINE_VISIBILITY 1114824e7fdSDimitry Andric bool operator!=(const binomial_distribution& __x, 1124824e7fdSDimitry Andric const binomial_distribution& __y) 1134824e7fdSDimitry Andric {return !(__x == __y);} 1144824e7fdSDimitry Andric }; 1154824e7fdSDimitry Andric 1164824e7fdSDimitry Andric #ifndef _LIBCPP_MSVCRT_LIKE 1174824e7fdSDimitry Andric extern "C" double lgamma_r(double, int *); 1184824e7fdSDimitry Andric #endif 1194824e7fdSDimitry Andric 1204824e7fdSDimitry Andric inline _LIBCPP_INLINE_VISIBILITY double __libcpp_lgamma(double __d) { 1214824e7fdSDimitry Andric #if defined(_LIBCPP_MSVCRT_LIKE) 1224824e7fdSDimitry Andric return lgamma(__d); 1234824e7fdSDimitry Andric #else 1244824e7fdSDimitry Andric int __sign; 1254824e7fdSDimitry Andric return lgamma_r(__d, &__sign); 1264824e7fdSDimitry Andric #endif 1274824e7fdSDimitry Andric } 1284824e7fdSDimitry Andric 1294824e7fdSDimitry Andric template<class _IntType> 1304824e7fdSDimitry Andric binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) 1314824e7fdSDimitry Andric : __t_(__t), __p_(__p) 1324824e7fdSDimitry Andric { 1334824e7fdSDimitry Andric if (0 < __p_ && __p_ < 1) 1344824e7fdSDimitry Andric { 1354824e7fdSDimitry Andric __r0_ = static_cast<result_type>((__t_ + 1) * __p_); 136*bdd1243dSDimitry Andric __pr_ = _VSTD::exp(std::__libcpp_lgamma(__t_ + 1.) - 137*bdd1243dSDimitry Andric std::__libcpp_lgamma(__r0_ + 1.) - 138*bdd1243dSDimitry Andric std::__libcpp_lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) + 1394824e7fdSDimitry Andric (__t_ - __r0_) * _VSTD::log(1 - __p_)); 1404824e7fdSDimitry Andric __odds_ratio_ = __p_ / (1 - __p_); 1414824e7fdSDimitry Andric } 1424824e7fdSDimitry Andric } 1434824e7fdSDimitry Andric 1444824e7fdSDimitry Andric // Reference: Kemp, C.D. (1986). `A modal method for generating binomial 1454824e7fdSDimitry Andric // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813. 1464824e7fdSDimitry Andric template<class _IntType> 1474824e7fdSDimitry Andric template<class _URNG> 1484824e7fdSDimitry Andric _IntType 1494824e7fdSDimitry Andric binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) 1504824e7fdSDimitry Andric { 15181ad6265SDimitry Andric static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 1524824e7fdSDimitry Andric if (__pr.__t_ == 0 || __pr.__p_ == 0) 1534824e7fdSDimitry Andric return 0; 1544824e7fdSDimitry Andric if (__pr.__p_ == 1) 1554824e7fdSDimitry Andric return __pr.__t_; 1564824e7fdSDimitry Andric uniform_real_distribution<double> __gen; 1574824e7fdSDimitry Andric double __u = __gen(__g) - __pr.__pr_; 1584824e7fdSDimitry Andric if (__u < 0) 1594824e7fdSDimitry Andric return __pr.__r0_; 1604824e7fdSDimitry Andric double __pu = __pr.__pr_; 1614824e7fdSDimitry Andric double __pd = __pu; 1624824e7fdSDimitry Andric result_type __ru = __pr.__r0_; 1634824e7fdSDimitry Andric result_type __rd = __ru; 1644824e7fdSDimitry Andric while (true) 1654824e7fdSDimitry Andric { 1664824e7fdSDimitry Andric bool __break = true; 1674824e7fdSDimitry Andric if (__rd >= 1) 1684824e7fdSDimitry Andric { 1694824e7fdSDimitry Andric __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1)); 1704824e7fdSDimitry Andric __u -= __pd; 1714824e7fdSDimitry Andric __break = false; 1724824e7fdSDimitry Andric if (__u < 0) 1734824e7fdSDimitry Andric return __rd - 1; 1744824e7fdSDimitry Andric } 1754824e7fdSDimitry Andric if ( __rd != 0 ) 1764824e7fdSDimitry Andric --__rd; 1774824e7fdSDimitry Andric ++__ru; 1784824e7fdSDimitry Andric if (__ru <= __pr.__t_) 1794824e7fdSDimitry Andric { 1804824e7fdSDimitry Andric __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru; 1814824e7fdSDimitry Andric __u -= __pu; 1824824e7fdSDimitry Andric __break = false; 1834824e7fdSDimitry Andric if (__u < 0) 1844824e7fdSDimitry Andric return __ru; 1854824e7fdSDimitry Andric } 1864824e7fdSDimitry Andric if (__break) 1874824e7fdSDimitry Andric return 0; 1884824e7fdSDimitry Andric } 1894824e7fdSDimitry Andric } 1904824e7fdSDimitry Andric 1914824e7fdSDimitry Andric template <class _CharT, class _Traits, class _IntType> 192*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1934824e7fdSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, 1944824e7fdSDimitry Andric const binomial_distribution<_IntType>& __x) 1954824e7fdSDimitry Andric { 1964824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__os); 1974824e7fdSDimitry Andric typedef basic_ostream<_CharT, _Traits> _OStream; 1984824e7fdSDimitry Andric __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | 1994824e7fdSDimitry Andric _OStream::scientific); 2004824e7fdSDimitry Andric _CharT __sp = __os.widen(' '); 2014824e7fdSDimitry Andric __os.fill(__sp); 2024824e7fdSDimitry Andric return __os << __x.t() << __sp << __x.p(); 2034824e7fdSDimitry Andric } 2044824e7fdSDimitry Andric 2054824e7fdSDimitry Andric template <class _CharT, class _Traits, class _IntType> 206*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 2074824e7fdSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, 2084824e7fdSDimitry Andric binomial_distribution<_IntType>& __x) 2094824e7fdSDimitry Andric { 2104824e7fdSDimitry Andric typedef binomial_distribution<_IntType> _Eng; 2114824e7fdSDimitry Andric typedef typename _Eng::result_type result_type; 2124824e7fdSDimitry Andric typedef typename _Eng::param_type param_type; 2134824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__is); 2144824e7fdSDimitry Andric typedef basic_istream<_CharT, _Traits> _Istream; 2154824e7fdSDimitry Andric __is.flags(_Istream::dec | _Istream::skipws); 2164824e7fdSDimitry Andric result_type __t; 2174824e7fdSDimitry Andric double __p; 2184824e7fdSDimitry Andric __is >> __t >> __p; 2194824e7fdSDimitry Andric if (!__is.fail()) 2204824e7fdSDimitry Andric __x.param(param_type(__t, __p)); 2214824e7fdSDimitry Andric return __is; 2224824e7fdSDimitry Andric } 2234824e7fdSDimitry Andric 2244824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 2254824e7fdSDimitry Andric 2264824e7fdSDimitry Andric _LIBCPP_POP_MACROS 2274824e7fdSDimitry Andric 2284824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H 229