poisson.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 589 行 · 第 1/2 页
HPP
589 行
template <class RealType, class Policy> inline RealType kurtosis(const poisson_distribution<RealType, Policy>& dist) { // kurtosis is 4th moment about the mean = u4 / sd ^ 4 // http://en.wikipedia.org/wiki/Curtosis // kurtosis can range from -2 (flat top) to +infinity (sharp peak & heavy tails). // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm return 3 + 1 / dist.mean(); // NIST. // http://mathworld.wolfram.com/Kurtosis.html explains that the kurtosis excess // is more convenient because the kurtosis excess of a normal distribution is zero // whereas the true kurtosis is 3. } // RealType kurtosis template <class RealType, class Policy> RealType pdf(const poisson_distribution<RealType, Policy>& dist, const RealType& k) { // Probability Density/Mass Function. // Probability that there are EXACTLY k occurrences (or arrivals). BOOST_FPU_EXCEPTION_GUARD BOOST_MATH_STD_USING // for ADL of std functions. RealType mean = dist.mean(); // Error check: RealType result; if(false == poisson_detail::check_dist_and_k( "boost::math::pdf(const poisson_distribution<%1%>&, %1%)", mean, k, &result, Policy())) { return result; } // Special case of mean zero, regardless of the number of events k. if (mean == 0) { // Probability for any k is zero. return 0; } if (k == 0) { // mean ^ k = 1, and k! = 1, so can simplify. return exp(-mean); } return boost::math::gamma_p_derivative(k+1, mean, Policy()); } // pdf template <class RealType, class Policy> RealType cdf(const poisson_distribution<RealType, Policy>& dist, const RealType& k) { // Cumulative Distribution Function Poisson. // The random variate k is the number of occurrences(or arrivals) // k argument may be integral, signed, or unsigned, or floating point. // If necessary, it has already been promoted from an integral type. // Returns the sum of the terms 0 through k of the Poisson Probability Density or Mass (pdf). // But note that the Poisson distribution // (like others including the binomial, negative binomial & Bernoulli) // is strictly defined as a discrete function: only integral values of k are envisaged. // However because of the method of calculation using a continuous gamma function, // it is convenient to treat it as if it is a continous function // and permit non-integral values of k. // To enforce the strict mathematical model, users should use floor or ceil functions // outside this function to ensure that k is integral. // The terms are not summed directly (at least for larger k) // instead the incomplete gamma integral is employed, BOOST_MATH_STD_USING // for ADL of std function exp. RealType mean = dist.mean(); // Error checks: RealType result; if(false == poisson_detail::check_dist_and_k( "boost::math::cdf(const poisson_distribution<%1%>&, %1%)", mean, k, &result, Policy())) { return result; } // Special cases: if (mean == 0) { // Probability for any k is zero. return 0; } if (k == 0) { // return pdf(dist, static_cast<RealType>(0)); // but mean (and k) have already been checked, // so this avoids unnecessary repeated checks. return exp(-mean); } // For small integral k could use a finite sum - // it's cheaper than the gamma function. // BUT this is now done efficiently by gamma_q function. // Calculate poisson cdf using the gamma_q function. return gamma_q(k+1, mean, Policy()); } // binomial cdf template <class RealType, class Policy> RealType cdf(const complemented2_type<poisson_distribution<RealType, Policy>, RealType>& c) { // Complemented Cumulative Distribution Function Poisson // The random variate k is the number of events, occurrences or arrivals. // k argument may be integral, signed, or unsigned, or floating point. // If necessary, it has already been promoted from an integral type. // But note that the Poisson distribution // (like others including the binomial, negative binomial & Bernoulli) // is strictly defined as a discrete function: only integral values of k are envisaged. // However because of the method of calculation using a continuous gamma function, // it is convenient to treat it as is it is a continous function // and permit non-integral values of k. // To enforce the strict mathematical model, users should use floor or ceil functions // outside this function to ensure that k is integral. // Returns the sum of the terms k+1 through inf of the Poisson Probability Density/Mass (pdf). // The terms are not summed directly (at least for larger k) // instead the incomplete gamma integral is employed, RealType const& k = c.param; poisson_distribution<RealType, Policy> const& dist = c.dist; RealType mean = dist.mean(); // Error checks: RealType result; if(false == poisson_detail::check_dist_and_k( "boost::math::cdf(const poisson_distribution<%1%>&, %1%)", mean, k, &result, Policy())) { return result; } // Special case of mean, regardless of the number of events k. if (mean == 0) { // Probability for any k is unity, complement of zero. return 1; } if (k == 0) { // Avoid repeated checks on k and mean in gamma_p. return -boost::math::expm1(-mean, Policy()); } // Unlike un-complemented cdf (sum from 0 to k), // can't use finite sum from k+1 to infinity for small integral k, // anyway it is now done efficiently by gamma_p. return gamma_p(k + 1, mean, Policy()); // Calculate Poisson cdf using the gamma_p function. // CCDF = gamma_p(k+1, lambda) } // poisson ccdf template <class RealType, class Policy> inline RealType quantile(const poisson_distribution<RealType, Policy>& dist, const RealType& p) { // Quantile (or Percent Point) Poisson function. // Return the number of expected events k for a given probability p. RealType result; // of Argument checks: if(false == poisson_detail::check_prob( "boost::math::quantile(const poisson_distribution<%1%>&, %1%)", p, &result, Policy())) { return result; } // Special case: if (dist.mean() == 0) { // if mean = 0 then p = 0, so k can be anything? if (false == poisson_detail::check_mean_NZ( "boost::math::quantile(const poisson_distribution<%1%>&, %1%)", dist.mean(), &result, Policy())) { return result; } } /* BOOST_MATH_STD_USING // ADL of std functions. // if(p == 0) NOT necessarily zero! // Not necessarily any special value of k because is unlimited. if (p <= exp(-dist.mean())) { // if p <= cdf for 0 events (== pdf for 0 events), then quantile must be zero. return 0; } return gamma_q_inva(dist.mean(), p, Policy()) - 1; */ typedef typename Policy::discrete_quantile_type discrete_type; boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>(); RealType guess, factor = 8; RealType z = dist.mean(); if(z < 1) guess = z; else guess = boost::math::detail::inverse_poisson_cornish_fisher(z, p, 1-p, Policy()); if(z > 5) { if(z > 1000) factor = 1.01f; else if(z > 50) factor = 1.1f; else if(guess > 10) factor = 1.25f; else factor = 2; if(guess < 1.1) factor = 8; } return detail::inverse_discrete_quantile( dist, p, 1-p, guess, factor, RealType(1), discrete_type(), max_iter); } // quantile template <class RealType, class Policy> inline RealType quantile(const complemented2_type<poisson_distribution<RealType, Policy>, RealType>& c) { // Quantile (or Percent Point) of Poisson function. // Return the number of expected events k for a given // complement of the probability q. // // Error checks: RealType q = c.param; const poisson_distribution<RealType, Policy>& dist = c.dist; RealType result; // of argument checks. if(false == poisson_detail::check_prob( "boost::math::quantile(const poisson_distribution<%1%>&, %1%)", q, &result, Policy())) { return result; } // Special case: if (dist.mean() == 0) { // if mean = 0 then p = 0, so k can be anything? if (false == poisson_detail::check_mean_NZ( "boost::math::quantile(const poisson_distribution<%1%>&, %1%)", dist.mean(), &result, Policy())) { return result; } } /* if (-q <= boost::math::expm1(-dist.mean())) { // if q <= cdf(complement for 0 events, then quantile must be zero. return 0; } return gamma_p_inva(dist.mean(), q, Policy()) -1; */ typedef typename Policy::discrete_quantile_type discrete_type; boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>(); RealType guess, factor = 8; RealType z = dist.mean(); if(z < 1) guess = z; else guess = boost::math::detail::inverse_poisson_cornish_fisher(z, 1-q, q, Policy()); if(z > 5) { if(z > 1000) factor = 1.01f; else if(z > 50) factor = 1.1f; else if(guess > 10) factor = 1.25f; else factor = 2; if(guess < 1.1) factor = 8; } 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>#include <boost/math/distributions/detail/inv_discrete_quantile.hpp>#endif // BOOST_MATH_SPECIAL_POISSON_HPP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?