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/tools/test.hpp>
27 #include <boost/math/concepts/real_concept.hpp> // for real_concept
28 #define BOOST_TEST_MAIN
29 #include <boost/test/unit_test.hpp> // Boost.Test
30 #include <boost/test/tools/floating_point_comparison.hpp>
32 #include <boost/math/distributions/normal.hpp>
33 using boost::math::normal_distribution
;
34 #include <boost/math/tools/test.hpp>
35 #include "test_out_of_range.hpp"
41 using std::setprecision
;
43 using std::numeric_limits
;
45 template <class RealType
>
46 RealType
NaivePDF(RealType mean
, RealType sd
, RealType x
)
48 // Deliberately naive PDF calculator again which
49 // we'll compare our pdf function. However some
50 // published values to compare against would be better....
52 return exp(-(x
-mean
)*(x
-mean
)/(2*sd
*sd
))/(sd
* sqrt(2*boost::math::constants::pi
<RealType
>()));
55 template <class RealType
>
56 void check_normal(RealType mean
, RealType sd
, RealType x
, RealType p
, RealType q
, RealType tol
)
60 normal_distribution
<RealType
>(mean
, sd
), // distribution.
61 x
), // random variable.
67 normal_distribution
<RealType
>(mean
, sd
), // distribution.
68 x
)), // random variable.
69 q
, // probability complement.
72 ::boost::math::quantile(
73 normal_distribution
<RealType
>(mean
, sd
), // distribution.
75 x
, // random variable.
78 ::boost::math::quantile(
80 normal_distribution
<RealType
>(mean
, sd
), // distribution.
81 q
)), // probability complement.
82 x
, // random variable.
86 template <class RealType
>
87 void test_spots(RealType
)
89 // Basic sanity checks
90 RealType tolerance
= 1e-2f
; // 1e-4 (as %)
91 // Some tests only pass at 1e-4 because values generated by
92 // http://faculty.vassar.edu/lowry/VassarStats.html
93 // give only 5 or 6 *fixed* places, so small values have fewer digits.
95 // Check some bad parameters to the distribution,
96 #ifndef BOOST_NO_EXCEPTIONS
97 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
> nbad1(0, 0), std::domain_error
); // zero sd
98 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
> nbad1(0, -1), std::domain_error
); // negative sd
100 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
>(0, 0), std::domain_error
); // zero sd
101 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
>(0, -1), std::domain_error
); // negative sd
104 // Tests on extreme values of random variate x, if has std::numeric_limits infinity etc.
105 normal_distribution
<RealType
> N01
;
106 if(std::numeric_limits
<RealType
>::has_infinity
)
108 BOOST_CHECK_EQUAL(pdf(N01
, +std::numeric_limits
<RealType
>::infinity()), 0); // x = + infinity, pdf = 0
109 BOOST_CHECK_EQUAL(pdf(N01
, -std::numeric_limits
<RealType
>::infinity()), 0); // x = - infinity, pdf = 0
110 BOOST_CHECK_EQUAL(cdf(N01
, +std::numeric_limits
<RealType
>::infinity()), 1); // x = + infinity, cdf = 1
111 BOOST_CHECK_EQUAL(cdf(N01
, -std::numeric_limits
<RealType
>::infinity()), 0); // x = - infinity, cdf = 0
112 BOOST_CHECK_EQUAL(cdf(complement(N01
, +std::numeric_limits
<RealType
>::infinity())), 0); // x = + infinity, c cdf = 0
113 BOOST_CHECK_EQUAL(cdf(complement(N01
, -std::numeric_limits
<RealType
>::infinity())), 1); // x = - infinity, c cdf = 1
114 #ifndef BOOST_NO_EXCEPTIONS
115 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
> nbad1(std::numeric_limits
<RealType
>::infinity(), static_cast<RealType
>(1)), std::domain_error
); // +infinite mean
116 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
> nbad1(-std::numeric_limits
<RealType
>::infinity(), static_cast<RealType
>(1)), std::domain_error
); // -infinite mean
117 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
> nbad1(static_cast<RealType
>(0), std::numeric_limits
<RealType
>::infinity()), std::domain_error
); // infinite sd
119 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
>(std::numeric_limits
<RealType
>::infinity(), static_cast<RealType
>(1)), std::domain_error
); // +infinite mean
120 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
>(-std::numeric_limits
<RealType
>::infinity(), static_cast<RealType
>(1)), std::domain_error
); // -infinite mean
121 BOOST_MATH_CHECK_THROW(boost::math::normal_distribution
<RealType
>(static_cast<RealType
>(0), std::numeric_limits
<RealType
>::infinity()), std::domain_error
); // infinite sd
125 if (std::numeric_limits
<RealType
>::has_quiet_NaN
)
127 // No longer allow x to be NaN, then these tests should throw.
128 BOOST_MATH_CHECK_THROW(pdf(N01
, +std::numeric_limits
<RealType
>::quiet_NaN()), std::domain_error
); // x = NaN
129 BOOST_MATH_CHECK_THROW(cdf(N01
, +std::numeric_limits
<RealType
>::quiet_NaN()), std::domain_error
); // x = NaN
130 BOOST_MATH_CHECK_THROW(cdf(complement(N01
, +std::numeric_limits
<RealType
>::quiet_NaN())), std::domain_error
); // x = + infinity
131 BOOST_MATH_CHECK_THROW(quantile(N01
, +std::numeric_limits
<RealType
>::quiet_NaN()), std::domain_error
); // p = + infinity
132 BOOST_MATH_CHECK_THROW(quantile(complement(N01
, +std::numeric_limits
<RealType
>::quiet_NaN())), std::domain_error
); // p = + infinity
135 cout
<< "Tolerance for type " << typeid(RealType
).name() << " is " << tolerance
<< " %" << endl
;
138 static_cast<RealType
>(5),
139 static_cast<RealType
>(2),
140 static_cast<RealType
>(4.8),
141 static_cast<RealType
>(0.46017),
142 static_cast<RealType
>(1 - 0.46017),
146 static_cast<RealType
>(5),
147 static_cast<RealType
>(2),
148 static_cast<RealType
>(5.2),
149 static_cast<RealType
>(1 - 0.46017),
150 static_cast<RealType
>(0.46017),
154 static_cast<RealType
>(5),
155 static_cast<RealType
>(2),
156 static_cast<RealType
>(2.2),
157 static_cast<RealType
>(0.08076),
158 static_cast<RealType
>(1 - 0.08076),
162 static_cast<RealType
>(5),
163 static_cast<RealType
>(2),
164 static_cast<RealType
>(7.8),
165 static_cast<RealType
>(1 - 0.08076),
166 static_cast<RealType
>(0.08076),
170 static_cast<RealType
>(-3),
171 static_cast<RealType
>(5),
172 static_cast<RealType
>(-4.5),
173 static_cast<RealType
>(0.38209),
174 static_cast<RealType
>(1 - 0.38209),
178 static_cast<RealType
>(-3),
179 static_cast<RealType
>(5),
180 static_cast<RealType
>(-1.5),
181 static_cast<RealType
>(1 - 0.38209),
182 static_cast<RealType
>(0.38209),
186 static_cast<RealType
>(-3),
187 static_cast<RealType
>(5),
188 static_cast<RealType
>(-8.5),
189 static_cast<RealType
>(0.13567),
190 static_cast<RealType
>(1 - 0.13567),
194 static_cast<RealType
>(-3),
195 static_cast<RealType
>(5),
196 static_cast<RealType
>(2.5),
197 static_cast<RealType
>(1 - 0.13567),
198 static_cast<RealType
>(0.13567),
202 // Tests for PDF: we know that the peak value is at 1/sqrt(2*pi)
204 tolerance
= boost::math::tools::epsilon
<RealType
>() * 5 * 100; // 5 eps as a percentage
206 pdf(normal_distribution
<RealType
>(), static_cast<RealType
>(0)),
207 static_cast<RealType
>(0.3989422804014326779399460599343818684759L), // 1/sqrt(2*pi)
210 pdf(normal_distribution
<RealType
>(3), static_cast<RealType
>(3)),
211 static_cast<RealType
>(0.3989422804014326779399460599343818684759L),
214 pdf(normal_distribution
<RealType
>(3, 5), static_cast<RealType
>(3)),
215 static_cast<RealType
>(0.3989422804014326779399460599343818684759L / 5),
219 // Spot checks for mean = -5, sd = 6:
221 for(RealType x
= -15; x
< 5; x
+= 0.125)
224 pdf(normal_distribution
<RealType
>(-5, 6), x
),
225 NaivePDF(RealType(-5), RealType(6), x
),
229 RealType tol2
= boost::math::tools::epsilon
<RealType
>() * 5;
230 normal_distribution
<RealType
> dist(8, 3);
231 RealType x
= static_cast<RealType
>(0.125);
233 BOOST_MATH_STD_USING
// ADL of std math lib names
238 , static_cast<RealType
>(8), tol2
);
242 , static_cast<RealType
>(9), tol2
);
245 standard_deviation(dist
)
246 , static_cast<RealType
>(3), tol2
);
250 , pdf(dist
, x
) / cdf(complement(dist
, x
)), tol2
);
251 // cumulative hazard:
254 , -log(cdf(complement(dist
, x
))), tol2
);
255 // coefficient_of_variation:
257 coefficient_of_variation(dist
)
258 , standard_deviation(dist
) / mean(dist
), tol2
);
262 , static_cast<RealType
>(8), tol2
);
266 , static_cast<RealType
>(8), tol2
);
271 , static_cast<RealType
>(0), tol2
);
275 , static_cast<RealType
>(3), tol2
);
278 kurtosis_excess(dist
)
279 , static_cast<RealType
>(0), tol2
);
281 RealType expected_entropy
= log(boost::math::constants::two_pi
<RealType
>()*boost::math::constants::e
<RealType
>()*9)/2;
284 ,expected_entropy
, tol2
);
286 normal_distribution
<RealType
> norm01(0, 1); // Test default (0, 1)
289 static_cast<RealType
>(0), 0); // Mean == zero
291 normal_distribution
<RealType
> defsd_norm01(0); // Test default (0, sd = 1)
294 static_cast<RealType
>(0), 0); // Mean == zero
296 normal_distribution
<RealType
> def_norm01
; // Test default (0, sd = 1)
299 static_cast<RealType
>(0), 0); // Mean == zero
302 standard_deviation(def_norm01
),
303 static_cast<RealType
>(1), 0); // Mean == zero
306 check_out_of_range
<boost::math::normal_distribution
<RealType
> >(0, 1); // (All) valid constructor parameter values.
308 BOOST_MATH_CHECK_THROW(pdf(normal_distribution
<RealType
>(0, 0), 0), std::domain_error
);
309 BOOST_MATH_CHECK_THROW(pdf(normal_distribution
<RealType
>(0, -1), 0), std::domain_error
);
310 BOOST_MATH_CHECK_THROW(quantile(normal_distribution
<RealType
>(0, 1), -1), std::domain_error
);
311 BOOST_MATH_CHECK_THROW(quantile(normal_distribution
<RealType
>(0, 1), 2), std::domain_error
);
312 } // template <class RealType>void test_spots(RealType)
314 BOOST_AUTO_TEST_CASE( test_main
)
316 // Check that can generate normal distribution using the two convenience methods:
317 boost::math::normal
myf1(1., 2); // Using typedef
318 normal_distribution
<> myf2(1., 2); // Using default RealType double.
319 boost::math::normal myn01
; // Use default values.
320 // Note NOT myn01() as the compiler will interpret as a function!
322 // Check the synonyms, provided to allow generic use of find_location and find_scale.
323 BOOST_CHECK_EQUAL(myn01
.mean(), myn01
.location());
324 BOOST_CHECK_EQUAL(myn01
.standard_deviation(), myn01
.scale());
326 // Basic sanity-check spot values.
327 // (Parameter value, arbitrarily zero, only communicates the floating point type).
328 test_spots(0.0F
); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
329 test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
330 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
331 test_spots(0.0L); // Test long double.
332 #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0582))
333 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
336 std::cout
<< "<note>The long double tests have been disabled on this platform "
337 "either because the long double overloads of the usual math functions are "
338 "not available at all, or because they are too inaccurate for these tests "
339 "to pass.</note>" << std::endl
;
343 } // BOOST_AUTO_TEST_CASE( test_main )
349 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_normal.exe"
350 Running 1 test case...
351 Tolerance for type float is 0.01 %
352 Tolerance for type double is 0.01 %
353 Tolerance for type long double is 0.01 %
354 Tolerance for type class boost::math::concepts::real_concept is 0.01 %
355 *** No errors detected
360 ------ Build started: Project: test_normal, Configuration: Release Win32 ------
363 Finished generating code
364 test_normal.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_normal.exe
365 Running 1 test case...
366 Tolerance for type float is 0.01 %
367 Tolerance for type double is 0.01 %
368 Tolerance for type long double is 0.01 %
369 Tolerance for type class boost::math::concepts::real_concept is 0.01 %
371 *** No errors detected
372 Detected memory leaks!
374 {2413} normal block at 0x00321190, 42 bytes long.
375 Data: <class boost::mat> 63 6C 61 73 73 20 62 6F 6F 73 74 3A 3A 6D 61 74
376 {2412} normal block at 0x003231F0, 8 bytes long.
377 Data: < 2 22 > 90 11 32 00 98 32 32 00
378 {1824} normal block at 0x00323180, 12 bytes long.
379 Data: <long double > 6C 6F 6E 67 20 64 6F 75 62 6C 65 00
380 {1823} normal block at 0x00323298, 8 bytes long.
381 Data: < 12 `22 > 80 31 32 00 60 32 32 00
382 {1227} normal block at 0x00323148, 7 bytes long.
383 Data: <double > 64 6F 75 62 6C 65 00
384 {1226} normal block at 0x00323260, 8 bytes long.
385 Data: <H12 02 > 48 31 32 00 A0 30 32 00
386 {633} normal block at 0x003230D8, 6 bytes long.
387 Data: <float > 66 6C 6F 61 74 00
388 {632} normal block at 0x003230A0, 8 bytes long.
389 Data: < 02 > D8 30 32 00 00 00 00 00
390 Object dump complete.
391 ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========