geometric.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // boost\math\distributions\geometric.hpp
  2. // Copyright John Maddock 2010.
  3. // Copyright Paul A. Bristow 2010.
  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. // geometric distribution is a discrete probability distribution.
  9. // It expresses the probability distribution of the number (k) of
  10. // events, occurrences, failures or arrivals before the first success.
  11. // supported on the set {0, 1, 2, 3...}
  12. // Note that the set includes zero (unlike some definitions that start at one).
  13. // The random variate k is the number of events, occurrences or arrivals.
  14. // k argument may be integral, signed, or unsigned, or floating point.
  15. // If necessary, it has already been promoted from an integral type.
  16. // Note that the geometric distribution
  17. // (like others including the binomial, geometric & Bernoulli)
  18. // is strictly defined as a discrete function:
  19. // only integral values of k are envisaged.
  20. // However because the method of calculation uses a continuous gamma function,
  21. // it is convenient to treat it as if a continous function,
  22. // and permit non-integral values of k.
  23. // To enforce the strict mathematical model, users should use floor or ceil functions
  24. // on k outside this function to ensure that k is integral.
  25. // See http://en.wikipedia.org/wiki/geometric_distribution
  26. // http://documents.wolfram.com/v5/Add-onsLinks/StandardPackages/Statistics/DiscreteDistributions.html
  27. // http://mathworld.wolfram.com/GeometricDistribution.html
  28. #ifndef BOOST_MATH_SPECIAL_GEOMETRIC_HPP
  29. #define BOOST_MATH_SPECIAL_GEOMETRIC_HPP
  30. #include <boost/math/distributions/fwd.hpp>
  31. #include <boost/math/special_functions/beta.hpp> // for ibeta(a, b, x) == Ix(a, b).
  32. #include <boost/math/distributions/complement.hpp> // complement.
  33. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks domain_error & logic_error.
  34. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  35. #include <boost/math/tools/roots.hpp> // for root finding.
  36. #include <boost/math/distributions/detail/inv_discrete_quantile.hpp>
  37. #include <boost/type_traits/is_floating_point.hpp>
  38. #include <boost/type_traits/is_integral.hpp>
  39. #include <boost/type_traits/is_same.hpp>
  40. #include <boost/mpl/if.hpp>
  41. #include <limits> // using std::numeric_limits;
  42. #include <utility>
  43. #if defined (BOOST_MSVC)
  44. # pragma warning(push)
  45. // This believed not now necessary, so commented out.
  46. //# pragma warning(disable: 4702) // unreachable code.
  47. // in domain_error_imp in error_handling.
  48. #endif
  49. namespace boost
  50. {
  51. namespace math
  52. {
  53. namespace geometric_detail
  54. {
  55. // Common error checking routines for geometric distribution function:
  56. template <class RealType, class Policy>
  57. inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& pol)
  58. {
  59. if( !(boost::math::isfinite)(p) || (p < 0) || (p > 1) )
  60. {
  61. *result = policies::raise_domain_error<RealType>(
  62. function,
  63. "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, pol);
  64. return false;
  65. }
  66. return true;
  67. }
  68. template <class RealType, class Policy>
  69. inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& pol)
  70. {
  71. return check_success_fraction(function, p, result, pol);
  72. }
  73. template <class RealType, class Policy>
  74. inline bool check_dist_and_k(const char* function, const RealType& p, RealType k, RealType* result, const Policy& pol)
  75. {
  76. if(check_dist(function, p, result, pol) == false)
  77. {
  78. return false;
  79. }
  80. if( !(boost::math::isfinite)(k) || (k < 0) )
  81. { // Check k failures.
  82. *result = policies::raise_domain_error<RealType>(
  83. function,
  84. "Number of failures argument is %1%, but must be >= 0 !", k, pol);
  85. return false;
  86. }
  87. return true;
  88. } // Check_dist_and_k
  89. template <class RealType, class Policy>
  90. inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& pol)
  91. {
  92. if((check_dist(function, p, result, pol) && detail::check_probability(function, prob, result, pol)) == false)
  93. {
  94. return false;
  95. }
  96. return true;
  97. } // check_dist_and_prob
  98. } // namespace geometric_detail
  99. template <class RealType = double, class Policy = policies::policy<> >
  100. class geometric_distribution
  101. {
  102. public:
  103. typedef RealType value_type;
  104. typedef Policy policy_type;
  105. geometric_distribution(RealType p) : m_p(p)
  106. { // Constructor stores success_fraction p.
  107. RealType result;
  108. geometric_detail::check_dist(
  109. "geometric_distribution<%1%>::geometric_distribution",
  110. m_p, // Check success_fraction 0 <= p <= 1.
  111. &result, Policy());
  112. } // geometric_distribution constructor.
  113. // Private data getter class member functions.
  114. RealType success_fraction() const
  115. { // Probability of success as fraction in range 0 to 1.
  116. return m_p;
  117. }
  118. RealType successes() const
  119. { // Total number of successes r = 1 (for compatibility with negative binomial?).
  120. return 1;
  121. }
  122. // Parameter estimation.
  123. // (These are copies of negative_binomial distribution with successes = 1).
  124. static RealType find_lower_bound_on_p(
  125. RealType trials,
  126. RealType alpha) // alpha 0.05 equivalent to 95% for one-sided test.
  127. {
  128. static const char* function = "boost::math::geometric<%1%>::find_lower_bound_on_p";
  129. RealType result = 0; // of error checks.
  130. RealType successes = 1;
  131. RealType failures = trials - successes;
  132. if(false == detail::check_probability(function, alpha, &result, Policy())
  133. && geometric_detail::check_dist_and_k(
  134. function, RealType(0), failures, &result, Policy()))
  135. {
  136. return result;
  137. }
  138. // Use complement ibeta_inv function for lower bound.
  139. // This is adapted from the corresponding binomial formula
  140. // here: http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
  141. // This is a Clopper-Pearson interval, and may be overly conservative,
  142. // see also "A Simple Improved Inferential Method for Some
  143. // Discrete Distributions" Yong CAI and K. KRISHNAMOORTHY
  144. // http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
  145. //
  146. return ibeta_inv(successes, failures + 1, alpha, static_cast<RealType*>(0), Policy());
  147. } // find_lower_bound_on_p
  148. static RealType find_upper_bound_on_p(
  149. RealType trials,
  150. RealType alpha) // alpha 0.05 equivalent to 95% for one-sided test.
  151. {
  152. static const char* function = "boost::math::geometric<%1%>::find_upper_bound_on_p";
  153. RealType result = 0; // of error checks.
  154. RealType successes = 1;
  155. RealType failures = trials - successes;
  156. if(false == geometric_detail::check_dist_and_k(
  157. function, RealType(0), failures, &result, Policy())
  158. && detail::check_probability(function, alpha, &result, Policy()))
  159. {
  160. return result;
  161. }
  162. if(failures == 0)
  163. {
  164. return 1;
  165. }// Use complement ibetac_inv function for upper bound.
  166. // Note adjusted failures value: *not* failures+1 as usual.
  167. // This is adapted from the corresponding binomial formula
  168. // here: http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
  169. // This is a Clopper-Pearson interval, and may be overly conservative,
  170. // see also "A Simple Improved Inferential Method for Some
  171. // Discrete Distributions" Yong CAI and K. Krishnamoorthy
  172. // http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
  173. //
  174. return ibetac_inv(successes, failures, alpha, static_cast<RealType*>(0), Policy());
  175. } // find_upper_bound_on_p
  176. // Estimate number of trials :
  177. // "How many trials do I need to be P% sure of seeing k or fewer failures?"
  178. static RealType find_minimum_number_of_trials(
  179. RealType k, // number of failures (k >= 0).
  180. RealType p, // success fraction 0 <= p <= 1.
  181. RealType alpha) // risk level threshold 0 <= alpha <= 1.
  182. {
  183. static const char* function = "boost::math::geometric<%1%>::find_minimum_number_of_trials";
  184. // Error checks:
  185. RealType result = 0;
  186. if(false == geometric_detail::check_dist_and_k(
  187. function, p, k, &result, Policy())
  188. && detail::check_probability(function, alpha, &result, Policy()))
  189. {
  190. return result;
  191. }
  192. result = ibeta_inva(k + 1, p, alpha, Policy()); // returns n - k
  193. return result + k;
  194. } // RealType find_number_of_failures
  195. static RealType find_maximum_number_of_trials(
  196. RealType k, // number of failures (k >= 0).
  197. RealType p, // success fraction 0 <= p <= 1.
  198. RealType alpha) // risk level threshold 0 <= alpha <= 1.
  199. {
  200. static const char* function = "boost::math::geometric<%1%>::find_maximum_number_of_trials";
  201. // Error checks:
  202. RealType result = 0;
  203. if(false == geometric_detail::check_dist_and_k(
  204. function, p, k, &result, Policy())
  205. && detail::check_probability(function, alpha, &result, Policy()))
  206. {
  207. return result;
  208. }
  209. result = ibetac_inva(k + 1, p, alpha, Policy()); // returns n - k
  210. return result + k;
  211. } // RealType find_number_of_trials complemented
  212. private:
  213. //RealType m_r; // successes fixed at unity.
  214. RealType m_p; // success_fraction
  215. }; // template <class RealType, class Policy> class geometric_distribution
  216. typedef geometric_distribution<double> geometric; // Reserved name of type double.
  217. template <class RealType, class Policy>
  218. inline const std::pair<RealType, RealType> range(const geometric_distribution<RealType, Policy>& /* dist */)
  219. { // Range of permissible values for random variable k.
  220. using boost::math::tools::max_value;
  221. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // max_integer?
  222. }
  223. template <class RealType, class Policy>
  224. inline const std::pair<RealType, RealType> support(const geometric_distribution<RealType, Policy>& /* dist */)
  225. { // Range of supported values for random variable k.
  226. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  227. using boost::math::tools::max_value;
  228. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // max_integer?
  229. }
  230. template <class RealType, class Policy>
  231. inline RealType mean(const geometric_distribution<RealType, Policy>& dist)
  232. { // Mean of geometric distribution = (1-p)/p.
  233. return (1 - dist.success_fraction() ) / dist.success_fraction();
  234. } // mean
  235. // median implemented via quantile(half) in derived accessors.
  236. template <class RealType, class Policy>
  237. inline RealType mode(const geometric_distribution<RealType, Policy>&)
  238. { // Mode of geometric distribution = zero.
  239. BOOST_MATH_STD_USING // ADL of std functions.
  240. return 0;
  241. } // mode
  242. template <class RealType, class Policy>
  243. inline RealType variance(const geometric_distribution<RealType, Policy>& dist)
  244. { // Variance of Binomial distribution = (1-p) / p^2.
  245. return (1 - dist.success_fraction())
  246. / (dist.success_fraction() * dist.success_fraction());
  247. } // variance
  248. template <class RealType, class Policy>
  249. inline RealType skewness(const geometric_distribution<RealType, Policy>& dist)
  250. { // skewness of geometric distribution = 2-p / (sqrt(r(1-p))
  251. BOOST_MATH_STD_USING // ADL of std functions.
  252. RealType p = dist.success_fraction();
  253. return (2 - p) / sqrt(1 - p);
  254. } // skewness
  255. template <class RealType, class Policy>
  256. inline RealType kurtosis(const geometric_distribution<RealType, Policy>& dist)
  257. { // kurtosis of geometric distribution
  258. // http://en.wikipedia.org/wiki/geometric is kurtosis_excess so add 3
  259. RealType p = dist.success_fraction();
  260. return 3 + (p*p - 6*p + 6) / (1 - p);
  261. } // kurtosis
  262. template <class RealType, class Policy>
  263. inline RealType kurtosis_excess(const geometric_distribution<RealType, Policy>& dist)
  264. { // kurtosis excess of geometric distribution
  265. // http://mathworld.wolfram.com/Kurtosis.html table of kurtosis_excess
  266. RealType p = dist.success_fraction();
  267. return (p*p - 6*p + 6) / (1 - p);
  268. } // kurtosis_excess
  269. // RealType standard_deviation(const geometric_distribution<RealType, Policy>& dist)
  270. // standard_deviation provided by derived accessors.
  271. // RealType hazard(const geometric_distribution<RealType, Policy>& dist)
  272. // hazard of geometric distribution provided by derived accessors.
  273. // RealType chf(const geometric_distribution<RealType, Policy>& dist)
  274. // chf of geometric distribution provided by derived accessors.
  275. template <class RealType, class Policy>
  276. inline RealType pdf(const geometric_distribution<RealType, Policy>& dist, const RealType& k)
  277. { // Probability Density/Mass Function.
  278. BOOST_FPU_EXCEPTION_GUARD
  279. BOOST_MATH_STD_USING // For ADL of math functions.
  280. static const char* function = "boost::math::pdf(const geometric_distribution<%1%>&, %1%)";
  281. RealType p = dist.success_fraction();
  282. RealType result = 0;
  283. if(false == geometric_detail::check_dist_and_k(
  284. function,
  285. p,
  286. k,
  287. &result, Policy()))
  288. {
  289. return result;
  290. }
  291. if (k == 0)
  292. {
  293. return p; // success_fraction
  294. }
  295. RealType q = 1 - p; // Inaccurate for small p?
  296. // So try to avoid inaccuracy for large or small p.
  297. // but has little effect > last significant bit.
  298. //cout << "p * pow(q, k) " << result << endl; // seems best whatever p
  299. //cout << "exp(p * k * log1p(-p)) " << p * exp(k * log1p(-p)) << endl;
  300. //if (p < 0.5)
  301. //{
  302. // result = p * pow(q, k);
  303. //}
  304. //else
  305. //{
  306. // result = p * exp(k * log1p(-p));
  307. //}
  308. result = p * pow(q, k);
  309. return result;
  310. } // geometric_pdf
  311. template <class RealType, class Policy>
  312. inline RealType cdf(const geometric_distribution<RealType, Policy>& dist, const RealType& k)
  313. { // Cumulative Distribution Function of geometric.
  314. static const char* function = "boost::math::cdf(const geometric_distribution<%1%>&, %1%)";
  315. // k argument may be integral, signed, or unsigned, or floating point.
  316. // If necessary, it has already been promoted from an integral type.
  317. RealType p = dist.success_fraction();
  318. // Error check:
  319. RealType result = 0;
  320. if(false == geometric_detail::check_dist_and_k(
  321. function,
  322. p,
  323. k,
  324. &result, Policy()))
  325. {
  326. return result;
  327. }
  328. if(k == 0)
  329. {
  330. return p; // success_fraction
  331. }
  332. //RealType q = 1 - p; // Bad for small p
  333. //RealType probability = 1 - std::pow(q, k+1);
  334. RealType z = boost::math::log1p(-p, Policy()) * (k + 1);
  335. RealType probability = -boost::math::expm1(z, Policy());
  336. return probability;
  337. } // cdf Cumulative Distribution Function geometric.
  338. template <class RealType, class Policy>
  339. inline RealType cdf(const complemented2_type<geometric_distribution<RealType, Policy>, RealType>& c)
  340. { // Complemented Cumulative Distribution Function geometric.
  341. BOOST_MATH_STD_USING
  342. static const char* function = "boost::math::cdf(const geometric_distribution<%1%>&, %1%)";
  343. // k argument may be integral, signed, or unsigned, or floating point.
  344. // If necessary, it has already been promoted from an integral type.
  345. RealType const& k = c.param;
  346. geometric_distribution<RealType, Policy> const& dist = c.dist;
  347. RealType p = dist.success_fraction();
  348. // Error check:
  349. RealType result = 0;
  350. if(false == geometric_detail::check_dist_and_k(
  351. function,
  352. p,
  353. k,
  354. &result, Policy()))
  355. {
  356. return result;
  357. }
  358. RealType z = boost::math::log1p(-p, Policy()) * (k+1);
  359. RealType probability = exp(z);
  360. return probability;
  361. } // cdf Complemented Cumulative Distribution Function geometric.
  362. template <class RealType, class Policy>
  363. inline RealType quantile(const geometric_distribution<RealType, Policy>& dist, const RealType& x)
  364. { // Quantile, percentile/100 or Percent Point geometric function.
  365. // Return the number of expected failures k for a given probability p.
  366. // Inverse cumulative Distribution Function or Quantile (percentile / 100) of geometric Probability.
  367. // k argument may be integral, signed, or unsigned, or floating point.
  368. static const char* function = "boost::math::quantile(const geometric_distribution<%1%>&, %1%)";
  369. BOOST_MATH_STD_USING // ADL of std functions.
  370. RealType success_fraction = dist.success_fraction();
  371. // Check dist and x.
  372. RealType result = 0;
  373. if(false == geometric_detail::check_dist_and_prob
  374. (function, success_fraction, x, &result, Policy()))
  375. {
  376. return result;
  377. }
  378. // Special cases.
  379. if (x == 1)
  380. { // Would need +infinity failures for total confidence.
  381. result = policies::raise_overflow_error<RealType>(
  382. function,
  383. "Probability argument is 1, which implies infinite failures !", Policy());
  384. return result;
  385. // usually means return +std::numeric_limits<RealType>::infinity();
  386. // unless #define BOOST_MATH_THROW_ON_OVERFLOW_ERROR
  387. }
  388. if (x == 0)
  389. { // No failures are expected if P = 0.
  390. return 0; // Total trials will be just dist.successes.
  391. }
  392. // if (P <= pow(dist.success_fraction(), 1))
  393. if (x <= success_fraction)
  394. { // p <= pdf(dist, 0) == cdf(dist, 0)
  395. return 0;
  396. }
  397. if (x == 1)
  398. {
  399. return 0;
  400. }
  401. // log(1-x) /log(1-success_fraction) -1; but use log1p in case success_fraction is small
  402. result = boost::math::log1p(-x, Policy()) / boost::math::log1p(-success_fraction, Policy()) - 1;
  403. // Subtract a few epsilons here too?
  404. // to make sure it doesn't slip over, so ceil would be one too many.
  405. return result;
  406. } // RealType quantile(const geometric_distribution dist, p)
  407. template <class RealType, class Policy>
  408. inline RealType quantile(const complemented2_type<geometric_distribution<RealType, Policy>, RealType>& c)
  409. { // Quantile or Percent Point Binomial function.
  410. // Return the number of expected failures k for a given
  411. // complement of the probability Q = 1 - P.
  412. static const char* function = "boost::math::quantile(const geometric_distribution<%1%>&, %1%)";
  413. BOOST_MATH_STD_USING
  414. // Error checks:
  415. RealType x = c.param;
  416. const geometric_distribution<RealType, Policy>& dist = c.dist;
  417. RealType success_fraction = dist.success_fraction();
  418. RealType result = 0;
  419. if(false == geometric_detail::check_dist_and_prob(
  420. function,
  421. success_fraction,
  422. x,
  423. &result, Policy()))
  424. {
  425. return result;
  426. }
  427. // Special cases:
  428. if(x == 1)
  429. { // There may actually be no answer to this question,
  430. // since the probability of zero failures may be non-zero,
  431. return 0; // but zero is the best we can do:
  432. }
  433. if (-x <= boost::math::powm1(dist.success_fraction(), dist.successes(), Policy()))
  434. { // q <= cdf(complement(dist, 0)) == pdf(dist, 0)
  435. return 0; //
  436. }
  437. if(x == 0)
  438. { // Probability 1 - Q == 1 so infinite failures to achieve certainty.
  439. // Would need +infinity failures for total confidence.
  440. result = policies::raise_overflow_error<RealType>(
  441. function,
  442. "Probability argument complement is 0, which implies infinite failures !", Policy());
  443. return result;
  444. // usually means return +std::numeric_limits<RealType>::infinity();
  445. // unless #define BOOST_MATH_THROW_ON_OVERFLOW_ERROR
  446. }
  447. // log(x) /log(1-success_fraction) -1; but use log1p in case success_fraction is small
  448. result = log(x) / boost::math::log1p(-success_fraction, Policy()) - 1;
  449. return result;
  450. } // quantile complement
  451. } // namespace math
  452. } // namespace boost
  453. // This include must be at the end, *after* the accessors
  454. // for this distribution have been defined, in order to
  455. // keep compilers that support two-phase lookup happy.
  456. #include <boost/math/distributions/detail/derived_accessors.hpp>
  457. #if defined (BOOST_MSVC)
  458. # pragma warning(pop)
  459. #endif
  460. #endif // BOOST_MATH_SPECIAL_GEOMETRIC_HPP