Path: blob/main/contrib/llvm-project/libcxx/include/__random/binomial_distribution.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H9#define _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H1011#include <__config>12#include <__random/is_valid.h>13#include <__random/uniform_real_distribution.h>14#include <cmath>15#include <iosfwd>1617#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)18# pragma GCC system_header19#endif2021_LIBCPP_PUSH_MACROS22#include <__undef_macros>2324_LIBCPP_BEGIN_NAMESPACE_STD2526template <class _IntType = int>27class _LIBCPP_TEMPLATE_VIS binomial_distribution {28static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");2930public:31// types32typedef _IntType result_type;3334class _LIBCPP_TEMPLATE_VIS param_type {35result_type __t_;36double __p_;37double __pr_;38double __odds_ratio_;39result_type __r0_;4041public:42typedef binomial_distribution distribution_type;4344_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);4546_LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }47_LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }4849friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {50return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;51}52friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }5354friend class binomial_distribution;55};5657private:58param_type __p_;5960public:61// constructors and reset functions62#ifndef _LIBCPP_CXX03_LANG63_LIBCPP_HIDE_FROM_ABI binomial_distribution() : binomial_distribution(1) {}64_LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t, double __p = 0.5)65: __p_(param_type(__t, __p)) {}66#else67_LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(result_type __t = 1, double __p = 0.5)68: __p_(param_type(__t, __p)) {}69#endif70_LIBCPP_HIDE_FROM_ABI explicit binomial_distribution(const param_type& __p) : __p_(__p) {}71_LIBCPP_HIDE_FROM_ABI void reset() {}7273// generating functions74template <class _URNG>75_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {76return (*this)(__g, __p_);77}78template <class _URNG>79_LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);8081// property functions82_LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }83_LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }8485_LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }86_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }8788_LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }89_LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }9091friend _LIBCPP_HIDE_FROM_ABI bool operator==(const binomial_distribution& __x, const binomial_distribution& __y) {92return __x.__p_ == __y.__p_;93}94friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const binomial_distribution& __x, const binomial_distribution& __y) {95return !(__x == __y);96}97};9899#ifndef _LIBCPP_MSVCRT_LIKE100extern "C" double lgamma_r(double, int*);101#endif102103inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {104#if defined(_LIBCPP_MSVCRT_LIKE)105return lgamma(__d);106#else107int __sign;108return lgamma_r(__d, &__sign);109#endif110}111112template <class _IntType>113binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {114if (0 < __p_ && __p_ < 1) {115__r0_ = static_cast<result_type>((__t_ + 1) * __p_);116__pr_ = std::exp(117std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +118__r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));119__odds_ratio_ = __p_ / (1 - __p_);120}121}122123// Reference: Kemp, C.D. (1986). `A modal method for generating binomial124// variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.125template <class _IntType>126template <class _URNG>127_IntType binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __pr) {128static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");129if (__pr.__t_ == 0 || __pr.__p_ == 0)130return 0;131if (__pr.__p_ == 1)132return __pr.__t_;133uniform_real_distribution<double> __gen;134double __u = __gen(__g) - __pr.__pr_;135if (__u < 0)136return __pr.__r0_;137double __pu = __pr.__pr_;138double __pd = __pu;139result_type __ru = __pr.__r0_;140result_type __rd = __ru;141while (true) {142bool __break = true;143if (__rd >= 1) {144__pd *= __rd / (__pr.__odds_ratio_ * (__pr.__t_ - __rd + 1));145__u -= __pd;146__break = false;147if (__u < 0)148return __rd - 1;149}150if (__rd != 0)151--__rd;152++__ru;153if (__ru <= __pr.__t_) {154__pu *= (__pr.__t_ - __ru + 1) * __pr.__odds_ratio_ / __ru;155__u -= __pu;156__break = false;157if (__u < 0)158return __ru;159}160if (__break)161return 0;162}163}164165template <class _CharT, class _Traits, class _IntType>166_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&167operator<<(basic_ostream<_CharT, _Traits>& __os, const binomial_distribution<_IntType>& __x) {168__save_flags<_CharT, _Traits> __lx(__os);169typedef basic_ostream<_CharT, _Traits> _OStream;170__os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);171_CharT __sp = __os.widen(' ');172__os.fill(__sp);173return __os << __x.t() << __sp << __x.p();174}175176template <class _CharT, class _Traits, class _IntType>177_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&178operator>>(basic_istream<_CharT, _Traits>& __is, binomial_distribution<_IntType>& __x) {179typedef binomial_distribution<_IntType> _Eng;180typedef typename _Eng::result_type result_type;181typedef typename _Eng::param_type param_type;182__save_flags<_CharT, _Traits> __lx(__is);183typedef basic_istream<_CharT, _Traits> _Istream;184__is.flags(_Istream::dec | _Istream::skipws);185result_type __t;186double __p;187__is >> __t >> __p;188if (!__is.fail())189__x.param(param_type(__t, __p));190return __is;191}192193_LIBCPP_END_NAMESPACE_STD194195_LIBCPP_POP_MACROS196197#endif // _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H198199200