// Copyright 2008 Gautam Sewani // Copyright 2008 John Maddock // // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MATH_DISTRIBUTIONS_HYPERGEOMETRIC_HPP #define BOOST_MATH_DISTRIBUTIONS_HYPERGEOMETRIC_HPP #include #include #include #include #include #include namespace boost { namespace math { template > class hypergeometric_distribution { public: typedef RealType value_type; typedef Policy policy_type; hypergeometric_distribution(unsigned r, unsigned n, unsigned N) // Constructor. : m_n(n), m_N(N), m_r(r) { static const char* function = "boost::math::hypergeometric_distribution<%1%>::hypergeometric_distribution"; RealType ret; check_params(function, &ret); } // Accessor functions. unsigned total()const { return m_N; } unsigned defective()const { return m_n; } unsigned sample_count()const { return m_r; } bool check_params(const char* function, RealType* result)const { if(m_r > m_N) { *result = boost::math::policies::raise_domain_error( function, "Parameter r out of range: must be <= N but got %1%", static_cast(m_r), Policy()); return false; } if(m_n > m_N) { *result = boost::math::policies::raise_domain_error( function, "Parameter n out of range: must be <= N but got %1%", static_cast(m_n), Policy()); return false; } return true; } bool check_x(unsigned x, const char* function, RealType* result)const { if(x < static_cast((std::max)(0, (int)(m_n + m_r) - (int)(m_N)))) { *result = boost::math::policies::raise_domain_error( function, "Random variable out of range: must be > 0 and > m + r - N but got %1%", static_cast(x), Policy()); return false; } if(x > (std::min)(m_r, m_n)) { *result = boost::math::policies::raise_domain_error( function, "Random variable out of range: must be less than both n and r but got %1%", static_cast(x), Policy()); return false; } return true; } private: // Data members: unsigned m_n; // number of "defective" items unsigned m_N; // number of "total" items unsigned m_r; // number of items picked }; // class hypergeometric_distribution typedef hypergeometric_distribution hypergeometric; template inline const std::pair range(const hypergeometric_distribution& dist) { // Range of permissible values for random variable x. #ifdef BOOST_MSVC # pragma warning(push) # pragma warning(disable:4267) #endif unsigned r = dist.sample_count(); unsigned n = dist.defective(); unsigned N = dist.total(); unsigned l = static_cast((std::max)(0, (int)(n + r) - (int)(N))); unsigned u = (std::min)(r, n); return std::pair(l, u); #ifdef BOOST_MSVC # pragma warning(pop) #endif } template inline const std::pair support(const hypergeometric_distribution& d) { return range(d); } template inline RealType pdf(const hypergeometric_distribution& dist, const unsigned& x) { static const char* function = "boost::math::pdf(const hypergeometric_distribution<%1%>&, const %1%&)"; RealType result = 0; if(!dist.check_params(function, &result)) return result; if(!dist.check_x(x, function, &result)) return result; return boost::math::detail::hypergeometric_pdf( x, dist.sample_count(), dist.defective(), dist.total(), Policy()); } template inline RealType pdf(const hypergeometric_distribution& dist, const U& x) { BOOST_MATH_STD_USING static const char* function = "boost::math::pdf(const hypergeometric_distribution<%1%>&, const %1%&)"; RealType r = static_cast(x); unsigned u = itrunc(r, typename policies::normalise >::type()); if(u != r) { return boost::math::policies::raise_domain_error( function, "Random variable out of range: must be an integer but got %1%", r, Policy()); } return pdf(dist, u); } template inline RealType cdf(const hypergeometric_distribution& dist, const unsigned& x) { static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)"; RealType result = 0; if(!dist.check_params(function, &result)) return result; if(!dist.check_x(x, function, &result)) return result; return boost::math::detail::hypergeometric_cdf( x, dist.sample_count(), dist.defective(), dist.total(), false, Policy()); } template inline RealType cdf(const hypergeometric_distribution& dist, const U& x) { BOOST_MATH_STD_USING static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)"; RealType r = static_cast(x); unsigned u = itrunc(r, typename policies::normalise >::type()); if(u != r) { return boost::math::policies::raise_domain_error( function, "Random variable out of range: must be an integer but got %1%", r, Policy()); } return cdf(dist, u); } template inline RealType cdf(const complemented2_type, unsigned>& c) { static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)"; RealType result = 0; if(!c.dist.check_params(function, &result)) return result; if(!c.dist.check_x(c.param, function, &result)) return result; return boost::math::detail::hypergeometric_cdf( c.param, c.dist.sample_count(), c.dist.defective(), c.dist.total(), true, Policy()); } template inline RealType cdf(const complemented2_type, U>& c) { BOOST_MATH_STD_USING static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)"; RealType r = static_cast(c.param); unsigned u = itrunc(r, typename policies::normalise >::type()); if(u != r) { return boost::math::policies::raise_domain_error( function, "Random variable out of range: must be an integer but got %1%", r, Policy()); } return cdf(complement(c.dist, u)); } template inline RealType quantile(const hypergeometric_distribution& dist, const RealType& p) { BOOST_MATH_STD_USING // for ADL of std functions // Checking function argument RealType result = 0; const char* function = "boost::math::quantile(const hypergeometric_distribution<%1%>&, %1%)"; if (false == dist.check_params(function, &result)) return result; if(false == detail::check_probability(function, p, &result, Policy())) return result; return static_cast(detail::hypergeometric_quantile(p, RealType(1 - p), dist.sample_count(), dist.defective(), dist.total(), Policy())); } // quantile template inline RealType quantile(const complemented2_type, RealType>& c) { BOOST_MATH_STD_USING // for ADL of std functions // Checking function argument RealType result = 0; const char* function = "quantile(const complemented2_type, %1%>&)"; if (false == c.dist.check_params(function, &result)) return result; if(false == detail::check_probability(function, c.param, &result, Policy())) return result; return static_cast(detail::hypergeometric_quantile(RealType(1 - c.param), c.param, c.dist.sample_count(), c.dist.defective(), c.dist.total(), Policy())); } // quantile template inline RealType mean(const hypergeometric_distribution& dist) { return static_cast(dist.sample_count() * dist.defective()) / dist.total(); } // RealType mean(const hypergeometric_distribution& dist) template inline RealType variance(const hypergeometric_distribution& dist) { RealType r = static_cast(dist.sample_count()); RealType n = static_cast(dist.defective()); RealType N = static_cast(dist.total()); return r * (n / N) * (1 - n / N) * (N - r) / (N - 1); } // RealType variance(const hypergeometric_distribution& dist) template inline RealType mode(const hypergeometric_distribution& dist) { BOOST_MATH_STD_USING RealType r = static_cast(dist.sample_count()); RealType n = static_cast(dist.defective()); RealType N = static_cast(dist.total()); return floor((r + 1) * (n + 1) / (N + 2)); } template inline RealType skewness(const hypergeometric_distribution& dist) { BOOST_MATH_STD_USING RealType r = static_cast(dist.sample_count()); RealType n = static_cast(dist.defective()); RealType N = static_cast(dist.total()); return (N - 2 * n) * sqrt(N - 1) * (N - 2 * r) / (sqrt(n * r * (N - n) * (N - r)) * (N - 2)); } // RealType skewness(const hypergeometric_distribution& dist) template inline RealType kurtosis_excess(const hypergeometric_distribution& dist) { RealType r = static_cast(dist.sample_count()); RealType n = static_cast(dist.defective()); RealType N = static_cast(dist.total()); RealType t1 = N * N * (N - 1) / (r * (N - 2) * (N - 3) * (N - r)); RealType t2 = (N * (N + 1) - 6 * N * (N - r)) / (n * (N - n)) + 3 * r * (N - r) * (N + 6) / (N * N) - 6; return t1 * t2; } // RealType kurtosis_excess(const hypergeometric_distribution& dist) template inline RealType kurtosis(const hypergeometric_distribution& dist) { return kurtosis_excess(dist) + 3; } // RealType kurtosis_excess(const hypergeometric_distribution& dist) }} // namespaces // This include must be at the end, *after* the accessors // for this distribution have been defined, in order to // keep compilers that support two-phase lookup happy. #include #endif // include guard