test_triangular.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // Copyright Paul Bristow 2006, 2007.
  2. // Copyright John Maddock 2006, 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_triangular.cpp
  8. #include <pch.hpp>
  9. #ifdef _MSC_VER
  10. # pragma warning(disable: 4127) // conditional expression is constant.
  11. # pragma warning(disable: 4305) // truncation from 'long double' to 'float'
  12. #endif
  13. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  14. #define BOOST_TEST_MAIN
  15. #include <boost/test/unit_test.hpp> // Boost.Test
  16. #include <boost/test/tools/floating_point_comparison.hpp>
  17. #include <boost/math/distributions/triangular.hpp>
  18. using boost::math::triangular_distribution;
  19. #include <boost/math/tools/test.hpp>
  20. #include <boost/math/special_functions/fpclassify.hpp>
  21. #include "test_out_of_range.hpp"
  22. #include <iostream>
  23. #include <iomanip>
  24. using std::cout;
  25. using std::endl;
  26. using std::scientific;
  27. using std::fixed;
  28. using std::left;
  29. using std::right;
  30. using std::setw;
  31. using std::setprecision;
  32. using std::showpos;
  33. #include <limits>
  34. using std::numeric_limits;
  35. template <class RealType>
  36. void check_triangular(RealType lower, RealType mode, RealType upper, RealType x, RealType p, RealType q, RealType tol)
  37. {
  38. BOOST_CHECK_CLOSE_FRACTION(
  39. ::boost::math::cdf(
  40. triangular_distribution<RealType>(lower, mode, upper), // distribution.
  41. x), // random variable.
  42. p, // probability.
  43. tol); // tolerance.
  44. BOOST_CHECK_CLOSE_FRACTION(
  45. ::boost::math::cdf(
  46. complement(
  47. triangular_distribution<RealType>(lower, mode, upper), // distribution.
  48. x)), // random variable.
  49. q, // probability complement.
  50. tol); // tolerance.
  51. BOOST_CHECK_CLOSE_FRACTION(
  52. ::boost::math::quantile(
  53. triangular_distribution<RealType>(lower,mode, upper), // distribution.
  54. p), // probability.
  55. x, // random variable.
  56. tol); // tolerance.
  57. BOOST_CHECK_CLOSE_FRACTION(
  58. ::boost::math::quantile(
  59. complement(
  60. triangular_distribution<RealType>(lower, mode, upper), // distribution.
  61. q)), // probability complement.
  62. x, // random variable.
  63. tol); // tolerance.
  64. } // void check_triangular
  65. template <class RealType>
  66. void test_spots(RealType)
  67. {
  68. // Basic sanity checks:
  69. //
  70. // Some test values were generated for the triangular distribution
  71. // using the online calculator at
  72. // http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm
  73. //
  74. // Tolerance is just over 5 epsilon expressed as a fraction:
  75. RealType tolerance = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.
  76. RealType tol5eps = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.
  77. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << "." << endl;
  78. using namespace std; // for ADL of std::exp;
  79. // Tests on construction
  80. // Default should be 0, 0, 1
  81. BOOST_CHECK_EQUAL(triangular_distribution<RealType>().lower(), -1);
  82. BOOST_CHECK_EQUAL(triangular_distribution<RealType>().mode(), 0);
  83. BOOST_CHECK_EQUAL(triangular_distribution<RealType>().upper(), 1);
  84. BOOST_CHECK_EQUAL(support(triangular_distribution<RealType>()).first, triangular_distribution<RealType>().lower());
  85. BOOST_CHECK_EQUAL(support(triangular_distribution<RealType>()).second, triangular_distribution<RealType>().upper());
  86. if (std::numeric_limits<RealType>::has_quiet_NaN == true)
  87. {
  88. BOOST_MATH_CHECK_THROW( // duff parameter lower.
  89. triangular_distribution<RealType>(static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN()), 0, 0),
  90. std::domain_error);
  91. BOOST_MATH_CHECK_THROW( // duff parameter mode.
  92. triangular_distribution<RealType>(0, static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN()), 0),
  93. std::domain_error);
  94. BOOST_MATH_CHECK_THROW( // duff parameter upper.
  95. triangular_distribution<RealType>(0, 0, static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),
  96. std::domain_error);
  97. } // quiet_NaN tests.
  98. BOOST_MATH_CHECK_THROW( // duff parameters upper < lower.
  99. triangular_distribution<RealType>(1, 0, -1),
  100. std::domain_error);
  101. BOOST_MATH_CHECK_THROW( // duff parameters upper == lower.
  102. triangular_distribution<RealType>(0, 0, 0),
  103. std::domain_error);
  104. BOOST_MATH_CHECK_THROW( // duff parameters mode < lower.
  105. triangular_distribution<RealType>(0, -1, 1),
  106. std::domain_error);
  107. BOOST_MATH_CHECK_THROW( // duff parameters mode > upper.
  108. triangular_distribution<RealType>(0, 2, 1),
  109. std::domain_error);
  110. // Tests for PDF
  111. // // triangular_distribution<RealType>() default is (0, 0, 1), mode == lower.
  112. BOOST_CHECK_CLOSE_FRACTION( // x == lower == mode
  113. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0)),
  114. static_cast<RealType>(2),
  115. tolerance);
  116. BOOST_CHECK_CLOSE_FRACTION( // x == upper
  117. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(1)),
  118. static_cast<RealType>(0),
  119. tolerance);
  120. BOOST_CHECK_CLOSE_FRACTION( // x > upper
  121. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-1)),
  122. static_cast<RealType>(0),
  123. tolerance);
  124. BOOST_CHECK_CLOSE_FRACTION( // x < lower
  125. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2)),
  126. static_cast<RealType>(0),
  127. tolerance);
  128. BOOST_CHECK_CLOSE_FRACTION( // x < lower
  129. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2)),
  130. static_cast<RealType>(0),
  131. tolerance);
  132. // triangular_distribution<RealType>() (0, 1, 1) mode == upper
  133. BOOST_CHECK_CLOSE_FRACTION( // x == lower
  134. pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0)),
  135. static_cast<RealType>(0),
  136. tolerance);
  137. BOOST_CHECK_CLOSE_FRACTION( // x == upper
  138. pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1)),
  139. static_cast<RealType>(2),
  140. tolerance);
  141. BOOST_CHECK_CLOSE_FRACTION( // x > upper
  142. pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(-1)),
  143. static_cast<RealType>(0),
  144. tolerance);
  145. BOOST_CHECK_CLOSE_FRACTION( // x < lower
  146. pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(2)),
  147. static_cast<RealType>(0),
  148. tolerance);
  149. BOOST_CHECK_CLOSE_FRACTION( // x < middle so Wiki says special case pdf = 2 * x
  150. pdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.25)),
  151. static_cast<RealType>(0.5),
  152. tolerance);
  153. BOOST_CHECK_CLOSE_FRACTION( // x < middle so Wiki says special case cdf = x * x
  154. cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.25)),
  155. static_cast<RealType>(0.25 * 0.25),
  156. tolerance);
  157. // triangular_distribution<RealType>() (0, 0.5, 1) mode == middle.
  158. BOOST_CHECK_CLOSE_FRACTION( // x == lower
  159. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0)),
  160. static_cast<RealType>(0),
  161. tolerance);
  162. BOOST_CHECK_CLOSE_FRACTION( // x == upper
  163. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(1)),
  164. static_cast<RealType>(0),
  165. tolerance);
  166. BOOST_CHECK_CLOSE_FRACTION( // x > upper
  167. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(-1)),
  168. static_cast<RealType>(0),
  169. tolerance);
  170. BOOST_CHECK_CLOSE_FRACTION( // x < lower
  171. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(2)),
  172. static_cast<RealType>(0),
  173. tolerance);
  174. BOOST_CHECK_CLOSE_FRACTION( // x == mode
  175. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.5)),
  176. static_cast<RealType>(2),
  177. tolerance);
  178. BOOST_CHECK_CLOSE_FRACTION( // x == half mode
  179. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.25)),
  180. static_cast<RealType>(1),
  181. tolerance);
  182. BOOST_CHECK_CLOSE_FRACTION( // x == half mode
  183. pdf(triangular_distribution<RealType>(0, 0.5, 1), static_cast<RealType>(0.75)),
  184. static_cast<RealType>(1),
  185. tolerance);
  186. if(std::numeric_limits<RealType>::has_infinity)
  187. { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
  188. // Note that infinity is not implemented for real_concept, so these tests
  189. // are only done for types, like built-in float, double.. that have infinity.
  190. // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.
  191. // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here.
  192. // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path
  193. // of error handling is tested below with BOOST_MATH_CHECK_THROW tests.
  194. BOOST_MATH_CHECK_THROW( // x == infinity NOT OK.
  195. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(std::numeric_limits<RealType>::infinity())),
  196. std::domain_error);
  197. BOOST_MATH_CHECK_THROW( // x == minus infinity not OK too.
  198. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::infinity())),
  199. std::domain_error);
  200. }
  201. if(std::numeric_limits<RealType>::has_quiet_NaN)
  202. { // BOOST_CHECK tests for NaN using std::numeric_limits<>::has_quiet_NaN() - should throw.
  203. BOOST_MATH_CHECK_THROW(
  204. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),
  205. std::domain_error);
  206. BOOST_MATH_CHECK_THROW(
  207. pdf(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::quiet_NaN())),
  208. std::domain_error);
  209. } // test for x = NaN using std::numeric_limits<>::quiet_NaN()
  210. // cdf
  211. BOOST_CHECK_EQUAL( // x < lower
  212. cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(-1)),
  213. static_cast<RealType>(0) );
  214. BOOST_CHECK_CLOSE_FRACTION( // x == lower
  215. cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0)),
  216. static_cast<RealType>(0),
  217. tolerance);
  218. BOOST_CHECK_CLOSE_FRACTION( // x == upper
  219. cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1)),
  220. static_cast<RealType>(1),
  221. tolerance);
  222. BOOST_CHECK_EQUAL( // x > upper
  223. cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(2)),
  224. static_cast<RealType>(1));
  225. BOOST_CHECK_CLOSE_FRACTION( // x == mode
  226. cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0)),
  227. //static_cast<RealType>((mode - lower) / (upper - lower)),
  228. static_cast<RealType>(0.5), // (0 --1) / (1 -- 1) = 0.5
  229. tolerance);
  230. BOOST_CHECK_CLOSE_FRACTION(
  231. cdf(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(0.9L)),
  232. static_cast<RealType>(0.81L),
  233. tolerance);
  234. BOOST_CHECK_CLOSE_FRACTION(
  235. cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-1)),
  236. static_cast<RealType>(0),
  237. tolerance);
  238. BOOST_CHECK_CLOSE_FRACTION(
  239. cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-0.5L)),
  240. static_cast<RealType>(0.125L),
  241. tolerance);
  242. BOOST_CHECK_CLOSE_FRACTION(
  243. cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0)),
  244. static_cast<RealType>(0.5),
  245. tolerance);
  246. BOOST_CHECK_CLOSE_FRACTION(
  247. cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(+0.5L)),
  248. static_cast<RealType>(0.875L),
  249. tolerance);
  250. BOOST_CHECK_CLOSE_FRACTION(
  251. cdf(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(1)),
  252. static_cast<RealType>(1),
  253. tolerance);
  254. // cdf complement
  255. BOOST_CHECK_EQUAL( // x < lower
  256. cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(-1))),
  257. static_cast<RealType>(1));
  258. BOOST_CHECK_EQUAL( // x == lower
  259. cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0))),
  260. static_cast<RealType>(1));
  261. BOOST_CHECK_EQUAL( // x == mode
  262. cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0))),
  263. static_cast<RealType>(0.5));
  264. BOOST_CHECK_EQUAL( // x == mode
  265. cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(0))),
  266. static_cast<RealType>(1));
  267. BOOST_CHECK_EQUAL( // x == mode
  268. cdf(complement(triangular_distribution<RealType>(0, 1, 1), static_cast<RealType>(1))),
  269. static_cast<RealType>(0));
  270. BOOST_CHECK_EQUAL( // x > upper
  271. cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(2))),
  272. static_cast<RealType>(0));
  273. BOOST_CHECK_EQUAL( // x == upper
  274. cdf(complement(triangular_distribution<RealType>(0, 0, 1), static_cast<RealType>(1))),
  275. static_cast<RealType>(0));
  276. BOOST_CHECK_CLOSE_FRACTION( // x = -0.5
  277. cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(-0.5))),
  278. static_cast<RealType>(0.875L),
  279. tolerance);
  280. BOOST_CHECK_CLOSE_FRACTION( // x = +0.5
  281. cdf(complement(triangular_distribution<RealType>(-1, 0, 1), static_cast<RealType>(0.5))),
  282. static_cast<RealType>(0.125),
  283. tolerance);
  284. triangular_distribution<RealType> triang; // Using typedef == triangular_distribution<double> tristd;
  285. triangular_distribution<RealType> tristd(0, 0.5, 1); // 'Standard' triangular distribution.
  286. BOOST_CHECK_CLOSE_FRACTION( // median of Standard triangular is sqrt(mode/2) if c > 1/2 else 1 - sqrt((1-c)/2)
  287. median(tristd),
  288. static_cast<RealType>(0.5),
  289. tolerance);
  290. triangular_distribution<RealType> tri011(0, 1, 1); // Using default RealType double.
  291. triangular_distribution<RealType> tri0q1(0, 0.25, 1); // mode is near bottom.
  292. triangular_distribution<RealType> tri0h1(0, 0.5, 1); // Equilateral triangle - mode is the middle.
  293. triangular_distribution<RealType> trim12(-1, -0.5, 2); // mode is negative.
  294. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.02L), static_cast<RealType>(0.0016L), tolerance);
  295. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.5L), static_cast<RealType>(0.66666666666666666666666666666666666666666666667L), tolerance);
  296. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.98L), static_cast<RealType>(0.9994666666666666666666666666666666666666666666L), tolerance);
  297. // quantile
  298. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.0016L)), static_cast<RealType>(0.02L), tol5eps);
  299. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.66666666666666666666666666666666666666666666667L)), static_cast<RealType>(0.5), tol5eps);
  300. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, static_cast<RealType>(0.3333333333333333333333333333333333333333333333333L))), static_cast<RealType>(0.5), tol5eps);
  301. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, static_cast<RealType>(0.999466666666666666666666666666666666666666666666666L)), static_cast<RealType>(98) / 100, 10 * tol5eps);
  302. BOOST_CHECK_CLOSE_FRACTION(pdf(trim12, 0), static_cast<RealType>(0.533333333333333333333333333333333333333333333L), tol5eps);
  303. BOOST_CHECK_CLOSE_FRACTION(cdf(trim12, 0), static_cast<RealType>(0.466666666666666666666666666666666666666666667L), tol5eps);
  304. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(trim12, 0)), static_cast<RealType>(1 - 0.466666666666666666666666666666666666666666667L), tol5eps);
  305. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, static_cast<RealType>(1 - 0.999466666666666666666666666666666666666666666666L))), static_cast<RealType>(0.98L), 10 * tol5eps);
  306. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1))), static_cast<RealType>(0), tol5eps);
  307. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(0.5))), static_cast<RealType>(0.5), tol5eps); // OK
  308. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1 - 0.02L))), static_cast<RealType>(0.1L), tol5eps);
  309. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, static_cast<RealType>(1 - 0.98L))), static_cast<RealType>(0.9L), tol5eps);
  310. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0)), static_cast<RealType>(1), tol5eps);
  311. RealType xs [] = {0, 0.01L, 0.02L, 0.05L, 0.1L, 0.2L, 0.3L, 0.4L, 0.5L, 0.6L, 0.7L, 0.8L, 0.9L, 0.95L, 0.98L, 0.99L, 1};
  312. const triangular_distribution<RealType>& distr = triang;
  313. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(distr, 1.)), static_cast<RealType>(-1), tol5eps);
  314. const triangular_distribution<RealType>* distp = &triang;
  315. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*distp, 1.)), static_cast<RealType>(-1), tol5eps);
  316. const triangular_distribution<RealType>* dists [] = {&tristd, &tri011, &tri0q1, &tri0h1, &trim12};
  317. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*dists[1], 1.)), static_cast<RealType>(0), tol5eps);
  318. for (int i = 0; i < 5; i++)
  319. {
  320. const triangular_distribution<RealType>* const dist = dists[i];
  321. // cout << "Distribution " << i << endl;
  322. BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.5L), quantile(complement(*dist, 0.5L)), tol5eps);
  323. BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98L), quantile(complement(*dist, 1.L - 0.98L)),tol5eps);
  324. BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98L), quantile(complement(*dist, 1.L - 0.98L)),tol5eps);
  325. } // for i
  326. // quantile complement
  327. for (int i = 0; i < 5; i++)
  328. {
  329. const triangular_distribution<RealType>* const dist = dists[i];
  330. //cout << "Distribution " << i << endl;
  331. BOOST_CHECK_EQUAL(quantile(complement(*dists[i], 1.)), quantile(*dists[i], 0.));
  332. for (unsigned j = 0; j < sizeof(xs) /sizeof(RealType); j++)
  333. {
  334. RealType x = xs[j];
  335. BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], x), quantile(complement(*dist, 1 - x)), tol5eps);
  336. } // for j
  337. } // for i
  338. check_triangular(
  339. static_cast<RealType>(0), // lower
  340. static_cast<RealType>(0.5), // mode
  341. static_cast<RealType>(1), // upper
  342. static_cast<RealType>(0.5), // x
  343. static_cast<RealType>(0.5), // p
  344. static_cast<RealType>(1 - 0.5), // q
  345. tolerance);
  346. // Some Not-standard triangular tests.
  347. check_triangular(
  348. static_cast<RealType>(-1), // lower
  349. static_cast<RealType>(0), // mode
  350. static_cast<RealType>(1), // upper
  351. static_cast<RealType>(0), // x
  352. static_cast<RealType>(0.5), // p
  353. static_cast<RealType>(1 - 0.5), // q = 1 - p
  354. tolerance);
  355. check_triangular(
  356. static_cast<RealType>(1), // lower
  357. static_cast<RealType>(1), // mode
  358. static_cast<RealType>(3), // upper
  359. static_cast<RealType>(2), // x
  360. static_cast<RealType>(0.75), // p
  361. static_cast<RealType>(1 - 0.75), // q = 1 - p
  362. tolerance);
  363. check_triangular(
  364. static_cast<RealType>(-1), // lower
  365. static_cast<RealType>(1), // mode
  366. static_cast<RealType>(2), // upper
  367. static_cast<RealType>(1), // x
  368. static_cast<RealType>(0.66666666666666666666666666666666666666666667L), // p
  369. static_cast<RealType>(0.33333333333333333333333333333333333333333333L), // q = 1 - p
  370. tolerance);
  371. tolerance = (std::max)(
  372. boost::math::tools::epsilon<RealType>(),
  373. static_cast<RealType>(boost::math::tools::epsilon<double>())) * 10; // 10 eps as a fraction.
  374. cout << "Tolerance (as fraction) for type " << typeid(RealType).name() << " is " << tolerance << "." << endl;
  375. triangular_distribution<RealType> tridef; // (-1, 0, 1) // Default distribution.
  376. RealType x = static_cast<RealType>(0.5);
  377. using namespace std; // ADL of std names.
  378. // mean:
  379. BOOST_CHECK_CLOSE_FRACTION(
  380. mean(tridef), static_cast<RealType>(0), tolerance);
  381. // variance:
  382. BOOST_CHECK_CLOSE_FRACTION(
  383. variance(tridef), static_cast<RealType>(0.16666666666666666666666666666666666666666667L), tolerance);
  384. // was 0.0833333333333333333333333333333333333333333L
  385. // std deviation:
  386. BOOST_CHECK_CLOSE_FRACTION(
  387. standard_deviation(tridef), sqrt(variance(tridef)), tolerance);
  388. // hazard:
  389. BOOST_CHECK_CLOSE_FRACTION(
  390. hazard(tridef, x), pdf(tridef, x) / cdf(complement(tridef, x)), tolerance);
  391. // cumulative hazard:
  392. BOOST_CHECK_CLOSE_FRACTION(
  393. chf(tridef, x), -log(cdf(complement(tridef, x))), tolerance);
  394. // coefficient_of_variation:
  395. if (mean(tridef) != 0)
  396. {
  397. BOOST_CHECK_CLOSE_FRACTION(
  398. coefficient_of_variation(tridef), standard_deviation(tridef) / mean(tridef), tolerance);
  399. }
  400. // mode:
  401. BOOST_CHECK_CLOSE_FRACTION(
  402. mode(tridef), static_cast<RealType>(0), tolerance);
  403. // skewness:
  404. BOOST_CHECK_CLOSE_FRACTION(
  405. median(tridef), static_cast<RealType>(0), tolerance);
  406. // https://reference.wolfram.com/language/ref/Skewness.html skewness{-1, 0, +1} = 0
  407. // skewness[triangulardistribution{-1, 0, +1}] does not compute a result.
  408. // skewness[triangulardistribution{0, +1}] result == 0
  409. // skewness[normaldistribution{0,1}] result == 0
  410. BOOST_CHECK_EQUAL(
  411. skewness(tridef), static_cast<RealType>(0));
  412. // kurtosis:
  413. BOOST_CHECK_CLOSE_FRACTION(
  414. kurtosis_excess(tridef), kurtosis(tridef) - static_cast<RealType>(3L), tolerance);
  415. // kurtosis excess = kurtosis - 3;
  416. BOOST_CHECK_CLOSE_FRACTION(
  417. kurtosis_excess(tridef), static_cast<RealType>(-0.6), tolerance); // Constant value of -3/5 for all distributions.
  418. {
  419. triangular_distribution<RealType> tri01(0, 1, 1); // Asymmetric 0, 1, 1 distribution.
  420. RealType x = static_cast<RealType>(0.5);
  421. using namespace std; // ADL of std names.
  422. // mean:
  423. BOOST_CHECK_CLOSE_FRACTION(
  424. mean(tri01), static_cast<RealType>(0.66666666666666666666666666666666666666666666666667L), tolerance);
  425. // variance: N[variance[triangulardistribution{0, 1}, 1], 50]
  426. BOOST_CHECK_CLOSE_FRACTION(
  427. variance(tri01), static_cast<RealType>(0.055555555555555555555555555555555555555555555555556L), tolerance);
  428. // std deviation:
  429. BOOST_CHECK_CLOSE_FRACTION(
  430. standard_deviation(tri01), sqrt(variance(tri01)), tolerance);
  431. // hazard:
  432. BOOST_CHECK_CLOSE_FRACTION(
  433. hazard(tri01, x), pdf(tri01, x) / cdf(complement(tri01, x)), tolerance);
  434. // cumulative hazard:
  435. BOOST_CHECK_CLOSE_FRACTION(
  436. chf(tri01, x), -log(cdf(complement(tri01, x))), tolerance);
  437. // coefficient_of_variation:
  438. if (mean(tri01) != 0)
  439. {
  440. BOOST_CHECK_CLOSE_FRACTION(
  441. coefficient_of_variation(tri01), standard_deviation(tri01) / mean(tri01), tolerance);
  442. }
  443. // mode:
  444. BOOST_CHECK_CLOSE_FRACTION(
  445. mode(tri01), static_cast<RealType>(1), tolerance);
  446. // skewness:
  447. BOOST_CHECK_CLOSE_FRACTION(
  448. median(tri01), static_cast<RealType>(0.70710678118654752440084436210484903928483593768847L), tolerance);
  449. // https://reference.wolfram.com/language/ref/Skewness.html
  450. // N[skewness[triangulardistribution{0, 1}, 1], 50]
  451. BOOST_CHECK_CLOSE_FRACTION(
  452. skewness(tri01), static_cast<RealType>(-0.56568542494923801952067548968387923142786875015078L), tolerance);
  453. // kurtosis:
  454. BOOST_CHECK_CLOSE_FRACTION(
  455. kurtosis_excess(tri01), kurtosis(tri01) - static_cast<RealType>(3L), tolerance);
  456. // kurtosis excess = kurtosis - 3;
  457. BOOST_CHECK_CLOSE_FRACTION(
  458. kurtosis_excess(tri01), static_cast<RealType>(-0.6), tolerance); // Constant value of -3/5 for all distributions.
  459. } // tri01 tests
  460. if(std::numeric_limits<RealType>::has_infinity)
  461. { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
  462. // Note that infinity is not implemented for real_concept, so these tests
  463. // are only done for types, like built-in float, double.. that have infinity.
  464. // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.
  465. // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here.
  466. // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path
  467. // of error handling is tested below with BOOST_MATH_CHECK_THROW tests.
  468. using boost::math::policies::policy;
  469. using boost::math::policies::domain_error;
  470. using boost::math::policies::ignore_error;
  471. // Define a (bad?) policy to ignore domain errors ('bad' arguments):
  472. typedef policy<domain_error<ignore_error> > inf_policy; // domain error returns infinity.
  473. triangular_distribution<RealType, inf_policy> tridef_inf(-1, 0., 1);
  474. // But can't use BOOST_CHECK_EQUAL(?, quiet_NaN)
  475. using boost::math::isnan;
  476. BOOST_CHECK((isnan)(pdf(tridef_inf, std::numeric_limits<RealType>::infinity())));
  477. } // test for infinity using std::numeric_limits<>::infinity()
  478. else
  479. { // real_concept case, does has_infinfity == false, so can't check it throws.
  480. // cout << std::numeric_limits<RealType>::infinity() << ' '
  481. // << (boost::math::fpclassify)(std::numeric_limits<RealType>::infinity()) << endl;
  482. // value of std::numeric_limits<RealType>::infinity() is zero, so FPclassify is zero,
  483. // so (boost::math::isfinite)(std::numeric_limits<RealType>::infinity()) does not detect infinity.
  484. // so these tests would never throw.
  485. //BOOST_MATH_CHECK_THROW(pdf(tridef, std::numeric_limits<RealType>::infinity()), std::domain_error);
  486. //BOOST_MATH_CHECK_THROW(pdf(tridef, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error);
  487. // BOOST_MATH_CHECK_THROW(pdf(tridef, boost::math::tools::max_value<RealType>() * 2), std::domain_error); // Doesn't throw.
  488. BOOST_CHECK_EQUAL(pdf(tridef, boost::math::tools::max_value<RealType>()), 0);
  489. }
  490. // Special cases:
  491. BOOST_CHECK(pdf(tridef, -1) == 0);
  492. BOOST_CHECK(pdf(tridef, 1) == 0);
  493. BOOST_CHECK(cdf(tridef, 0) == 0.5);
  494. BOOST_CHECK(pdf(tridef, 1) == 0);
  495. BOOST_CHECK(cdf(tridef, 1) == 1);
  496. BOOST_CHECK(cdf(complement(tridef, -1)) == 1);
  497. BOOST_CHECK(cdf(complement(tridef, 1)) == 0);
  498. BOOST_CHECK(quantile(tridef, 1) == 1);
  499. BOOST_CHECK(quantile(complement(tridef, 1)) == -1);
  500. BOOST_CHECK_EQUAL(support(trim12).first, trim12.lower());
  501. BOOST_CHECK_EQUAL(support(trim12).second, trim12.upper());
  502. // Error checks:
  503. if(std::numeric_limits<RealType>::has_quiet_NaN)
  504. { // BOOST_CHECK tests for quiet_NaN (not for real_concept, for example - see notes above).
  505. BOOST_MATH_CHECK_THROW(triangular_distribution<RealType>(0, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error);
  506. BOOST_MATH_CHECK_THROW(triangular_distribution<RealType>(0, -std::numeric_limits<RealType>::quiet_NaN()), std::domain_error);
  507. }
  508. BOOST_MATH_CHECK_THROW(triangular_distribution<RealType>(1, 0), std::domain_error); // lower > upper!
  509. check_out_of_range<triangular_distribution<RealType> >(-1, 0, 1);
  510. } // template <class RealType>void test_spots(RealType)
  511. BOOST_AUTO_TEST_CASE( test_main )
  512. {
  513. // double toleps = std::numeric_limits<double>::epsilon(); // 5 eps as a fraction.
  514. double tol5eps = std::numeric_limits<double>::epsilon() * 5; // 5 eps as a fraction.
  515. // double tol50eps = std::numeric_limits<double>::epsilon() * 50; // 50 eps as a fraction.
  516. double tol500eps = std::numeric_limits<double>::epsilon() * 500; // 500 eps as a fraction.
  517. // Check that can construct triangular distribution using the two convenience methods:
  518. using namespace boost::math;
  519. triangular triang; // Using typedef
  520. // == triangular_distribution<double> triang;
  521. BOOST_CHECK_EQUAL(triang.lower(), -1); // Check default.
  522. BOOST_CHECK_EQUAL(triang.mode(), 0);
  523. BOOST_CHECK_EQUAL(triang.upper(), 1);
  524. triangular tristd (0, 0.5, 1); // Using typedef
  525. BOOST_CHECK_EQUAL(tristd.lower(), 0);
  526. BOOST_CHECK_EQUAL(tristd.mode(), 0.5);
  527. BOOST_CHECK_EQUAL(tristd.upper(), 1);
  528. //cout << "X range from " << range(tristd).first << " to " << range(tristd).second << endl;
  529. //cout << "Supported from "<< support(tristd).first << ' ' << support(tristd).second << endl;
  530. BOOST_CHECK_EQUAL(support(tristd).first, tristd.lower());
  531. BOOST_CHECK_EQUAL(support(tristd).second, tristd.upper());
  532. triangular_distribution<> tri011(0, 1, 1); // Using default RealType double.
  533. // mode is upper
  534. BOOST_CHECK_EQUAL(tri011.lower(), 0); // Check defaults again.
  535. BOOST_CHECK_EQUAL(tri011.mode(), 1); // Check defaults again.
  536. BOOST_CHECK_EQUAL(tri011.upper(), 1);
  537. BOOST_CHECK_EQUAL(mode(tri011), 1);
  538. BOOST_CHECK_EQUAL(pdf(tri011, 0), 0);
  539. BOOST_CHECK_EQUAL(pdf(tri011, 0.1), 0.2);
  540. BOOST_CHECK_EQUAL(pdf(tri011, 0.5), 1);
  541. BOOST_CHECK_EQUAL(pdf(tri011, 0.9), 1.8);
  542. BOOST_CHECK_EQUAL(pdf(tri011, 1), 2);
  543. BOOST_CHECK_EQUAL(cdf(tri011, 0), 0);
  544. BOOST_CHECK_CLOSE_FRACTION(cdf(tri011, 0.1), 0.01, tol5eps);
  545. BOOST_CHECK_EQUAL(cdf(tri011, 0.5), 0.25);
  546. BOOST_CHECK_EQUAL(cdf(tri011, 0.9), 0.81);
  547. BOOST_CHECK_EQUAL(cdf(tri011, 1), 1);
  548. BOOST_CHECK_EQUAL(cdf(tri011, 9), 1);
  549. BOOST_CHECK_EQUAL(mean(tri011), 0.666666666666666666666666666666666666666666666666667);
  550. BOOST_CHECK_EQUAL(variance(tri011), 1./18.);
  551. triangular tri0h1(0, 0.5, 1); // Equilateral triangle - mode is the middle.
  552. BOOST_CHECK_EQUAL(tri0h1.lower(), 0);
  553. BOOST_CHECK_EQUAL(tri0h1.mode(), 0.5);
  554. BOOST_CHECK_EQUAL(tri0h1.upper(), 1);
  555. BOOST_CHECK_EQUAL(mean(tri0h1), 0.5);
  556. BOOST_CHECK_EQUAL(mode(tri0h1), 0.5);
  557. BOOST_CHECK_EQUAL(pdf(tri0h1, -1), 0);
  558. BOOST_CHECK_EQUAL(cdf(tri0h1, -1), 0);
  559. BOOST_CHECK_EQUAL(pdf(tri0h1, 1), 0);
  560. BOOST_CHECK_EQUAL(pdf(tri0h1, 999), 0);
  561. BOOST_CHECK_EQUAL(cdf(tri0h1, 999), 1);
  562. BOOST_CHECK_EQUAL(cdf(tri0h1, 1), 1);
  563. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0h1, 0.1), 0.02, tol5eps);
  564. BOOST_CHECK_EQUAL(cdf(tri0h1, 0.5), 0.5);
  565. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0h1, 0.9), 0.98, tol5eps);
  566. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.), 0., tol5eps);
  567. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.02), 0.1, tol5eps);
  568. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.5), 0.5, tol5eps);
  569. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 0.98), 0.9, tol5eps);
  570. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0h1, 1.), 1., tol5eps);
  571. triangular tri0q1(0, 0.25, 1); // mode is near bottom.
  572. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.02), 0.0016, tol5eps);
  573. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.5), 0.66666666666666666666666666666666666666666666667, tol5eps);
  574. BOOST_CHECK_CLOSE_FRACTION(cdf(tri0q1, 0.98), 0.99946666666666661, tol5eps);
  575. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, 0.0016), 0.02, tol5eps);
  576. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, 0.66666666666666666666666666666666666666666666667), 0.5, tol5eps);
  577. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, 0.3333333333333333333333333333333333333333333333333)), 0.5, tol5eps);
  578. BOOST_CHECK_CLOSE_FRACTION(quantile(tri0q1, 0.99946666666666661), 0.98, 10 * tol5eps);
  579. triangular trim12(-1, -0.5, 2); // mode is negative.
  580. BOOST_CHECK_CLOSE_FRACTION(pdf(trim12, 0), 0.533333333333333333333333333333333333333333333, tol5eps);
  581. BOOST_CHECK_CLOSE_FRACTION(cdf(trim12, 0), 0.466666666666666666666666666666666666666666667, tol5eps);
  582. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(trim12, 0)), 1 - 0.466666666666666666666666666666666666666666667, tol5eps);
  583. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0q1, 1 - 0.99946666666666661)), 0.98, 10 * tol5eps);
  584. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 1.)), 0., tol5eps);
  585. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0.5)), 0.5, tol5eps); // OK
  586. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 1. - 0.02)), 0.1, tol5eps);
  587. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 1. - 0.98)), 0.9, tol5eps);
  588. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(tri0h1, 0)), 1., tol5eps);
  589. double xs [] = {0., 0.01, 0.02, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.98, 0.99, 1.};
  590. const triangular_distribution<double>& distr = tristd;
  591. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(distr, 1.)), 0., tol5eps);
  592. const triangular_distribution<double>* distp = &tristd;
  593. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*distp, 1.)), 0., tol5eps);
  594. const triangular_distribution<double>* dists [] = {&tristd, &tri011, &tri0q1, &tri0h1, &trim12};
  595. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(*dists[1], 1.)), 0., tol5eps);
  596. for (int i = 0; i < 5; i++)
  597. {
  598. const triangular_distribution<double>* const dist = dists[i];
  599. cout << "Distribution " << i << endl;
  600. BOOST_CHECK_EQUAL(quantile(complement(*dists[i], 1.)), quantile(*dists[i], 0.));
  601. BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.5), quantile(complement(*dist, 0.5)), tol5eps); // OK
  602. BOOST_CHECK_CLOSE_FRACTION(quantile(*dists[i], 0.98), quantile(complement(*dist, 1. - 0.98)),tol5eps);
  603. // cout << setprecision(17) << median(*dist) << endl;
  604. }
  605. cout << showpos << setprecision(2) << endl;
  606. //triangular_distribution<double>& dist = trim12;
  607. for (unsigned i = 0; i < sizeof(xs) /sizeof(double); i++)
  608. {
  609. double x = xs[i] * (trim12.upper() - trim12.lower()) + trim12.lower();
  610. double dx = cdf(trim12, x);
  611. double cx = cdf(complement(trim12, x));
  612. //cout << fixed << showpos << setprecision(3)
  613. // << xs[i] << ", " << x << ", " << pdf(trim12, x) << ", " << dx << ", " << cx << ",, " ;
  614. BOOST_CHECK_CLOSE_FRACTION(cx, 1 - dx, tol500eps); // cx == 1 - dx
  615. // << setprecision(2) << scientific << cr - x << ", " // difference x - quan(cdf)
  616. // << setprecision(3) << fixed
  617. // << quantile(trim12, dx) << ", "
  618. // << quantile(complement(trim12, 1 - dx)) << ", "
  619. // << quantile(complement(trim12, cx)) << ", "
  620. // << endl;
  621. BOOST_CHECK_CLOSE_FRACTION(quantile(trim12, dx), quantile(complement(trim12, 1 - dx)), tol500eps);
  622. }
  623. cout << endl;
  624. // Basic sanity-check spot values.
  625. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  626. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  627. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  628. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  629. test_spots(0.0L); // Test long double.
  630. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  631. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  632. #endif
  633. #else
  634. std::cout << "<note>The long double tests have been disabled on this platform "
  635. "either because the long double overloads of the usual math functions are "
  636. "not available at all, or because they are too inaccurate for these tests "
  637. "to pass.</note>" << std::endl;
  638. #endif
  639. } // BOOST_AUTO_TEST_CASE( test_main )
  640. /*
  641. Output:
  642. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_triangular.exe"
  643. Running 1 test case...
  644. Distribution 0
  645. Distribution 1
  646. Distribution 2
  647. Distribution 3
  648. Distribution 4
  649. Tolerance for type float is 5.96046e-007.
  650. Tolerance for type double is 1.11022e-015.
  651. Tolerance for type long double is 1.11022e-015.
  652. Tolerance for type class boost::math::concepts::real_concept is 1.11022e-015.
  653. *** No errors detected
  654. */