test_weibull.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Copyright John Maddock 2006, 2012.
  2. // Copyright Paul A. Bristow 2007, 2012.
  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_weibull.cpp
  8. #ifdef _MSC_VER
  9. # pragma warning (disable : 4127) // conditional expression is constant.
  10. #endif
  11. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  12. #define BOOST_TEST_MAIN
  13. #include <boost/test/unit_test.hpp> // Boost.Test
  14. #include <boost/test/tools/floating_point_comparison.hpp>
  15. #include <boost/math/distributions/weibull.hpp>
  16. using boost::math::weibull_distribution;
  17. #include <boost/math/tools/test.hpp>
  18. #include "test_out_of_range.hpp"
  19. #include <iostream>
  20. using std::cout;
  21. using std::endl;
  22. using std::setprecision;
  23. #include <limits>
  24. using std::numeric_limits;
  25. template <class RealType>
  26. void check_weibull(RealType shape, RealType scale, RealType x, RealType p, RealType q, RealType tol)
  27. {
  28. BOOST_CHECK_CLOSE(
  29. ::boost::math::cdf(
  30. weibull_distribution<RealType>(shape, scale), // distribution.
  31. x), // random variable.
  32. p, // probability.
  33. tol); // %tolerance.
  34. BOOST_CHECK_CLOSE(
  35. ::boost::math::cdf(
  36. complement(
  37. weibull_distribution<RealType>(shape, scale), // distribution.
  38. x)), // random variable.
  39. q, // probability complement.
  40. tol); // %tolerance.
  41. BOOST_CHECK_CLOSE(
  42. ::boost::math::quantile(
  43. weibull_distribution<RealType>(shape, scale), // distribution.
  44. p), // probability.
  45. x, // random variable.
  46. tol); // %tolerance.
  47. BOOST_CHECK_CLOSE(
  48. ::boost::math::quantile(
  49. complement(
  50. weibull_distribution<RealType>(shape, scale), // distribution.
  51. q)), // probability complement.
  52. x, // random variable.
  53. tol); // %tolerance.
  54. }
  55. template <class RealType>
  56. void test_spots(RealType)
  57. {
  58. // Basic sanity checks
  59. //
  60. // These test values were generated for the normal distribution
  61. // using the online calculator at
  62. // http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm
  63. //
  64. // Tolerance is just over 5 decimal digits expressed as a persentage:
  65. // that's the limit of the test data.
  66. RealType tolerance = 2e-5f * 100;
  67. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  68. using std::exp;
  69. check_weibull(
  70. static_cast<RealType>(0.25), // shape
  71. static_cast<RealType>(0.5), // scale
  72. static_cast<RealType>(0.1), // x
  73. static_cast<RealType>(0.487646), // p
  74. static_cast<RealType>(1-0.487646), // q
  75. tolerance);
  76. check_weibull(
  77. static_cast<RealType>(0.25), // shape
  78. static_cast<RealType>(0.5), // scale
  79. static_cast<RealType>(0.5), // x
  80. static_cast<RealType>(1-0.367879), // p
  81. static_cast<RealType>(0.367879), // q
  82. tolerance);
  83. check_weibull(
  84. static_cast<RealType>(0.25), // shape
  85. static_cast<RealType>(0.5), // scale
  86. static_cast<RealType>(1), // x
  87. static_cast<RealType>(1-0.304463), // p
  88. static_cast<RealType>(0.304463), // q
  89. tolerance);
  90. check_weibull(
  91. static_cast<RealType>(0.25), // shape
  92. static_cast<RealType>(0.5), // scale
  93. static_cast<RealType>(2), // x
  94. static_cast<RealType>(1-0.243117), // p
  95. static_cast<RealType>(0.243117), // q
  96. tolerance);
  97. check_weibull(
  98. static_cast<RealType>(0.25), // shape
  99. static_cast<RealType>(0.5), // scale
  100. static_cast<RealType>(5), // x
  101. static_cast<RealType>(1-0.168929), // p
  102. static_cast<RealType>(0.168929), // q
  103. tolerance);
  104. check_weibull(
  105. static_cast<RealType>(0.5), // shape
  106. static_cast<RealType>(2), // scale
  107. static_cast<RealType>(0.1), // x
  108. static_cast<RealType>(0.200371), // p
  109. static_cast<RealType>(1-0.200371), // q
  110. tolerance);
  111. check_weibull(
  112. static_cast<RealType>(0.5), // shape
  113. static_cast<RealType>(2), // scale
  114. static_cast<RealType>(0.5), // x
  115. static_cast<RealType>(0.393469), // p
  116. static_cast<RealType>(1-0.393469), // q
  117. tolerance);
  118. check_weibull(
  119. static_cast<RealType>(0.5), // shape
  120. static_cast<RealType>(2), // scale
  121. static_cast<RealType>(1), // x
  122. static_cast<RealType>(1-0.493069), // p
  123. static_cast<RealType>(0.493069), // q
  124. tolerance);
  125. check_weibull(
  126. static_cast<RealType>(0.5), // shape
  127. static_cast<RealType>(2), // scale
  128. static_cast<RealType>(2), // x
  129. static_cast<RealType>(1-0.367879), // p
  130. static_cast<RealType>(0.367879), // q
  131. tolerance);
  132. check_weibull(
  133. static_cast<RealType>(0.5), // shape
  134. static_cast<RealType>(2), // scale
  135. static_cast<RealType>(5), // x
  136. static_cast<RealType>(1-0.205741), // p
  137. static_cast<RealType>(0.205741), // q
  138. tolerance);
  139. check_weibull(
  140. static_cast<RealType>(2), // shape
  141. static_cast<RealType>(0.25), // scale
  142. static_cast<RealType>(0.1), // x
  143. static_cast<RealType>(0.147856), // p
  144. static_cast<RealType>(1-0.147856), // q
  145. tolerance);
  146. check_weibull(
  147. static_cast<RealType>(2), // shape
  148. static_cast<RealType>(0.25), // scale
  149. static_cast<RealType>(0.5), // x
  150. static_cast<RealType>(1-0.018316), // p
  151. static_cast<RealType>(0.018316), // q
  152. tolerance);
  153. /*
  154. This test value came from
  155. http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm
  156. but appears to be grossly incorrect: certainly it does not agree with the values
  157. I get from pushing numbers into a calculator (0.0001249921878255106610615995196123).
  158. Strangely other test values generated for the same shape and scale parameters do look OK.
  159. check_weibull(
  160. static_cast<RealType>(3), // shape
  161. static_cast<RealType>(2), // scale
  162. static_cast<RealType>(0.1), // x
  163. static_cast<RealType>(1.25E-40), // p
  164. static_cast<RealType>(1-1.25E-40), // q
  165. tolerance);
  166. */
  167. check_weibull(
  168. static_cast<RealType>(3), // shape
  169. static_cast<RealType>(2), // scale
  170. static_cast<RealType>(0.5), // x
  171. static_cast<RealType>(0.015504), // p
  172. static_cast<RealType>(1-0.015504), // q
  173. tolerance * 10); // few digits in test value
  174. check_weibull(
  175. static_cast<RealType>(3), // shape
  176. static_cast<RealType>(2), // scale
  177. static_cast<RealType>(1), // x
  178. static_cast<RealType>(0.117503), // p
  179. static_cast<RealType>(1-0.117503), // q
  180. tolerance);
  181. check_weibull(
  182. static_cast<RealType>(3), // shape
  183. static_cast<RealType>(2), // scale
  184. static_cast<RealType>(2), // x
  185. static_cast<RealType>(1-0.367879), // p
  186. static_cast<RealType>(0.367879), // q
  187. tolerance);
  188. //
  189. // Tests for PDF
  190. //
  191. BOOST_CHECK_CLOSE(
  192. pdf(weibull_distribution<RealType>(0.25, 0.5), static_cast<RealType>(0.1)),
  193. static_cast<RealType>(0.856579),
  194. tolerance);
  195. BOOST_CHECK_CLOSE(
  196. pdf(weibull_distribution<RealType>(0.25, 0.5), static_cast<RealType>(0.5)),
  197. static_cast<RealType>(0.183940),
  198. tolerance);
  199. BOOST_CHECK_CLOSE(
  200. pdf(weibull_distribution<RealType>(0.25, 0.5), static_cast<RealType>(5)),
  201. static_cast<RealType>(0.015020),
  202. tolerance * 10); // fewer digits in test value
  203. BOOST_CHECK_CLOSE(
  204. pdf(weibull_distribution<RealType>(0.5, 2), static_cast<RealType>(0.1)),
  205. static_cast<RealType>(0.894013),
  206. tolerance);
  207. BOOST_CHECK_CLOSE(
  208. pdf(weibull_distribution<RealType>(0.5, 2), static_cast<RealType>(0.5)),
  209. static_cast<RealType>(0.303265),
  210. tolerance);
  211. BOOST_CHECK_CLOSE(
  212. pdf(weibull_distribution<RealType>(0.5, 2), static_cast<RealType>(1)),
  213. static_cast<RealType>(0.174326),
  214. tolerance);
  215. BOOST_CHECK_CLOSE(
  216. pdf(weibull_distribution<RealType>(2, 0.25), static_cast<RealType>(0.1)),
  217. static_cast<RealType>(2.726860),
  218. tolerance);
  219. BOOST_CHECK_CLOSE(
  220. pdf(weibull_distribution<RealType>(2, 0.25), static_cast<RealType>(0.5)),
  221. static_cast<RealType>(0.293050),
  222. tolerance);
  223. BOOST_CHECK_CLOSE(
  224. pdf(weibull_distribution<RealType>(3, 2), static_cast<RealType>(1)),
  225. static_cast<RealType>(0.330936),
  226. tolerance);
  227. BOOST_CHECK_CLOSE(
  228. pdf(weibull_distribution<RealType>(3, 2), static_cast<RealType>(2)),
  229. static_cast<RealType>(0.551819),
  230. tolerance);
  231. //
  232. // These test values were obtained using the formulas at
  233. // http://en.wikipedia.org/wiki/Weibull_distribution
  234. // which are subtly different to (though mathematically
  235. // the same as) the ones on the Mathworld site
  236. // http://mathworld.wolfram.com/WeibullDistribution.html
  237. // which are the ones used in the implementation.
  238. // The assumption is that if both computation methods
  239. // agree then the implementation is probably correct...
  240. // What's not clear is which method is more accurate.
  241. //
  242. tolerance = (std::max)(
  243. boost::math::tools::epsilon<RealType>(),
  244. static_cast<RealType>(boost::math::tools::epsilon<double>())) * 5 * 100; // 5 eps as a percentage
  245. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  246. weibull_distribution<RealType> dist(2, 3);
  247. RealType x = static_cast<RealType>(0.125);
  248. BOOST_MATH_STD_USING // ADL of std lib math functions
  249. // mean:
  250. BOOST_CHECK_CLOSE(
  251. mean(dist)
  252. , dist.scale() * boost::math::tgamma(1 + 1 / dist.shape()), tolerance);
  253. // variance:
  254. BOOST_CHECK_CLOSE(
  255. variance(dist)
  256. , dist.scale() * dist.scale() * boost::math::tgamma(1 + 2 / dist.shape()) - mean(dist) * mean(dist), tolerance);
  257. // std deviation:
  258. BOOST_CHECK_CLOSE(
  259. standard_deviation(dist)
  260. , sqrt(variance(dist)), tolerance);
  261. // hazard:
  262. BOOST_CHECK_CLOSE(
  263. hazard(dist, x)
  264. , pdf(dist, x) / cdf(complement(dist, x)), tolerance);
  265. // cumulative hazard:
  266. BOOST_CHECK_CLOSE(
  267. chf(dist, x)
  268. , -log(cdf(complement(dist, x))), tolerance);
  269. // coefficient_of_variation:
  270. BOOST_CHECK_CLOSE(
  271. coefficient_of_variation(dist)
  272. , standard_deviation(dist) / mean(dist), tolerance);
  273. // mode:
  274. BOOST_CHECK_CLOSE(
  275. mode(dist)
  276. , dist.scale() * pow((dist.shape() - 1) / dist.shape(), 1/dist.shape()), tolerance);
  277. // median:
  278. BOOST_CHECK_CLOSE(
  279. median(dist)
  280. , dist.scale() * pow(log(static_cast<RealType>(2)), 1 / dist.shape()), tolerance);
  281. // skewness:
  282. BOOST_CHECK_CLOSE(
  283. skewness(dist),
  284. (boost::math::tgamma(1 + 3/dist.shape()) * pow(dist.scale(), RealType(3)) - 3 * mean(dist) * variance(dist) - pow(mean(dist), RealType(3))) / pow(standard_deviation(dist), RealType(3)),
  285. tolerance * 100);
  286. // kertosis:
  287. BOOST_CHECK_CLOSE(
  288. kurtosis(dist)
  289. , kurtosis_excess(dist) + 3, tolerance);
  290. // kertosis excess:
  291. BOOST_CHECK_CLOSE(
  292. kurtosis_excess(dist),
  293. (pow(dist.scale(), RealType(4)) * boost::math::tgamma(1 + 4/dist.shape())
  294. - 3 * variance(dist) * variance(dist)
  295. - 4 * skewness(dist) * variance(dist) * standard_deviation(dist) * mean(dist)
  296. - 6 * variance(dist) * mean(dist) * mean(dist)
  297. - pow(mean(dist), RealType(4))) / (variance(dist) * variance(dist)),
  298. tolerance * 1000);
  299. //
  300. // Special cases:
  301. //
  302. BOOST_CHECK(cdf(dist, 0) == 0);
  303. BOOST_CHECK(cdf(complement(dist, 0)) == 1);
  304. BOOST_CHECK(quantile(dist, 0) == 0);
  305. BOOST_CHECK(quantile(complement(dist, 1)) == 0);
  306. BOOST_CHECK_EQUAL(pdf(weibull_distribution<RealType>(1, 1), 0), 1);
  307. //
  308. // Error checks:
  309. //
  310. BOOST_MATH_CHECK_THROW(weibull_distribution<RealType>(1, -1), std::domain_error);
  311. BOOST_MATH_CHECK_THROW(weibull_distribution<RealType>(-1, 1), std::domain_error);
  312. BOOST_MATH_CHECK_THROW(weibull_distribution<RealType>(1, 0), std::domain_error);
  313. BOOST_MATH_CHECK_THROW(weibull_distribution<RealType>(0, 1), std::domain_error);
  314. BOOST_MATH_CHECK_THROW(pdf(dist, -1), std::domain_error);
  315. BOOST_MATH_CHECK_THROW(cdf(dist, -1), std::domain_error);
  316. BOOST_MATH_CHECK_THROW(cdf(complement(dist, -1)), std::domain_error);
  317. BOOST_MATH_CHECK_THROW(quantile(dist, 1), std::overflow_error);
  318. BOOST_MATH_CHECK_THROW(quantile(complement(dist, 0)), std::overflow_error);
  319. BOOST_MATH_CHECK_THROW(quantile(dist, -1), std::domain_error);
  320. BOOST_MATH_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
  321. BOOST_CHECK_EQUAL(pdf(dist, 0), exp(-pow(RealType(0) / RealType(3), RealType(2))) * pow(RealType(0), RealType(1)) * RealType(2) / RealType(3));
  322. BOOST_CHECK_EQUAL(pdf(weibull_distribution<RealType>(1, 3), 0), exp(-pow(RealType(0) / RealType(3), RealType(1))) * pow(RealType(0), RealType(0)) * RealType(1) / RealType(3));
  323. BOOST_MATH_CHECK_THROW(pdf(weibull_distribution<RealType>(0.5, 3), 0), std::overflow_error);
  324. check_out_of_range<weibull_distribution<RealType> >(1, 1);
  325. } // template <class RealType>void test_spots(RealType)
  326. BOOST_AUTO_TEST_CASE( test_main )
  327. {
  328. // Check that can construct weibull distribution using the two convenience methods:
  329. using namespace boost::math;
  330. weibull myw1(2); // Using typedef
  331. weibull_distribution<> myw2(2); // Using default RealType double.
  332. // Basic sanity-check spot values.
  333. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  334. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  335. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  336. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  337. test_spots(0.0L); // Test long double.
  338. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  339. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  340. #endif
  341. #else
  342. std::cout << "<note>The long double tests have been disabled on this platform "
  343. "either because the long double overloads of the usual math functions are "
  344. "not available at all, or because they are too inaccurate for these tests "
  345. "to pass.</note>" << std::endl;
  346. #endif
  347. } // BOOST_AUTO_TEST_CASE( test_main )
  348. /*
  349. Output:
  350. Description: Autorun "J:\Cpp\MathToolkit\test\Math_test\Debug\test_weibull.exe"
  351. Running 1 test case...
  352. Tolerance for type float is 0.002 %
  353. Tolerance for type float is 5.96046e-005 %
  354. Tolerance for type double is 0.002 %
  355. Tolerance for type double is 1.11022e-013 %
  356. Tolerance for type long double is 0.002 %
  357. Tolerance for type long double is 1.11022e-013 %
  358. Tolerance for type class boost::math::concepts::real_concept is 0.002 %
  359. Tolerance for type class boost::math::concepts::real_concept is 1.11022e-013 %
  360. *** No errors detected
  361. */