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;
74 template <class RealType, class Policy>
75 inline const std::pair<RealType, RealType> range(const normal_distribution<RealType, Policy>& /*dist*/)
76 { // Range of permissible values for random variable x.
77 if (std::numeric_limits<RealType>::has_infinity)
79 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
82 { // Can only use max_value.
83 using boost::math::tools::max_value;
84 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
88 template <class RealType, class Policy>
89 inline const std::pair<RealType, RealType> support(const normal_distribution<RealType, Policy>& /*dist*/)
90 { // Range of supported values for random variable x.
91 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
92 if (std::numeric_limits<RealType>::has_infinity)
94 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
97 { // Can only use max_value.
98 using boost::math::tools::max_value;
99 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
103 template <class RealType, class Policy>
104 inline RealType pdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
106 BOOST_MATH_STD_USING // for ADL of std functions
108 RealType sd = dist.standard_deviation();
109 RealType mean = dist.mean();
111 static const char* function = "boost::math::pdf(const normal_distribution<%1%>&, %1%)";
114 if(false == detail::check_scale(function, sd, &result, Policy()))
118 if(false == detail::check_location(function, mean, &result, Policy()))
122 if((boost::math::isinf)(x))
124 return 0; // pdf + and - infinity is zero.
126 // Below produces MSVC 4127 warnings, so the above used instead.
127 //if(std::numeric_limits<RealType>::has_infinity && abs(x) == std::numeric_limits<RealType>::infinity())
128 //{ // pdf + and - infinity is zero.
131 if(false == detail::check_x(function, x, &result, Policy()))
136 RealType exponent = x - mean;
137 exponent *= -exponent;
138 exponent /= 2 * sd * sd;
140 result = exp(exponent);
141 result /= sd * sqrt(2 * constants::pi<RealType>());
146 template <class RealType, class Policy>
147 inline RealType cdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
149 BOOST_MATH_STD_USING // for ADL of std functions
151 RealType sd = dist.standard_deviation();
152 RealType mean = dist.mean();
153 static const char* function = "boost::math::cdf(const normal_distribution<%1%>&, %1%)";
155 if(false == detail::check_scale(function, sd, &result, Policy()))
159 if(false == detail::check_location(function, mean, &result, Policy()))
163 if((boost::math::isinf)(x))
165 if(x < 0) return 0; // -infinity
166 return 1; // + infinity
168 // These produce MSVC 4127 warnings, so the above used instead.
169 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
170 //{ // cdf +infinity is unity.
173 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
174 //{ // cdf -infinity is zero.
177 if(false == detail::check_x(function, x, &result, Policy()))
181 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
182 result = boost::math::erfc(-diff, Policy()) / 2;
186 template <class RealType, class Policy>
187 inline RealType quantile(const normal_distribution<RealType, Policy>& dist, const RealType& p)
189 BOOST_MATH_STD_USING // for ADL of std functions
191 RealType sd = dist.standard_deviation();
192 RealType mean = dist.mean();
193 static const char* function = "boost::math::quantile(const normal_distribution<%1%>&, %1%)";
196 if(false == detail::check_scale(function, sd, &result, Policy()))
198 if(false == detail::check_location(function, mean, &result, Policy()))
200 if(false == detail::check_probability(function, p, &result, Policy()))
203 result= boost::math::erfc_inv(2 * p, Policy());
205 result *= sd * constants::root_two<RealType>();
210 template <class RealType, class Policy>
211 inline RealType cdf(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
213 BOOST_MATH_STD_USING // for ADL of std functions
215 RealType sd = c.dist.standard_deviation();
216 RealType mean = c.dist.mean();
217 RealType x = c.param;
218 static const char* function = "boost::math::cdf(const complement(normal_distribution<%1%>&), %1%)";
221 if(false == detail::check_scale(function, sd, &result, Policy()))
223 if(false == detail::check_location(function, mean, &result, Policy()))
225 if((boost::math::isinf)(x))
227 if(x < 0) return 1; // cdf complement -infinity is unity.
228 return 0; // cdf complement +infinity is zero
230 // These produce MSVC 4127 warnings, so the above used instead.
231 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
232 //{ // cdf complement +infinity is zero.
235 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
236 //{ // cdf complement -infinity is unity.
239 if(false == detail::check_x(function, x, &result, Policy()))
242 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
243 result = boost::math::erfc(diff, Policy()) / 2;
247 template <class RealType, class Policy>
248 inline RealType quantile(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
250 BOOST_MATH_STD_USING // for ADL of std functions
252 RealType sd = c.dist.standard_deviation();
253 RealType mean = c.dist.mean();
254 static const char* function = "boost::math::quantile(const complement(normal_distribution<%1%>&), %1%)";
256 if(false == detail::check_scale(function, sd, &result, Policy()))
258 if(false == detail::check_location(function, mean, &result, Policy()))
260 RealType q = c.param;
261 if(false == detail::check_probability(function, q, &result, Policy()))
263 result = boost::math::erfc_inv(2 * q, Policy());
264 result *= sd * constants::root_two<RealType>();
269 template <class RealType, class Policy>
270 inline RealType mean(const normal_distribution<RealType, Policy>& dist)
275 template <class RealType, class Policy>
276 inline RealType standard_deviation(const normal_distribution<RealType, Policy>& dist)
278 return dist.standard_deviation();
281 template <class RealType, class Policy>
282 inline RealType mode(const normal_distribution<RealType, Policy>& dist)
287 template <class RealType, class Policy>
288 inline RealType median(const normal_distribution<RealType, Policy>& dist)
293 template <class RealType, class Policy>
294 inline RealType skewness(const normal_distribution<RealType, Policy>& /*dist*/)
299 template <class RealType, class Policy>
300 inline RealType kurtosis(const normal_distribution<RealType, Policy>& /*dist*/)
305 template <class RealType, class Policy>
306 inline RealType kurtosis_excess(const normal_distribution<RealType, Policy>& /*dist*/)
314 // This include must be at the end, *after* the accessors
315 // for this distribution have been defined, in order to
316 // keep compilers that support two-phase lookup happy.
317 #include <boost/math/distributions/detail/derived_accessors.hpp>
319 #endif // BOOST_STATS_NORMAL_HPP