123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- #ifndef BOOST_MATH_SPECIAL_BINOMIAL_HPP
- #define BOOST_MATH_SPECIAL_BINOMIAL_HPP
- #include <boost/math/distributions/fwd.hpp>
- #include <boost/math/special_functions/beta.hpp>
- #include <boost/math/distributions/complement.hpp>
- #include <boost/math/distributions/detail/common_error_handling.hpp>
- #include <boost/math/distributions/detail/inv_discrete_quantile.hpp>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include <boost/math/tools/roots.hpp>
- #include <utility>
- namespace boost
- {
- namespace math
- {
- template <class RealType, class Policy>
- class binomial_distribution;
- namespace binomial_detail{
-
- template <class RealType, class Policy>
- inline bool check_N(const char* function, const RealType& N, RealType* result, const Policy& pol)
- {
- if((N < 0) || !(boost::math::isfinite)(N))
- {
- *result = policies::raise_domain_error<RealType>(
- function,
- "Number of Trials argument is %1%, but must be >= 0 !", N, pol);
- return false;
- }
- return true;
- }
- template <class RealType, class Policy>
- inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& pol)
- {
- if((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
- {
- *result = policies::raise_domain_error<RealType>(
- function,
- "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, pol);
- return false;
- }
- return true;
- }
- template <class RealType, class Policy>
- inline bool check_dist(const char* function, const RealType& N, const RealType& p, RealType* result, const Policy& pol)
- {
- return check_success_fraction(
- function, p, result, pol)
- && check_N(
- function, N, result, pol);
- }
- template <class RealType, class Policy>
- inline bool check_dist_and_k(const char* function, const RealType& N, const RealType& p, RealType k, RealType* result, const Policy& pol)
- {
- if(check_dist(function, N, p, result, pol) == false)
- return false;
- if((k < 0) || !(boost::math::isfinite)(k))
- {
- *result = policies::raise_domain_error<RealType>(
- function,
- "Number of Successes argument is %1%, but must be >= 0 !", k, pol);
- return false;
- }
- if(k > N)
- {
- *result = policies::raise_domain_error<RealType>(
- function,
- "Number of Successes argument is %1%, but must be <= Number of Trials !", k, pol);
- return false;
- }
- return true;
- }
- template <class RealType, class Policy>
- inline bool check_dist_and_prob(const char* function, const RealType& N, RealType p, RealType prob, RealType* result, const Policy& pol)
- {
- if((check_dist(function, N, p, result, pol) && detail::check_probability(function, prob, result, pol)) == false)
- return false;
- return true;
- }
- template <class T, class Policy>
- T inverse_binomial_cornish_fisher(T n, T sf, T p, T q, const Policy& pol)
- {
- BOOST_MATH_STD_USING
-
- T m = n * sf;
-
- T sigma = sqrt(n * sf * (1 - sf));
-
- T sk = (1 - 2 * sf) / sigma;
-
-
-
- T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
-
- if(p < 0.5)
- x = -x;
- T x2 = x * x;
-
- T w = x + sk * (x2 - 1) / 6;
-
- w = m + sigma * w;
- if(w < tools::min_value<T>())
- return sqrt(tools::min_value<T>());
- if(w > n)
- return n;
- return w;
- }
- template <class RealType, class Policy>
- RealType quantile_imp(const binomial_distribution<RealType, Policy>& dist, const RealType& p, const RealType& q, bool comp)
- {
-
-
-
-
- BOOST_MATH_STD_USING
- RealType result = 0;
- RealType trials = dist.trials();
- RealType success_fraction = dist.success_fraction();
- if(false == binomial_detail::check_dist_and_prob(
- "boost::math::quantile(binomial_distribution<%1%> const&, %1%)",
- trials,
- success_fraction,
- p,
- &result, Policy()))
- {
- return result;
- }
-
-
- if(p == 0)
- {
-
-
- return 0;
- }
- if(p == 1 || success_fraction == 1)
- {
-
- return trials;
- }
- if (p <= pow(1 - success_fraction, trials))
- {
- return 0;
- }
-
-
- RealType guess = binomial_detail::inverse_binomial_cornish_fisher(trials, success_fraction, p, q, Policy());
- RealType factor = 8;
- if(trials > 100)
- factor = 1.01f;
- else if((trials > 10) && (trials - 1 > guess) && (guess > 3))
- factor = 1.15f;
- else if(trials < 10)
- {
-
- if(guess > trials / 64)
- {
- guess = trials / 4;
- factor = 2;
- }
- else
- guess = trials / 1024;
- }
- else
- factor = 2;
- typedef typename Policy::discrete_quantile_type discrete_quantile_type;
- std::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
- result = detail::inverse_discrete_quantile(
- dist,
- comp ? q : p,
- comp,
- guess,
- factor,
- RealType(1),
- discrete_quantile_type(),
- max_iter);
- return result;
- }
- }
- template <class RealType = double, class Policy = policies::policy<> >
- class binomial_distribution
- {
- public:
- typedef RealType value_type;
- typedef Policy policy_type;
- binomial_distribution(RealType n = 1, RealType p = 0.5) : m_n(n), m_p(p)
- {
-
- RealType r;
- binomial_detail::check_dist(
- "boost::math::binomial_distribution<%1%>::binomial_distribution",
- m_n,
- m_p,
- &r, Policy());
- }
- RealType success_fraction() const
- {
- return m_p;
- }
- RealType trials() const
- {
- return m_n;
- }
- enum interval_type{
- clopper_pearson_exact_interval,
- jeffreys_prior_interval
- };
-
-
-
-
-
-
- static RealType find_lower_bound_on_p(
- RealType trials,
- RealType successes,
- RealType probability,
- interval_type t = clopper_pearson_exact_interval)
- {
- static const char* function = "boost::math::binomial_distribution<%1%>::find_lower_bound_on_p";
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- function, trials, RealType(0), successes, &result, Policy())
- &&
- binomial_detail::check_dist_and_prob(
- function, trials, RealType(0), probability, &result, Policy()))
- { return result; }
- if(successes == 0)
- return 0;
-
-
-
- return (t == clopper_pearson_exact_interval) ? ibeta_inv(successes, trials - successes + 1, probability, static_cast<RealType*>(nullptr), Policy())
- : ibeta_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(nullptr), Policy());
- }
- static RealType find_upper_bound_on_p(
- RealType trials,
- RealType successes,
- RealType probability,
- interval_type t = clopper_pearson_exact_interval)
- {
- static const char* function = "boost::math::binomial_distribution<%1%>::find_upper_bound_on_p";
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- function, trials, RealType(0), successes, &result, Policy())
- &&
- binomial_detail::check_dist_and_prob(
- function, trials, RealType(0), probability, &result, Policy()))
- { return result; }
- if(trials == successes)
- return 1;
- return (t == clopper_pearson_exact_interval) ? ibetac_inv(successes + 1, trials - successes, probability, static_cast<RealType*>(nullptr), Policy())
- : ibetac_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(nullptr), Policy());
- }
-
-
-
-
-
-
- static RealType find_minimum_number_of_trials(
- RealType k,
- RealType p,
- RealType alpha)
- {
- static const char* function = "boost::math::binomial_distribution<%1%>::find_minimum_number_of_trials";
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- function, k, p, k, &result, Policy())
- &&
- binomial_detail::check_dist_and_prob(
- function, k, p, alpha, &result, Policy()))
- { return result; }
- result = ibetac_invb(k + 1, p, alpha, Policy());
- return result + k;
- }
- static RealType find_maximum_number_of_trials(
- RealType k,
- RealType p,
- RealType alpha)
- {
- static const char* function = "boost::math::binomial_distribution<%1%>::find_maximum_number_of_trials";
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- function, k, p, k, &result, Policy())
- &&
- binomial_detail::check_dist_and_prob(
- function, k, p, alpha, &result, Policy()))
- { return result; }
- result = ibeta_invb(k + 1, p, alpha, Policy());
- return result + k;
- }
- private:
- RealType m_n;
- RealType m_p;
- };
- typedef binomial_distribution<> binomial;
-
-
-
- #ifdef __cpp_deduction_guides
- template <class RealType>
- binomial_distribution(RealType)->binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- template <class RealType>
- binomial_distribution(RealType,RealType)->binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- #endif
- template <class RealType, class Policy>
- const std::pair<RealType, RealType> range(const binomial_distribution<RealType, Policy>& dist)
- {
- using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
- }
- template <class RealType, class Policy>
- const std::pair<RealType, RealType> support(const binomial_distribution<RealType, Policy>& dist)
- {
-
- return std::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
- }
- template <class RealType, class Policy>
- inline RealType mean(const binomial_distribution<RealType, Policy>& dist)
- {
- return dist.trials() * dist.success_fraction();
- }
- template <class RealType, class Policy>
- inline RealType variance(const binomial_distribution<RealType, Policy>& dist)
- {
- return dist.trials() * dist.success_fraction() * (1 - dist.success_fraction());
- }
- template <class RealType, class Policy>
- RealType pdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
- {
- BOOST_FPU_EXCEPTION_GUARD
- BOOST_MATH_STD_USING
- RealType n = dist.trials();
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- "boost::math::pdf(binomial_distribution<%1%> const&, %1%)",
- n,
- dist.success_fraction(),
- k,
- &result, Policy()))
- {
- return result;
- }
-
- if (dist.success_fraction() == 0)
- {
- return static_cast<RealType>(k == 0 ? 1 : 0);
- }
- if (dist.success_fraction() == 1)
- {
- return static_cast<RealType>(k == n ? 1 : 0);
- }
-
-
- if (n == 0)
- {
- return 1;
- }
- if (k == n)
- {
-
- return pow(dist.success_fraction(), k);
- }
-
-
-
-
-
-
-
-
-
- using boost::math::ibeta_derivative;
- return ibeta_derivative(k+1, n-k+1, dist.success_fraction(), Policy()) / (n+1);
- }
- template <class RealType, class Policy>
- inline RealType cdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- BOOST_MATH_STD_USING
- RealType n = dist.trials();
- RealType p = dist.success_fraction();
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- "boost::math::cdf(binomial_distribution<%1%> const&, %1%)",
- n,
- p,
- k,
- &result, Policy()))
- {
- return result;
- }
- if (k == n)
- {
- return 1;
- }
-
- if (p == 0)
- {
-
-
-
-
-
- return 1;
- }
- if (p == 1)
- {
-
-
-
- return 0;
- }
-
-
-
-
-
-
-
-
-
-
- return ibetac(k + 1, n - k, p, Policy());
- }
- template <class RealType, class Policy>
- inline RealType cdf(const complemented2_type<binomial_distribution<RealType, Policy>, RealType>& c)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- BOOST_MATH_STD_USING
- RealType const& k = c.param;
- binomial_distribution<RealType, Policy> const& dist = c.dist;
- RealType n = dist.trials();
- RealType p = dist.success_fraction();
-
- RealType result = 0;
- if(false == binomial_detail::check_dist_and_k(
- "boost::math::cdf(binomial_distribution<%1%> const&, %1%)",
- n,
- p,
- k,
- &result, Policy()))
- {
- return result;
- }
- if (k == n)
- {
- return 0;
- }
-
- if (p == 0)
- {
-
-
-
-
-
- return 0;
- }
- if (p == 1)
- {
-
-
-
-
- return 1;
- }
-
-
-
-
-
-
-
-
-
-
-
- return ibeta(k + 1, n - k, p, Policy());
- }
- template <class RealType, class Policy>
- inline RealType quantile(const binomial_distribution<RealType, Policy>& dist, const RealType& p)
- {
- return binomial_detail::quantile_imp(dist, p, RealType(1-p), false);
- }
- template <class RealType, class Policy>
- RealType quantile(const complemented2_type<binomial_distribution<RealType, Policy>, RealType>& c)
- {
- return binomial_detail::quantile_imp(c.dist, RealType(1-c.param), c.param, true);
- }
- template <class RealType, class Policy>
- inline RealType mode(const binomial_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING
- RealType p = dist.success_fraction();
- RealType n = dist.trials();
- return floor(p * (n + 1));
- }
- template <class RealType, class Policy>
- inline RealType median(const binomial_distribution<RealType, Policy>& dist)
- {
-
-
-
-
-
-
-
- BOOST_MATH_STD_USING
- RealType p = dist.success_fraction();
- RealType n = dist.trials();
-
- return floor(p * n);
- }
- template <class RealType, class Policy>
- inline RealType skewness(const binomial_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING
- RealType p = dist.success_fraction();
- RealType n = dist.trials();
- return (1 - 2 * p) / sqrt(n * p * (1 - p));
- }
- template <class RealType, class Policy>
- inline RealType kurtosis(const binomial_distribution<RealType, Policy>& dist)
- {
- RealType p = dist.success_fraction();
- RealType n = dist.trials();
- return 3 - 6 / n + 1 / (n * p * (1 - p));
- }
- template <class RealType, class Policy>
- inline RealType kurtosis_excess(const binomial_distribution<RealType, Policy>& dist)
- {
- RealType p = dist.success_fraction();
- RealType q = 1 - p;
- RealType n = dist.trials();
- return (1 - 6 * p * q) / (n * p * q);
- }
- }
- }
- #include <boost/math/distributions/detail/derived_accessors.hpp>
- #endif
|