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
8 #ifndef BOOST_MATH_STATISTICS_ANDERSON_DARLING_HPP
9 #define BOOST_MATH_STATISTICS_ANDERSON_DARLING_HPP
10
11 #include <cmath>
12 #include <algorithm>
13 #include <boost/math/statistics/univariate_statistics.hpp>
14 #include <boost/math/special_functions/erf.hpp>
15
16 namespace boost { namespace math { namespace statistics {
17
18 template<class RandomAccessContainer>
anderson_darling_normality_statistic(RandomAccessContainer const & v,typename RandomAccessContainer::value_type mu=std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN (),typename RandomAccessContainer::value_type sd=std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN ())19 auto anderson_darling_normality_statistic(RandomAccessContainer const & v,
20 typename RandomAccessContainer::value_type mu = std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN(),
21 typename RandomAccessContainer::value_type sd = std::numeric_limits<typename RandomAccessContainer::value_type>::quiet_NaN())
22 {
23 using Real = typename RandomAccessContainer::value_type;
24 using std::log;
25 using std::sqrt;
26 using boost::math::erfc;
27
28 if (std::isnan(mu)) {
29 mu = boost::math::statistics::mean(v);
30 }
31 if (std::isnan(sd)) {
32 sd = sqrt(boost::math::statistics::sample_variance(v));
33 }
34
35 typedef boost::math::policies::policy<
36 boost::math::policies::promote_float<false>,
37 boost::math::policies::promote_double<false> >
38 no_promote_policy;
39
40 // This is where Knuth's literate programming could really come in handy!
41 // I need some LaTeX. The idea is that before any observation, the ecdf is identically zero.
42 // So we need to compute:
43 // \int_{-\infty}^{v_0} \frac{F(x)F'(x)}{1- F(x)} \, \mathrm{d}x, where F(x) := \frac{1}{2}[1+\erf(\frac{x-\mu}{\sigma \sqrt{2}})]
44 // Astonishingly, there is an analytic evaluation to this integral, as you can validate with the following Mathematica command:
45 // Integrate[(1/2 (1 + Erf[(x - mu)/Sqrt[2*sigma^2]])*Exp[-(x - mu)^2/(2*sigma^2)]*1/Sqrt[2*\[Pi]*sigma^2])/(1 - 1/2 (1 + Erf[(x - mu)/Sqrt[2*sigma^2]])),
46 // {x, -Infinity, x0}, Assumptions -> {x0 \[Element] Reals && mu \[Element] Reals && sigma > 0}]
47 // This gives (for s = x-mu/sqrt(2sigma^2))
48 // -1/2 + erf(s) + log(2/(1+erf(s)))
49
50
51 Real inv_var_scale = 1/(sd*sqrt(Real(2)));
52 Real s0 = (v[0] - mu)*inv_var_scale;
53 Real erfcs0 = erfc(s0, no_promote_policy());
54 // Note that if erfcs0 == 0, then left_tail = inf (numerically), and hence the entire integral is numerically infinite:
55 if (erfcs0 <= 0) {
56 return std::numeric_limits<Real>::infinity();
57 }
58
59 // Note that we're going to add erfcs0/2 when we compute the integral over [x_0, x_1], so drop it here:
60 Real left_tail = -1 + log(Real(2));
61
62
63 // For the right tail, the ecdf is identically 1.
64 // Hence we need the integral:
65 // \int_{v_{n-1}}^{\infty} \frac{(1-F(x))F'(x)}{F(x)} \, \mathrm{d}x
66 // This also has an analytic evaluation! It can be found via the following Mathematica command:
67 // Integrate[(E^(-(z^2/2)) *(1 - 1/2 (1 + Erf[z/Sqrt[2]])))/(Sqrt[2 \[Pi]] (1/2 (1 + Erf[z/Sqrt[2]]))),
68 // {z, zn, \[Infinity]}, Assumptions -> {zn \[Element] Reals && mu \[Element] Reals}]
69 // This gives (for sf = xf-mu/sqrt(2sigma^2))
70 // -1/2 + erf(sf)/2 + 2log(2/(1+erf(sf)))
71
72 Real sf = (v[v.size()-1] - mu)*inv_var_scale;
73 //Real erfcsf = erfc<Real>(sf, no_promote_policy());
74 // This is the actual value of the tail integral. However, the -erfcsf/2 cancels from the integral over [v_{n-2}, v_{n-1}]:
75 //Real right_tail = -erfcsf/2 + log(Real(2)) - log(2-erfcsf);
76
77 // Use erfc(-x) = 2 - erfc(x)
78 Real erfcmsf = erfc<Real>(-sf, no_promote_policy());
79 // Again if this is precisely zero then the integral is numerically infinite:
80 if (erfcmsf == 0) {
81 return std::numeric_limits<Real>::infinity();
82 }
83 Real right_tail = log(2/erfcmsf);
84
85 // Now we need each integral:
86 // \int_{v_i}^{v_{i+1}} \frac{(i+1/n - F(x))^2F'(x)}{F(x)(1-F(x))} \, \mathrm{d}x
87 // Again we get an analytical evaluation via the following Mathematica command:
88 // Integrate[((E^(-(z^2/2))/Sqrt[2 \[Pi]])*(k1 - F[z])^2)/(F[z]*(1 - F[z])),
89 // {z, z1, z2}, Assumptions -> {z1 \[Element] Reals && z2 \[Element] Reals &&k1 \[Element] Reals}] // FullSimplify
90
91 Real integrals = 0;
92 int64_t N = v.size();
93 for (int64_t i = 0; i < N - 1; ++i) {
94 if (v[i] > v[i+1]) {
95 throw std::domain_error("Input data must be sorted in increasing order v[0] <= v[1] <= . . . <= v[n-1]");
96 }
97
98 Real k = (i+1)/Real(N);
99 Real s1 = (v[i+1]-mu)*inv_var_scale;
100 Real erfcs1 = erfc<Real>(s1, no_promote_policy());
101 Real term = k*(k*log(erfcs0*(-2 + erfcs1)/(erfcs1*(-2 + erfcs0))) + 2*log(erfcs1/erfcs0));
102
103 integrals += term;
104 s0 = s1;
105 erfcs0 = erfcs1;
106 }
107 integrals -= log(erfcs0);
108 return v.size()*(left_tail + right_tail + integrals);
109 }
110
111 }}}
112 #endif
113