rayleigh.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #ifdef _MSC_VER
  14. # pragma warning(push)
  15. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  16. #endif
  17. #include <utility>
  18. #include <cmath>
  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. using value_type = RealType;
  52. using policy_type = Policy;
  53. explicit 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. using rayleigh = rayleigh_distribution<double>;
  67. #ifdef __cpp_deduction_guides
  68. template <class RealType>
  69. rayleigh_distribution(RealType)->rayleigh_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  70. #endif
  71. template <class RealType, class Policy>
  72. inline std::pair<RealType, RealType> range(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  73. { // Range of permissible values for random variable x.
  74. using boost::math::tools::max_value;
  75. return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
  76. }
  77. template <class RealType, class Policy>
  78. inline std::pair<RealType, RealType> support(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  79. { // Range of supported values for random variable x.
  80. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  81. using boost::math::tools::max_value;
  82. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  83. }
  84. template <class RealType, class Policy>
  85. inline RealType pdf(const rayleigh_distribution<RealType, Policy>& dist, const RealType& x)
  86. {
  87. BOOST_MATH_STD_USING // for ADL of std function exp.
  88. RealType sigma = dist.sigma();
  89. RealType result = 0;
  90. static const char* function = "boost::math::pdf(const rayleigh_distribution<%1%>&, %1%)";
  91. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  92. {
  93. return result;
  94. }
  95. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  96. {
  97. return result;
  98. }
  99. if((boost::math::isinf)(x))
  100. {
  101. return 0;
  102. }
  103. RealType sigmasqr = sigma * sigma;
  104. result = x * (exp(-(x * x) / ( 2 * sigmasqr))) / sigmasqr;
  105. return result;
  106. } // pdf
  107. template <class RealType, class Policy>
  108. inline RealType logpdf(const rayleigh_distribution<RealType, Policy>& dist, const RealType& x)
  109. {
  110. BOOST_MATH_STD_USING // for ADL of std function exp.
  111. const RealType sigma = dist.sigma();
  112. RealType result = -std::numeric_limits<RealType>::infinity();
  113. static const char* function = "boost::math::logpdf(const rayleigh_distribution<%1%>&, %1%)";
  114. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  115. {
  116. return result;
  117. }
  118. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  119. {
  120. return result;
  121. }
  122. if((boost::math::isinf)(x))
  123. {
  124. return result;
  125. }
  126. result = -(x*x)/(2*sigma*sigma) - 2*log(sigma) + log(x);
  127. return result;
  128. } // logpdf
  129. template <class RealType, class Policy>
  130. inline RealType cdf(const rayleigh_distribution<RealType, Policy>& dist, const RealType& x)
  131. {
  132. BOOST_MATH_STD_USING // for ADL of std functions
  133. RealType result = 0;
  134. RealType sigma = dist.sigma();
  135. static const char* function = "boost::math::cdf(const rayleigh_distribution<%1%>&, %1%)";
  136. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  137. {
  138. return result;
  139. }
  140. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  141. {
  142. return result;
  143. }
  144. result = -boost::math::expm1(-x * x / ( 2 * sigma * sigma), Policy());
  145. return result;
  146. } // cdf
  147. template <class RealType, class Policy>
  148. inline RealType quantile(const rayleigh_distribution<RealType, Policy>& dist, const RealType& p)
  149. {
  150. BOOST_MATH_STD_USING // for ADL of std functions
  151. RealType result = 0;
  152. RealType sigma = dist.sigma();
  153. static const char* function = "boost::math::quantile(const rayleigh_distribution<%1%>&, %1%)";
  154. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  155. return result;
  156. if(false == detail::check_probability(function, p, &result, Policy()))
  157. return result;
  158. if(p == 0)
  159. {
  160. return 0;
  161. }
  162. if(p == 1)
  163. {
  164. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  165. }
  166. result = sqrt(-2 * sigma * sigma * boost::math::log1p(-p, Policy()));
  167. return result;
  168. } // quantile
  169. template <class RealType, class Policy>
  170. inline RealType cdf(const complemented2_type<rayleigh_distribution<RealType, Policy>, RealType>& c)
  171. {
  172. BOOST_MATH_STD_USING // for ADL of std functions
  173. RealType result = 0;
  174. RealType sigma = c.dist.sigma();
  175. static const char* function = "boost::math::cdf(const rayleigh_distribution<%1%>&, %1%)";
  176. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  177. {
  178. return result;
  179. }
  180. RealType x = c.param;
  181. if(false == detail::verify_rayleigh_x(function, x, &result, Policy()))
  182. {
  183. return result;
  184. }
  185. RealType ea = x * x / (2 * sigma * sigma);
  186. // Fix for VC11/12 x64 bug in exp(float):
  187. if (ea >= tools::max_value<RealType>())
  188. return 0;
  189. result = exp(-ea);
  190. return result;
  191. } // cdf complement
  192. template <class RealType, class Policy>
  193. inline RealType quantile(const complemented2_type<rayleigh_distribution<RealType, Policy>, RealType>& c)
  194. {
  195. BOOST_MATH_STD_USING // for ADL of std functions, log & sqrt.
  196. RealType result = 0;
  197. RealType sigma = c.dist.sigma();
  198. static const char* function = "boost::math::quantile(const rayleigh_distribution<%1%>&, %1%)";
  199. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  200. {
  201. return result;
  202. }
  203. RealType q = c.param;
  204. if(false == detail::check_probability(function, q, &result, Policy()))
  205. {
  206. return result;
  207. }
  208. if(q == 1)
  209. {
  210. return 0;
  211. }
  212. if(q == 0)
  213. {
  214. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  215. }
  216. result = sqrt(-2 * sigma * sigma * log(q));
  217. return result;
  218. } // quantile complement
  219. template <class RealType, class Policy>
  220. inline RealType mean(const rayleigh_distribution<RealType, Policy>& dist)
  221. {
  222. RealType result = 0;
  223. RealType sigma = dist.sigma();
  224. static const char* function = "boost::math::mean(const rayleigh_distribution<%1%>&, %1%)";
  225. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  226. {
  227. return result;
  228. }
  229. using boost::math::constants::root_half_pi;
  230. return sigma * root_half_pi<RealType>();
  231. } // mean
  232. template <class RealType, class Policy>
  233. inline RealType variance(const rayleigh_distribution<RealType, Policy>& dist)
  234. {
  235. RealType result = 0;
  236. RealType sigma = dist.sigma();
  237. static const char* function = "boost::math::variance(const rayleigh_distribution<%1%>&, %1%)";
  238. if(false == detail::verify_sigma(function, sigma, &result, Policy()))
  239. {
  240. return result;
  241. }
  242. using boost::math::constants::four_minus_pi;
  243. return four_minus_pi<RealType>() * sigma * sigma / 2;
  244. } // variance
  245. template <class RealType, class Policy>
  246. inline RealType mode(const rayleigh_distribution<RealType, Policy>& dist)
  247. {
  248. return dist.sigma();
  249. }
  250. template <class RealType, class Policy>
  251. inline RealType median(const rayleigh_distribution<RealType, Policy>& dist)
  252. {
  253. using boost::math::constants::root_ln_four;
  254. return root_ln_four<RealType>() * dist.sigma();
  255. }
  256. template <class RealType, class Policy>
  257. inline RealType skewness(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  258. {
  259. return static_cast<RealType>(0.63111065781893713819189935154422777984404221106391L);
  260. // Computed using NTL at 150 bit, about 50 decimal digits.
  261. // 2 * sqrt(pi) * (pi-3) / pow(4, 2/3) - pi
  262. }
  263. template <class RealType, class Policy>
  264. inline RealType kurtosis(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  265. {
  266. return static_cast<RealType>(3.2450893006876380628486604106197544154170667057995L);
  267. // Computed using NTL at 150 bit, about 50 decimal digits.
  268. // 3 - (6*pi*pi - 24*pi + 16) / pow(4-pi, 2)
  269. }
  270. template <class RealType, class Policy>
  271. inline RealType kurtosis_excess(const rayleigh_distribution<RealType, Policy>& /*dist*/)
  272. {
  273. return static_cast<RealType>(0.2450893006876380628486604106197544154170667057995L);
  274. // Computed using NTL at 150 bit, about 50 decimal digits.
  275. // -(6*pi*pi - 24*pi + 16) / pow(4-pi,2)
  276. } // kurtosis_excess
  277. template <class RealType, class Policy>
  278. inline RealType entropy(const rayleigh_distribution<RealType, Policy>& dist)
  279. {
  280. using std::log;
  281. return 1 + log(dist.sigma()*constants::one_div_root_two<RealType>()) + constants::euler<RealType>()/2;
  282. }
  283. } // namespace math
  284. } // namespace boost
  285. #ifdef _MSC_VER
  286. # pragma warning(pop)
  287. #endif
  288. // This include must be at the end, *after* the accessors
  289. // for this distribution have been defined, in order to
  290. // keep compilers that support two-phase lookup happy.
  291. #include <boost/math/distributions/detail/derived_accessors.hpp>
  292. #endif // BOOST_STATS_rayleigh_HPP