xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/binomial_distribution.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
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>
28*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS binomial_distribution {
29fcaf7f86SDimitry Andric   static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
30*cb14a3feSDimitry Andric 
314824e7fdSDimitry Andric public:
324824e7fdSDimitry Andric   // types
334824e7fdSDimitry Andric   typedef _IntType result_type;
344824e7fdSDimitry Andric 
35*cb14a3feSDimitry Andric   class _LIBCPP_TEMPLATE_VIS param_type {
364824e7fdSDimitry Andric     result_type __t_;
374824e7fdSDimitry Andric     double __p_;
384824e7fdSDimitry Andric     double __pr_;
394824e7fdSDimitry Andric     double __odds_ratio_;
404824e7fdSDimitry Andric     result_type __r0_;
41*cb14a3feSDimitry Andric 
424824e7fdSDimitry Andric   public:
434824e7fdSDimitry Andric     typedef binomial_distribution distribution_type;
444824e7fdSDimitry Andric 
4506c3fb27SDimitry Andric     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);
464824e7fdSDimitry Andric 
t()47*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }
p()48*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
494824e7fdSDimitry Andric 
50*cb14a3feSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
51*cb14a3feSDimitry Andric       return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;
52*cb14a3feSDimitry Andric     }
53*cb14a3feSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
544824e7fdSDimitry Andric 
554824e7fdSDimitry Andric     friend class binomial_distribution;
564824e7fdSDimitry Andric   };
574824e7fdSDimitry Andric 
584824e7fdSDimitry Andric private:
594824e7fdSDimitry Andric   param_type __p_;
604824e7fdSDimitry Andric 
614824e7fdSDimitry Andric public:
624824e7fdSDimitry Andric   // constructors and reset functions
634824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG
binomial_distribution()64*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI binomial_distribution() : binomial_distribution(1) {}
65*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t, double __p = 0.5)
__p_(param_type (__t,__p))664824e7fdSDimitry Andric       : __p_(param_type(__t, __p)) {}
674824e7fdSDimitry Andric #else
68*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
694824e7fdSDimitry Andric       : __p_(param_type(__t, __p)) {}
704824e7fdSDimitry Andric #endif
binomial_distribution(const param_type & __p)71*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
reset()72*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void reset() {}
734824e7fdSDimitry Andric 
744824e7fdSDimitry Andric   // generating functions
754824e7fdSDimitry Andric   template <class _URNG>
operator()76*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
77*cb14a3feSDimitry Andric     return (*this)(__g, __p_);
78*cb14a3feSDimitry Andric   }
7906c3fb27SDimitry Andric   template <class _URNG>
8006c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
814824e7fdSDimitry Andric 
824824e7fdSDimitry Andric   // property functions
t()83*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }
p()84*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
854824e7fdSDimitry Andric 
param()86*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)87*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
884824e7fdSDimitry Andric 
min()89*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()90*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }
914824e7fdSDimitry Andric 
92*cb14a3feSDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const binomial_distribution& __x, const binomial_distribution& __y) {
93*cb14a3feSDimitry Andric     return __x.__p_ == __y.__p_;
94*cb14a3feSDimitry Andric   }
95*cb14a3feSDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const binomial_distribution& __x, const binomial_distribution& __y) {
96*cb14a3feSDimitry Andric     return !(__x == __y);
97*cb14a3feSDimitry Andric   }
984824e7fdSDimitry Andric };
994824e7fdSDimitry Andric 
1004824e7fdSDimitry Andric #ifndef _LIBCPP_MSVCRT_LIKE
1014824e7fdSDimitry Andric extern "C" double lgamma_r(double, int*);
1024824e7fdSDimitry Andric #endif
1034824e7fdSDimitry Andric 
__libcpp_lgamma(double __d)1045f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
1054824e7fdSDimitry Andric #if defined(_LIBCPP_MSVCRT_LIKE)
1064824e7fdSDimitry Andric   return lgamma(__d);
1074824e7fdSDimitry Andric #else
1084824e7fdSDimitry Andric   int __sign;
1094824e7fdSDimitry Andric   return lgamma_r(__d, &__sign);
1104824e7fdSDimitry Andric #endif
1114824e7fdSDimitry Andric }
1124824e7fdSDimitry Andric 
1134824e7fdSDimitry Andric template <class _IntType>
param_type(result_type __t,double __p)114*cb14a3feSDimitry Andric binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {
115*cb14a3feSDimitry Andric   if (0 < __p_ && __p_ < 1) {
1164824e7fdSDimitry Andric     __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
117*cb14a3feSDimitry Andric     __pr_ = std::exp(
118*cb14a3feSDimitry Andric         std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +
119*cb14a3feSDimitry Andric         __r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));
1204824e7fdSDimitry Andric     __odds_ratio_ = __p_ / (1 - __p_);
1214824e7fdSDimitry Andric   }
1224824e7fdSDimitry Andric }
1234824e7fdSDimitry Andric 
1244824e7fdSDimitry Andric // Reference: Kemp, C.D. (1986). `A modal method for generating binomial
1254824e7fdSDimitry Andric //           variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
1264824e7fdSDimitry Andric template <class _IntType>
1274824e7fdSDimitry Andric template <class _URNG>
operator()128*cb14a3feSDimitry Andric _IntType binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) {
12981ad6265SDimitry Andric   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
1304824e7fdSDimitry Andric   if (__pr.__t_ == 0 || __pr.__p_ == 0)
1314824e7fdSDimitry Andric     return 0;
1324824e7fdSDimitry Andric   if (__pr.__p_ == 1)
1334824e7fdSDimitry Andric     return __pr.__t_;
1344824e7fdSDimitry Andric   uniform_real_distribution<double> __gen;
1354824e7fdSDimitry Andric   double __u = __gen(__g) - __pr.__pr_;
1364824e7fdSDimitry Andric   if (__u < 0)
1374824e7fdSDimitry Andric     return __pr.__r0_;
1384824e7fdSDimitry Andric   double __pu      = __pr.__pr_;
1394824e7fdSDimitry Andric   double __pd      = __pu;
1404824e7fdSDimitry Andric   result_type __ru = __pr.__r0_;
1414824e7fdSDimitry Andric   result_type __rd = __ru;
142*cb14a3feSDimitry Andric   while (true) {
1434824e7fdSDimitry Andric     bool __break = true;
144*cb14a3feSDimitry Andric     if (__rd >= 1) {
1454824e7fdSDimitry Andric       __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
1464824e7fdSDimitry Andric       __u -= __pd;
1474824e7fdSDimitry Andric       __break = false;
1484824e7fdSDimitry Andric       if (__u < 0)
1494824e7fdSDimitry Andric         return __rd - 1;
1504824e7fdSDimitry Andric     }
1514824e7fdSDimitry Andric     if (__rd != 0)
1524824e7fdSDimitry Andric       --__rd;
1534824e7fdSDimitry Andric     ++__ru;
154*cb14a3feSDimitry Andric     if (__ru <= __pr.__t_) {
1554824e7fdSDimitry Andric       __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
1564824e7fdSDimitry Andric       __u -= __pu;
1574824e7fdSDimitry Andric       __break = false;
1584824e7fdSDimitry Andric       if (__u < 0)
1594824e7fdSDimitry Andric         return __ru;
1604824e7fdSDimitry Andric     }
1614824e7fdSDimitry Andric     if (__break)
1624824e7fdSDimitry Andric       return 0;
1634824e7fdSDimitry Andric   }
1644824e7fdSDimitry Andric }
1654824e7fdSDimitry Andric 
1664824e7fdSDimitry Andric template <class _CharT, class _Traits, class _IntType>
167bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
168*cb14a3feSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const binomial_distribution<_IntType>& __x) {
1694824e7fdSDimitry Andric   __save_flags<_CharT, _Traits> __lx(__os);
1704824e7fdSDimitry Andric   typedef basic_ostream<_CharT, _Traits> _OStream;
171*cb14a3feSDimitry Andric   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
1724824e7fdSDimitry Andric   _CharT __sp = __os.widen(' ');
1734824e7fdSDimitry Andric   __os.fill(__sp);
1744824e7fdSDimitry Andric   return __os << __x.t() << __sp << __x.p();
1754824e7fdSDimitry Andric }
1764824e7fdSDimitry Andric 
1774824e7fdSDimitry Andric template <class _CharT, class _Traits, class _IntType>
178bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
179*cb14a3feSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, binomial_distribution<_IntType>& __x) {
1804824e7fdSDimitry Andric   typedef binomial_distribution<_IntType> _Eng;
1814824e7fdSDimitry Andric   typedef typename _Eng::result_type result_type;
1824824e7fdSDimitry Andric   typedef typename _Eng::param_type param_type;
1834824e7fdSDimitry Andric   __save_flags<_CharT, _Traits> __lx(__is);
1844824e7fdSDimitry Andric   typedef basic_istream<_CharT, _Traits> _Istream;
1854824e7fdSDimitry Andric   __is.flags(_Istream::dec | _Istream::skipws);
1864824e7fdSDimitry Andric   result_type __t;
1874824e7fdSDimitry Andric   double __p;
1884824e7fdSDimitry Andric   __is >> __t >> __p;
1894824e7fdSDimitry Andric   if (!__is.fail())
1904824e7fdSDimitry Andric     __x.param(param_type(__t, __p));
1914824e7fdSDimitry Andric   return __is;
1924824e7fdSDimitry Andric }
1934824e7fdSDimitry Andric 
1944824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1954824e7fdSDimitry Andric 
1964824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1974824e7fdSDimitry Andric 
1984824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
199