bernoulli.hpp 12 KB

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