1 // Copyright John Maddock 2006, 2007.
2 // Copyright Paul A. Bristow 2006, 2007.
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)
8 #ifndef BOOST_STATS_NORMAL_HPP
9 #define BOOST_STATS_NORMAL_HPP
11 // http://en.wikipedia.org/wiki/Normal_distribution
12 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
14 // Weisstein, Eric W. "Normal Distribution."
15 // From MathWorld--A Wolfram Web Resource.
16 // http://mathworld.wolfram.com/NormalDistribution.html
18 #include <boost/math/distributions/fwd.hpp>
19 #include <boost/math/special_functions/erf.hpp> // for erf/erfc.
20 #include <boost/math/distributions/complement.hpp>
21 #include <boost/math/distributions/detail/common_error_handling.hpp>
25 namespace boost{ namespace math{
27 template <class RealType = double, class Policy = policies::policy<> >
28 class normal_distribution
31 typedef RealType value_type;
32 typedef Policy policy_type;
34 normal_distribution(RealType l_mean = 0, RealType sd = 1)
35 : m_mean(l_mean), m_sd(sd)
36 { // Default is a 'standard' normal distribution N01.
37 static const char* function = "boost::math::normal_distribution<%1%>::normal_distribution";
40 detail::check_scale(function, sd, &result, Policy());
41 detail::check_location(function, l_mean, &result, Policy());
45 { // alias for location.
49 RealType standard_deviation()const
54 // Synonyms, provided to allow generic use of find_location and find_scale.
55 RealType location()const
68 RealType m_mean; // distribution mean or location.
69 RealType m_sd; // distribution standard deviation or scale.
70 }; // class normal_distribution
72 typedef normal_distribution<double> normal;
75 // Deduction guides, note we don't check the
76 // value of __cpp_deduction_guides, just assume
77 // they work as advertised, even if this is pre-final C++17.
79 #ifdef __cpp_deduction_guides
81 template <class RealType>
82 normal_distribution(RealType, RealType)->normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
83 template <class RealType>
84 normal_distribution(RealType)->normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
90 #pragma warning(disable:4127)
93 template <class RealType, class Policy>
94 inline const std::pair<RealType, RealType> range(const normal_distribution<RealType, Policy>& /*dist*/)
95 { // Range of permissible values for random variable x.
96 if (std::numeric_limits<RealType>::has_infinity)
98 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
101 { // Can only use max_value.
102 using boost::math::tools::max_value;
103 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
107 template <class RealType, class Policy>
108 inline const std::pair<RealType, RealType> support(const normal_distribution<RealType, Policy>& /*dist*/)
109 { // This is range values for random variable x where cdf rises from 0 to 1, and outside it, the pdf is zero.
110 if (std::numeric_limits<RealType>::has_infinity)
112 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
115 { // Can only use max_value.
116 using boost::math::tools::max_value;
117 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
125 template <class RealType, class Policy>
126 inline RealType pdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
128 BOOST_MATH_STD_USING // for ADL of std functions
130 RealType sd = dist.standard_deviation();
131 RealType mean = dist.mean();
133 static const char* function = "boost::math::pdf(const normal_distribution<%1%>&, %1%)";
136 if(false == detail::check_scale(function, sd, &result, Policy()))
140 if(false == detail::check_location(function, mean, &result, Policy()))
144 if((boost::math::isinf)(x))
146 return 0; // pdf + and - infinity is zero.
148 // Below produces MSVC 4127 warnings, so the above used instead.
149 //if(std::numeric_limits<RealType>::has_infinity && abs(x) == std::numeric_limits<RealType>::infinity())
150 //{ // pdf + and - infinity is zero.
153 if(false == detail::check_x(function, x, &result, Policy()))
158 RealType exponent = x - mean;
159 exponent *= -exponent;
160 exponent /= 2 * sd * sd;
162 result = exp(exponent);
163 result /= sd * sqrt(2 * constants::pi<RealType>());
168 template <class RealType, class Policy>
169 inline RealType cdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
171 BOOST_MATH_STD_USING // for ADL of std functions
173 RealType sd = dist.standard_deviation();
174 RealType mean = dist.mean();
175 static const char* function = "boost::math::cdf(const normal_distribution<%1%>&, %1%)";
177 if(false == detail::check_scale(function, sd, &result, Policy()))
181 if(false == detail::check_location(function, mean, &result, Policy()))
185 if((boost::math::isinf)(x))
187 if(x < 0) return 0; // -infinity
188 return 1; // + infinity
190 // These produce MSVC 4127 warnings, so the above used instead.
191 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
192 //{ // cdf +infinity is unity.
195 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
196 //{ // cdf -infinity is zero.
199 if(false == detail::check_x(function, x, &result, Policy()))
203 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
204 result = boost::math::erfc(-diff, Policy()) / 2;
208 template <class RealType, class Policy>
209 inline RealType quantile(const normal_distribution<RealType, Policy>& dist, const RealType& p)
211 BOOST_MATH_STD_USING // for ADL of std functions
213 RealType sd = dist.standard_deviation();
214 RealType mean = dist.mean();
215 static const char* function = "boost::math::quantile(const normal_distribution<%1%>&, %1%)";
218 if(false == detail::check_scale(function, sd, &result, Policy()))
220 if(false == detail::check_location(function, mean, &result, Policy()))
222 if(false == detail::check_probability(function, p, &result, Policy()))
225 result= boost::math::erfc_inv(2 * p, Policy());
227 result *= sd * constants::root_two<RealType>();
232 template <class RealType, class Policy>
233 inline RealType cdf(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
235 BOOST_MATH_STD_USING // for ADL of std functions
237 RealType sd = c.dist.standard_deviation();
238 RealType mean = c.dist.mean();
239 RealType x = c.param;
240 static const char* function = "boost::math::cdf(const complement(normal_distribution<%1%>&), %1%)";
243 if(false == detail::check_scale(function, sd, &result, Policy()))
245 if(false == detail::check_location(function, mean, &result, Policy()))
247 if((boost::math::isinf)(x))
249 if(x < 0) return 1; // cdf complement -infinity is unity.
250 return 0; // cdf complement +infinity is zero
252 // These produce MSVC 4127 warnings, so the above used instead.
253 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
254 //{ // cdf complement +infinity is zero.
257 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
258 //{ // cdf complement -infinity is unity.
261 if(false == detail::check_x(function, x, &result, Policy()))
264 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
265 result = boost::math::erfc(diff, Policy()) / 2;
269 template <class RealType, class Policy>
270 inline RealType quantile(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
272 BOOST_MATH_STD_USING // for ADL of std functions
274 RealType sd = c.dist.standard_deviation();
275 RealType mean = c.dist.mean();
276 static const char* function = "boost::math::quantile(const complement(normal_distribution<%1%>&), %1%)";
278 if(false == detail::check_scale(function, sd, &result, Policy()))
280 if(false == detail::check_location(function, mean, &result, Policy()))
282 RealType q = c.param;
283 if(false == detail::check_probability(function, q, &result, Policy()))
285 result = boost::math::erfc_inv(2 * q, Policy());
286 result *= sd * constants::root_two<RealType>();
291 template <class RealType, class Policy>
292 inline RealType mean(const normal_distribution<RealType, Policy>& dist)
297 template <class RealType, class Policy>
298 inline RealType standard_deviation(const normal_distribution<RealType, Policy>& dist)
300 return dist.standard_deviation();
303 template <class RealType, class Policy>
304 inline RealType mode(const normal_distribution<RealType, Policy>& dist)
309 template <class RealType, class Policy>
310 inline RealType median(const normal_distribution<RealType, Policy>& dist)
315 template <class RealType, class Policy>
316 inline RealType skewness(const normal_distribution<RealType, Policy>& /*dist*/)
321 template <class RealType, class Policy>
322 inline RealType kurtosis(const normal_distribution<RealType, Policy>& /*dist*/)
327 template <class RealType, class Policy>
328 inline RealType kurtosis_excess(const normal_distribution<RealType, Policy>& /*dist*/)
333 template <class RealType, class Policy>
334 inline RealType entropy(const normal_distribution<RealType, Policy> & dist)
337 RealType arg = constants::two_pi<RealType>()*constants::e<RealType>()*dist.standard_deviation()*dist.standard_deviation();
344 // This include must be at the end, *after* the accessors
345 // for this distribution have been defined, in order to
346 // keep compilers that support two-phase lookup happy.
347 #include <boost/math/distributions/detail/derived_accessors.hpp>
349 #endif // BOOST_STATS_NORMAL_HPP