JayBeams  0.1
Another project to have fun coding.
ut_explicit_cuts_binning.cpp
Go to the documentation of this file.
2 #include <jb/histogram.hpp>
3 
4 #include <boost/test/unit_test.hpp>
5 
6 namespace {
7 
8 typedef jb::histogram<jb::explicit_cuts_binning<int>> tested_histogram;
9 
10 } // anonymous namespace
11 
12 /**
13  * @test Verify the constructor in jb::explicit_cuts_binning works as expected.
14  */
15 BOOST_AUTO_TEST_CASE(explicit_cuts_binning_constructor) {
16  std::vector<int> cuts;
17  BOOST_CHECK_THROW(
18  jb::explicit_cuts_binning<int>(cuts.begin(), cuts.end()), std::exception);
19  BOOST_CHECK_THROW(jb::explicit_cuts_binning<int>({10}), std::exception);
20  BOOST_CHECK_NO_THROW(jb::explicit_cuts_binning<int>({1, 2}));
21  BOOST_CHECK_NO_THROW(jb::explicit_cuts_binning<int>({1, 2, 3, 4}));
22  BOOST_CHECK_THROW(
23  jb::explicit_cuts_binning<int>({1, 2, 5, 4}), std::exception);
24  BOOST_CHECK_THROW(
25  jb::explicit_cuts_binning<int>({1, 2, 2, 4}), std::exception);
26 }
27 
28 /**
29  * @test Verify that jb::explicit_cuts_binning works as expected.
30  */
31 BOOST_AUTO_TEST_CASE(explicit_cuts_binning_basic) {
32  std::vector<int> cuts{0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
33  100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
34 
35  jb::explicit_cuts_binning<int> bin(cuts.begin(), cuts.end());
36  BOOST_CHECK_EQUAL(bin.histogram_min(), 0);
37  BOOST_CHECK_EQUAL(bin.histogram_max(), 1000);
38  BOOST_CHECK_EQUAL(bin.theoretical_min(), std::numeric_limits<int>::min());
39  BOOST_CHECK_EQUAL(bin.theoretical_max(), std::numeric_limits<int>::max());
40  BOOST_CHECK_EQUAL(bin.sample2bin(0), 0);
41  BOOST_CHECK_EQUAL(bin.sample2bin(5), 0);
42  BOOST_CHECK_EQUAL(bin.sample2bin(25), 2);
43  BOOST_CHECK_EQUAL(bin.sample2bin(90), 9);
44  BOOST_CHECK_EQUAL(bin.sample2bin(99), 9);
45  BOOST_CHECK_EQUAL(bin.sample2bin(100), 10);
46  BOOST_CHECK_EQUAL(bin.sample2bin(120), 10);
47  BOOST_CHECK_EQUAL(bin.sample2bin(193), 10);
48  BOOST_CHECK_EQUAL(bin.sample2bin(400), 13);
49  BOOST_CHECK_EQUAL(bin.sample2bin(999), 18);
50  BOOST_CHECK_EQUAL(bin.bin2sample(0), 0);
51  BOOST_CHECK_EQUAL(bin.bin2sample(1), 10);
52  BOOST_CHECK_EQUAL(bin.bin2sample(10), 100);
53  BOOST_CHECK_EQUAL(bin.bin2sample(11), 200);
54  BOOST_CHECK_EQUAL(bin.bin2sample(14), 500);
55 }
56 
57 /**
58  * @test Verify that jb::explicit_cuts_binning works with jb::histogram.
59  */
60 BOOST_AUTO_TEST_CASE(explicit_cuts_binning_histogram) {
61  tested_histogram h(
62  tested_histogram::binning_strategy{10, 20, 30, 40, 50, 100, 150, 200});
63  BOOST_CHECK_THROW(h.estimated_mean(), std::invalid_argument);
64 
65  h.sample(0);
66  h.sample(10);
67  h.sample(20);
68  h.sample(30);
69  h.sample(40);
70  // each bucket is estimated at the central point
71  BOOST_CHECK_EQUAL(h.estimated_mean(), 25);
72 
73  h.sample(40);
74  h.sample(40);
75  h.sample(100);
76  h.sample(200);
77  h.sample(1000);
78 
79  double eps = (1 << 8) * std::numeric_limits<double>::epsilon();
80  BOOST_CHECK_CLOSE(h.estimated_quantile(0.00), 0.00, eps);
81  BOOST_CHECK_CLOSE(h.estimated_quantile(0.10), 10.00, eps);
82  BOOST_CHECK_CLOSE(h.estimated_quantile(0.20), 20.00, eps);
83  BOOST_CHECK_CLOSE(h.estimated_quantile(0.30), 29.00, eps);
84  BOOST_CHECK_CLOSE(h.estimated_quantile(0.40), 40.00, eps);
85  BOOST_CHECK_CLOSE(h.estimated_quantile(0.50), 43.00, eps);
86  BOOST_CHECK_CLOSE(h.estimated_quantile(0.60), 46.00, eps);
87  BOOST_CHECK_CLOSE(h.estimated_quantile(0.70), 50.00, eps);
88  BOOST_CHECK_CLOSE(h.estimated_quantile(0.80), 150.00, eps);
89  BOOST_CHECK_CLOSE(h.estimated_quantile(1.00), 1000.00, eps);
90 }
A histogram class with controllable binning and range strategy.
Definition: histogram.hpp:46
A histogram binning_strategy for integer numbers with user defined cuts.
BOOST_AUTO_TEST_CASE(explicit_cuts_binning_constructor)