derived_accessors.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef BOOST_STATS_DERIVED_HPP
  7. #define BOOST_STATS_DERIVED_HPP
  8. // This file implements various common properties of distributions
  9. // that can be implemented in terms of other properties:
  10. // variance OR standard deviation (see note below),
  11. // hazard, cumulative hazard (chf), coefficient_of_variation.
  12. //
  13. // Note that while both variance and standard_deviation are provided
  14. // here, each distribution MUST SPECIALIZE AT LEAST ONE OF THESE
  15. // otherwise these two versions will just call each other over and over
  16. // until stack space runs out ...
  17. // Of course there may be more efficient means of implementing these
  18. // that are specific to a particular distribution, but these generic
  19. // versions give these properties "for free" with most distributions.
  20. //
  21. // In order to make use of this header, it must be included AT THE END
  22. // of the distribution header, AFTER the distribution and its core
  23. // property accessors have been defined: this is so that compilers
  24. // that implement 2-phase lookup and early-type-checking of templates
  25. // can find the definitions referred to herein.
  26. //
  27. #include <boost/math/tools/config.hpp>
  28. #include <boost/math/tools/assert.hpp>
  29. #ifndef BOOST_MATH_HAS_NVRTC
  30. #include <cmath>
  31. #endif
  32. #ifdef _MSC_VER
  33. # pragma warning(push)
  34. # pragma warning(disable: 4723) // potential divide by 0
  35. // Suppressing spurious warning in coefficient_of_variation
  36. #endif
  37. namespace boost{ namespace math{
  38. template <class Distribution>
  39. BOOST_MATH_GPU_ENABLED typename Distribution::value_type variance(const Distribution& dist);
  40. template <class Distribution>
  41. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type standard_deviation(const Distribution& dist)
  42. {
  43. BOOST_MATH_STD_USING // ADL of sqrt.
  44. return sqrt(variance(dist));
  45. }
  46. template <class Distribution>
  47. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type variance(const Distribution& dist)
  48. {
  49. typename Distribution::value_type result = standard_deviation(dist);
  50. return result * result;
  51. }
  52. template <class Distribution, class RealType>
  53. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type hazard(const Distribution& dist, const RealType& x)
  54. { // hazard function
  55. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ
  56. typedef typename Distribution::value_type value_type;
  57. typedef typename Distribution::policy_type policy_type;
  58. value_type p = cdf(complement(dist, x));
  59. value_type d = pdf(dist, x);
  60. if(d > p * tools::max_value<value_type>())
  61. return policies::raise_overflow_error<value_type>(
  62. "boost::math::hazard(const Distribution&, %1%)", nullptr, policy_type());
  63. if(d == 0)
  64. {
  65. // This protects against 0/0, but is it the right thing to do?
  66. return 0;
  67. }
  68. return d / p;
  69. }
  70. template <class Distribution, class RealType>
  71. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)
  72. { // cumulative hazard function.
  73. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda362.htm#HAZ
  74. BOOST_MATH_STD_USING
  75. return -log(cdf(complement(dist, x)));
  76. }
  77. template <class Distribution>
  78. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type coefficient_of_variation(const Distribution& dist)
  79. {
  80. typedef typename Distribution::value_type value_type;
  81. typedef typename Distribution::policy_type policy_type;
  82. using std::abs;
  83. value_type m = mean(dist);
  84. value_type d = standard_deviation(dist);
  85. if((abs(m) < 1) && (d > abs(m) * tools::max_value<value_type>()))
  86. { // Checks too that m is not zero,
  87. return policies::raise_overflow_error<value_type>("boost::math::coefficient_of_variation(const Distribution&, %1%)", nullptr, policy_type());
  88. }
  89. return d / m; // so MSVC warning on zerodivide is spurious, and suppressed.
  90. }
  91. //
  92. // Next follow overloads of some of the standard accessors with mixed
  93. // argument types. We just use a typecast to forward on to the "real"
  94. // implementation with all arguments of the same type:
  95. //
  96. template <class Distribution, class RealType>
  97. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type pdf(const Distribution& dist, const RealType& x)
  98. {
  99. typedef typename Distribution::value_type value_type;
  100. return pdf(dist, static_cast<value_type>(x));
  101. }
  102. template <class Distribution, class RealType>
  103. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type logpdf(const Distribution& dist, const RealType& x)
  104. {
  105. using std::log;
  106. typedef typename Distribution::value_type value_type;
  107. return log(pdf(dist, static_cast<value_type>(x)));
  108. }
  109. template <class Distribution, class RealType>
  110. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type cdf(const Distribution& dist, const RealType& x)
  111. {
  112. typedef typename Distribution::value_type value_type;
  113. return cdf(dist, static_cast<value_type>(x));
  114. }
  115. template <class Distribution, class Realtype>
  116. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type logcdf(const Distribution& dist, const Realtype& x)
  117. {
  118. using std::log;
  119. using value_type = typename Distribution::value_type;
  120. return log(cdf(dist, static_cast<value_type>(x)));
  121. }
  122. template <class Distribution, class RealType>
  123. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type quantile(const Distribution& dist, const RealType& x)
  124. {
  125. typedef typename Distribution::value_type value_type;
  126. return quantile(dist, static_cast<value_type>(x));
  127. }
  128. /*
  129. template <class Distribution, class RealType>
  130. inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)
  131. {
  132. typedef typename Distribution::value_type value_type;
  133. return chf(dist, static_cast<value_type>(x));
  134. }
  135. */
  136. template <class Distribution, class RealType>
  137. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type cdf(const complemented2_type<Distribution, RealType>& c)
  138. {
  139. typedef typename Distribution::value_type value_type;
  140. return cdf(complement(c.dist, static_cast<value_type>(c.param)));
  141. }
  142. template <class Distribution, class RealType>
  143. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type logcdf(const complemented2_type<Distribution, RealType>& c)
  144. {
  145. using std::log;
  146. typedef typename Distribution::value_type value_type;
  147. return log(cdf(complement(c.dist, static_cast<value_type>(c.param))));
  148. }
  149. template <class Distribution, class RealType>
  150. BOOST_MATH_GPU_ENABLED inline typename Distribution::value_type quantile(const complemented2_type<Distribution, RealType>& c)
  151. {
  152. typedef typename Distribution::value_type value_type;
  153. return quantile(complement(c.dist, static_cast<value_type>(c.param)));
  154. }
  155. template <class Dist>
  156. BOOST_MATH_GPU_ENABLED inline typename Dist::value_type median(const Dist& d)
  157. { // median - default definition for those distributions for which a
  158. // simple closed form is not known,
  159. // and for which a domain_error and/or NaN generating function is NOT defined.
  160. typedef typename Dist::value_type value_type;
  161. return quantile(d, static_cast<value_type>(0.5f));
  162. }
  163. } // namespace math
  164. } // namespace boost
  165. #ifdef _MSC_VER
  166. # pragma warning(pop)
  167. #endif
  168. #endif // BOOST_STATS_DERIVED_HPP