Chris@16: // Copyright John Maddock 2006, 2007. Chris@16: // Copyright Paul A. Bristow 2007. Chris@16: Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_STATS_CAUCHY_HPP Chris@16: #define BOOST_STATS_CAUCHY_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable : 4127) // conditional expression is constant Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math Chris@16: { Chris@16: Chris@16: template Chris@16: class cauchy_distribution; Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: template Chris@16: RealType cdf_imp(const cauchy_distribution& dist, const RealType& x, bool complement) Chris@16: { Chris@16: // Chris@16: // This calculates the cdf of the Cauchy distribution and/or its complement. Chris@16: // Chris@16: // The usual formula for the Cauchy cdf is: Chris@16: // Chris@16: // cdf = 0.5 + atan(x)/pi Chris@16: // Chris@16: // But that suffers from cancellation error as x -> -INF. Chris@16: // Chris@16: // Recall that for x < 0: Chris@16: // Chris@16: // atan(x) = -pi/2 - atan(1/x) Chris@16: // Chris@16: // Substituting into the above we get: Chris@16: // Chris@16: // CDF = -atan(1/x) ; x < 0 Chris@16: // Chris@16: // So the proceedure is to calculate the cdf for -fabs(x) Chris@16: // using the above formula, and then subtract from 1 when required Chris@16: // to get the result. Chris@16: // Chris@16: BOOST_MATH_STD_USING // for ADL of std functions Chris@16: static const char* function = "boost::math::cdf(cauchy<%1%>&, %1%)"; Chris@16: RealType result = 0; Chris@16: RealType location = dist.location(); Chris@16: RealType scale = dist.scale(); Chris@16: if(false == detail::check_location(function, location, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: if(false == detail::check_scale(function, scale, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: if(std::numeric_limits::has_infinity && x == std::numeric_limits::infinity()) Chris@16: { // cdf +infinity is unity. Chris@16: return static_cast((complement) ? 0 : 1); Chris@16: } Chris@16: if(std::numeric_limits::has_infinity && x == -std::numeric_limits::infinity()) Chris@16: { // cdf -infinity is zero. Chris@16: return static_cast((complement) ? 1 : 0); Chris@16: } Chris@16: if(false == detail::check_x(function, x, &result, Policy())) Chris@16: { // Catches x == NaN Chris@16: return result; Chris@16: } Chris@16: RealType mx = -fabs((x - location) / scale); // scale is > 0 Chris@16: if(mx > -tools::epsilon() / 8) Chris@16: { // special case first: x extremely close to location. Chris@16: return 0.5; Chris@16: } Chris@16: result = -atan(1 / mx) / constants::pi(); Chris@16: return (((x > location) != complement) ? 1 - result : result); Chris@16: } // cdf Chris@16: Chris@16: template Chris@16: RealType quantile_imp( Chris@16: const cauchy_distribution& dist, Chris@16: const RealType& p, Chris@16: bool complement) Chris@16: { Chris@16: // This routine implements the quantile for the Cauchy distribution, Chris@16: // the value p may be the probability, or its complement if complement=true. Chris@16: // Chris@16: // The procedure first performs argument reduction on p to avoid error Chris@16: // when calculating the tangent, then calulates the distance from the Chris@16: // mid-point of the distribution. This is either added or subtracted Chris@16: // from the location parameter depending on whether `complement` is true. Chris@16: // Chris@16: static const char* function = "boost::math::quantile(cauchy<%1%>&, %1%)"; Chris@16: BOOST_MATH_STD_USING // for ADL of std functions Chris@16: Chris@16: RealType result = 0; Chris@16: RealType location = dist.location(); Chris@16: RealType scale = dist.scale(); Chris@16: if(false == detail::check_location(function, location, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: if(false == detail::check_scale(function, scale, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: if(false == detail::check_probability(function, p, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: // Special cases: Chris@16: if(p == 1) Chris@16: { Chris@16: return (complement ? -1 : 1) * policies::raise_overflow_error(function, 0, Policy()); Chris@16: } Chris@16: if(p == 0) Chris@16: { Chris@16: return (complement ? 1 : -1) * policies::raise_overflow_error(function, 0, Policy()); Chris@16: } Chris@16: Chris@16: RealType P = p - floor(p); // argument reduction of p: Chris@16: if(P > 0.5) Chris@16: { Chris@16: P = P - 1; Chris@16: } Chris@16: if(P == 0.5) // special case: Chris@16: { Chris@16: return location; Chris@16: } Chris@16: result = -scale / tan(constants::pi() * P); Chris@16: return complement ? RealType(location - result) : RealType(location + result); Chris@16: } // quantile Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template > Chris@16: class cauchy_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType value_type; Chris@16: typedef Policy policy_type; Chris@16: Chris@16: cauchy_distribution(RealType l_location = 0, RealType l_scale = 1) Chris@16: : m_a(l_location), m_hg(l_scale) Chris@16: { Chris@16: static const char* function = "boost::math::cauchy_distribution<%1%>::cauchy_distribution"; Chris@16: RealType result; Chris@16: detail::check_location(function, l_location, &result, Policy()); Chris@16: detail::check_scale(function, l_scale, &result, Policy()); Chris@16: } // cauchy_distribution Chris@16: Chris@16: RealType location()const Chris@16: { Chris@16: return m_a; Chris@16: } Chris@16: RealType scale()const Chris@16: { Chris@16: return m_hg; Chris@16: } Chris@16: Chris@16: private: Chris@16: RealType m_a; // The location, this is the median of the distribution. Chris@16: RealType m_hg; // The scale )or shape), this is the half width at half height. Chris@16: }; Chris@16: Chris@16: typedef cauchy_distribution cauchy; Chris@16: Chris@16: template Chris@16: inline const std::pair range(const cauchy_distribution&) Chris@16: { // Range of permissible values for random variable x. Chris@16: if (std::numeric_limits::has_infinity) Chris@16: { Chris@16: return std::pair(-std::numeric_limits::infinity(), std::numeric_limits::infinity()); // - to + infinity. Chris@16: } Chris@16: else Chris@16: { // Can only use max_value. Chris@16: using boost::math::tools::max_value; Chris@16: return std::pair(-max_value(), max_value()); // - to + max. Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: inline const std::pair support(const cauchy_distribution& ) Chris@16: { // Range of supported values for random variable x. Chris@16: // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero. Chris@16: if (std::numeric_limits::has_infinity) Chris@16: { Chris@16: return std::pair(-std::numeric_limits::infinity(), std::numeric_limits::infinity()); // - to + infinity. Chris@16: } Chris@16: else Chris@16: { // Can only use max_value. Chris@16: using boost::math::tools::max_value; Chris@16: return std::pair(-tools::max_value(), max_value()); // - to + max. Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType pdf(const cauchy_distribution& dist, const RealType& x) Chris@16: { Chris@16: BOOST_MATH_STD_USING // for ADL of std functions Chris@16: Chris@16: static const char* function = "boost::math::pdf(cauchy<%1%>&, %1%)"; Chris@16: RealType result = 0; Chris@16: RealType location = dist.location(); Chris@16: RealType scale = dist.scale(); Chris@16: if(false == detail::check_scale("boost::math::pdf(cauchy<%1%>&, %1%)", scale, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: if(false == detail::check_location("boost::math::pdf(cauchy<%1%>&, %1%)", location, &result, Policy())) Chris@16: { Chris@16: return result; Chris@16: } Chris@16: if((boost::math::isinf)(x)) Chris@16: { Chris@16: return 0; // pdf + and - infinity is zero. Chris@16: } Chris@16: // These produce MSVC 4127 warnings, so the above used instead. Chris@16: //if(std::numeric_limits::has_infinity && abs(x) == std::numeric_limits::infinity()) Chris@16: //{ // pdf + and - infinity is zero. Chris@16: // return 0; Chris@16: //} Chris@16: Chris@16: if(false == detail::check_x(function, x, &result, Policy())) Chris@16: { // Catches x = NaN Chris@16: return result; Chris@16: } Chris@16: Chris@16: RealType xs = (x - location) / scale; Chris@16: result = 1 / (constants::pi() * scale * (1 + xs * xs)); Chris@16: return result; Chris@16: } // pdf Chris@16: Chris@16: template Chris@16: inline RealType cdf(const cauchy_distribution& dist, const RealType& x) Chris@16: { Chris@16: return detail::cdf_imp(dist, x, false); Chris@16: } // cdf Chris@16: Chris@16: template Chris@16: inline RealType quantile(const cauchy_distribution& dist, const RealType& p) Chris@16: { Chris@16: return detail::quantile_imp(dist, p, false); Chris@16: } // quantile Chris@16: Chris@16: template Chris@16: inline RealType cdf(const complemented2_type, RealType>& c) Chris@16: { Chris@16: return detail::cdf_imp(c.dist, c.param, true); Chris@16: } // cdf complement Chris@16: Chris@16: template Chris@16: inline RealType quantile(const complemented2_type, RealType>& c) Chris@16: { Chris@16: return detail::quantile_imp(c.dist, c.param, true); Chris@16: } // quantile complement Chris@16: Chris@16: template Chris@16: inline RealType mean(const cauchy_distribution&) Chris@16: { // There is no mean: Chris@16: typedef typename Policy::assert_undefined_type assert_type; Chris@16: BOOST_STATIC_ASSERT(assert_type::value == 0); Chris@16: Chris@16: return policies::raise_domain_error( Chris@16: "boost::math::mean(cauchy<%1%>&)", Chris@16: "The Cauchy distribution does not have a mean: " Chris@16: "the only possible return value is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), Policy()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType variance(const cauchy_distribution& /*dist*/) Chris@16: { Chris@16: // There is no variance: Chris@16: typedef typename Policy::assert_undefined_type assert_type; Chris@16: BOOST_STATIC_ASSERT(assert_type::value == 0); Chris@16: Chris@16: return policies::raise_domain_error( Chris@16: "boost::math::variance(cauchy<%1%>&)", Chris@16: "The Cauchy distribution does not have a variance: " Chris@16: "the only possible return value is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), Policy()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType mode(const cauchy_distribution& dist) Chris@16: { Chris@16: return dist.location(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType median(const cauchy_distribution& dist) Chris@16: { Chris@16: return dist.location(); Chris@16: } Chris@16: template Chris@16: inline RealType skewness(const cauchy_distribution& /*dist*/) Chris@16: { Chris@16: // There is no skewness: Chris@16: typedef typename Policy::assert_undefined_type assert_type; Chris@16: BOOST_STATIC_ASSERT(assert_type::value == 0); Chris@16: Chris@16: return policies::raise_domain_error( Chris@16: "boost::math::skewness(cauchy<%1%>&)", Chris@16: "The Cauchy distribution does not have a skewness: " Chris@16: "the only possible return value is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), Policy()); // infinity? Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis(const cauchy_distribution& /*dist*/) Chris@16: { Chris@16: // There is no kurtosis: Chris@16: typedef typename Policy::assert_undefined_type assert_type; Chris@16: BOOST_STATIC_ASSERT(assert_type::value == 0); Chris@16: Chris@16: return policies::raise_domain_error( Chris@16: "boost::math::kurtosis(cauchy<%1%>&)", Chris@16: "The Cauchy distribution does not have a kurtosis: " Chris@16: "the only possible return value is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), Policy()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline RealType kurtosis_excess(const cauchy_distribution& /*dist*/) Chris@16: { Chris@16: // There is no kurtosis excess: Chris@16: typedef typename Policy::assert_undefined_type assert_type; Chris@16: BOOST_STATIC_ASSERT(assert_type::value == 0); Chris@16: Chris@16: return policies::raise_domain_error( Chris@16: "boost::math::kurtosis_excess(cauchy<%1%>&)", Chris@16: "The Cauchy distribution does not have a kurtosis: " Chris@16: "the only possible return value is %1%.", Chris@16: std::numeric_limits::quiet_NaN(), Policy()); Chris@16: } Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: // This include must be at the end, *after* the accessors Chris@16: // for this distribution have been defined, in order to Chris@16: // keep compilers that support two-phase lookup happy. Chris@16: #include Chris@16: Chris@16: #endif // BOOST_STATS_CAUCHY_HPP