annotate DEPENDENCIES/generic/include/boost/math/distributions/geometric.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // boost\math\distributions\geometric.hpp
Chris@16 2
Chris@16 3 // Copyright John Maddock 2010.
Chris@16 4 // Copyright Paul A. Bristow 2010.
Chris@16 5
Chris@16 6 // Use, modification and distribution are subject to the
Chris@16 7 // Boost Software License, Version 1.0.
Chris@16 8 // (See accompanying file LICENSE_1_0.txt
Chris@16 9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 10
Chris@16 11 // geometric distribution is a discrete probability distribution.
Chris@16 12 // It expresses the probability distribution of the number (k) of
Chris@16 13 // events, occurrences, failures or arrivals before the first success.
Chris@16 14 // supported on the set {0, 1, 2, 3...}
Chris@16 15
Chris@16 16 // Note that the set includes zero (unlike some definitions that start at one).
Chris@16 17
Chris@16 18 // The random variate k is the number of events, occurrences or arrivals.
Chris@16 19 // k argument may be integral, signed, or unsigned, or floating point.
Chris@16 20 // If necessary, it has already been promoted from an integral type.
Chris@16 21
Chris@16 22 // Note that the geometric distribution
Chris@16 23 // (like others including the binomial, geometric & Bernoulli)
Chris@16 24 // is strictly defined as a discrete function:
Chris@16 25 // only integral values of k are envisaged.
Chris@16 26 // However because the method of calculation uses a continuous gamma function,
Chris@16 27 // it is convenient to treat it as if a continous function,
Chris@16 28 // and permit non-integral values of k.
Chris@16 29 // To enforce the strict mathematical model, users should use floor or ceil functions
Chris@16 30 // on k outside this function to ensure that k is integral.
Chris@16 31
Chris@16 32 // See http://en.wikipedia.org/wiki/geometric_distribution
Chris@16 33 // http://documents.wolfram.com/v5/Add-onsLinks/StandardPackages/Statistics/DiscreteDistributions.html
Chris@16 34 // http://mathworld.wolfram.com/GeometricDistribution.html
Chris@16 35
Chris@16 36 #ifndef BOOST_MATH_SPECIAL_GEOMETRIC_HPP
Chris@16 37 #define BOOST_MATH_SPECIAL_GEOMETRIC_HPP
Chris@16 38
Chris@16 39 #include <boost/math/distributions/fwd.hpp>
Chris@16 40 #include <boost/math/special_functions/beta.hpp> // for ibeta(a, b, x) == Ix(a, b).
Chris@16 41 #include <boost/math/distributions/complement.hpp> // complement.
Chris@16 42 #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks domain_error & logic_error.
Chris@16 43 #include <boost/math/special_functions/fpclassify.hpp> // isnan.
Chris@16 44 #include <boost/math/tools/roots.hpp> // for root finding.
Chris@16 45 #include <boost/math/distributions/detail/inv_discrete_quantile.hpp>
Chris@16 46
Chris@16 47 #include <boost/type_traits/is_floating_point.hpp>
Chris@16 48 #include <boost/type_traits/is_integral.hpp>
Chris@16 49 #include <boost/type_traits/is_same.hpp>
Chris@16 50 #include <boost/mpl/if.hpp>
Chris@16 51
Chris@16 52 #include <limits> // using std::numeric_limits;
Chris@16 53 #include <utility>
Chris@16 54
Chris@16 55 #if defined (BOOST_MSVC)
Chris@16 56 # pragma warning(push)
Chris@16 57 // This believed not now necessary, so commented out.
Chris@16 58 //# pragma warning(disable: 4702) // unreachable code.
Chris@16 59 // in domain_error_imp in error_handling.
Chris@16 60 #endif
Chris@16 61
Chris@16 62 namespace boost
Chris@16 63 {
Chris@16 64 namespace math
Chris@16 65 {
Chris@16 66 namespace geometric_detail
Chris@16 67 {
Chris@16 68 // Common error checking routines for geometric distribution function:
Chris@16 69 template <class RealType, class Policy>
Chris@16 70 inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& pol)
Chris@16 71 {
Chris@16 72 if( !(boost::math::isfinite)(p) || (p < 0) || (p > 1) )
Chris@16 73 {
Chris@16 74 *result = policies::raise_domain_error<RealType>(
Chris@16 75 function,
Chris@16 76 "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, pol);
Chris@16 77 return false;
Chris@16 78 }
Chris@16 79 return true;
Chris@16 80 }
Chris@16 81
Chris@16 82 template <class RealType, class Policy>
Chris@16 83 inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& pol)
Chris@16 84 {
Chris@16 85 return check_success_fraction(function, p, result, pol);
Chris@16 86 }
Chris@16 87
Chris@16 88 template <class RealType, class Policy>
Chris@16 89 inline bool check_dist_and_k(const char* function, const RealType& p, RealType k, RealType* result, const Policy& pol)
Chris@16 90 {
Chris@16 91 if(check_dist(function, p, result, pol) == false)
Chris@16 92 {
Chris@16 93 return false;
Chris@16 94 }
Chris@16 95 if( !(boost::math::isfinite)(k) || (k < 0) )
Chris@16 96 { // Check k failures.
Chris@16 97 *result = policies::raise_domain_error<RealType>(
Chris@16 98 function,
Chris@16 99 "Number of failures argument is %1%, but must be >= 0 !", k, pol);
Chris@16 100 return false;
Chris@16 101 }
Chris@16 102 return true;
Chris@16 103 } // Check_dist_and_k
Chris@16 104
Chris@16 105 template <class RealType, class Policy>
Chris@16 106 inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& pol)
Chris@16 107 {
Chris@16 108 if(check_dist(function, p, result, pol) && detail::check_probability(function, prob, result, pol) == false)
Chris@16 109 {
Chris@16 110 return false;
Chris@16 111 }
Chris@16 112 return true;
Chris@16 113 } // check_dist_and_prob
Chris@16 114 } // namespace geometric_detail
Chris@16 115
Chris@16 116 template <class RealType = double, class Policy = policies::policy<> >
Chris@16 117 class geometric_distribution
Chris@16 118 {
Chris@16 119 public:
Chris@16 120 typedef RealType value_type;
Chris@16 121 typedef Policy policy_type;
Chris@16 122
Chris@16 123 geometric_distribution(RealType p) : m_p(p)
Chris@16 124 { // Constructor stores success_fraction p.
Chris@16 125 RealType result;
Chris@16 126 geometric_detail::check_dist(
Chris@16 127 "geometric_distribution<%1%>::geometric_distribution",
Chris@16 128 m_p, // Check success_fraction 0 <= p <= 1.
Chris@16 129 &result, Policy());
Chris@16 130 } // geometric_distribution constructor.
Chris@16 131
Chris@16 132 // Private data getter class member functions.
Chris@16 133 RealType success_fraction() const
Chris@16 134 { // Probability of success as fraction in range 0 to 1.
Chris@16 135 return m_p;
Chris@16 136 }
Chris@16 137 RealType successes() const
Chris@16 138 { // Total number of successes r = 1 (for compatibility with negative binomial?).
Chris@16 139 return 1;
Chris@16 140 }
Chris@16 141
Chris@16 142 // Parameter estimation.
Chris@16 143 // (These are copies of negative_binomial distribution with successes = 1).
Chris@16 144 static RealType find_lower_bound_on_p(
Chris@16 145 RealType trials,
Chris@16 146 RealType alpha) // alpha 0.05 equivalent to 95% for one-sided test.
Chris@16 147 {
Chris@16 148 static const char* function = "boost::math::geometric<%1%>::find_lower_bound_on_p";
Chris@16 149 RealType result = 0; // of error checks.
Chris@16 150 RealType successes = 1;
Chris@16 151 RealType failures = trials - successes;
Chris@16 152 if(false == detail::check_probability(function, alpha, &result, Policy())
Chris@16 153 && geometric_detail::check_dist_and_k(
Chris@16 154 function, RealType(0), failures, &result, Policy()))
Chris@16 155 {
Chris@16 156 return result;
Chris@16 157 }
Chris@16 158 // Use complement ibeta_inv function for lower bound.
Chris@16 159 // This is adapted from the corresponding binomial formula
Chris@16 160 // here: http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
Chris@16 161 // This is a Clopper-Pearson interval, and may be overly conservative,
Chris@16 162 // see also "A Simple Improved Inferential Method for Some
Chris@16 163 // Discrete Distributions" Yong CAI and K. KRISHNAMOORTHY
Chris@16 164 // http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
Chris@16 165 //
Chris@16 166 return ibeta_inv(successes, failures + 1, alpha, static_cast<RealType*>(0), Policy());
Chris@16 167 } // find_lower_bound_on_p
Chris@16 168
Chris@16 169 static RealType find_upper_bound_on_p(
Chris@16 170 RealType trials,
Chris@16 171 RealType alpha) // alpha 0.05 equivalent to 95% for one-sided test.
Chris@16 172 {
Chris@16 173 static const char* function = "boost::math::geometric<%1%>::find_upper_bound_on_p";
Chris@16 174 RealType result = 0; // of error checks.
Chris@16 175 RealType successes = 1;
Chris@16 176 RealType failures = trials - successes;
Chris@16 177 if(false == geometric_detail::check_dist_and_k(
Chris@16 178 function, RealType(0), failures, &result, Policy())
Chris@16 179 && detail::check_probability(function, alpha, &result, Policy()))
Chris@16 180 {
Chris@16 181 return result;
Chris@16 182 }
Chris@16 183 if(failures == 0)
Chris@16 184 {
Chris@16 185 return 1;
Chris@16 186 }// Use complement ibetac_inv function for upper bound.
Chris@16 187 // Note adjusted failures value: *not* failures+1 as usual.
Chris@16 188 // This is adapted from the corresponding binomial formula
Chris@16 189 // here: http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
Chris@16 190 // This is a Clopper-Pearson interval, and may be overly conservative,
Chris@16 191 // see also "A Simple Improved Inferential Method for Some
Chris@16 192 // Discrete Distributions" Yong CAI and K. Krishnamoorthy
Chris@16 193 // http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
Chris@16 194 //
Chris@16 195 return ibetac_inv(successes, failures, alpha, static_cast<RealType*>(0), Policy());
Chris@16 196 } // find_upper_bound_on_p
Chris@16 197
Chris@16 198 // Estimate number of trials :
Chris@16 199 // "How many trials do I need to be P% sure of seeing k or fewer failures?"
Chris@16 200
Chris@16 201 static RealType find_minimum_number_of_trials(
Chris@16 202 RealType k, // number of failures (k >= 0).
Chris@16 203 RealType p, // success fraction 0 <= p <= 1.
Chris@16 204 RealType alpha) // risk level threshold 0 <= alpha <= 1.
Chris@16 205 {
Chris@16 206 static const char* function = "boost::math::geometric<%1%>::find_minimum_number_of_trials";
Chris@16 207 // Error checks:
Chris@16 208 RealType result = 0;
Chris@16 209 if(false == geometric_detail::check_dist_and_k(
Chris@16 210 function, p, k, &result, Policy())
Chris@16 211 && detail::check_probability(function, alpha, &result, Policy()))
Chris@16 212 {
Chris@16 213 return result;
Chris@16 214 }
Chris@16 215 result = ibeta_inva(k + 1, p, alpha, Policy()); // returns n - k
Chris@16 216 return result + k;
Chris@16 217 } // RealType find_number_of_failures
Chris@16 218
Chris@16 219 static RealType find_maximum_number_of_trials(
Chris@16 220 RealType k, // number of failures (k >= 0).
Chris@16 221 RealType p, // success fraction 0 <= p <= 1.
Chris@16 222 RealType alpha) // risk level threshold 0 <= alpha <= 1.
Chris@16 223 {
Chris@16 224 static const char* function = "boost::math::geometric<%1%>::find_maximum_number_of_trials";
Chris@16 225 // Error checks:
Chris@16 226 RealType result = 0;
Chris@16 227 if(false == geometric_detail::check_dist_and_k(
Chris@16 228 function, p, k, &result, Policy())
Chris@16 229 && detail::check_probability(function, alpha, &result, Policy()))
Chris@16 230 {
Chris@16 231 return result;
Chris@16 232 }
Chris@16 233 result = ibetac_inva(k + 1, p, alpha, Policy()); // returns n - k
Chris@16 234 return result + k;
Chris@16 235 } // RealType find_number_of_trials complemented
Chris@16 236
Chris@16 237 private:
Chris@16 238 //RealType m_r; // successes fixed at unity.
Chris@16 239 RealType m_p; // success_fraction
Chris@16 240 }; // template <class RealType, class Policy> class geometric_distribution
Chris@16 241
Chris@16 242 typedef geometric_distribution<double> geometric; // Reserved name of type double.
Chris@16 243
Chris@16 244 template <class RealType, class Policy>
Chris@16 245 inline const std::pair<RealType, RealType> range(const geometric_distribution<RealType, Policy>& /* dist */)
Chris@16 246 { // Range of permissible values for random variable k.
Chris@16 247 using boost::math::tools::max_value;
Chris@16 248 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // max_integer?
Chris@16 249 }
Chris@16 250
Chris@16 251 template <class RealType, class Policy>
Chris@16 252 inline const std::pair<RealType, RealType> support(const geometric_distribution<RealType, Policy>& /* dist */)
Chris@16 253 { // Range of supported values for random variable k.
Chris@16 254 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
Chris@16 255 using boost::math::tools::max_value;
Chris@16 256 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // max_integer?
Chris@16 257 }
Chris@16 258
Chris@16 259 template <class RealType, class Policy>
Chris@16 260 inline RealType mean(const geometric_distribution<RealType, Policy>& dist)
Chris@16 261 { // Mean of geometric distribution = (1-p)/p.
Chris@16 262 return (1 - dist.success_fraction() ) / dist.success_fraction();
Chris@16 263 } // mean
Chris@16 264
Chris@16 265 // median implemented via quantile(half) in derived accessors.
Chris@16 266
Chris@16 267 template <class RealType, class Policy>
Chris@16 268 inline RealType mode(const geometric_distribution<RealType, Policy>&)
Chris@16 269 { // Mode of geometric distribution = zero.
Chris@16 270 BOOST_MATH_STD_USING // ADL of std functions.
Chris@16 271 return 0;
Chris@16 272 } // mode
Chris@16 273
Chris@16 274 template <class RealType, class Policy>
Chris@16 275 inline RealType variance(const geometric_distribution<RealType, Policy>& dist)
Chris@16 276 { // Variance of Binomial distribution = (1-p) / p^2.
Chris@16 277 return (1 - dist.success_fraction())
Chris@16 278 / (dist.success_fraction() * dist.success_fraction());
Chris@16 279 } // variance
Chris@16 280
Chris@16 281 template <class RealType, class Policy>
Chris@16 282 inline RealType skewness(const geometric_distribution<RealType, Policy>& dist)
Chris@16 283 { // skewness of geometric distribution = 2-p / (sqrt(r(1-p))
Chris@16 284 BOOST_MATH_STD_USING // ADL of std functions.
Chris@16 285 RealType p = dist.success_fraction();
Chris@16 286 return (2 - p) / sqrt(1 - p);
Chris@16 287 } // skewness
Chris@16 288
Chris@16 289 template <class RealType, class Policy>
Chris@16 290 inline RealType kurtosis(const geometric_distribution<RealType, Policy>& dist)
Chris@16 291 { // kurtosis of geometric distribution
Chris@16 292 // http://en.wikipedia.org/wiki/geometric is kurtosis_excess so add 3
Chris@16 293 RealType p = dist.success_fraction();
Chris@16 294 return 3 + (p*p - 6*p + 6) / (1 - p);
Chris@16 295 } // kurtosis
Chris@16 296
Chris@16 297 template <class RealType, class Policy>
Chris@16 298 inline RealType kurtosis_excess(const geometric_distribution<RealType, Policy>& dist)
Chris@16 299 { // kurtosis excess of geometric distribution
Chris@16 300 // http://mathworld.wolfram.com/Kurtosis.html table of kurtosis_excess
Chris@16 301 RealType p = dist.success_fraction();
Chris@16 302 return (p*p - 6*p + 6) / (1 - p);
Chris@16 303 } // kurtosis_excess
Chris@16 304
Chris@16 305 // RealType standard_deviation(const geometric_distribution<RealType, Policy>& dist)
Chris@16 306 // standard_deviation provided by derived accessors.
Chris@16 307 // RealType hazard(const geometric_distribution<RealType, Policy>& dist)
Chris@16 308 // hazard of geometric distribution provided by derived accessors.
Chris@16 309 // RealType chf(const geometric_distribution<RealType, Policy>& dist)
Chris@16 310 // chf of geometric distribution provided by derived accessors.
Chris@16 311
Chris@16 312 template <class RealType, class Policy>
Chris@16 313 inline RealType pdf(const geometric_distribution<RealType, Policy>& dist, const RealType& k)
Chris@16 314 { // Probability Density/Mass Function.
Chris@16 315 BOOST_FPU_EXCEPTION_GUARD
Chris@16 316 BOOST_MATH_STD_USING // For ADL of math functions.
Chris@16 317 static const char* function = "boost::math::pdf(const geometric_distribution<%1%>&, %1%)";
Chris@16 318
Chris@16 319 RealType p = dist.success_fraction();
Chris@16 320 RealType result = 0;
Chris@16 321 if(false == geometric_detail::check_dist_and_k(
Chris@16 322 function,
Chris@16 323 p,
Chris@16 324 k,
Chris@16 325 &result, Policy()))
Chris@16 326 {
Chris@16 327 return result;
Chris@16 328 }
Chris@16 329 if (k == 0)
Chris@16 330 {
Chris@16 331 return p; // success_fraction
Chris@16 332 }
Chris@16 333 RealType q = 1 - p; // Inaccurate for small p?
Chris@16 334 // So try to avoid inaccuracy for large or small p.
Chris@16 335 // but has little effect > last significant bit.
Chris@16 336 //cout << "p * pow(q, k) " << result << endl; // seems best whatever p
Chris@16 337 //cout << "exp(p * k * log1p(-p)) " << p * exp(k * log1p(-p)) << endl;
Chris@16 338 //if (p < 0.5)
Chris@16 339 //{
Chris@16 340 // result = p * pow(q, k);
Chris@16 341 //}
Chris@16 342 //else
Chris@16 343 //{
Chris@16 344 // result = p * exp(k * log1p(-p));
Chris@16 345 //}
Chris@16 346 result = p * pow(q, k);
Chris@16 347 return result;
Chris@16 348 } // geometric_pdf
Chris@16 349
Chris@16 350 template <class RealType, class Policy>
Chris@16 351 inline RealType cdf(const geometric_distribution<RealType, Policy>& dist, const RealType& k)
Chris@16 352 { // Cumulative Distribution Function of geometric.
Chris@16 353 static const char* function = "boost::math::cdf(const geometric_distribution<%1%>&, %1%)";
Chris@16 354
Chris@16 355 // k argument may be integral, signed, or unsigned, or floating point.
Chris@16 356 // If necessary, it has already been promoted from an integral type.
Chris@16 357 RealType p = dist.success_fraction();
Chris@16 358 // Error check:
Chris@16 359 RealType result = 0;
Chris@16 360 if(false == geometric_detail::check_dist_and_k(
Chris@16 361 function,
Chris@16 362 p,
Chris@16 363 k,
Chris@16 364 &result, Policy()))
Chris@16 365 {
Chris@16 366 return result;
Chris@16 367 }
Chris@16 368 if(k == 0)
Chris@16 369 {
Chris@16 370 return p; // success_fraction
Chris@16 371 }
Chris@16 372 //RealType q = 1 - p; // Bad for small p
Chris@16 373 //RealType probability = 1 - std::pow(q, k+1);
Chris@16 374
Chris@101 375 RealType z = boost::math::log1p(-p, Policy()) * (k + 1);
Chris@101 376 RealType probability = -boost::math::expm1(z, Policy());
Chris@16 377
Chris@16 378 return probability;
Chris@16 379 } // cdf Cumulative Distribution Function geometric.
Chris@16 380
Chris@16 381 template <class RealType, class Policy>
Chris@16 382 inline RealType cdf(const complemented2_type<geometric_distribution<RealType, Policy>, RealType>& c)
Chris@16 383 { // Complemented Cumulative Distribution Function geometric.
Chris@16 384 BOOST_MATH_STD_USING
Chris@16 385 static const char* function = "boost::math::cdf(const geometric_distribution<%1%>&, %1%)";
Chris@16 386 // k argument may be integral, signed, or unsigned, or floating point.
Chris@16 387 // If necessary, it has already been promoted from an integral type.
Chris@16 388 RealType const& k = c.param;
Chris@16 389 geometric_distribution<RealType, Policy> const& dist = c.dist;
Chris@16 390 RealType p = dist.success_fraction();
Chris@16 391 // Error check:
Chris@16 392 RealType result = 0;
Chris@16 393 if(false == geometric_detail::check_dist_and_k(
Chris@16 394 function,
Chris@16 395 p,
Chris@16 396 k,
Chris@16 397 &result, Policy()))
Chris@16 398 {
Chris@16 399 return result;
Chris@16 400 }
Chris@101 401 RealType z = boost::math::log1p(-p, Policy()) * (k+1);
Chris@16 402 RealType probability = exp(z);
Chris@16 403 return probability;
Chris@16 404 } // cdf Complemented Cumulative Distribution Function geometric.
Chris@16 405
Chris@16 406 template <class RealType, class Policy>
Chris@16 407 inline RealType quantile(const geometric_distribution<RealType, Policy>& dist, const RealType& x)
Chris@16 408 { // Quantile, percentile/100 or Percent Point geometric function.
Chris@16 409 // Return the number of expected failures k for a given probability p.
Chris@16 410
Chris@16 411 // Inverse cumulative Distribution Function or Quantile (percentile / 100) of geometric Probability.
Chris@16 412 // k argument may be integral, signed, or unsigned, or floating point.
Chris@16 413
Chris@16 414 static const char* function = "boost::math::quantile(const geometric_distribution<%1%>&, %1%)";
Chris@16 415 BOOST_MATH_STD_USING // ADL of std functions.
Chris@16 416
Chris@16 417 RealType success_fraction = dist.success_fraction();
Chris@16 418 // Check dist and x.
Chris@16 419 RealType result = 0;
Chris@16 420 if(false == geometric_detail::check_dist_and_prob
Chris@16 421 (function, success_fraction, x, &result, Policy()))
Chris@16 422 {
Chris@16 423 return result;
Chris@16 424 }
Chris@16 425
Chris@16 426 // Special cases.
Chris@16 427 if (x == 1)
Chris@16 428 { // Would need +infinity failures for total confidence.
Chris@16 429 result = policies::raise_overflow_error<RealType>(
Chris@16 430 function,
Chris@16 431 "Probability argument is 1, which implies infinite failures !", Policy());
Chris@16 432 return result;
Chris@16 433 // usually means return +std::numeric_limits<RealType>::infinity();
Chris@16 434 // unless #define BOOST_MATH_THROW_ON_OVERFLOW_ERROR
Chris@16 435 }
Chris@16 436 if (x == 0)
Chris@16 437 { // No failures are expected if P = 0.
Chris@16 438 return 0; // Total trials will be just dist.successes.
Chris@16 439 }
Chris@16 440 // if (P <= pow(dist.success_fraction(), 1))
Chris@16 441 if (x <= success_fraction)
Chris@16 442 { // p <= pdf(dist, 0) == cdf(dist, 0)
Chris@16 443 return 0;
Chris@16 444 }
Chris@16 445 if (x == 1)
Chris@16 446 {
Chris@16 447 return 0;
Chris@16 448 }
Chris@16 449
Chris@16 450 // log(1-x) /log(1-success_fraction) -1; but use log1p in case success_fraction is small
Chris@101 451 result = boost::math::log1p(-x, Policy()) / boost::math::log1p(-success_fraction, Policy()) - 1;
Chris@16 452 // Subtract a few epsilons here too?
Chris@16 453 // to make sure it doesn't slip over, so ceil would be one too many.
Chris@16 454 return result;
Chris@16 455 } // RealType quantile(const geometric_distribution dist, p)
Chris@16 456
Chris@16 457 template <class RealType, class Policy>
Chris@16 458 inline RealType quantile(const complemented2_type<geometric_distribution<RealType, Policy>, RealType>& c)
Chris@16 459 { // Quantile or Percent Point Binomial function.
Chris@16 460 // Return the number of expected failures k for a given
Chris@16 461 // complement of the probability Q = 1 - P.
Chris@16 462 static const char* function = "boost::math::quantile(const geometric_distribution<%1%>&, %1%)";
Chris@16 463 BOOST_MATH_STD_USING
Chris@16 464 // Error checks:
Chris@16 465 RealType x = c.param;
Chris@16 466 const geometric_distribution<RealType, Policy>& dist = c.dist;
Chris@16 467 RealType success_fraction = dist.success_fraction();
Chris@16 468 RealType result = 0;
Chris@16 469 if(false == geometric_detail::check_dist_and_prob(
Chris@16 470 function,
Chris@16 471 success_fraction,
Chris@16 472 x,
Chris@16 473 &result, Policy()))
Chris@16 474 {
Chris@16 475 return result;
Chris@16 476 }
Chris@16 477
Chris@16 478 // Special cases:
Chris@16 479 if(x == 1)
Chris@16 480 { // There may actually be no answer to this question,
Chris@16 481 // since the probability of zero failures may be non-zero,
Chris@16 482 return 0; // but zero is the best we can do:
Chris@16 483 }
Chris@16 484 if (-x <= boost::math::powm1(dist.success_fraction(), dist.successes(), Policy()))
Chris@16 485 { // q <= cdf(complement(dist, 0)) == pdf(dist, 0)
Chris@16 486 return 0; //
Chris@16 487 }
Chris@16 488 if(x == 0)
Chris@16 489 { // Probability 1 - Q == 1 so infinite failures to achieve certainty.
Chris@16 490 // Would need +infinity failures for total confidence.
Chris@16 491 result = policies::raise_overflow_error<RealType>(
Chris@16 492 function,
Chris@16 493 "Probability argument complement is 0, which implies infinite failures !", Policy());
Chris@16 494 return result;
Chris@16 495 // usually means return +std::numeric_limits<RealType>::infinity();
Chris@16 496 // unless #define BOOST_MATH_THROW_ON_OVERFLOW_ERROR
Chris@16 497 }
Chris@16 498 // log(x) /log(1-success_fraction) -1; but use log1p in case success_fraction is small
Chris@101 499 result = log(x) / boost::math::log1p(-success_fraction, Policy()) - 1;
Chris@16 500 return result;
Chris@16 501
Chris@16 502 } // quantile complement
Chris@16 503
Chris@16 504 } // namespace math
Chris@16 505 } // namespace boost
Chris@16 506
Chris@16 507 // This include must be at the end, *after* the accessors
Chris@16 508 // for this distribution have been defined, in order to
Chris@16 509 // keep compilers that support two-phase lookup happy.
Chris@16 510 #include <boost/math/distributions/detail/derived_accessors.hpp>
Chris@16 511
Chris@16 512 #if defined (BOOST_MSVC)
Chris@16 513 # pragma warning(pop)
Chris@16 514 #endif
Chris@16 515
Chris@16 516 #endif // BOOST_MATH_SPECIAL_GEOMETRIC_HPP