beta.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // boost\math\distributions\beta.hpp
  2. // Copyright John Maddock 2006.
  3. // Copyright Paul A. Bristow 2006.
  4. // Copyright Matt Borland 2023.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt
  8. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. // http://en.wikipedia.org/wiki/Beta_distribution
  10. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366h.htm
  11. // http://mathworld.wolfram.com/BetaDistribution.html
  12. // The Beta Distribution is a continuous probability distribution.
  13. // The beta distribution is used to model events which are constrained to take place
  14. // within an interval defined by maxima and minima,
  15. // so is used extensively in PERT and other project management systems
  16. // to describe the time to completion.
  17. // The cdf of the beta distribution is used as a convenient way
  18. // of obtaining the sum over a set of binomial outcomes.
  19. // The beta distribution is also used in Bayesian statistics.
  20. #ifndef BOOST_MATH_DIST_BETA_HPP
  21. #define BOOST_MATH_DIST_BETA_HPP
  22. #include <boost/math/distributions/fwd.hpp>
  23. #include <boost/math/special_functions/beta.hpp> // for beta.
  24. #include <boost/math/distributions/complement.hpp> // complements.
  25. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  26. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  27. #include <boost/math/tools/roots.hpp> // for root finding.
  28. #if defined (BOOST_MSVC)
  29. # pragma warning(push)
  30. # pragma warning(disable: 4702) // unreachable code
  31. // in domain_error_imp in error_handling
  32. #endif
  33. #include <utility>
  34. namespace boost
  35. {
  36. namespace math
  37. {
  38. namespace beta_detail
  39. {
  40. // Common error checking routines for beta distribution functions:
  41. template <class RealType, class Policy>
  42. inline bool check_alpha(const char* function, const RealType& alpha, RealType* result, const Policy& pol)
  43. {
  44. if(!(boost::math::isfinite)(alpha) || (alpha <= 0))
  45. {
  46. *result = policies::raise_domain_error<RealType>(
  47. function,
  48. "Alpha argument is %1%, but must be > 0 !", alpha, pol);
  49. return false;
  50. }
  51. return true;
  52. } // bool check_alpha
  53. template <class RealType, class Policy>
  54. inline bool check_beta(const char* function, const RealType& beta, RealType* result, const Policy& pol)
  55. {
  56. if(!(boost::math::isfinite)(beta) || (beta <= 0))
  57. {
  58. *result = policies::raise_domain_error<RealType>(
  59. function,
  60. "Beta argument is %1%, but must be > 0 !", beta, pol);
  61. return false;
  62. }
  63. return true;
  64. } // bool check_beta
  65. template <class RealType, class Policy>
  66. inline bool check_prob(const char* function, const RealType& p, RealType* result, const Policy& pol)
  67. {
  68. if((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
  69. {
  70. *result = policies::raise_domain_error<RealType>(
  71. function,
  72. "Probability argument is %1%, but must be >= 0 and <= 1 !", p, pol);
  73. return false;
  74. }
  75. return true;
  76. } // bool check_prob
  77. template <class RealType, class Policy>
  78. inline bool check_x(const char* function, const RealType& x, RealType* result, const Policy& pol)
  79. {
  80. if(!(boost::math::isfinite)(x) || (x < 0) || (x > 1))
  81. {
  82. *result = policies::raise_domain_error<RealType>(
  83. function,
  84. "x argument is %1%, but must be >= 0 and <= 1 !", x, pol);
  85. return false;
  86. }
  87. return true;
  88. } // bool check_x
  89. template <class RealType, class Policy>
  90. inline bool check_dist(const char* function, const RealType& alpha, const RealType& beta, RealType* result, const Policy& pol)
  91. { // Check both alpha and beta.
  92. return check_alpha(function, alpha, result, pol)
  93. && check_beta(function, beta, result, pol);
  94. } // bool check_dist
  95. template <class RealType, class Policy>
  96. inline bool check_dist_and_x(const char* function, const RealType& alpha, const RealType& beta, RealType x, RealType* result, const Policy& pol)
  97. {
  98. return check_dist(function, alpha, beta, result, pol)
  99. && beta_detail::check_x(function, x, result, pol);
  100. } // bool check_dist_and_x
  101. template <class RealType, class Policy>
  102. inline bool check_dist_and_prob(const char* function, const RealType& alpha, const RealType& beta, RealType p, RealType* result, const Policy& pol)
  103. {
  104. return check_dist(function, alpha, beta, result, pol)
  105. && check_prob(function, p, result, pol);
  106. } // bool check_dist_and_prob
  107. template <class RealType, class Policy>
  108. inline bool check_mean(const char* function, const RealType& mean, RealType* result, const Policy& pol)
  109. {
  110. if(!(boost::math::isfinite)(mean) || (mean <= 0))
  111. {
  112. *result = policies::raise_domain_error<RealType>(
  113. function,
  114. "mean argument is %1%, but must be > 0 !", mean, pol);
  115. return false;
  116. }
  117. return true;
  118. } // bool check_mean
  119. template <class RealType, class Policy>
  120. inline bool check_variance(const char* function, const RealType& variance, RealType* result, const Policy& pol)
  121. {
  122. if(!(boost::math::isfinite)(variance) || (variance <= 0))
  123. {
  124. *result = policies::raise_domain_error<RealType>(
  125. function,
  126. "variance argument is %1%, but must be > 0 !", variance, pol);
  127. return false;
  128. }
  129. return true;
  130. } // bool check_variance
  131. } // namespace beta_detail
  132. // typedef beta_distribution<double> beta;
  133. // is deliberately NOT included to avoid a name clash with the beta function.
  134. // Use beta_distribution<> mybeta(...) to construct type double.
  135. template <class RealType = double, class Policy = policies::policy<> >
  136. class beta_distribution
  137. {
  138. public:
  139. typedef RealType value_type;
  140. typedef Policy policy_type;
  141. beta_distribution(RealType l_alpha = 1, RealType l_beta = 1) : m_alpha(l_alpha), m_beta(l_beta)
  142. {
  143. RealType result;
  144. beta_detail::check_dist(
  145. "boost::math::beta_distribution<%1%>::beta_distribution",
  146. m_alpha,
  147. m_beta,
  148. &result, Policy());
  149. } // beta_distribution constructor.
  150. // Accessor functions:
  151. RealType alpha() const
  152. {
  153. return m_alpha;
  154. }
  155. RealType beta() const
  156. { // .
  157. return m_beta;
  158. }
  159. // Estimation of the alpha & beta parameters.
  160. // http://en.wikipedia.org/wiki/Beta_distribution
  161. // gives formulae in section on parameter estimation.
  162. // Also NIST EDA page 3 & 4 give the same.
  163. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366h.htm
  164. // http://www.epi.ucdavis.edu/diagnostictests/betabuster.html
  165. static RealType find_alpha(
  166. RealType mean, // Expected value of mean.
  167. RealType variance) // Expected value of variance.
  168. {
  169. static const char* function = "boost::math::beta_distribution<%1%>::find_alpha";
  170. RealType result = 0; // of error checks.
  171. if(false ==
  172. (
  173. beta_detail::check_mean(function, mean, &result, Policy())
  174. && beta_detail::check_variance(function, variance, &result, Policy())
  175. )
  176. )
  177. {
  178. return result;
  179. }
  180. return mean * (( (mean * (1 - mean)) / variance)- 1);
  181. } // RealType find_alpha
  182. static RealType find_beta(
  183. RealType mean, // Expected value of mean.
  184. RealType variance) // Expected value of variance.
  185. {
  186. static const char* function = "boost::math::beta_distribution<%1%>::find_beta";
  187. RealType result = 0; // of error checks.
  188. if(false ==
  189. (
  190. beta_detail::check_mean(function, mean, &result, Policy())
  191. &&
  192. beta_detail::check_variance(function, variance, &result, Policy())
  193. )
  194. )
  195. {
  196. return result;
  197. }
  198. return (1 - mean) * (((mean * (1 - mean)) /variance)-1);
  199. } // RealType find_beta
  200. // Estimate alpha & beta from either alpha or beta, and x and probability.
  201. // Uses for these parameter estimators are unclear.
  202. static RealType find_alpha(
  203. RealType beta, // from beta.
  204. RealType x, // x.
  205. RealType probability) // cdf
  206. {
  207. static const char* function = "boost::math::beta_distribution<%1%>::find_alpha";
  208. RealType result = 0; // of error checks.
  209. if(false ==
  210. (
  211. beta_detail::check_prob(function, probability, &result, Policy())
  212. &&
  213. beta_detail::check_beta(function, beta, &result, Policy())
  214. &&
  215. beta_detail::check_x(function, x, &result, Policy())
  216. )
  217. )
  218. {
  219. return result;
  220. }
  221. return static_cast<RealType>(ibeta_inva(beta, x, probability, Policy()));
  222. } // RealType find_alpha(beta, a, probability)
  223. static RealType find_beta(
  224. // ibeta_invb(T b, T x, T p); (alpha, x, cdf,)
  225. RealType alpha, // alpha.
  226. RealType x, // probability x.
  227. RealType probability) // probability cdf.
  228. {
  229. static const char* function = "boost::math::beta_distribution<%1%>::find_beta";
  230. RealType result = 0; // of error checks.
  231. if(false ==
  232. (
  233. beta_detail::check_prob(function, probability, &result, Policy())
  234. &&
  235. beta_detail::check_alpha(function, alpha, &result, Policy())
  236. &&
  237. beta_detail::check_x(function, x, &result, Policy())
  238. )
  239. )
  240. {
  241. return result;
  242. }
  243. return static_cast<RealType>(ibeta_invb(alpha, x, probability, Policy()));
  244. } // RealType find_beta(alpha, x, probability)
  245. private:
  246. RealType m_alpha; // Two parameters of the beta distribution.
  247. RealType m_beta;
  248. }; // template <class RealType, class Policy> class beta_distribution
  249. #ifdef __cpp_deduction_guides
  250. template <class RealType>
  251. beta_distribution(RealType)->beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  252. template <class RealType>
  253. beta_distribution(RealType, RealType)->beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  254. #endif
  255. template <class RealType, class Policy>
  256. inline const std::pair<RealType, RealType> range(const beta_distribution<RealType, Policy>& /* dist */)
  257. { // Range of permissible values for random variable x.
  258. using boost::math::tools::max_value;
  259. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  260. }
  261. template <class RealType, class Policy>
  262. inline const std::pair<RealType, RealType> support(const beta_distribution<RealType, Policy>& /* dist */)
  263. { // Range of supported values for random variable x.
  264. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  265. return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  266. }
  267. template <class RealType, class Policy>
  268. inline RealType mean(const beta_distribution<RealType, Policy>& dist)
  269. { // Mean of beta distribution = np.
  270. return dist.alpha() / (dist.alpha() + dist.beta());
  271. } // mean
  272. template <class RealType, class Policy>
  273. inline RealType variance(const beta_distribution<RealType, Policy>& dist)
  274. { // Variance of beta distribution = np(1-p).
  275. RealType a = dist.alpha();
  276. RealType b = dist.beta();
  277. return (a * b) / ((a + b ) * (a + b) * (a + b + 1));
  278. } // variance
  279. template <class RealType, class Policy>
  280. inline RealType mode(const beta_distribution<RealType, Policy>& dist)
  281. {
  282. static const char* function = "boost::math::mode(beta_distribution<%1%> const&)";
  283. RealType result;
  284. if ((dist.alpha() <= 1))
  285. {
  286. result = policies::raise_domain_error<RealType>(
  287. function,
  288. "mode undefined for alpha = %1%, must be > 1!", dist.alpha(), Policy());
  289. return result;
  290. }
  291. if ((dist.beta() <= 1))
  292. {
  293. result = policies::raise_domain_error<RealType>(
  294. function,
  295. "mode undefined for beta = %1%, must be > 1!", dist.beta(), Policy());
  296. return result;
  297. }
  298. RealType a = dist.alpha();
  299. RealType b = dist.beta();
  300. return (a-1) / (a + b - 2);
  301. } // mode
  302. //template <class RealType, class Policy>
  303. //inline RealType median(const beta_distribution<RealType, Policy>& dist)
  304. //{ // Median of beta distribution is not defined.
  305. // return tools::domain_error<RealType>(function, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());
  306. //} // median
  307. //But WILL be provided by the derived accessor as quantile(0.5).
  308. template <class RealType, class Policy>
  309. inline RealType skewness(const beta_distribution<RealType, Policy>& dist)
  310. {
  311. BOOST_MATH_STD_USING // ADL of std functions.
  312. RealType a = dist.alpha();
  313. RealType b = dist.beta();
  314. return (2 * (b-a) * sqrt(a + b + 1)) / ((a + b + 2) * sqrt(a * b));
  315. } // skewness
  316. template <class RealType, class Policy>
  317. inline RealType kurtosis_excess(const beta_distribution<RealType, Policy>& dist)
  318. {
  319. RealType a = dist.alpha();
  320. RealType b = dist.beta();
  321. RealType a_2 = a * a;
  322. RealType n = 6 * (a_2 * a - a_2 * (2 * b - 1) + b * b * (b + 1) - 2 * a * b * (b + 2));
  323. RealType d = a * b * (a + b + 2) * (a + b + 3);
  324. return n / d;
  325. } // kurtosis_excess
  326. template <class RealType, class Policy>
  327. inline RealType kurtosis(const beta_distribution<RealType, Policy>& dist)
  328. {
  329. return 3 + kurtosis_excess(dist);
  330. } // kurtosis
  331. template <class RealType, class Policy>
  332. inline RealType pdf(const beta_distribution<RealType, Policy>& dist, const RealType& x)
  333. { // Probability Density/Mass Function.
  334. BOOST_FPU_EXCEPTION_GUARD
  335. static const char* function = "boost::math::pdf(beta_distribution<%1%> const&, %1%)";
  336. BOOST_MATH_STD_USING // for ADL of std functions
  337. RealType a = dist.alpha();
  338. RealType b = dist.beta();
  339. // Argument checks:
  340. RealType result = 0;
  341. if(false == beta_detail::check_dist_and_x(
  342. function,
  343. a, b, x,
  344. &result, Policy()))
  345. {
  346. return result;
  347. }
  348. using boost::math::beta;
  349. // Corner cases: check_x ensures x element of [0, 1], but PDF is 0 for x = 0 and x = 1. PDF EQN:
  350. // https://wikimedia.org/api/rest_v1/media/math/render/svg/125fdaa41844a8703d1a8610ac00fbf3edacc8e7
  351. if(x == 0)
  352. {
  353. if (a == 1)
  354. {
  355. return static_cast<RealType>(1 / beta(a, b));
  356. }
  357. else if (a < 1)
  358. {
  359. policies::raise_overflow_error<RealType>(function, nullptr, Policy());
  360. }
  361. else
  362. {
  363. return RealType(0);
  364. }
  365. }
  366. else if (x == 1)
  367. {
  368. if (b == 1)
  369. {
  370. return static_cast<RealType>(1 / beta(a, b));
  371. }
  372. else if (b < 1)
  373. {
  374. policies::raise_overflow_error<RealType>(function, nullptr, Policy());
  375. }
  376. else
  377. {
  378. return RealType(0);
  379. }
  380. }
  381. return static_cast<RealType>(ibeta_derivative(a, b, x, Policy()));
  382. } // pdf
  383. template <class RealType, class Policy>
  384. inline RealType cdf(const beta_distribution<RealType, Policy>& dist, const RealType& x)
  385. { // Cumulative Distribution Function beta.
  386. BOOST_MATH_STD_USING // for ADL of std functions
  387. static const char* function = "boost::math::cdf(beta_distribution<%1%> const&, %1%)";
  388. RealType a = dist.alpha();
  389. RealType b = dist.beta();
  390. // Argument checks:
  391. RealType result = 0;
  392. if(false == beta_detail::check_dist_and_x(
  393. function,
  394. a, b, x,
  395. &result, Policy()))
  396. {
  397. return result;
  398. }
  399. // Special cases:
  400. if (x == 0)
  401. {
  402. return 0;
  403. }
  404. else if (x == 1)
  405. {
  406. return 1;
  407. }
  408. return static_cast<RealType>(ibeta(a, b, x, Policy()));
  409. } // beta cdf
  410. template <class RealType, class Policy>
  411. inline RealType cdf(const complemented2_type<beta_distribution<RealType, Policy>, RealType>& c)
  412. { // Complemented Cumulative Distribution Function beta.
  413. BOOST_MATH_STD_USING // for ADL of std functions
  414. static const char* function = "boost::math::cdf(beta_distribution<%1%> const&, %1%)";
  415. RealType const& x = c.param;
  416. beta_distribution<RealType, Policy> const& dist = c.dist;
  417. RealType a = dist.alpha();
  418. RealType b = dist.beta();
  419. // Argument checks:
  420. RealType result = 0;
  421. if(false == beta_detail::check_dist_and_x(
  422. function,
  423. a, b, x,
  424. &result, Policy()))
  425. {
  426. return result;
  427. }
  428. if (x == 0)
  429. {
  430. return RealType(1);
  431. }
  432. else if (x == 1)
  433. {
  434. return RealType(0);
  435. }
  436. // Calculate cdf beta using the incomplete beta function.
  437. // Use of ibeta here prevents cancellation errors in calculating
  438. // 1 - x if x is very small, perhaps smaller than machine epsilon.
  439. return static_cast<RealType>(ibetac(a, b, x, Policy()));
  440. } // beta cdf
  441. template <class RealType, class Policy>
  442. inline RealType quantile(const beta_distribution<RealType, Policy>& dist, const RealType& p)
  443. { // Quantile or Percent Point beta function or
  444. // Inverse Cumulative probability distribution function CDF.
  445. // Return x (0 <= x <= 1),
  446. // for a given probability p (0 <= p <= 1).
  447. // These functions take a probability as an argument
  448. // and return a value such that the probability that a random variable x
  449. // will be less than or equal to that value
  450. // is whatever probability you supplied as an argument.
  451. static const char* function = "boost::math::quantile(beta_distribution<%1%> const&, %1%)";
  452. RealType result = 0; // of argument checks:
  453. RealType a = dist.alpha();
  454. RealType b = dist.beta();
  455. if(false == beta_detail::check_dist_and_prob(
  456. function,
  457. a, b, p,
  458. &result, Policy()))
  459. {
  460. return result;
  461. }
  462. // Special cases:
  463. if (p == 0)
  464. {
  465. return RealType(0);
  466. }
  467. if (p == 1)
  468. {
  469. return RealType(1);
  470. }
  471. return static_cast<RealType>(ibeta_inv(a, b, p, static_cast<RealType*>(nullptr), Policy()));
  472. } // quantile
  473. template <class RealType, class Policy>
  474. inline RealType quantile(const complemented2_type<beta_distribution<RealType, Policy>, RealType>& c)
  475. { // Complement Quantile or Percent Point beta function .
  476. // Return the number of expected x for a given
  477. // complement of the probability q.
  478. static const char* function = "boost::math::quantile(beta_distribution<%1%> const&, %1%)";
  479. //
  480. // Error checks:
  481. RealType q = c.param;
  482. const beta_distribution<RealType, Policy>& dist = c.dist;
  483. RealType result = 0;
  484. RealType a = dist.alpha();
  485. RealType b = dist.beta();
  486. if(false == beta_detail::check_dist_and_prob(
  487. function,
  488. a,
  489. b,
  490. q,
  491. &result, Policy()))
  492. {
  493. return result;
  494. }
  495. // Special cases:
  496. if(q == 1)
  497. {
  498. return RealType(0);
  499. }
  500. if(q == 0)
  501. {
  502. return RealType(1);
  503. }
  504. return static_cast<RealType>(ibetac_inv(a, b, q, static_cast<RealType*>(nullptr), Policy()));
  505. } // Quantile Complement
  506. } // namespace math
  507. } // namespace boost
  508. // This include must be at the end, *after* the accessors
  509. // for this distribution have been defined, in order to
  510. // keep compilers that support two-phase lookup happy.
  511. #include <boost/math/distributions/detail/derived_accessors.hpp>
  512. #if defined (BOOST_MSVC)
  513. # pragma warning(pop)
  514. #endif
  515. #endif // BOOST_MATH_DIST_BETA_HPP