anderson_darling.qbk 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [/
  2. Copyright (c) 2019 Nick Thompson
  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. [section:anderson_darling The Anderson-Darling Test]
  8. [heading Synopsis]
  9. ```
  10. #include <boost/math/statistics/anderson_darling.hpp>
  11. namespace boost{ namespace math { namespace { statistics {
  12. template<class RandomAccessContainer>
  13. auto anderson_darling_normality_statistic(RandomAccessContainer const & v,
  14. typename RandomAccessContainer::value_type mu = std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN(),
  15. typename RandomAccessContainer::value_type sd = std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN());
  16. }}}
  17. ```
  18. [heading Background]
  19. The Anderson-Darling test for normality asks if a given sequence of numbers are drawn from a normal distribution by computing an integral over the empirical cumulative distribution function.
  20. The test statistic /A/[super 2] is given by
  21. [$../graphs/anderson_darling_definition.svg]
  22. where /F/[sub /n/] is the empirical cumulative distribution and /F/ is the CDF of the normal distribution.
  23. The value returned by the routine is /A/[super 2].
  24. If /A/[super 2]\/n converges to zero as /n/ goes to infinity, then the hypothesis that the data is normally distributed is supported by the test.
  25. If /A/[super 2]\/n converges to a finite positive value as /n/ goes to infinity, then the hypothesis is not supported by the test.
  26. An example usage is demonstrated below:
  27. ```
  28. #include <vector>
  29. #include <random>
  30. #include <iostream>
  31. #include <boost/math/statistics/anderson_darling.hpp>
  32. using boost::math::statistics::anderson_darling_normality_statistic;
  33. std::random_device rd;
  34. std::normal_distribution<double> dis(0, 1);
  35. std::vector<double> v(8192);
  36. for (auto & x : v) { x = dis(rd); }
  37. std::sort(v.begin(), v.end());
  38. double presumed_mean = 0;
  39. double presumed_standard_deviation = 0;
  40. double Asq = anderson_darling_normality_statistic(v, presumed_mean, presumed_standard_deviation);
  41. std::cout << "A^2/n = " << Asq/v.size() << "\n";
  42. 5.39e-05 // should be small . . .
  43. // Now use an incorrect hypothesis:
  44. presumed_mean = 4;
  45. Asq = anderson_darling_normality_statistic(v, presumed_mean, presumed_standard_deviation);
  46. std::cout << "A^2/n = " << Asq/v.size() << "\n";
  47. 7.41 // should be somewhat large . . .
  48. ```
  49. The Anderson-Darling normality requires sorted data.
  50. If the data are not sorted an exception is thrown.
  51. If you simply wish to know whether or not data is normally distributed, and not whether it is normally distributed with a presumed mean and variance,
  52. then you can call the function without the final two arguments, and the mean and variance will be estimated from the data themselves:
  53. ```
  54. double Asq = anderson_darling_normality_statistic(v);
  55. ```
  56. The following graph demonstrates the convergence of the test statistic.
  57. Each data point represents a vector of length /n/ which is filled with normally distributed data.
  58. The test statistic is computed over this vector, divided by /n/, and passed to the natural logarithm.
  59. This exhibits the (admittedly slow) convergence of the integral to zero when the hypothesis is true.
  60. [$../graphs/anderson_darling_simulation.svg]
  61. [heading Performance]
  62. ```
  63. ---------------------------------------------------------------
  64. Benchmark Time
  65. ---------------------------------------------------------------
  66. AndersonDarlingNormalityTest<float>/8 224 ns bytes_per_second=136.509M/s
  67. AndersonDarlingNormalityTest<float>/16 435 ns bytes_per_second=140.254M/s
  68. AndersonDarlingNormalityTest<float>/32 898 ns bytes_per_second=135.995M/s
  69. AndersonDarlingNormalityTest<float>/64 1773 ns bytes_per_second=137.675M/s
  70. AndersonDarlingNormalityTest<float>/128 3455 ns bytes_per_second=141.338M/s
  71. AndersonDarlingNormalityTest<float>/256 7001 ns bytes_per_second=139.488M/s
  72. AndersonDarlingNormalityTest<float>/512 13996 ns bytes_per_second=139.551M/s
  73. AndersonDarlingNormalityTest<float>/1024 28129 ns bytes_per_second=138.868M/s
  74. AndersonDarlingNormalityTest<float>/2048 55723 ns bytes_per_second=140.206M/s
  75. AndersonDarlingNormalityTest<float>/4096 112008 ns bytes_per_second=139.501M/s
  76. AndersonDarlingNormalityTest<float>/8192 224643 ns bytes_per_second=139.11M/s
  77. AndersonDarlingNormalityTest<float>/16384 450320 ns bytes_per_second=138.791M/s
  78. AndersonDarlingNormalityTest<float>/32768 896409 ns bytes_per_second=139.45M/s
  79. AndersonDarlingNormalityTest<float>/65536 1797800 ns bytes_per_second=139.058M/s
  80. AndersonDarlingNormalityTest<float>/131072 3604995 ns bytes_per_second=138.698M/s
  81. AndersonDarlingNormalityTest<float>/262144 7235625 ns bytes_per_second=138.207M/s
  82. AndersonDarlingNormalityTest<float>/524288 14502815 ns bytes_per_second=137.904M/s
  83. AndersonDarlingNormalityTest<float>/1048576 29058087 ns bytes_per_second=137.659M/s
  84. AndersonDarlingNormalityTest<float>/2097152 58470439 ns bytes_per_second=136.824M/s
  85. AndersonDarlingNormalityTest<float>/4194304 117476365 ns bytes_per_second=136.201M/s
  86. AndersonDarlingNormalityTest<float>/8388608 239887895 ns bytes_per_second=133.397M/s
  87. AndersonDarlingNormalityTest<float>/16777216 488787211 ns bytes_per_second=130.94M/s
  88. AndersonDarlingNormalityTest<float>_BigO 28.96 N 28.96 N
  89. AndersonDarlingNormalityTest<double>/8 470 ns bytes_per_second=129.733M/s
  90. AndersonDarlingNormalityTest<double>/16 911 ns bytes_per_second=133.989M/s
  91. AndersonDarlingNormalityTest<double>/32 1773 ns bytes_per_second=137.723M/s
  92. AndersonDarlingNormalityTest<double>/64 3368 ns bytes_per_second=144.966M/s
  93. AndersonDarlingNormalityTest<double>/128 6627 ns bytes_per_second=147.357M/s
  94. AndersonDarlingNormalityTest<double>/256 12458 ns bytes_per_second=156.777M/s
  95. AndersonDarlingNormalityTest<double>/512 23060 ns bytes_per_second=169.395M/s
  96. AndersonDarlingNormalityTest<double>/1024 44529 ns bytes_per_second=175.45M/s
  97. AndersonDarlingNormalityTest<double>/2048 88735 ns bytes_per_second=176.087M/s
  98. AndersonDarlingNormalityTest<double>/4096 175583 ns bytes_per_second=177.978M/s
  99. AndersonDarlingNormalityTest<double>/8192 348042 ns bytes_per_second=179.577M/s
  100. AndersonDarlingNormalityTest<double>/16384 701439 ns bytes_per_second=178.206M/s
  101. AndersonDarlingNormalityTest<double>/32768 1394597 ns bytes_per_second=179.262M/s
  102. AndersonDarlingNormalityTest<double>/65536 2777943 ns bytes_per_second=179.994M/s
  103. AndersonDarlingNormalityTest<double>/131072 5571455 ns bytes_per_second=179.487M/s
  104. AndersonDarlingNormalityTest<double>/262144 11161456 ns bytes_per_second=179.193M/s
  105. AndersonDarlingNormalityTest<double>/524288 22048950 ns bytes_per_second=181.417M/s
  106. AndersonDarlingNormalityTest<double>/1048576 44094409 ns bytes_per_second=181.429M/s
  107. AndersonDarlingNormalityTest<double>/2097152 88300185 ns bytes_per_second=181.199M/s
  108. AndersonDarlingNormalityTest<double>/4194304 176140378 ns bytes_per_second=181.678M/s
  109. AndersonDarlingNormalityTest<double>/8388608 352102955 ns bytes_per_second=181.769M/s
  110. AndersonDarlingNormalityTest<double>/16777216 706160246 ns bytes_per_second=181.267M/s
  111. AndersonDarlingNormalityTest<double>_BigO 42.06 N
  112. ```
  113. [heading Caveats]
  114. Some authors, including [@https://www.itl.nist.gov/div898/handbook/eda/section3/eda35e.htm NIST], give the following definition of the Anderson-Darling test statistic:
  115. [$../graphs/alternative_anderson_darling_definition.svg]
  116. This is an approximation to the quadrature sum we use as our definition.
  117. Boost.Math /does not compute this quantity/.
  118. (However, with a sufficiently large amount of data the two definitions seem to agree to two digits, so the importance of making a clear distinction between the two is unclear.)
  119. Our computation of the Anderson-Darling test statistic agrees with Mathematica.
  120. [endsect]
  121. [/section:anderson_darling]