41 #include "Wm5Vector2.h" 42 #include "Wm5LinearSystem.h" 45 using Wm5::LinearSystem;
66 template <
typename Iterator>
67 void computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin);
70 template <
typename Iterator>
71 void computeRegressionWeighted(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin);
74 double eval(
double x)
const;
77 static double eval(
double A,
double B,
double C,
double x);
82 double getChiSquared()
const;
94 template <
typename Iterator>
95 double computeChiSquareWeighted_(
96 Iterator x_begin,
const Iterator& x_end, Iterator y_begin, Iterator w_begin,
97 const double a,
const double b,
const double c)
99 double chi_squared(0.0);
100 for (; x_begin != x_end; ++x_begin, ++y_begin, ++w_begin)
102 const double& x = *x_begin;
103 const double& y = *y_begin;
104 const double& weight = *w_begin;
105 chi_squared += weight * std::pow(y - a - b * x -
c * x * x, 2);
113 template <
typename Iterator>
116 std::vector<double> weights(std::distance(x_begin, x_end), 1);
117 computeRegressionWeighted<Iterator>(x_begin, x_end, y_begin, weights.begin());
120 template <
typename Iterator>
122 Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
129 int numPoints =
static_cast<Int>(points.size());
130 double sumX = 0, sumXX = 0, sumXXX = 0, sumXXXX = 0;
131 double sumY = 0, sumXY = 0, sumXXY = 0;
134 Iterator wIter = w_begin;
135 for (
int i = 0; i < numPoints; ++i, ++wIter)
138 double x = points[i].X();
139 double y = points[i].Y();
140 double weight = *wIter;
143 sumXX += weight * x * x;
144 sumXXX += weight * x * x * x;
145 sumXXXX += weight * x * x * x * x;
148 sumXY += weight * x * y;
149 sumXXY += weight * x * x * y;
157 {sumX, sumXX, sumXXX},
158 {sumXX, sumXXX, sumXXXX}
166 double X[3] = {0, 0, 0};
168 bool nonsingular = Wm5::LinearSystem<double>().Solve3(A, B, X);
174 chi_squared_ = computeChiSquareWeighted_(x_begin, x_end, y_begin, w_begin,
a_,
b_,
c_);
178 throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"UnableToFit-QuadraticRegression",
"Could not fit a linear model to the data");
double c_
Definition: QuadraticRegression.h:87
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:46
Definition: QuadraticRegression.h:60
double chi_squared_
Definition: QuadraticRegression.h:88
double b_
Definition: QuadraticRegression.h:86
void computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin)
Definition: QuadraticRegression.h:114
double a_
Definition: QuadraticRegression.h:85
void computeRegressionWeighted(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
Definition: QuadraticRegression.h:121
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:676
int Int
Signed integer type.
Definition: Types.h:102