1 // Copyright Paul A. Bristow 2010.
2 // Copyright John Maddock 2007.
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
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 <pch.hpp> // include directory /libs/math/src/tr1/ is needed.
21 #pragma warning (disable: 4127) // conditional expression is constant
22 // caused by using if(std::numeric_limits<RealType>::has_infinity)
23 // and if (std::numeric_limits<RealType>::has_quiet_NaN)
26 #include <boost/math/concepts/real_concept.hpp> // for real_concept
27 #include <boost/test/test_exec_monitor.hpp> // Boost.Test
28 #include <boost/test/floating_point_comparison.hpp>
30 #include <boost/math/distributions/normal.hpp>
31 using boost::math::normal_distribution;
32 #include <boost/math/tools/test.hpp>
37 using std::setprecision;
39 using std::numeric_limits;
41 template <class RealType>
42 RealType NaivePDF(RealType mean, RealType sd, RealType x)
44 // Deliberately naive PDF calculator again which
45 // we'll compare our pdf function. However some
46 // published values to compare against would be better....
48 return exp(-(x-mean)*(x-mean)/(2*sd*sd))/(sd * sqrt(2*boost::math::constants::pi<RealType>()));
51 template <class RealType>
52 void check_normal(RealType mean, RealType sd, RealType x, RealType p, RealType q, RealType tol)
56 normal_distribution<RealType>(mean, sd), // distribution.
57 x), // random variable.
63 normal_distribution<RealType>(mean, sd), // distribution.
64 x)), // random variable.
65 q, // probability complement.
68 ::boost::math::quantile(
69 normal_distribution<RealType>(mean, sd), // distribution.
71 x, // random variable.
74 ::boost::math::quantile(
76 normal_distribution<RealType>(mean, sd), // distribution.
77 q)), // probability complement.
78 x, // random variable.
82 template <class RealType>
83 void test_spots(RealType)
85 // Basic sanity checks
86 RealType tolerance = 1e-2f; // 1e-4 (as %)
87 // Some tests only pass at 1e-4 because values generated by
88 // http://faculty.vassar.edu/lowry/VassarStats.html
89 // give only 5 or 6 *fixed* places, so small values have fewer digits.
91 // Check some bad parameters to the distribution,
92 BOOST_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(0, 0), std::domain_error); // zero sd
93 BOOST_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(0, -1), std::domain_error); // negative sd
95 // Tests on extreme values of random variate x, if has numeric_limit infinity etc.
96 normal_distribution<RealType> N01;
97 if(std::numeric_limits<RealType>::has_infinity)
99 BOOST_CHECK_EQUAL(pdf(N01, +std::numeric_limits<RealType>::infinity()), 0); // x = + infinity, pdf = 0
100 BOOST_CHECK_EQUAL(pdf(N01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, pdf = 0
101 BOOST_CHECK_EQUAL(cdf(N01, +std::numeric_limits<RealType>::infinity()), 1); // x = + infinity, cdf = 1
102 BOOST_CHECK_EQUAL(cdf(N01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, cdf = 0
103 BOOST_CHECK_EQUAL(cdf(complement(N01, +std::numeric_limits<RealType>::infinity())), 0); // x = + infinity, c cdf = 0
104 BOOST_CHECK_EQUAL(cdf(complement(N01, -std::numeric_limits<RealType>::infinity())), 1); // x = - infinity, c cdf = 1
105 BOOST_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
106 BOOST_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
107 BOOST_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
110 if (std::numeric_limits<RealType>::has_quiet_NaN)
112 // No longer allow x to be NaN, then these tests should throw.
113 BOOST_CHECK_THROW(pdf(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
114 BOOST_CHECK_THROW(cdf(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
115 BOOST_CHECK_THROW(cdf(complement(N01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
116 BOOST_CHECK_THROW(quantile(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + infinity
117 BOOST_CHECK_THROW(quantile(complement(N01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + infinity
120 cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
123 static_cast<RealType>(5),
124 static_cast<RealType>(2),
125 static_cast<RealType>(4.8),
126 static_cast<RealType>(0.46017),
127 static_cast<RealType>(1 - 0.46017),
131 static_cast<RealType>(5),
132 static_cast<RealType>(2),
133 static_cast<RealType>(5.2),
134 static_cast<RealType>(1 - 0.46017),
135 static_cast<RealType>(0.46017),
139 static_cast<RealType>(5),
140 static_cast<RealType>(2),
141 static_cast<RealType>(2.2),
142 static_cast<RealType>(0.08076),
143 static_cast<RealType>(1 - 0.08076),
147 static_cast<RealType>(5),
148 static_cast<RealType>(2),
149 static_cast<RealType>(7.8),
150 static_cast<RealType>(1 - 0.08076),
151 static_cast<RealType>(0.08076),
155 static_cast<RealType>(-3),
156 static_cast<RealType>(5),
157 static_cast<RealType>(-4.5),
158 static_cast<RealType>(0.38209),
159 static_cast<RealType>(1 - 0.38209),
163 static_cast<RealType>(-3),
164 static_cast<RealType>(5),
165 static_cast<RealType>(-1.5),
166 static_cast<RealType>(1 - 0.38209),
167 static_cast<RealType>(0.38209),
171 static_cast<RealType>(-3),
172 static_cast<RealType>(5),
173 static_cast<RealType>(-8.5),
174 static_cast<RealType>(0.13567),
175 static_cast<RealType>(1 - 0.13567),
179 static_cast<RealType>(-3),
180 static_cast<RealType>(5),
181 static_cast<RealType>(2.5),
182 static_cast<RealType>(1 - 0.13567),
183 static_cast<RealType>(0.13567),
187 // Tests for PDF: we know that the peak value is at 1/sqrt(2*pi)
189 tolerance = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage
191 pdf(normal_distribution<RealType>(), static_cast<RealType>(0)),
192 static_cast<RealType>(0.3989422804014326779399460599343818684759L), // 1/sqrt(2*pi)
195 pdf(normal_distribution<RealType>(3), static_cast<RealType>(3)),
196 static_cast<RealType>(0.3989422804014326779399460599343818684759L),
199 pdf(normal_distribution<RealType>(3, 5), static_cast<RealType>(3)),
200 static_cast<RealType>(0.3989422804014326779399460599343818684759L / 5),
204 // Spot checks for mean = -5, sd = 6:
206 for(RealType x = -15; x < 5; x += 0.125)
209 pdf(normal_distribution<RealType>(-5, 6), x),
210 NaivePDF(RealType(-5), RealType(6), x),
214 RealType tol2 = boost::math::tools::epsilon<RealType>() * 5;
215 normal_distribution<RealType> dist(8, 3);
216 RealType x = static_cast<RealType>(0.125);
218 BOOST_MATH_STD_USING // ADL of std math lib names
223 , static_cast<RealType>(8), tol2);
227 , static_cast<RealType>(9), tol2);
230 standard_deviation(dist)
231 , static_cast<RealType>(3), tol2);
235 , pdf(dist, x) / cdf(complement(dist, x)), tol2);
236 // cumulative hazard:
239 , -log(cdf(complement(dist, x))), tol2);
240 // coefficient_of_variation:
242 coefficient_of_variation(dist)
243 , standard_deviation(dist) / mean(dist), tol2);
247 , static_cast<RealType>(8), tol2);
251 , static_cast<RealType>(8), tol2);
256 , static_cast<RealType>(0), tol2);
260 , static_cast<RealType>(3), tol2);
263 kurtosis_excess(dist)
264 , static_cast<RealType>(0), tol2);
266 normal_distribution<RealType> norm01(0, 1); // Test default (0, 1)
269 static_cast<RealType>(0), 0); // Mean == zero
271 normal_distribution<RealType> defsd_norm01(0); // Test default (0, sd = 1)
274 static_cast<RealType>(0), 0); // Mean == zero
276 normal_distribution<RealType> def_norm01; // Test default (0, sd = 1)
279 static_cast<RealType>(0), 0); // Mean == zero
282 standard_deviation(def_norm01),
283 static_cast<RealType>(1), 0); // Mean == zero
286 } // template <class RealType>void test_spots(RealType)
288 int test_main(int, char* [])
290 // Check that can generate normal distribution using the two convenience methods:
291 boost::math::normal myf1(1., 2); // Using typedef
292 normal_distribution<> myf2(1., 2); // Using default RealType double.
293 boost::math::normal myn01; // Use default values.
294 // Note NOT myn01() as the compiler will interpret as a function!
296 // Check the synonyms, provided to allow generic use of find_location and find_scale.
297 BOOST_CHECK_EQUAL(myn01.mean(), myn01.location());
298 BOOST_CHECK_EQUAL(myn01.standard_deviation(), myn01.scale());
300 // Basic sanity-check spot values.
301 // (Parameter value, arbitrarily zero, only communicates the floating point type).
302 test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
303 test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
304 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
305 test_spots(0.0L); // Test long double.
306 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
307 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
310 std::cout << "<note>The long double tests have been disabled on this platform "
311 "either because the long double overloads of the usual math functions are "
312 "not available at all, or because they are too inaccurate for these tests "
313 "to pass.</note>" << std::cout;
317 } // int test_main(int, char* [])
323 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_normal.exe"
324 Running 1 test case...
325 Tolerance for type float is 0.01 %
326 Tolerance for type double is 0.01 %
327 Tolerance for type long double is 0.01 %
328 Tolerance for type class boost::math::concepts::real_concept is 0.01 %
329 *** No errors detected
334 ------ Build started: Project: test_normal, Configuration: Release Win32 ------
337 Finished generating code
338 test_normal.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_normal.exe
339 Running 1 test case...
340 Tolerance for type float is 0.01 %
341 Tolerance for type double is 0.01 %
342 Tolerance for type long double is 0.01 %
343 Tolerance for type class boost::math::concepts::real_concept is 0.01 %
345 *** No errors detected
346 Detected memory leaks!
348 {2413} normal block at 0x00321190, 42 bytes long.
349 Data: <class boost::mat> 63 6C 61 73 73 20 62 6F 6F 73 74 3A 3A 6D 61 74
350 {2412} normal block at 0x003231F0, 8 bytes long.
351 Data: < 2 22 > 90 11 32 00 98 32 32 00
352 {1824} normal block at 0x00323180, 12 bytes long.
353 Data: <long double > 6C 6F 6E 67 20 64 6F 75 62 6C 65 00
354 {1823} normal block at 0x00323298, 8 bytes long.
355 Data: < 12 `22 > 80 31 32 00 60 32 32 00
356 {1227} normal block at 0x00323148, 7 bytes long.
357 Data: <double > 64 6F 75 62 6C 65 00
358 {1226} normal block at 0x00323260, 8 bytes long.
359 Data: <H12 02 > 48 31 32 00 A0 30 32 00
360 {633} normal block at 0x003230D8, 6 bytes long.
361 Data: <float > 66 6C 6F 61 74 00
362 {632} normal block at 0x003230A0, 8 bytes long.
363 Data: < 02 > D8 30 32 00 00 00 00 00
364 Object dump complete.
365 ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========