brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.2 KiB · 90679ef Raw
191 lines · plain
1//  Copyright John Maddock 2006.2//  Copyright Matt Borland 2024.3//  Use, modification and distribution are subject to the4//  Boost Software License, Version 1.0. (See accompanying file5//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)6 7#ifndef BOOST_STATS_DERIVED_HPP8#define BOOST_STATS_DERIVED_HPP9 10// This file implements various common properties of distributions11// that can be implemented in terms of other properties:12// variance OR standard deviation (see note below),13// hazard, cumulative hazard (chf), coefficient_of_variation.14//15// Note that while both variance and standard_deviation are provided16// here, each distribution MUST SPECIALIZE AT LEAST ONE OF THESE17// otherwise these two versions will just call each other over and over18// until stack space runs out ...19 20// Of course there may be more efficient means of implementing these21// that are specific to a particular distribution, but these generic22// versions give these properties "for free" with most distributions.23//24// In order to make use of this header, it must be included AT THE END25// of the distribution header, AFTER the distribution and its core26// property accessors have been defined: this is so that compilers27// that implement 2-phase lookup and early-type-checking of templates28// can find the definitions referred to herein.29//30 31#include <boost/math/tools/config.hpp>32#include <boost/math/tools/assert.hpp>33 34#ifndef BOOST_MATH_HAS_NVRTC35#include <cmath>36#endif37 38#ifdef _MSC_VER39# pragma warning(push)40# pragma warning(disable: 4723) // potential divide by 041// Suppressing spurious warning in coefficient_of_variation42#endif43 44namespace boost{ namespace math{45 46template <class Distribution>47BOOST_MATH_GPU_ENABLED typename Distribution::value_type variance(const Distribution& dist);48 49template <class Distribution>50BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type standard_deviation(const Distribution& dist)51{52   BOOST_MATH_STD_USING  // ADL of sqrt.53   return sqrt(variance(dist));54}55 56template <class Distribution>57BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type variance(const Distribution& dist)58{59   typename Distribution::value_type result = standard_deviation(dist);60   return result * result;61}62 63template <class Distribution, class RealType>64BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type hazard(const Distribution& dist, const RealType& x)65{ // hazard function66  // http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ67   typedef typename Distribution::value_type value_type;68   typedef typename Distribution::policy_type policy_type;69   value_type p = cdf(complement(dist, x));70   value_type d = pdf(dist, x);71   if(d > p * tools::max_value<value_type>())72      return policies::raise_overflow_error<value_type>(73      "boost::math::hazard(const Distribution&, %1%)", nullptr, policy_type());74   if(d == 0)75   {76      // This protects against 0/0, but is it the right thing to do?77      return 0;78   }79   return d / p;80}81 82template <class Distribution, class RealType>83BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)84{ // cumulative hazard function.85  // http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ86   BOOST_MATH_STD_USING87   return -log(cdf(complement(dist, x)));88}89 90template <class Distribution>91BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type coefficient_of_variation(const Distribution& dist)92{93   typedef typename Distribution::value_type value_type;94   typedef typename Distribution::policy_type policy_type;95 96   using std::abs;97 98   value_type m = mean(dist);99   value_type d = standard_deviation(dist);100   if((abs(m) < 1) && (d > abs(m) * tools::max_value<value_type>()))101   { // Checks too that m is not zero,102      return policies::raise_overflow_error<value_type>("boost::math::coefficient_of_variation(const Distribution&, %1%)", nullptr, policy_type());103   }104   return d / m; // so MSVC warning on zerodivide is spurious, and suppressed.105}106//107// Next follow overloads of some of the standard accessors with mixed108// argument types. We just use a typecast to forward on to the "real"109// implementation with all arguments of the same type:110//111template <class Distribution, class RealType>112BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type pdf(const Distribution& dist, const RealType& x)113{114   typedef typename Distribution::value_type value_type;115   return pdf(dist, static_cast<value_type>(x));116}117template <class Distribution, class RealType>118BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type logpdf(const Distribution& dist, const RealType& x)119{120   using std::log;121   typedef typename Distribution::value_type value_type;122   return log(pdf(dist, static_cast<value_type>(x)));123}124template <class Distribution, class RealType>125BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type cdf(const Distribution& dist, const RealType& x)126{127   typedef typename Distribution::value_type value_type;128   return cdf(dist, static_cast<value_type>(x));129}130template <class Distribution, class Realtype>131BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type logcdf(const Distribution& dist, const Realtype& x)132{133   using std::log;134   using value_type = typename Distribution::value_type;135   return log(cdf(dist, static_cast<value_type>(x)));136}137template <class Distribution, class RealType>138BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type quantile(const Distribution& dist, const RealType& x)139{140   typedef typename Distribution::value_type value_type;141   return quantile(dist, static_cast<value_type>(x));142}143/*144template <class Distribution, class RealType>145inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)146{147   typedef typename Distribution::value_type value_type;148   return chf(dist, static_cast<value_type>(x));149}150*/151template <class Distribution, class RealType>152BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type cdf(const complemented2_type<Distribution, RealType>& c)153{154   typedef typename Distribution::value_type value_type;155   return cdf(complement(c.dist, static_cast<value_type>(c.param)));156}157 158template <class Distribution, class RealType>159BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type logcdf(const complemented2_type<Distribution, RealType>& c)160{161   using std::log;162   typedef typename Distribution::value_type value_type;163   return log(cdf(complement(c.dist, static_cast<value_type>(c.param))));164}165 166template <class Distribution, class RealType>167BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type quantile(const complemented2_type<Distribution, RealType>& c)168{169   typedef typename Distribution::value_type value_type;170   return quantile(complement(c.dist, static_cast<value_type>(c.param)));171}172 173template <class Dist>174BOOST_MATH_GPU_ENABLED inline typename Dist::value_type median(const Dist& d)175{ // median - default definition for those distributions for which a176  // simple closed form is not known,177  // and for which a domain_error and/or NaN generating function is NOT defined.178  typedef typename Dist::value_type value_type;179  return quantile(d, static_cast<value_type>(0.5f));180}181 182} // namespace math183} // namespace boost184 185 186#ifdef _MSC_VER187# pragma warning(pop)188#endif189 190#endif // BOOST_STATS_DERIVED_HPP191