Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / empirical_cumulative_distribution_test.cpp
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 #include "math_unit_test.hpp"
9 #include <numeric>
10 #include <utility>
11 #include <random>
12 #include <boost/core/demangle.hpp>
13 #include <boost/math/distributions/empirical_cumulative_distribution_function.hpp>
14 #ifdef BOOST_HAS_FLOAT128
15 #include <boost/multiprecision/float128.hpp>
16 using boost::multiprecision::float128;
17 #endif
18
19 using boost::math::empirical_cumulative_distribution_function;
20
21 template<class Z>
22 void test_uniform_z()
23 {
24     std::vector<Z> v{6,3,4,1,2,5};
25
26     auto ecdf = empirical_cumulative_distribution_function(std::move(v));
27
28     CHECK_ULP_CLOSE(1.0/6.0, ecdf(1), 1);
29     CHECK_ULP_CLOSE(2.0/6.0, ecdf(2), 1);
30     CHECK_ULP_CLOSE(3.0/6.0, ecdf(3), 1);
31     CHECK_ULP_CLOSE(4.0/6.0, ecdf(4), 1);
32     CHECK_ULP_CLOSE(5.0/6.0, ecdf(5), 1);
33     CHECK_ULP_CLOSE(6.0/6.0, ecdf(6), 1);
34
35     // Less trivial:
36
37     v = {6,3,4,1,1,1,2,4};
38     ecdf = empirical_cumulative_distribution_function(std::move(v));
39     CHECK_ULP_CLOSE(3.0/8.0, ecdf(1), 1);
40     CHECK_ULP_CLOSE(4.0/8.0, ecdf(2), 1);
41     CHECK_ULP_CLOSE(5.0/8.0, ecdf(3), 1);
42     CHECK_ULP_CLOSE(7.0/8.0, ecdf(4), 1);
43     CHECK_ULP_CLOSE(7.0/8.0, ecdf(5), 1);
44     CHECK_ULP_CLOSE(8.0/8.0, ecdf(6), 1);
45 }
46
47 template<class Real>
48 void test_uniform()
49 {
50     size_t n = 128;
51     std::vector<Real> v(n);
52     for (size_t i = 0; i < n; ++i) {
53       v[i] = Real(i+1)/Real(n);
54     }
55
56     auto ecdf = empirical_cumulative_distribution_function(std::move(v));
57
58     for (size_t i = 0; i < n; ++i) {
59       CHECK_ULP_CLOSE(Real(i+1)/Real(n), ecdf(Real(i+1)/Real(n)), 1);
60     }
61 }
62
63
64 int main()
65 {
66     test_uniform_z<int>();
67     test_uniform<double>();
68     return boost::math::test::report_errors();
69 }