]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_logistic_dist.cpp
1 // Copyright 2008 Gautam Sewani
2 // Copyright 2013 Paul A. Bristow
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)
10 # pragma warning (disable : 4127) // conditional expression is constant.
11 # pragma warning (disable : 4512) // assignment operator could not be generated.
14 #include <boost/config.hpp>
15 #ifndef BOOST_NO_EXCEPTIONS
16 #define BOOST_MATH_UNDERFLOW_ERROR_POLICY throw_on_error
17 #define BOOST_MATH_OVERFLOW_ERROR_POLICY throw_on_error
19 #include <boost/math/tools/test.hpp>
20 #include <boost/math/concepts/real_concept.hpp> // for real_concept
21 #include <boost/math/distributions/logistic.hpp>
22 using boost::math::logistic_distribution
;
24 #define BOOST_TEST_MAIN
25 #include <boost/test/unit_test.hpp> // Boost.Test
26 #include <boost/test/tools/floating_point_comparison.hpp>
27 #include "test_out_of_range.hpp"
32 using std::setprecision
;
34 template <class RealType
>
35 void test_spot(RealType location
, RealType scale
, RealType x
, RealType p
, RealType q
, RealType tolerance
)
39 logistic_distribution
<RealType
>(location
,scale
),
45 complement(logistic_distribution
<RealType
>(location
,scale
),
52 ::boost::math::quantile(
53 logistic_distribution
<RealType
>(location
,scale
),
61 ::boost::math::quantile(
62 complement(logistic_distribution
<RealType
>(location
,scale
),
70 template <class RealType
>
71 void test_spots(RealType T
)
73 // Basic sanity checks.
74 // 50 eps as a percentage, up to a maximum of double precision
75 // Test data taken from Mathematica 6
76 RealType tolerance
= (std::max
)(
77 static_cast<RealType
>(1e-33L),
78 boost::math::tools::epsilon
<RealType
>());
79 cout
<<"Absolute tolerance:"<<tolerance
<<endl
;
81 tolerance
*= 50 * 100;
82 // # pragma warning(disable: 4100) // unreferenced formal parameter.
83 // prevent his spurious warning.
86 cout
<< "Expect parameter T == 0!" << endl
;
88 cout
<< "Tolerance for type " << typeid(T
).name() << " is " << tolerance
<< " %" << endl
;
91 static_cast<RealType
>(1), // location
92 static_cast<RealType
>(0.5L), // scale
93 static_cast<RealType
>(0.1L), // x
94 static_cast<RealType
>(0.141851064900487789594278108470953L), // p
95 static_cast<RealType
>(0.858148935099512210405721891529047L), //q
99 static_cast<RealType
>(5), // location
100 static_cast<RealType
>(2), // scale
101 static_cast<RealType
>(3.123123123L),//x
102 static_cast<RealType
>(0.281215878622547904873088053477813L), // p
103 static_cast<RealType
>(0.718784121377452095126911946522187L), //q
107 static_cast<RealType
>(1.2345L), // location
108 static_cast<RealType
>(0.12345L), // scale
109 static_cast<RealType
>(3.123123123L),//x
110 static_cast<RealType
>(0.999999773084685079723328282229357L), // p
111 static_cast<RealType
>(2.26915314920276671717770643005212e-7L), //q
117 static_cast<RealType
>(1), // location
118 static_cast<RealType
>(0.5L), // scale
119 static_cast<RealType
>(10), // x
120 static_cast<RealType
>(0.99999998477002048723965105559179L), // p
121 static_cast<RealType
>(1.5229979512760348944408208801237e-8L), //q
126 static_cast<RealType
>(5), // location
127 static_cast<RealType
>(2), // scale
128 static_cast<RealType
>(-0.1L), // scale
129 static_cast<RealType
>(0.0724264853615177178439235061476928L), // p
130 static_cast<RealType
>(0.927573514638482282156076493852307L), //q
135 static_cast<RealType
>(5), // location
136 static_cast<RealType
>(2), // scale
137 static_cast<RealType
>(-20), // x
138 static_cast<RealType
>(3.72663928418656138608800947863869e-6L), // p
139 static_cast<RealType
>(0.999996273360715813438613911990521L), //q
143 // Test value to check cancellation error in straight/complemented quantile.
144 // The subtraction in the formula location-scale*log term introduces catastrophic
145 // cancellation error if location and scale*log term are close.
146 // For these values, the tests fail at tolerance, but work at 100*tolerance.
148 static_cast<RealType
>(-1.2345L), // location
149 static_cast<RealType
>(1.4555L), // scale
150 static_cast<RealType
>(-0.00125796420642514024493852425918807L),// x
151 static_cast<RealType
>(0.7L), // p
152 static_cast<RealType
>(0.3L), //q
156 static_cast<RealType
>(1.2345L), // location
157 static_cast<RealType
>(0.12345L), // scale
158 static_cast<RealType
>(0.0012345L), // x
159 static_cast<RealType
>(0.0000458541039469413343331170952855318L), // p
160 static_cast<RealType
>(0.999954145896053058665666882904714L), //q
166 static_cast<RealType
>(5L), // location
167 static_cast<RealType
>(2L), // scale
168 static_cast<RealType
>(0.0012345L), // x
169 static_cast<RealType
>(0.0759014628704232983512906076564256L), // p
170 static_cast<RealType
>(0.924098537129576701648709392343574L), //q
175 static_cast<RealType
>(-123.123123L), // location
176 static_cast<RealType
>(2.123L), // scale
177 static_cast<RealType
>(3), // x
178 static_cast<RealType
>(0.999999999999999999999999984171276L), // p
179 static_cast<RealType
>(1.58287236765203121622150720373972e-26L), //q
184 logistic_distribution
<RealType
>(5,2),
185 static_cast<RealType
>(0.125L) ),//x
186 static_cast<RealType
>(0.0369500730133475464584898192104821L), // probability
191 logistic_distribution
<RealType
>(static_cast<RealType
>(1.2345L), static_cast<RealType
>(0.12345L)),
192 static_cast<RealType
>(0.0012345L) ),//x
193 static_cast<RealType
>(0.000371421639109700748742498671686243L), // probability
197 logistic_distribution
<RealType
>(2,1),
198 static_cast<RealType
>(2L) ),//x
199 static_cast<RealType
>(0.25L), // probability
202 //Extreme value testing
204 if(std::numeric_limits
<RealType
>::has_infinity
)
206 BOOST_CHECK_EQUAL(pdf(logistic_distribution
<RealType
>(), +std::numeric_limits
<RealType
>::infinity()), 0); // x = + infinity, pdf = 0
207 BOOST_CHECK_EQUAL(pdf(logistic_distribution
<RealType
>(), -std::numeric_limits
<RealType
>::infinity()), 0); // x = - infinity, pdf = 0
208 BOOST_CHECK_EQUAL(cdf(logistic_distribution
<RealType
>(), +std::numeric_limits
<RealType
>::infinity()), 1); // x = + infinity, cdf = 1
209 BOOST_CHECK_EQUAL(cdf(logistic_distribution
<RealType
>(), -std::numeric_limits
<RealType
>::infinity()), 0); // x = - infinity, cdf = 0
210 BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution
<RealType
>(), +std::numeric_limits
<RealType
>::infinity())), 0); // x = + infinity, c cdf = 0
211 BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution
<RealType
>(), -std::numeric_limits
<RealType
>::infinity())), 1); // x = - infinity, c cdf = 1
213 BOOST_MATH_CHECK_THROW(quantile(logistic_distribution
<RealType
>(), static_cast<RealType
>(1)), std::overflow_error
); // x = + infinity, cdf = 1
214 BOOST_MATH_CHECK_THROW(quantile(logistic_distribution
<RealType
>(), static_cast<RealType
>(0)), std::overflow_error
); // x = - infinity, cdf = 0
215 BOOST_MATH_CHECK_THROW(quantile(complement(logistic_distribution
<RealType
>(), static_cast<RealType
>(1))), std::overflow_error
); // x = - infinity, cdf = 0
216 BOOST_MATH_CHECK_THROW(quantile(complement(logistic_distribution
<RealType
>(), static_cast<RealType
>(0))), std::overflow_error
); // x = + infinity, cdf = 1
217 BOOST_CHECK_EQUAL(cdf(logistic_distribution
<RealType
>(), +boost::math::tools::max_value
<RealType
>()), 1); // x = + infinity, cdf = 1
218 BOOST_CHECK_EQUAL(cdf(logistic_distribution
<RealType
>(), -boost::math::tools::max_value
<RealType
>()), 0); // x = - infinity, cdf = 0
219 BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution
<RealType
>(), +boost::math::tools::max_value
<RealType
>())), 0); // x = + infinity, c cdf = 0
220 BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution
<RealType
>(), -boost::math::tools::max_value
<RealType
>())), 1); // x = - infinity, c cdf = 1
221 BOOST_CHECK_EQUAL(pdf(logistic_distribution
<RealType
>(), +boost::math::tools::max_value
<RealType
>()), 0); // x = + infinity, pdf = 0
222 BOOST_CHECK_EQUAL(pdf(logistic_distribution
<RealType
>(), -boost::math::tools::max_value
<RealType
>()), 0); // x = - infinity, pdf = 0
225 // Things that are errors:
226 // 1. Domain errors for scale and location.
228 // 3. Probabilities being outside (0,1).
229 check_out_of_range
<logistic_distribution
<RealType
> >(0, 1);
230 if(std::numeric_limits
<RealType
>::has_infinity
)
232 RealType inf
= std::numeric_limits
<RealType
>::infinity();
233 BOOST_CHECK_EQUAL(pdf(logistic_distribution
<RealType
>(0, 1), inf
), 0);
234 BOOST_CHECK_EQUAL(pdf(logistic_distribution
<RealType
>(0, 1), -inf
), 0);
235 BOOST_CHECK_EQUAL(cdf(logistic_distribution
<RealType
>(0, 1), inf
), 1);
236 BOOST_CHECK_EQUAL(cdf(logistic_distribution
<RealType
>(0, 1), -inf
), 0);
237 BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution
<RealType
>(0, 1), inf
)), 0);
238 BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution
<RealType
>(0, 1), -inf
)), 1);
241 // location/scale can't be infinity.
242 if(std::numeric_limits
<RealType
>::has_infinity
)
244 #ifndef BOOST_NO_EXCEPTIONS
245 BOOST_MATH_CHECK_THROW(
246 logistic_distribution
<RealType
> dist(std::numeric_limits
<RealType
>::infinity(), 0.5),
248 BOOST_MATH_CHECK_THROW(
249 logistic_distribution
<RealType
> dist(0.5, std::numeric_limits
<RealType
>::infinity()),
252 BOOST_MATH_CHECK_THROW(
253 logistic_distribution
<RealType
>(std::numeric_limits
<RealType
>::infinity(), 0.5),
255 BOOST_MATH_CHECK_THROW(
256 logistic_distribution
<RealType
>(0.5, std::numeric_limits
<RealType
>::infinity()),
260 // scale can't be negative or 0.
261 #ifndef BOOST_NO_EXCEPTIONS
262 BOOST_MATH_CHECK_THROW(
263 logistic_distribution
<RealType
> dist(0.5, -0.5),
265 BOOST_MATH_CHECK_THROW(
266 logistic_distribution
<RealType
> dist(0.5, 0),
269 BOOST_MATH_CHECK_THROW(
270 logistic_distribution
<RealType
>(0.5, -0.5),
272 BOOST_MATH_CHECK_THROW(
273 logistic_distribution
<RealType
>(0.5, 0),
277 logistic_distribution
<RealType
> dist(0.5, 0.5);
278 // x can't be NaN, p can't be NaN.
280 if (std::numeric_limits
<RealType
>::has_quiet_NaN
)
282 // No longer allow x to be NaN, then these tests should throw.
283 BOOST_MATH_CHECK_THROW(pdf(dist
, +std::numeric_limits
<RealType
>::quiet_NaN()), std::domain_error
); // x = NaN
284 BOOST_MATH_CHECK_THROW(cdf(dist
, +std::numeric_limits
<RealType
>::quiet_NaN()), std::domain_error
); // x = NaN
285 BOOST_MATH_CHECK_THROW(cdf(complement(dist
, +std::numeric_limits
<RealType
>::quiet_NaN())), std::domain_error
); // x = + infinity
286 BOOST_MATH_CHECK_THROW(quantile(dist
, +std::numeric_limits
<RealType
>::quiet_NaN()), std::domain_error
); // p = + infinity
287 BOOST_MATH_CHECK_THROW(quantile(complement(dist
, +std::numeric_limits
<RealType
>::quiet_NaN())), std::domain_error
); // p = + infinity
289 if (std::numeric_limits
<RealType
>::has_infinity
)
291 // Added test for Trac https://svn.boost.org/trac/boost/ticket/9126#comment:1
292 logistic_distribution
<RealType
> dist(0., 0.5);
293 BOOST_CHECK_EQUAL(pdf(dist
, +std::numeric_limits
<RealType
>::infinity()), static_cast<RealType
>(0) ); // x = infinity
298 // p can't be outside (0,1).
299 BOOST_MATH_CHECK_THROW(quantile(dist
, static_cast<RealType
>(1.1)), std::domain_error
);
300 BOOST_MATH_CHECK_THROW(quantile(dist
, static_cast<RealType
>(-0.1)), std::domain_error
);
301 BOOST_MATH_CHECK_THROW(quantile(dist
, static_cast<RealType
>(1)), std::overflow_error
);
302 BOOST_MATH_CHECK_THROW(quantile(dist
, static_cast<RealType
>(0)), std::overflow_error
);
304 BOOST_MATH_CHECK_THROW(quantile(complement(dist
, static_cast<RealType
>(1.1))), std::domain_error
);
305 BOOST_MATH_CHECK_THROW(quantile(complement(dist
, static_cast<RealType
>(-0.1))), std::domain_error
);
306 BOOST_MATH_CHECK_THROW(quantile(complement(dist
, static_cast<RealType
>(1))), std::overflow_error
);
307 BOOST_MATH_CHECK_THROW(quantile(complement(dist
, static_cast<RealType
>(0))), std::overflow_error
);
309 // Tests for mean,mode,median,variance,skewness,kurtosis.
313 logistic_distribution
<RealType
>(2,1)
315 static_cast<RealType
>(2), // probability
319 ::boost::math::median(
320 logistic_distribution
<RealType
>(2,1)
322 static_cast<RealType
>(2), // probability
327 logistic_distribution
<RealType
>(2,1)
329 static_cast<RealType
>(2), // probability
333 ::boost::math::variance(
334 logistic_distribution
<RealType
>(2,1)
336 static_cast<RealType
>(3.28986813369645287294483033329205L), // probability
340 ::boost::math::skewness(
341 logistic_distribution
<RealType
>(2,1)
343 static_cast<RealType
>(0), // probability
346 ::boost::math::kurtosis_excess(
347 logistic_distribution
<RealType
>(2,1)
349 static_cast<RealType
>(1.2L), // probability
353 ::boost::math::entropy(
354 logistic_distribution
<RealType
>(2,1)
356 static_cast<RealType
>(2),
359 } // template <class RealType>void test_spots(RealType)
362 BOOST_AUTO_TEST_CASE( test_main
)
364 // Check that can generate logistic distribution using the two convenience methods:
365 boost::math::logistic
mycexp1(1.); // Using typedef
366 logistic_distribution
<> myexp2(1.); // Using default RealType double.
368 // Basic sanity-check spot values.
369 // (Parameter value, arbitrarily zero, only communicates the floating point type).
370 test_spots(0.0F
); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
371 test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
372 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
373 test_spots(0.0L); // Test long double.
374 #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x582)) && !defined(BOOST_MATH_NO_REAL_CONCEPT_TESTS)
375 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
378 std::cout
<< "<note>The long double tests have been disabled on this platform "
379 "either because the long double overloads of the usual math functions are "
380 "not available at all, or because they are too inaccurate for these tests "
381 "to pass.</note>" << std::endl
;
384 } // BOOST_AUTO_TEST_CASE( test_main )