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