bernoulli.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 continous 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 mpl::true_&)
  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 mpl::false_&)
  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. template <class RealType, class Policy>
  113. inline const std::pair<RealType, RealType> range(const bernoulli_distribution<RealType, Policy>& /* dist */)
  114. { // Range of permissible values for random variable k = {0, 1}.
  115. using boost::math::tools::max_value;
  116. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  117. }
  118. template <class RealType, class Policy>
  119. inline const std::pair<RealType, RealType> support(const bernoulli_distribution<RealType, Policy>& /* dist */)
  120. { // Range of supported values for random variable k = {0, 1}.
  121. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  122. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  123. }
  124. template <class RealType, class Policy>
  125. inline RealType mean(const bernoulli_distribution<RealType, Policy>& dist)
  126. { // Mean of bernoulli distribution = p (n = 1).
  127. return dist.success_fraction();
  128. } // mean
  129. // Rely on dereived_accessors quantile(half)
  130. //template <class RealType>
  131. //inline RealType median(const bernoulli_distribution<RealType, Policy>& dist)
  132. //{ // Median of bernoulli distribution is not defined.
  133. // return tools::domain_error<RealType>(BOOST_CURRENT_FUNCTION, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
  134. //} // median
  135. template <class RealType, class Policy>
  136. inline RealType variance(const bernoulli_distribution<RealType, Policy>& dist)
  137. { // Variance of bernoulli distribution =p * q.
  138. return dist.success_fraction() * (1 - dist.success_fraction());
  139. } // variance
  140. template <class RealType, class Policy>
  141. RealType pdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
  142. { // Probability Density/Mass Function.
  143. BOOST_FPU_EXCEPTION_GUARD
  144. // Error check:
  145. RealType result = 0; // of checks.
  146. if(false == bernoulli_detail::check_dist_and_k(
  147. "boost::math::pdf(bernoulli_distribution<%1%>, %1%)",
  148. dist.success_fraction(), // 0 to 1
  149. k, // 0 or 1
  150. &result, Policy()))
  151. {
  152. return result;
  153. }
  154. // Assume k is integral.
  155. if (k == 0)
  156. {
  157. return 1 - dist.success_fraction(); // 1 - p
  158. }
  159. else // k == 1
  160. {
  161. return dist.success_fraction(); // p
  162. }
  163. } // pdf
  164. template <class RealType, class Policy>
  165. inline RealType cdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
  166. { // Cumulative Distribution Function Bernoulli.
  167. RealType p = dist.success_fraction();
  168. // Error check:
  169. RealType result = 0;
  170. if(false == bernoulli_detail::check_dist_and_k(
  171. "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
  172. p,
  173. k,
  174. &result, Policy()))
  175. {
  176. return result;
  177. }
  178. if (k == 0)
  179. {
  180. return 1 - p;
  181. }
  182. else
  183. { // k == 1
  184. return 1;
  185. }
  186. } // bernoulli cdf
  187. template <class RealType, class Policy>
  188. inline RealType cdf(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
  189. { // Complemented Cumulative Distribution Function bernoulli.
  190. RealType const& k = c.param;
  191. bernoulli_distribution<RealType, Policy> const& dist = c.dist;
  192. RealType p = dist.success_fraction();
  193. // Error checks:
  194. RealType result = 0;
  195. if(false == bernoulli_detail::check_dist_and_k(
  196. "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
  197. p,
  198. k,
  199. &result, Policy()))
  200. {
  201. return result;
  202. }
  203. if (k == 0)
  204. {
  205. return p;
  206. }
  207. else
  208. { // k == 1
  209. return 0;
  210. }
  211. } // bernoulli cdf complement
  212. template <class RealType, class Policy>
  213. inline RealType quantile(const bernoulli_distribution<RealType, Policy>& dist, const RealType& p)
  214. { // Quantile or Percent Point Bernoulli function.
  215. // Return the number of expected successes k either 0 or 1.
  216. // for a given probability p.
  217. RealType result = 0; // of error checks:
  218. if(false == bernoulli_detail::check_dist_and_prob(
  219. "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
  220. dist.success_fraction(),
  221. p,
  222. &result, Policy()))
  223. {
  224. return result;
  225. }
  226. if (p <= (1 - dist.success_fraction()))
  227. { // p <= pdf(dist, 0) == cdf(dist, 0)
  228. return 0;
  229. }
  230. else
  231. {
  232. return 1;
  233. }
  234. } // quantile
  235. template <class RealType, class Policy>
  236. inline RealType quantile(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
  237. { // Quantile or Percent Point bernoulli function.
  238. // Return the number of expected successes k for a given
  239. // complement of the probability q.
  240. //
  241. // Error checks:
  242. RealType q = c.param;
  243. const bernoulli_distribution<RealType, Policy>& dist = c.dist;
  244. RealType result = 0;
  245. if(false == bernoulli_detail::check_dist_and_prob(
  246. "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
  247. dist.success_fraction(),
  248. q,
  249. &result, Policy()))
  250. {
  251. return result;
  252. }
  253. if (q <= 1 - dist.success_fraction())
  254. { // // q <= cdf(complement(dist, 0)) == pdf(dist, 0)
  255. return 1;
  256. }
  257. else
  258. {
  259. return 0;
  260. }
  261. } // quantile complemented.
  262. template <class RealType, class Policy>
  263. inline RealType mode(const bernoulli_distribution<RealType, Policy>& dist)
  264. {
  265. return static_cast<RealType>((dist.success_fraction() <= 0.5) ? 0 : 1); // p = 0.5 can be 0 or 1
  266. }
  267. template <class RealType, class Policy>
  268. inline RealType skewness(const bernoulli_distribution<RealType, Policy>& dist)
  269. {
  270. BOOST_MATH_STD_USING; // Aid ADL for sqrt.
  271. RealType p = dist.success_fraction();
  272. return (1 - 2 * p) / sqrt(p * (1 - p));
  273. }
  274. template <class RealType, class Policy>
  275. inline RealType kurtosis_excess(const bernoulli_distribution<RealType, Policy>& dist)
  276. {
  277. RealType p = dist.success_fraction();
  278. // Note Wolfram says this is kurtosis in text, but gamma2 is the kurtosis excess,
  279. // and Wikipedia also says this is the kurtosis excess formula.
  280. // return (6 * p * p - 6 * p + 1) / (p * (1 - p));
  281. // But Wolfram kurtosis article gives this simpler formula for kurtosis excess:
  282. return 1 / (1 - p) + 1/p -6;
  283. }
  284. template <class RealType, class Policy>
  285. inline RealType kurtosis(const bernoulli_distribution<RealType, Policy>& dist)
  286. {
  287. RealType p = dist.success_fraction();
  288. return 1 / (1 - p) + 1/p -6 + 3;
  289. // Simpler than:
  290. // return (6 * p * p - 6 * p + 1) / (p * (1 - p)) + 3;
  291. }
  292. } // namespace math
  293. } // namespace boost
  294. // This include must be at the end, *after* the accessors
  295. // for this distribution have been defined, in order to
  296. // keep compilers that support two-phase lookup happy.
  297. #include <boost/math/distributions/detail/derived_accessors.hpp>
  298. #endif // BOOST_MATH_SPECIAL_BERNOULLI_HPP