rayleigh.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright Paul A. Bristow 2007.
  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_rayleigh_HPP
  6. #define BOOST_STATS_rayleigh_HPP
  7. #include <boost/math/distributions/fwd.hpp>
  8. #include <boost/math/constants/constants.hpp>
  9. #include <boost/math/special_functions/log1p.hpp>
  10. #include <boost/math/special_functions/expm1.hpp>
  11. #include <boost/math/distributions/complement.hpp>
  12. #include <boost/math/distributions/detail/common_error_handling.hpp>
  13. #include <boost/config/no_tr1/cmath.hpp>
  14. #ifdef BOOST_MSVC
  15. # pragma warning(push)
  16. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  17. #endif
  18. #include <utility>
  19. namespace boost{ namespace math{
  20. namespace detail
  21. { // Error checks:
  22. template <class RealType, class Policy>
  23. inline bool verify_sigma(const char* function, RealType sigma, RealType* presult, const Policy& pol)
  24. {
  25. if((sigma <= 0) || (!(boost::math::isfinite)(sigma)))
  26. {
  27. *presult = policies::raise_domain_error<RealType>(
  28. function,
  29. "The scale parameter \"sigma\" must be > 0 and finite, but was: %1%.", sigma, pol);
  30. return false;
  31. }
  32. return true;
  33. } // bool verify_sigma
  34. template <class RealType, class Policy>
  35. inline bool verify_rayleigh_x(const char* function, RealType x, RealType* presult, const Policy& pol)
  36. {
  37. if((x < 0) || (boost::math::isnan)(x))
  38. {
  39. *presult = policies::raise_domain_error<RealType>(
  40. function,
  41. "The random variable must be >= 0, but was: %1%.", x, pol);
  42. return false;
  43. }
  44. return true;
  45. } // bool verify_rayleigh_x
  46. } // namespace detail
  47. template <class RealType = double, class Policy = policies::policy<> >
  48. class rayleigh_distribution
  49. {
  50. public:
  51. typedef RealType value_type;
  52. typedef Policy policy_type;
  53. rayleigh_distribution(RealType l_sigma = 1)
  54. : m_sigma(l_sigma)
  55. {
  56. RealType err;
  57. detail::verify_sigma("boost::math::rayleigh_distribution<%1%>::rayleigh_distribution", l_sigma, &err, Policy());
  58. } // rayleigh_distribution
  59. RealType sigma()const
  60. { // Accessor.
  61. return m_sigma;
  62. }
  63. private:
  64. RealType m_sigma;
  65. }; // class rayleigh_distribution
  66. typedef rayleigh_distribution<double> rayleigh;
  67. template <class RealType, class Policy>
  68. inline const std::pair<RealType, RealType> range(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  69. { // Range of permissible values for random variable x.
  70. using boost::math::tools::max_value;
  71. return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
  72. }
  73. template <class RealType, class Policy>
  74. inline const std::pair<RealType, RealType> support(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  75. { // Range of supported values for random variable x.
  76. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  77. using boost::math::tools::max_value;
  78. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  79. }
  80. template <class RealType, class Policy>
  81. inline RealType pdf(const rayleigh_distribution<RealType, Policy>& dist, const RealType& x)
  82. {
  83. BOOST_MATH_STD_USING // for ADL of std function exp.
  84. RealType sigma = dist.sigma();
  85. RealType result = 0;
  86. static const char* function = "boost::math::pdf(const rayleigh_distribution<%1%>&, %1%)";
  87. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  88. {
  89. return result;
  90. }
  91. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  92. {
  93. return result;
  94. }
  95. if((boost::math::isinf)(x))
  96. {
  97. return 0;
  98. }
  99. RealType sigmasqr = sigma * sigma;
  100. result = x * (exp(-(x * x) / ( 2 * sigmasqr))) / sigmasqr;
  101. return result;
  102. } // pdf
  103. template <class RealType, class Policy>
  104. inline RealType cdf(const rayleigh_distribution<RealType, Policy>& dist, const RealType& x)
  105. {
  106. BOOST_MATH_STD_USING // for ADL of std functions
  107. RealType result = 0;
  108. RealType sigma = dist.sigma();
  109. static const char* function = "boost::math::cdf(const rayleigh_distribution<%1%>&, %1%)";
  110. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  111. {
  112. return result;
  113. }
  114. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  115. {
  116. return result;
  117. }
  118. result = -boost::math::expm1(-x * x / ( 2 * sigma * sigma), Policy());
  119. return result;
  120. } // cdf
  121. template <class RealType, class Policy>
  122. inline RealType quantile(const rayleigh_distribution<RealType, Policy>& dist, const RealType& p)
  123. {
  124. BOOST_MATH_STD_USING // for ADL of std functions
  125. RealType result = 0;
  126. RealType sigma = dist.sigma();
  127. static const char* function = "boost::math::quantile(const rayleigh_distribution<%1%>&, %1%)";
  128. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  129. return result;
  130. if(false == detail::check_probability(function, p, &result, Policy()))
  131. return result;
  132. if(p == 0)
  133. {
  134. return 0;
  135. }
  136. if(p == 1)
  137. {
  138. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  139. }
  140. result = sqrt(-2 * sigma * sigma * boost::math::log1p(-p, Policy()));
  141. return result;
  142. } // quantile
  143. template <class RealType, class Policy>
  144. inline RealType cdf(const complemented2_type<rayleigh_distribution<RealType, Policy>, RealType>& c)
  145. {
  146. BOOST_MATH_STD_USING // for ADL of std functions
  147. RealType result = 0;
  148. RealType sigma = c.dist.sigma();
  149. static const char* function = "boost::math::cdf(const rayleigh_distribution<%1%>&, %1%)";
  150. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  151. {
  152. return result;
  153. }
  154. RealType x = c.param;
  155. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  156. {
  157. return result;
  158. }
  159. RealType ea = x * x / (2 * sigma * sigma);
  160. // Fix for VC11/12 x64 bug in exp(float):
  161. if (ea >= tools::max_value<RealType>())
  162. return 0;
  163. result = exp(-ea);
  164. return result;
  165. } // cdf complement
  166. template <class RealType, class Policy>
  167. inline RealType quantile(const complemented2_type<rayleigh_distribution<RealType, Policy>, RealType>& c)
  168. {
  169. BOOST_MATH_STD_USING // for ADL of std functions, log & sqrt.
  170. RealType result = 0;
  171. RealType sigma = c.dist.sigma();
  172. static const char* function = "boost::math::quantile(const rayleigh_distribution<%1%>&, %1%)";
  173. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  174. {
  175. return result;
  176. }
  177. RealType q = c.param;
  178. if(false == detail::check_probability(function, q, &result, Policy()))
  179. {
  180. return result;
  181. }
  182. if(q == 1)
  183. {
  184. return 0;
  185. }
  186. if(q == 0)
  187. {
  188. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  189. }
  190. result = sqrt(-2 * sigma * sigma * log(q));
  191. return result;
  192. } // quantile complement
  193. template <class RealType, class Policy>
  194. inline RealType mean(const rayleigh_distribution<RealType, Policy>& dist)
  195. {
  196. RealType result = 0;
  197. RealType sigma = dist.sigma();
  198. static const char* function = "boost::math::mean(const rayleigh_distribution<%1%>&, %1%)";
  199. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  200. {
  201. return result;
  202. }
  203. using boost::math::constants::root_half_pi;
  204. return sigma * root_half_pi<RealType>();
  205. } // mean
  206. template <class RealType, class Policy>
  207. inline RealType variance(const rayleigh_distribution<RealType, Policy>& dist)
  208. {
  209. RealType result = 0;
  210. RealType sigma = dist.sigma();
  211. static const char* function = "boost::math::variance(const rayleigh_distribution<%1%>&, %1%)";
  212. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  213. {
  214. return result;
  215. }
  216. using boost::math::constants::four_minus_pi;
  217. return four_minus_pi<RealType>() * sigma * sigma / 2;
  218. } // variance
  219. template <class RealType, class Policy>
  220. inline RealType mode(const rayleigh_distribution<RealType, Policy>& dist)
  221. {
  222. return dist.sigma();
  223. }
  224. template <class RealType, class Policy>
  225. inline RealType median(const rayleigh_distribution<RealType, Policy>& dist)
  226. {
  227. using boost::math::constants::root_ln_four;
  228. return root_ln_four<RealType>() * dist.sigma();
  229. }
  230. template <class RealType, class Policy>
  231. inline RealType skewness(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  232. {
  233. // using namespace boost::math::constants;
  234. return static_cast<RealType>(0.63111065781893713819189935154422777984404221106391L);
  235. // Computed using NTL at 150 bit, about 50 decimal digits.
  236. // return 2 * root_pi<RealType>() * pi_minus_three<RealType>() / pow23_four_minus_pi<RealType>();
  237. }
  238. template <class RealType, class Policy>
  239. inline RealType kurtosis(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  240. {
  241. // using namespace boost::math::constants;
  242. return static_cast<RealType>(3.2450893006876380628486604106197544154170667057995L);
  243. // Computed using NTL at 150 bit, about 50 decimal digits.
  244. // return 3 - (6 * pi<RealType>() * pi<RealType>() - 24 * pi<RealType>() + 16) /
  245. // (four_minus_pi<RealType>() * four_minus_pi<RealType>());
  246. }
  247. template <class RealType, class Policy>
  248. inline RealType kurtosis_excess(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  249. {
  250. //using namespace boost::math::constants;
  251. // Computed using NTL at 150 bit, about 50 decimal digits.
  252. return static_cast<RealType>(0.2450893006876380628486604106197544154170667057995L);
  253. // return -(6 * pi<RealType>() * pi<RealType>() - 24 * pi<RealType>() + 16) /
  254. // (four_minus_pi<RealType>() * four_minus_pi<RealType>());
  255. } // kurtosis
  256. } // namespace math
  257. } // namespace boost
  258. #ifdef BOOST_MSVC
  259. # pragma warning(pop)
  260. #endif
  261. // This include must be at the end, *after* the accessors
  262. // for this distribution have been defined, in order to
  263. // keep compilers that support two-phase lookup happy.
  264. #include <boost/math/distributions/detail/derived_accessors.hpp>
  265. #endif // BOOST_STATS_rayleigh_HPP