anderson_darling_test.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright Nick Thompson, 2019
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include "math_unit_test.hpp"
  8. #include <numeric>
  9. #include <utility>
  10. #include <random>
  11. #include <boost/core/demangle.hpp>
  12. #include <boost/math/statistics/anderson_darling.hpp>
  13. #ifdef BOOST_HAS_FLOAT128
  14. #include <boost/multiprecision/float128.hpp>
  15. using boost::multiprecision::float128;
  16. #endif
  17. using boost::math::statistics::anderson_darling_normality_statistic;
  18. void test_ad_normal_agreement_w_mathematica()
  19. {
  20. // To reproduce:
  21. // data = RandomVariate[NormalDistribution[0.0, 1], 32];
  22. // AndersonDarlingTest[data, NormalDistribution[0.0, 1.0], "TestStatistic"]
  23. std::vector<double> v = {0.6018671202167422, 0.34935310534248165, -0.2975464874480147, 0.19075371678288966, 0.7871329594028097, 1.153968559452821,
  24. -0.22255484635945819, 0.08509539017654276, -1.3495873906132927, 0.5579622886226612, -0.8781637835839865, 1.144016010880056,
  25. 0.3117638373078617, 1.2774825413798292, 0.24847416220730323, -0.5002468827503855, -0.23124277830160153, 0.1880980838556693,
  26. -1.2249409521605903, 2.583585311795, -1.4237305768919715, -0.7613985266437971, -1.089096700141564, -1.2595396780059587,
  27. -0.735507006841229, -0.6957024998691735, 0.24671590370045435, 0.24620128485023313, -2.4445908627621833, 0.1882647961008893,
  28. 1.8527432579288248, -0.1908629038857823, -1.6542624501251562, 1.414542907065601, 0.8188282235404601, -0.5040501225935047,
  29. -1.634617885381599, -0.43608956281596023, -1.7347258501594351, 0.8387606547880405, 0.3554282034994122, -0.27872688042721255,
  30. -1.6445047421517192, 1.0262620986278987, -0.14517030192098415, -0.15386932114942647, -0.13364750619758223, 0.27194802295714676,
  31. -0.6396907621289796, 0.3390884293844603, -0.08298748318375798, -0.7165572035514544, -0.2580817977433051, -1.9250094572058072,
  32. -0.07995578394716944, -1.146629910012214, -2.0053073148074665, -1.6745995591714353, 0.2973860474005926, -0.4981763213542575,
  33. 0.22190763958244972, 0.699111028057888, -1.1872952966557475, 0.6151420566782272};
  34. double expected = 1.542329830419774;
  35. std::sort(v.begin(), v.end());
  36. double ADtest = anderson_darling_normality_statistic(v, 0.0, 1.0);
  37. CHECK_ULP_CLOSE(expected, ADtest, 250);
  38. double sigma = 2;
  39. for (auto & x : v) {
  40. x *= sigma;
  41. }
  42. ADtest = anderson_darling_normality_statistic(v, 0.0, sigma);
  43. CHECK_ULP_CLOSE(expected, ADtest, 250);
  44. double mu = 10;
  45. for (auto & x : v) {
  46. x += mu;
  47. }
  48. ADtest = anderson_darling_normality_statistic(v, mu, sigma);
  49. CHECK_ULP_CLOSE(expected, ADtest, 250);
  50. }
  51. int main()
  52. {
  53. test_ad_normal_agreement_w_mathematica();
  54. return boost::math::test::report_errors();
  55. }