SimpleFits_test.cc
Go to the documentation of this file.
1 /**
2  * @file SimpleFits_test.cc
3  * @brief Tests the classes in SimpleFits.h
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date 20141229
6  * @version 1.0
7  * @see SimpleFits.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 // define this to see some fit information
16 #define SIMPLEFITS_TEST_DEBUG
17 
18 // C/C++ standard libraries
19 #include <cmath>
20 #include <tuple>
21 #include <array>
22 #include <stdexcept> // std::range_error
23 #include <iterator> // std::ostream_iterator
24 #include <iostream>
25 
26 // Boost libraries
27 /*
28  * Boost Magic: define the name of the module;
29  * and do that before the inclusion of Boost unit test headers
30  * because it will change what they provide.
31  * Among the those, there is a main() function and some wrapping catching
32  * unhandled exceptions and considering them test failures, and probably more.
33  * This also makes fairly complicate to receive parameters from the command line
34  * (for example, a random seed).
35  */
36 #define BOOST_TEST_MODULE ( SimpleFits_test )
37 #include "boost/test/unit_test.hpp"
38 
40 
41 // LArSoft libraries
43 
44 
45 //==============================================================================
46 //=== Test code
47 //===
48 
49 //------------------------------------------------------------------------------
50 //--- Fit tests
51 //---
52 
53 
54 template <typename Array>
55 void PrintMatrix
56  (std::ostream& out, Array const& m, std::string name = "Matrix")
57 {
58  const size_t Dim = size_t(std::sqrt(m.size()));
59 
60  out << name << " " << Dim << "x" << Dim << ":";
61  for (size_t r = 0; r < Dim; ++r) {
62  out << "\n |";
63  for (size_t c = 0; c < Dim; ++c)
64  out << " " << m[r * Dim + c];
65  out << " |";
66  } // for
67  out << std::endl;
68 } // PrintMatrix()
69 
70 
71 template <typename Array>
72 void PrintVector
73  (std::ostream& out, Array const& v, std::string name = "Vector")
74 {
75  using Data_t = typename Array::value_type;
76  const size_t Dim = v.size();
77 
78  out << name << " [" << Dim << "]: { ";
79  std::copy(v.begin(), v.end(), std::ostream_iterator<Data_t>(std::cout, " "));
80  out << "}" << std::endl;
81 } // PrintVector()
82 
83 
84 template <typename Fitter>
85 void PrintFitterInfo(Fitter const& fitter) {
86 #ifdef SIMPLEFITS_TEST_DEBUG
87  typename Fitter::FitParameters_t params;
88  typename Fitter::FitMatrix_t Xmat, Smat;
89  typename Fitter::Data_t det;
90 
91  bool res = fitter.FillResults(params, Xmat, det, Smat);
92  fitter.PrintStats(std::cout);
93  PrintMatrix(std::cout, Xmat, "X matrix");
94  std::cout << "det(X) = " << det << std::endl;
95  PrintMatrix(std::cout, Smat, "S matrix");
96  PrintVector(std::cout, params, "Fit parameters");
97  if (!res) std::cout << "The fit results are marked as invalid!" << std::endl;
98 #else // !SIMPLEFITS_TEST_DEBUG
99  fitter.isValid(); // just to avoid compiler warnings
100 #endif // SIMPLEFITS_TEST_DEBUG
101 } // PrintFitterInfo()
102 
103 
104 template <typename T>
106  lar::util::LinearFit<T> const& fitter,
107  int n,
108  T intercept,
109  T slope,
110  T intercept_error,
111  T slope_error,
112  T intercept_slope_covariance,
113  T chisq,
114  int NDF
115 ) {
116 
117  BOOST_TEST(fitter.N() == n);
118  if (n == 0) {
119  BOOST_TEST(!fitter.isValid());
120  BOOST_CHECK_THROW(fitter.Slope(), std::range_error);
121  BOOST_CHECK_THROW(fitter.Intercept(), std::range_error);
122  BOOST_CHECK_THROW(fitter.SlopeError(), std::range_error);
123  BOOST_CHECK_THROW(fitter.InterceptError(), std::range_error);
124  BOOST_CHECK_THROW(fitter.InterceptSlopeCovariance(), std::range_error);
125  BOOST_CHECK_THROW(fitter.ChiSquare(), std::range_error);
126  BOOST_TEST(fitter.NDF() == -2);
127  }
128  else {
129  BOOST_TEST(fitter.isValid());
130 
131  PrintFitterInfo(fitter);
132 
133  BOOST_TEST(double(fitter.Intercept()) == double(intercept), 0.1% tolerance());
134  BOOST_TEST(double(fitter.Slope()) == double(slope), 0.1% tolerance());
135  BOOST_TEST
136  (double(fitter.InterceptError()) == double(intercept_error), 0.1% tolerance());
137  BOOST_TEST(double(fitter.SlopeError()) == double(slope_error), 0.1% tolerance());
138  BOOST_TEST(double(fitter.InterceptSlopeCovariance()) ==
139  double(intercept_slope_covariance), 0.1% tolerance());
140  if (double(chisq) == 0.)
141  BOOST_TEST(double(fitter.ChiSquare()) == 0, 1e-5% tolerance());
142  else
143  BOOST_TEST(double(fitter.ChiSquare()) == double(chisq), 0.1% tolerance());
144  BOOST_TEST(fitter.NDF() == NDF);
145  }
146 
147 } // CheckLinearFit<>()
148 
149 
150 template <typename T>
152  lar::util::QuadraticFit<T> const& fitter,
153  int n,
154  std::array<T, 3> const& solution,
155  std::array<T, 3> const& error2,
156  T chisq,
157  int NDF
158 ) {
159 
160  BOOST_TEST(fitter.N() == n);
161  if (n == 0) {
162  BOOST_TEST(!fitter.isValid());
163  BOOST_CHECK_THROW(fitter.FitParameter(0), std::range_error);
164  BOOST_CHECK_THROW(fitter.FitParameter(1), std::range_error);
165  BOOST_CHECK_THROW(fitter.FitParameter(2), std::range_error);
166  BOOST_CHECK_THROW(fitter.FitParameterError(0), std::range_error);
167  BOOST_CHECK_THROW(fitter.FitParameterError(1), std::range_error);
168  BOOST_CHECK_THROW(fitter.FitParameterError(2), std::range_error);
169  BOOST_CHECK_THROW(fitter.ChiSquare(), std::range_error);
170  BOOST_TEST(fitter.NDF() == -3);
171  }
172  else {
173  BOOST_TEST(fitter.isValid());
174 
175  PrintFitterInfo(fitter);
176 
177  // tolerance: 0.1%
178  BOOST_TEST(double(fitter.FitParameter(0)) == double(solution[0]), 0.1% tolerance());
179  BOOST_TEST(double(fitter.FitParameter(1)) == double(solution[1]), 0.1% tolerance());
180  BOOST_TEST(double(fitter.FitParameter(2)) == double(solution[2]), 0.1% tolerance());
181  BOOST_TEST
182  (double(fitter.FitParameterError(0)) == std::sqrt(double(error2[0])), 0.1% tolerance());
183  BOOST_TEST
184  (double(fitter.FitParameterError(1)) == std::sqrt(double(error2[1])), 0.1% tolerance());
185  BOOST_TEST
186  (double(fitter.FitParameterError(2)) == std::sqrt(double(error2[2])), 0.1% tolerance());
187  if (double(chisq) == 0.)
188  BOOST_TEST(double(fitter.ChiSquare()) == 0, 1e-5% tolerance());
189  else
190  BOOST_TEST(double(fitter.ChiSquare()) == double(chisq), 0.1% tolerance());
191  BOOST_TEST(fitter.NDF() == NDF);
192  }
193 
194 } // CheckQuadraticFit<>()
195 
196 
197 template <typename T>
199  lar::util::GaussianFit<T> const& fitter,
200  int n,
201  std::array<T, 3> const& solution,
202  std::array<T, 3> const& error2,
203  T chisq,
204  int NDF
205 ) {
206 
207  BOOST_TEST(fitter.N() == n);
208  if (n == 0) {
209  BOOST_TEST(!fitter.isValid());
210  BOOST_CHECK_THROW(fitter.FitParameters(), std::runtime_error);
211  BOOST_CHECK_THROW(fitter.FitParameterErrors(), std::runtime_error);
212  BOOST_CHECK_THROW(fitter.ChiSquare(), std::runtime_error);
213  BOOST_TEST(fitter.NDF() == -3);
214  }
215  else {
216  BOOST_TEST(fitter.isValid());
217 
218  using FitParameters_t = typename lar::util::GaussianFit<T>::FitParameters_t;
219 
220  // I am disabling the check on the parameter errors since I have no
221  // cross check available
222 
223  PrintFitterInfo(fitter.Fitter());
224  PrintFitterInfo(fitter);
225  FitParameters_t params = fitter.FitParameters();
226  /* FitParameters_t perrors = */ fitter.FitParameterErrors();
227 
228  // tolerance: 0.1%
229  BOOST_TEST(double(params[0]) == double(solution[0]), 0.1% tolerance());
230  BOOST_TEST(double(params[1]) == double(solution[1]), 0.1% tolerance());
231  BOOST_TEST(double(params[2]) == double(solution[2]), 0.1% tolerance());
232  // BOOST_TEST(double(perrors[0]) == std::sqrt(double(error2[0])), 0.1% tolerance());
233  // BOOST_TEST(double(perrors[1]) == std::sqrt(double(error2[1])), 0.1% tolerance());
234  // BOOST_TEST(double(perrors[2]) == std::sqrt(double(error2[2])), 0.1% tolerance());
235  if (double(chisq) == 0.)
236  BOOST_TEST(double(fitter.ChiSquare()) == 0, 1e-5% tolerance());
237  else
238  BOOST_TEST(double(fitter.ChiSquare()) == double(chisq), 0.1% tolerance());
239  BOOST_TEST(fitter.NDF() == NDF);
240  }
241 
242 } // CheckGaussianFit<>()
243 
244 
245 
246 /**
247  * @brief Tests LinearFit object with a known input
248  */
249 template <typename T>
251 
252  using Data_t = T;
253 
254  using PerfectItem_t = std::pair<Data_t, Data_t>;
255  using UncertainItem_t = std::tuple<Data_t, Data_t, Data_t>;
256 
257  using PerfectData_t = std::vector<PerfectItem_t>;
258  using UncertainData_t = std::vector<UncertainItem_t>;
259 
260  // prepare input data
261  PerfectData_t perfect_data{
262  { Data_t(-4), Data_t( 8) },
263  { Data_t( 0), Data_t( 0) },
264  { Data_t( 4), Data_t(-8) }
265  };
266 
267  const int n = 3;
268  const Data_t intercept = Data_t( 0);
269  const Data_t slope = Data_t(-2);
270  const Data_t perf_chisq = Data_t( 0);
271  const Data_t perf_intercept_error = std::sqrt(Data_t(32)/Data_t(96));
272  const Data_t perf_slope_error = std::sqrt(Data_t( 3)/Data_t(96));
273  const Data_t perf_intercept_slope_cov = - Data_t(0)/Data_t(96);
274  const int perf_DoF = 1;
275 
276  UncertainData_t uncertain_data({
277  UncertainItem_t{ Data_t(-4), Data_t( 8), Data_t(1) },
278  UncertainItem_t{ Data_t( 0), Data_t( 0), Data_t(2) },
279  UncertainItem_t{ Data_t( 4), Data_t(-8), Data_t(2) }
280  });
281 
282  const Data_t unc_chisq = Data_t( 0);
283  const Data_t unc_intercept_error = std::sqrt(Data_t(20)/Data_t(21));
284  const Data_t unc_slope_error = std::sqrt(Data_t(1.5)/Data_t(21));
285  const Data_t unc_intercept_slope_cov = - Data_t(-3)/Data_t(21);
286  const int unc_DoF = 1;
287 
288  //
289  // part I: construction
290  //
292 
293  // check that everything is 0 or NaN-like
294  CheckLinearFit<Data_t>(fitter, 0, 0., 0., 0., 0., 0., 0., 0);
295 
296  //
297  // part II: add elements one by one
298  //
299  // the data is the same as uncertain_data, just inserted one by one
300  // and exercising both uncertain and certain addition;
301  // this part deliberately ignores directly interfaces adding pairs and tuples
302  for (auto const& data: uncertain_data) {
303  if (std::get<2>(data) == Data_t(1))
304  fitter.add(std::get<0>(data), std::get<1>(data));
305  else
306  fitter.add(std::get<0>(data), std::get<1>(data), std::get<2>(data));
307  } // for
308 
309  // by construction of the input, the statistics for X and Y are the same
310  CheckLinearFit<Data_t>(fitter, n,
311  intercept, slope,
312  unc_intercept_error, unc_slope_error, unc_intercept_slope_cov,
313  unc_chisq, unc_DoF
314  );
315 
316 
317  //
318  // part III: add elements without uncertainty by bulk
319  //
320 
321  // - III.1: clear the fitter
322  fitter.clear();
323  CheckLinearFit<Data_t>(fitter, 0, 0., 0., 0., 0., 0., 0., 0);
324 
325  // - III.2: fill by iterators
326  fitter.add_without_uncertainty
327  (std::begin(perfect_data), std::end(perfect_data));
328  CheckLinearFit<Data_t>(fitter, n,
329  intercept, slope,
330  perf_intercept_error, perf_slope_error, perf_intercept_slope_cov,
331  perf_chisq, perf_DoF
332  );
333 
334  // - III.3: fill by container
335  fitter.clear();
336  fitter.add_without_uncertainty(perfect_data);
337  CheckLinearFit<Data_t>(fitter, n,
338  intercept, slope,
339  perf_intercept_error, perf_slope_error, perf_intercept_slope_cov,
340  perf_chisq, perf_DoF
341  );
342 
343  // - III.4: fill by iterators and extractor
344  fitter.clear();
345  fitter.add_without_uncertainty(
346  uncertain_data.begin(), uncertain_data.end(),
347  [](UncertainItem_t const& d)
348  { return PerfectItem_t{ std::get<0>(d), std::get<1>(d) }; }
349  );
350  CheckLinearFit<Data_t>(fitter, n,
351  intercept, slope,
352  perf_intercept_error, perf_slope_error, perf_intercept_slope_cov,
353  perf_chisq, perf_DoF
354  );
355 
356  // - III.5: fill by container and extractor
357  fitter.clear();
358  fitter.add_without_uncertainty(uncertain_data,
359  [](UncertainItem_t const& d)
360  { return PerfectItem_t{ std::get<0>(d), std::get<1>(d) }; }
361  );
362  CheckLinearFit<Data_t>(fitter, n,
363  intercept, slope,
364  perf_intercept_error, perf_slope_error, perf_intercept_slope_cov,
365  perf_chisq, perf_DoF
366  );
367 
368 
369  //
370  // part IV: add elements with uncertainty by bulk
371  //
372 
373  // - IV.1: fill by iterators
374  fitter.clear();
375  fitter.add_with_uncertainty(uncertain_data.begin(), uncertain_data.end());
376  CheckLinearFit<Data_t>(fitter, n,
377  intercept, slope,
378  unc_intercept_error, unc_slope_error, unc_intercept_slope_cov,
379  unc_chisq, unc_DoF
380  );
381 
382  // - IV.2: fill by container
383  fitter.clear();
384  fitter.add_with_uncertainty(uncertain_data);
385  CheckLinearFit<Data_t>(fitter, n,
386  intercept, slope,
387  unc_intercept_error, unc_slope_error, unc_intercept_slope_cov,
388  unc_chisq, unc_DoF
389  );
390 
391 } // LinearFitTest()
392 
393 
394 /** ****************************************************************************
395  * @brief Tests QuadraticFit object with a known input
396  */
397 template <typename T>
399 
400  using Data_t = T;
401 
402  using PerfectItem_t = std::pair<Data_t, Data_t>;
403  using UncertainItem_t = std::tuple<Data_t, Data_t, Data_t>;
404 
405  using PerfectData_t = std::vector<PerfectItem_t>;
406  using UncertainData_t = std::vector<UncertainItem_t>;
407 
408  // prepare input data
409  PerfectData_t perfect_data{
410  { Data_t(-4), Data_t( 9) },
411  { Data_t( 0), Data_t(-1) },
412  { Data_t( 4), Data_t( 5) },
413  { Data_t( 6), Data_t(14) }
414  };
415 
416  const int n = 4;
417  // BUG the double brace syntax is required to work around clang bug 21629
418  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
419  const std::array<Data_t, 3> solution = {{ -1.0, -0.5, 0.5 }};
420  const std::array<Data_t, 3> perf_errors2 = {{ 149./199., 163./6368., 59./25472. }};
421  const Data_t perf_chisq = Data_t( 0);
422  const int perf_DoF = 1;
423 
424  UncertainData_t uncertain_data({
425  UncertainItem_t{ Data_t(-4), Data_t( 9), Data_t(2) },
426  UncertainItem_t{ Data_t( 0), Data_t(-1), Data_t(1) },
427  UncertainItem_t{ Data_t( 4), Data_t( 5), Data_t(1) },
428  UncertainItem_t{ Data_t( 6), Data_t(14), Data_t(2) }
429  });
430 
431  const Data_t unc_chisq = Data_t( 0);
432  // BUG the double brace syntax is required to work around clang bug 21629
433  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
434  const std::array<Data_t, 3> unc_errors2 = {{ 517./617., 769./9872., 209./39488. }};
435  const int unc_DoF = 1;
436 
437  //
438  // part I: construction
439  //
441 
442  std::array<Data_t, 3> empty_params;
443  empty_params.fill(0);
444 
445  // check that everything is 0 or NaN-like
446  CheckQuadraticFit<Data_t>(fitter, 0, empty_params, empty_params, 0., 0);
447 
448  //
449  // part II: add elements one by one
450  //
451  // the data is the same as uncertain_data, just inserted one by one
452  // and exercising both uncertain and certain addition;
453  // this part deliberately ignores directly interfaces adding pairs and tuples
454  for (auto const& data: uncertain_data) {
455  if (std::get<2>(data) == Data_t(1))
456  fitter.add(std::get<0>(data), std::get<1>(data));
457  else
458  fitter.add(std::get<0>(data), std::get<1>(data), std::get<2>(data));
459  } // for
460 
461  // by construction of the input, the statistics for X and Y are the same
462  CheckQuadraticFit<Data_t>
463  (fitter, n, solution, unc_errors2, unc_chisq, unc_DoF);
464 
465 
466  //
467  // part III: add elements without uncertainty by bulk
468  //
469 
470  // - III.1: clear the fitter
471  fitter.clear();
472  CheckQuadraticFit<Data_t>(fitter, 0, empty_params, empty_params, 0., 0);
473 
474  // - III.2: fill by iterators
475  fitter.add_without_uncertainty
476  (std::begin(perfect_data), std::end(perfect_data));
477  CheckQuadraticFit<Data_t>
478  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
479 
480  // - III.3: fill by container
481  fitter.clear();
482  fitter.add_without_uncertainty(perfect_data);
483  CheckQuadraticFit<Data_t>
484  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
485 
486  // - III.4: fill by iterators and extractor
487  fitter.clear();
488  fitter.add_without_uncertainty(
489  uncertain_data.begin(), uncertain_data.end(),
490  [](UncertainItem_t const& d)
491  { return PerfectItem_t{ std::get<0>(d), std::get<1>(d) }; }
492  );
493  CheckQuadraticFit<Data_t>
494  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
495 
496  // - III.5: fill by container and extractor
497  fitter.clear();
498  fitter.add_without_uncertainty(uncertain_data,
499  [](UncertainItem_t const& d)
500  { return PerfectItem_t{ std::get<0>(d), std::get<1>(d) }; }
501  );
502  CheckQuadraticFit<Data_t>
503  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
504 
505 
506  //
507  // part IV: add elements with uncertainty by bulk
508  //
509 
510  // - IV.1: fill by iterators
511  fitter.clear();
512  fitter.add_with_uncertainty(uncertain_data.begin(), uncertain_data.end());
513  CheckQuadraticFit<Data_t>
514  (fitter, n, solution, unc_errors2, unc_chisq, unc_DoF);
515 
516  // - IV.2: fill by container
517  fitter.clear();
518  fitter.add_with_uncertainty(uncertain_data);
519  CheckQuadraticFit<Data_t>
520  (fitter, n, solution, unc_errors2, unc_chisq, unc_DoF);
521 
522 } // QuadraticFitTest()
523 
524 
525 /** ****************************************************************************
526  * @brief Tests GausssianFit object with a known input
527  */
528 
529 template <typename T>
530 T gaus(T x, T amplitude, T mean, T sigma) {
531  const T z = (x - mean) / sigma;
532  return amplitude * std::exp(-0.5*z*z);
533 } // gaus()
534 
535 
536 template <typename T>
538 
539  using Data_t = T;
540 
541  using PerfectItem_t = std::pair<Data_t, Data_t>;
542  using UncertainItem_t = std::tuple<Data_t, Data_t, Data_t>;
543 
544  using PerfectData_t = std::vector<PerfectItem_t>;
545  using UncertainData_t = std::vector<UncertainItem_t>;
546 
547  // BUG the double brace syntax is required to work around clang bug 21629
548  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
549  const std::array<Data_t, 3> solution = {{ 5.0, 1.0, 2.0 }};
550 
551  // prepare input data
552  PerfectData_t perfect_data{
553  { Data_t(-1), gaus(Data_t(-1), solution[0], solution[1], solution[2]) },
554  { Data_t( 0), gaus(Data_t( 0), solution[0], solution[1], solution[2]) },
555  { Data_t(+1), gaus(Data_t(+1), solution[0], solution[1], solution[2]) },
556  { Data_t(+3), gaus(Data_t(+3), solution[0], solution[1], solution[2]) }
557  };
558 
559  const int n = 4;
560  // BUG the double brace syntax is required to work around clang bug 21629
561  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
562  const std::array<Data_t, 3> perf_errors2 = {{ 0., 0., 0. }};
563  const Data_t perf_chisq = Data_t( 0);
564  const int perf_DoF = 1;
565 
566  UncertainData_t uncertain_data({
567  UncertainItem_t{ Data_t(-1), gaus(Data_t(-1), solution[0], solution[1], solution[2]), Data_t(2) },
568  UncertainItem_t{ Data_t( 0), gaus(Data_t( 0), solution[0], solution[1], solution[2]), Data_t(1) },
569  UncertainItem_t{ Data_t(+1), gaus(Data_t(+1), solution[0], solution[1], solution[2]), Data_t(1) },
570  UncertainItem_t{ Data_t(+3), gaus(Data_t(+3), solution[0], solution[1], solution[2]), Data_t(2) }
571  });
572 
573  const Data_t unc_chisq = Data_t( 0);
574  // BUG the double brace syntax is required to work around clang bug 21629
575  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
576  const std::array<Data_t, 3> unc_errors2 = {{ 0., 0., 0. }};
577  const int unc_DoF = 1;
578 
579  //
580  // part I: construction
581  //
583 
584  std::array<Data_t, 3> empty_params;
585  empty_params.fill(0);
586 
587  // check that everything is 0 or NaN-like
588  CheckGaussianFit<Data_t>(fitter, 0, empty_params, empty_params, 0., 0);
589 
590  //
591  // part II: add elements one by one
592  //
593  // the data is the same as uncertain_data, just inserted one by one
594  // and exercising both uncertain and certain addition;
595  // this part deliberately ignores directly interfaces adding pairs and tuples
596  for (auto const& data: uncertain_data) {
597  if (std::get<2>(data) == Data_t(1))
598  fitter.add(std::get<0>(data), std::get<1>(data));
599  else
600  fitter.add(std::get<0>(data), std::get<1>(data), std::get<2>(data));
601  } // for
602 
603  // by construction of the input, the statistics for X and Y are the same
604  CheckGaussianFit<Data_t>
605  (fitter, n, solution, unc_errors2, unc_chisq, unc_DoF);
606 
607 
608  //
609  // part III: add elements without uncertainty by bulk
610  //
611 
612  // - III.1: clear the fitter
613  fitter.clear();
614  CheckGaussianFit<Data_t>(fitter, 0, empty_params, empty_params, 0., 0);
615 
616  // - III.2: fill by iterators
617  fitter.add_without_uncertainty
618  (std::begin(perfect_data), std::end(perfect_data));
619  CheckGaussianFit<Data_t>
620  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
621 
622  // - III.3: fill by container
623  fitter.clear();
624  fitter.add_without_uncertainty(perfect_data);
625  CheckGaussianFit<Data_t>
626  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
627 
628  // - III.4: fill by iterators and extractor
629  fitter.clear();
630  fitter.add_without_uncertainty(
631  uncertain_data.begin(), uncertain_data.end(),
632  [](UncertainItem_t const& d)
633  { return PerfectItem_t{ std::get<0>(d), std::get<1>(d) }; }
634  );
635  CheckGaussianFit<Data_t>
636  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
637 
638  // - III.5: fill by container and extractor
639  fitter.clear();
640  fitter.add_without_uncertainty(uncertain_data,
641  [](UncertainItem_t const& d)
642  { return PerfectItem_t{ std::get<0>(d), std::get<1>(d) }; }
643  );
644  CheckGaussianFit<Data_t>
645  (fitter, n, solution, perf_errors2, perf_chisq, perf_DoF);
646 
647 
648  //
649  // part IV: add elements with uncertainty by bulk
650  //
651 
652  // - IV.1: fill by iterators
653  fitter.clear();
654  fitter.add_with_uncertainty(uncertain_data.begin(), uncertain_data.end());
655  CheckGaussianFit<Data_t>
656  (fitter, n, solution, unc_errors2, unc_chisq, unc_DoF);
657 
658  // - IV.2: fill by container
659  fitter.clear();
660  fitter.add_with_uncertainty(uncertain_data);
661  CheckGaussianFit<Data_t>
662  (fitter, n, solution, unc_errors2, unc_chisq, unc_DoF);
663 
664 } // QuadraticFitTest()
665 
666 
667 //------------------------------------------------------------------------------
668 //--- registration of tests
669 //
670 // Boost needs now to know which tests we want to run.
671 // Tests are "automatically" registered, hence the BOOST_AUTO_TEST_CASE()
672 // macro name. The argument is a name for the test; each test will have a
673 // number of checks and it will fail if any of them does.
674 //
675 
676 //
677 // LinearFit tests
678 //
679 BOOST_AUTO_TEST_CASE(LinearFitRealTest) {
680  LinearFitTest<double>();
681 }
682 
683 //
684 // QuadraticFit tests
685 //
686 BOOST_AUTO_TEST_CASE(QuadraticFitRealTest) {
687  QuadraticFitTest<double>();
688 }
689 
690 //
691 // GaussianFit tests
692 //
693 BOOST_AUTO_TEST_CASE(GaussianFitRealTest) {
694  GaussianFitTest<double>();
695 }
static QCString name
Definition: declinfo.cpp:673
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void LinearFitTest()
Tests LinearFit object with a known input.
BOOST_AUTO_TEST_CASE(LinearFitRealTest)
Data_t InterceptError() const
Returns the error on intercept of the fit.
Definition: SimpleFits.h:891
std::string string
Definition: nybbler.cc:12
virtual FitParameters_t FitParameterErrors() const override
Computes and returns all the parameter errors of the fit result.
Definition: SimpleFits.h:1847
auto const tolerance
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
Definition: SimpleFits.h:731
void QuadraticFitTest()
Tests QuadraticFit object with a known input.
virtual Data_t ChiSquare() const override
Returns the of the fit.
Definition: SimpleFits.h:1771
virtual FitParameters_t FitParameters() const override
Computes and returns all the parameters of the fit result.
Definition: SimpleFits.h:1841
int N() const
Returns the number of (valid) points added.
Definition: SimpleFits.h:1100
Classes performing simple fits.
Performs a linear regression of data.
Definition: SimpleFits.h:849
void CheckGaussianFit(lar::util::GaussianFit< T > const &fitter, int n, std::array< T, 3 > const &solution, std::array< T, 3 > const &error2, T chisq, int NDF)
Data_t Slope() const
Returns the slope of the fit.
Definition: SimpleFits.h:884
virtual Data_t ChiSquare() const override
Returns the of the fit.
Definition: SimpleFits.h:1756
const double e
#define Array
Definition: scanner.cpp:11549
Data_t InterceptSlopeCovariance() const
Returns the covariance between intercept and slope of the fit.
Definition: SimpleFits.h:905
T gaus(T x, T amplitude, T mean, T sigma)
Tests GausssianFit object with a known input.
Data_t Intercept() const
Returns the intercept of the fit.
Definition: SimpleFits.h:877
void PrintMatrix(std::ostream &out, Array const &m, std::string name="Matrix")
std::void_t< T > n
"Fast" Gaussian fit
Definition: SimpleFits.h:1018
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
Definition: SimpleFits.h:1170
Data_t SlopeError() const
Returns the error in slope of the fit.
Definition: SimpleFits.h:898
virtual Data_t FitParameterError(unsigned int n) const override
Returns the error on parameter n of the fit result.
Performs a second-degree fit of data.
Definition: SimpleFits.h:953
virtual Fitter_t const & Fitter() const
Returns the internal fitter (mostly for debugging)
Definition: SimpleFits.h:1230
void CheckLinearFit(lar::util::LinearFit< T > const &fitter, int n, T intercept, T slope, T intercept_error, T slope_error, T intercept_slope_covariance, T chisq, int NDF)
void CheckQuadraticFit(lar::util::QuadraticFit< T > const &fitter, int n, std::array< T, 3 > const &solution, std::array< T, 3 > const &error2, T chisq, int NDF)
void GaussianFitTest()
T copy(T const &v)
virtual bool isValid() const override
Returns if the fit has valid results.
list x
Definition: train.py:276
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
virtual Data_t ChiSquare() const override
Returns the of the original fit.
Definition: SimpleFits.h:1161
typename Fitter_t::FitParameters_t FitParameters_t
Definition: SimpleFits.h:1035
void PrintVector(std::ostream &out, Array const &v, std::string name="Vector")
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Definition: SimpleFits.h:366
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
virtual Data_t FitParameter(unsigned int n) const override
Returns the parameter n of the fit result.
QTextStream & endl(QTextStream &s)
void PrintFitterInfo(Fitter const &fitter)
virtual bool isValid() const override
Returns if the fit has valid results.
Definition: SimpleFits.h:1122