binomial.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // boost\math\distributions\binomial.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/binomial_distribution
  9. // Binomial distribution is the discrete probability distribution of
  10. // the number (k) of successes, in a sequence of
  11. // n independent (yes or no, success or failure) Bernoulli trials.
  12. // It expresses the probability of a number of events occurring in a fixed time
  13. // if these events occur with a known average rate (probability of success),
  14. // and are independent of the time since the last event.
  15. // The number of cars that pass through a certain point on a road during a given period of time.
  16. // The number of spelling mistakes a secretary makes while typing a single page.
  17. // The number of phone calls at a call center per minute.
  18. // The number of times a web server is accessed per minute.
  19. // The number of light bulbs that burn out in a certain amount of time.
  20. // The number of roadkill found per unit length of road
  21. // http://en.wikipedia.org/wiki/binomial_distribution
  22. // Given a sample of N measured values k[i],
  23. // we wish to estimate the value of the parameter x (mean)
  24. // of the binomial population from which the sample was drawn.
  25. // To calculate the maximum likelihood value = 1/N sum i = 1 to N of k[i]
  26. // Also may want a function for EXACTLY k.
  27. // And probability that there are EXACTLY k occurrences is
  28. // exp(-x) * pow(x, k) / factorial(k)
  29. // where x is expected occurrences (mean) during the given interval.
  30. // For example, if events occur, on average, every 4 min,
  31. // and we are interested in number of events occurring in 10 min,
  32. // then x = 10/4 = 2.5
  33. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366i.htm
  34. // The binomial distribution is used when there are
  35. // exactly two mutually exclusive outcomes of a trial.
  36. // These outcomes are appropriately labeled "success" and "failure".
  37. // The binomial distribution is used to obtain
  38. // the probability of observing x successes in N trials,
  39. // with the probability of success on a single trial denoted by p.
  40. // The binomial distribution assumes that p is fixed for all trials.
  41. // P(x, p, n) = n!/(x! * (n-x)!) * p^x * (1-p)^(n-x)
  42. // http://mathworld.wolfram.com/BinomialCoefficient.html
  43. // The binomial coefficient (n; k) is the number of ways of picking
  44. // k unordered outcomes from n possibilities,
  45. // also known as a combination or combinatorial number.
  46. // The symbols _nC_k and (n; k) are used to denote a binomial coefficient,
  47. // and are sometimes read as "n choose k."
  48. // (n; k) therefore gives the number of k-subsets possible out of a set of n distinct items.
  49. // For example:
  50. // The 2-subsets of {1,2,3,4} are the six pairs {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, and {3,4}, so (4; 2)==6.
  51. // http://functions.wolfram.com/GammaBetaErf/Binomial/ for evaluation.
  52. // But note that the binomial distribution
  53. // (like others including the poisson, negative binomial & Bernoulli)
  54. // is strictly defined as a discrete function: only integral values of k are envisaged.
  55. // However because of the method of calculation using a continuous gamma function,
  56. // it is convenient to treat it as if a continuous function,
  57. // and permit non-integral values of k.
  58. // To enforce the strict mathematical model, users should use floor or ceil functions
  59. // on k outside this function to ensure that k is integral.
  60. #ifndef BOOST_MATH_SPECIAL_BINOMIAL_HPP
  61. #define BOOST_MATH_SPECIAL_BINOMIAL_HPP
  62. #include <boost/math/tools/config.hpp>
  63. #include <boost/math/tools/tuple.hpp>
  64. #include <boost/math/distributions/fwd.hpp>
  65. #include <boost/math/special_functions/beta.hpp> // for incomplete beta.
  66. #include <boost/math/distributions/complement.hpp> // complements
  67. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  68. #include <boost/math/distributions/detail/inv_discrete_quantile.hpp> // error checks
  69. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  70. #include <boost/math/tools/roots.hpp> // for root finding.
  71. #include <utility>
  72. namespace boost
  73. {
  74. namespace math
  75. {
  76. template <class RealType, class Policy>
  77. class binomial_distribution;
  78. namespace binomial_detail{
  79. // common error checking routines for binomial distribution functions:
  80. template <class RealType, class Policy>
  81. BOOST_MATH_CUDA_ENABLED inline bool check_N(const char* function, const RealType& N, RealType* result, const Policy& pol)
  82. {
  83. if((N < 0) || !(boost::math::isfinite)(N))
  84. {
  85. *result = policies::raise_domain_error<RealType>(
  86. function,
  87. "Number of Trials argument is %1%, but must be >= 0 !", N, pol);
  88. return false;
  89. }
  90. return true;
  91. }
  92. template <class RealType, class Policy>
  93. BOOST_MATH_CUDA_ENABLED inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& pol)
  94. {
  95. if((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
  96. {
  97. *result = policies::raise_domain_error<RealType>(
  98. function,
  99. "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, pol);
  100. return false;
  101. }
  102. return true;
  103. }
  104. template <class RealType, class Policy>
  105. BOOST_MATH_CUDA_ENABLED inline bool check_dist(const char* function, const RealType& N, const RealType& p, RealType* result, const Policy& pol)
  106. {
  107. return check_success_fraction(
  108. function, p, result, pol)
  109. && check_N(
  110. function, N, result, pol);
  111. }
  112. template <class RealType, class Policy>
  113. BOOST_MATH_CUDA_ENABLED inline bool check_dist_and_k(const char* function, const RealType& N, const RealType& p, RealType k, RealType* result, const Policy& pol)
  114. {
  115. if(check_dist(function, N, p, result, pol) == false)
  116. return false;
  117. if((k < 0) || !(boost::math::isfinite)(k))
  118. {
  119. *result = policies::raise_domain_error<RealType>(
  120. function,
  121. "Number of Successes argument is %1%, but must be >= 0 !", k, pol);
  122. return false;
  123. }
  124. if(k > N)
  125. {
  126. *result = policies::raise_domain_error<RealType>(
  127. function,
  128. "Number of Successes argument is %1%, but must be <= Number of Trials !", k, pol);
  129. return false;
  130. }
  131. return true;
  132. }
  133. template <class RealType, class Policy>
  134. BOOST_MATH_CUDA_ENABLED inline bool check_dist_and_prob(const char* function, const RealType& N, RealType p, RealType prob, RealType* result, const Policy& pol)
  135. {
  136. if((check_dist(function, N, p, result, pol) && detail::check_probability(function, prob, result, pol)) == false)
  137. return false;
  138. return true;
  139. }
  140. template <class T, class Policy>
  141. BOOST_MATH_CUDA_ENABLED T inverse_binomial_cornish_fisher(T n, T sf, T p, T q, const Policy& pol)
  142. {
  143. BOOST_MATH_STD_USING
  144. // mean:
  145. T m = n * sf;
  146. // standard deviation:
  147. T sigma = sqrt(n * sf * (1 - sf));
  148. // skewness
  149. T sk = (1 - 2 * sf) / sigma;
  150. // kurtosis:
  151. // T k = (1 - 6 * sf * (1 - sf) ) / (n * sf * (1 - sf));
  152. // Get the inverse of a std normal distribution:
  153. T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
  154. // Set the sign:
  155. if(p < 0.5)
  156. x = -x;
  157. T x2 = x * x;
  158. // w is correction term due to skewness
  159. T w = x + sk * (x2 - 1) / 6;
  160. /*
  161. // Add on correction due to kurtosis.
  162. // Disabled for now, seems to make things worse?
  163. //
  164. if(n >= 10)
  165. w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36;
  166. */
  167. w = m + sigma * w;
  168. if(w < tools::min_value<T>())
  169. return sqrt(tools::min_value<T>());
  170. if(w > n)
  171. return n;
  172. return w;
  173. }
  174. template <class RealType, class Policy>
  175. BOOST_MATH_CUDA_ENABLED RealType quantile_imp(const binomial_distribution<RealType, Policy>& dist, const RealType& p, const RealType& q, bool comp)
  176. { // Quantile or Percent Point Binomial function.
  177. // Return the number of expected successes k,
  178. // for a given probability p.
  179. //
  180. // Error checks:
  181. BOOST_MATH_STD_USING // ADL of std names
  182. RealType result = 0;
  183. RealType trials = dist.trials();
  184. RealType success_fraction = dist.success_fraction();
  185. if(false == binomial_detail::check_dist_and_prob(
  186. "boost::math::quantile(binomial_distribution<%1%> const&, %1%)",
  187. trials,
  188. success_fraction,
  189. p,
  190. &result, Policy()))
  191. {
  192. return result;
  193. }
  194. // Special cases:
  195. //
  196. if(p == 0)
  197. { // There may actually be no answer to this question,
  198. // since the probability of zero successes may be non-zero,
  199. // but zero is the best we can do:
  200. return 0;
  201. }
  202. if(p == 1 || success_fraction == 1)
  203. { // Probability of n or fewer successes is always one,
  204. // so n is the most sensible answer here:
  205. return trials;
  206. }
  207. if (p <= pow(1 - success_fraction, trials))
  208. { // p <= pdf(dist, 0) == cdf(dist, 0)
  209. return 0; // So the only reasonable result is zero.
  210. } // And root finder would fail otherwise.
  211. // Solve for quantile numerically:
  212. //
  213. RealType guess = binomial_detail::inverse_binomial_cornish_fisher(trials, success_fraction, p, q, Policy());
  214. RealType factor = 8;
  215. if(trials > 100)
  216. factor = 1.01f; // guess is pretty accurate
  217. else if((trials > 10) && (trials - 1 > guess) && (guess > 3))
  218. factor = 1.15f; // less accurate but OK.
  219. else if(trials < 10)
  220. {
  221. // pretty inaccurate guess in this area:
  222. if(guess > trials / 64)
  223. {
  224. guess = trials / 4;
  225. factor = 2;
  226. }
  227. else
  228. guess = trials / 1024;
  229. }
  230. else
  231. factor = 2; // trials largish, but in far tails.
  232. typedef typename Policy::discrete_quantile_type discrete_quantile_type;
  233. std::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  234. result = detail::inverse_discrete_quantile(
  235. dist,
  236. comp ? q : p,
  237. comp,
  238. guess,
  239. factor,
  240. RealType(1),
  241. discrete_quantile_type(),
  242. max_iter);
  243. return result;
  244. } // quantile
  245. }
  246. template <class RealType = double, class Policy = policies::policy<> >
  247. class binomial_distribution
  248. {
  249. public:
  250. typedef RealType value_type;
  251. typedef Policy policy_type;
  252. binomial_distribution(RealType n = 1, RealType p = 0.5) : m_n(n), m_p(p)
  253. { // Default n = 1 is the Bernoulli distribution
  254. // with equal probability of 'heads' or 'tails.
  255. RealType r;
  256. binomial_detail::check_dist(
  257. "boost::math::binomial_distribution<%1%>::binomial_distribution",
  258. m_n,
  259. m_p,
  260. &r, Policy());
  261. } // binomial_distribution constructor.
  262. BOOST_MATH_CUDA_ENABLED RealType success_fraction() const
  263. { // Probability.
  264. return m_p;
  265. }
  266. BOOST_MATH_CUDA_ENABLED RealType trials() const
  267. { // Total number of trials.
  268. return m_n;
  269. }
  270. enum interval_type{
  271. clopper_pearson_exact_interval,
  272. jeffreys_prior_interval
  273. };
  274. //
  275. // Estimation of the success fraction parameter.
  276. // The best estimate is actually simply successes/trials,
  277. // these functions are used
  278. // to obtain confidence intervals for the success fraction.
  279. //
  280. BOOST_MATH_CUDA_ENABLED static RealType find_lower_bound_on_p(
  281. RealType trials,
  282. RealType successes,
  283. RealType probability,
  284. interval_type t = clopper_pearson_exact_interval)
  285. {
  286. BOOST_MATH_STATIC const char* function = "boost::math::binomial_distribution<%1%>::find_lower_bound_on_p";
  287. // Error checks:
  288. RealType result = 0;
  289. if(false == binomial_detail::check_dist_and_k(
  290. function, trials, RealType(0), successes, &result, Policy())
  291. &&
  292. binomial_detail::check_dist_and_prob(
  293. function, trials, RealType(0), probability, &result, Policy()))
  294. { return result; }
  295. if(successes == 0)
  296. return 0;
  297. // NOTE!!! The Clopper Pearson formula uses "successes" not
  298. // "successes+1" as usual to get the lower bound,
  299. // see http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
  300. return (t == clopper_pearson_exact_interval) ? ibeta_inv(successes, trials - successes + 1, probability, static_cast<RealType*>(nullptr), Policy())
  301. : ibeta_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(nullptr), Policy());
  302. }
  303. BOOST_MATH_CUDA_ENABLED static RealType find_upper_bound_on_p(
  304. RealType trials,
  305. RealType successes,
  306. RealType probability,
  307. interval_type t = clopper_pearson_exact_interval)
  308. {
  309. BOOST_MATH_STATIC const char* function = "boost::math::binomial_distribution<%1%>::find_upper_bound_on_p";
  310. // Error checks:
  311. RealType result = 0;
  312. if(false == binomial_detail::check_dist_and_k(
  313. function, trials, RealType(0), successes, &result, Policy())
  314. &&
  315. binomial_detail::check_dist_and_prob(
  316. function, trials, RealType(0), probability, &result, Policy()))
  317. { return result; }
  318. if(trials == successes)
  319. return 1;
  320. return (t == clopper_pearson_exact_interval) ? ibetac_inv(successes + 1, trials - successes, probability, static_cast<RealType*>(nullptr), Policy())
  321. : ibetac_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(nullptr), Policy());
  322. }
  323. // Estimate number of trials parameter:
  324. //
  325. // "How many trials do I need to be P% sure of seeing k events?"
  326. // or
  327. // "How many trials can I have to be P% sure of seeing fewer than k events?"
  328. //
  329. BOOST_MATH_CUDA_ENABLED static RealType find_minimum_number_of_trials(
  330. RealType k, // number of events
  331. RealType p, // success fraction
  332. RealType alpha) // risk level
  333. {
  334. BOOST_MATH_STATIC const char* function = "boost::math::binomial_distribution<%1%>::find_minimum_number_of_trials";
  335. // Error checks:
  336. RealType result = 0;
  337. if(false == binomial_detail::check_dist_and_k(
  338. function, k, p, k, &result, Policy())
  339. &&
  340. binomial_detail::check_dist_and_prob(
  341. function, k, p, alpha, &result, Policy()))
  342. { return result; }
  343. result = ibetac_invb(k + 1, p, alpha, Policy()); // returns n - k
  344. return result + k;
  345. }
  346. BOOST_MATH_CUDA_ENABLED static RealType find_maximum_number_of_trials(
  347. RealType k, // number of events
  348. RealType p, // success fraction
  349. RealType alpha) // risk level
  350. {
  351. BOOST_MATH_STATIC const char* function = "boost::math::binomial_distribution<%1%>::find_maximum_number_of_trials";
  352. // Error checks:
  353. RealType result = 0;
  354. if(false == binomial_detail::check_dist_and_k(
  355. function, k, p, k, &result, Policy())
  356. &&
  357. binomial_detail::check_dist_and_prob(
  358. function, k, p, alpha, &result, Policy()))
  359. { return result; }
  360. result = ibeta_invb(k + 1, p, alpha, Policy()); // returns n - k
  361. return result + k;
  362. }
  363. private:
  364. RealType m_n; // Not sure if this shouldn't be an int?
  365. RealType m_p; // success_fraction
  366. }; // template <class RealType, class Policy> class binomial_distribution
  367. typedef binomial_distribution<> binomial;
  368. // typedef binomial_distribution<double> binomial;
  369. // IS now included since no longer a name clash with function binomial.
  370. //typedef binomial_distribution<double> binomial; // Reserved name of type double.
  371. #ifdef __cpp_deduction_guides
  372. template <class RealType>
  373. binomial_distribution(RealType)->binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  374. template <class RealType>
  375. binomial_distribution(RealType,RealType)->binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  376. #endif
  377. template <class RealType, class Policy>
  378. BOOST_MATH_CUDA_ENABLED const boost::math::pair<RealType, RealType> range(const binomial_distribution<RealType, Policy>& dist)
  379. { // Range of permissible values for random variable k.
  380. using boost::math::tools::max_value;
  381. return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
  382. }
  383. template <class RealType, class Policy>
  384. BOOST_MATH_CUDA_ENABLED const boost::math::pair<RealType, RealType> support(const binomial_distribution<RealType, Policy>& dist)
  385. { // Range of supported values for random variable k.
  386. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  387. return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
  388. }
  389. template <class RealType, class Policy>
  390. BOOST_MATH_CUDA_ENABLED inline RealType mean(const binomial_distribution<RealType, Policy>& dist)
  391. { // Mean of Binomial distribution = np.
  392. return dist.trials() * dist.success_fraction();
  393. } // mean
  394. template <class RealType, class Policy>
  395. BOOST_MATH_CUDA_ENABLED inline RealType variance(const binomial_distribution<RealType, Policy>& dist)
  396. { // Variance of Binomial distribution = np(1-p).
  397. return dist.trials() * dist.success_fraction() * (1 - dist.success_fraction());
  398. } // variance
  399. template <class RealType, class Policy>
  400. BOOST_MATH_CUDA_ENABLED RealType pdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
  401. { // Probability Density/Mass Function.
  402. BOOST_FPU_EXCEPTION_GUARD
  403. BOOST_MATH_STD_USING // for ADL of std functions
  404. RealType n = dist.trials();
  405. // Error check:
  406. RealType result = 0; // initialization silences some compiler warnings
  407. if(false == binomial_detail::check_dist_and_k(
  408. "boost::math::pdf(binomial_distribution<%1%> const&, %1%)",
  409. n,
  410. dist.success_fraction(),
  411. k,
  412. &result, Policy()))
  413. {
  414. return result;
  415. }
  416. // Special cases of success_fraction, regardless of k successes and regardless of n trials.
  417. if (dist.success_fraction() == 0)
  418. { // probability of zero successes is 1:
  419. return static_cast<RealType>(k == 0 ? 1 : 0);
  420. }
  421. if (dist.success_fraction() == 1)
  422. { // probability of n successes is 1:
  423. return static_cast<RealType>(k == n ? 1 : 0);
  424. }
  425. // k argument may be integral, signed, or unsigned, or floating point.
  426. // If necessary, it has already been promoted from an integral type.
  427. if (n == 0)
  428. {
  429. return 1; // Probability = 1 = certainty.
  430. }
  431. if (k == n)
  432. { // binomial coeffic (n n) = 1,
  433. // n ^ 0 = 1
  434. return pow(dist.success_fraction(), k); // * pow((1 - dist.success_fraction()), (n - k)) = 1
  435. }
  436. // Probability of getting exactly k successes
  437. // if C(n, k) is the binomial coefficient then:
  438. //
  439. // f(k; n,p) = C(n, k) * p^k * (1-p)^(n-k)
  440. // = (n!/(k!(n-k)!)) * p^k * (1-p)^(n-k)
  441. // = (tgamma(n+1) / (tgamma(k+1)*tgamma(n-k+1))) * p^k * (1-p)^(n-k)
  442. // = p^k (1-p)^(n-k) / (beta(k+1, n-k+1) * (n+1))
  443. // = ibeta_derivative(k+1, n-k+1, p) / (n+1)
  444. //
  445. using boost::math::ibeta_derivative; // a, b, x
  446. return ibeta_derivative(k+1, n-k+1, dist.success_fraction(), Policy()) / (n+1);
  447. } // pdf
  448. template <class RealType, class Policy>
  449. BOOST_MATH_CUDA_ENABLED inline RealType cdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
  450. { // Cumulative Distribution Function Binomial.
  451. // The random variate k is the number of successes in n trials.
  452. // k argument may be integral, signed, or unsigned, or floating point.
  453. // If necessary, it has already been promoted from an integral type.
  454. // Returns the sum of the terms 0 through k of the Binomial Probability Density/Mass:
  455. //
  456. // i=k
  457. // -- ( n ) i n-i
  458. // > | | p (1-p)
  459. // -- ( i )
  460. // i=0
  461. // The terms are not summed directly instead
  462. // the incomplete beta integral is employed,
  463. // according to the formula:
  464. // P = I[1-p]( n-k, k+1).
  465. // = 1 - I[p](k + 1, n - k)
  466. BOOST_MATH_STD_USING // for ADL of std functions
  467. RealType n = dist.trials();
  468. RealType p = dist.success_fraction();
  469. // Error check:
  470. RealType result = 0;
  471. if(false == binomial_detail::check_dist_and_k(
  472. "boost::math::cdf(binomial_distribution<%1%> const&, %1%)",
  473. n,
  474. p,
  475. k,
  476. &result, Policy()))
  477. {
  478. return result;
  479. }
  480. if (k == n)
  481. {
  482. return 1;
  483. }
  484. // Special cases, regardless of k.
  485. if (p == 0)
  486. { // This need explanation:
  487. // the pdf is zero for all cases except when k == 0.
  488. // For zero p the probability of zero successes is one.
  489. // Therefore the cdf is always 1:
  490. // the probability of k or *fewer* successes is always 1
  491. // if there are never any successes!
  492. return 1;
  493. }
  494. if (p == 1)
  495. { // This is correct but needs explanation:
  496. // when k = 1
  497. // all the cdf and pdf values are zero *except* when k == n,
  498. // and that case has been handled above already.
  499. return 0;
  500. }
  501. //
  502. // P = I[1-p](n - k, k + 1)
  503. // = 1 - I[p](k + 1, n - k)
  504. // Use of ibetac here prevents cancellation errors in calculating
  505. // 1-p if p is very small, perhaps smaller than machine epsilon.
  506. //
  507. // Note that we do not use a finite sum here, since the incomplete
  508. // beta uses a finite sum internally for integer arguments, so
  509. // we'll just let it take care of the necessary logic.
  510. //
  511. return ibetac(k + 1, n - k, p, Policy());
  512. } // binomial cdf
  513. template <class RealType, class Policy>
  514. BOOST_MATH_CUDA_ENABLED inline RealType cdf(const complemented2_type<binomial_distribution<RealType, Policy>, RealType>& c)
  515. { // Complemented Cumulative Distribution Function Binomial.
  516. // The random variate k is the number of successes in n trials.
  517. // k argument may be integral, signed, or unsigned, or floating point.
  518. // If necessary, it has already been promoted from an integral type.
  519. // Returns the sum of the terms k+1 through n of the Binomial Probability Density/Mass:
  520. //
  521. // i=n
  522. // -- ( n ) i n-i
  523. // > | | p (1-p)
  524. // -- ( i )
  525. // i=k+1
  526. // The terms are not summed directly instead
  527. // the incomplete beta integral is employed,
  528. // according to the formula:
  529. // Q = 1 -I[1-p]( n-k, k+1).
  530. // = I[p](k + 1, n - k)
  531. BOOST_MATH_STD_USING // for ADL of std functions
  532. RealType const& k = c.param;
  533. binomial_distribution<RealType, Policy> const& dist = c.dist;
  534. RealType n = dist.trials();
  535. RealType p = dist.success_fraction();
  536. // Error checks:
  537. RealType result = 0;
  538. if(false == binomial_detail::check_dist_and_k(
  539. "boost::math::cdf(binomial_distribution<%1%> const&, %1%)",
  540. n,
  541. p,
  542. k,
  543. &result, Policy()))
  544. {
  545. return result;
  546. }
  547. if (k == n)
  548. { // Probability of greater than n successes is necessarily zero:
  549. return 0;
  550. }
  551. // Special cases, regardless of k.
  552. if (p == 0)
  553. {
  554. // This need explanation: the pdf is zero for all
  555. // cases except when k == 0. For zero p the probability
  556. // of zero successes is one. Therefore the cdf is always
  557. // 1: the probability of *more than* k successes is always 0
  558. // if there are never any successes!
  559. return 0;
  560. }
  561. if (p == 1)
  562. {
  563. // This needs explanation, when p = 1
  564. // we always have n successes, so the probability
  565. // of more than k successes is 1 as long as k < n.
  566. // The k == n case has already been handled above.
  567. return 1;
  568. }
  569. //
  570. // Calculate cdf binomial using the incomplete beta function.
  571. // Q = 1 -I[1-p](n - k, k + 1)
  572. // = I[p](k + 1, n - k)
  573. // Use of ibeta here prevents cancellation errors in calculating
  574. // 1-p if p is very small, perhaps smaller than machine epsilon.
  575. //
  576. // Note that we do not use a finite sum here, since the incomplete
  577. // beta uses a finite sum internally for integer arguments, so
  578. // we'll just let it take care of the necessary logic.
  579. //
  580. return ibeta(k + 1, n - k, p, Policy());
  581. } // binomial cdf
  582. template <class RealType, class Policy>
  583. BOOST_MATH_CUDA_ENABLED inline RealType quantile(const binomial_distribution<RealType, Policy>& dist, const RealType& p)
  584. {
  585. return binomial_detail::quantile_imp(dist, p, RealType(1-p), false);
  586. } // quantile
  587. template <class RealType, class Policy>
  588. BOOST_MATH_CUDA_ENABLED RealType quantile(const complemented2_type<binomial_distribution<RealType, Policy>, RealType>& c)
  589. {
  590. return binomial_detail::quantile_imp(c.dist, RealType(1-c.param), c.param, true);
  591. } // quantile
  592. template <class RealType, class Policy>
  593. BOOST_MATH_CUDA_ENABLED inline RealType mode(const binomial_distribution<RealType, Policy>& dist)
  594. {
  595. BOOST_MATH_STD_USING // ADL of std functions.
  596. RealType p = dist.success_fraction();
  597. RealType n = dist.trials();
  598. return floor(p * (n + 1));
  599. }
  600. template <class RealType, class Policy>
  601. BOOST_MATH_CUDA_ENABLED inline RealType median(const binomial_distribution<RealType, Policy>& dist)
  602. { // Bounds for the median of the negative binomial distribution
  603. // VAN DE VEN R. ; WEBER N. C. ;
  604. // Univ. Sydney, school mathematics statistics, Sydney N.S.W. 2006, AUSTRALIE
  605. // Metrika (Metrika) ISSN 0026-1335 CODEN MTRKA8
  606. // 1993, vol. 40, no3-4, pp. 185-189 (4 ref.)
  607. // Bounds for median and 50 percentage point of binomial and negative binomial distribution
  608. // Metrika, ISSN 0026-1335 (Print) 1435-926X (Online)
  609. // Volume 41, Number 1 / December, 1994, DOI 10.1007/BF01895303
  610. BOOST_MATH_STD_USING // ADL of std functions.
  611. RealType p = dist.success_fraction();
  612. RealType n = dist.trials();
  613. // Wikipedia says one of floor(np) -1, floor (np), floor(np) +1
  614. return floor(p * n); // Chose the middle value.
  615. }
  616. template <class RealType, class Policy>
  617. BOOST_MATH_CUDA_ENABLED inline RealType skewness(const binomial_distribution<RealType, Policy>& dist)
  618. {
  619. BOOST_MATH_STD_USING // ADL of std functions.
  620. RealType p = dist.success_fraction();
  621. RealType n = dist.trials();
  622. return (1 - 2 * p) / sqrt(n * p * (1 - p));
  623. }
  624. template <class RealType, class Policy>
  625. BOOST_MATH_CUDA_ENABLED inline RealType kurtosis(const binomial_distribution<RealType, Policy>& dist)
  626. {
  627. RealType p = dist.success_fraction();
  628. RealType n = dist.trials();
  629. return 3 - 6 / n + 1 / (n * p * (1 - p));
  630. }
  631. template <class RealType, class Policy>
  632. BOOST_MATH_CUDA_ENABLED inline RealType kurtosis_excess(const binomial_distribution<RealType, Policy>& dist)
  633. {
  634. RealType p = dist.success_fraction();
  635. RealType q = 1 - p;
  636. RealType n = dist.trials();
  637. return (1 - 6 * p * q) / (n * p * q);
  638. }
  639. } // namespace math
  640. } // namespace boost
  641. // This include must be at the end, *after* the accessors
  642. // for this distribution have been defined, in order to
  643. // keep compilers that support two-phase lookup happy.
  644. #include <boost/math/distributions/detail/derived_accessors.hpp>
  645. #endif // BOOST_MATH_SPECIAL_BINOMIAL_HPP