14 #define SIMPLEFITS_H 1 22 #include <type_traits> 36 template <
typename T,
unsigned int D>
56 template <
unsigned int Power,
unsigned int N>
60 {
return sums.template SumN<N>(); }
64 template <
unsigned int Power>
73 static constexpr
unsigned int Degree = D;
74 static constexpr
unsigned int NParams = Degree + 1;
109 {
return add(std::get<0>(value), std::get<1>(value), sy); }
121 add(std::get<0>(value), std::get<1>(value), std::get<2>(value));
134 template <
typename Iter>
151 template <
typename Iter,
typename Pred>
168 template <
typename Cont,
typename Pred>
182 template <
typename Cont>
214 typename VIter,
typename UIter,
215 typename VPred,
typename UPred =
identity 218 VIter begin_value, VIter end_value,
219 UIter begin_uncertainty,
220 VPred value_extractor,
221 UPred uncertainty_extractor = UPred()
239 template <
typename Iter>
253 template <
typename Cont>
266 int N()
const {
return s2.
N(); }
282 template <
typename V>
283 static constexpr V
sqr(V
const& v) {
return v*v; }
288 {
return (n == 0)?
s2.
Weights(): x.Sum(n); }
292 {
return (n == 0)? y.Weights():
xy.
Sum(n); }
296 template <
unsigned int N>
301 template <
unsigned int N>
313 template <
typename Stream>
314 void Print(Stream& out)
const;
322 {
return Data_t(1.)/std::sqrt(w); }
336 template <
typename T,
unsigned int D>
337 inline std::ostream&
operator<<
339 { stats.Print(out);
return out; }
343 template <
typename T,
unsigned int D>
349 static constexpr
unsigned int Degree = Collector_t::Degree;
367 {
return stats.add(x, y, sy); }
370 {
return stats.add(value, sy); }
373 {
return stats.add(value); }
376 template <
typename Iter>
378 { stats.add_without_uncertainty(begin, end); }
380 template <
typename Iter,
typename Pred>
382 { stats.add_without_uncertainty(begin, end, extractor); }
384 template <
typename Cont,
typename Pred>
386 { stats.add_without_uncertainty(cont, extractor); }
388 template <
typename Cont>
390 { stats.add_without_uncertainty(cont); }
394 typename VIter,
typename UIter,
395 typename VPred,
typename UPred =
identity 398 VIter begin_value, VIter end_value,
399 UIter begin_uncertainty,
400 VPred value_extractor,
401 UPred uncertainty_extractor = UPred()
404 return stats.add_with_uncertainty(
405 begin_value, end_value, begin_uncertainty,
406 value_extractor, uncertainty_extractor);
410 template <
typename Iter>
412 {
return stats.add_with_uncertainty(begin, end); }
414 template <
typename Cont>
416 {
return stats.add_with_uncertainty(cont); }
427 int N()
const {
return stats.N(); }
435 template <
typename Stream>
440 template <
typename V>
457 template <
typename T,
unsigned int N>
462 static constexpr
unsigned int NParams =
N;
489 virtual bool isValid()
const = 0;
516 virtual FitMatrix_t FitParameterCovariance()
const = 0;
525 {
return FitParameters()[
n]; }
534 {
return std::sqrt(FitParameterCovariance()[n * (NParams + 1)]); }
541 virtual Data_t ChiSquare()
const = 0;
550 virtual int NDF()
const = 0;
563 virtual bool FillResults(
577 virtual bool FillResults(
588 virtual bool FillResults
616 {
return MatrixOps::Determinant(mat); }
621 {
return MatrixOps::InvertSymmetricMatrix(mat, det); }
625 {
return MatrixOps::InvertSymmetricMatrix(mat); }
630 {
return MatrixOps::MatrixVectorProduct(mat, vec); }
636 template <
typename T,
unsigned int D>
648 static constexpr
unsigned int Degree = Base_t::Degree;
651 static constexpr
unsigned int NParams = Interface_t::NParams;
672 virtual bool isValid()
const override;
699 virtual FitMatrix_t FitParameterCovariance()
const override;
707 virtual Data_t FitParameter(
unsigned int n)
const override;
715 virtual Data_t FitParameterError(
unsigned int n)
const override;
722 virtual Data_t ChiSquare()
const override;
731 virtual int NDF()
const override {
return Base_t::N() -
NParams; }
799 {
return ExtractParameterErrors(Smat); }
818 using Interface_t::Determinant;
819 using Interface_t::InvertMatrix;
820 using Interface_t::MatrixProduct;
848 template <
typename T>
853 using Base_t::Degree;
854 using Base_t::NParams;
906 {
return this->FitParameterCovariance()[0 * NParams + 1]; }
913 virtual Data_t ChiSquare()
const override;
919 Data_t I()
const {
return Base_t::stats.template XN<0>(); }
921 Data_t X()
const {
return Base_t::stats.template XN<1>(); }
922 Data_t X2()
const {
return Base_t::stats.template XN<2>(); }
923 Data_t Y()
const {
return Base_t::stats.template XNY<0>(); }
924 Data_t XY()
const {
return Base_t::stats.template XNY<1>(); }
952 template <
typename T>
957 using Base_t::Degree;
958 using Base_t::NParams;
980 virtual Data_t ChiSquare()
const override;
986 Data_t I()
const {
return Base_t::stats.template XN<0>(); }
988 Data_t X()
const {
return Base_t::stats.template XN<1>(); }
989 Data_t X2()
const {
return Base_t::stats.template XN<2>(); }
990 Data_t X3()
const {
return Base_t::stats.template XN<3>(); }
991 Data_t X4()
const {
return Base_t::stats.template XN<4>(); }
992 Data_t Y()
const {
return Base_t::stats.template XNY<0>(); }
993 Data_t XY()
const {
return Base_t::stats.template XNY<1>(); }
994 Data_t X2Y()
const {
return Base_t::stats.template XNY<2>(); }
1017 template <
typename T>
1024 static constexpr
unsigned int NParams = Base_t::NParams;
1051 {
return add(std::get<0>(value), std::get<1>(value), sy); }
1056 add(std::get<0>(value), std::get<1>(value), std::get<2>(value));
1060 template <
typename Iter>
1064 template <
typename Iter,
typename Pred>
1067 template <
typename Cont,
typename Pred>
1071 template <
typename Cont>
1077 typename VIter,
typename UIter,
1078 typename VPred,
typename UPred =
identity 1081 VIter begin_value, VIter end_value,
1082 UIter begin_uncertainty,
1083 VPred value_extractor,
1084 UPred uncertainty_extractor = UPred()
1088 template <
typename Iter>
1091 template <
typename Cont>
1100 int N()
const {
return fitter.N(); }
1104 template <
typename Stream>
1122 virtual bool isValid()
const override {
return fitter.isValid(); }
1150 virtual FitMatrix_t FitParameterCovariance()
const override;
1170 virtual int NDF()
const override {
return fitter.NDF(); }
1186 virtual bool FillResults(
1201 virtual bool FillResults(
1214 virtual bool FillResults
1226 {
return Evaluate(x, FitParameters().data()); }
1248 struct Value_t:
public std::tuple<Data_t, Data_t> {
1273 {
return { std::log(value), error / std::abs(value) }; }
1278 {
return EncodeValue(value.
value(), value.
error()); }
1285 return { v, v * value.
error() };
1292 Measurement_t(std::get<0>(meas), EncodeValue(std::get<1>(meas)));
1300 return { std::get<0>(meas), value.
value(), value.
error() };
1308 return { std::get<0>(meas), value.
value(), value.
error() };
1314 template <
typename VPred,
typename UPred =
void>
1317 value_extractor(vpred), error_extractor(upred) {}
1322 template <
typename Elem>
1323 auto operator() (Elem elem)
1327 static_cast<Measurement_t&&>(value_extractor(elem)),
1328 static_cast<Data_t&&>(error_extractor(elem))
1338 template <
typename Pred>
1350 auto operator() (Elem elem)
const 1351 {
return EncodeValue(extractor(elem)); }
1357 auto operator() (Elem elem)
1358 {
return EncodeValue(extractor(elem)); }
1363 template <
typename Pred>
1366 template <
typename VPred,
typename UPred>
1368 {
return { vpred, upred }; }
1386 static void ConvertParametersAndErrors(
1398 static void ConvertParametersAndVariances(
1410 static void ConvertParametersAndErrorMatrix(
1425 static void ThrowNotImplemented [[noreturn]] (std::string method)
1426 {
throw std::logic_error(
"Method " + method +
"() not implemented"); }
1444 template <
typename T,
unsigned int D>
1449 if (!std::isnormal(w))
return false;
1458 xy.
add(x_value, yw);
1464 template <
typename T,
unsigned int D>
1465 template <
typename Iter,
typename Pred>
1470 (begin, end, [
this, extractor](
auto item) { this->
add(extractor(item)); });
1474 template <
typename T,
unsigned int D>
1475 template <
typename VIter,
typename UIter,
typename VPred,
typename UPred>
1478 VIter begin_value, VIter end_value,
1479 UIter begin_uncertainty,
1480 VPred value_extractor,
1481 UPred uncertainty_extractor
1484 while (begin_value != end_value) {
1486 (value_extractor(*begin_value), uncertainty_extractor(*begin_uncertainty))
1490 ++begin_uncertainty;
1496 template <
typename T,
unsigned int D>
1497 template <
typename Iter>
1499 (Iter begin, Iter end)
1501 unsigned int old_n =
N();
1502 std::for_each(begin, end, [
this](
auto p) { this->
add(p); });
1508 template <
typename T,
unsigned int D>
1518 template <
typename T,
unsigned int D>
template <
typename Stream>
1522 <<
"\n x/s^2=" << x.template SumN<1>();
1523 for (
unsigned int degree = 2; degree <= x.Power; ++degree)
1524 out <<
"\n x^" << degree <<
"/s^2=" << x.Sum(degree);
1526 <<
"\n y/s^2=" << y.Weights()
1527 <<
"\n y^2/s^2=" <<
y2.
Sum();
1529 out <<
"\n xy/s^2=" <<
xy.template SumN<1>();
1530 for (
unsigned int degree = 2; degree <=
xy.
Power; ++degree)
1531 out <<
"\n x^" << degree <<
"y/s^2=" <<
xy.
Sum(degree);
1539 template <
typename T,
unsigned int D>
1541 return (Base_t::N() > (
int) Degree)
1542 && std::isnormal(Determinant(MakeMatrixX()));
1546 template <
typename T,
unsigned int D>
1550 return Param(n, MakeMatrixX());
1554 template <
typename T,
unsigned int D>
1558 if (n > Degree)
return Data_t(0);
1559 return std::sqrt(FitParameterCovariance()[n * (NParams + 1)]);
1563 template <
typename T,
unsigned int D>
1569 for (
unsigned int iParam = 0; iParam <
NParams; ++iParam)
1570 fit_params[iParam] = Param(iParam, Xmat);
1575 template <
typename T,
unsigned int D>
1579 return FitParameterErrors(FitParameterCovariance());
1583 template <
typename T,
unsigned int D>
1588 Data_t det = Determinant(Xmat);
1589 if (!std::isnormal(det)) {
1590 throw std::range_error
1591 (
"SimplePolyFitterBase::FitParameterCovariance(): determinant 0 while fitting");
1593 return InvertMatrix(Xmat, det);
1597 template <
typename T,
unsigned int D>
1603 Xmat = MakeMatrixX();
1604 det = Determinant(Xmat);
1605 if (!std::isnormal(det)) {
1610 Smat = InvertMatrix(Xmat, det);
1611 params = FitParameters(Smat, det);
1616 template <
typename T,
unsigned int D>
1622 if (!this->FillResults(params, Xmat, det, Smat))
return false;
1623 paramerrors = ExtractParameterErrors(Smat);
1628 template <
typename T,
unsigned int D>
1636 return FillResults(params, paramerrors, Xmat, det, Smat);
1640 template <
typename T,
unsigned int D>
1645 unsigned int iParam = NParams - 1;
1646 Data_t v = params[iParam];
1647 while (iParam > 0) v = v * x + params[--iParam];
1653 template <
typename T,
unsigned int D>
1658 for (
unsigned int i = 0; i <
NParams; ++i) {
1659 for (
unsigned int j = i; j <
NParams; ++j) {
1660 Xmat[j * NParams + i] = Xmat[i * NParams + j] = Base_t::XN(i+j);
1667 template <
typename T,
unsigned int D>
1672 for (
unsigned int i = 0; i <
NParams; ++i) Ymat[i] = Base_t::XNY(i);
1677 template <
typename T,
unsigned int D>
1683 for (
unsigned int iParam = 0; iParam <
NParams; ++iParam)
1684 fit_params[iParam] = Param(iParam, Xmat);
1689 template <
typename T,
unsigned int D>
1694 return MatrixProduct(Smat, MakeMatrixY());
1698 template <
typename T,
unsigned int D>
1702 if (n > Degree)
return Data_t(0);
1704 Data_t detXmat = Determinant(Xmat);
1705 if (!std::isnormal(detXmat)) {
1706 throw std::range_error
1707 (
"SimplePolyFitterBase::Param(): Determinant 0 while fitting");
1709 return Param(n, Xmat, detXmat);
1713 template <
typename T,
unsigned int D>
1719 for (
unsigned int iParam = 0; iParam <=
Degree; ++iParam)
1720 fit_errors[iParam] = std::sqrt(Smat[iParam * (NParams + 1)]);
1725 template <
typename T,
unsigned int D>
1729 if (n > Degree)
return Data_t(0);
1733 for (
unsigned int i = 0; i <
NParams; ++i)
1734 XYmat[i * NParams + n] = Base_t::XNY(i);
1736 return Determinant(XYmat) / detXmat;
1740 template <
typename T,
unsigned int D>
1745 throw std::logic_error
1746 (
"SimplePolyFitterBase::ChiSquare() not implemented for generic fit");
1755 template <
typename T>
1759 const Data_t b = fit_params[0];
1760 const Data_t a = fit_params[1];
1762 +
Data_t(2) * (a * b *
X2() - a * XY() - b *
Y());
1770 template <
typename T>
1774 return Y2() -
Data_t(2) * (a[0]*
Y() + a[1]*XY() + a[2]*X2Y())
1775 +
sqr(a[0])*I() +
Data_t(2) * a[0] * ( a[1]*
X() + a[2]*
X2() )
1776 +
sqr(a[1])*
X2() +
Data_t(2) * a[1] * ( a[2]*X3() )
1788 template <
typename T>
1792 if (y <=
Data_t(0))
return false;
1794 return fitter.add(x, value.
value(), value.
error());
1798 template <
typename T>
1799 template <
typename Iter,
typename Pred>
1801 (Iter begin, Iter end, Pred extractor)
1803 return fitter.add_without_uncertainty(begin, end, Encoder(extractor));
1807 template <
typename T>
1809 typename VIter,
typename UIter,
1810 typename VPred,
typename UPred
1813 VIter begin_value, VIter end_value,
1814 UIter begin_uncertainty,
1815 VPred value_extractor,
1816 UPred uncertainty_extractor
1820 begin_value, end_value, begin_uncertainty,
1821 Encoder(value_extractor, uncertainty_extractor)
1826 template <
typename T>
1827 template <
typename Iter>
1829 (Iter begin, Iter end)
1831 unsigned int old_n =
N();
1832 std::for_each(begin, end, [
this](
auto p) { this->
add(p); });
1840 template <
typename T>
1842 return ConvertParameters(fitter.FitParameters());
1846 template <
typename T>
1849 if (!FillResults(qpars, qparerrors)) {
1850 throw std::runtime_error
1851 (
"GaussianFit::FitParameterErrors() yielded invalid results");
1857 template <
typename T>
1865 if (!FillResults(params, Xmat, det, Smat)) {
1866 throw std::runtime_error
1867 (
"GaussianFit::FitParameterCovariance() yielded invalid results");
1873 template <
typename T>
1881 if (!fitter.FillResults(qpars, Xmat, det, qparerrmat))
return false;
1882 ConvertParametersAndErrors(qpars, qparerrmat, params, paramerrors);
1883 return isValid(params, qpars);
1887 template <
typename T>
1893 if (!fitter.FillResults(qpars, Xmat, det, qparerrmat))
return false;
1894 ConvertParametersAndErrorMatrix(qpars, qparerrmat, params, Smat);
1895 return isValid(params, qpars);
1899 template <
typename T>
1904 if (!FillResults(params, Xmat, det, Smat))
return false;
1905 paramerrors = fitter.ExtractParameterErrors(Smat);
1910 template <
typename T>
1914 Data_t z = (x - params[1]) / params[2];
1915 return params[0] * std::exp(-0.5*
sqr(z));
1919 template <
typename T>
1926 Data_t sigma2 = -0.5 / qpars[2];
1927 params[2] = std::sqrt(sigma2);
1929 params[1] = sigma2 * qpars[1];
1931 params[0] = std::exp(qpars[0] - 0.25 *
sqr(qpars[1])/qpars[2]);
1937 template <
typename T>
1943 params = ConvertParameters(qpars);
1946 Data_t const& A = params[0];
1947 Data_t const& mu = params[1];
1948 Data_t const& sigma = params[2];
1951 paramvariances[2] = qparerrmat[3 * 2 + 2] /
sqr(cube(sigma));
1954 paramvariances[1] =
sqr(mu * (
1955 + qparerrmat[3 * 1 + 1] /
sqr(a[1])
1956 - 2.*qparerrmat[3 * 2 + 1] / (a[1]*a[2])
1957 + qparerrmat[3 * 2 + 2] /
sqr(a[2])
1961 paramvariances[0] =
sqr(A * (
1962 + qparerrmat[3 * 0 + 0]
1963 + 2.*qparerrmat[3 * 0 + 1] * mu
1964 +( qparerrmat[3 * 1 + 1]
1965 + 2.*qparerrmat[3 * 0 + 2]) *
sqr(mu)
1966 + 2.*qparerrmat[3 * 1 + 2] * cube(mu)
1967 + qparerrmat[3 * 2 + 2] *
sqr(
sqr(mu))
1973 template <
typename T>
1979 ConvertParametersAndVariances(qpars, qparerrmat, params, paramerrors);
1981 for (
Data_t& paramerror: paramerrors) paramerror = std::sqrt(paramerror);
1985 template <
typename T>
1992 ConvertParametersAndVariances(qpars, qparerrmat, params, paramvariances);
1996 Data_t const& A = params[0];
1997 Data_t const& mu = params[1];
1998 Data_t const& sigma = params[2];
2001 Smat[3 * 2 + 2] = paramvariances[2];
2004 Smat[3 * 1 + 1] = paramvariances[1];
2007 Smat[3 * 0 + 0] = paramvariances[0];
2010 Smat[3 * 1 + 2] = Smat[3 * 2 + 1]
2011 = (qparerrmat[3 * 1 + 2] + 2 * mu * qparerrmat[3 * 2 + 2]) / sigma;
2015 const Data_t dA_dak_cov_aka2 = A * (
2016 qparerrmat[3 * 0 + 2]
2017 + qparerrmat[3 * 1 + 2] * mu
2018 + qparerrmat[3 * 2 + 2] *
sqr(mu)
2021 Smat[3 * 0 + 2] = Smat[3 * 2 + 0]
2022 = dA_dak_cov_aka2 / cube(sigma);
2025 const Data_t dA_dak_cov_aka1 = A * (
2026 qparerrmat[3 * 0 + 1]
2027 + qparerrmat[3 * 1 + 1] * mu
2028 + qparerrmat[3 * 2 + 1] *
sqr(mu)
2032 Smat[3 * 0 + 1] = Smat[3 * 1 + 0] = mu *
2033 (dA_dak_cov_aka1 / a[1] - dA_dak_cov_aka2 / a[2]);
2038 template <
typename T>
2042 return (qpars[2] <
Data_t(0)) && (params[0] >=
Data_t(0));
2049 #endif // SIMPLEFITS_H static constexpr Data_t cube(Data_t v)
Returns the cube of the specified data value.
Weight_t Weights() const
Returns the sum of the weights.
void add(Data_t v, Weight_t w)
Adds the specified weight to the statistics.
void add_without_uncertainty(Iter begin, Iter end)
Adds measurements from a sequence, with no uncertainty.
static constexpr unsigned int NParams
virtual Data_t Evaluate(Data_t x) const override
Evaluates the fitted function at the specified point.
Data_t X4() const
Aliases.
bool add(MeasurementAndUncertainty_t value)
int N() const
Returns the number of entries added.
Data_t InterceptError() const
Returns the error on intercept of the fit.
Namespace for general, non-LArSoft-specific utilities.
unsigned int add_with_uncertainty(Iter begin, Iter end)
unsigned int add_with_uncertainty(VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
Adds measurements with uncertainties from a sequence.
void add_without_uncertainty(Iter begin, Iter end)
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Clears all the input statistics.
typename Base_t::Data_t Data_t
type of the data
unsigned int add_with_uncertainty(VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
Data_t XN(unsigned int n) const
Returns the weighted sum of x^n.
constexpr Data_t error() const
virtual Data_t FitParameterError(unsigned int n) const
Returns the error on parameter n of the fit result.
void add_without_uncertainty(Cont cont)
Data_t XN(unsigned int n) const
Returns the weighted sum of x^n.
virtual FitParameters_t FitParameterErrors() const override
Computes and returns all the parameter errors of the fit result.
Data_t XN() const
Returns the weighted sum of x^N.
typename Collector_t::Data_t Data_t
type of the data
Weight_t AverageWeight() const
Returns the arithmetic average of the weights.
unsigned int add_with_uncertainty(Cont cont)
Clears all the input statistics.
std::array< Data_t, sqr(NParams)> FitMatrix_t
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
unsigned int add_with_uncertainty(VIter begin_value, VIter end_value, UIter begin_uncertainty, VPred value_extractor, UPred uncertainty_extractor=UPred())
Clears all the input statistics.
static Data_t UncertaintyToWeight(Data_t s)
Transforms an uncertainty into a weight ( )
< type of value and error
static constexpr Data_t sqr(Data_t v)
Returns the square of the specified data value.
WeightTracker< Data_t > y
accumulator for y
static void ConvertParametersAndVariances(FitParameters_t const &qpars, FitMatrix_t const &qparerrmat, FitParameters_t ¶ms, FitParameters_t ¶mvariances)
Converts the specified quadratic fit parameters and errors.
Data_t Y2() const
Aliases.
virtual Data_t Param(unsigned int n, FitMatrix_t const &Xmat) const
Computes a single fit parameter using the given information.
virtual Data_t Evaluate(Data_t x) const override
Evaluates the fitted function at the specified point.
bool add(Measurement_t value, Data_t sy=Data_t(1.0))
Clears all the input statistics.
static Value_t EncodeValue(Data_t value, Data_t error)
Converts a value and error into a proper input for the quadratic fit.
Class providing data collection for the simple polynomial fitters.
typename Fitter_t::FitMatrix_t FitMatrix_t
static constexpr V sqr(V const &v)
Returns the square of the specified value.
virtual Data_t ChiSquare() const override
Returns the of the fit.
void add(Weight_t weight)
Adds the specified weight to the statistics.
std::tuple< Data_t, Data_t > Base_t
Classes gathering simple statistics.
std::array< Data_t, sqr(NParams)> FitMatrix_t
DataTracker< 1, Data_t > y2
accumulator for y2
virtual FitParameters_t FitParameters() const override
Computes and returns all the parameters of the fit result.
int N() const
Returns the number of (valid) points added.
Value_t(Data_t v, Data_t e)
bool add(MeasurementAndUncertainty_t value)
Adds one entry with specified x, y and uncertainty.
Performs a linear regression of data.
static Measurement_t EncodeValue(Measurement_t const &meas)
Converts a value and error into a proper input for the quadratic fit.
Weight_t Sum(unsigned int n) const
Returns the sum of the values to the power n (1 <= n <= 2, no check)
Data_t XNY(unsigned int n) const
Returns the weighted sum of x^n y.
virtual FitMatrix_t MakeMatrixX() const
Fills and returns the matrix of x^n sum coefficients ( { x^(i+j) } )
virtual FitMatrix_t FitParameterCovariance() const override
Computes and returns all the covariance matrix of the fit result.
typename Collector_t::MeasurementAndUncertainty_t MeasurementAndUncertainty_t
type of measurement with uncertainty
Data_t XNY() const
Returns the weighted sum of x^N y.
virtual FitParameters_t FitParameterErrors() const override
Computes and returns all the parameter errors of the fit result.
void clear()
Resets the count.
typename Base_t::Data_t Data_t
type of the data
Classes with hard-coded (hence "fast") matrix math.
Data_t Slope() const
Returns the slope of the fit.
void add_without_uncertainty(Cont cont)
Clears all the input statistics.
static Value_t DecodeValue(Value_t const &value)
Converts a value from the quadratic fit into a proper value.
virtual bool FillResults(FitParameters_t ¶ms, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const override
Fills the specified parameters.
virtual Data_t ChiSquare() const override
Returns the of the fit.
virtual FitMatrix_t InvertMatrix(FitMatrix_t const &mat) const
Computes the inverse of a matrix.
unsigned int add_with_uncertainty(Cont cont)
Adds measurements with uncertainties from a container.
static FitParameters_t ConvertParameters(FitParameters_t const &qpars)
Converts the specified quadratic fit parameters into Gaussian.
Provides "fast" matrix operations.
constexpr Data_t value() const
WeightTracker< Data_t > s2
accumulator for uncertainty
Data_t InterceptSlopeCovariance() const
Returns the covariance between intercept and slope of the fit.
Data_t Intercept() const
Returns the intercept of the fit.
Data_t X2() const
Aliases.
virtual FitMatrix_t FitParameterCovariance() const override
Computes and returns all the covariance matrix of the fit result.
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
DataTracker< Degree *2, Data_t > x
accumulator for variable x^k
A unary functor returning its own argument (any type)
std::array< Data_t, NParams > FitParameters_t
type of set of fit parameters
void add_without_uncertainty(Cont cont, Pred extractor)
void add_without_uncertainty(Cont cont, Pred extractor)
Clears all the input statistics.
bool FillResults(FitParameters_t ¶ms, FitMatrix_t &Xmat, Data_t &det, FitMatrix_t &Smat) const override
Fills the specified parameters.
static void ConvertParametersAndErrors(FitParameters_t const &qpars, FitMatrix_t const &qparerrmat, FitParameters_t ¶ms, FitParameters_t ¶merrors)
Converts the specified quadratic fit parameters and errors.
typename Interface_t::FitParameters_t FitParameters_t
type of set of fit parameters
static Data_t WeightToUncertainty(Data_t w)
Transforms a weight back to an uncertainty ( )
int N() const
Returns the number of entries added.
void add_without_uncertainty(Cont cont, Pred extractor)
Adds all measurements from a container, with no uncertainty.
void PrintStats(Stream &out) const
Prints the collected statistics into a stream.
void clear()
Clears all the statistics.
Data_t X() const
Aliases.
void add_without_uncertainty(Iter begin, Iter end)
Clears all the input statistics.
Class tracking sums of variables up to a specified power.
virtual int NDF() const override
Returns the degrees of freedom in the determination of the fit.
Data_t SlopeError() const
Returns the error in slope of the fit.
virtual Data_t FitParameterError(unsigned int n) const override
Returns the error on parameter n of the fit result.
bool add(Measurement_t value, Data_t sy=Data_t(1.0))
std::array< Data_t, NParams > FitParameters_t
type of set of fit parameters
Data_t AverageUncertainty() const
Value_t(MeasurementAndUncertainty_t meas)
Data_t AverageUncertainty() const
Returns an average of the uncertainties.
static Value_t EncodeValue(Value_t const &value)
Converts a value and error into a proper input for the quadratic fit.
Data_t XNY(unsigned int n) const
Returns the weighted sum of x^n y.
Performs a second-degree fit of data.
typename Interface_t::FitMatrix_t FitMatrix_t
type of matrix for covariance (a std::array)
void clear()
Resets the count.
typename MatrixOps::Matrix_t FitMatrix_t
type of matrix for covariance (a std::array)
Data_t Y() const
Aliases.
void Print(Stream &out) const
Prints the statistics into a stream.
static constexpr unsigned int Power
Fitter_t fitter
the actual fitter and data holder
virtual FitParameters_t FitParameters() const override
Computes and returns all the parameters of the fit result.
static constexpr V sqr(V const &v)
Returns the square of the specified value.
std::tuple< Data_t, Data_t > Measurement_t
type of measurement without uncertainty
static FitParameters_t ExtractParameterErrors(FitMatrix_t const &Smat)
Extracts parameter errors from diagonal of the covarriance matrix.
Data_t X2Y() const
Aliases.
virtual Fitter_t const & Fitter() const
Returns the internal fitter (mostly for debugging)
std::string value(boost::any const &)
DataTracker< Degree, Data_t > xy
accumulator for variable xy
Base class providing virtual fitting interface for polynomial fitters.
bool add(MeasurementAndUncertainty_t value)
Clears all the input statistics.
LArSoft-specific namespace.
Base class providing data collection for the simple polynomial fitters.
typename Fitter_t::Measurement_t Measurement_t
type of measurement without uncertainty
Data_t XY() const
Aliases.
Simple fitter abstract interface.
typename Fitter_t::MeasurementAndUncertainty_t MeasurementAndUncertainty_t
type of measurement with uncertainty
bool add(Measurement_t value, Data_t sy=Data_t(1.0))
Adds one entry with specified x, y and uncertainty.
virtual FitParameters_t MakeMatrixY() const
Fills and returns the matrix (vector) of x^n y sum coefficients.
void clear()
Clears all the statistics.
static EncodeExtractor< VPred, UPred > Encoder(VPred &vpred, UPred &upred)
static constexpr unsigned int Degree
Degree of the fit.
virtual Data_t Determinant(FitMatrix_t const &mat) const
Computes the determinant of a matrix.
void add_without_uncertainty(Cont cont)
Adds all measurements from a container, with no uncertainty.
void clear()
Clears all the input statistics.
virtual Data_t ChiSquare() const override
Returns the of the fit.
unsigned int add_with_uncertainty(Cont cont)
Data_t XY() const
Aliases.
virtual bool isValid() const override
Returns if the fit has valid results.
void add_without_uncertainty(Iter begin, Iter end, Pred extractor)
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
static void ConvertParametersAndErrorMatrix(FitParameters_t const &qpars, FitMatrix_t const &qparerrmat, FitParameters_t ¶ms, FitMatrix_t &Smat)
Converts the specified quadratic fit parameters and errors.
Data_t X() const
Aliases.
typename Base_t::Matrix_t Matrix_t
typename Collector_t::Measurement_t Measurement_t
type of measurement without uncertainty
virtual Data_t ChiSquare() const override
Returns the of the original fit.
std::array< Data_t, NParams > FitParameters_t
type of set of fit parameters
typename Fitter_t::FitParameters_t FitParameters_t
Data_t Y() const
Aliases.
static Data_t DecodeValue(Data_t value)
Converts a value from the quadratic fit into a proper value.
static Data_t EncodeValue(Data_t value)
virtual Data_t FitParameter(unsigned int n) const
Returns the parameter n of the fit result.
Data_t X2() const
Aliases.
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
static EncodeExtractor< Pred > Encoder(Pred &pred)
Data_t X3() const
Aliases.
Collector_t stats
statistics collected from fit data input
bool add(Data_t x, Data_t y, Data_t sy=Data_t(1.0))
Adds one entry with specified x, y and uncertainty.
virtual Data_t FitParameter(unsigned int n) const override
Returns the parameter n of the fit result.
void PrintStats(Stream &out) const
Prints the collected statistics into a stream.
Data_t Y2() const
Aliases.
Data_t Y2() const
Returns the weighted sum of y^2.
std::tuple< Data_t, Data_t, Data_t > MeasurementAndUncertainty_t
type of measurement with uncertainty
virtual bool isValid() const override
Returns if the fit has valid results.