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);
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");
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:684
Definition: QuadraticRegression.h:59
double b_
Definition: QuadraticRegression.h:84
double eval(double x) const
double c_
Definition: QuadraticRegression.h:85
static double eval(double A, double B, double C, double x)
void computeRegressionWeighted(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
Definition: QuadraticRegression.h:119
double getChiSquared() const
C*X^2.
double chi_squared_
Definition: QuadraticRegression.h:86
void computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin)
Definition: QuadraticRegression.h:112
double getB() const
A = the intercept.
double a_
Definition: QuadraticRegression.h:83
int Int
Signed integer type.
Definition: Types.h:102
const double c
Definition: Constants.h:209
std::vector< Wm5::Vector2d > iteratorRange2Wm5Vectors(Iterator x_begin, Iterator x_end, Iterator y_begin)
Copies the distance(x_begin,x_end) elements starting at x_begin and y_begin into the Wm5::Vector.
Definition: RegressionUtils.h:44
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47