derived_accessors.hpp 6.7 KB

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