]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_rayleigh.cpp
1 // Copyright John Maddock 2006.
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)
11 # pragma warning(disable: 4127) // conditional expression is constant.
12 # pragma warning(disable: 4100) // unreferenced formal parameter.
15 #include <boost/math/concepts/real_concept.hpp> // for real_concept
16 #include <boost/math/distributions/rayleigh.hpp>
17 using boost::math::rayleigh_distribution
;
18 #include <boost/math/tools/test.hpp>
20 #define BOOST_TEST_MAIN
21 #include <boost/test/unit_test.hpp> // Boost.Test
22 #include <boost/test/tools/floating_point_comparison.hpp>
23 #include "test_out_of_range.hpp"
28 using std::setprecision
;
30 template <class RealType
>
31 void test_spot(RealType s
, RealType x
, RealType p
, RealType q
, RealType tolerance
)
35 rayleigh_distribution
<RealType
>(s
),
41 complement(rayleigh_distribution
<RealType
>(s
),
45 // Special extra tests for p and q near to unity.
49 ::boost::math::quantile(
50 rayleigh_distribution
<RealType
>(s
),
58 ::boost::math::quantile(
59 complement(rayleigh_distribution
<RealType
>(s
),
64 if(std::numeric_limits
<RealType
>::has_infinity
)
66 RealType inf
= std::numeric_limits
<RealType
>::infinity();
67 BOOST_CHECK_EQUAL(pdf(rayleigh_distribution
<RealType
>(s
), inf
), 0);
68 BOOST_CHECK_EQUAL(cdf(rayleigh_distribution
<RealType
>(s
), inf
), 1);
69 BOOST_CHECK_EQUAL(cdf(complement(rayleigh_distribution
<RealType
>(s
), inf
)), 0);
73 template <class RealType
>
74 void test_spots(RealType T
)
76 using namespace std
; // ADL of std names.
77 // Basic sanity checks.
78 // 50 eps as a percentage, up to a maximum of double precision
79 // (that's the limit of our test data: obtained by punching
80 // numbers into a calculator).
81 RealType tolerance
= (std::max
)(
82 static_cast<RealType
>(boost::math::tools::epsilon
<double>()),
83 boost::math::tools::epsilon
<RealType
>());
84 tolerance
*= 10 * 100; // 10 eps as a percent
85 cout
<< "Tolerance for type " << typeid(T
).name() << " is " << tolerance
<< " %" << endl
;
87 using namespace boost::math::constants
;
89 // Things that are errors:
90 rayleigh_distribution
<RealType
> dist(0.5);
92 check_out_of_range
<rayleigh_distribution
<RealType
> >(1);
93 BOOST_MATH_CHECK_THROW(
95 RealType(1.)), // quantile unity should overflow.
97 BOOST_MATH_CHECK_THROW(
98 quantile(complement(dist
,
99 RealType(0.))), // quantile complement zero should overflow.
100 std::overflow_error
);
101 BOOST_MATH_CHECK_THROW(
102 pdf(dist
, RealType(-1)), // Bad negative x.
104 BOOST_MATH_CHECK_THROW(
105 cdf(dist
, RealType(-1)), // Bad negative x.
107 BOOST_MATH_CHECK_THROW(
108 cdf(rayleigh_distribution
<RealType
>(-1), // bad sigma < 0
111 BOOST_MATH_CHECK_THROW(
112 cdf(rayleigh_distribution
<RealType
>(0), // bad sigma == 0
115 BOOST_MATH_CHECK_THROW(
116 quantile(dist
, RealType(-1)), // negative quantile probability.
118 BOOST_MATH_CHECK_THROW(
119 quantile(dist
, RealType(2)), // > unity quantile probability.
123 static_cast<RealType
>(1.L
), // sigma
124 static_cast<RealType
>(1.L
), // x
125 static_cast<RealType
>(1 - exp_minus_half
<RealType
>()), // p
126 static_cast<RealType
>(exp_minus_half
<RealType
>()), // q
130 static_cast<RealType
>(0.5L), // sigma
131 static_cast<RealType
>(0.5L), // x
132 static_cast<RealType
>(1 - exp_minus_half
<RealType
>()), // p
133 static_cast<RealType
>(exp_minus_half
<RealType
>()), //q
137 static_cast<RealType
>(3.L
), // sigma
138 static_cast<RealType
>(3.L
), // x
139 static_cast<RealType
>(1 - exp_minus_half
<RealType
>()), // p
140 static_cast<RealType
>(exp_minus_half
<RealType
>()), //q
145 rayleigh_distribution
<RealType
>(1.L
),
146 static_cast<RealType
>(1.L
)), // x
147 static_cast<RealType
>(exp_minus_half
<RealType
>()), // probability.
151 rayleigh_distribution
<RealType
>(0.5L),
152 static_cast<RealType
>(0.5L)), // x
153 static_cast<RealType
>(2 * exp_minus_half
<RealType
>()), // probability.
157 rayleigh_distribution
<RealType
>(2.L
),
158 static_cast<RealType
>(2.L
)), // x
159 static_cast<RealType
>(exp_minus_half
<RealType
>() /2), // probability.
164 rayleigh_distribution
<RealType
>(1.L
)),
165 static_cast<RealType
>(root_half_pi
<RealType
>()),
168 ::boost::math::variance(
169 rayleigh_distribution
<RealType
>(root_two
<RealType
>())),
170 static_cast<RealType
>(four_minus_pi
<RealType
>()),
171 tolerance
* 100); // %
175 rayleigh_distribution
<RealType
>(1.L
)),
176 static_cast<RealType
>(1.L
),
180 ::boost::math::median(
181 rayleigh_distribution
<RealType
>(1.L
)),
182 static_cast<RealType
>(sqrt(log(4.L
))), // sigma * sqrt(log_four)
186 ::boost::math::skewness(
187 rayleigh_distribution
<RealType
>(1.L
)),
188 static_cast<RealType
>(2.L
* root_pi
<RealType
>()) * (pi
<RealType
>() - 3) / (pow((4 - pi
<RealType
>()), static_cast<RealType
>(1.5L))),
189 tolerance
* 100); // %
192 ::boost::math::skewness(
193 rayleigh_distribution
<RealType
>(1.L
)),
194 static_cast<RealType
>(0.63111065781893713819189935154422777984404221106391L),
195 tolerance
* 100); // %
198 ::boost::math::kurtosis_excess(
199 rayleigh_distribution
<RealType
>(1.L
)),
200 -static_cast<RealType
>(6 * pi
<RealType
>() * pi
<RealType
>() - 24 * pi
<RealType
>() + 16) /
201 ((4 - pi
<RealType
>()) * (4 - pi
<RealType
>())),
202 // static_cast<RealType>(0.2450893006876380628486604106197544154170667057995L),
203 tolerance
* 1000); // %
206 ::boost::math::kurtosis(
207 rayleigh_distribution
<RealType
>(1.L
)),
208 static_cast<RealType
>(3.2450893006876380628486604106197544154170667057995L),
209 tolerance
* 100); // %
213 ::boost::math::kurtosis_excess(rayleigh_distribution
<RealType
>(2)),
214 ::boost::math::kurtosis(rayleigh_distribution
<RealType
>(2)) -3,
215 tolerance
* 100); // %
218 } // template <class RealType>void test_spots(RealType)
220 BOOST_AUTO_TEST_CASE( test_main
)
222 // Check that can generate rayleigh distribution using the two convenience methods:
223 boost::math::rayleigh
ray1(1.); // Using typedef
224 rayleigh_distribution
<> ray2(1.); // Using default RealType double.
226 using namespace boost::math::constants
;
227 // Basic sanity-check spot values.
229 // Double only tests.
230 BOOST_CHECK_CLOSE_FRACTION(
232 rayleigh_distribution
<double>(1.),
233 static_cast<double>(1)), // x
234 static_cast<double>(exp_minus_half
<double>()), // p
237 BOOST_CHECK_CLOSE_FRACTION(
239 rayleigh_distribution
<double>(0.5),
240 static_cast<double>(0.5)), // x
241 static_cast<double>(2 * exp_minus_half
<double>()), // p
244 BOOST_CHECK_CLOSE_FRACTION(
246 rayleigh_distribution
<double>(2.),
247 static_cast<double>(2)), // x
248 static_cast<double>(exp_minus_half
<double>() /2 ), // p
251 BOOST_CHECK_CLOSE_FRACTION(
253 rayleigh_distribution
<double>(1.),
254 static_cast<double>(1)), // x
255 static_cast<double>(1- exp_minus_half
<double>()), // p
258 BOOST_CHECK_CLOSE_FRACTION(
260 rayleigh_distribution
<double>(2.),
261 static_cast<double>(2)), // x
262 static_cast<double>(1- exp_minus_half
<double>()), // p
265 BOOST_CHECK_CLOSE_FRACTION(
267 rayleigh_distribution
<double>(3.),
268 static_cast<double>(3)), // x
269 static_cast<double>(1- exp_minus_half
<double>()), // p
272 BOOST_CHECK_CLOSE_FRACTION(
274 rayleigh_distribution
<double>(4.),
275 static_cast<double>(4)), // x
276 static_cast<double>(1- exp_minus_half
<double>()), // p
279 BOOST_CHECK_CLOSE_FRACTION(
280 ::boost::math::cdf(complement(
281 rayleigh_distribution
<double>(4.),
282 static_cast<double>(4))), // x
283 static_cast<double>(exp_minus_half
<double>()), // q = 1 - p
286 BOOST_CHECK_CLOSE_FRACTION(
287 ::boost::math::quantile(
288 rayleigh_distribution
<double>(4.),
289 static_cast<double>(1- exp_minus_half
<double>())), // x
290 static_cast<double>(4), // p
293 BOOST_CHECK_CLOSE_FRACTION(
294 ::boost::math::quantile(complement(
295 rayleigh_distribution
<double>(4.),
296 static_cast<double>(exp_minus_half
<double>()))), // x
297 static_cast<double>(4), // p
300 // (Parameter value, arbitrarily zero, only communicates the floating point type).
301 test_spots(0.0F
); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
302 test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
303 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
304 test_spots(0.0L); // Test long double.
305 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
306 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
309 std::cout
<< "<note>The long double tests have been disabled on this platform "
310 "either because the long double overloads of the usual math functions are "
311 "not available at all, or because they are too inaccurate for these tests "
312 "to pass.</note>" << std::endl
;
316 } // BOOST_AUTO_TEST_CASE( test_main )
322 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_rayleigh.exe"
323 Running 1 test case...
324 Tolerance for type float is 0.000119209 %
325 Tolerance for type double is 2.22045e-013 %
326 Tolerance for type long double is 2.22045e-013 %
327 Tolerance for type class boost::math::concepts::real_concept is 2.22045e-013 %
328 *** No errors detected