35 #ifndef OPENMS_MATH_STATISTICS_QUADRATICREGRESSION_H 36 #define OPENMS_MATH_STATISTICS_QUADRATICREGRESSION_H 42 #include "Wm5Vector2.h" 43 #include "Wm5LinearSystem.h" 46 using Wm5::LinearSystem;
67 template <
typename Iterator>
68 void computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin);
71 template <
typename Iterator>
72 void computeRegressionWeighted(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin);
75 double eval(
double x)
const;
78 static double eval(
double A,
double B,
double C,
double x);
83 double getChiSquared()
const;
95 template <
typename Iterator>
96 double computeChiSquareWeighted_(
97 Iterator x_begin,
const Iterator& x_end, Iterator y_begin, Iterator w_begin,
98 const double a,
const double b,
const double c)
100 double chi_squared(0.0);
101 for (; x_begin != x_end; ++x_begin, ++y_begin, ++w_begin)
103 const double& x = *x_begin;
104 const double& y = *y_begin;
105 const double& weight = *w_begin;
106 chi_squared += weight * std::pow(y - a - b * x - c * x * x, 2);
114 template <
typename Iterator>
117 std::vector<double> weights(std::distance(x_begin, x_end), 1);
118 computeRegressionWeighted<Iterator>(x_begin, x_end, y_begin, weights.begin());
121 template <
typename Iterator>
123 Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
130 int numPoints =
static_cast<Int>(points.size());
131 double sumX = 0, sumXX = 0, sumXXX = 0, sumXXXX = 0;
132 double sumY = 0, sumXY = 0, sumXXY = 0;
135 Iterator wIter = w_begin;
136 for (
int i = 0; i < numPoints; ++i, ++wIter)
139 double x = points[i].X();
140 double y = points[i].Y();
141 double weight = *wIter;
144 sumXX += weight * x * x;
145 sumXXX += weight * x * x * x;
146 sumXXXX += weight * x * x * x * x;
149 sumXY += weight * x * y;
150 sumXXY += weight * x * x * y;
158 {sumX, sumXX, sumXXX},
159 {sumXX, sumXXX, sumXXXX}
167 double X[3] = {0, 0, 0};
169 bool nonsingular = Wm5::LinearSystem<double>().Solve3(A, B, X);
175 chi_squared_ = computeChiSquareWeighted_(x_begin, x_end, y_begin, w_begin, a_, b_, c_);
179 throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"UnableToFit-QuadraticRegression",
"Could not fit a linear model to the data");
186 #endif // OPENMS_MATH_STATISTICS_QUADRATICREGRESSION_H double c_
Definition: QuadraticRegression.h:88
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:45
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Definition: QuadraticRegression.h:61
double chi_squared_
Definition: QuadraticRegression.h:89
double b_
Definition: QuadraticRegression.h:87
void computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin)
Definition: QuadraticRegression.h:115
double a_
Definition: QuadraticRegression.h:86
void computeRegressionWeighted(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
Definition: QuadraticRegression.h:122
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:677
int Int
Signed integer type.
Definition: Types.h:103