Macros | Functions
SimpleFits_test.cc File Reference

Tests the classes in SimpleFits.h. More...

#include <cmath>
#include <tuple>
#include <array>
#include <stdexcept>
#include <iterator>
#include <iostream>
#include "boost/test/unit_test.hpp"
#include "lardata/Utilities/SimpleFits.h"

Go to the source code of this file.

Macros

#define SIMPLEFITS_TEST_DEBUG
 
#define BOOST_TEST_MODULE   ( SimpleFits_test )
 

Functions

template<typename Array >
void PrintMatrix (std::ostream &out, Array const &m, std::string name="Matrix")
 
template<typename Array >
void PrintVector (std::ostream &out, Array const &v, std::string name="Vector")
 
template<typename Fitter >
void PrintFitterInfo (Fitter const &fitter)
 
template<typename T >
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)
 
template<typename T >
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)
 
template<typename T >
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)
 
template<typename T >
void LinearFitTest ()
 Tests LinearFit object with a known input. More...
 
template<typename T >
void QuadraticFitTest ()
 Tests QuadraticFit object with a known input. More...
 
template<typename T >
gaus (T x, T amplitude, T mean, T sigma)
 Tests GausssianFit object with a known input. More...
 
template<typename T >
void GaussianFitTest ()
 
 BOOST_AUTO_TEST_CASE (LinearFitRealTest)
 
 BOOST_AUTO_TEST_CASE (QuadraticFitRealTest)
 
 BOOST_AUTO_TEST_CASE (GaussianFitRealTest)
 

Detailed Description

Tests the classes in SimpleFits.h.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
20141229
Version
1.0
See also
SimpleFits.h

See http://www.boost.org/libs/test for the Boost test library home page.

Timing: not given yet

Definition in file SimpleFits_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( SimpleFits_test )

Definition at line 36 of file SimpleFits_test.cc.

#define SIMPLEFITS_TEST_DEBUG

Definition at line 16 of file SimpleFits_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( LinearFitRealTest  )

Definition at line 679 of file SimpleFits_test.cc.

679  {
680  LinearFitTest<double>();
681 }
BOOST_AUTO_TEST_CASE ( QuadraticFitRealTest  )

Definition at line 686 of file SimpleFits_test.cc.

686  {
687  QuadraticFitTest<double>();
688 }
BOOST_AUTO_TEST_CASE ( GaussianFitRealTest  )

Definition at line 693 of file SimpleFits_test.cc.

693  {
694  GaussianFitTest<double>();
695 }
template<typename T >
void CheckGaussianFit ( lar::util::GaussianFit< T > const &  fitter,
int  n,
std::array< T, 3 > const &  solution,
std::array< T, 3 > const &  error2,
chisq,
int  NDF 
)

Definition at line 198 of file SimpleFits_test.cc.

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<>()
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 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
const double e
std::void_t< T > n
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
Definition: SimpleFits.h:1170
virtual Fitter_t const & Fitter() const
Returns the internal fitter (mostly for debugging)
Definition: SimpleFits.h:1230
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 PrintFitterInfo(Fitter const &fitter)
virtual bool isValid() const override
Returns if the fit has valid results.
Definition: SimpleFits.h:1122
template<typename T >
void CheckLinearFit ( lar::util::LinearFit< T > const &  fitter,
int  n,
intercept,
slope,
intercept_error,
slope_error,
intercept_slope_covariance,
chisq,
int  NDF 
)

Definition at line 105 of file SimpleFits_test.cc.

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<>()
Data_t InterceptError() const
Returns the error on intercept of the fit.
Definition: SimpleFits.h:891
auto const tolerance
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
Definition: SimpleFits.h:731
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
Data_t InterceptSlopeCovariance() const
Returns the covariance between intercept and slope of the fit.
Definition: SimpleFits.h:905
Data_t Intercept() const
Returns the intercept of the fit.
Definition: SimpleFits.h:877
std::void_t< T > n
Data_t SlopeError() const
Returns the error in slope of the fit.
Definition: SimpleFits.h:898
virtual bool isValid() const override
Returns if the fit has valid results.
void PrintFitterInfo(Fitter const &fitter)
template<typename T >
void CheckQuadraticFit ( lar::util::QuadraticFit< T > const &  fitter,
int  n,
std::array< T, 3 > const &  solution,
std::array< T, 3 > const &  error2,
chisq,
int  NDF 
)

Definition at line 151 of file SimpleFits_test.cc.

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<>()
auto const tolerance
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
Definition: SimpleFits.h:731
virtual Data_t ChiSquare() const override
Returns the of the fit.
Definition: SimpleFits.h:1771
const double e
std::void_t< T > n
virtual Data_t FitParameterError(unsigned int n) const override
Returns the error on parameter n of the fit result.
virtual bool isValid() const override
Returns if the fit has valid results.
virtual Data_t FitParameter(unsigned int n) const override
Returns the parameter n of the fit result.
void PrintFitterInfo(Fitter const &fitter)
template<typename T >
T gaus ( x,
amplitude,
mean,
sigma 
)

Tests GausssianFit object with a known input.


Definition at line 530 of file SimpleFits_test.cc.

530  {
531  const T z = (x - mean) / sigma;
532  return amplitude * std::exp(-0.5*z*z);
533 } // gaus()
list x
Definition: train.py:276
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
template<typename T >
void GaussianFitTest ( )

Definition at line 537 of file SimpleFits_test.cc.

537  {
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()
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
T gaus(T x, T amplitude, T mean, T sigma)
Tests GausssianFit object with a known input.
std::void_t< T > n
"Fast" Gaussian fit
Definition: SimpleFits.h:1018
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
template<typename T >
void LinearFitTest ( )

Tests LinearFit object with a known input.

Definition at line 250 of file SimpleFits_test.cc.

250  {
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()
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
Performs a linear regression of data.
Definition: SimpleFits.h:849
std::void_t< T > n
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Definition: SimpleFits.h:366
template<typename Fitter >
void PrintFitterInfo ( Fitter const &  fitter)

Definition at line 85 of file SimpleFits_test.cc.

85  {
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()
void PrintMatrix(std::ostream &out, Array const &m, std::string name="Matrix")
void PrintVector(std::ostream &out, Array const &v, std::string name="Vector")
QTextStream & endl(QTextStream &s)
template<typename Array >
void PrintMatrix ( std::ostream &  out,
Array const &  m,
std::string  name = "Matrix" 
)

Definition at line 56 of file SimpleFits_test.cc.

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()
static QCString name
Definition: declinfo.cpp:673
QTextStream & endl(QTextStream &s)
template<typename Array >
void PrintVector ( std::ostream &  out,
Array const &  v,
std::string  name = "Vector" 
)

Definition at line 73 of file SimpleFits_test.cc.

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()
static QCString name
Definition: declinfo.cpp:673
T copy(T const &v)
QTextStream & endl(QTextStream &s)
template<typename T >
void QuadraticFitTest ( )

Tests QuadraticFit object with a known input.


Definition at line 398 of file SimpleFits_test.cc.

398  {
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()
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::void_t< T > n
Performs a second-degree fit of data.
Definition: SimpleFits.h:953
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72