Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_extreme_value.cpp
1 // Copyright John Maddock 2006.
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 // test_extreme_value.cpp
9
10 #include <boost/math/tools/test.hpp>
11 #include <boost/math/concepts/real_concept.hpp> // for real_concept
12 #include <boost/math/distributions/extreme_value.hpp>
13     using boost::math::extreme_value_distribution;
14
15 #define BOOST_TEST_MAIN
16 #include <boost/test/unit_test.hpp> // Boost.Test
17 #include <boost/test/tools/floating_point_comparison.hpp>
18 #include "test_out_of_range.hpp"
19
20 #include <iostream>
21    using std::cout;
22    using std::endl;
23    using std::setprecision;
24
25 template <class RealType>
26 void test_spot(RealType a, RealType b, RealType x, RealType p, RealType q, RealType tolerance)
27 {
28    BOOST_CHECK_CLOSE(
29       ::boost::math::cdf(
30          extreme_value_distribution<RealType>(a, b),      
31          x),
32          p,
33          tolerance); // %
34    BOOST_CHECK_CLOSE(
35       ::boost::math::cdf(
36          complement(extreme_value_distribution<RealType>(a, b),      
37          x)),
38          q,
39          tolerance); // %
40    if((p < 0.999) && (p > 0))
41    {
42       BOOST_CHECK_CLOSE(
43          ::boost::math::quantile(
44             extreme_value_distribution<RealType>(a, b),      
45             p),
46             x,
47             tolerance); // %
48    }
49    if((q < 0.999) && (q > 0))
50    {
51       BOOST_CHECK_CLOSE(
52          ::boost::math::quantile(
53             complement(extreme_value_distribution<RealType>(a, b),      
54             q)),
55             x,
56             tolerance); // %
57    }
58 }
59
60 template <class RealType>
61 void test_spots(RealType)
62 {
63    // Basic sanity checks.
64    // 50eps as a percentage, up to a maximum of double precision
65    // (that's the limit of our test data).
66    RealType tolerance = (std::max)(
67       static_cast<RealType>(boost::math::tools::epsilon<double>()),
68       boost::math::tools::epsilon<RealType>());
69    tolerance *= 50 * 100;  
70
71    cout << "Tolerance for type " << typeid(RealType).name()  << " is " << tolerance << " %" << endl;
72
73    // Results calculated by punching numbers into a calculator,
74    // and using the formula at http://mathworld.wolfram.com/ExtremeValueDistribution.html
75    test_spot(
76       static_cast<RealType>(0.5), // a
77       static_cast<RealType>(1.5), // b
78       static_cast<RealType>(0.125), // x
79       static_cast<RealType>(0.27692033409990891617007608217222L), // p
80       static_cast<RealType>(0.72307966590009108382992391782778L), //q
81       tolerance);
82    test_spot(
83       static_cast<RealType>(0.5), // a
84       static_cast<RealType>(2), // b
85       static_cast<RealType>(-5), // x
86       static_cast<RealType>(1.6087601139887776413169427645933e-7L), // p
87       static_cast<RealType>(0.99999983912398860112223586830572L), //q
88       tolerance);
89    test_spot(
90       static_cast<RealType>(0.5), // a
91       static_cast<RealType>(0.25), // b
92       static_cast<RealType>(0.75), // x
93       static_cast<RealType>(0.69220062755534635386542199718279L), // p
94       static_cast<RealType>(0.30779937244465364613457800281721), //q
95       tolerance);
96    test_spot(
97       static_cast<RealType>(0.5), // a
98       static_cast<RealType>(0.25), // b
99       static_cast<RealType>(5), // x
100       static_cast<RealType>(0.99999998477002037126351248727041L), // p
101       static_cast<RealType>(1.5229979628736487512729586276294e-8L), //q
102       tolerance);
103
104    BOOST_CHECK_CLOSE(
105       ::boost::math::pdf(
106          extreme_value_distribution<RealType>(0.5, 2),      
107          static_cast<RealType>(0.125)),              // x
108          static_cast<RealType>(0.18052654830890205978204427757846L),                // probability.
109          tolerance); // %
110    BOOST_CHECK_CLOSE(
111       ::boost::math::pdf(
112          extreme_value_distribution<RealType>(1, 3),      
113          static_cast<RealType>(5)),              // x
114          static_cast<RealType>(0.0675057324099851209129017326286L),                // probability.
115          tolerance); // %
116    BOOST_CHECK_CLOSE(
117       ::boost::math::pdf(
118          extreme_value_distribution<RealType>(1, 3),      
119          static_cast<RealType>(0)),              // x
120          static_cast<RealType>(0.11522236828583456431277265757312L),                // probability.
121          tolerance); // %
122
123    BOOST_CHECK_CLOSE(
124       ::boost::math::mean(
125          extreme_value_distribution<RealType>(2, 3)),
126          static_cast<RealType>(3.731646994704598581819536270246L),           
127          tolerance); // %
128    BOOST_CHECK_CLOSE(
129       ::boost::math::standard_deviation(
130          extreme_value_distribution<RealType>(1, 0.5)), 
131          static_cast<RealType>(0.6412749150809320477720181798355L),
132          tolerance); // %
133    BOOST_CHECK_CLOSE(
134       ::boost::math::mode(
135          extreme_value_distribution<RealType>(2, 3)),
136          static_cast<RealType>(2),           
137          tolerance); // %
138    BOOST_CHECK_CLOSE(
139       ::boost::math::median(
140          extreme_value_distribution<RealType>(0, 1)),
141          static_cast<RealType>(+0.36651292058166432701243915823266946945426344783710526305367771367056),           
142          tolerance); // %
143
144    BOOST_CHECK_CLOSE(
145       ::boost::math::skewness(
146          extreme_value_distribution<RealType>(2, 3)),
147          static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L),           
148          tolerance); // %
149    BOOST_CHECK_CLOSE(
150       ::boost::math::kurtosis(
151          extreme_value_distribution<RealType>(2, 3)),
152          static_cast<RealType>(5.4),           
153          tolerance); // %
154    BOOST_CHECK_CLOSE(
155       ::boost::math::kurtosis_excess(
156          extreme_value_distribution<RealType>(2, 3)),
157          static_cast<RealType>(2.4),           
158          tolerance); // %
159
160    //
161    // Things that are errors:
162    //
163    extreme_value_distribution<RealType> dist(0.5, 2);
164    BOOST_MATH_CHECK_THROW(
165        quantile(dist, RealType(1.0)),
166        std::overflow_error);
167    BOOST_MATH_CHECK_THROW(
168        quantile(complement(dist, RealType(0.0))),
169        std::overflow_error);
170    BOOST_MATH_CHECK_THROW(
171        quantile(dist, RealType(0.0)),
172        std::overflow_error);
173    BOOST_MATH_CHECK_THROW(
174        quantile(complement(dist, RealType(1.0))),
175        std::overflow_error);
176    BOOST_MATH_CHECK_THROW(
177        cdf(extreme_value_distribution<RealType>(0, -1), RealType(1)),
178        std::domain_error);
179    BOOST_MATH_CHECK_THROW(
180        quantile(dist, RealType(-1)),
181        std::domain_error);
182    BOOST_MATH_CHECK_THROW(
183        quantile(dist, RealType(2)),
184        std::domain_error);
185    check_out_of_range<extreme_value_distribution<RealType> >(1, 2);
186    if(std::numeric_limits<RealType>::has_infinity)
187    {
188       RealType inf = std::numeric_limits<RealType>::infinity();
189       BOOST_CHECK_EQUAL(pdf(extreme_value_distribution<RealType>(), -inf), 0);
190       BOOST_CHECK_EQUAL(pdf(extreme_value_distribution<RealType>(), inf), 0);
191       BOOST_CHECK_EQUAL(cdf(extreme_value_distribution<RealType>(), -inf), 0);
192       BOOST_CHECK_EQUAL(cdf(extreme_value_distribution<RealType>(), inf), 1);
193       BOOST_CHECK_EQUAL(cdf(complement(extreme_value_distribution<RealType>(), -inf)), 1);
194       BOOST_CHECK_EQUAL(cdf(complement(extreme_value_distribution<RealType>(), inf)), 0);
195    }
196    //
197    // Bug reports:
198    //
199    // https://svn.boost.org/trac/boost/ticket/10938:
200    BOOST_CHECK_CLOSE(
201    ::boost::math::pdf(
202       extreme_value_distribution<RealType>(0, 1),
203       static_cast<RealType>(-1000)),              // x
204       static_cast<RealType>(0),                // probability.
205       tolerance); // %
206
207 } // template <class RealType>void test_spots(RealType)
208
209 BOOST_AUTO_TEST_CASE( test_main )
210 {
211
212   // Check that can generate extreme_value distribution using the two convenience methods:
213    boost::math::extreme_value mycev1(1.); // Using typedef
214    extreme_value_distribution<> myev2(1.); // Using default RealType double.
215
216     // Basic sanity-check spot values.
217    // (Parameter value, arbitrarily zero, only communicates the floating point type).
218   test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
219   test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
220 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
221   test_spots(0.0L); // Test long double.
222 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
223   test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
224 #endif
225 #else
226    std::cout << "<note>The long double tests have been disabled on this platform "
227       "either because the long double overloads of the usual math functions are "
228       "not available at all, or because they are too inaccurate for these tests "
229       "to pass.</note>" << std::endl;
230 #endif
231
232    
233 } // BOOST_AUTO_TEST_CASE( test_main )
234
235 /*
236
237 Output is:
238
239 -Running 1 test case...
240 Tolerance for type float is 0.000596046 %
241 Tolerance for type double is 1.11022e-012 %
242 Tolerance for type long double is 1.11022e-012 %
243 Tolerance for type class boost::math::concepts::real_concept is 1.11022e-012 %
244 *** No errors detected
245 */
246
247