1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef _LIBCPP___CXX03___RANDOM_BINOMIAL_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_BINOMIAL_DISTRIBUTION_H
11
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/is_valid.h>
14 #include <__cxx03/__random/uniform_real_distribution.h>
15 #include <__cxx03/cmath>
16 #include <__cxx03/iosfwd>
17
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
20 #endif
21
22 _LIBCPP_PUSH_MACROS
23 #include <__cxx03/__undef_macros>
24
25 _LIBCPP_BEGIN_NAMESPACE_STD
26
27 template <class _IntType = int>
28 class _LIBCPP_TEMPLATE_VIS binomial_distribution {
29 static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
30
31 public:
32 // types
33 typedef _IntType result_type;
34
35 class _LIBCPP_TEMPLATE_VIS param_type {
36 result_type __t_;
37 double __p_;
38 double __pr_;
39 double __odds_ratio_;
40 result_type __r0_;
41
42 public:
43 typedef binomial_distribution distribution_type;
44
45 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);
46
t()47 _LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }
p()48 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
49
50 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
51 return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;
52 }
53 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
54
55 friend class binomial_distribution;
56 };
57
58 private:
59 param_type __p_;
60
61 public:
62 // constructors and reset functions
63 _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
__p_(param_type (__t,__p))64 : __p_(param_type(__t, __p)) {}
binomial_distribution(const param_type & __p)65 _LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
reset()66 _LIBCPP_HIDE_FROM_ABI void reset() {}
67
68 // generating functions
69 template <class _URNG>
operator()70 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
71 return (*this)(__g, __p_);
72 }
73 template <class _URNG>
74 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
75
76 // property functions
t()77 _LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }
p()78 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
79
param()80 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)81 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
82
min()83 _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()84 _LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }
85
86 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const binomial_distribution& __x, const binomial_distribution& __y) {
87 return __x.__p_ == __y.__p_;
88 }
89 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const binomial_distribution& __x, const binomial_distribution& __y) {
90 return !(__x == __y);
91 }
92 };
93
94 #ifndef _LIBCPP_MSVCRT_LIKE
95 extern "C" double lgamma_r(double, int*);
96 #endif
97
__libcpp_lgamma(double __d)98 inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
99 #if defined(_LIBCPP_MSVCRT_LIKE)
100 return lgamma(__d);
101 #else
102 int __sign;
103 return lgamma_r(__d, &__sign);
104 #endif
105 }
106
107 template <class _IntType>
param_type(result_type __t,double __p)108 binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {
109 if (0 < __p_ && __p_ < 1) {
110 __r0_ = static_cast<result_type>((__t_ + 1) * __p_);
111 __pr_ = std::exp(
112 std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +
113 __r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));
114 __odds_ratio_ = __p_ / (1 - __p_);
115 }
116 }
117
118 // Reference: Kemp, C.D. (1986). `A modal method for generating binomial
119 // variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
120 template <class _IntType>
121 template <class _URNG>
operator()122 _IntType binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) {
123 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
124 if (__pr.__t_ == 0 || __pr.__p_ == 0)
125 return 0;
126 if (__pr.__p_ == 1)
127 return __pr.__t_;
128 uniform_real_distribution<double> __gen;
129 double __u = __gen(__g) - __pr.__pr_;
130 if (__u < 0)
131 return __pr.__r0_;
132 double __pu = __pr.__pr_;
133 double __pd = __pu;
134 result_type __ru = __pr.__r0_;
135 result_type __rd = __ru;
136 while (true) {
137 bool __break = true;
138 if (__rd >= 1) {
139 __pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));
140 __u -= __pd;
141 __break = false;
142 if (__u < 0)
143 return __rd - 1;
144 }
145 if (__rd != 0)
146 --__rd;
147 ++__ru;
148 if (__ru <= __pr.__t_) {
149 __pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;
150 __u -= __pu;
151 __break = false;
152 if (__u < 0)
153 return __ru;
154 }
155 if (__break)
156 return 0;
157 }
158 }
159
160 template <class _CharT, class _Traits, class _IntType>
161 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
162 operator<<(basic_ostream<_CharT, _Traits>& __os, const binomial_distribution<_IntType>& __x) {
163 __save_flags<_CharT, _Traits> __lx(__os);
164 typedef basic_ostream<_CharT, _Traits> _OStream;
165 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
166 _CharT __sp = __os.widen(' ');
167 __os.fill(__sp);
168 return __os << __x.t() << __sp << __x.p();
169 }
170
171 template <class _CharT, class _Traits, class _IntType>
172 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
173 operator>>(basic_istream<_CharT, _Traits>& __is, binomial_distribution<_IntType>& __x) {
174 typedef binomial_distribution<_IntType> _Eng;
175 typedef typename _Eng::result_type result_type;
176 typedef typename _Eng::param_type param_type;
177 __save_flags<_CharT, _Traits> __lx(__is);
178 typedef basic_istream<_CharT, _Traits> _Istream;
179 __is.flags(_Istream::dec | _Istream::skipws);
180 result_type __t;
181 double __p;
182 __is >> __t >> __p;
183 if (!__is.fail())
184 __x.param(param_type(__t, __p));
185 return __is;
186 }
187
188 _LIBCPP_END_NAMESPACE_STD
189
190 _LIBCPP_POP_MACROS
191
192 #endif // _LIBCPP___CXX03___RANDOM_BINOMIAL_DISTRIBUTION_H
193