/usr/include/boost/math/distributions/hypergeometric.hpp is in libboost1.46-dev 1.46.1-7ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | // 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 <boost/math/distributions/detail/common_error_handling.hpp>
#include <boost/math/distributions/complement.hpp>
#include <boost/math/distributions/detail/hypergeometric_pdf.hpp>
#include <boost/math/distributions/detail/hypergeometric_cdf.hpp>
#include <boost/math/distributions/detail/hypergeometric_quantile.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
namespace boost { namespace math {
template <class RealType = double, class Policy = policies::policy<> >
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<RealType>(
function, "Parameter r out of range: must be <= N but got %1%", static_cast<RealType>(m_r), Policy());
return false;
}
if(m_n > m_N)
{
*result = boost::math::policies::raise_domain_error<RealType>(
function, "Parameter n out of range: must be <= N but got %1%", static_cast<RealType>(m_n), Policy());
return false;
}
return true;
}
bool check_x(unsigned x, const char* function, RealType* result)const
{
if(x < static_cast<unsigned>((std::max)(0, (int)(m_n + m_r) - (int)(m_N))))
{
*result = boost::math::policies::raise_domain_error<RealType>(
function, "Random variable out of range: must be > 0 and > m + r - N but got %1%", static_cast<RealType>(x), Policy());
return false;
}
if(x > (std::min)(m_r, m_n))
{
*result = boost::math::policies::raise_domain_error<RealType>(
function, "Random variable out of range: must be less than both n and r but got %1%", static_cast<RealType>(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<double> hypergeometric;
template <class RealType, class Policy>
inline const std::pair<unsigned, unsigned> range(const hypergeometric_distribution<RealType, Policy>& 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<unsigned>((std::max)(0, (int)(n + r) - (int)(N)));
unsigned u = (std::min)(r, n);
return std::pair<unsigned, unsigned>(l, u);
#ifdef BOOST_MSVC
# pragma warning(pop)
#endif
}
template <class RealType, class Policy>
inline const std::pair<unsigned, unsigned> support(const hypergeometric_distribution<RealType, Policy>& d)
{
return range(d);
}
template <class RealType, class Policy>
inline RealType pdf(const hypergeometric_distribution<RealType, Policy>& dist, const unsigned& x)
{
static const char* function = "boost::math::pdf(const hypergeometric_distribution<%1%>&, const %1%&)";
RealType result;
if(!dist.check_params(function, &result))
return result;
if(!dist.check_x(x, function, &result))
return result;
return boost::math::detail::hypergeometric_pdf<RealType>(
x, dist.sample_count(), dist.defective(), dist.total(), Policy());
}
template <class RealType, class Policy, class U>
inline RealType pdf(const hypergeometric_distribution<RealType, Policy>& dist, const U& x)
{
static const char* function = "boost::math::pdf(const hypergeometric_distribution<%1%>&, const %1%&)";
RealType r = static_cast<RealType>(x);
unsigned u = boost::math::itrunc(r);
if(u != r)
{
return boost::math::policies::raise_domain_error<RealType>(
function, "Random variable out of range: must be an integer but got %1%", r, Policy());
}
return pdf(dist, u);
}
template <class RealType, class Policy>
inline RealType cdf(const hypergeometric_distribution<RealType, Policy>& dist, const unsigned& x)
{
static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)";
RealType result;
if(!dist.check_params(function, &result))
return result;
if(!dist.check_x(x, function, &result))
return result;
return boost::math::detail::hypergeometric_cdf<RealType>(
x, dist.sample_count(), dist.defective(), dist.total(), false, Policy());
}
template <class RealType, class Policy, class U>
inline RealType cdf(const hypergeometric_distribution<RealType, Policy>& dist, const U& x)
{
static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)";
RealType r = static_cast<RealType>(x);
unsigned u = boost::math::itrunc(r);
if(u != r)
{
return boost::math::policies::raise_domain_error<RealType>(
function, "Random variable out of range: must be an integer but got %1%", r, Policy());
}
return cdf(dist, u);
}
template <class RealType, class Policy>
inline RealType cdf(const complemented2_type<hypergeometric_distribution<RealType, Policy>, unsigned>& c)
{
static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)";
RealType result;
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<RealType>(
c.param, c.dist.sample_count(), c.dist.defective(), c.dist.total(), true, Policy());
}
template <class RealType, class Policy, class U>
inline RealType cdf(const complemented2_type<hypergeometric_distribution<RealType, Policy>, U>& c)
{
static const char* function = "boost::math::cdf(const hypergeometric_distribution<%1%>&, const %1%&)";
RealType r = static_cast<RealType>(c.param);
unsigned u = boost::math::itrunc(r);
if(u != r)
{
return boost::math::policies::raise_domain_error<RealType>(
function, "Random variable out of range: must be an integer but got %1%", r, Policy());
}
return cdf(complement(c.dist, u));
}
template <class RealType, class Policy>
inline RealType quantile(const hypergeometric_distribution<RealType, Policy>& dist, const RealType& p)
{
BOOST_MATH_STD_USING // for ADL of std functions
// Checking function argument
RealType result;
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<RealType>(detail::hypergeometric_quantile(p, RealType(1 - p), dist.sample_count(), dist.defective(), dist.total(), Policy()));
} // quantile
template <class RealType, class Policy>
inline RealType quantile(const complemented2_type<hypergeometric_distribution<RealType, Policy>, RealType>& c)
{
BOOST_MATH_STD_USING // for ADL of std functions
// Checking function argument
RealType result;
const char* function = "quantile(const complemented2_type<hypergeometric_distribution<%1%>, %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<RealType>(detail::hypergeometric_quantile(RealType(1 - c.param), c.param, c.dist.sample_count(), c.dist.defective(), c.dist.total(), Policy()));
} // quantile
template <class RealType, class Policy>
inline RealType mean(const hypergeometric_distribution<RealType, Policy>& dist)
{
return static_cast<RealType>(dist.sample_count() * dist.defective()) / dist.total();
} // RealType mean(const hypergeometric_distribution<RealType, Policy>& dist)
template <class RealType, class Policy>
inline RealType variance(const hypergeometric_distribution<RealType, Policy>& dist)
{
RealType r = static_cast<RealType>(dist.sample_count());
RealType n = static_cast<RealType>(dist.defective());
RealType N = static_cast<RealType>(dist.total());
return r * (n / N) * (1 - n / N) * (N - r) / (N - 1);
} // RealType variance(const hypergeometric_distribution<RealType, Policy>& dist)
template <class RealType, class Policy>
inline RealType mode(const hypergeometric_distribution<RealType, Policy>& dist)
{
BOOST_MATH_STD_USING
RealType r = static_cast<RealType>(dist.sample_count());
RealType n = static_cast<RealType>(dist.defective());
RealType N = static_cast<RealType>(dist.total());
return floor((r + 1) * (n + 1) / (N + 2));
}
template <class RealType, class Policy>
inline RealType skewness(const hypergeometric_distribution<RealType, Policy>& dist)
{
BOOST_MATH_STD_USING
RealType r = static_cast<RealType>(dist.sample_count());
RealType n = static_cast<RealType>(dist.defective());
RealType N = static_cast<RealType>(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<RealType, Policy>& dist)
template <class RealType, class Policy>
inline RealType kurtosis_excess(const hypergeometric_distribution<RealType, Policy>& dist)
{
RealType r = static_cast<RealType>(dist.sample_count());
RealType n = static_cast<RealType>(dist.defective());
RealType N = static_cast<RealType>(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<RealType, Policy>& dist)
template <class RealType, class Policy>
inline RealType kurtosis(const hypergeometric_distribution<RealType, Policy>& dist)
{
return kurtosis_excess(dist) + 3;
} // RealType kurtosis_excess(const hypergeometric_distribution<RealType, Policy>& 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 <boost/math/distributions/detail/derived_accessors.hpp>
#endif // include guard
|