bernoulli.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // boost\math\distributions\bernoulli.hpp
  2. // Copyright John Maddock 2006.
  3. // Copyright Paul A. Bristow 2007.
  4. // Copyright Matt Borland 2024.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt
  8. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. // http://en.wikipedia.org/wiki/bernoulli_distribution
  10. // http://mathworld.wolfram.com/BernoulliDistribution.html
  11. // bernoulli distribution is the discrete probability distribution of
  12. // the number (k) of successes, in a single Bernoulli trials.
  13. // It is a version of the binomial distribution when n = 1.
  14. // But note that the bernoulli distribution
  15. // (like others including the poisson, binomial & negative binomial)
  16. // is strictly defined as a discrete function: only integral values of k are envisaged.
  17. // However because of the method of calculation using a continuous gamma function,
  18. // it is convenient to treat it as if a continuous function,
  19. // and permit non-integral values of k.
  20. // To enforce the strict mathematical model, users should use floor or ceil functions
  21. // on k outside this function to ensure that k is integral.
  22. #ifndef BOOST_MATH_SPECIAL_BERNOULLI_HPP
  23. #define BOOST_MATH_SPECIAL_BERNOULLI_HPP
  24. #include <boost/math/tools/config.hpp>
  25. #include <boost/math/tools/tuple.hpp>
  26. #include <boost/math/tools/type_traits.hpp>
  27. #include <boost/math/tools/promotion.hpp>
  28. #include <boost/math/distributions/complement.hpp> // complements
  29. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  30. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  31. #include <boost/math/policies/policy.hpp>
  32. #include <boost/math/policies/error_handling.hpp>
  33. #ifndef BOOST_MATH_HAS_NVRTC
  34. #include <utility>
  35. #include <boost/math/distributions/fwd.hpp>
  36. #endif
  37. namespace boost
  38. {
  39. namespace math
  40. {
  41. namespace bernoulli_detail
  42. {
  43. // Common error checking routines for bernoulli distribution functions:
  44. template <class RealType, class Policy>
  45. BOOST_MATH_GPU_ENABLED inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& /* pol */)
  46. {
  47. if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
  48. {
  49. *result = policies::raise_domain_error<RealType>(
  50. function,
  51. "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, Policy());
  52. return false;
  53. }
  54. return true;
  55. }
  56. template <class RealType, class Policy>
  57. BOOST_MATH_GPU_ENABLED inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& /* pol */, const boost::math::true_type&)
  58. {
  59. return check_success_fraction(function, p, result, Policy());
  60. }
  61. template <class RealType, class Policy>
  62. BOOST_MATH_GPU_ENABLED inline bool check_dist(const char* , const RealType& , RealType* , const Policy& /* pol */, const boost::math::false_type&)
  63. {
  64. return true;
  65. }
  66. template <class RealType, class Policy>
  67. BOOST_MATH_GPU_ENABLED inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& /* pol */)
  68. {
  69. return check_dist(function, p, result, Policy(), typename policies::constructor_error_check<Policy>::type());
  70. }
  71. template <class RealType, class Policy>
  72. BOOST_MATH_GPU_ENABLED inline bool check_dist_and_k(const char* function, const RealType& p, RealType k, RealType* result, const Policy& pol)
  73. {
  74. if(check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) == false)
  75. {
  76. return false;
  77. }
  78. if(!(boost::math::isfinite)(k) || !((k == 0) || (k == 1)))
  79. {
  80. *result = policies::raise_domain_error<RealType>(
  81. function,
  82. "Number of successes argument is %1%, but must be 0 or 1 !", k, pol);
  83. return false;
  84. }
  85. return true;
  86. }
  87. template <class RealType, class Policy>
  88. BOOST_MATH_GPU_ENABLED inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& /* pol */)
  89. {
  90. if((check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) && detail::check_probability(function, prob, result, Policy())) == false)
  91. {
  92. return false;
  93. }
  94. return true;
  95. }
  96. } // namespace bernoulli_detail
  97. template <class RealType = double, class Policy = policies::policy<> >
  98. class bernoulli_distribution
  99. {
  100. public:
  101. typedef RealType value_type;
  102. typedef Policy policy_type;
  103. BOOST_MATH_GPU_ENABLED bernoulli_distribution(RealType p = 0.5) : m_p(p)
  104. { // Default probability = half suits 'fair' coin tossing
  105. // where probability of heads == probability of tails.
  106. RealType result; // of checks.
  107. bernoulli_detail::check_dist(
  108. "boost::math::bernoulli_distribution<%1%>::bernoulli_distribution",
  109. m_p,
  110. &result, Policy());
  111. } // bernoulli_distribution constructor.
  112. BOOST_MATH_GPU_ENABLED RealType success_fraction() const
  113. { // Probability.
  114. return m_p;
  115. }
  116. private:
  117. RealType m_p; // success_fraction
  118. }; // template <class RealType> class bernoulli_distribution
  119. typedef bernoulli_distribution<double> bernoulli;
  120. #ifdef __cpp_deduction_guides
  121. template <class RealType>
  122. bernoulli_distribution(RealType)->bernoulli_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  123. #endif
  124. template <class RealType, class Policy>
  125. BOOST_MATH_GPU_ENABLED inline const boost::math::pair<RealType, RealType> range(const bernoulli_distribution<RealType, Policy>& /* dist */)
  126. { // Range of permissible values for random variable k = {0, 1}.
  127. using boost::math::tools::max_value;
  128. return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  129. }
  130. template <class RealType, class Policy>
  131. BOOST_MATH_GPU_ENABLED inline const boost::math::pair<RealType, RealType> support(const bernoulli_distribution<RealType, Policy>& /* dist */)
  132. { // Range of supported values for random variable k = {0, 1}.
  133. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  134. return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  135. }
  136. template <class RealType, class Policy>
  137. BOOST_MATH_GPU_ENABLED inline RealType mean(const bernoulli_distribution<RealType, Policy>& dist)
  138. { // Mean of bernoulli distribution = p (n = 1).
  139. return dist.success_fraction();
  140. } // mean
  141. // Rely on derived_accessors quantile(half)
  142. //template <class RealType>
  143. //inline RealType median(const bernoulli_distribution<RealType, Policy>& dist)
  144. //{ // Median of bernoulli distribution is not defined.
  145. // return tools::domain_error<RealType>(BOOST_CURRENT_FUNCTION, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
  146. //} // median
  147. template <class RealType, class Policy>
  148. BOOST_MATH_GPU_ENABLED inline RealType variance(const bernoulli_distribution<RealType, Policy>& dist)
  149. { // Variance of bernoulli distribution =p * q.
  150. return dist.success_fraction() * (1 - dist.success_fraction());
  151. } // variance
  152. template <class RealType, class Policy>
  153. BOOST_MATH_GPU_ENABLED RealType pdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
  154. { // Probability Density/Mass Function.
  155. BOOST_FPU_EXCEPTION_GUARD
  156. // Error check:
  157. RealType result = 0; // of checks.
  158. if(false == bernoulli_detail::check_dist_and_k(
  159. "boost::math::pdf(bernoulli_distribution<%1%>, %1%)",
  160. dist.success_fraction(), // 0 to 1
  161. k, // 0 or 1
  162. &result, Policy()))
  163. {
  164. return result;
  165. }
  166. // Assume k is integral.
  167. if (k == 0)
  168. {
  169. return 1 - dist.success_fraction(); // 1 - p
  170. }
  171. else // k == 1
  172. {
  173. return dist.success_fraction(); // p
  174. }
  175. } // pdf
  176. template <class RealType, class Policy>
  177. BOOST_MATH_GPU_ENABLED inline RealType cdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
  178. { // Cumulative Distribution Function Bernoulli.
  179. RealType p = dist.success_fraction();
  180. // Error check:
  181. RealType result = 0;
  182. if(false == bernoulli_detail::check_dist_and_k(
  183. "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
  184. p,
  185. k,
  186. &result, Policy()))
  187. {
  188. return result;
  189. }
  190. if (k == 0)
  191. {
  192. return 1 - p;
  193. }
  194. else
  195. { // k == 1
  196. return 1;
  197. }
  198. } // bernoulli cdf
  199. template <class RealType, class Policy>
  200. BOOST_MATH_GPU_ENABLED inline RealType cdf(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
  201. { // Complemented Cumulative Distribution Function bernoulli.
  202. RealType const& k = c.param;
  203. bernoulli_distribution<RealType, Policy> const& dist = c.dist;
  204. RealType p = dist.success_fraction();
  205. // Error checks:
  206. RealType result = 0;
  207. if(false == bernoulli_detail::check_dist_and_k(
  208. "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
  209. p,
  210. k,
  211. &result, Policy()))
  212. {
  213. return result;
  214. }
  215. if (k == 0)
  216. {
  217. return p;
  218. }
  219. else
  220. { // k == 1
  221. return 0;
  222. }
  223. } // bernoulli cdf complement
  224. template <class RealType, class Policy>
  225. BOOST_MATH_GPU_ENABLED inline RealType quantile(const bernoulli_distribution<RealType, Policy>& dist, const RealType& p)
  226. { // Quantile or Percent Point Bernoulli function.
  227. // Return the number of expected successes k either 0 or 1.
  228. // for a given probability p.
  229. RealType result = 0; // of error checks:
  230. if(false == bernoulli_detail::check_dist_and_prob(
  231. "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
  232. dist.success_fraction(),
  233. p,
  234. &result, Policy()))
  235. {
  236. return result;
  237. }
  238. if (p <= (1 - dist.success_fraction()))
  239. { // p <= pdf(dist, 0) == cdf(dist, 0)
  240. return 0;
  241. }
  242. else
  243. {
  244. return 1;
  245. }
  246. } // quantile
  247. template <class RealType, class Policy>
  248. BOOST_MATH_GPU_ENABLED inline RealType quantile(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
  249. { // Quantile or Percent Point bernoulli function.
  250. // Return the number of expected successes k for a given
  251. // complement of the probability q.
  252. //
  253. // Error checks:
  254. RealType q = c.param;
  255. const bernoulli_distribution<RealType, Policy>& dist = c.dist;
  256. RealType result = 0;
  257. if(false == bernoulli_detail::check_dist_and_prob(
  258. "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
  259. dist.success_fraction(),
  260. q,
  261. &result, Policy()))
  262. {
  263. return result;
  264. }
  265. if (q <= 1 - dist.success_fraction())
  266. { // // q <= cdf(complement(dist, 0)) == pdf(dist, 0)
  267. return 1;
  268. }
  269. else
  270. {
  271. return 0;
  272. }
  273. } // quantile complemented.
  274. template <class RealType, class Policy>
  275. BOOST_MATH_GPU_ENABLED inline RealType mode(const bernoulli_distribution<RealType, Policy>& dist)
  276. {
  277. return static_cast<RealType>((dist.success_fraction() <= 0.5) ? 0 : 1); // p = 0.5 can be 0 or 1
  278. }
  279. template <class RealType, class Policy>
  280. BOOST_MATH_GPU_ENABLED inline RealType skewness(const bernoulli_distribution<RealType, Policy>& dist)
  281. {
  282. BOOST_MATH_STD_USING; // Aid ADL for sqrt.
  283. RealType p = dist.success_fraction();
  284. return (1 - 2 * p) / sqrt(p * (1 - p));
  285. }
  286. template <class RealType, class Policy>
  287. BOOST_MATH_GPU_ENABLED inline RealType kurtosis_excess(const bernoulli_distribution<RealType, Policy>& dist)
  288. {
  289. RealType p = dist.success_fraction();
  290. // Note Wolfram says this is kurtosis in text, but gamma2 is the kurtosis excess,
  291. // and Wikipedia also says this is the kurtosis excess formula.
  292. // return (6 * p * p - 6 * p + 1) / (p * (1 - p));
  293. // But Wolfram kurtosis article gives this simpler formula for kurtosis excess:
  294. return 1 / (1 - p) + 1/p -6;
  295. }
  296. template <class RealType, class Policy>
  297. BOOST_MATH_GPU_ENABLED inline RealType kurtosis(const bernoulli_distribution<RealType, Policy>& dist)
  298. {
  299. RealType p = dist.success_fraction();
  300. return 1 / (1 - p) + 1/p -6 + 3;
  301. // Simpler than:
  302. // return (6 * p * p - 6 * p + 1) / (p * (1 - p)) + 3;
  303. }
  304. } // namespace math
  305. } // namespace boost
  306. // This include must be at the end, *after* the accessors
  307. // for this distribution have been defined, in order to
  308. // keep compilers that support two-phase lookup happy.
  309. #include <boost/math/distributions/detail/derived_accessors.hpp>
  310. #endif // BOOST_MATH_SPECIAL_BERNOULLI_HPP