test_logistic_dist.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // Copyright 2008 Gautam Sewani
  2. // Copyright 2013 Paul A. Bristow
  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. #ifdef _MSC_VER
  8. # pragma warning (disable : 4127) // conditional expression is constant.
  9. # pragma warning (disable : 4512) // assignment operator could not be generated.
  10. #endif
  11. #include <boost/config.hpp>
  12. #ifndef BOOST_NO_EXCEPTIONS
  13. #define BOOST_MATH_UNDERFLOW_ERROR_POLICY throw_on_error
  14. #define BOOST_MATH_OVERFLOW_ERROR_POLICY throw_on_error
  15. #endif
  16. #include <boost/math/tools/test.hpp>
  17. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  18. #include <boost/math/distributions/logistic.hpp>
  19. using boost::math::logistic_distribution;
  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"
  24. #include <iostream>
  25. using std::cout;
  26. using std::endl;
  27. using std::setprecision;
  28. template <class RealType>
  29. void test_spot(RealType location, RealType scale, RealType x, RealType p, RealType q, RealType tolerance)
  30. {
  31. BOOST_CHECK_CLOSE(
  32. ::boost::math::cdf(
  33. logistic_distribution<RealType>(location,scale),
  34. x),
  35. p,
  36. tolerance); // %
  37. BOOST_CHECK_CLOSE(
  38. ::boost::math::cdf(
  39. complement(logistic_distribution<RealType>(location,scale),
  40. x)),
  41. q,
  42. tolerance); // %
  43. if(p < 0.999)
  44. {
  45. BOOST_CHECK_CLOSE(
  46. ::boost::math::quantile(
  47. logistic_distribution<RealType>(location,scale),
  48. p),
  49. x,
  50. tolerance); // %
  51. }
  52. if(q < 0.999)
  53. {
  54. BOOST_CHECK_CLOSE(
  55. ::boost::math::quantile(
  56. complement(logistic_distribution<RealType>(location,scale),
  57. q)),
  58. x,
  59. 2 * tolerance); // %
  60. }
  61. }
  62. template <class RealType>
  63. void test_spots(RealType T)
  64. {
  65. // Basic sanity checks.
  66. // 50 eps as a percentage, up to a maximum of double precision
  67. // Test data taken from Mathematica 6
  68. RealType tolerance = (std::max)(
  69. static_cast<RealType>(1e-33L),
  70. boost::math::tools::epsilon<RealType>());
  71. cout<<"Absolute tolerance:"<<tolerance<<endl;
  72. tolerance *= 50 * 100;
  73. // # pragma warning(disable: 4100) // unreferenced formal parameter.
  74. // prevent his spurious warning.
  75. if (T != 0)
  76. {
  77. cout << "Expect parameter T == 0!" << endl;
  78. }
  79. cout << "Tolerance for type " << typeid(T).name() << " is " << tolerance << " %" << endl;
  80. test_spot(
  81. static_cast<RealType>(1), // location
  82. static_cast<RealType>(0.5L), // scale
  83. static_cast<RealType>(0.1L), // x
  84. static_cast<RealType>(0.141851064900487789594278108470953L), // p
  85. static_cast<RealType>(0.858148935099512210405721891529047L), //q
  86. tolerance);
  87. test_spot(
  88. static_cast<RealType>(5), // location
  89. static_cast<RealType>(2), // scale
  90. static_cast<RealType>(3.123123123L),//x
  91. static_cast<RealType>(0.281215878622547904873088053477813L), // p
  92. static_cast<RealType>(0.718784121377452095126911946522187L), //q
  93. tolerance);
  94. test_spot(
  95. static_cast<RealType>(1.2345L), // location
  96. static_cast<RealType>(0.12345L), // scale
  97. static_cast<RealType>(3.123123123L),//x
  98. static_cast<RealType>(0.999999773084685079723328282229357L), // p
  99. static_cast<RealType>(2.26915314920276671717770643005212e-7L), //q
  100. tolerance);
  101. //High probability
  102. test_spot(
  103. static_cast<RealType>(1), // location
  104. static_cast<RealType>(0.5L), // scale
  105. static_cast<RealType>(10), // x
  106. static_cast<RealType>(0.99999998477002048723965105559179L), // p
  107. static_cast<RealType>(1.5229979512760348944408208801237e-8L), //q
  108. tolerance);
  109. //negative x
  110. test_spot(
  111. static_cast<RealType>(5), // location
  112. static_cast<RealType>(2), // scale
  113. static_cast<RealType>(-0.1L), // scale
  114. static_cast<RealType>(0.0724264853615177178439235061476928L), // p
  115. static_cast<RealType>(0.927573514638482282156076493852307L), //q
  116. tolerance);
  117. test_spot(
  118. static_cast<RealType>(5), // location
  119. static_cast<RealType>(2), // scale
  120. static_cast<RealType>(-20), // x
  121. static_cast<RealType>(3.72663928418656138608800947863869e-6L), // p
  122. static_cast<RealType>(0.999996273360715813438613911990521L), //q
  123. tolerance);
  124. // Test value to check cancellation error in straight/complemented quantile.
  125. // The subtraction in the formula location-scale*log term introduces catastrophic
  126. // cancellation error if location and scale*log term are close.
  127. // For these values, the tests fail at tolerance, but work at 100*tolerance.
  128. test_spot(
  129. static_cast<RealType>(-1.2345L), // location
  130. static_cast<RealType>(1.4555L), // scale
  131. static_cast<RealType>(-0.00125796420642514024493852425918807L),// x
  132. static_cast<RealType>(0.7L), // p
  133. static_cast<RealType>(0.3L), //q
  134. 80*tolerance);
  135. test_spot(
  136. static_cast<RealType>(1.2345L), // location
  137. static_cast<RealType>(0.12345L), // scale
  138. static_cast<RealType>(0.0012345L), // x
  139. static_cast<RealType>(0.0000458541039469413343331170952855318L), // p
  140. static_cast<RealType>(0.999954145896053058665666882904714L), //q
  141. 80*tolerance);
  142. test_spot(
  143. static_cast<RealType>(5L), // location
  144. static_cast<RealType>(2L), // scale
  145. static_cast<RealType>(0.0012345L), // x
  146. static_cast<RealType>(0.0759014628704232983512906076564256L), // p
  147. static_cast<RealType>(0.924098537129576701648709392343574L), //q
  148. 80*tolerance);
  149. //negative location
  150. test_spot(
  151. static_cast<RealType>(-123.123123L), // location
  152. static_cast<RealType>(2.123L), // scale
  153. static_cast<RealType>(3), // x
  154. static_cast<RealType>(0.999999999999999999999999984171276L), // p
  155. static_cast<RealType>(1.58287236765203121622150720373972e-26L), //q
  156. tolerance);
  157. //PDF Testing
  158. BOOST_CHECK_CLOSE(
  159. ::boost::math::pdf(
  160. logistic_distribution<RealType>(5,2),
  161. static_cast<RealType>(0.125L) ),//x
  162. static_cast<RealType>(0.0369500730133475464584898192104821L), // probability
  163. tolerance); // %
  164. BOOST_CHECK_CLOSE(
  165. ::boost::math::pdf(
  166. logistic_distribution<RealType>(static_cast<RealType>(1.2345L), static_cast<RealType>(0.12345L)),
  167. static_cast<RealType>(0.0012345L) ),//x
  168. static_cast<RealType>(0.000371421639109700748742498671686243L), // probability
  169. tolerance); // %
  170. BOOST_CHECK_CLOSE(
  171. ::boost::math::pdf(
  172. logistic_distribution<RealType>(2,1),
  173. static_cast<RealType>(2L) ),//x
  174. static_cast<RealType>(0.25L), // probability
  175. tolerance); // %
  176. //Extreme value testing
  177. if(std::numeric_limits<RealType>::has_infinity)
  178. {
  179. BOOST_CHECK_EQUAL(pdf(logistic_distribution<RealType>(), +std::numeric_limits<RealType>::infinity()), 0); // x = + infinity, pdf = 0
  180. BOOST_CHECK_EQUAL(pdf(logistic_distribution<RealType>(), -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, pdf = 0
  181. BOOST_CHECK_EQUAL(cdf(logistic_distribution<RealType>(), +std::numeric_limits<RealType>::infinity()), 1); // x = + infinity, cdf = 1
  182. BOOST_CHECK_EQUAL(cdf(logistic_distribution<RealType>(), -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, cdf = 0
  183. BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution<RealType>(), +std::numeric_limits<RealType>::infinity())), 0); // x = + infinity, c cdf = 0
  184. BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution<RealType>(), -std::numeric_limits<RealType>::infinity())), 1); // x = - infinity, c cdf = 1
  185. }
  186. BOOST_MATH_CHECK_THROW(quantile(logistic_distribution<RealType>(), static_cast<RealType>(1)), std::overflow_error); // x = + infinity, cdf = 1
  187. BOOST_MATH_CHECK_THROW(quantile(logistic_distribution<RealType>(), static_cast<RealType>(0)), std::overflow_error); // x = - infinity, cdf = 0
  188. BOOST_MATH_CHECK_THROW(quantile(complement(logistic_distribution<RealType>(), static_cast<RealType>(1))), std::overflow_error); // x = - infinity, cdf = 0
  189. BOOST_MATH_CHECK_THROW(quantile(complement(logistic_distribution<RealType>(), static_cast<RealType>(0))), std::overflow_error); // x = + infinity, cdf = 1
  190. BOOST_CHECK_EQUAL(cdf(logistic_distribution<RealType>(), +boost::math::tools::max_value<RealType>()), 1); // x = + infinity, cdf = 1
  191. BOOST_CHECK_EQUAL(cdf(logistic_distribution<RealType>(), -boost::math::tools::max_value<RealType>()), 0); // x = - infinity, cdf = 0
  192. BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution<RealType>(), +boost::math::tools::max_value<RealType>())), 0); // x = + infinity, c cdf = 0
  193. BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution<RealType>(), -boost::math::tools::max_value<RealType>())), 1); // x = - infinity, c cdf = 1
  194. BOOST_CHECK_EQUAL(pdf(logistic_distribution<RealType>(), +boost::math::tools::max_value<RealType>()), 0); // x = + infinity, pdf = 0
  195. BOOST_CHECK_EQUAL(pdf(logistic_distribution<RealType>(), -boost::math::tools::max_value<RealType>()), 0); // x = - infinity, pdf = 0
  196. //
  197. // Things that are errors:
  198. // 1. Domain errors for scale and location.
  199. // 2. x being NAN.
  200. // 3. Probabilies being outside (0,1).
  201. check_out_of_range<logistic_distribution<RealType> >(0, 1);
  202. if(std::numeric_limits<RealType>::has_infinity)
  203. {
  204. RealType inf = std::numeric_limits<RealType>::infinity();
  205. BOOST_CHECK_EQUAL(pdf(logistic_distribution<RealType>(0, 1), inf), 0);
  206. BOOST_CHECK_EQUAL(pdf(logistic_distribution<RealType>(0, 1), -inf), 0);
  207. BOOST_CHECK_EQUAL(cdf(logistic_distribution<RealType>(0, 1), inf), 1);
  208. BOOST_CHECK_EQUAL(cdf(logistic_distribution<RealType>(0, 1), -inf), 0);
  209. BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution<RealType>(0, 1), inf)), 0);
  210. BOOST_CHECK_EQUAL(cdf(complement(logistic_distribution<RealType>(0, 1), -inf)), 1);
  211. }
  212. // location/scale can't be infinity.
  213. if(std::numeric_limits<RealType>::has_infinity)
  214. {
  215. #ifndef BOOST_NO_EXCEPTIONS
  216. BOOST_MATH_CHECK_THROW(
  217. logistic_distribution<RealType> dist(std::numeric_limits<RealType>::infinity(), 0.5),
  218. std::domain_error);
  219. BOOST_MATH_CHECK_THROW(
  220. logistic_distribution<RealType> dist(0.5, std::numeric_limits<RealType>::infinity()),
  221. std::domain_error);
  222. #else
  223. BOOST_MATH_CHECK_THROW(
  224. logistic_distribution<RealType>(std::numeric_limits<RealType>::infinity(), 0.5),
  225. std::domain_error);
  226. BOOST_MATH_CHECK_THROW(
  227. logistic_distribution<RealType>(0.5, std::numeric_limits<RealType>::infinity()),
  228. std::domain_error);
  229. #endif
  230. }
  231. // scale can't be negative or 0.
  232. #ifndef BOOST_NO_EXCEPTIONS
  233. BOOST_MATH_CHECK_THROW(
  234. logistic_distribution<RealType> dist(0.5, -0.5),
  235. std::domain_error);
  236. BOOST_MATH_CHECK_THROW(
  237. logistic_distribution<RealType> dist(0.5, 0),
  238. std::domain_error);
  239. #else
  240. BOOST_MATH_CHECK_THROW(
  241. logistic_distribution<RealType>(0.5, -0.5),
  242. std::domain_error);
  243. BOOST_MATH_CHECK_THROW(
  244. logistic_distribution<RealType>(0.5, 0),
  245. std::domain_error);
  246. #endif
  247. logistic_distribution<RealType> dist(0.5, 0.5);
  248. // x can't be NaN, p can't be NaN.
  249. if (std::numeric_limits<RealType>::has_quiet_NaN)
  250. {
  251. // No longer allow x to be NaN, then these tests should throw.
  252. BOOST_MATH_CHECK_THROW(pdf(dist, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  253. BOOST_MATH_CHECK_THROW(cdf(dist, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  254. BOOST_MATH_CHECK_THROW(cdf(complement(dist, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
  255. BOOST_MATH_CHECK_THROW(quantile(dist, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + infinity
  256. BOOST_MATH_CHECK_THROW(quantile(complement(dist, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + infinity
  257. }
  258. if (std::numeric_limits<RealType>::has_infinity)
  259. {
  260. // Added test for Trac https://svn.boost.org/trac/boost/ticket/9126#comment:1
  261. logistic_distribution<RealType> dist(0., 0.5);
  262. BOOST_CHECK_EQUAL(pdf(dist, +std::numeric_limits<RealType>::infinity()), static_cast<RealType>(0) ); // x = infinity
  263. }
  264. // p can't be outside (0,1).
  265. BOOST_MATH_CHECK_THROW(quantile(dist, static_cast<RealType>(1.1)), std::domain_error);
  266. BOOST_MATH_CHECK_THROW(quantile(dist, static_cast<RealType>(-0.1)), std::domain_error);
  267. BOOST_MATH_CHECK_THROW(quantile(dist, static_cast<RealType>(1)), std::overflow_error);
  268. BOOST_MATH_CHECK_THROW(quantile(dist, static_cast<RealType>(0)), std::overflow_error);
  269. BOOST_MATH_CHECK_THROW(quantile(complement(dist, static_cast<RealType>(1.1))), std::domain_error);
  270. BOOST_MATH_CHECK_THROW(quantile(complement(dist, static_cast<RealType>(-0.1))), std::domain_error);
  271. BOOST_MATH_CHECK_THROW(quantile(complement(dist, static_cast<RealType>(1))), std::overflow_error);
  272. BOOST_MATH_CHECK_THROW(quantile(complement(dist, static_cast<RealType>(0))), std::overflow_error);
  273. // Tests for mean,mode,median,variance,skewness,kurtosis.
  274. //mean
  275. BOOST_CHECK_CLOSE(
  276. ::boost::math::mean(
  277. logistic_distribution<RealType>(2,1)
  278. ),//x
  279. static_cast<RealType>(2), // probability
  280. tolerance); // %
  281. //median
  282. BOOST_CHECK_CLOSE(
  283. ::boost::math::median(
  284. logistic_distribution<RealType>(2,1)
  285. ),//x
  286. static_cast<RealType>(2), // probability
  287. tolerance);
  288. //mode
  289. BOOST_CHECK_CLOSE(
  290. ::boost::math::mode(
  291. logistic_distribution<RealType>(2,1)
  292. ),//x
  293. static_cast<RealType>(2), // probability
  294. tolerance);
  295. //variance
  296. BOOST_CHECK_CLOSE(
  297. ::boost::math::variance(
  298. logistic_distribution<RealType>(2,1)
  299. ),//x
  300. static_cast<RealType>(3.28986813369645287294483033329205L), // probability
  301. tolerance);
  302. //skewness
  303. BOOST_CHECK_CLOSE(
  304. ::boost::math::skewness(
  305. logistic_distribution<RealType>(2,1)
  306. ),//x
  307. static_cast<RealType>(0), // probability
  308. tolerance);
  309. BOOST_CHECK_CLOSE(
  310. ::boost::math::kurtosis_excess(
  311. logistic_distribution<RealType>(2,1)
  312. ),//x
  313. static_cast<RealType>(1.2L), // probability
  314. tolerance);
  315. } // template <class RealType>void test_spots(RealType)
  316. BOOST_AUTO_TEST_CASE( test_main )
  317. {
  318. // Check that can generate logistic distribution using the two convenience methods:
  319. boost::math::logistic mycexp1(1.); // Using typedef
  320. logistic_distribution<> myexp2(1.); // Using default RealType double.
  321. // Basic sanity-check spot values.
  322. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  323. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  324. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  325. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  326. test_spots(0.0L); // Test long double.
  327. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  328. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  329. #endif
  330. #else
  331. std::cout << "<note>The long double tests have been disabled on this platform "
  332. "either because the long double overloads of the usual math functions are "
  333. "not available at all, or because they are too inaccurate for these tests "
  334. "to pass.</note>" << std::endl;
  335. #endif
  336. } // BOOST_AUTO_TEST_CASE( test_main )