ChiSquareAccumulator_test.cc
Go to the documentation of this file.
1 /**
2  * @file ChiSquareAccumulator_test.cc
3  * @brief Tests the classes in `ChiSquareAccumulator.h`
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date July 26, 2018
6  * @version 1.0
7  * @see `lardata/Utilities/ChiSquareAccumulator.h`
8  *
9  * See http://www.boost.org/libs/test for the Boost test library home page.
10  *
11  * Timing:
12  * not given yet
13  */
14 
15 
16 // Boost libraries
17 #define BOOST_TEST_MODULE ( ChiSquareAccumulator_test )
18 #include "boost/test/unit_test.hpp"
19 
20 // LArSoft libraries
22 
23 // C/C++ standard libraries
24 #include <type_traits> // std::is_same<>
25 
27 
28 //------------------------------------------------------------------------------
30 
31  auto one = [](double){ return 1.0; };
32  auto chiSquare = lar::util::makeChiSquareAccumulator(one);
33 
34  BOOST_TEST(chiSquare.expected(1.0) == 1.0);
35  BOOST_TEST(chiSquare.expected(2.0) == 1.0);
36  BOOST_TEST(chiSquare.expected(3.0) == 1.0);
37 
38  BOOST_TEST(chiSquare.N() == 0U);
39  BOOST_TEST(chiSquare() == 0.0);
40  BOOST_TEST(double(chiSquare) == 0.0);
41  BOOST_TEST(chiSquare.chiSquare() == 0.0);
42 
43  chiSquare.add(1.0, 1.0); // uncertainty: 1
44  BOOST_TEST(chiSquare.N() == 1U);
45  BOOST_TEST(chiSquare() == 0, 1e-5% tolerance());
46  BOOST_TEST(double(chiSquare) == 0, 1e-5% tolerance());
47  BOOST_TEST(chiSquare.chiSquare() == 0, 1e-5% tolerance());
48 
49  chiSquare.add(2.0, 0.5); // uncertainty: 1
50  BOOST_TEST(chiSquare.N() == 2U);
51  BOOST_TEST(chiSquare() == 0.25, 1e-4% tolerance());
52  BOOST_TEST(double(chiSquare) == 0.25, 1e-4% tolerance());
53  BOOST_TEST(chiSquare.chiSquare() == 0.25, 1e-4% tolerance());
54 
55  chiSquare.add(3.0, 2.0, 0.5);
56  BOOST_TEST(chiSquare.N() == 3U);
57  BOOST_TEST(chiSquare() == 4.25, 1e-4% tolerance());
58  BOOST_TEST(double(chiSquare) == 4.25, 1e-4% tolerance());
59  BOOST_TEST(chiSquare.chiSquare() == 4.25, 1e-4% tolerance());
60 
61 } // testChiSquareAccumulator()
62 
63 
64 //------------------------------------------------------------------------------
66  /*
67  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
68  * double const a = 2.0;
69  * double const b = -1.0;
70  * auto f = [a,b](double x){ return a + b * x; };
71  * lar::util::ChiSquareAccumulator<decltype(f)> chiSquare;
72  *
73  * chiSquare.add(0.0, 1.0, 0.5); // add ( 0 ; 1.0 +/- 0.5 )
74  * chiSquare.add(1.0, 1.0, 0.5); // add ( 1 ; 1.0 +/- 0.5 )
75  * chiSquare.add(2.0, 1.0, 0.5); // add ( 2 ; 1.0 +/- 0.5 )
76  *
77  * double const chi2value = chiSquare();
78  * int degreesOfFreedom = int(chiSquare.N()) - 3;
79  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80  * promised `chi2value` `8.0` and `degreeOfFreedom` `0`.
81  */
82  double const a = 2.0;
83  double const b = -1.0;
84  auto f = [a,b](double x){ return a + b * x; };
86 
87  chiSquare.add(0.0, 1.0, 0.5); // add ( 0 ; 1.0 +/- 0.5 )
88  chiSquare.add(1.0, 1.0, 0.5); // add ( 1 ; 1.0 +/- 0.5 )
89  chiSquare.add(2.0, 1.0, 0.5); // add ( 2 ; 1.0 +/- 0.5 )
90 
91  double const chi2value = chiSquare();
92  int degreesOfFreedom = chiSquare.N() - 3;
93 
94  BOOST_TEST(chi2value == 8.0, 0.001% tolerance());
95  BOOST_TEST(degreesOfFreedom == 0U);
96 
97 } // testChiSquareAccumulator_documentation();
98 
99 
100 //------------------------------------------------------------------------------
102 
103  /*
104  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
105  * auto zero = [](double){ return 0.0; }; // expectation function
106  * auto chiSquare = lar::util::makeChiSquareAccumulator(zero);
107  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108  * declare `chiSquare` in a way equivalent to:
109  * `lar::util::ChiSquareAccumulator<decltype(zero)> chiSquare(zero)`.
110  */
111  auto zero = [](double){ return 0.0; }; // expectation function
112  auto const& chiSquare = lar::util::makeChiSquareAccumulator(zero);
113 
114  BOOST_TEST(chiSquare.expected(-2.0) == 0.0);
115  BOOST_TEST(chiSquare.expected(0.0) == 0.0);
116  BOOST_TEST(chiSquare.expected(2.0) == 0.0);
117  static_assert(std::is_same<decltype(chiSquare()), double>::value,
118  "makeChiSquareAccumulator() returned an unexpected type!"
119  );
120 
121 } // testMakeChiSquareAccumulator_documentation1()
122 
123 
124 //------------------------------------------------------------------------------
126 
127  /*
128  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
129  * auto zero = [](float){ return 0.0F; }; // expectation function
130  * auto chiSquare = lar::util::makeChiSquareAccumulator<float>(zero);
131  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132  * declare `chiSquare` in a way equivalent to:
133  * `lar::util::ChiSquareAccumulator<decltype(zero), float> chiSquare(zero)`.
134  */
135  auto zero = [](float){ return 0.0F; }; // expectation function
136  auto chiSquare = lar::util::makeChiSquareAccumulator<float>(zero);
137 
138  BOOST_TEST(chiSquare.expected(-2.0F) == 0.0F);
139  BOOST_TEST(chiSquare.expected(0.0F) == 0.0F);
140  BOOST_TEST(chiSquare.expected(2.0F) == 0.0F);
141  static_assert(std::is_same<decltype(chiSquare()), float>::value,
142  "makeChiSquareAccumulator<float>() returned an unexpected type!"
143  );
144 
145 } // testMakeChiSquareAccumulator_documentation2()
146 
147 
148 //------------------------------------------------------------------------------
149 BOOST_AUTO_TEST_CASE(ChiSquareAccumulatorTestCase) {
150 
155 
156 } // ChiSquareAccumulatorTestCase
157 
158 
159 //------------------------------------------------------------------------------
auto makeChiSquareAccumulator(F &&e)
Creates a ChiSquareAccumulator object with the specified function.
Computes a χ² from expectation function and data points.
void testChiSquareAccumulator()
auto const tolerance
void add(Data_t x, Data_t y)
Adds a data point to the χ².
const double e
const double a
BOOST_AUTO_TEST_CASE(ChiSquareAccumulatorTestCase)
void testChiSquareAccumulator_documentation()
void testMakeChiSquareAccumulator_documentation2()
void testMakeChiSquareAccumulator_documentation1()
Computes a simple χ² sum from data and a expectation function.
static bool * b
Definition: config.cpp:1043
list x
Definition: train.py:276
unsigned int N() const
Returns the number of added points (it&#39;s not degrees of freedom yet!).