negative_binomial.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 589 行 · 第 1/2 页

HPP
589
字号
    { // skewness of Negative Binomial distribution = 2-p / (sqrt(r(1-p))      BOOST_MATH_STD_USING // ADL of std functions.      RealType p = dist.success_fraction();      RealType r = dist.successes();      return (2 - p) /        sqrt(r * (1 - p));    } // skewness    template <class RealType, class Policy>    inline RealType kurtosis(const negative_binomial_distribution<RealType, Policy>& dist)    { // kurtosis of Negative Binomial distribution      // http://en.wikipedia.org/wiki/Negative_binomial is kurtosis_excess so add 3      RealType p = dist.success_fraction();      RealType r = dist.successes();      return 3 + (6 / r) + ((p * p) / (r * (1 - p)));    } // kurtosis     template <class RealType, class Policy>    inline RealType kurtosis_excess(const negative_binomial_distribution<RealType, Policy>& dist)    { // kurtosis excess of Negative Binomial distribution      // http://mathworld.wolfram.com/Kurtosis.html table of kurtosis_excess      RealType p = dist.success_fraction();      RealType r = dist.successes();      return (6 - p * (6-p)) / (r * (1-p));    } // kurtosis_excess    template <class RealType, class Policy>    inline RealType variance(const negative_binomial_distribution<RealType, Policy>& dist)    { // Variance of Binomial distribution = r (1-p) / p^2.      return  dist.successes() * (1 - dist.success_fraction())        / (dist.success_fraction() * dist.success_fraction());    } // variance    // RealType standard_deviation(const negative_binomial_distribution<RealType, Policy>& dist)    // standard_deviation provided by derived accessors.    // RealType hazard(const negative_binomial_distribution<RealType, Policy>& dist)    // hazard of Negative Binomial distribution provided by derived accessors.    // RealType chf(const negative_binomial_distribution<RealType, Policy>& dist)    // chf of Negative Binomial distribution provided by derived accessors.    template <class RealType, class Policy>    inline RealType pdf(const negative_binomial_distribution<RealType, Policy>& dist, const RealType& k)    { // Probability Density/Mass Function.      BOOST_FPU_EXCEPTION_GUARD      static const char* function = "boost::math::pdf(const negative_binomial_distribution<%1%>&, %1%)";      RealType r = dist.successes();      RealType p = dist.success_fraction();      RealType result;      if(false == negative_binomial_detail::check_dist_and_k(        function,        r,        dist.success_fraction(),        k,        &result, Policy()))      {        return result;      }      result = (p/(r + k)) * ibeta_derivative(r, static_cast<RealType>(k+1), p, Policy());      // Equivalent to:      // return exp(lgamma(r + k) - lgamma(r) - lgamma(k+1)) * pow(p, r) * pow((1-p), k);      return result;    } // negative_binomial_pdf    template <class RealType, class Policy>    inline RealType cdf(const negative_binomial_distribution<RealType, Policy>& dist, const RealType& k)    { // Cumulative Distribution Function of Negative Binomial.      static const char* function = "boost::math::cdf(const negative_binomial_distribution<%1%>&, %1%)";      using boost::math::ibeta; // Regularized incomplete beta function.      // k argument may be integral, signed, or unsigned, or floating point.      // If necessary, it has already been promoted from an integral type.      RealType p = dist.success_fraction();      RealType r = dist.successes();      // Error check:      RealType result;      if(false == negative_binomial_detail::check_dist_and_k(        function,        r,        dist.success_fraction(),        k,        &result, Policy()))      {        return result;      }      RealType probability = ibeta(r, static_cast<RealType>(k+1), p, Policy());      // Ip(r, k+1) = ibeta(r, k+1, p)      return probability;    } // cdf Cumulative Distribution Function Negative Binomial.      template <class RealType, class Policy>      inline RealType cdf(const complemented2_type<negative_binomial_distribution<RealType, Policy>, RealType>& c)      { // Complemented Cumulative Distribution Function Negative Binomial.      static const char* function = "boost::math::cdf(const negative_binomial_distribution<%1%>&, %1%)";      using boost::math::ibetac; // Regularized incomplete beta function complement.      // k argument may be integral, signed, or unsigned, or floating point.      // If necessary, it has already been promoted from an integral type.      RealType const& k = c.param;      negative_binomial_distribution<RealType, Policy> const& dist = c.dist;      RealType p = dist.success_fraction();      RealType r = dist.successes();      // Error check:      RealType result;      if(false == negative_binomial_detail::check_dist_and_k(        function,        r,        p,        k,        &result, Policy()))      {        return result;      }      // Calculate cdf negative binomial using the incomplete beta function.      // Use of ibeta here prevents cancellation errors in calculating      // 1-p if p is very small, perhaps smaller than machine epsilon.      // Ip(k+1, r) = ibetac(r, k+1, p)      // constrain_probability here?     RealType probability = ibetac(r, static_cast<RealType>(k+1), p, Policy());      // Numerical errors might cause probability to be slightly outside the range < 0 or > 1.      // This might cause trouble downstream, so warn, possibly throw exception, but constrain to the limits.      return probability;    } // cdf Cumulative Distribution Function Negative Binomial.    template <class RealType, class Policy>    inline RealType quantile(const negative_binomial_distribution<RealType, Policy>& dist, const RealType& P)    { // Quantile, percentile/100 or Percent Point Negative Binomial function.      // Return the number of expected failures k for a given probability p.      // Inverse cumulative Distribution Function or Quantile (percentile / 100) of negative_binomial Probability.      // MAthCAD pnbinom return smallest k such that negative_binomial(k, n, p) >= probability.      // k argument may be integral, signed, or unsigned, or floating point.      // BUT Cephes/CodeCogs says: finds argument p (0 to 1) such that cdf(k, n, p) = y      static const char* function = "boost::math::quantile(const negative_binomial_distribution<%1%>&, %1%)";      BOOST_MATH_STD_USING // ADL of std functions.      RealType p = dist.success_fraction();      RealType r = dist.successes();      // Check dist and P.      RealType result;      if(false == negative_binomial_detail::check_dist_and_prob        (function, r, p, P, &result, Policy()))      {        return result;      }      // Special cases.      if (P == 1)      {  // Would need +infinity failures for total confidence.        result = policies::raise_overflow_error<RealType>(            function,            "Probability argument is 1, which implies infinite failures !", Policy());        return result;       // usually means return +std::numeric_limits<RealType>::infinity();       // unless #define BOOST_MATH_THROW_ON_OVERFLOW_ERROR      }      if (P == 0)      { // No failures are expected if P = 0.        return 0; // Total trials will be just dist.successes.      }      if (P <= pow(dist.success_fraction(), dist.successes()))      { // p <= pdf(dist, 0) == cdf(dist, 0)        return 0;      }      /*      // Calculate quantile of negative_binomial using the inverse incomplete beta function.      using boost::math::ibeta_invb;      return ibeta_invb(r, p, P, Policy()) - 1; //      */      RealType guess = 0;      RealType factor = 5;      if(r * r * r * P * p > 0.005)         guess = detail::inverse_negative_binomial_cornish_fisher(r, p, 1-p, P, 1-P, Policy());      if(guess < 10)      {         //         // Cornish-Fisher Negative binomial approximation not accurate in this area:         //         guess = (std::min)(r * 2, RealType(10));      }      else         factor = (1-P < sqrt(tools::epsilon<RealType>())) ? 2 : (guess < 20 ? 1.2f : 1.1f);      BOOST_MATH_INSTRUMENT_CODE("guess = " << guess);      //      // Max iterations permitted:      //      boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();      typedef typename Policy::discrete_quantile_type discrete_type;      return detail::inverse_discrete_quantile(         dist,         P,         1-P,         guess,         factor,         RealType(1),         discrete_type(),         max_iter);    } // RealType quantile(const negative_binomial_distribution dist, p)    template <class RealType, class Policy>    inline RealType quantile(const complemented2_type<negative_binomial_distribution<RealType, Policy>, RealType>& c)    {  // Quantile or Percent Point Binomial function.       // Return the number of expected failures k for a given       // complement of the probability Q = 1 - P.       static const char* function = "boost::math::quantile(const negative_binomial_distribution<%1%>&, %1%)";       BOOST_MATH_STD_USING       // Error checks:       RealType Q = c.param;       const negative_binomial_distribution<RealType, Policy>& dist = c.dist;       RealType p = dist.success_fraction();       RealType r = dist.successes();       RealType result;       if(false == negative_binomial_detail::check_dist_and_prob(          function,          r,          p,          Q,          &result, Policy()))       {          return result;       }       // Special cases:       //       if(Q == 1)       {  // There may actually be no answer to this question,          // since the probability of zero failures may be non-zero,          return 0; // but zero is the best we can do:       }       if (-Q <= boost::math::powm1(dist.success_fraction(), dist.successes(), Policy()))       {  // q <= cdf(complement(dist, 0)) == pdf(dist, 0)          return 0; //       }       if(Q == 0)       {  // Probability 1 - Q  == 1 so infinite failures to achieve certainty.          // Would need +infinity failures for total confidence.          result = policies::raise_overflow_error<RealType>(             function,             "Probability argument complement is 0, which implies infinite failures !", Policy());          return result;          // usually means return +std::numeric_limits<RealType>::infinity();          // unless #define BOOST_MATH_THROW_ON_OVERFLOW_ERROR       }       //return ibetac_invb(r, p, Q, Policy()) -1;       RealType guess = 0;       RealType factor = 5;       if(r * r * r * (1-Q) * p > 0.005)          guess = detail::inverse_negative_binomial_cornish_fisher(r, p, 1-p, 1-Q, Q, Policy());       if(guess < 10)       {          //          // Cornish-Fisher Negative binomial approximation not accurate in this area:          //          guess = (std::min)(r * 2, RealType(10));       }       else          factor = (Q < sqrt(tools::epsilon<RealType>())) ? 2 : (guess < 20 ? 1.2f : 1.1f);       BOOST_MATH_INSTRUMENT_CODE("guess = " << guess);       //       // Max iterations permitted:       //       boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();       typedef typename Policy::discrete_quantile_type discrete_type;       return detail::inverse_discrete_quantile(          dist,          1-Q,          Q,          guess,          factor,          RealType(1),          discrete_type(),          max_iter);    } // quantile complement } // namespace math} // namespace boost// 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>#if defined (BOOST_MSVC)# pragma warning(pop)#endif#endif // BOOST_MATH_SPECIAL_NEGATIVE_BINOMIAL_HPP

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?