Imported Upstream version 1.49.0
[platform/upstream/boost.git] / libs / math / test / test_normal.cpp
1 // Copyright Paul A. Bristow 2010.
2 // Copyright John Maddock 2007.
3
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)
8
9 // test_normal.cpp
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 <pch.hpp> // include directory /libs/math/src/tr1/ is needed.
19
20 #ifdef _MSC_VER
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)
24 #endif
25
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>
29
30 #include <boost/math/distributions/normal.hpp>
31     using boost::math::normal_distribution;
32 #include <boost/math/tools/test.hpp>
33
34 #include <iostream>
35    using std::cout;
36    using std::endl;
37    using std::setprecision;
38 #include <limits>
39   using std::numeric_limits;
40
41 template <class RealType>
42 RealType NaivePDF(RealType mean, RealType sd, RealType x)
43 {
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....
47    using namespace std;
48    return exp(-(x-mean)*(x-mean)/(2*sd*sd))/(sd * sqrt(2*boost::math::constants::pi<RealType>()));
49 }
50
51 template <class RealType>
52 void check_normal(RealType mean, RealType sd, RealType x, RealType p, RealType q, RealType tol)
53 {
54    BOOST_CHECK_CLOSE(
55       ::boost::math::cdf(
56          normal_distribution<RealType>(mean, sd),       // distribution.
57          x),                                            // random variable.
58          p,                                             // probability.
59          tol);                                          // %tolerance.
60    BOOST_CHECK_CLOSE(
61       ::boost::math::cdf(
62          complement(
63             normal_distribution<RealType>(mean, sd),    // distribution.
64             x)),                                        // random variable.
65          q,                                             // probability complement.
66          tol);                                          // %tolerance.
67    BOOST_CHECK_CLOSE(
68       ::boost::math::quantile(
69          normal_distribution<RealType>(mean, sd),       // distribution.
70          p),                                            // probability.
71          x,                                             // random variable.
72          tol);                                          // %tolerance.
73    BOOST_CHECK_CLOSE(
74       ::boost::math::quantile(
75          complement(
76             normal_distribution<RealType>(mean, sd),    // distribution.
77             q)),                                        // probability complement.
78          x,                                             // random variable.
79          tol);                                          // %tolerance.
80 }
81
82 template <class RealType>
83 void test_spots(RealType)
84 {
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.
90
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
94
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)
98   {
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
108   }
109
110   if (std::numeric_limits<RealType>::has_quiet_NaN)
111   {
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
118   }
119
120    cout << "Tolerance for type " << typeid(RealType).name()  << " is " << tolerance << " %" << endl;
121
122    check_normal(
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),
128       tolerance);
129
130    check_normal(
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),
136       tolerance);
137
138    check_normal(
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),
144       tolerance);
145
146    check_normal(
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),
152       tolerance);
153
154    check_normal(
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),
160       tolerance);
161
162    check_normal(
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),
168       tolerance);
169
170    check_normal(
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),
176       tolerance);
177
178    check_normal(
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),
184       tolerance);
185
186    //
187    // Tests for PDF: we know that the peak value is at 1/sqrt(2*pi)
188    //
189    tolerance = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage
190    BOOST_CHECK_CLOSE(
191       pdf(normal_distribution<RealType>(), static_cast<RealType>(0)),
192       static_cast<RealType>(0.3989422804014326779399460599343818684759L), // 1/sqrt(2*pi)
193       tolerance);
194    BOOST_CHECK_CLOSE(
195       pdf(normal_distribution<RealType>(3), static_cast<RealType>(3)),
196       static_cast<RealType>(0.3989422804014326779399460599343818684759L),
197       tolerance);
198    BOOST_CHECK_CLOSE(
199       pdf(normal_distribution<RealType>(3, 5), static_cast<RealType>(3)),
200       static_cast<RealType>(0.3989422804014326779399460599343818684759L / 5),
201       tolerance);
202
203    //
204    // Spot checks for mean = -5, sd = 6:
205    //
206    for(RealType x = -15; x < 5; x += 0.125)
207    {
208       BOOST_CHECK_CLOSE(
209          pdf(normal_distribution<RealType>(-5, 6), x),
210          NaivePDF(RealType(-5), RealType(6), x),
211          tolerance);
212    }
213
214     RealType tol2 = boost::math::tools::epsilon<RealType>() * 5;
215     normal_distribution<RealType> dist(8, 3);
216     RealType x = static_cast<RealType>(0.125);
217
218     BOOST_MATH_STD_USING // ADL of std math lib names
219
220     // mean:
221     BOOST_CHECK_CLOSE(
222        mean(dist)
223        , static_cast<RealType>(8), tol2);
224     // variance:
225     BOOST_CHECK_CLOSE(
226        variance(dist)
227        , static_cast<RealType>(9), tol2);
228     // std deviation:
229     BOOST_CHECK_CLOSE(
230        standard_deviation(dist)
231        , static_cast<RealType>(3), tol2);
232     // hazard:
233     BOOST_CHECK_CLOSE(
234        hazard(dist, x)
235        , pdf(dist, x) / cdf(complement(dist, x)), tol2);
236     // cumulative hazard:
237     BOOST_CHECK_CLOSE(
238        chf(dist, x)
239        , -log(cdf(complement(dist, x))), tol2);
240     // coefficient_of_variation:
241     BOOST_CHECK_CLOSE(
242        coefficient_of_variation(dist)
243        , standard_deviation(dist) / mean(dist), tol2);
244     // mode:
245     BOOST_CHECK_CLOSE(
246        mode(dist)
247        , static_cast<RealType>(8), tol2);
248
249     BOOST_CHECK_CLOSE(
250        median(dist)
251        , static_cast<RealType>(8), tol2);
252
253     // skewness:
254     BOOST_CHECK_CLOSE(
255        skewness(dist)
256        , static_cast<RealType>(0), tol2);
257     // kertosis:
258     BOOST_CHECK_CLOSE(
259        kurtosis(dist)
260        , static_cast<RealType>(3), tol2);
261     // kertosis excess:
262     BOOST_CHECK_CLOSE(
263        kurtosis_excess(dist)
264        , static_cast<RealType>(0), tol2);
265
266     normal_distribution<RealType> norm01(0, 1); // Test default (0, 1)
267     BOOST_CHECK_CLOSE(
268        mean(norm01),
269        static_cast<RealType>(0), 0); // Mean == zero
270
271     normal_distribution<RealType> defsd_norm01(0); // Test default (0, sd = 1)
272     BOOST_CHECK_CLOSE(
273        mean(defsd_norm01),
274        static_cast<RealType>(0), 0); // Mean == zero
275
276     normal_distribution<RealType> def_norm01; // Test default (0, sd = 1)
277     BOOST_CHECK_CLOSE(
278        mean(def_norm01),
279        static_cast<RealType>(0), 0); // Mean == zero
280
281     BOOST_CHECK_CLOSE(
282        standard_deviation(def_norm01),
283        static_cast<RealType>(1), 0); // Mean == zero
284
285
286 } // template <class RealType>void test_spots(RealType)
287
288 int test_main(int, char* [])
289 {
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!
295
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());
299
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.
308 #endif
309 #else
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;
314 #endif
315
316    return 0;
317 } // int test_main(int, char* [])
318
319 /*
320
321 Output:
322
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
330
331
332
333
334 ------ Build started: Project: test_normal, Configuration: Release Win32 ------
335   test_normal.cpp
336   Generating code
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 %
344   
345   *** No errors detected
346   Detected memory leaks!
347   Dumping objects ->
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 ==========
366
367
368 */
369
370