41 #include "Wm5Vector2.h"
42 #include "Wm5LinearSystem.h"
64 template <
typename Iterator>
68 template <
typename Iterator>
72 double eval(
double x)
const;
75 static double eval(
double A,
double B,
double C,
double x);
80 double getChiSquared()
const;
92 template <
typename Iterator>
93 double computeChiSquareWeighted_(
95 const double a,
const double b,
const double c)
97 double chi_squared(0.0);
98 for (; x_begin != x_end; ++x_begin, ++y_begin, ++w_begin)
100 const double& x = *x_begin;
101 const double& y = *y_begin;
102 const double& weight = *w_begin;
103 chi_squared += weight * std::pow(y - a - b * x -
c * x * x, 2);
111 template <
typename Iterator>
114 std::vector<double> weights(std::distance(x_begin, x_end), 1);
115 computeRegressionWeighted<Iterator>(x_begin, x_end, y_begin, weights.begin());
118 template <
typename Iterator>
127 int numPoints = static_cast<Int>(points.size());
128 double sumX = 0, sumXX = 0, sumXXX = 0, sumXXXX = 0;
129 double sumY = 0, sumXY = 0, sumXXY = 0;
133 for (
int i = 0; i < numPoints; ++i, ++wIter)
136 double x = points[i].X();
137 double y = points[i].Y();
138 double weight = *wIter;
141 sumXX += weight * x * x;
142 sumXXX += weight * x * x * x;
143 sumXXXX += weight * x * x * x * x;
146 sumXY += weight * x * y;
147 sumXXY += weight * x * x * y;
155 {sumX, sumXX, sumXXX},
156 {sumXX, sumXXX, sumXXXX}
164 double X[3] = {0, 0, 0};
166 bool nonsingular = Wm5::LinearSystem<double>().Solve3(A, B, X);
172 chi_squared_ = computeChiSquareWeighted_(x_begin, x_end, y_begin, w_begin,
a_,
b_,
c_);
176 throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"UnableToFit-QuadraticRegression",
"Could not fit a linear model to the data");