beta.hpp 21 KB

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