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