Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / math / test / test_skew_normal.cpp
1 // Copyright Paul A. Bristow 2012.
2 // Copyright John Maddock 2012.
3 // Copyright Benjamin Sobotta 2012
4
5 // Use, modification and distribution are subject to the
6 // Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt
8 // or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 #ifdef _MSC_VER
11 #  pragma warning (disable : 4127) // conditional expression is constant.
12 #  pragma warning (disable : 4305) // 'initializing' : truncation from 'double' to 'const float'.
13 #  pragma warning (disable : 4310) // cast truncates constant value.
14 #  pragma warning (disable : 4512) // assignment operator could not be generated.
15 #endif
16
17 //#include <pch.hpp> // include directory libs/math/src/tr1/ is needed.
18
19 #include <boost/math/concepts/real_concept.hpp> // for real_concept
20 #include <boost/test/test_exec_monitor.hpp> // Boost.Test
21 #include <boost/test/floating_point_comparison.hpp>
22
23 #include <boost/math/distributions/skew_normal.hpp>
24 using boost::math::skew_normal_distribution;
25 using boost::math::skew_normal;
26
27 #include <iostream>
28 using std::cout;
29 using std::endl;
30 using std::setprecision;
31 #include <limits>
32 using std::numeric_limits;
33
34 template <class RealType>
35 void check_skew_normal(RealType mean, RealType scale, RealType shape, RealType x, RealType p, RealType q, RealType tol)
36 {
37  using boost::math::skew_normal_distribution;
38
39   BOOST_CHECK_CLOSE_FRACTION(
40     ::boost::math::cdf(   // Check cdf
41     skew_normal_distribution<RealType>(mean, scale, shape),      // distribution.
42     x),    // random variable.
43     p,     // probability.
44     tol);   // tolerance.
45   BOOST_CHECK_CLOSE_FRACTION(
46     ::boost::math::cdf( // Check cdf complement
47     complement( 
48     skew_normal_distribution<RealType>(mean, scale, shape),   // distribution.
49     x)),   // random variable.
50     q,      // probability complement.
51     tol);    // %tolerance.
52   BOOST_CHECK_CLOSE_FRACTION(
53     ::boost::math::quantile( // Check quantile
54     skew_normal_distribution<RealType>(mean, scale, shape),    // distribution.
55     p),   // probability.
56     x,   // random variable.
57     tol);   // tolerance.
58   BOOST_CHECK_CLOSE_FRACTION(
59     ::boost::math::quantile( // Check quantile complement
60     complement(
61     skew_normal_distribution<RealType>(mean, scale, shape),   // distribution.
62     q)),   // probability complement.
63     x,     // random variable.
64     tol);  // tolerance.
65
66    skew_normal_distribution<RealType> dist (mean, scale, shape);
67
68    if((p < 0.999) && (q < 0.999))
69    {  // We can only check this if P is not too close to 1,
70       // so that we can guarantee Q is accurate:
71       BOOST_CHECK_CLOSE_FRACTION(
72         cdf(complement(dist, x)), q, tol); // 1 - cdf
73       BOOST_CHECK_CLOSE_FRACTION(
74         quantile(dist, p), x, tol); // quantile(cdf) = x
75       BOOST_CHECK_CLOSE_FRACTION(
76         quantile(complement(dist, q)), x, tol); // quantile(complement(1 - cdf)) = x
77    }
78 } // template <class RealType>void check_skew_normal()
79
80
81 template <class RealType>
82 void test_spots(RealType)
83 {
84    // Basic sanity checks
85    RealType tolerance = 1e-4f; // 1e-4 (as %)
86
87   // Check some bad parameters to the distribution,
88
89    BOOST_CHECK_THROW(boost::math::skew_normal_distribution<RealType> nbad1(0, 0), std::domain_error); // zero sd
90    BOOST_CHECK_THROW(boost::math::skew_normal_distribution<RealType> nbad1(0, -1), std::domain_error); // negative sd
91
92   // Tests on extreme values of random variate x, if has numeric_limit infinity etc.
93     skew_normal_distribution<RealType> N01;
94   if(std::numeric_limits<RealType>::has_infinity)
95   {
96     BOOST_CHECK_EQUAL(pdf(N01, +std::numeric_limits<RealType>::infinity()), 0); // x = + infinity, pdf = 0
97     BOOST_CHECK_EQUAL(pdf(N01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, pdf = 0
98     BOOST_CHECK_EQUAL(cdf(N01, +std::numeric_limits<RealType>::infinity()), 1); // x = + infinity, cdf = 1
99     BOOST_CHECK_EQUAL(cdf(N01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, cdf = 0
100     BOOST_CHECK_EQUAL(cdf(complement(N01, +std::numeric_limits<RealType>::infinity())), 0); // x = + infinity, c cdf = 0
101     BOOST_CHECK_EQUAL(cdf(complement(N01, -std::numeric_limits<RealType>::infinity())), 1); // x = - infinity, c cdf = 1
102     BOOST_CHECK_THROW(boost::math::skew_normal_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
103     BOOST_CHECK_THROW(boost::math::skew_normal_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(),  static_cast<RealType>(1)), std::domain_error); // -infinite mean
104     BOOST_CHECK_THROW(boost::math::skew_normal_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
105   }
106
107   if (std::numeric_limits<RealType>::has_quiet_NaN)
108   {
109     // No longer allow x to be NaN, then these tests should throw.
110     BOOST_CHECK_THROW(pdf(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
111     BOOST_CHECK_THROW(cdf(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
112     BOOST_CHECK_THROW(cdf(complement(N01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
113     BOOST_CHECK_THROW(quantile(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + infinity
114     BOOST_CHECK_THROW(quantile(complement(N01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + infinity
115   }
116
117    cout << "Tolerance for type " << typeid(RealType).name()  << " is " << tolerance << " %" << endl;
118
119    // Tests where shape = 0, so same as normal tests.
120    // (These might be removed later).
121    check_skew_normal(
122       static_cast<RealType>(5),
123       static_cast<RealType>(2),
124       static_cast<RealType>(0),
125       static_cast<RealType>(4.8),
126       static_cast<RealType>(0.46017),
127       static_cast<RealType>(1 - 0.46017),
128       tolerance);
129
130    check_skew_normal(
131       static_cast<RealType>(5),
132       static_cast<RealType>(2),
133       static_cast<RealType>(0),
134       static_cast<RealType>(5.2),
135       static_cast<RealType>(1 - 0.46017),
136       static_cast<RealType>(0.46017),
137       tolerance);
138
139    check_skew_normal(
140       static_cast<RealType>(5),
141       static_cast<RealType>(2),
142       static_cast<RealType>(0),
143       static_cast<RealType>(2.2),
144       static_cast<RealType>(0.08076),
145       static_cast<RealType>(1 - 0.08076),
146       tolerance);
147
148    check_skew_normal(
149       static_cast<RealType>(5),
150       static_cast<RealType>(2),
151       static_cast<RealType>(0),
152       static_cast<RealType>(7.8),
153       static_cast<RealType>(1 - 0.08076),
154       static_cast<RealType>(0.08076),
155       tolerance);
156
157    check_skew_normal(
158       static_cast<RealType>(-3),
159       static_cast<RealType>(5),
160       static_cast<RealType>(0),
161       static_cast<RealType>(-4.5),
162       static_cast<RealType>(0.38209),
163       static_cast<RealType>(1 - 0.38209),
164       tolerance);
165
166    check_skew_normal(
167       static_cast<RealType>(-3),
168       static_cast<RealType>(5),
169       static_cast<RealType>(0),
170       static_cast<RealType>(-1.5),
171       static_cast<RealType>(1 - 0.38209),
172       static_cast<RealType>(0.38209),
173       tolerance);
174
175    check_skew_normal(
176       static_cast<RealType>(-3),
177       static_cast<RealType>(5),
178       static_cast<RealType>(0),
179       static_cast<RealType>(-8.5),
180       static_cast<RealType>(0.13567),
181       static_cast<RealType>(1 - 0.13567),
182       tolerance);
183
184    check_skew_normal(
185       static_cast<RealType>(-3),
186       static_cast<RealType>(5),
187       static_cast<RealType>(0),
188       static_cast<RealType>(2.5),
189       static_cast<RealType>(1 - 0.13567),
190       static_cast<RealType>(0.13567),
191       tolerance);
192
193    // Tests where shape != 0, specific to skew_normal distribution.
194    //void check_skew_normal(RealType mean, RealType scale, RealType shape, RealType x, RealType p, RealType q, RealType tol)
195       check_skew_normal( // 1st R example.
196       static_cast<RealType>(1.1),
197       static_cast<RealType>(2.2),
198       static_cast<RealType>(-3.3),
199       static_cast<RealType>(0.4), // x
200       static_cast<RealType>(0.733918618927874), // p == psn
201       static_cast<RealType>(1 - 0.733918618927874), // q 
202       tolerance);
203
204    // Not sure about these yet.
205       //check_skew_normal( // 2nd R example.
206       //static_cast<RealType>(1.1),
207       //static_cast<RealType>(0.02),
208       //static_cast<RealType>(0.03),
209       //static_cast<RealType>(1.3), // x
210       //static_cast<RealType>(0.01), // p
211       //static_cast<RealType>(0.09), // q
212       //tolerance);
213       //check_skew_normal( // 3nd R example.
214       //static_cast<RealType>(10.1),
215       //static_cast<RealType>(5.),
216       //static_cast<RealType>(-0.03),
217       //static_cast<RealType>(-1.3), // x
218       //static_cast<RealType>(0.01201290665838824), // p
219       //static_cast<RealType>(1. - 0.01201290665838824), // q 0.987987101
220       //tolerance);
221
222     // Tests for PDF: we know that the normal peak value is at 1/sqrt(2*pi)
223    //
224    tolerance = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction
225    BOOST_CHECK_CLOSE_FRACTION(
226       pdf(skew_normal_distribution<RealType>(), static_cast<RealType>(0)),
227       static_cast<RealType>(0.3989422804014326779399460599343818684759L), // 1/sqrt(2*pi)
228       tolerance);
229    BOOST_CHECK_CLOSE_FRACTION(
230       pdf(skew_normal_distribution<RealType>(3), static_cast<RealType>(3)),
231       static_cast<RealType>(0.3989422804014326779399460599343818684759L),
232       tolerance);
233    BOOST_CHECK_CLOSE_FRACTION(
234       pdf(skew_normal_distribution<RealType>(3, 5), static_cast<RealType>(3)),
235       static_cast<RealType>(0.3989422804014326779399460599343818684759L / 5),
236       tolerance);
237
238    // Shape != 0.
239    BOOST_CHECK_CLOSE_FRACTION(
240       pdf(skew_normal_distribution<RealType>(3,5,1e-6), static_cast<RealType>(3)),
241       static_cast<RealType>(0.3989422804014326779399460599343818684759L / 5),
242       tolerance);
243
244
245    // Checks on mean, variance cumulants etc.
246    // Checks on shape ==0
247
248     RealType tol5 = boost::math::tools::epsilon<RealType>() * 5;
249     skew_normal_distribution<RealType> dist(8, 3);
250     RealType x = static_cast<RealType>(0.125);
251
252     BOOST_MATH_STD_USING // ADL of std math lib names
253
254     // mean:
255     BOOST_CHECK_CLOSE(
256        mean(dist)
257        , static_cast<RealType>(8), tol5);
258     // variance:
259     BOOST_CHECK_CLOSE(
260        variance(dist)
261        , static_cast<RealType>(9), tol5);
262     // std deviation:
263     BOOST_CHECK_CLOSE(
264        standard_deviation(dist)
265        , static_cast<RealType>(3), tol5);
266     // hazard:
267     BOOST_CHECK_CLOSE(
268        hazard(dist, x)
269        , pdf(dist, x) / cdf(complement(dist, x)), tol5);
270     // cumulative hazard:
271     BOOST_CHECK_CLOSE(
272        chf(dist, x)
273        , -log(cdf(complement(dist, x))), tol5);
274     // coefficient_of_variation:
275     BOOST_CHECK_CLOSE(
276        coefficient_of_variation(dist)
277        , standard_deviation(dist) / mean(dist), tol5);
278     // mode: 
279     BOOST_CHECK_CLOSE_FRACTION(mode(dist), static_cast<RealType>(8), 0.001);
280
281     BOOST_CHECK_CLOSE(
282        median(dist)
283        , static_cast<RealType>(8), tol5);
284
285     // skewness:
286     BOOST_CHECK_CLOSE(
287        skewness(dist)
288        , static_cast<RealType>(0), tol5);
289     // kurtosis:
290     BOOST_CHECK_CLOSE(
291        kurtosis(dist)
292        , static_cast<RealType>(3), tol5);
293     // kurtosis excess:
294     BOOST_CHECK_CLOSE(
295        kurtosis_excess(dist)
296        , static_cast<RealType>(0), tol5);
297
298     skew_normal_distribution<RealType> norm01(0, 1); // Test default (0, 1)
299     BOOST_CHECK_CLOSE(
300        mean(norm01),
301        static_cast<RealType>(0), 0); // Mean == zero
302
303     skew_normal_distribution<RealType> defsd_norm01(0); // Test default (0, sd = 1)
304     BOOST_CHECK_CLOSE(
305        mean(defsd_norm01),
306        static_cast<RealType>(0), 0); // Mean == zero
307
308     skew_normal_distribution<RealType> def_norm01; // Test default (0, sd = 1)
309     BOOST_CHECK_CLOSE(
310        mean(def_norm01),
311        static_cast<RealType>(0), 0); // Mean == zero
312
313     BOOST_CHECK_CLOSE(
314        standard_deviation(def_norm01),
315        static_cast<RealType>(1), 0);  // 
316
317     BOOST_CHECK_CLOSE(
318        mode(def_norm01),
319        static_cast<RealType>(0), 0); // Mode == zero
320
321
322     // Skew_normal tests with shape != 0.
323     {
324       //RealType tol5 = boost::math::tools::epsilon<RealType>() * 5;
325       RealType tol100 = boost::math::tools::epsilon<RealType>() * 100;
326       RealType tol1000 = boost::math::tools::epsilon<RealType>() * 1000;
327
328       //skew_normal_distribution<RealType> dist(1.1, 0.02, 0.03);
329
330       BOOST_MATH_STD_USING // ADL of std math lib names.
331
332       // Test values from R = see skew_normal_drv.cpp which included the R code used.
333       {
334         skew_normal_distribution<RealType> dist(static_cast<RealType>(1.1l), static_cast<RealType>(2.2l), static_cast<RealType>(-3.3l));
335
336         BOOST_CHECK_CLOSE(      // mean:
337            mean(dist)
338            , static_cast<RealType>(-0.579908992539856825862549L), tol100);
339          BOOST_CHECK_CLOSE(      // variance:
340           variance(dist)
341           , static_cast<RealType>(2.0179057767837232633904L), tol100);
342
343         BOOST_CHECK_CLOSE(      // skewness:
344            skewness(dist)
345            , static_cast<RealType>(-0.709854548171537509192897824663L), tol1000);
346         BOOST_CHECK_CLOSE(      // kurtosis:
347            kurtosis(dist)
348            , static_cast<RealType>(3.5538752625241790601377L), tol1000);
349         BOOST_CHECK_CLOSE(      // kurtosis excess:
350            kurtosis_excess(dist)
351            , static_cast<RealType>(0.5538752625241790601377L), tol1000);
352
353         BOOST_CHECK_CLOSE(
354           pdf(dist, static_cast<RealType>(0.4L)),
355           static_cast<RealType>(0.294140110156599539564571L),
356           tol100);
357
358         BOOST_CHECK_CLOSE(
359           cdf(dist, static_cast<RealType>(0.4L)),
360           static_cast<RealType>(0.7339186189278737976326676452L),
361           tol1000);
362
363         BOOST_CHECK_CLOSE(
364           quantile(dist, static_cast<RealType>(0.3L)),
365           static_cast<RealType>(-1.180104068086875314419247L),
366           tol1000);
367
368
369       { // mode tests
370
371         skew_normal_distribution<RealType> dist(static_cast<RealType>(0.l), static_cast<RealType>(1.l), static_cast<RealType>(4.l));
372
373         cout << "pdf(dist, 0) = " << pdf(dist, 0) <<  ", pdf(dist, 0.45) = " << pdf(dist, 0.45) << endl;
374        // BOOST_CHECK_CLOSE(mode(dist), boost::math::constants::root_two<RealType>() / 2, tol5);
375         BOOST_CHECK_CLOSE(mode(dist), static_cast<RealType>(0.41697299497388863932L), tol1000);
376       }
377
378
379       }
380       {
381         skew_normal_distribution<RealType> dist(static_cast<RealType>(1.1l), static_cast<RealType>(0.02l), static_cast<RealType>(0.03l));
382
383         BOOST_CHECK_CLOSE(      // mean:
384            mean(dist)
385            , static_cast<RealType>(1.1004785154529557886162L), tol100);
386         BOOST_CHECK_CLOSE(      // variance:
387           variance(dist)
388            , static_cast<RealType>(0.00039977102296128251645L), tol100);
389
390         BOOST_CHECK_CLOSE(      // skewness:
391            skewness(dist)
392            , static_cast<RealType>(5.8834811259890359782e-006L), tol1000);
393         BOOST_CHECK_CLOSE(      // kurtosis:
394            kurtosis(dist)
395            , static_cast<RealType>(3.L + 9.2903475812137800239002e-008L), tol1000);
396         BOOST_CHECK_CLOSE(      // kurtosis excess:
397            kurtosis_excess(dist)
398            , static_cast<RealType>(9.2903475812137800239002e-008L), tol1000);
399       }
400       {
401         skew_normal_distribution<RealType> dist(static_cast<RealType>(10.1l), static_cast<RealType>(5.l), static_cast<RealType>(-0.03l));
402         BOOST_CHECK_CLOSE(      // mean:
403            mean(dist)
404            , static_cast<RealType>(9.9803711367610528459485937L), tol100);
405         BOOST_CHECK_CLOSE(      // variance:
406           variance(dist)
407            , static_cast<RealType>(24.98568893508015727823L), tol100);
408
409         BOOST_CHECK_CLOSE(      // skewness:
410            skewness(dist)
411            , static_cast<RealType>(-5.8834811259890359782085e-006L), tol1000);
412         BOOST_CHECK_CLOSE(      // kurtosis:
413            kurtosis(dist)
414            , static_cast<RealType>(3.L + 9.2903475812137800239002e-008L), tol1000);
415         BOOST_CHECK_CLOSE(      // kurtosis excess:
416            kurtosis_excess(dist)
417            , static_cast<RealType>(9.2903475812137800239002e-008L), tol1000);
418       }
419       {
420         skew_normal_distribution<RealType> dist(static_cast<RealType>(-10.1l), static_cast<RealType>(5.l), static_cast<RealType>(30.l));
421         BOOST_CHECK_CLOSE(      // mean:
422            mean(dist)
423            , static_cast<RealType>(-6.11279169674138408531365L), 2 * tol100);
424         BOOST_CHECK_CLOSE(      // variance:
425           variance(dist)
426           , static_cast<RealType>(9.10216994642554914628242L), tol100 * 2);
427
428         BOOST_CHECK_CLOSE(      // skewness:
429            skewness(dist)
430            , static_cast<RealType>(0.99072425443686904424L), tol1000);
431         BOOST_CHECK_CLOSE(      // kurtosis:
432            kurtosis(dist)
433            , static_cast<RealType>(3.L + 0.8638862008406084244563L), tol1000);
434         BOOST_CHECK_CLOSE(      // kurtosis excess:
435            kurtosis_excess(dist)
436            , static_cast<RealType>(0.8638862008406084244563L), tol1000);
437       }
438
439
440     }
441
442
443 } // template <class RealType>void test_spots(RealType)
444
445 int test_main(int, char* [])
446 {
447
448
449   using boost::math::skew_normal;
450   using boost::math::skew_normal_distribution;
451
452   //int precision = 17; // std::numeric_limits<double::max_digits10;
453   double tolfeweps = numeric_limits<double>::epsilon() * 5;
454   //double tol6decdigits = numeric_limits<float>::epsilon() * 2;
455   // Check that can generate skew_normal distribution using the two convenience methods:
456   boost::math::skew_normal w12(1., 2); // Using typedef.
457   boost::math::skew_normal_distribution<> w01; // Use default unity values for mean and scale.
458   // Note NOT myn01() as the compiler will interpret as a function!
459
460   // Checks on constructors.
461   // Default parameters.
462   BOOST_CHECK_EQUAL(w01.location(), 0);
463   BOOST_CHECK_EQUAL(w01.scale(), 1);
464   BOOST_CHECK_EQUAL(w01.shape(), 0);
465
466   skew_normal_distribution<> w23(2., 3); // Using default RealType double.
467   BOOST_CHECK_EQUAL(w23.scale(), 3);
468   BOOST_CHECK_EQUAL(w23.shape(), 0);
469
470   skew_normal_distribution<> w123(1., 2., 3.); // Using default RealType double.
471   BOOST_CHECK_EQUAL(w123.location(), 1.);
472   BOOST_CHECK_EQUAL(w123.scale(), 2.);
473   BOOST_CHECK_EQUAL(w123.shape(), 3.);
474
475   BOOST_CHECK_CLOSE_FRACTION(mean(w01), static_cast<double>(0), tolfeweps); // Default mean == zero
476   BOOST_CHECK_CLOSE_FRACTION(scale(w01), static_cast<double>(1), tolfeweps); // Default scale == unity
477
478   // Basic sanity-check spot values for all floating-point types..
479   // (Parameter value, arbitrarily zero, only communicates the floating point type).
480   test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
481   test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
482 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
483   test_spots(0.0L); // Test long double.
484 #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
485   test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
486 #endif
487 #else
488   std::cout << "<note>The long double tests have been disabled on this platform "
489     "either because the long double overloads of the usual math functions are "
490     "not available at all, or because they are too inaccurate for these tests "
491     "to pass.</note>" << std::cout;
492 #endif
493   /*      */
494   return 0;
495 } // int test_main(int, char* [])
496
497 /*
498
499 Output:
500
501
502 */
503
504