empirical_cumulative_distribution_test.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/distributions/empirical_cumulative_distribution_function.hpp>
  13. #ifdef BOOST_HAS_FLOAT128
  14. #include <boost/multiprecision/float128.hpp>
  15. using boost::multiprecision::float128;
  16. #endif
  17. using boost::math::empirical_cumulative_distribution_function;
  18. template<class Z>
  19. void test_uniform_z()
  20. {
  21. std::vector<Z> v{6,3,4,1,2,5};
  22. auto ecdf = empirical_cumulative_distribution_function(std::move(v));
  23. CHECK_ULP_CLOSE(1.0/6.0, ecdf(1), 1);
  24. CHECK_ULP_CLOSE(2.0/6.0, ecdf(2), 1);
  25. CHECK_ULP_CLOSE(3.0/6.0, ecdf(3), 1);
  26. CHECK_ULP_CLOSE(4.0/6.0, ecdf(4), 1);
  27. CHECK_ULP_CLOSE(5.0/6.0, ecdf(5), 1);
  28. CHECK_ULP_CLOSE(6.0/6.0, ecdf(6), 1);
  29. // Less trivial:
  30. v = {6,3,4,1,1,1,2,4};
  31. ecdf = empirical_cumulative_distribution_function(std::move(v));
  32. CHECK_ULP_CLOSE(3.0/8.0, ecdf(1), 1);
  33. CHECK_ULP_CLOSE(4.0/8.0, ecdf(2), 1);
  34. CHECK_ULP_CLOSE(5.0/8.0, ecdf(3), 1);
  35. CHECK_ULP_CLOSE(7.0/8.0, ecdf(4), 1);
  36. CHECK_ULP_CLOSE(7.0/8.0, ecdf(5), 1);
  37. CHECK_ULP_CLOSE(8.0/8.0, ecdf(6), 1);
  38. }
  39. template<class Real>
  40. void test_uniform()
  41. {
  42. size_t n = 128;
  43. std::vector<Real> v(n);
  44. for (size_t i = 0; i < n; ++i) {
  45. v[i] = Real(i+1)/Real(n);
  46. }
  47. auto ecdf = empirical_cumulative_distribution_function(std::move(v));
  48. for (size_t i = 0; i < n; ++i) {
  49. CHECK_ULP_CLOSE(Real(i+1)/Real(n), ecdf(Real(i+1)/Real(n)), 1);
  50. }
  51. }
  52. int main()
  53. {
  54. test_uniform_z<int>();
  55. test_uniform<double>();
  56. return boost::math::test::report_errors();
  57. }