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;
76 #pragma warning(disable:4127)
79 template <class RealType, class Policy>
80 inline const std::pair<RealType, RealType> range(const normal_distribution<RealType, Policy>& /*dist*/)
81 { // Range of permissible values for random variable x.
82 if (std::numeric_limits<RealType>::has_infinity)
84 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
87 { // Can only use max_value.
88 using boost::math::tools::max_value;
89 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
93 template <class RealType, class Policy>
94 inline const std::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 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.
111 template <class RealType, class Policy>
112 inline RealType pdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
114 BOOST_MATH_STD_USING // for ADL of std functions
116 RealType sd = dist.standard_deviation();
117 RealType mean = dist.mean();
119 static const char* function = "boost::math::pdf(const normal_distribution<%1%>&, %1%)";
122 if(false == detail::check_scale(function, sd, &result, Policy()))
126 if(false == detail::check_location(function, mean, &result, Policy()))
130 if((boost::math::isinf)(x))
132 return 0; // pdf + and - infinity is zero.
134 // Below produces MSVC 4127 warnings, so the above used instead.
135 //if(std::numeric_limits<RealType>::has_infinity && abs(x) == std::numeric_limits<RealType>::infinity())
136 //{ // pdf + and - infinity is zero.
139 if(false == detail::check_x(function, x, &result, Policy()))
144 RealType exponent = x - mean;
145 exponent *= -exponent;
146 exponent /= 2 * sd * sd;
148 result = exp(exponent);
149 result /= sd * sqrt(2 * constants::pi<RealType>());
154 template <class RealType, class Policy>
155 inline RealType cdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
157 BOOST_MATH_STD_USING // for ADL of std functions
159 RealType sd = dist.standard_deviation();
160 RealType mean = dist.mean();
161 static const char* function = "boost::math::cdf(const normal_distribution<%1%>&, %1%)";
163 if(false == detail::check_scale(function, sd, &result, Policy()))
167 if(false == detail::check_location(function, mean, &result, Policy()))
171 if((boost::math::isinf)(x))
173 if(x < 0) return 0; // -infinity
174 return 1; // + infinity
176 // These produce MSVC 4127 warnings, so the above used instead.
177 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
178 //{ // cdf +infinity is unity.
181 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
182 //{ // cdf -infinity is zero.
185 if(false == detail::check_x(function, x, &result, Policy()))
189 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
190 result = boost::math::erfc(-diff, Policy()) / 2;
194 template <class RealType, class Policy>
195 inline RealType quantile(const normal_distribution<RealType, Policy>& dist, const RealType& p)
197 BOOST_MATH_STD_USING // for ADL of std functions
199 RealType sd = dist.standard_deviation();
200 RealType mean = dist.mean();
201 static const char* function = "boost::math::quantile(const normal_distribution<%1%>&, %1%)";
204 if(false == detail::check_scale(function, sd, &result, Policy()))
206 if(false == detail::check_location(function, mean, &result, Policy()))
208 if(false == detail::check_probability(function, p, &result, Policy()))
211 result= boost::math::erfc_inv(2 * p, Policy());
213 result *= sd * constants::root_two<RealType>();
218 template <class RealType, class Policy>
219 inline RealType cdf(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
221 BOOST_MATH_STD_USING // for ADL of std functions
223 RealType sd = c.dist.standard_deviation();
224 RealType mean = c.dist.mean();
225 RealType x = c.param;
226 static const char* function = "boost::math::cdf(const complement(normal_distribution<%1%>&), %1%)";
229 if(false == detail::check_scale(function, sd, &result, Policy()))
231 if(false == detail::check_location(function, mean, &result, Policy()))
233 if((boost::math::isinf)(x))
235 if(x < 0) return 1; // cdf complement -infinity is unity.
236 return 0; // cdf complement +infinity is zero
238 // These produce MSVC 4127 warnings, so the above used instead.
239 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
240 //{ // cdf complement +infinity is zero.
243 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
244 //{ // cdf complement -infinity is unity.
247 if(false == detail::check_x(function, x, &result, Policy()))
250 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
251 result = boost::math::erfc(diff, Policy()) / 2;
255 template <class RealType, class Policy>
256 inline RealType quantile(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
258 BOOST_MATH_STD_USING // for ADL of std functions
260 RealType sd = c.dist.standard_deviation();
261 RealType mean = c.dist.mean();
262 static const char* function = "boost::math::quantile(const complement(normal_distribution<%1%>&), %1%)";
264 if(false == detail::check_scale(function, sd, &result, Policy()))
266 if(false == detail::check_location(function, mean, &result, Policy()))
268 RealType q = c.param;
269 if(false == detail::check_probability(function, q, &result, Policy()))
271 result = boost::math::erfc_inv(2 * q, Policy());
272 result *= sd * constants::root_two<RealType>();
277 template <class RealType, class Policy>
278 inline RealType mean(const normal_distribution<RealType, Policy>& dist)
283 template <class RealType, class Policy>
284 inline RealType standard_deviation(const normal_distribution<RealType, Policy>& dist)
286 return dist.standard_deviation();
289 template <class RealType, class Policy>
290 inline RealType mode(const normal_distribution<RealType, Policy>& dist)
295 template <class RealType, class Policy>
296 inline RealType median(const normal_distribution<RealType, Policy>& dist)
301 template <class RealType, class Policy>
302 inline RealType skewness(const normal_distribution<RealType, Policy>& /*dist*/)
307 template <class RealType, class Policy>
308 inline RealType kurtosis(const normal_distribution<RealType, Policy>& /*dist*/)
313 template <class RealType, class Policy>
314 inline RealType kurtosis_excess(const normal_distribution<RealType, Policy>& /*dist*/)
322 // This include must be at the end, *after* the accessors
323 // for this distribution have been defined, in order to
324 // keep compilers that support two-phase lookup happy.
325 #include <boost/math/distributions/detail/derived_accessors.hpp>
327 #endif // BOOST_STATS_NORMAL_HPP