find_location.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright John Maddock 2007.
  2. // Copyright Paul A. Bristow 2007.
  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_FIND_LOCATION_HPP
  7. #define BOOST_STATS_FIND_LOCATION_HPP
  8. #include <boost/math/distributions/fwd.hpp> // for all distribution signatures.
  9. #include <boost/math/distributions/complement.hpp>
  10. #include <boost/math/policies/policy.hpp>
  11. #include <boost/math/tools/traits.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. // using boost::math::policies::policy;
  16. // using boost::math::complement; // will be needed by users who want complement,
  17. // but NOT placed here to avoid putting it in global scope.
  18. namespace boost
  19. {
  20. namespace math
  21. {
  22. // Function to find location of random variable z
  23. // to give probability p (given scale)
  24. // Applies to normal, lognormal, extreme value, Cauchy, (and symmetrical triangular),
  25. // enforced by BOOST_STATIC_ASSERT below.
  26. template <class Dist, class Policy>
  27. inline
  28. typename Dist::value_type find_location( // For example, normal mean.
  29. typename Dist::value_type z, // location of random variable z to give probability, P(X > z) == p.
  30. // For example, a nominal minimum acceptable z, so that p * 100 % are > z
  31. typename Dist::value_type p, // probability value desired at x, say 0.95 for 95% > z.
  32. typename Dist::value_type scale, // scale parameter, for example, normal standard deviation.
  33. const Policy& pol
  34. )
  35. {
  36. #if !defined(BOOST_NO_SFINAE) && !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  37. // Will fail to compile here if try to use with a distribution without scale & location,
  38. // for example pareto, and many others. These tests are disabled by the pp-logic
  39. // above if the compiler doesn't support the SFINAE tricks used in the traits class.
  40. BOOST_STATIC_ASSERT(::boost::math::tools::is_distribution<Dist>::value);
  41. BOOST_STATIC_ASSERT(::boost::math::tools::is_scaled_distribution<Dist>::value);
  42. #endif
  43. static const char* function = "boost::math::find_location<Dist, Policy>&, %1%)";
  44. if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
  45. {
  46. return policies::raise_domain_error<typename Dist::value_type>(
  47. function, "Probability parameter was %1%, but must be >= 0 and <= 1!", p, pol);
  48. }
  49. if(!(boost::math::isfinite)(z))
  50. {
  51. return policies::raise_domain_error<typename Dist::value_type>(
  52. function, "z parameter was %1%, but must be finite!", z, pol);
  53. }
  54. if(!(boost::math::isfinite)(scale))
  55. {
  56. return policies::raise_domain_error<typename Dist::value_type>(
  57. function, "scale parameter was %1%, but must be finite!", scale, pol);
  58. }
  59. //cout << "z " << z << ", p " << p << ", quantile(Dist(), p) "
  60. // << quantile(Dist(), p) << ", quan * scale " << quantile(Dist(), p) * scale << endl;
  61. return z - (quantile(Dist(), p) * scale);
  62. } // find_location
  63. template <class Dist>
  64. inline // with default policy.
  65. typename Dist::value_type find_location( // For example, normal mean.
  66. typename Dist::value_type z, // location of random variable z to give probability, P(X > z) == p.
  67. // For example, a nominal minimum acceptable z, so that p * 100 % are > z
  68. typename Dist::value_type p, // probability value desired at x, say 0.95 for 95% > z.
  69. typename Dist::value_type scale) // scale parameter, for example, normal standard deviation.
  70. { // Forward to find_location with default policy.
  71. return (find_location<Dist>(z, p, scale, policies::policy<>()));
  72. } // find_location
  73. // So the user can start from the complement q = (1 - p) of the probability p,
  74. // for example, l = find_location<normal>(complement(z, q, sd));
  75. template <class Dist, class Real1, class Real2, class Real3>
  76. inline typename Dist::value_type find_location( // Default policy.
  77. complemented3_type<Real1, Real2, Real3> const& c)
  78. {
  79. static const char* function = "boost::math::find_location<Dist, Policy>&, %1%)";
  80. typename Dist::value_type p = c.param1;
  81. if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
  82. {
  83. return policies::raise_domain_error<typename Dist::value_type>(
  84. function, "Probability parameter was %1%, but must be >= 0 and <= 1!", p, policies::policy<>());
  85. }
  86. typename Dist::value_type z = c.dist;
  87. if(!(boost::math::isfinite)(z))
  88. {
  89. return policies::raise_domain_error<typename Dist::value_type>(
  90. function, "z parameter was %1%, but must be finite!", z, policies::policy<>());
  91. }
  92. typename Dist::value_type scale = c.param2;
  93. if(!(boost::math::isfinite)(scale))
  94. {
  95. return policies::raise_domain_error<typename Dist::value_type>(
  96. function, "scale parameter was %1%, but must be finite!", scale, policies::policy<>());
  97. }
  98. // cout << "z " << c.dist << ", quantile (Dist(), " << c.param1 << ") * scale " << c.param2 << endl;
  99. return z - quantile(Dist(), p) * scale;
  100. } // find_location complement
  101. template <class Dist, class Real1, class Real2, class Real3, class Real4>
  102. inline typename Dist::value_type find_location( // Explicit policy.
  103. complemented4_type<Real1, Real2, Real3, Real4> const& c)
  104. {
  105. static const char* function = "boost::math::find_location<Dist, Policy>&, %1%)";
  106. typename Dist::value_type p = c.param1;
  107. if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
  108. {
  109. return policies::raise_domain_error<typename Dist::value_type>(
  110. function, "Probability parameter was %1%, but must be >= 0 and <= 1!", p, c.param3);
  111. }
  112. typename Dist::value_type z = c.dist;
  113. if(!(boost::math::isfinite)(z))
  114. {
  115. return policies::raise_domain_error<typename Dist::value_type>(
  116. function, "z parameter was %1%, but must be finite!", z, c.param3);
  117. }
  118. typename Dist::value_type scale = c.param2;
  119. if(!(boost::math::isfinite)(scale))
  120. {
  121. return policies::raise_domain_error<typename Dist::value_type>(
  122. function, "scale parameter was %1%, but must be finite!", scale, c.param3);
  123. }
  124. // cout << "z " << c.dist << ", quantile (Dist(), " << c.param1 << ") * scale " << c.param2 << endl;
  125. return z - quantile(Dist(), p) * scale;
  126. } // find_location complement
  127. } // namespace boost
  128. } // namespace math
  129. #endif // BOOST_STATS_FIND_LOCATION_HPP