find_scale.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_SCALE_HPP
  7. #define BOOST_STATS_FIND_SCALE_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. // using boost::math::policies::policy;
  12. #include <boost/math/tools/traits.hpp>
  13. #include <boost/math/tools/assert.hpp>
  14. #include <boost/math/special_functions/fpclassify.hpp>
  15. #include <boost/math/policies/error_handling.hpp>
  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. // distributions that have scale.
  26. // BOOST_STATIC_ASSERTs, see below, are used to enforce this.
  27. template <class Dist, class Policy>
  28. inline
  29. typename Dist::value_type find_scale( // For example, normal mean.
  30. typename Dist::value_type z, // location of random variable z to give probability, P(X > z) == p.
  31. // For example, a nominal minimum acceptable weight z, so that p * 100 % are > z
  32. typename Dist::value_type p, // probability value desired at x, say 0.95 for 95% > z.
  33. typename Dist::value_type location, // location parameter, for example, normal distribution mean.
  34. const Policy& pol
  35. )
  36. {
  37. static_assert(::boost::math::tools::is_distribution<Dist>::value, "The provided distribution does not meet the conceptual requirements of a distribution.");
  38. static_assert(::boost::math::tools::is_scaled_distribution<Dist>::value, "The provided distribution does not meet the conceptual requirements of a scaled distribution.");
  39. static const char* function = "boost::math::find_scale<Dist, Policy>(%1%, %1%, %1%, Policy)";
  40. if(!(boost::math::isfinite)(p) || (p < 0) || (p > 1))
  41. {
  42. return policies::raise_domain_error<typename Dist::value_type>(
  43. function, "Probability parameter was %1%, but must be >= 0 and <= 1!", p, pol);
  44. }
  45. if(!(boost::math::isfinite)(z))
  46. {
  47. return policies::raise_domain_error<typename Dist::value_type>(
  48. function, "find_scale z parameter was %1%, but must be finite!", z, pol);
  49. }
  50. if(!(boost::math::isfinite)(location))
  51. {
  52. return policies::raise_domain_error<typename Dist::value_type>(
  53. function, "find_scale location parameter was %1%, but must be finite!", location, pol);
  54. }
  55. //cout << "z " << z << ", p " << p << ", quantile(Dist(), p) "
  56. //<< quantile(Dist(), p) << ", z - mean " << z - location
  57. //<<", sd " << (z - location) / quantile(Dist(), p) << endl;
  58. //quantile(N01, 0.001) -3.09023
  59. //quantile(N01, 0.01) -2.32635
  60. //quantile(N01, 0.05) -1.64485
  61. //quantile(N01, 0.333333) -0.430728
  62. //quantile(N01, 0.5) 0
  63. //quantile(N01, 0.666667) 0.430728
  64. //quantile(N01, 0.9) 1.28155
  65. //quantile(N01, 0.95) 1.64485
  66. //quantile(N01, 0.99) 2.32635
  67. //quantile(N01, 0.999) 3.09023
  68. typename Dist::value_type result =
  69. (z - location) // difference between desired x and current location.
  70. / quantile(Dist(), p); // standard distribution.
  71. if (result <= 0)
  72. { // If policy isn't to throw, return the scale <= 0.
  73. policies::raise_evaluation_error<typename Dist::value_type>(function, "Computed scale (%1%) is <= 0!" " Was the complement intended?", result, Policy()); // LCOV_EXCL_LINE
  74. }
  75. return result;
  76. } // template <class Dist, class Policy> find_scale
  77. template <class Dist>
  78. inline // with default policy.
  79. typename Dist::value_type find_scale( // For example, normal mean.
  80. typename Dist::value_type z, // location of random variable z to give probability, P(X > z) == p.
  81. // For example, a nominal minimum acceptable z, so that p * 100 % are > z
  82. typename Dist::value_type p, // probability value desired at x, say 0.95 for 95% > z.
  83. typename Dist::value_type location) // location parameter, for example, mean.
  84. { // Forward to find_scale using the default policy.
  85. return (find_scale<Dist>(z, p, location, policies::policy<>()));
  86. } // find_scale
  87. template <class Dist, class Real1, class Real2, class Real3, class Policy>
  88. inline typename Dist::value_type find_scale(
  89. complemented4_type<Real1, Real2, Real3, Policy> const& c)
  90. {
  91. //cout << "cparam1 q " << c.param1 // q
  92. // << ", c.dist z " << c.dist // z
  93. // << ", c.param2 l " << c.param2 // l
  94. // << ", quantile (Dist(), c.param1 = q) "
  95. // << quantile(Dist(), c.param1) //q
  96. // << endl;
  97. static_assert(::boost::math::tools::is_distribution<Dist>::value, "The provided distribution does not meet the conceptual requirements of a distribution.");
  98. static_assert(::boost::math::tools::is_scaled_distribution<Dist>::value, "The provided distribution does not meet the conceptual requirements of a scaled distribution.");
  99. static const char* function = "boost::math::find_scale<Dist, Policy>(complement(%1%, %1%, %1%, Policy))";
  100. // Checks on arguments, as not complemented version,
  101. // Explicit policy.
  102. typename Dist::value_type q = c.param1;
  103. if(!(boost::math::isfinite)(q) || (q < 0) || (q > 1))
  104. {
  105. return policies::raise_domain_error<typename Dist::value_type>(
  106. function, "Probability parameter was %1%, but must be >= 0 and <= 1!", q, c.param3);
  107. }
  108. typename Dist::value_type z = c.dist;
  109. if(!(boost::math::isfinite)(z))
  110. {
  111. return policies::raise_domain_error<typename Dist::value_type>(
  112. function, "find_scale z parameter was %1%, but must be finite!", z, c.param3);
  113. }
  114. typename Dist::value_type location = c.param2;
  115. if(!(boost::math::isfinite)(location))
  116. {
  117. return policies::raise_domain_error<typename Dist::value_type>(
  118. function, "find_scale location parameter was %1%, but must be finite!", location, c.param3);
  119. }
  120. typename Dist::value_type result =
  121. (c.dist - c.param2) // difference between desired x and current location.
  122. / quantile(complement(Dist(), c.param1));
  123. // ( z - location) / (quantile(complement(Dist(), q))
  124. if (result <= 0)
  125. { // If policy isn't to throw, return the scale <= 0.
  126. policies::raise_evaluation_error<typename Dist::value_type>(function, "Computed scale (%1%) is <= 0!" " Was the complement intended?", result, Policy()); // LCOV_EXCL_LINE
  127. }
  128. return result;
  129. } // template <class Dist, class Policy, class Real1, class Real2, class Real3> typename Dist::value_type find_scale
  130. // So the user can start from the complement q = (1 - p) of the probability p,
  131. // for example, s = find_scale<normal>(complement(z, q, l));
  132. template <class Dist, class Real1, class Real2, class Real3>
  133. inline typename Dist::value_type find_scale(
  134. complemented3_type<Real1, Real2, Real3> const& c)
  135. {
  136. //cout << "cparam1 q " << c.param1 // q
  137. // << ", c.dist z " << c.dist // z
  138. // << ", c.param2 l " << c.param2 // l
  139. // << ", quantile (Dist(), c.param1 = q) "
  140. // << quantile(Dist(), c.param1) //q
  141. // << endl;
  142. static_assert(::boost::math::tools::is_distribution<Dist>::value, "The provided distribution does not meet the conceptual requirements of a distribution.");
  143. static_assert(::boost::math::tools::is_scaled_distribution<Dist>::value, "The provided distribution does not meet the conceptual requirements of a scaled distribution.");
  144. static const char* function = "boost::math::find_scale<Dist, Policy>(complement(%1%, %1%, %1%, Policy))";
  145. // Checks on arguments, as not complemented version,
  146. // default policy policies::policy<>().
  147. typename Dist::value_type q = c.param1;
  148. if(!(boost::math::isfinite)(q) || (q < 0) || (q > 1))
  149. {
  150. return policies::raise_domain_error<typename Dist::value_type>(
  151. function, "Probability parameter was %1%, but must be >= 0 and <= 1!", q, policies::policy<>());
  152. }
  153. typename Dist::value_type z = c.dist;
  154. if(!(boost::math::isfinite)(z))
  155. {
  156. return policies::raise_domain_error<typename Dist::value_type>(
  157. function, "find_scale z parameter was %1%, but must be finite!", z, policies::policy<>());
  158. }
  159. typename Dist::value_type location = c.param2;
  160. if(!(boost::math::isfinite)(location))
  161. {
  162. return policies::raise_domain_error<typename Dist::value_type>(
  163. function, "find_scale location parameter was %1%, but must be finite!", location, policies::policy<>());
  164. }
  165. typename Dist::value_type result =
  166. (z - location) // difference between desired x and current location.
  167. / quantile(complement(Dist(), q));
  168. // ( z - location) / (quantile(complement(Dist(), q))
  169. if (result <= 0)
  170. { // If policy isn't to throw, return the scale <= 0.
  171. policies::raise_evaluation_error<typename Dist::value_type>(function, "Computed scale (%1%) is <= 0!" " Was the complement intended?", // LCOV_EXCL_LINE
  172. result, policies::policy<>()); // This is only the default policy - also Want a version with Policy here. LCOV_EXCL_LINE
  173. }
  174. return result;
  175. } // template <class Dist, class Real1, class Real2, class Real3> typename Dist::value_type find_scale
  176. } // namespace boost
  177. } // namespace math
  178. #endif // BOOST_STATS_FIND_SCALE_HPP