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