test_normal.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Copyright Paul A. Bristow 2010.
  2. // Copyright John Maddock 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_normal.cpp
  8. // http://en.wikipedia.org/wiki/Normal_distribution
  9. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
  10. // Also:
  11. // Weisstein, Eric W. "Normal Distribution."
  12. // From MathWorld--A Wolfram Web Resource.
  13. // http://mathworld.wolfram.com/NormalDistribution.html
  14. #include <pch.hpp> // include directory /libs/math/src/tr1/ is needed.
  15. #ifdef _MSC_VER
  16. # pragma warning (disable: 4127) // conditional expression is constant
  17. // caused by using if(std::numeric_limits<RealType>::has_infinity)
  18. // and if (std::numeric_limits<RealType>::has_quiet_NaN)
  19. #endif
  20. #include <boost/math/tools/test.hpp>
  21. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  22. #define BOOST_TEST_MAIN
  23. #include <boost/test/unit_test.hpp> // Boost.Test
  24. #include <boost/test/tools/floating_point_comparison.hpp>
  25. #include <boost/math/distributions/normal.hpp>
  26. using boost::math::normal_distribution;
  27. #include <boost/math/tools/test.hpp>
  28. #include "test_out_of_range.hpp"
  29. #include <iostream>
  30. #include <iomanip>
  31. using std::cout;
  32. using std::endl;
  33. using std::setprecision;
  34. #include <limits>
  35. using std::numeric_limits;
  36. template <class RealType>
  37. RealType NaivePDF(RealType mean, RealType sd, RealType x)
  38. {
  39. // Deliberately naive PDF calculator again which
  40. // we'll compare our pdf function. However some
  41. // published values to compare against would be better....
  42. using namespace std;
  43. return exp(-(x-mean)*(x-mean)/(2*sd*sd))/(sd * sqrt(2*boost::math::constants::pi<RealType>()));
  44. }
  45. template <class RealType>
  46. void check_normal(RealType mean, RealType sd, RealType x, RealType p, RealType q, RealType tol)
  47. {
  48. BOOST_CHECK_CLOSE(
  49. ::boost::math::cdf(
  50. normal_distribution<RealType>(mean, sd), // distribution.
  51. x), // random variable.
  52. p, // probability.
  53. tol); // %tolerance.
  54. BOOST_CHECK_CLOSE(
  55. ::boost::math::cdf(
  56. complement(
  57. normal_distribution<RealType>(mean, sd), // distribution.
  58. x)), // random variable.
  59. q, // probability complement.
  60. tol); // %tolerance.
  61. BOOST_CHECK_CLOSE(
  62. ::boost::math::quantile(
  63. normal_distribution<RealType>(mean, sd), // distribution.
  64. p), // probability.
  65. x, // random variable.
  66. tol); // %tolerance.
  67. BOOST_CHECK_CLOSE(
  68. ::boost::math::quantile(
  69. complement(
  70. normal_distribution<RealType>(mean, sd), // distribution.
  71. q)), // probability complement.
  72. x, // random variable.
  73. tol); // %tolerance.
  74. }
  75. template <class RealType>
  76. void test_spots(RealType)
  77. {
  78. // Basic sanity checks
  79. RealType tolerance = 1e-2f; // 1e-4 (as %)
  80. // Some tests only pass at 1e-4 because values generated by
  81. // http://faculty.vassar.edu/lowry/VassarStats.html
  82. // give only 5 or 6 *fixed* places, so small values have fewer digits.
  83. // Check some bad parameters to the distribution,
  84. #ifndef BOOST_NO_EXCEPTIONS
  85. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(0, 0), std::domain_error); // zero sd
  86. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(0, -1), std::domain_error); // negative sd
  87. #else
  88. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType>(0, 0), std::domain_error); // zero sd
  89. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType>(0, -1), std::domain_error); // negative sd
  90. #endif
  91. // Tests on extreme values of random variate x, if has std::numeric_limits infinity etc.
  92. normal_distribution<RealType> N01;
  93. if(std::numeric_limits<RealType>::has_infinity)
  94. {
  95. BOOST_CHECK_EQUAL(pdf(N01, +std::numeric_limits<RealType>::infinity()), 0); // x = + infinity, pdf = 0
  96. BOOST_CHECK_EQUAL(pdf(N01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, pdf = 0
  97. BOOST_CHECK_EQUAL(cdf(N01, +std::numeric_limits<RealType>::infinity()), 1); // x = + infinity, cdf = 1
  98. BOOST_CHECK_EQUAL(cdf(N01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, cdf = 0
  99. BOOST_CHECK_EQUAL(cdf(complement(N01, +std::numeric_limits<RealType>::infinity())), 0); // x = + infinity, c cdf = 0
  100. BOOST_CHECK_EQUAL(cdf(complement(N01, -std::numeric_limits<RealType>::infinity())), 1); // x = - infinity, c cdf = 1
  101. #ifndef BOOST_NO_EXCEPTIONS
  102. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
  103. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
  104. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
  105. #else
  106. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType>(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
  107. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType>(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
  108. BOOST_MATH_CHECK_THROW(boost::math::normal_distribution<RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
  109. #endif
  110. }
  111. if (std::numeric_limits<RealType>::has_quiet_NaN)
  112. {
  113. // No longer allow x to be NaN, then these tests should throw.
  114. BOOST_MATH_CHECK_THROW(pdf(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  115. BOOST_MATH_CHECK_THROW(cdf(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  116. BOOST_MATH_CHECK_THROW(cdf(complement(N01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
  117. BOOST_MATH_CHECK_THROW(quantile(N01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + infinity
  118. BOOST_MATH_CHECK_THROW(quantile(complement(N01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + infinity
  119. }
  120. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  121. check_normal(
  122. static_cast<RealType>(5),
  123. static_cast<RealType>(2),
  124. static_cast<RealType>(4.8),
  125. static_cast<RealType>(0.46017),
  126. static_cast<RealType>(1 - 0.46017),
  127. tolerance);
  128. check_normal(
  129. static_cast<RealType>(5),
  130. static_cast<RealType>(2),
  131. static_cast<RealType>(5.2),
  132. static_cast<RealType>(1 - 0.46017),
  133. static_cast<RealType>(0.46017),
  134. tolerance);
  135. check_normal(
  136. static_cast<RealType>(5),
  137. static_cast<RealType>(2),
  138. static_cast<RealType>(2.2),
  139. static_cast<RealType>(0.08076),
  140. static_cast<RealType>(1 - 0.08076),
  141. tolerance);
  142. check_normal(
  143. static_cast<RealType>(5),
  144. static_cast<RealType>(2),
  145. static_cast<RealType>(7.8),
  146. static_cast<RealType>(1 - 0.08076),
  147. static_cast<RealType>(0.08076),
  148. tolerance);
  149. check_normal(
  150. static_cast<RealType>(-3),
  151. static_cast<RealType>(5),
  152. static_cast<RealType>(-4.5),
  153. static_cast<RealType>(0.38209),
  154. static_cast<RealType>(1 - 0.38209),
  155. tolerance);
  156. check_normal(
  157. static_cast<RealType>(-3),
  158. static_cast<RealType>(5),
  159. static_cast<RealType>(-1.5),
  160. static_cast<RealType>(1 - 0.38209),
  161. static_cast<RealType>(0.38209),
  162. tolerance);
  163. check_normal(
  164. static_cast<RealType>(-3),
  165. static_cast<RealType>(5),
  166. static_cast<RealType>(-8.5),
  167. static_cast<RealType>(0.13567),
  168. static_cast<RealType>(1 - 0.13567),
  169. tolerance);
  170. check_normal(
  171. static_cast<RealType>(-3),
  172. static_cast<RealType>(5),
  173. static_cast<RealType>(2.5),
  174. static_cast<RealType>(1 - 0.13567),
  175. static_cast<RealType>(0.13567),
  176. tolerance);
  177. //
  178. // Tests for PDF: we know that the peak value is at 1/sqrt(2*pi)
  179. //
  180. tolerance = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percentage
  181. BOOST_CHECK_CLOSE(
  182. pdf(normal_distribution<RealType>(), static_cast<RealType>(0)),
  183. static_cast<RealType>(0.3989422804014326779399460599343818684759L), // 1/sqrt(2*pi)
  184. tolerance);
  185. BOOST_CHECK_CLOSE(
  186. pdf(normal_distribution<RealType>(3), static_cast<RealType>(3)),
  187. static_cast<RealType>(0.3989422804014326779399460599343818684759L),
  188. tolerance);
  189. BOOST_CHECK_CLOSE(
  190. pdf(normal_distribution<RealType>(3, 5), static_cast<RealType>(3)),
  191. static_cast<RealType>(0.3989422804014326779399460599343818684759L / 5),
  192. tolerance);
  193. //
  194. // Spot checks for mean = -5, sd = 6:
  195. //
  196. for(RealType x = -15; x < 5; x += 0.125)
  197. {
  198. BOOST_CHECK_CLOSE(
  199. pdf(normal_distribution<RealType>(-5, 6), x),
  200. NaivePDF(RealType(-5), RealType(6), x),
  201. tolerance);
  202. }
  203. RealType tol2 = boost::math::tools::epsilon<RealType>() * 5;
  204. normal_distribution<RealType> dist(8, 3);
  205. RealType x = static_cast<RealType>(0.125);
  206. BOOST_MATH_STD_USING // ADL of std math lib names
  207. // mean:
  208. BOOST_CHECK_CLOSE(
  209. mean(dist)
  210. , static_cast<RealType>(8), tol2);
  211. // variance:
  212. BOOST_CHECK_CLOSE(
  213. variance(dist)
  214. , static_cast<RealType>(9), tol2);
  215. // std deviation:
  216. BOOST_CHECK_CLOSE(
  217. standard_deviation(dist)
  218. , static_cast<RealType>(3), tol2);
  219. // hazard:
  220. BOOST_CHECK_CLOSE(
  221. hazard(dist, x)
  222. , pdf(dist, x) / cdf(complement(dist, x)), tol2);
  223. // cumulative hazard:
  224. BOOST_CHECK_CLOSE(
  225. chf(dist, x)
  226. , -log(cdf(complement(dist, x))), tol2);
  227. // coefficient_of_variation:
  228. BOOST_CHECK_CLOSE(
  229. coefficient_of_variation(dist)
  230. , standard_deviation(dist) / mean(dist), tol2);
  231. // mode:
  232. BOOST_CHECK_CLOSE(
  233. mode(dist)
  234. , static_cast<RealType>(8), tol2);
  235. BOOST_CHECK_CLOSE(
  236. median(dist)
  237. , static_cast<RealType>(8), tol2);
  238. // skewness:
  239. BOOST_CHECK_CLOSE(
  240. skewness(dist)
  241. , static_cast<RealType>(0), tol2);
  242. // kertosis:
  243. BOOST_CHECK_CLOSE(
  244. kurtosis(dist)
  245. , static_cast<RealType>(3), tol2);
  246. // kertosis excess:
  247. BOOST_CHECK_CLOSE(
  248. kurtosis_excess(dist)
  249. , static_cast<RealType>(0), tol2);
  250. normal_distribution<RealType> norm01(0, 1); // Test default (0, 1)
  251. BOOST_CHECK_CLOSE(
  252. mean(norm01),
  253. static_cast<RealType>(0), 0); // Mean == zero
  254. normal_distribution<RealType> defsd_norm01(0); // Test default (0, sd = 1)
  255. BOOST_CHECK_CLOSE(
  256. mean(defsd_norm01),
  257. static_cast<RealType>(0), 0); // Mean == zero
  258. normal_distribution<RealType> def_norm01; // Test default (0, sd = 1)
  259. BOOST_CHECK_CLOSE(
  260. mean(def_norm01),
  261. static_cast<RealType>(0), 0); // Mean == zero
  262. BOOST_CHECK_CLOSE(
  263. standard_deviation(def_norm01),
  264. static_cast<RealType>(1), 0); // Mean == zero
  265. // Error tests:
  266. check_out_of_range<boost::math::normal_distribution<RealType> >(0, 1); // (All) valid constructor parameter values.
  267. BOOST_MATH_CHECK_THROW(pdf(normal_distribution<RealType>(0, 0), 0), std::domain_error);
  268. BOOST_MATH_CHECK_THROW(pdf(normal_distribution<RealType>(0, -1), 0), std::domain_error);
  269. BOOST_MATH_CHECK_THROW(quantile(normal_distribution<RealType>(0, 1), -1), std::domain_error);
  270. BOOST_MATH_CHECK_THROW(quantile(normal_distribution<RealType>(0, 1), 2), std::domain_error);
  271. } // template <class RealType>void test_spots(RealType)
  272. BOOST_AUTO_TEST_CASE( test_main )
  273. {
  274. // Check that can generate normal distribution using the two convenience methods:
  275. boost::math::normal myf1(1., 2); // Using typedef
  276. normal_distribution<> myf2(1., 2); // Using default RealType double.
  277. boost::math::normal myn01; // Use default values.
  278. // Note NOT myn01() as the compiler will interpret as a function!
  279. // Check the synonyms, provided to allow generic use of find_location and find_scale.
  280. BOOST_CHECK_EQUAL(myn01.mean(), myn01.location());
  281. BOOST_CHECK_EQUAL(myn01.standard_deviation(), myn01.scale());
  282. // Basic sanity-check spot values.
  283. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  284. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  285. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  286. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  287. test_spots(0.0L); // Test long double.
  288. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  289. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  290. #endif
  291. #else
  292. std::cout << "<note>The long double tests have been disabled on this platform "
  293. "either because the long double overloads of the usual math functions are "
  294. "not available at all, or because they are too inaccurate for these tests "
  295. "to pass.</note>" << std::endl;
  296. #endif
  297. } // BOOST_AUTO_TEST_CASE( test_main )
  298. /*
  299. Output:
  300. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_normal.exe"
  301. Running 1 test case...
  302. Tolerance for type float is 0.01 %
  303. Tolerance for type double is 0.01 %
  304. Tolerance for type long double is 0.01 %
  305. Tolerance for type class boost::math::concepts::real_concept is 0.01 %
  306. *** No errors detected
  307. ------ Build started: Project: test_normal, Configuration: Release Win32 ------
  308. test_normal.cpp
  309. Generating code
  310. Finished generating code
  311. test_normal.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_normal.exe
  312. Running 1 test case...
  313. Tolerance for type float is 0.01 %
  314. Tolerance for type double is 0.01 %
  315. Tolerance for type long double is 0.01 %
  316. Tolerance for type class boost::math::concepts::real_concept is 0.01 %
  317. *** No errors detected
  318. Detected memory leaks!
  319. Dumping objects ->
  320. {2413} normal block at 0x00321190, 42 bytes long.
  321. Data: <class boost::mat> 63 6C 61 73 73 20 62 6F 6F 73 74 3A 3A 6D 61 74
  322. {2412} normal block at 0x003231F0, 8 bytes long.
  323. Data: < 2 22 > 90 11 32 00 98 32 32 00
  324. {1824} normal block at 0x00323180, 12 bytes long.
  325. Data: <long double > 6C 6F 6E 67 20 64 6F 75 62 6C 65 00
  326. {1823} normal block at 0x00323298, 8 bytes long.
  327. Data: < 12 `22 > 80 31 32 00 60 32 32 00
  328. {1227} normal block at 0x00323148, 7 bytes long.
  329. Data: <double > 64 6F 75 62 6C 65 00
  330. {1226} normal block at 0x00323260, 8 bytes long.
  331. Data: <H12 02 > 48 31 32 00 A0 30 32 00
  332. {633} normal block at 0x003230D8, 6 bytes long.
  333. Data: <float > 66 6C 6F 61 74 00
  334. {632} normal block at 0x003230A0, 8 bytes long.
  335. Data: < 02 > D8 30 32 00 00 00 00 00
  336. Object dump complete.
  337. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
  338. */