Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement skewness for K-S distribution [CI SKIP]
The third moment integrates nicely with the help of Apery's constant
(zeta_three). Verify the result via quadrature.
  • Loading branch information
evanmiller committed Aug 31, 2020
commit 59c4da1dc1d4a643283438be504edcf97094b70e
14 changes: 14 additions & 0 deletions include/boost/math/distributions/kolmogorov_smirnov.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,5 +447,19 @@ inline RealType variance(const kolmogorov_smirnov_distribution<RealType, Policy>
return 0.5 * (constants::pi_sqr_div_six<RealType>()
- constants::pi<RealType>() * constants::ln_two<RealType>() * constants::ln_two<RealType>()) / n;
}

template <class RealType, class Policy>
inline RealType skewness(const kolmogorov_smirnov_distribution<RealType, Policy>& dist)
{
static const char* function = "boost::math::skewness(const kolmogorov_smirnov_distribution<%1%>&)";
RealType n = dist.number_of_observations();
RealType error_result = 0;
if(false == detail::check_df(function, n, &error_result, Policy()))
return error_result;
RealType ex3 = 9.0 / 16.0 * constants::root_half_pi<RealType>() * constants::zeta_three<RealType>() / n / sqrt(n);
RealType mean = boost::math::mean(dist);
RealType var = boost::math::variance(dist);
return (ex3 - 3 * mean * var - mean * mean * mean) / var / sqrt(var);
}
}}
#endif
6 changes: 5 additions & 1 deletion test/test_kolmogorov_smirnov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ void test_spots(RealType T)
BOOST_TEST_CHECK(pdf(dist, mode) >= pdf(dist, mode - 100 * tol));
BOOST_TEST_CHECK(pdf(dist, mode) >= pdf(dist, mode + 100 * tol));

// Test first three moments - pretty well covers the entire distribution
// Test first four moments - pretty well covers the entire distribution
RealType mean = boost::math::mean(dist);
RealType var = variance(dist);
RealType skew = skewness(dist);
quadrature::exp_sinh<RealType> integrator;
auto f_one = [&, dist](RealType t) { return pdf(dist, t); };
auto f_mean = [&, dist](RealType t) { return pdf(dist, t) * t; };
auto f_var = [&, dist, mean](RealType t) { return pdf(dist, t) * (t - mean) * (t - mean); };
auto f_skew = [&, dist, mean, var](RealType t) { return pdf(dist, t)
* (t - mean) * (t - mean) * (t - mean) / var / sqrt(var); };
BOOST_CHECK_CLOSE_FRACTION(integrator.integrate(f_one, eps), RealType(1), tol);
BOOST_CHECK_CLOSE_FRACTION(integrator.integrate(f_mean, eps), mean, tol);
BOOST_CHECK_CLOSE_FRACTION(integrator.integrate(f_var, eps), var, tol);
BOOST_CHECK_CLOSE_FRACTION(integrator.integrate(f_skew, eps), skew, 4*tol);
}

BOOST_AUTO_TEST_CASE( test_main )
Expand Down