normal.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright John Maddock 2006, 2007.
  2. // Copyright Paul A. Bristow 2006, 2007.
  3. // Copyright Matt Borland 2024.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_STATS_NORMAL_HPP
  8. #define BOOST_STATS_NORMAL_HPP
  9. // http://en.wikipedia.org/wiki/Normal_distribution
  10. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
  11. // Also:
  12. // Weisstein, Eric W. "Normal Distribution."
  13. // From MathWorld--A Wolfram Web Resource.
  14. // http://mathworld.wolfram.com/NormalDistribution.html
  15. #include <boost/math/tools/config.hpp>
  16. #include <boost/math/tools/tuple.hpp>
  17. #include <boost/math/tools/numeric_limits.hpp>
  18. #include <boost/math/tools/promotion.hpp>
  19. #include <boost/math/distributions/fwd.hpp>
  20. #include <boost/math/special_functions/erf.hpp> // for erf/erfc.
  21. #include <boost/math/distributions/complement.hpp>
  22. #include <boost/math/distributions/detail/common_error_handling.hpp>
  23. #include <boost/math/constants/constants.hpp>
  24. #include <boost/math/policies/policy.hpp>
  25. namespace boost{ namespace math{
  26. template <class RealType = double, class Policy = policies::policy<> >
  27. class normal_distribution
  28. {
  29. public:
  30. using value_type = RealType;
  31. using policy_type = Policy;
  32. BOOST_MATH_GPU_ENABLED explicit normal_distribution(RealType l_mean = 0, RealType sd = 1)
  33. : m_mean(l_mean), m_sd(sd)
  34. { // Default is a 'standard' normal distribution N01.
  35. constexpr auto function = "boost::math::normal_distribution<%1%>::normal_distribution";
  36. RealType result;
  37. detail::check_scale(function, sd, &result, Policy());
  38. detail::check_location(function, l_mean, &result, Policy());
  39. }
  40. BOOST_MATH_GPU_ENABLED RealType mean()const
  41. { // alias for location.
  42. return m_mean;
  43. }
  44. BOOST_MATH_GPU_ENABLED RealType standard_deviation()const
  45. { // alias for scale.
  46. return m_sd;
  47. }
  48. // Synonyms, provided to allow generic use of find_location and find_scale.
  49. BOOST_MATH_GPU_ENABLED RealType location()const
  50. { // location.
  51. return m_mean;
  52. }
  53. BOOST_MATH_GPU_ENABLED RealType scale()const
  54. { // scale.
  55. return m_sd;
  56. }
  57. private:
  58. //
  59. // Data members:
  60. //
  61. RealType m_mean; // distribution mean or location.
  62. RealType m_sd; // distribution standard deviation or scale.
  63. }; // class normal_distribution
  64. using normal = normal_distribution<double>;
  65. //
  66. // Deduction guides, note we don't check the
  67. // value of __cpp_deduction_guides, just assume
  68. // they work as advertised, even if this is pre-final C++17.
  69. //
  70. #ifdef __cpp_deduction_guides
  71. template <class RealType>
  72. normal_distribution(RealType, RealType)->normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  73. template <class RealType>
  74. normal_distribution(RealType)->normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  75. #endif
  76. #ifdef _MSC_VER
  77. #pragma warning(push)
  78. #pragma warning(disable:4127)
  79. #endif
  80. template <class RealType, class Policy>
  81. BOOST_MATH_GPU_ENABLED inline boost::math::pair<RealType, RealType> range(const normal_distribution<RealType, Policy>& /*dist*/)
  82. { // Range of permissible values for random variable x.
  83. BOOST_MATH_IF_CONSTEXPR (boost::math::numeric_limits<RealType>::has_infinity)
  84. {
  85. return boost::math::pair<RealType, RealType>(-boost::math::numeric_limits<RealType>::infinity(), boost::math::numeric_limits<RealType>::infinity()); // - to + infinity.
  86. }
  87. else
  88. { // Can only use max_value.
  89. using boost::math::tools::max_value;
  90. return boost::math::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
  91. }
  92. }
  93. template <class RealType, class Policy>
  94. BOOST_MATH_GPU_ENABLED inline boost::math::pair<RealType, RealType> support(const normal_distribution<RealType, Policy>& /*dist*/)
  95. { // This is range values for random variable x where cdf rises from 0 to 1, and outside it, the pdf is zero.
  96. BOOST_MATH_IF_CONSTEXPR (boost::math::numeric_limits<RealType>::has_infinity)
  97. {
  98. return boost::math::pair<RealType, RealType>(-boost::math::numeric_limits<RealType>::infinity(), boost::math::numeric_limits<RealType>::infinity()); // - to + infinity.
  99. }
  100. else
  101. { // Can only use max_value.
  102. using boost::math::tools::max_value;
  103. return boost::math::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
  104. }
  105. }
  106. #ifdef _MSC_VER
  107. #pragma warning(pop)
  108. #endif
  109. template <class RealType, class Policy>
  110. BOOST_MATH_GPU_ENABLED inline RealType pdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
  111. {
  112. BOOST_MATH_STD_USING // for ADL of std functions
  113. RealType sd = dist.standard_deviation();
  114. RealType mean = dist.mean();
  115. constexpr auto function = "boost::math::pdf(const normal_distribution<%1%>&, %1%)";
  116. RealType result = 0;
  117. if(false == detail::check_scale(function, sd, &result, Policy()))
  118. {
  119. return result;
  120. }
  121. if(false == detail::check_location(function, mean, &result, Policy()))
  122. {
  123. return result;
  124. }
  125. if((boost::math::isinf)(x))
  126. {
  127. return 0; // pdf + and - infinity is zero.
  128. }
  129. if(false == detail::check_x(function, x, &result, Policy()))
  130. {
  131. return result;
  132. }
  133. RealType exponent = x - mean;
  134. exponent *= -exponent;
  135. exponent /= 2 * sd * sd;
  136. result = exp(exponent);
  137. result /= sd * sqrt(2 * constants::pi<RealType>());
  138. return result;
  139. } // pdf
  140. template <class RealType, class Policy>
  141. BOOST_MATH_GPU_ENABLED inline RealType logpdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
  142. {
  143. BOOST_MATH_STD_USING // for ADL of std functions
  144. const RealType sd = dist.standard_deviation();
  145. const RealType mean = dist.mean();
  146. constexpr auto function = "boost::math::logpdf(const normal_distribution<%1%>&, %1%)";
  147. RealType result = -boost::math::numeric_limits<RealType>::infinity();
  148. if(false == detail::check_scale(function, sd, &result, Policy()))
  149. {
  150. return result;
  151. }
  152. if(false == detail::check_location(function, mean, &result, Policy()))
  153. {
  154. return result;
  155. }
  156. if((boost::math::isinf)(x))
  157. {
  158. return result; // pdf + and - infinity is zero so logpdf is -inf
  159. }
  160. if(false == detail::check_x(function, x, &result, Policy()))
  161. {
  162. return result;
  163. }
  164. const RealType pi = boost::math::constants::pi<RealType>();
  165. const RealType half = boost::math::constants::half<RealType>();
  166. result = -log(sd) - half*log(2*pi) - (x-mean)*(x-mean)/(2*sd*sd);
  167. return result;
  168. }
  169. template <class RealType, class Policy>
  170. BOOST_MATH_GPU_ENABLED inline RealType cdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
  171. {
  172. BOOST_MATH_STD_USING // for ADL of std functions
  173. RealType sd = dist.standard_deviation();
  174. RealType mean = dist.mean();
  175. constexpr auto function = "boost::math::cdf(const normal_distribution<%1%>&, %1%)";
  176. RealType result = 0;
  177. if(false == detail::check_scale(function, sd, &result, Policy()))
  178. {
  179. return result;
  180. }
  181. if(false == detail::check_location(function, mean, &result, Policy()))
  182. {
  183. return result;
  184. }
  185. if((boost::math::isinf)(x))
  186. {
  187. if(x < 0) return 0; // -infinity
  188. return 1; // + infinity
  189. }
  190. if(false == detail::check_x(function, x, &result, Policy()))
  191. {
  192. return result;
  193. }
  194. RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
  195. result = boost::math::erfc(-diff, Policy()) / 2;
  196. return result;
  197. } // cdf
  198. template <class RealType, class Policy>
  199. BOOST_MATH_GPU_ENABLED inline RealType quantile(const normal_distribution<RealType, Policy>& dist, const RealType& p)
  200. {
  201. BOOST_MATH_STD_USING // for ADL of std functions
  202. RealType sd = dist.standard_deviation();
  203. RealType mean = dist.mean();
  204. constexpr auto function = "boost::math::quantile(const normal_distribution<%1%>&, %1%)";
  205. RealType result = 0;
  206. if(false == detail::check_scale(function, sd, &result, Policy()))
  207. return result;
  208. if(false == detail::check_location(function, mean, &result, Policy()))
  209. return result;
  210. if(false == detail::check_probability(function, p, &result, Policy()))
  211. return result;
  212. result= boost::math::erfc_inv(2 * p, Policy());
  213. result = -result;
  214. result *= sd * constants::root_two<RealType>();
  215. result += mean;
  216. return result;
  217. } // quantile
  218. template <class RealType, class Policy>
  219. BOOST_MATH_GPU_ENABLED inline RealType cdf(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
  220. {
  221. BOOST_MATH_STD_USING // for ADL of std functions
  222. RealType sd = c.dist.standard_deviation();
  223. RealType mean = c.dist.mean();
  224. RealType x = c.param;
  225. constexpr auto function = "boost::math::cdf(const complement(normal_distribution<%1%>&), %1%)";
  226. RealType result = 0;
  227. if(false == detail::check_scale(function, sd, &result, Policy()))
  228. return result;
  229. if(false == detail::check_location(function, mean, &result, Policy()))
  230. return result;
  231. if((boost::math::isinf)(x))
  232. {
  233. if(x < 0) return 1; // cdf complement -infinity is unity.
  234. return 0; // cdf complement +infinity is zero
  235. }
  236. if(false == detail::check_x(function, x, &result, Policy()))
  237. return result;
  238. RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
  239. result = boost::math::erfc(diff, Policy()) / 2;
  240. return result;
  241. } // cdf complement
  242. template <class RealType, class Policy>
  243. BOOST_MATH_GPU_ENABLED inline RealType quantile(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
  244. {
  245. BOOST_MATH_STD_USING // for ADL of std functions
  246. RealType sd = c.dist.standard_deviation();
  247. RealType mean = c.dist.mean();
  248. constexpr auto function = "boost::math::quantile(const complement(normal_distribution<%1%>&), %1%)";
  249. RealType result = 0;
  250. if(false == detail::check_scale(function, sd, &result, Policy()))
  251. return result;
  252. if(false == detail::check_location(function, mean, &result, Policy()))
  253. return result;
  254. RealType q = c.param;
  255. if(false == detail::check_probability(function, q, &result, Policy()))
  256. return result;
  257. result = boost::math::erfc_inv(2 * q, Policy());
  258. result *= sd * constants::root_two<RealType>();
  259. result += mean;
  260. return result;
  261. } // quantile
  262. template <class RealType, class Policy>
  263. BOOST_MATH_GPU_ENABLED inline RealType mean(const normal_distribution<RealType, Policy>& dist)
  264. {
  265. return dist.mean();
  266. }
  267. template <class RealType, class Policy>
  268. BOOST_MATH_GPU_ENABLED inline RealType standard_deviation(const normal_distribution<RealType, Policy>& dist)
  269. {
  270. return dist.standard_deviation();
  271. }
  272. template <class RealType, class Policy>
  273. BOOST_MATH_GPU_ENABLED inline RealType mode(const normal_distribution<RealType, Policy>& dist)
  274. {
  275. return dist.mean();
  276. }
  277. template <class RealType, class Policy>
  278. BOOST_MATH_GPU_ENABLED inline RealType median(const normal_distribution<RealType, Policy>& dist)
  279. {
  280. return dist.mean();
  281. }
  282. template <class RealType, class Policy>
  283. BOOST_MATH_GPU_ENABLED inline RealType skewness(const normal_distribution<RealType, Policy>& /*dist*/)
  284. {
  285. return 0;
  286. }
  287. template <class RealType, class Policy>
  288. BOOST_MATH_GPU_ENABLED inline RealType kurtosis(const normal_distribution<RealType, Policy>& /*dist*/)
  289. {
  290. return 3;
  291. }
  292. template <class RealType, class Policy>
  293. BOOST_MATH_GPU_ENABLED inline RealType kurtosis_excess(const normal_distribution<RealType, Policy>& /*dist*/)
  294. {
  295. return 0;
  296. }
  297. template <class RealType, class Policy>
  298. BOOST_MATH_GPU_ENABLED inline RealType entropy(const normal_distribution<RealType, Policy> & dist)
  299. {
  300. BOOST_MATH_STD_USING
  301. RealType arg = constants::two_pi<RealType>()*constants::e<RealType>()*dist.standard_deviation()*dist.standard_deviation();
  302. return log(arg)/2;
  303. }
  304. } // namespace math
  305. } // namespace boost
  306. // This include must be at the end, *after* the accessors
  307. // for this distribution have been defined, in order to
  308. // keep compilers that support two-phase lookup happy.
  309. #include <boost/math/distributions/detail/derived_accessors.hpp>
  310. #endif // BOOST_STATS_NORMAL_HPP