OpenMS  2.4.0
QuadraticRegression.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2018.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Christian Ehrlich, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
40 
41 #include "Wm5Vector2.h"
42 #include "Wm5LinearSystem.h"
43 #include <iterator>
44 
45 using Wm5::LinearSystem;
46 
47 namespace OpenMS
48 {
49  namespace Math
50  {
51  /*
52  @brief Estimates model parameters for a quadratic equation
53 
54  The quadratic equation is of the form
55  y = a + b*x + c*x^2
56 
57  Weighted inputs are supported.
58 
59  */
60  class OPENMS_DLLAPI QuadraticRegression
61  {
62 public:
64 
66  template <typename Iterator>
67  void computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin);
68 
70  template <typename Iterator>
71  void computeRegressionWeighted(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin);
72 
74  double eval(double x) const;
75 
77  static double eval(double A, double B, double C, double x);
78 
79  double getA() const;
80  double getB() const;
81  double getC() const;
82  double getChiSquared() const;
83 
84 protected:
85  double a_;
86  double b_;
87  double c_;
88  double chi_squared_;
89  }; //class
90 
91  namespace
92  {
93  //x, y must be of same size
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)
98  {
99  double chi_squared(0.0);
100  for (; x_begin != x_end; ++x_begin, ++y_begin, ++w_begin)
101  {
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);
106  }
107 
108  return chi_squared;
109  }
110 
111  } // anonymous namespace
112 
113  template <typename Iterator>
114  void QuadraticRegression::computeRegression(Iterator x_begin, Iterator x_end, Iterator y_begin)
115  {
116  std::vector<double> weights(std::distance(x_begin, x_end), 1);
117  computeRegressionWeighted<Iterator>(x_begin, x_end, y_begin, weights.begin());
118  }
119 
120  template <typename Iterator>
122  Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
123  {
124  // Compute the linear fit of a quadratic function.
125  // Get the coefficients for y = w_1*a +w_2*b*x + w_3*c*x^2.
126  std::vector<Wm5::Vector2d> points = iteratorRange2Wm5Vectors(x_begin, x_end, y_begin);
127  // Compute sums for linear system. copy&paste from GeometricTools Wm5ApprLineFit2.cpp
128  // and modified to allow quadratic functions
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;
132  double sumW = 0;
133 
134  Iterator wIter = w_begin;
135  for (int i = 0; i < numPoints; ++i, ++wIter)
136  {
137 
138  double x = points[i].X();
139  double y = points[i].Y();
140  double weight = *wIter;
141 
142  sumX += weight * x;
143  sumXX += weight * x * x;
144  sumXXX += weight * x * x * x;
145  sumXXXX += weight * x * x * x * x;
146 
147  sumY += weight * y;
148  sumXY += weight * x * y;
149  sumXXY += weight * x * x * y;
150 
151  sumW += weight;
152  }
153  //create matrices to solve Ax = B
154  double A[3][3] =
155  {
156  {sumW, sumX, sumXX},
157  {sumX, sumXX, sumXXX},
158  {sumXX, sumXXX, sumXXXX}
159  };
160  double B[3] =
161  {
162  sumY,
163  sumXY,
164  sumXXY
165  };
166  double X[3] = {0, 0, 0};
167 
168  bool nonsingular = Wm5::LinearSystem<double>().Solve3(A, B, X);
169  if (nonsingular)
170  {
171  a_ = X[0];
172  b_ = X[1];
173  c_ = X[2];
174  chi_squared_ = computeChiSquareWeighted_(x_begin, x_end, y_begin, w_begin, a_, b_, c_);
175  }
176  else
177  {
178  throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "UnableToFit-QuadraticRegression", "Could not fit a linear model to the data");
179  }
180  }
181 
182  } //namespace
183 } //namespace
184 
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
const double c
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