| 1 |
// Copyright John Maddock 2006. |
| 2 |
// Copyright Matt Borland 2024. |
| 3 |
// Use, modification and distribution are subject to the |
| 4 |
// Boost Software License, Version 1.0. (See accompanying file |
| 5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
#ifndef BOOST_STATS_DERIVED_HPP |
| 8 |
#define BOOST_STATS_DERIVED_HPP |
| 9 |
|
| 10 |
// This file implements various common properties of distributions |
| 11 |
// 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 provided |
| 16 |
// here, each distribution MUST SPECIALIZE AT LEAST ONE OF THESE |
| 17 |
// otherwise these two versions will just call each other over and over |
| 18 |
// until stack space runs out ... |
| 19 |
|
| 20 |
// Of course there may be more efficient means of implementing these |
| 21 |
// that are specific to a particular distribution, but these generic |
| 22 |
// 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 END |
| 25 |
// of the distribution header, AFTER the distribution and its core |
| 26 |
// property accessors have been defined: this is so that compilers |
| 27 |
// that implement 2-phase lookup and early-type-checking of templates |
| 28 |
// 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_NVRTC |
| 35 |
#include <cmath> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef _MSC_VER |
| 39 |
# pragma warning(push) |
| 40 |
# pragma warning(disable: 4723) // potential divide by 0 |
| 41 |
// Suppressing spurious warning in coefficient_of_variation |
| 42 |
#endif |
| 43 |
|
| 44 |
namespace boost{ namespace math{ |
| 45 |
|
| 46 |
template <class Distribution> |
| 47 |
BOOST_MATH_GPU_ENABLED typename Distribution::value_type variance(const Distribution& dist); |
| 48 |
|
| 49 |
template <class Distribution> |
| 50 |
BOOST_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 |
|
| 56 |
template <class Distribution> |
| 57 |
BOOST_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 |
|
| 63 |
template <class Distribution, class RealType> |
| 64 |
BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type hazard(const Distribution& dist, const RealType& x) |
| 65 |
{ // hazard function |
| 66 |
// http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ |
| 67 |
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 |
|
| 82 |
template <class Distribution, class RealType> |
| 83 |
BOOST_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#HAZ |
| 86 |
BOOST_MATH_STD_USING |
| 87 |
return -log(cdf(complement(dist, x))); |
| 88 |
} |
| 89 |
|
| 90 |
template <class Distribution> |
| 91 |
BOOST_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 mixed |
| 108 |
// argument types. We just use a typecast to forward on to the "real" |
| 109 |
// implementation with all arguments of the same type: |
| 110 |
// |
| 111 |
template <class Distribution, class RealType> |
| 112 |
BOOST_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 |
} |
| 117 |
template <class Distribution, class RealType> |
| 118 |
BOOST_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 |
} |
| 124 |
template <class Distribution, class RealType> |
| 125 |
BOOST_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 |
} |
| 130 |
template <class Distribution, class Realtype> |
| 131 |
BOOST_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 |
} |
| 137 |
template <class Distribution, class RealType> |
| 138 |
BOOST_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 |
/* |
| 144 |
template <class Distribution, class RealType> |
| 145 |
inline 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 |
*/ |
| 151 |
template <class Distribution, class RealType> |
| 152 |
BOOST_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 |
|
| 158 |
template <class Distribution, class RealType> |
| 159 |
BOOST_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 |
|
| 166 |
template <class Distribution, class RealType> |
| 167 |
BOOST_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 |
|
| 173 |
template <class Dist> |
| 174 |
BOOST_MATH_GPU_ENABLED inline typename Dist::value_type median(const Dist& d) |
| 175 |
{ // median - default definition for those distributions for which a |
| 176 |
// 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 math |
| 183 |
} // namespace boost |
| 184 |
|
| 185 |
|
| 186 |
#ifdef _MSC_VER |
| 187 |
# pragma warning(pop) |
| 188 |
#endif |
| 189 |
|
| 190 |
#endif // BOOST_STATS_DERIVED_HPP |