14 #define SIMPLEFITS_H 1 24 #include <type_traits> 35 template <
typename T,
unsigned int D>
55 template <
unsigned int Power,
unsigned int N>
59 return sums.template SumN<N>();
64 template <
unsigned int Power>
74 static constexpr
unsigned int Degree = D;
75 static constexpr
unsigned int NParams = Degree + 1;
111 return add(std::get<0>(value), std::get<1>(value), sy);
123 return add(std::get<0>(value), std::get<1>(value), std::get<2>(value));
135 template <
typename Iter>
154 template <
typename Iter,
typename Pred>
171 template <
typename Cont,
typename Pred>
187 template <
typename Cont>
219 template <
typename VIter,
typename UIter,
typename VPred,
typename UPred =
identity>
222 UIter begin_uncertainty,
223 VPred value_extractor,
224 UPred uncertainty_extractor = UPred());
240 template <
typename Iter>
254 template <
typename Cont>
269 int N()
const {
return s2.
N(); }
283 template <
typename V>
284 static constexpr V
sqr(V
const& v)
296 template <
unsigned int N>
303 template <
unsigned int N>
315 template <
typename Stream>
316 void Print(Stream& out)
const;
333 template <
typename T,
unsigned int D>
334 inline std::ostream& operator<<(std::ostream& out, FitDataCollector<T, D>
const& stats)
341 template <
typename T,
unsigned int D>
347 static constexpr
unsigned int Degree = Collector_t::Degree;
369 template <
typename Iter>
372 stats.add_without_uncertainty(begin, end);
375 template <
typename Iter,
typename Pred>
378 stats.add_without_uncertainty(begin, end, extractor);
381 template <
typename Cont,
typename Pred>
384 stats.add_without_uncertainty(cont, extractor);
387 template <
typename Cont>
390 stats.add_without_uncertainty(cont);
393 template <
typename VIter,
typename UIter,
typename VPred,
typename UPred =
identity>
396 UIter begin_uncertainty,
397 VPred value_extractor,
398 UPred uncertainty_extractor = UPred())
400 return stats.add_with_uncertainty(
401 begin_value, end_value, begin_uncertainty, value_extractor, uncertainty_extractor);
404 template <
typename Iter>
407 return stats.add_with_uncertainty(begin, end);
410 template <
typename Cont>
413 return stats.add_with_uncertainty(cont);
425 int N()
const {
return stats.N(); }
432 template <
typename Stream>
439 template <
typename V>
440 static constexpr V
sqr(V
const& v)
457 template <
typename T,
unsigned int N>
461 static constexpr
unsigned int NParams =
N;
486 virtual bool isValid()
const = 0;
513 virtual FitMatrix_t FitParameterCovariance()
const = 0;
531 return std::sqrt(FitParameterCovariance()[n * (NParams + 1)]);
538 virtual Data_t ChiSquare()
const = 0;
547 virtual int NDF()
const = 0;
609 return MatrixOps::Determinant(mat);
615 return MatrixOps::InvertSymmetricMatrix(mat, det);
621 return MatrixOps::InvertSymmetricMatrix(mat);
628 return MatrixOps::MatrixVectorProduct(mat, vec);
634 template <
typename T,
unsigned int D>
644 static constexpr
unsigned int Degree = Base_t::Degree;
647 static constexpr
unsigned int NParams = Interface_t::NParams;
667 virtual bool isValid()
const override;
694 virtual FitMatrix_t FitParameterCovariance()
const override;
702 virtual Data_t FitParameter(
unsigned int n)
const override;
710 virtual Data_t FitParameterError(
unsigned int n)
const override;
716 virtual Data_t ChiSquare()
const override;
725 virtual int NDF()
const override {
return Base_t::N() -
NParams; }
787 return ExtractParameterErrors(Smat);
804 using Interface_t::Determinant;
805 using Interface_t::InvertMatrix;
806 using Interface_t::MatrixProduct;
832 template <
typename T>
837 using Base_t::Degree;
838 using Base_t::NParams;
890 return this->FitParameterCovariance()[0 * NParams + 1];
897 virtual Data_t ChiSquare()
const override;
901 Data_t I()
const {
return Base_t::stats.template XN<0>(); }
903 Data_t X()
const {
return Base_t::stats.template XN<1>(); }
904 Data_t X2()
const {
return Base_t::stats.template XN<2>(); }
905 Data_t Y()
const {
return Base_t::stats.template XNY<0>(); }
906 Data_t XY()
const {
return Base_t::stats.template XNY<1>(); }
932 template <
typename T>
937 using Base_t::Degree;
938 using Base_t::NParams;
959 virtual Data_t ChiSquare()
const override;
963 Data_t I()
const {
return Base_t::stats.template XN<0>(); }
965 Data_t X()
const {
return Base_t::stats.template XN<1>(); }
966 Data_t X2()
const {
return Base_t::stats.template XN<2>(); }
967 Data_t X3()
const {
return Base_t::stats.template XN<3>(); }
968 Data_t X4()
const {
return Base_t::stats.template XN<4>(); }
969 Data_t Y()
const {
return Base_t::stats.template XNY<0>(); }
970 Data_t XY()
const {
return Base_t::stats.template XNY<1>(); }
971 Data_t X2Y()
const {
return Base_t::stats.template XNY<2>(); }
992 template <
typename T>
999 static constexpr
unsigned int NParams = Base_t::NParams;
1025 return add(std::get<0>(value), std::get<1>(value), sy);
1030 return add(std::get<0>(value), std::get<1>(value), std::get<2>(value));
1033 template <
typename Iter>
1039 template <
typename Iter,
typename Pred>
1042 template <
typename Cont,
typename Pred>
1048 template <
typename Cont>
1054 template <
typename VIter,
typename UIter,
typename VPred,
typename UPred =
identity>
1057 UIter begin_uncertainty,
1058 VPred value_extractor,
1059 UPred uncertainty_extractor = UPred());
1061 template <
typename Iter>
1064 template <
typename Cont>
1074 int N()
const {
return fitter.N(); }
1077 template <
typename Stream>
1080 fitter.PrintStats(out);
1097 virtual bool isValid()
const override {
return fitter.isValid(); }
1125 virtual FitMatrix_t FitParameterCovariance()
const override;
1144 virtual int NDF()
const override {
return fitter.NDF(); }
1200 return Evaluate(x, FitParameters().data());
1221 struct Value_t :
public std::tuple<Data_t, Data_t> {
1245 return {std::log(value), error /
std::abs(value)};
1251 return EncodeValue(value.
value(), value.
error());
1258 return {v, v * value.
error()};
1264 return Measurement_t(std::get<0>(meas), EncodeValue(std::get<1>(meas)));
1271 return {std::get<0>(meas), value.
value(), value.
error()};
1278 return {std::get<0>(meas), value.
value(), value.
error()};
1283 template <
typename VPred,
typename UPred =
void>
1285 EncodeExtractor(VPred& vpred, UPred& upred) : value_extractor(vpred), error_extractor(upred)
1291 template <
typename Elem>
1295 return EncodeValue(static_cast<Measurement_t&&>(value_extractor(elem)),
1296 static_cast<Data_t&&>(error_extractor(elem)));
1305 template <
typename Pred>
1316 return EncodeValue(extractor(elem));
1322 return EncodeValue(extractor(elem));
1328 template <
typename Pred>
1334 template <
typename VPred,
typename UPred>
1337 return {vpred, upred};
1368 static void ConvertParametersAndVariances(
FitParameters_t const& qpars,
1380 static void ConvertParametersAndErrorMatrix(
FitParameters_t const& qpars,
1393 static void ThrowNotImplemented [[noreturn]] (std::string method)
1395 throw std::logic_error(
"Method " + method +
"() not implemented");
1411 template <
typename T,
unsigned int D>
1417 if (!std::isnormal(w))
return false;
1426 xy.
add(x_value, yw);
1431 template <
typename T,
unsigned int D>
1432 template <
typename Iter,
typename Pred>
1437 std::for_each(begin, end, [
this, extractor](
auto item) { this->
add(extractor(item)); });
1440 template <
typename T,
unsigned int D>
1441 template <
typename VIter,
typename UIter,
typename VPred,
typename UPred>
1445 UIter begin_uncertainty,
1446 VPred value_extractor,
1447 UPred uncertainty_extractor
1451 while (begin_value != end_value) {
1452 if (
add(value_extractor(*begin_value), uncertainty_extractor(*begin_uncertainty))) ++
n;
1454 ++begin_uncertainty;
1459 template <
typename T,
unsigned int D>
1460 template <
typename Iter>
1463 unsigned int old_n =
N();
1464 std::for_each(begin, end, [
this](
auto p) { this->
add(p); });
1468 template <
typename T,
unsigned int D>
1478 template <
typename T,
unsigned int D>
1479 template <
typename Stream>
1483 out <<
"Sums 1/s^2=" <<
s2.
Weights() <<
"\n x/s^2=" << x.template SumN<1>();
1484 for (
unsigned int degree = 2; degree <= x.Power; ++degree)
1485 out <<
"\n x^" << degree <<
"/s^2=" << x.Sum(degree);
1486 out <<
"\n y/s^2=" << y.Weights() <<
"\n y^2/s^2=" <<
y2.
Sum();
1487 if (
xy.
Power >= 1) out <<
"\n xy/s^2=" <<
xy.template SumN<1>();
1488 for (
unsigned int degree = 2; degree <=
xy.
Power; ++degree)
1489 out <<
"\n x^" << degree <<
"y/s^2=" <<
xy.
Sum(degree);
1496 template <
typename T,
unsigned int D>
1499 return (Base_t::N() > (
int)Degree) && std::isnormal(Determinant(MakeMatrixX()));
1502 template <
typename T,
unsigned int D>
1506 return Param(
n, MakeMatrixX());
1509 template <
typename T,
unsigned int D>
1513 if (
n > Degree)
return Data_t(0);
1514 return std::sqrt(FitParameterCovariance()[
n * (NParams + 1)]);
1517 template <
typename T,
unsigned int D>
1522 for (
unsigned int iParam = 0; iParam <
NParams; ++iParam)
1523 fit_params[iParam] = Param(iParam, Xmat);
1527 template <
typename T,
unsigned int D>
1530 return FitParameterErrors(FitParameterCovariance());
1533 template <
typename T,
unsigned int D>
1537 Data_t det = Determinant(Xmat);
1538 if (!std::isnormal(det)) {
1539 throw std::range_error(
1540 "SimplePolyFitterBase::FitParameterCovariance(): determinant 0 while fitting");
1542 return InvertMatrix(Xmat, det);
1545 template <
typename T,
unsigned int D>
1552 Xmat = MakeMatrixX();
1553 det = Determinant(Xmat);
1554 if (!std::isnormal(det)) {
1559 Smat = InvertMatrix(Xmat, det);
1560 params = FitParameters(Smat, det);
1564 template <
typename T,
unsigned int D>
1572 if (!this->FillResults(params, Xmat, det, Smat))
return false;
1573 paramerrors = ExtractParameterErrors(Smat);
1577 template <
typename T,
unsigned int D>
1585 return FillResults(params, paramerrors, Xmat, det, Smat);
1588 template <
typename T,
unsigned int D>
1592 unsigned int iParam = NParams - 1;
1593 Data_t v = params[iParam];
1595 v = v * x + params[--iParam];
1600 template <
typename T,
unsigned int D>
1604 for (
unsigned int i = 0; i <
NParams; ++i) {
1605 for (
unsigned int j = i; j <
NParams; ++j) {
1606 Xmat[j * NParams + i] = Xmat[i * NParams + j] = Base_t::XN(i + j);
1612 template <
typename T,
unsigned int D>
1616 for (
unsigned int i = 0; i <
NParams; ++i)
1617 Ymat[i] = Base_t::XNY(i);
1621 template <
typename T,
unsigned int D>
1626 for (
unsigned int iParam = 0; iParam <
NParams; ++iParam)
1627 fit_params[iParam] = Param(iParam, Xmat);
1631 template <
typename T,
unsigned int D>
1636 return MatrixProduct(Smat, MakeMatrixY());
1639 template <
typename T,
unsigned int D>
1643 if (
n > Degree)
return Data_t(0);
1645 Data_t detXmat = Determinant(Xmat);
1646 if (!std::isnormal(detXmat)) {
1647 throw std::range_error(
"SimplePolyFitterBase::Param(): Determinant 0 while fitting");
1649 return Param(
n, Xmat, detXmat);
1652 template <
typename T,
unsigned int D>
1657 for (
unsigned int iParam = 0; iParam <=
Degree; ++iParam)
1658 fit_errors[iParam] = std::sqrt(Smat[iParam * (NParams + 1)]);
1662 template <
typename T,
unsigned int D>
1667 if (
n > Degree)
return Data_t(0);
1671 for (
unsigned int i = 0; i <
NParams; ++i)
1672 XYmat[i * NParams +
n] = Base_t::XNY(i);
1674 return Determinant(XYmat) / detXmat;
1677 template <
typename T,
unsigned int D>
1682 throw std::logic_error(
"SimplePolyFitterBase::ChiSquare() not implemented for generic fit");
1689 template <
typename T>
1693 const Data_t b = fit_params[0];
1694 const Data_t a = fit_params[1];
1695 return Y2() +
sqr(a) *
X2() +
sqr(b) * I() +
Data_t(2) * (a * b *
X2() - a * XY() - b *
Y());
1702 template <
typename T>
1706 return Y2() -
Data_t(2) * (a[0] *
Y() + a[1] * XY() + a[2] * X2Y()) +
sqr(a[0]) * I() +
1707 Data_t(2) * a[0] * (a[1] *
X() + a[2] *
X2()) +
sqr(a[1]) *
X2() +
1708 Data_t(2) * a[1] * (a[2] * X3()) +
sqr(a[2]) * X4();
1718 template <
typename T>
1721 if (y <=
Data_t(0))
return false;
1723 return fitter.add(x, value.
value(), value.
error());
1726 template <
typename T>
1727 template <
typename Iter,
typename Pred>
1730 return fitter.add_without_uncertainty(begin, end, Encoder(extractor));
1733 template <
typename T>
1734 template <
typename VIter,
typename UIter,
typename VPred,
typename UPred>
1738 UIter begin_uncertainty,
1739 VPred value_extractor,
1740 UPred uncertainty_extractor
1744 begin_value, end_value, begin_uncertainty, Encoder(value_extractor, uncertainty_extractor));
1747 template <
typename T>
1748 template <
typename Iter>
1751 unsigned int old_n =
N();
1752 std::for_each(begin, end, [
this](
auto p) { this->
add(p); });
1759 template <
typename T>
1762 return ConvertParameters(fitter.FitParameters());
1765 template <
typename T>
1769 if (!FillResults(qpars, qparerrors)) {
1770 throw std::runtime_error(
"GaussianFit::FitParameterErrors() yielded invalid results");
1775 template <
typename T>
1783 if (!FillResults(params, Xmat, det, Smat)) {
1784 throw std::runtime_error(
"GaussianFit::FitParameterCovariance() yielded invalid results");
1789 template <
typename T>
1797 if (!fitter.FillResults(qpars, Xmat, det, qparerrmat))
return false;
1798 ConvertParametersAndErrors(qpars, qparerrmat, params, paramerrors);
1799 return isValid(params, qpars);
1802 template <
typename T>
1810 if (!fitter.FillResults(qpars, Xmat, det, qparerrmat))
return false;
1811 ConvertParametersAndErrorMatrix(qpars, qparerrmat, params, Smat);
1812 return isValid(params, qpars);
1815 template <
typename T>
1822 if (!FillResults(params, Xmat, det, Smat))
return false;
1823 paramerrors = fitter.ExtractParameterErrors(Smat);
1827 template <
typename T>
1830 Data_t z = (x - params[1]) / params[2];
1831 return params[0] * std::exp(-0.5 *
sqr(z));
1834 template <
typename T>
1839 Data_t sigma2 = -0.5 / qpars[2];
1840 params[2] = std::sqrt(sigma2);
1842 params[1] = sigma2 * qpars[1];
1844 params[0] = std::exp(qpars[0] - 0.25 *
sqr(qpars[1]) / qpars[2]);
1849 template <
typename T>
1855 params = ConvertParameters(qpars);
1858 Data_t const& A = params[0];
1859 Data_t const& mu = params[1];
1860 Data_t const& sigma = params[2];
1863 paramvariances[2] = qparerrmat[3 * 2 + 2] /
sqr(cube(sigma));
1867 sqr(mu * (+qparerrmat[3 * 1 + 1] /
sqr(a[1]) - 2. * qparerrmat[3 * 2 + 1] / (a[1] * a[2]) +
1868 qparerrmat[3 * 2 + 2] /
sqr(a[2])));
1872 sqr(A * (+qparerrmat[3 * 0 + 0] + 2. * qparerrmat[3 * 0 + 1] * mu +
1873 (qparerrmat[3 * 1 + 1] + 2. * qparerrmat[3 * 0 + 2]) *
sqr(mu) +
1874 2. * qparerrmat[3 * 1 + 2] * cube(mu) + qparerrmat[3 * 2 + 2] *
sqr(
sqr(mu))));
1878 template <
typename T>
1884 ConvertParametersAndVariances(qpars, qparerrmat, params, paramerrors);
1886 for (
Data_t& paramerror : paramerrors)
1887 paramerror = std::sqrt(paramerror);
1890 template <
typename T>
1897 ConvertParametersAndVariances(qpars, qparerrmat, params, paramvariances);
1901 Data_t const& A = params[0];
1902 Data_t const& mu = params[1];
1903 Data_t const& sigma = params[2];
1906 Smat[3 * 2 + 2] = paramvariances[2];
1909 Smat[3 * 1 + 1] = paramvariances[1];
1912 Smat[3 * 0 + 0] = paramvariances[0];
1915 Smat[3 * 1 + 2] = Smat[3 * 2 + 1] =
1916 (qparerrmat[3 * 1 + 2] + 2 * mu * qparerrmat[3 * 2 + 2]) / sigma;
1920 const Data_t dA_dak_cov_aka2 =
1921 A * (qparerrmat[3 * 0 + 2] + qparerrmat[3 * 1 + 2] * mu + qparerrmat[3 * 2 + 2] *
sqr(mu));
1923 Smat[3 * 0 + 2] = Smat[3 * 2 + 0] = dA_dak_cov_aka2 / cube(sigma);
1926 const Data_t dA_dak_cov_aka1 =
1927 A * (qparerrmat[3 * 0 + 1] + qparerrmat[3 * 1 + 1] * mu + qparerrmat[3 * 2 + 1] *
sqr(mu));
1930 Smat[3 * 0 + 1] = Smat[3 * 1 + 0] = mu * (dA_dak_cov_aka1 / a[1] - dA_dak_cov_aka2 / a[2]);
1934 template <
typename T>
1937 return (qpars[2] <
Data_t(0)) && (params[0] >=
Data_t(0));
1942 #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())
virtual FitMatrix_t InvertMatrix(FitMatrix_t const &mat, Data_t det) const
Computes the inverse of a matrix (using provided determinant)
Data_t XN(unsigned int n) const
Returns the weighted sum of x^n.
constexpr Data_t error() const
Data_t operator()(Data_t x) const
Evaluates the fitted function; alias of Evaluate()
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 MeasurementAndUncertainty_t EncodeValue(MeasurementAndUncertainty_t const &meas)
Converts a value and error into a proper input for the quadratic fit.
constexpr auto abs(T v)
Returns the absolute value of the argument.
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.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
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.
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.
virtual FitParameters_t MatrixProduct(FitMatrix_t const &mat, FitParameters_t const &vec) const
Computes the product of a FitMatrix_t and a FitParameters_t.
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
virtual FitParameters_t FitParameterErrors(FitMatrix_t const &Smat) const
Computes and returns all the parameter errors of the fit result.
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)
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
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.
static MeasurementAndUncertainty_t EncodeValue(Measurement_t const &meas, Data_t error)
Converts a value and error into a proper input for the quadratic fit.
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)
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.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
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.