beta.hpp

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

HPP
545
字号
      RealType m_alpha; // Two parameters of the beta distribution.      RealType m_beta;    }; // template <class RealType, class Policy> class beta_distribution    template <class RealType, class Policy>    inline const std::pair<RealType, RealType> range(const beta_distribution<RealType, Policy>& /* dist */)    { // Range of permissible values for random variable x.      using boost::math::tools::max_value;      return std::pair<RealType, RealType>(0, 1);    }    template <class RealType, class Policy>    inline const std::pair<RealType, RealType> support(const beta_distribution<RealType, Policy>&  /* dist */)    { // Range of supported values for random variable x.      // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.      return std::pair<RealType, RealType>(0, 1);    }    template <class RealType, class Policy>    inline RealType mean(const beta_distribution<RealType, Policy>& dist)    { // Mean of beta distribution = np.      return  dist.alpha() / (dist.alpha() + dist.beta());    } // mean    template <class RealType, class Policy>    inline RealType variance(const beta_distribution<RealType, Policy>& dist)    { // Variance of beta distribution = np(1-p).      RealType a = dist.alpha();      RealType b = dist.beta();      return  (a * b) / ((a + b ) * (a + b) * (a + b + 1));    } // variance    template <class RealType, class Policy>    inline RealType mode(const beta_distribution<RealType, Policy>& dist)    {      static const char* function = "boost::math::mode(beta_distribution<%1%> const&)";      RealType result;      if ((dist.alpha() <= 1))      {        result = policies::raise_domain_error<RealType>(          function,          "mode undefined for alpha = %1%, must be > 1!", dist.alpha(), Policy());        return result;      }      if ((dist.beta() <= 1))      {        result = policies::raise_domain_error<RealType>(          function,          "mode undefined for beta = %1%, must be > 1!", dist.beta(), Policy());        return result;      }      RealType a = dist.alpha();      RealType b = dist.beta();      return (a-1) / (a + b - 2);    } // mode    //template <class RealType, class Policy>    //inline RealType median(const beta_distribution<RealType, Policy>& dist)    //{ // Median of beta distribution is not defined.    //  return tools::domain_error<RealType>(function, "Median is not implemented, result is %1%!", std::numeric_limits<RealType>::quiet_NaN());    //} // median    //But WILL be provided by the derived accessor as quantile(0.5).    template <class RealType, class Policy>    inline RealType skewness(const beta_distribution<RealType, Policy>& dist)    {      BOOST_MATH_STD_USING // ADL of std functions.      RealType a = dist.alpha();      RealType b = dist.beta();      return (2 * (b-a) * sqrt(a + b + 1)) / ((a + b + 2) * sqrt(a * b));    } // skewness    template <class RealType, class Policy>    inline RealType kurtosis_excess(const beta_distribution<RealType, Policy>& dist)    {      RealType a = dist.alpha();      RealType b = dist.beta();      RealType a_2 = a * a;      RealType n = 6 * (a_2 * a - a_2 * (2 * b - 1) + b * b * (b + 1) - 2 * a * b * (b + 2));      RealType d = a * b * (a + b + 2) * (a + b + 3);      return  n / d;    } // kurtosis_excess    template <class RealType, class Policy>    inline RealType kurtosis(const beta_distribution<RealType, Policy>& dist)    {      return 3 + kurtosis_excess(dist);    } // kurtosis    template <class RealType, class Policy>    inline RealType pdf(const beta_distribution<RealType, Policy>& dist, const RealType& x)    { // Probability Density/Mass Function.      BOOST_FPU_EXCEPTION_GUARD      static const char* function = "boost::math::pdf(beta_distribution<%1%> const&, %1%)";      BOOST_MATH_STD_USING // for ADL of std functions      RealType a = dist.alpha();      RealType b = dist.beta();      // Argument checks:      RealType result;      if(false == beta_detail::check_dist_and_x(        function,        a, b, x,        &result, Policy()))      {        return result;      }      using boost::math::beta;      return ibeta_derivative(a, b, x, Policy());    } // pdf    template <class RealType, class Policy>    inline RealType cdf(const beta_distribution<RealType, Policy>& dist, const RealType& x)    { // Cumulative Distribution Function beta.      BOOST_MATH_STD_USING // for ADL of std functions      static const char* function = "boost::math::cdf(beta_distribution<%1%> const&, %1%)";      RealType a = dist.alpha();      RealType b = dist.beta();      // Argument checks:      RealType result;      if(false == beta_detail::check_dist_and_x(        function,        a, b, x,        &result, Policy()))      {        return result;      }      // Special cases:      if (x == 0)      {        return 0;      }      else if (x == 1)      {        return 1;      }      return ibeta(a, b, x, Policy());    } // beta cdf    template <class RealType, class Policy>    inline RealType cdf(const complemented2_type<beta_distribution<RealType, Policy>, RealType>& c)    { // Complemented Cumulative Distribution Function beta.      BOOST_MATH_STD_USING // for ADL of std functions      static const char* function = "boost::math::cdf(beta_distribution<%1%> const&, %1%)";      RealType const& x = c.param;      beta_distribution<RealType, Policy> const& dist = c.dist;      RealType a = dist.alpha();      RealType b = dist.beta();      // Argument checks:      RealType result;      if(false == beta_detail::check_dist_and_x(        function,        a, b, x,        &result, Policy()))      {        return result;      }      if (x == 0)      {        return 1;      }      else if (x == 1)      {        return 0;      }      // Calculate cdf beta using the incomplete beta function.      // Use of ibeta here prevents cancellation errors in calculating      // 1 - x if x is very small, perhaps smaller than machine epsilon.      return ibetac(a, b, x, Policy());    } // beta cdf    template <class RealType, class Policy>    inline RealType quantile(const beta_distribution<RealType, Policy>& dist, const RealType& p)    { // Quantile or Percent Point beta function or      // Inverse Cumulative probability distribution function CDF.      // Return x (0 <= x <= 1),      // for a given probability p (0 <= p <= 1).      // These functions take a probability as an argument      // and return a value such that the probability that a random variable x      // will be less than or equal to that value      // is whatever probability you supplied as an argument.      static const char* function = "boost::math::quantile(beta_distribution<%1%> const&, %1%)";      RealType result; // of argument checks:      RealType a = dist.alpha();      RealType b = dist.beta();      if(false == beta_detail::check_dist_and_prob(        function,        a, b, p,        &result, Policy()))      {        return result;      }      // Special cases:      if (p == 0)      {        return 0;      }      if (p == 1)      {        return 1;      }      return ibeta_inv(a, b, p, static_cast<RealType*>(0), Policy());    } // quantile    template <class RealType, class Policy>    inline RealType quantile(const complemented2_type<beta_distribution<RealType, Policy>, RealType>& c)    { // Complement Quantile or Percent Point beta function .      // Return the number of expected x for a given      // complement of the probability q.      static const char* function = "boost::math::quantile(beta_distribution<%1%> const&, %1%)";      //      // Error checks:      RealType q = c.param;      const beta_distribution<RealType, Policy>& dist = c.dist;      RealType result;      RealType a = dist.alpha();      RealType b = dist.beta();      if(false == beta_detail::check_dist_and_prob(        function,        a,        b,        q,        &result, Policy()))      {        return result;      }      // Special cases:      if(q == 1)      {        return 0;      }      if(q == 0)      {        return 1;      }      return ibetac_inv(a, b, q, static_cast<RealType*>(0), Policy());    } // 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_DIST_BETA_HPP

⌨️ 快捷键说明

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