generic_mode.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright John Maddock 2008.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_DISTRIBUTIONS_DETAIL_MODE_HPP
  7. #define BOOST_MATH_DISTRIBUTIONS_DETAIL_MODE_HPP
  8. #include <boost/math/tools/minima.hpp> // function minimization for mode
  9. #include <boost/math/policies/error_handling.hpp>
  10. #include <boost/math/distributions/fwd.hpp>
  11. namespace boost{ namespace math{ namespace detail{
  12. template <class Dist>
  13. struct pdf_minimizer
  14. {
  15. pdf_minimizer(const Dist& d)
  16. : dist(d) {}
  17. typename Dist::value_type operator()(const typename Dist::value_type& x)
  18. {
  19. return -pdf(dist, x);
  20. }
  21. private:
  22. Dist dist;
  23. };
  24. template <class Dist>
  25. typename Dist::value_type generic_find_mode(const Dist& dist, typename Dist::value_type guess, const char* function, typename Dist::value_type step = 0)
  26. {
  27. BOOST_MATH_STD_USING
  28. typedef typename Dist::value_type value_type;
  29. typedef typename Dist::policy_type policy_type;
  30. //
  31. // Need to begin by bracketing the maxima of the PDF:
  32. //
  33. value_type maxval;
  34. value_type upper_bound = guess;
  35. value_type lower_bound;
  36. value_type v = pdf(dist, guess);
  37. if(v == 0)
  38. {
  39. //
  40. // Oops we don't know how to handle this, or even in which
  41. // direction we should move in, treat as an evaluation error:
  42. //
  43. return policies::raise_evaluation_error(
  44. function,
  45. "Could not locate a starting location for the search for the mode, original guess was %1%", guess, policy_type());
  46. }
  47. do
  48. {
  49. maxval = v;
  50. if(step != 0)
  51. upper_bound += step;
  52. else
  53. upper_bound *= 2;
  54. v = pdf(dist, upper_bound);
  55. }while(maxval < v);
  56. lower_bound = upper_bound;
  57. do
  58. {
  59. maxval = v;
  60. if(step != 0)
  61. lower_bound -= step;
  62. else
  63. lower_bound /= 2;
  64. v = pdf(dist, lower_bound);
  65. }while(maxval < v);
  66. boost::uintmax_t max_iter = policies::get_max_root_iterations<policy_type>();
  67. value_type result = tools::brent_find_minima(
  68. pdf_minimizer<Dist>(dist),
  69. lower_bound,
  70. upper_bound,
  71. policies::digits<value_type, policy_type>(),
  72. max_iter).first;
  73. if(max_iter >= policies::get_max_root_iterations<policy_type>())
  74. {
  75. return policies::raise_evaluation_error<value_type>(
  76. function,
  77. "Unable to locate solution in a reasonable time:"
  78. " either there is no answer to the mode of the distribution"
  79. " or the answer is infinite. Current best guess is %1%", result, policy_type());
  80. }
  81. return result;
  82. }
  83. //
  84. // As above,but confined to the interval [0,1]:
  85. //
  86. template <class Dist>
  87. typename Dist::value_type generic_find_mode_01(const Dist& dist, typename Dist::value_type guess, const char* function)
  88. {
  89. BOOST_MATH_STD_USING
  90. typedef typename Dist::value_type value_type;
  91. typedef typename Dist::policy_type policy_type;
  92. //
  93. // Need to begin by bracketing the maxima of the PDF:
  94. //
  95. value_type maxval;
  96. value_type upper_bound = guess;
  97. value_type lower_bound;
  98. value_type v = pdf(dist, guess);
  99. do
  100. {
  101. maxval = v;
  102. upper_bound = 1 - (1 - upper_bound) / 2;
  103. if(upper_bound == 1)
  104. return 1;
  105. v = pdf(dist, upper_bound);
  106. }while(maxval < v);
  107. lower_bound = upper_bound;
  108. do
  109. {
  110. maxval = v;
  111. lower_bound /= 2;
  112. if(lower_bound < tools::min_value<value_type>())
  113. return 0;
  114. v = pdf(dist, lower_bound);
  115. }while(maxval < v);
  116. boost::uintmax_t max_iter = policies::get_max_root_iterations<policy_type>();
  117. value_type result = tools::brent_find_minima(
  118. pdf_minimizer<Dist>(dist),
  119. lower_bound,
  120. upper_bound,
  121. policies::digits<value_type, policy_type>(),
  122. max_iter).first;
  123. if(max_iter >= policies::get_max_root_iterations<policy_type>())
  124. {
  125. return policies::raise_evaluation_error<value_type>(
  126. function,
  127. "Unable to locate solution in a reasonable time:"
  128. " either there is no answer to the mode of the distribution"
  129. " or the answer is infinite. Current best guess is %1%", result, policy_type());
  130. }
  131. return result;
  132. }
  133. }}} // namespaces
  134. #endif // BOOST_MATH_DISTRIBUTIONS_DETAIL_MODE_HPP