OpenMS
LinearRegression.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Types.h>
14 
15 #include <cmath>
16 #include <vector>
17 
18 
19 namespace OpenMS
20 {
21  namespace Math
22  {
39  class OPENMS_DLLAPI LinearRegression
40  {
41 public:
42 
45  intercept_(0),
46  slope_(0),
47  x_intercept_(0),
48  lower_(0),
49  upper_(0),
50  t_star_(0),
51  r_squared_(0),
52  stand_dev_residuals_(0),
53  mean_residuals_(0),
54  stand_error_slope_(0),
55  chi_squared_(0),
56  rsd_(0)
57  {
58  }
59 
61  virtual ~LinearRegression() = default;
62 
83  void computeRegression(double confidence_interval_P,
84  std::vector<double>::const_iterator x_begin,
85  std::vector<double>::const_iterator x_end,
86  std::vector<double>::const_iterator y_begin,
87  bool compute_goodness = true);
88 
110  void computeRegressionWeighted(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  std::vector<double>::const_iterator w_begin,
115  bool compute_goodness = true);
116 
118  double getIntercept() const;
120  double getSlope() const;
122  double getXIntercept() const;
124  double getLower() const;
126  double getUpper() const;
128  double getTValue() const;
130  double getRSquared() const;
132  double getStandDevRes() const;
134  double getMeanRes() const;
136  double getStandErrSlope() const;
138  double getChiSquared() const;
140  double getRSD() const;
141 
143  static inline double computePointY(double x, double slope, double intercept)
144  {
145  return slope * x + intercept;
146  }
147 
148 protected:
149 
151  double intercept_;
153  double slope_;
155  double x_intercept_;
157  double lower_;
159  double upper_;
161  double t_star_;
163  double r_squared_;
171  double chi_squared_;
173  double rsd_;
174 
176  void computeGoodness_(const std::vector<double>& X, const std::vector<double>& Y, double confidence_interval_P);
177 
179  template <typename Iterator>
180  double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept);
181 
183  template <typename Iterator>
184  double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept);
185 
186 private:
187 
192 
193  }; //class
194 
195  //x, y, w must be of same size
196  template <typename Iterator>
197  double LinearRegression::computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept)
198  {
199  double chi_squared = 0.0;
200  Iterator xIter = x_begin;
201  Iterator yIter = y_begin;
202  for (; xIter != x_end; ++xIter, ++yIter)
203  {
204  chi_squared += std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
205  }
206 
207  return chi_squared;
208  }
209 
210  //x, y, w must be of same size
211  template <typename Iterator>
212  double LinearRegression::computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept)
213  {
214  double chi_squared = 0.0;
215  Iterator xIter = x_begin;
216  Iterator yIter = y_begin;
217  Iterator wIter = w_begin;
218  for (; xIter != x_end; ++xIter, ++yIter, ++wIter)
219  {
220  chi_squared += *wIter * std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
221  }
222 
223  return chi_squared;
224  }
225  } // namespace Math
226 } // namespace OpenMS
227 
228 
This class offers functions to perform least-squares fits to a straight line model,...
Definition: LinearRegression.h:40
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:163
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:155
double getUpper() const
Non-mutable access to the upper border of confidence interval.
LinearRegression()
Constructor.
Definition: LinearRegression.h:44
double lower_
The lower bound of the confidence interval.
Definition: LinearRegression.h:157
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:143
double upper_
The upper bound of the confidence interval.
Definition: LinearRegression.h:159
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:212
double t_star_
The value of the t-statistic.
Definition: LinearRegression.h:161
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:197
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:171
double intercept_
The intercept of the fitted line with the y-axis.
Definition: LinearRegression.h:151
double slope_
The slope of the fitted line.
Definition: LinearRegression.h:153
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:167
double stand_dev_residuals_
The standard deviation of the residuals.
Definition: LinearRegression.h:165
LinearRegression(const LinearRegression &arg)
Not implemented.
double rsd_
the relative standard deviation
Definition: LinearRegression.h:173
double getMeanRes() const
Non-mutable access to the residual mean.
double stand_error_slope_
The standard error of the slope.
Definition: LinearRegression.h:169
double getStandDevRes() const
Non-mutable access to the standard deviation of the residuals.
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19