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 mean = 0, RealType sd = 1)
35 : m_mean(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, 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 using boost::math::tools::max_value;
78 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
81 template <class RealType, class Policy>
82 inline const std::pair<RealType, RealType> support(const normal_distribution<RealType, Policy>& /*dist*/)
83 { // Range of supported values for random variable x.
84 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
86 using boost::math::tools::max_value;
87 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
90 template <class RealType, class Policy>
91 inline RealType pdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
93 BOOST_MATH_STD_USING // for ADL of std functions
95 RealType sd = dist.standard_deviation();
96 RealType mean = dist.mean();
98 static const char* function = "boost::math::pdf(const normal_distribution<%1%>&, %1%)";
99 if((boost::math::isinf)(x))
101 return 0; // pdf + and - infinity is zero.
103 // Below produces MSVC 4127 warnings, so the above used instead.
104 //if(std::numeric_limits<RealType>::has_infinity && abs(x) == std::numeric_limits<RealType>::infinity())
105 //{ // pdf + and - infinity is zero.
110 if(false == detail::check_scale(function, sd, &result, Policy()))
114 if(false == detail::check_location(function, mean, &result, Policy()))
118 if(false == detail::check_x(function, x, &result, Policy()))
123 RealType exponent = x - mean;
124 exponent *= -exponent;
125 exponent /= 2 * sd * sd;
127 result = exp(exponent);
128 result /= sd * sqrt(2 * constants::pi<RealType>());
133 template <class RealType, class Policy>
134 inline RealType cdf(const normal_distribution<RealType, Policy>& dist, const RealType& x)
136 BOOST_MATH_STD_USING // for ADL of std functions
138 RealType sd = dist.standard_deviation();
139 RealType mean = dist.mean();
140 static const char* function = "boost::math::cdf(const normal_distribution<%1%>&, %1%)";
142 if(false == detail::check_scale(function, sd, &result, Policy()))
146 if(false == detail::check_location(function, mean, &result, Policy()))
150 if((boost::math::isinf)(x))
152 if(x < 0) return 0; // -infinity
153 return 1; // + infinity
155 // These produce MSVC 4127 warnings, so the above used instead.
156 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
157 //{ // cdf +infinity is unity.
160 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
161 //{ // cdf -infinity is zero.
164 if(false == detail::check_x(function, x, &result, Policy()))
168 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
169 result = boost::math::erfc(-diff, Policy()) / 2;
173 template <class RealType, class Policy>
174 inline RealType quantile(const normal_distribution<RealType, Policy>& dist, const RealType& p)
176 BOOST_MATH_STD_USING // for ADL of std functions
178 RealType sd = dist.standard_deviation();
179 RealType mean = dist.mean();
180 static const char* function = "boost::math::quantile(const normal_distribution<%1%>&, %1%)";
183 if(false == detail::check_scale(function, sd, &result, Policy()))
185 if(false == detail::check_location(function, mean, &result, Policy()))
187 if(false == detail::check_probability(function, p, &result, Policy()))
190 result= boost::math::erfc_inv(2 * p, Policy());
192 result *= sd * constants::root_two<RealType>();
197 template <class RealType, class Policy>
198 inline RealType cdf(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
200 BOOST_MATH_STD_USING // for ADL of std functions
202 RealType sd = c.dist.standard_deviation();
203 RealType mean = c.dist.mean();
204 RealType x = c.param;
205 static const char* function = "boost::math::cdf(const complement(normal_distribution<%1%>&), %1%)";
207 if((boost::math::isinf)(x))
209 if(x < 0) return 1; // cdf complement -infinity is unity.
210 return 0; // cdf complement +infinity is zero
212 // These produce MSVC 4127 warnings, so the above used instead.
213 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
214 //{ // cdf complement +infinity is zero.
217 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
218 //{ // cdf complement -infinity is unity.
222 if(false == detail::check_scale(function, sd, &result, Policy()))
224 if(false == detail::check_location(function, mean, &result, Policy()))
226 if(false == detail::check_x(function, x, &result, Policy()))
229 RealType diff = (x - mean) / (sd * constants::root_two<RealType>());
230 result = boost::math::erfc(diff, Policy()) / 2;
234 template <class RealType, class Policy>
235 inline RealType quantile(const complemented2_type<normal_distribution<RealType, Policy>, RealType>& c)
237 BOOST_MATH_STD_USING // for ADL of std functions
239 RealType sd = c.dist.standard_deviation();
240 RealType mean = c.dist.mean();
241 static const char* function = "boost::math::quantile(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 RealType q = c.param;
248 if(false == detail::check_probability(function, q, &result, Policy()))
250 result = boost::math::erfc_inv(2 * q, Policy());
251 result *= sd * constants::root_two<RealType>();
256 template <class RealType, class Policy>
257 inline RealType mean(const normal_distribution<RealType, Policy>& dist)
262 template <class RealType, class Policy>
263 inline RealType standard_deviation(const normal_distribution<RealType, Policy>& dist)
265 return dist.standard_deviation();
268 template <class RealType, class Policy>
269 inline RealType mode(const normal_distribution<RealType, Policy>& dist)
274 template <class RealType, class Policy>
275 inline RealType median(const normal_distribution<RealType, Policy>& dist)
280 template <class RealType, class Policy>
281 inline RealType skewness(const normal_distribution<RealType, Policy>& /*dist*/)
286 template <class RealType, class Policy>
287 inline RealType kurtosis(const normal_distribution<RealType, Policy>& /*dist*/)
292 template <class RealType, class Policy>
293 inline RealType kurtosis_excess(const normal_distribution<RealType, Policy>& /*dist*/)
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>
306 #endif // BOOST_STATS_NORMAL_HPP