normal.hpp 11 KB

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