geometric.hpp 21 KB

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