binomial.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 continous 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/distributions/fwd.hpp>
  63. #include <boost/math/special_functions/beta.hpp> // for incomplete beta.
  64. #include <boost/math/distributions/complement.hpp> // complements
  65. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  66. #include <boost/math/distributions/detail/inv_discrete_quantile.hpp> // error checks
  67. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  68. #include <boost/math/tools/roots.hpp> // for root finding.
  69. #include <utility>
  70. namespace boost
  71. {
  72. namespace math
  73. {
  74. template <class RealType, class Policy>
  75. class binomial_distribution;
  76. namespace binomial_detail{
  77. // common error checking routines for binomial distribution functions:
  78. template <class RealType, class Policy>
  79. inline bool check_N(const char* function, const RealType& N, RealType* result, const Policy& pol)
  80. {
  81. if((N < 0) || !(boost::math::isfinite)(N))
  82. {
  83. *result = policies::raise_domain_error<RealType>(
  84. function,
  85. "Number of Trials argument is %1%, but must be >= 0 !", N, pol);
  86. return false;
  87. }
  88. return true;
  89. }
  90. template <class RealType, class Policy>
  91. inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& pol)
  92. {
  93. if((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
  94. {
  95. *result = policies::raise_domain_error<RealType>(
  96. function,
  97. "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, pol);
  98. return false;
  99. }
  100. return true;
  101. }
  102. template <class RealType, class Policy>
  103. inline bool check_dist(const char* function, const RealType& N, const RealType& p, RealType* result, const Policy& pol)
  104. {
  105. return check_success_fraction(
  106. function, p, result, pol)
  107. && check_N(
  108. function, N, result, pol);
  109. }
  110. template <class RealType, class Policy>
  111. inline bool check_dist_and_k(const char* function, const RealType& N, const RealType& p, RealType k, RealType* result, const Policy& pol)
  112. {
  113. if(check_dist(function, N, p, result, pol) == false)
  114. return false;
  115. if((k < 0) || !(boost::math::isfinite)(k))
  116. {
  117. *result = policies::raise_domain_error<RealType>(
  118. function,
  119. "Number of Successes argument is %1%, but must be >= 0 !", k, pol);
  120. return false;
  121. }
  122. if(k > N)
  123. {
  124. *result = policies::raise_domain_error<RealType>(
  125. function,
  126. "Number of Successes argument is %1%, but must be <= Number of Trials !", k, pol);
  127. return false;
  128. }
  129. return true;
  130. }
  131. template <class RealType, class Policy>
  132. inline bool check_dist_and_prob(const char* function, const RealType& N, RealType p, RealType prob, RealType* result, const Policy& pol)
  133. {
  134. if((check_dist(function, N, p, result, pol) && detail::check_probability(function, prob, result, pol)) == false)
  135. return false;
  136. return true;
  137. }
  138. template <class T, class Policy>
  139. T inverse_binomial_cornish_fisher(T n, T sf, T p, T q, const Policy& pol)
  140. {
  141. BOOST_MATH_STD_USING
  142. // mean:
  143. T m = n * sf;
  144. // standard deviation:
  145. T sigma = sqrt(n * sf * (1 - sf));
  146. // skewness
  147. T sk = (1 - 2 * sf) / sigma;
  148. // kurtosis:
  149. // T k = (1 - 6 * sf * (1 - sf) ) / (n * sf * (1 - sf));
  150. // Get the inverse of a std normal distribution:
  151. T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
  152. // Set the sign:
  153. if(p < 0.5)
  154. x = -x;
  155. T x2 = x * x;
  156. // w is correction term due to skewness
  157. T w = x + sk * (x2 - 1) / 6;
  158. /*
  159. // Add on correction due to kurtosis.
  160. // Disabled for now, seems to make things worse?
  161. //
  162. if(n >= 10)
  163. w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36;
  164. */
  165. w = m + sigma * w;
  166. if(w < tools::min_value<T>())
  167. return sqrt(tools::min_value<T>());
  168. if(w > n)
  169. return n;
  170. return w;
  171. }
  172. template <class RealType, class Policy>
  173. RealType quantile_imp(const binomial_distribution<RealType, Policy>& dist, const RealType& p, const RealType& q, bool comp)
  174. { // Quantile or Percent Point Binomial function.
  175. // Return the number of expected successes k,
  176. // for a given probability p.
  177. //
  178. // Error checks:
  179. BOOST_MATH_STD_USING // ADL of std names
  180. RealType result = 0;
  181. RealType trials = dist.trials();
  182. RealType success_fraction = dist.success_fraction();
  183. if(false == binomial_detail::check_dist_and_prob(
  184. "boost::math::quantile(binomial_distribution<%1%> const&, %1%)",
  185. trials,
  186. success_fraction,
  187. p,
  188. &result, Policy()))
  189. {
  190. return result;
  191. }
  192. // Special cases:
  193. //
  194. if(p == 0)
  195. { // There may actually be no answer to this question,
  196. // since the probability of zero successes may be non-zero,
  197. // but zero is the best we can do:
  198. return 0;
  199. }
  200. if(p == 1)
  201. { // Probability of n or fewer successes is always one,
  202. // so n is the most sensible answer here:
  203. return trials;
  204. }
  205. if (p <= pow(1 - success_fraction, trials))
  206. { // p <= pdf(dist, 0) == cdf(dist, 0)
  207. return 0; // So the only reasonable result is zero.
  208. } // And root finder would fail otherwise.
  209. if(success_fraction == 1)
  210. { // our formulae break down in this case:
  211. return p > 0.5f ? trials : 0;
  212. }
  213. // Solve for quantile numerically:
  214. //
  215. RealType guess = binomial_detail::inverse_binomial_cornish_fisher(trials, success_fraction, p, q, Policy());
  216. RealType factor = 8;
  217. if(trials > 100)
  218. factor = 1.01f; // guess is pretty accurate
  219. else if((trials > 10) && (trials - 1 > guess) && (guess > 3))
  220. factor = 1.15f; // less accurate but OK.
  221. else if(trials < 10)
  222. {
  223. // pretty inaccurate guess in this area:
  224. if(guess > trials / 64)
  225. {
  226. guess = trials / 4;
  227. factor = 2;
  228. }
  229. else
  230. guess = trials / 1024;
  231. }
  232. else
  233. factor = 2; // trials largish, but in far tails.
  234. typedef typename Policy::discrete_quantile_type discrete_quantile_type;
  235. boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  236. return detail::inverse_discrete_quantile(
  237. dist,
  238. comp ? q : p,
  239. comp,
  240. guess,
  241. factor,
  242. RealType(1),
  243. discrete_quantile_type(),
  244. max_iter);
  245. } // quantile
  246. }
  247. template <class RealType = double, class Policy = policies::policy<> >
  248. class binomial_distribution
  249. {
  250. public:
  251. typedef RealType value_type;
  252. typedef Policy policy_type;
  253. binomial_distribution(RealType n = 1, RealType p = 0.5) : m_n(n), m_p(p)
  254. { // Default n = 1 is the Bernoulli distribution
  255. // with equal probability of 'heads' or 'tails.
  256. RealType r;
  257. binomial_detail::check_dist(
  258. "boost::math::binomial_distribution<%1%>::binomial_distribution",
  259. m_n,
  260. m_p,
  261. &r, Policy());
  262. } // binomial_distribution constructor.
  263. RealType success_fraction() const
  264. { // Probability.
  265. return m_p;
  266. }
  267. RealType trials() const
  268. { // Total number of trials.
  269. return m_n;
  270. }
  271. enum interval_type{
  272. clopper_pearson_exact_interval,
  273. jeffreys_prior_interval
  274. };
  275. //
  276. // Estimation of the success fraction parameter.
  277. // The best estimate is actually simply successes/trials,
  278. // these functions are used
  279. // to obtain confidence intervals for the success fraction.
  280. //
  281. static RealType find_lower_bound_on_p(
  282. RealType trials,
  283. RealType successes,
  284. RealType probability,
  285. interval_type t = clopper_pearson_exact_interval)
  286. {
  287. static const char* function = "boost::math::binomial_distribution<%1%>::find_lower_bound_on_p";
  288. // Error checks:
  289. RealType result = 0;
  290. if(false == binomial_detail::check_dist_and_k(
  291. function, trials, RealType(0), successes, &result, Policy())
  292. &&
  293. binomial_detail::check_dist_and_prob(
  294. function, trials, RealType(0), probability, &result, Policy()))
  295. { return result; }
  296. if(successes == 0)
  297. return 0;
  298. // NOTE!!! The Clopper Pearson formula uses "successes" not
  299. // "successes+1" as usual to get the lower bound,
  300. // see http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
  301. return (t == clopper_pearson_exact_interval) ? ibeta_inv(successes, trials - successes + 1, probability, static_cast<RealType*>(0), Policy())
  302. : ibeta_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(0), Policy());
  303. }
  304. static RealType find_upper_bound_on_p(
  305. RealType trials,
  306. RealType successes,
  307. RealType probability,
  308. interval_type t = clopper_pearson_exact_interval)
  309. {
  310. static const char* function = "boost::math::binomial_distribution<%1%>::find_upper_bound_on_p";
  311. // Error checks:
  312. RealType result = 0;
  313. if(false == binomial_detail::check_dist_and_k(
  314. function, trials, RealType(0), successes, &result, Policy())
  315. &&
  316. binomial_detail::check_dist_and_prob(
  317. function, trials, RealType(0), probability, &result, Policy()))
  318. { return result; }
  319. if(trials == successes)
  320. return 1;
  321. return (t == clopper_pearson_exact_interval) ? ibetac_inv(successes + 1, trials - successes, probability, static_cast<RealType*>(0), Policy())
  322. : ibetac_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(0), Policy());
  323. }
  324. // Estimate number of trials parameter:
  325. //
  326. // "How many trials do I need to be P% sure of seeing k events?"
  327. // or
  328. // "How many trials can I have to be P% sure of seeing fewer than k events?"
  329. //
  330. static RealType find_minimum_number_of_trials(
  331. RealType k, // number of events
  332. RealType p, // success fraction
  333. RealType alpha) // risk level
  334. {
  335. static const char* function = "boost::math::binomial_distribution<%1%>::find_minimum_number_of_trials";
  336. // Error checks:
  337. RealType result = 0;
  338. if(false == binomial_detail::check_dist_and_k(
  339. function, k, p, k, &result, Policy())
  340. &&
  341. binomial_detail::check_dist_and_prob(
  342. function, k, p, alpha, &result, Policy()))
  343. { return result; }
  344. result = ibetac_invb(k + 1, p, alpha, Policy()); // returns n - k
  345. return result + k;
  346. }
  347. static RealType find_maximum_number_of_trials(
  348. RealType k, // number of events
  349. RealType p, // success fraction
  350. RealType alpha) // risk level
  351. {
  352. static const char* function = "boost::math::binomial_distribution<%1%>::find_maximum_number_of_trials";
  353. // Error checks:
  354. RealType result = 0;
  355. if(false == binomial_detail::check_dist_and_k(
  356. function, k, p, k, &result, Policy())
  357. &&
  358. binomial_detail::check_dist_and_prob(
  359. function, k, p, alpha, &result, Policy()))
  360. { return result; }
  361. result = ibeta_invb(k + 1, p, alpha, Policy()); // returns n - k
  362. return result + k;
  363. }
  364. private:
  365. RealType m_n; // Not sure if this shouldn't be an int?
  366. RealType m_p; // success_fraction
  367. }; // template <class RealType, class Policy> class binomial_distribution
  368. typedef binomial_distribution<> binomial;
  369. // typedef binomial_distribution<double> binomial;
  370. // IS now included since no longer a name clash with function binomial.
  371. //typedef binomial_distribution<double> binomial; // Reserved name of type double.
  372. template <class RealType, class Policy>
  373. const std::pair<RealType, RealType> range(const binomial_distribution<RealType, Policy>& dist)
  374. { // Range of permissible values for random variable k.
  375. using boost::math::tools::max_value;
  376. return std::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
  377. }
  378. template <class RealType, class Policy>
  379. const std::pair<RealType, RealType> support(const binomial_distribution<RealType, Policy>& dist)
  380. { // Range of supported values for random variable k.
  381. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  382. return std::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
  383. }
  384. template <class RealType, class Policy>
  385. inline RealType mean(const binomial_distribution<RealType, Policy>& dist)
  386. { // Mean of Binomial distribution = np.
  387. return dist.trials() * dist.success_fraction();
  388. } // mean
  389. template <class RealType, class Policy>
  390. inline RealType variance(const binomial_distribution<RealType, Policy>& dist)
  391. { // Variance of Binomial distribution = np(1-p).
  392. return dist.trials() * dist.success_fraction() * (1 - dist.success_fraction());
  393. } // variance
  394. template <class RealType, class Policy>
  395. RealType pdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
  396. { // Probability Density/Mass Function.
  397. BOOST_FPU_EXCEPTION_GUARD
  398. BOOST_MATH_STD_USING // for ADL of std functions
  399. RealType n = dist.trials();
  400. // Error check:
  401. RealType result = 0; // initialization silences some compiler warnings
  402. if(false == binomial_detail::check_dist_and_k(
  403. "boost::math::pdf(binomial_distribution<%1%> const&, %1%)",
  404. n,
  405. dist.success_fraction(),
  406. k,
  407. &result, Policy()))
  408. {
  409. return result;
  410. }
  411. // Special cases of success_fraction, regardless of k successes and regardless of n trials.
  412. if (dist.success_fraction() == 0)
  413. { // probability of zero successes is 1:
  414. return static_cast<RealType>(k == 0 ? 1 : 0);
  415. }
  416. if (dist.success_fraction() == 1)
  417. { // probability of n successes is 1:
  418. return static_cast<RealType>(k == n ? 1 : 0);
  419. }
  420. // k argument may be integral, signed, or unsigned, or floating point.
  421. // If necessary, it has already been promoted from an integral type.
  422. if (n == 0)
  423. {
  424. return 1; // Probability = 1 = certainty.
  425. }
  426. if (k == 0)
  427. { // binomial coeffic (n 0) = 1,
  428. // n ^ 0 = 1
  429. return pow(1 - dist.success_fraction(), n);
  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. 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. 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. 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. 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. 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. 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 percetage 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. 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. 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. 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