beta.qbk 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. [section:beta_dist Beta Distribution]
  2. ``#include <boost/math/distributions/beta.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class beta_distribution;
  7. // typedef beta_distribution<double> beta;
  8. // Note that this is deliberately NOT provided,
  9. // to avoid a clash with the function name beta.
  10. template <class RealType, class ``__Policy``>
  11. class beta_distribution
  12. {
  13. public:
  14. typedef RealType value_type;
  15. typedef Policy policy_type;
  16. // Constructor from two shape parameters, alpha & beta:
  17. beta_distribution(RealType a, RealType b);
  18. // Parameter accessors:
  19. RealType alpha() const;
  20. RealType beta() const;
  21. // Parameter estimators of alpha or beta from mean and variance.
  22. static RealType find_alpha(
  23. RealType mean, // Expected value of mean.
  24. RealType variance); // Expected value of variance.
  25. static RealType find_beta(
  26. RealType mean, // Expected value of mean.
  27. RealType variance); // Expected value of variance.
  28. // Parameter estimators from
  29. // either alpha or beta, and x and probability.
  30. static RealType find_alpha(
  31. RealType beta, // from beta.
  32. RealType x, // x.
  33. RealType probability); // cdf
  34. static RealType find_beta(
  35. RealType alpha, // alpha.
  36. RealType x, // probability x.
  37. RealType probability); // probability cdf.
  38. };
  39. }} // namespaces
  40. The class type `beta_distribution` represents a
  41. [@http://en.wikipedia.org/wiki/Beta_distribution beta ]
  42. [@http://en.wikipedia.org/wiki/Probability_distribution probability distribution function].
  43. The [@http://mathworld.wolfram.com/BetaDistribution.htm beta distribution ]
  44. is used as a [@http://en.wikipedia.org/wiki/Prior_distribution prior distribution]
  45. for binomial proportions in
  46. [@http://mathworld.wolfram.com/BayesianAnalysis.html Bayesian analysis].
  47. See also:
  48. [@http://documents.wolfram.com/calculationcenter/v2/Functions/ListsMatrices/Statistics/BetaDistribution.html beta distribution]
  49. and [@http://en.wikipedia.org/wiki/Bayesian_statistics Bayesian statistics].
  50. How the beta distribution is used for
  51. [@http://home.uchicago.edu/~grynav/bayes/ABSLec5.ppt
  52. Bayesian analysis of one parameter models]
  53. is discussed by Jeff Grynaviski.
  54. The [@http://en.wikipedia.org/wiki/Probability_density_function probability density function PDF]
  55. for the [@http://en.wikipedia.org/wiki/Beta_distribution beta distribution]
  56. defined on the interval \[0,1\] is given by:
  57. [expression f(x;[alpha],[beta]) = x[super[alpha] - 1] (1 - x)[super[beta] -1] / B([alpha], [beta])]
  58. where [role serif_italic B([alpha], [beta])] is the
  59. [@http://en.wikipedia.org/wiki/Beta_function beta function],
  60. implemented in this library as __beta. Division by the beta function
  61. ensures that the pdf is normalized to the range zero to unity.
  62. The following graph illustrates examples of the pdf for various values
  63. of the shape parameters. Note the ['[alpha] = [beta] = 2] (blue line)
  64. is dome-shaped, and might be approximated by a symmetrical triangular
  65. distribution.
  66. [graph beta_pdf]
  67. If [alpha] = [beta] = 1, then it is a[emspace]
  68. [@http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29 uniform distribution],
  69. equal to unity in the entire interval x = 0 to 1.
  70. If [alpha] and [beta] are < 1, then the pdf is U-shaped.
  71. If [alpha] != [beta], then the shape is asymmetric
  72. and could be approximated by a triangle
  73. whose apex is away from the centre (where x = half).
  74. [h4 Member Functions]
  75. [h5 Constructor]
  76. beta_distribution(RealType alpha, RealType beta);
  77. Constructs a beta distribution with shape parameters /alpha/ and /beta/.
  78. Requires alpha,beta > 0,otherwise __domain_error is called. Note that
  79. technically the beta distribution is defined for alpha,beta >= 0, but
  80. it's not clear whether any program can actually make use of that latitude
  81. or how many of the non-member functions can be usefully defined in that case.
  82. Therefore for now, we regard it as an error if alpha or beta is zero.
  83. For example:
  84. beta_distribution<> mybeta(2, 5);
  85. Constructs a the beta distribution with alpha=2 and beta=5 (shown in yellow
  86. in the graph above).
  87. [h5 Parameter Accessors]
  88. RealType alpha() const;
  89. Returns the parameter /alpha/ from which this distribution was constructed.
  90. RealType beta() const;
  91. Returns the parameter /beta/ from which this distribution was constructed.
  92. So for example:
  93. beta_distribution<> mybeta(2, 5);
  94. assert(mybeta.alpha() == 2.); // mybeta.alpha() returns 2
  95. assert(mybeta.beta() == 5.); // mybeta.beta() returns 5
  96. [h4 Parameter Estimators]
  97. Two pairs of parameter estimators are provided.
  98. One estimates either [alpha] or [beta]
  99. from presumed-known mean and variance.
  100. The other pair estimates either [alpha] or [beta] from
  101. the cdf and x.
  102. It is also possible to estimate [alpha] and [beta] from
  103. 'known' mode & quantile. For example, calculators are provided by the
  104. [@http://www.ausvet.com.au/pprev/content.php?page=PPscript
  105. Pooled Prevalence Calculator] and
  106. [@http://www.epi.ucdavis.edu/diagnostictests/betabuster.html Beta Buster]
  107. but this is not yet implemented here.
  108. static RealType find_alpha(
  109. RealType mean, // Expected value of mean.
  110. RealType variance); // Expected value of variance.
  111. Returns the unique value of [alpha] that corresponds to a
  112. beta distribution with mean /mean/ and variance /variance/.
  113. static RealType find_beta(
  114. RealType mean, // Expected value of mean.
  115. RealType variance); // Expected value of variance.
  116. Returns the unique value of [beta] that corresponds to a
  117. beta distribution with mean /mean/ and variance /variance/.
  118. static RealType find_alpha(
  119. RealType beta, // from beta.
  120. RealType x, // x.
  121. RealType probability); // probability cdf
  122. Returns the value of [alpha] that gives:
  123. `cdf(beta_distribution<RealType>(alpha, beta), x) == probability`.
  124. static RealType find_beta(
  125. RealType alpha, // alpha.
  126. RealType x, // probability x.
  127. RealType probability); // probability cdf.
  128. Returns the value of [beta] that gives:
  129. `cdf(beta_distribution<RealType>(alpha, beta), x) == probability`.
  130. [h4 Non-member Accessor Functions]
  131. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
  132. that are generic to all distributions are supported: __usual_accessors.
  133. The formulae for calculating these are shown in the table below, and at
  134. [@http://mathworld.wolfram.com/BetaDistribution.html Wolfram Mathworld].
  135. [h4 Applications]
  136. The beta distribution can be used to model events constrained
  137. to take place within an interval defined by a minimum and maximum value:
  138. so it is used in project management systems.
  139. It is also widely used in [@http://en.wikipedia.org/wiki/Bayesian_inference Bayesian statistical inference].
  140. [h4 Related distributions]
  141. The beta distribution with both [alpha] and [beta] = 1 follows a
  142. [@http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29 uniform distribution].
  143. The [@http://en.wikipedia.org/wiki/Triangular_distribution triangular]
  144. is used when less precise information is available.
  145. The [@http://en.wikipedia.org/wiki/Binomial_distribution binomial distribution]
  146. is closely related when [alpha] and [beta] are integers.
  147. With integer values of [alpha] and [beta] the distribution B(i, j) is
  148. that of the j-th highest of a sample of i + j + 1 independent random variables
  149. uniformly distributed between 0 and 1.
  150. The cumulative probability from 0 to x is thus
  151. the probability that the j-th highest value is less than x.
  152. Or it is the probability that at least i of the random variables are less than x,
  153. a probability given by summing over the __binomial_distrib
  154. with its p parameter set to x.
  155. [h4 Accuracy]
  156. This distribution is implemented using the
  157. [link math_toolkit.sf_beta.beta_function beta functions] __beta and
  158. [link math_toolkit.sf_beta.ibeta_function incomplete beta functions] __ibeta and __ibetac;
  159. please refer to these functions for information on accuracy.
  160. [h4 Implementation]
  161. In the following table /a/ and /b/ are the parameters [alpha] and [beta],
  162. /x/ is the random variable, /p/ is the probability and /q = 1-p/.
  163. [table
  164. [[Function][Implementation Notes]]
  165. [[pdf][[role serif_italic f(x;[alpha],[beta]) = x[super[alpha] - 1] (1 - x)[super[beta] -1] / B([alpha], [beta])]
  166. Implemented using __ibeta_derivative(a, b, x).]]
  167. [[cdf][Using the incomplete beta function __ibeta(a, b, x)]]
  168. [[cdf complement][__ibetac(a, b, x)]]
  169. [[quantile][Using the inverse incomplete beta function __ibeta_inv(a, b, p)]]
  170. [[quantile from the complement][__ibetac_inv(a, b, q)]]
  171. [[mean][`a/(a+b)`]]
  172. [[variance][`a * b / (a+b)^2 * (a + b + 1)`]]
  173. [[mode][`(a-1) / (a + b - 2)`]]
  174. [[skewness][`2 (b-a) sqrt(a+b+1)/(a+b+2) * sqrt(a * b)`]]
  175. [[kurtosis excess][ [equation beta_dist_kurtosis] ]]
  176. [[kurtosis][`kurtosis + 3`]]
  177. [[parameter estimation][ ]]
  178. [[alpha (from mean and variance)][`mean * (( (mean * (1 - mean)) / variance)- 1)`]]
  179. [[beta (from mean and variance)][`(1 - mean) * (((mean * (1 - mean)) /variance)-1)`]]
  180. [[The member functions `find_alpha` and `find_beta`
  181. from cdf and probability x
  182. and *either* `alpha` or `beta`]
  183. [Implemented in terms of the inverse incomplete beta functions
  184. __ibeta_inva, and __ibeta_invb respectively.]]
  185. [[`find_alpha`][`ibeta_inva(beta, x, probability)`]]
  186. [[`find_beta`][`ibeta_invb(alpha, x, probability)`]]
  187. ] [/table]
  188. [h4 References]
  189. [@http://en.wikipedia.org/wiki/Beta_distribution Wikipedia Beta distribution]
  190. [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda366h.htm NIST Exploratory Data Analysis]
  191. [@http://mathworld.wolfram.com/BetaDistribution.html Wolfram MathWorld]
  192. [endsect] [/section:beta_dist beta]
  193. [/ beta.qbk
  194. Copyright 2006 John Maddock and Paul A. Bristow.
  195. Distributed under the Boost Software License, Version 1.0.
  196. (See accompanying file LICENSE_1_0.txt or copy at
  197. http://www.boost.org/LICENSE_1_0.txt).
  198. ]