123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- #ifndef BOOST_MATH_SPECIAL_BERNOULLI_HPP
- #define BOOST_MATH_SPECIAL_BERNOULLI_HPP
- #include <boost/math/distributions/fwd.hpp>
- #include <boost/math/tools/config.hpp>
- #include <boost/math/distributions/complement.hpp>
- #include <boost/math/distributions/detail/common_error_handling.hpp>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include <utility>
- namespace boost
- {
- namespace math
- {
- namespace bernoulli_detail
- {
-
- template <class RealType, class Policy>
- inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& )
- {
- if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
- {
- *result = policies::raise_domain_error<RealType>(
- function,
- "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, Policy());
- return false;
- }
- return true;
- }
- template <class RealType, class Policy>
- inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& , const std::true_type&)
- {
- return check_success_fraction(function, p, result, Policy());
- }
- template <class RealType, class Policy>
- inline bool check_dist(const char* , const RealType& , RealType* , const Policy& , const std::false_type&)
- {
- return true;
- }
- template <class RealType, class Policy>
- inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& )
- {
- return check_dist(function, p, result, Policy(), typename policies::constructor_error_check<Policy>::type());
- }
- template <class RealType, class Policy>
- inline bool check_dist_and_k(const char* function, const RealType& p, RealType k, RealType* result, const Policy& pol)
- {
- if(check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) == false)
- {
- return false;
- }
- if(!(boost::math::isfinite)(k) || !((k == 0) || (k == 1)))
- {
- *result = policies::raise_domain_error<RealType>(
- function,
- "Number of successes argument is %1%, but must be 0 or 1 !", k, pol);
- return false;
- }
- return true;
- }
- template <class RealType, class Policy>
- inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& )
- {
- if((check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) && detail::check_probability(function, prob, result, Policy())) == false)
- {
- return false;
- }
- return true;
- }
- }
- template <class RealType = double, class Policy = policies::policy<> >
- class bernoulli_distribution
- {
- public:
- typedef RealType value_type;
- typedef Policy policy_type;
- bernoulli_distribution(RealType p = 0.5) : m_p(p)
- {
-
- RealType result;
- bernoulli_detail::check_dist(
- "boost::math::bernoulli_distribution<%1%>::bernoulli_distribution",
- m_p,
- &result, Policy());
- }
- RealType success_fraction() const
- {
- return m_p;
- }
- private:
- RealType m_p;
- };
- typedef bernoulli_distribution<double> bernoulli;
- #ifdef __cpp_deduction_guides
- template <class RealType>
- bernoulli_distribution(RealType)->bernoulli_distribution<typename boost::math::tools::promote_args<RealType>::type>;
- #endif
- template <class RealType, class Policy>
- inline const std::pair<RealType, RealType> range(const bernoulli_distribution<RealType, Policy>& )
- {
- using boost::math::tools::max_value;
- return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
- }
- template <class RealType, class Policy>
- inline const std::pair<RealType, RealType> support(const bernoulli_distribution<RealType, Policy>& )
- {
-
- return std::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
- }
- template <class RealType, class Policy>
- inline RealType mean(const bernoulli_distribution<RealType, Policy>& dist)
- {
- return dist.success_fraction();
- }
-
-
-
-
-
-
- template <class RealType, class Policy>
- inline RealType variance(const bernoulli_distribution<RealType, Policy>& dist)
- {
- return dist.success_fraction() * (1 - dist.success_fraction());
- }
- template <class RealType, class Policy>
- RealType pdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
- {
- BOOST_FPU_EXCEPTION_GUARD
-
- RealType result = 0;
- if(false == bernoulli_detail::check_dist_and_k(
- "boost::math::pdf(bernoulli_distribution<%1%>, %1%)",
- dist.success_fraction(),
- k,
- &result, Policy()))
- {
- return result;
- }
-
- if (k == 0)
- {
- return 1 - dist.success_fraction();
- }
- else
- {
- return dist.success_fraction();
- }
- }
- template <class RealType, class Policy>
- inline RealType cdf(const bernoulli_distribution<RealType, Policy>& dist, const RealType& k)
- {
- RealType p = dist.success_fraction();
-
- RealType result = 0;
- if(false == bernoulli_detail::check_dist_and_k(
- "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
- p,
- k,
- &result, Policy()))
- {
- return result;
- }
- if (k == 0)
- {
- return 1 - p;
- }
- else
- {
- return 1;
- }
- }
- template <class RealType, class Policy>
- inline RealType cdf(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
- {
- RealType const& k = c.param;
- bernoulli_distribution<RealType, Policy> const& dist = c.dist;
- RealType p = dist.success_fraction();
-
- RealType result = 0;
- if(false == bernoulli_detail::check_dist_and_k(
- "boost::math::cdf(bernoulli_distribution<%1%>, %1%)",
- p,
- k,
- &result, Policy()))
- {
- return result;
- }
- if (k == 0)
- {
- return p;
- }
- else
- {
- return 0;
- }
- }
- template <class RealType, class Policy>
- inline RealType quantile(const bernoulli_distribution<RealType, Policy>& dist, const RealType& p)
- {
-
-
- RealType result = 0;
- if(false == bernoulli_detail::check_dist_and_prob(
- "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
- dist.success_fraction(),
- p,
- &result, Policy()))
- {
- return result;
- }
- if (p <= (1 - dist.success_fraction()))
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- template <class RealType, class Policy>
- inline RealType quantile(const complemented2_type<bernoulli_distribution<RealType, Policy>, RealType>& c)
- {
-
-
-
-
- RealType q = c.param;
- const bernoulli_distribution<RealType, Policy>& dist = c.dist;
- RealType result = 0;
- if(false == bernoulli_detail::check_dist_and_prob(
- "boost::math::quantile(bernoulli_distribution<%1%>, %1%)",
- dist.success_fraction(),
- q,
- &result, Policy()))
- {
- return result;
- }
- if (q <= 1 - dist.success_fraction())
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- template <class RealType, class Policy>
- inline RealType mode(const bernoulli_distribution<RealType, Policy>& dist)
- {
- return static_cast<RealType>((dist.success_fraction() <= 0.5) ? 0 : 1);
- }
- template <class RealType, class Policy>
- inline RealType skewness(const bernoulli_distribution<RealType, Policy>& dist)
- {
- BOOST_MATH_STD_USING;
- RealType p = dist.success_fraction();
- return (1 - 2 * p) / sqrt(p * (1 - p));
- }
- template <class RealType, class Policy>
- inline RealType kurtosis_excess(const bernoulli_distribution<RealType, Policy>& dist)
- {
- RealType p = dist.success_fraction();
-
-
-
-
- return 1 / (1 - p) + 1/p -6;
- }
- template <class RealType, class Policy>
- inline RealType kurtosis(const bernoulli_distribution<RealType, Policy>& dist)
- {
- RealType p = dist.success_fraction();
- return 1 / (1 - p) + 1/p -6 + 3;
-
-
- }
- }
- }
- #include <boost/math/distributions/detail/derived_accessors.hpp>
- #endif
|