test_lognormal.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007
  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. // test_lognormal.cpp
  8. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  9. #define BOOST_TEST_MAIN
  10. #include <boost/test/unit_test.hpp> // Boost.Test
  11. #include <boost/test/tools/floating_point_comparison.hpp>
  12. #include <boost/math/distributions/lognormal.hpp>
  13. using boost::math::lognormal_distribution;
  14. #include <boost/math/tools/test.hpp>
  15. #include "test_out_of_range.hpp"
  16. #include <iostream>
  17. using std::cout;
  18. using std::endl;
  19. using std::setprecision;
  20. #include <limits>
  21. using std::numeric_limits;
  22. #include <cassert>
  23. template <class RealType>
  24. void check_lognormal(RealType loc, RealType scale, RealType x, RealType p, RealType q, RealType tol)
  25. {
  26. BOOST_CHECK_CLOSE(
  27. ::boost::math::cdf(
  28. lognormal_distribution<RealType>(loc, scale), // distribution.
  29. x), // random variable.
  30. p, // probability.
  31. tol); // %tolerance.
  32. BOOST_CHECK_CLOSE(
  33. ::boost::math::cdf(
  34. complement(
  35. lognormal_distribution<RealType>(loc, scale), // distribution.
  36. x)), // random variable.
  37. q, // probability complement.
  38. tol); // %tolerance.
  39. BOOST_CHECK_CLOSE(
  40. ::boost::math::quantile(
  41. lognormal_distribution<RealType>(loc, scale), // distribution.
  42. p), // probability.
  43. x, // random variable.
  44. tol); // %tolerance.
  45. BOOST_CHECK_CLOSE(
  46. ::boost::math::quantile(
  47. complement(
  48. lognormal_distribution<RealType>(loc, scale), // distribution.
  49. q)), // probability complement.
  50. x, // random variable.
  51. tol); // %tolerance.
  52. }
  53. template <class RealType>
  54. void test_spots(RealType)
  55. {
  56. // Basic sanity checks.
  57. RealType tolerance = 5e-3 * 100;
  58. // Some tests only pass at 1e-4 because values generated by
  59. // http://faculty.vassar.edu/lowry/VassarStats.html
  60. // give only 5 or 6 *fixed* places, so small values have fewer digits.
  61. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  62. using std::exp;
  63. //
  64. // These test values were generated for the normal distribution
  65. // using the online calculator at http://faculty.vassar.edu/lowry/VassarStats.html
  66. // and then exponentiating the random variate x.
  67. //
  68. check_lognormal(
  69. static_cast<RealType>(0), // location
  70. static_cast<RealType>(5), // scale
  71. static_cast<RealType>(1), // x
  72. static_cast<RealType>(0.5), // p
  73. static_cast<RealType>(0.5), // q
  74. tolerance);
  75. check_lognormal(
  76. static_cast<RealType>(2), // location
  77. static_cast<RealType>(2), // scale
  78. static_cast<RealType>(exp(1.8)), // x
  79. static_cast<RealType>(0.46017), // p
  80. static_cast<RealType>(1-0.46017), // q
  81. tolerance);
  82. check_lognormal(
  83. static_cast<RealType>(2), // location
  84. static_cast<RealType>(2), // scale
  85. static_cast<RealType>(exp(2.2)), // x
  86. static_cast<RealType>(1-0.46017), // p
  87. static_cast<RealType>(0.46017), // q
  88. tolerance);
  89. check_lognormal(
  90. static_cast<RealType>(2), // location
  91. static_cast<RealType>(2), // scale
  92. static_cast<RealType>(exp(-1.4)), // x
  93. static_cast<RealType>(0.04457), // p
  94. static_cast<RealType>(1-0.04457), // q
  95. tolerance);
  96. check_lognormal(
  97. static_cast<RealType>(2), // location
  98. static_cast<RealType>(2), // scale
  99. static_cast<RealType>(exp(5.4)), // x
  100. static_cast<RealType>(1-0.04457), // p
  101. static_cast<RealType>(0.04457), // q
  102. tolerance);
  103. check_lognormal(
  104. static_cast<RealType>(-3), // location
  105. static_cast<RealType>(5), // scale
  106. static_cast<RealType>(exp(-5.0)), // x
  107. static_cast<RealType>(0.34458), // p
  108. static_cast<RealType>(1-0.34458), // q
  109. tolerance);
  110. check_lognormal(
  111. static_cast<RealType>(-3), // location
  112. static_cast<RealType>(5), // scale
  113. static_cast<RealType>(exp(-1.0)), // x
  114. static_cast<RealType>(1-0.34458), // p
  115. static_cast<RealType>(0.34458), // q
  116. tolerance);
  117. check_lognormal(
  118. static_cast<RealType>(-3), // location
  119. static_cast<RealType>(5), // scale
  120. static_cast<RealType>(exp(-9.0)), // x
  121. static_cast<RealType>(0.11507), // p
  122. static_cast<RealType>(1-0.11507), // q
  123. tolerance);
  124. check_lognormal(
  125. static_cast<RealType>(-3), // location
  126. static_cast<RealType>(5), // scale
  127. static_cast<RealType>(exp(3.0)), // x
  128. static_cast<RealType>(1-0.11507), // p
  129. static_cast<RealType>(0.11507), // q
  130. tolerance);
  131. //
  132. // Tests for PDF
  133. //
  134. tolerance = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage
  135. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  136. BOOST_CHECK_CLOSE(
  137. pdf(lognormal_distribution<RealType>(), static_cast<RealType>(1)),
  138. static_cast<RealType>(0.3989422804014326779399460599343818684759L), // 1/sqrt(2*pi)
  139. tolerance);
  140. BOOST_CHECK_CLOSE(
  141. pdf(lognormal_distribution<RealType>(3), exp(static_cast<RealType>(3))),
  142. static_cast<RealType>(0.3989422804014326779399460599343818684759L) / exp(static_cast<RealType>(3)),
  143. tolerance);
  144. BOOST_CHECK_CLOSE(
  145. pdf(lognormal_distribution<RealType>(3, 5), exp(static_cast<RealType>(3))),
  146. static_cast<RealType>(0.3989422804014326779399460599343818684759L / (5 * exp(static_cast<RealType>(3)))),
  147. tolerance);
  148. //
  149. // Spot checks for location = -5, scale = 6,
  150. // use relation to normal to test:
  151. //
  152. for(RealType x = -15; x < 5; x += 0.125)
  153. {
  154. BOOST_CHECK_CLOSE(
  155. pdf(lognormal_distribution<RealType>(-5, 6), exp(x)),
  156. pdf(boost::math::normal_distribution<RealType>(-5, 6), x) / exp(x),
  157. tolerance);
  158. }
  159. //
  160. // These test values were obtained by punching numbers into
  161. // a calculator, using the formulas at http://mathworld.wolfram.com/LogNormalDistribution.html
  162. //
  163. tolerance = (std::max)(
  164. boost::math::tools::epsilon<RealType>(),
  165. static_cast<RealType>(boost::math::tools::epsilon<double>())) * 5 * 100; // 5 eps as a percentage
  166. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  167. lognormal_distribution<RealType> dist(8, 3);
  168. RealType x = static_cast<RealType>(0.125);
  169. BOOST_MATH_STD_USING // ADL of std math lib names.
  170. // mean:
  171. BOOST_CHECK_CLOSE(
  172. mean(dist)
  173. , static_cast<RealType>(268337.28652087445695647967378715L), tolerance);
  174. // variance:
  175. BOOST_CHECK_CLOSE(
  176. variance(dist)
  177. , static_cast<RealType>(583389737628117.49553037857325892L), tolerance);
  178. // std deviation:
  179. BOOST_CHECK_CLOSE(
  180. standard_deviation(dist)
  181. , static_cast<RealType>(24153462.228594009489719473727471L), tolerance);
  182. // hazard:
  183. BOOST_CHECK_CLOSE(
  184. hazard(dist, x)
  185. , pdf(dist, x) / cdf(complement(dist, x)), tolerance);
  186. // cumulative hazard:
  187. BOOST_CHECK_CLOSE(
  188. chf(dist, x)
  189. , -log(cdf(complement(dist, x))), tolerance);
  190. // coefficient_of_variation:
  191. BOOST_CHECK_CLOSE(
  192. coefficient_of_variation(dist)
  193. , standard_deviation(dist) / mean(dist), tolerance);
  194. // mode:
  195. BOOST_CHECK_CLOSE(
  196. mode(dist)
  197. , static_cast<RealType>(0.36787944117144232159552377016146L), tolerance);
  198. BOOST_CHECK_CLOSE(
  199. median(dist)
  200. , static_cast<RealType>(exp(dist.location())), tolerance);
  201. BOOST_CHECK_CLOSE(
  202. median(dist),
  203. quantile(dist, static_cast<RealType>(0.5)), tolerance);
  204. // skewness:
  205. BOOST_CHECK_CLOSE(
  206. skewness(dist)
  207. , static_cast<RealType>(729551.38304660255658441529235697L), tolerance);
  208. // kertosis:
  209. BOOST_CHECK_CLOSE(
  210. kurtosis(dist)
  211. , static_cast<RealType>(4312295840576303.2363383232038251L), tolerance);
  212. // kertosis excess:
  213. BOOST_CHECK_CLOSE(
  214. kurtosis_excess(dist)
  215. , static_cast<RealType>(4312295840576300.2363383232038251L), tolerance);
  216. BOOST_CHECK_CLOSE(
  217. range(dist).first
  218. , static_cast<RealType>(0), tolerance);
  219. //
  220. // Special cases:
  221. //
  222. BOOST_CHECK(pdf(dist, 0) == 0);
  223. BOOST_CHECK(cdf(dist, 0) == 0);
  224. BOOST_CHECK(cdf(complement(dist, 0)) == 1);
  225. BOOST_CHECK(quantile(dist, 0) == 0);
  226. BOOST_CHECK(quantile(complement(dist, 1)) == 0);
  227. //
  228. // Error checks:
  229. //
  230. BOOST_MATH_CHECK_THROW(lognormal_distribution<RealType>(0, 0), std::domain_error);
  231. BOOST_MATH_CHECK_THROW(lognormal_distribution<RealType>(2, -1), std::domain_error);
  232. BOOST_MATH_CHECK_THROW(pdf(dist, -1), std::domain_error);
  233. BOOST_MATH_CHECK_THROW(cdf(dist, -1), std::domain_error);
  234. BOOST_MATH_CHECK_THROW(cdf(complement(dist, -1)), std::domain_error);
  235. BOOST_MATH_CHECK_THROW(quantile(dist, 1), std::overflow_error);
  236. BOOST_MATH_CHECK_THROW(quantile(complement(dist, 0)), std::overflow_error);
  237. check_out_of_range<lognormal_distribution<RealType> >(1, 2);
  238. } // template <class RealType>void test_spots(RealType)
  239. BOOST_AUTO_TEST_CASE( test_main )
  240. {
  241. // Check that can generate lognormal distribution using the two convenience methods:
  242. boost::math::lognormal myf1(1., 2); // Using typedef
  243. lognormal_distribution<> myf2(1., 2); // Using default RealType double.
  244. // Test range and support using double only,
  245. // because it supports numeric_limits max for a pseudo-infinity.
  246. BOOST_CHECK_EQUAL(range(myf2).first, 0); // range 0 to +infinity
  247. BOOST_CHECK_EQUAL(range(myf2).second, (std::numeric_limits<double>::max)());
  248. BOOST_CHECK_EQUAL(support(myf2).first, 0); // support 0 to + infinity.
  249. BOOST_CHECK_EQUAL(support(myf2).second, (std::numeric_limits<double>::max)());
  250. // Basic sanity-check spot values.
  251. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  252. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  253. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  254. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  255. test_spots(0.0L); // Test long double.
  256. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  257. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  258. #endif
  259. #else
  260. std::cout << "<note>The long double tests have been disabled on this platform "
  261. "either because the long double overloads of the usual math functions are "
  262. "not available at all, or because they are too inaccurate for these tests "
  263. "to pass.</note>" << std::endl;
  264. #endif
  265. } // BOOST_AUTO_TEST_CASE( test_main )
  266. /*
  267. Running 1 test case...
  268. Tolerance for type float is 0.5 %
  269. Tolerance for type float is 5.96046e-005 %
  270. Tolerance for type float is 5.96046e-005 %
  271. Tolerance for type double is 0.5 %
  272. Tolerance for type double is 1.11022e-013 %
  273. Tolerance for type double is 1.11022e-013 %
  274. Tolerance for type long double is 0.5 %
  275. Tolerance for type long double is 1.11022e-013 %
  276. Tolerance for type long double is 1.11022e-013 %
  277. Tolerance for type class boost::math::concepts::real_concept is 0.5 %
  278. Tolerance for type class boost::math::concepts::real_concept is 1.11022e-013 %
  279. Tolerance for type class boost::math::concepts::real_concept is 1.11022e-013 %
  280. *** No errors detected
  281. */