OpenMS
LinearRegression.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-2023.
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: $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
40 
41 #include <cmath>
42 #include <vector>
43 
44 
45 namespace OpenMS
46 {
47  namespace Math
48  {
65  class OPENMS_DLLAPI LinearRegression
66  {
67 public:
68 
71  intercept_(0),
72  slope_(0),
73  x_intercept_(0),
74  lower_(0),
75  upper_(0),
76  t_star_(0),
77  r_squared_(0),
78  stand_dev_residuals_(0),
79  mean_residuals_(0),
80  stand_error_slope_(0),
81  chi_squared_(0),
82  rsd_(0)
83  {
84  }
85 
87  virtual ~LinearRegression() = default;
88 
110  void computeRegression(double confidence_interval_P,
111  std::vector<double>::const_iterator x_begin,
112  std::vector<double>::const_iterator x_end,
113  std::vector<double>::const_iterator y_begin,
114  bool compute_goodness = true);
115 
138  void computeRegressionWeighted(double confidence_interval_P,
139  std::vector<double>::const_iterator x_begin,
140  std::vector<double>::const_iterator x_end,
141  std::vector<double>::const_iterator y_begin,
142  std::vector<double>::const_iterator w_begin,
143  bool compute_goodness = true);
144 
146  double getIntercept() const;
148  double getSlope() const;
150  double getXIntercept() const;
152  double getLower() const;
154  double getUpper() const;
156  double getTValue() const;
158  double getRSquared() const;
160  double getStandDevRes() const;
162  double getMeanRes() const;
164  double getStandErrSlope() const;
166  double getChiSquared() const;
168  double getRSD() const;
169 
171  static inline double computePointY(double x, double slope, double intercept)
172  {
173  return slope * x + intercept;
174  }
175 
176 protected:
177 
179  double intercept_;
181  double slope_;
183  double x_intercept_;
185  double lower_;
187  double upper_;
189  double t_star_;
191  double r_squared_;
199  double chi_squared_;
201  double rsd_;
202 
204  void computeGoodness_(const std::vector<double>& X, const std::vector<double>& Y, double confidence_interval_P);
205 
207  template <typename Iterator>
208  double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept);
209 
211  template <typename Iterator>
212  double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept);
213 
214 private:
215 
220 
221  }; //class
222 
223  //x, y, w must be of same size
224  template <typename Iterator>
225  double LinearRegression::computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept)
226  {
227  double chi_squared = 0.0;
228  Iterator xIter = x_begin;
229  Iterator yIter = y_begin;
230  for (; xIter != x_end; ++xIter, ++yIter)
231  {
232  chi_squared += std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
233  }
234 
235  return chi_squared;
236  }
237 
238  //x, y, w must be of same size
239  template <typename Iterator>
240  double LinearRegression::computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept)
241  {
242  double chi_squared = 0.0;
243  Iterator xIter = x_begin;
244  Iterator yIter = y_begin;
245  Iterator wIter = w_begin;
246  for (; xIter != x_end; ++xIter, ++yIter, ++wIter)
247  {
248  chi_squared += *wIter * std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
249  }
250 
251  return chi_squared;
252  }
253  } // namespace Math
254 } // namespace OpenMS
255 
256 
This class offers functions to perform least-squares fits to a straight line model,...
Definition: LinearRegression.h:66
void computeRegressionWeighted(double confidence_interval_P, std::vector< double >::const_iterator x_begin, std::vector< double >::const_iterator x_end, std::vector< double >::const_iterator y_begin, std::vector< double >::const_iterator w_begin, bool compute_goodness=true)
This function computes the best-fit linear regression coefficients of the model for the weighted da...
double r_squared_
The squared correlation coefficient (Pearson)
Definition: LinearRegression.h:191
double getRSquared() const
Non-mutable access to the squared Pearson coefficient.
double getIntercept() const
Non-mutable access to the y-intercept of the straight line.
double x_intercept_
The intercept of the fitted line with the x-axis.
Definition: LinearRegression.h:183
double getUpper() const
Non-mutable access to the upper border of confidence interval.
LinearRegression()
Constructor.
Definition: LinearRegression.h:70
double lower_
The lower bound of the confidence interval.
Definition: LinearRegression.h:185
virtual ~LinearRegression()=default
Destructor.
void computeRegression(double confidence_interval_P, std::vector< double >::const_iterator x_begin, std::vector< double >::const_iterator x_end, std::vector< double >::const_iterator y_begin, bool compute_goodness=true)
This function computes the best-fit linear regression coefficients of the model for the dataset .
double getXIntercept() const
Non-mutable access to the x-intercept of the straight line.
static double computePointY(double x, double slope, double intercept)
given x compute y = slope * x + intercept
Definition: LinearRegression.h:171
double upper_
The upper bound of the confidence interval.
Definition: LinearRegression.h:187
LinearRegression & operator=(const LinearRegression &arg)
Not implemented.
double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept)
Compute the chi squared of a weighted linear fit.
Definition: LinearRegression.h:240
double t_star_
The value of the t-statistic.
Definition: LinearRegression.h:189
double getRSD() const
Non-mutable access to relative standard deviation.
double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept)
Compute the chi squared of a linear fit.
Definition: LinearRegression.h:225
double getTValue() const
Non-mutable access to the value of the t-distribution.
double getStandErrSlope() const
Non-mutable access to the standard error of the slope.
double getSlope() const
Non-mutable access to the slope of the straight line.
double getChiSquared() const
Non-mutable access to the chi squared value.
double chi_squared_
The value of the Chi Squared statistic.
Definition: LinearRegression.h:199
double intercept_
The intercept of the fitted line with the y-axis.
Definition: LinearRegression.h:179
double slope_
The slope of the fitted line.
Definition: LinearRegression.h:181
double getLower() const
Non-mutable access to the lower border of confidence interval.
void computeGoodness_(const std::vector< double > &X, const std::vector< double > &Y, double confidence_interval_P)
Computes the goodness of the fitted regression line.
double mean_residuals_
Mean of residuals.
Definition: LinearRegression.h:195
double stand_dev_residuals_
The standard deviation of the residuals.
Definition: LinearRegression.h:193
LinearRegression(const LinearRegression &arg)
Not implemented.
double rsd_
the relative standard deviation
Definition: LinearRegression.h:201
double getMeanRes() const
Non-mutable access to the residual mean.
double stand_error_slope_
The standard error of the slope.
Definition: LinearRegression.h:197
double getStandDevRes() const
Non-mutable access to the standard deviation of the residuals.
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48