derived_accessors.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 refered to herein.
  25. //
  26. #include <boost/type_traits/is_same.hpp>
  27. #include <boost/static_assert.hpp>
  28. #ifdef BOOST_MSVC
  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%)", 0, 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%)", 0, 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 cdf(const Distribution& dist, const RealType& x)
  100. {
  101. typedef typename Distribution::value_type value_type;
  102. return cdf(dist, static_cast<value_type>(x));
  103. }
  104. template <class Distribution, class RealType>
  105. inline typename Distribution::value_type quantile(const Distribution& dist, const RealType& x)
  106. {
  107. typedef typename Distribution::value_type value_type;
  108. return quantile(dist, static_cast<value_type>(x));
  109. }
  110. /*
  111. template <class Distribution, class RealType>
  112. inline typename Distribution::value_type chf(const Distribution& dist, const RealType& x)
  113. {
  114. typedef typename Distribution::value_type value_type;
  115. return chf(dist, static_cast<value_type>(x));
  116. }
  117. */
  118. template <class Distribution, class RealType>
  119. inline typename Distribution::value_type cdf(const complemented2_type<Distribution, RealType>& c)
  120. {
  121. typedef typename Distribution::value_type value_type;
  122. return cdf(complement(c.dist, static_cast<value_type>(c.param)));
  123. }
  124. template <class Distribution, class RealType>
  125. inline typename Distribution::value_type quantile(const complemented2_type<Distribution, RealType>& c)
  126. {
  127. typedef typename Distribution::value_type value_type;
  128. return quantile(complement(c.dist, static_cast<value_type>(c.param)));
  129. }
  130. template <class Dist>
  131. inline typename Dist::value_type median(const Dist& d)
  132. { // median - default definition for those distributions for which a
  133. // simple closed form is not known,
  134. // and for which a domain_error and/or NaN generating function is NOT defined.
  135. typedef typename Dist::value_type value_type;
  136. return quantile(d, static_cast<value_type>(0.5f));
  137. }
  138. } // namespace math
  139. } // namespace boost
  140. #ifdef BOOST_MSVC
  141. # pragma warning(pop)
  142. #endif
  143. #endif // BOOST_STATS_DERIVED_HPP