OpenMS
Loading...
Searching...
No Matches
LinearRegression.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
13
14#include <cmath>
15#include <vector>
16
17
18namespace OpenMS
19{
20 namespace Math
21 {
38 class OPENMS_DLLAPI LinearRegression
39 {
40public:
41
44 intercept_(0),
45 slope_(0),
46 x_intercept_(0),
47 lower_(0),
48 upper_(0),
49 t_star_(0),
50 r_squared_(0),
51 stand_dev_residuals_(0),
52 mean_residuals_(0),
53 stand_error_slope_(0),
54 chi_squared_(0),
55 rsd_(0)
56 {
57 }
58
60 virtual ~LinearRegression() = default;
61
82 void computeRegression(double confidence_interval_P,
83 std::vector<double>::const_iterator x_begin,
84 std::vector<double>::const_iterator x_end,
85 std::vector<double>::const_iterator y_begin,
86 bool compute_goodness = true);
87
109 void computeRegressionWeighted(double confidence_interval_P,
110 std::vector<double>::const_iterator x_begin,
111 std::vector<double>::const_iterator x_end,
112 std::vector<double>::const_iterator y_begin,
113 std::vector<double>::const_iterator w_begin,
114 bool compute_goodness = true);
115
117 double getIntercept() const;
119 double getSlope() const;
121 double getXIntercept() const;
123 double getLower() const;
125 double getUpper() const;
127 double getTValue() const;
129 double getRSquared() const;
131 double getStandDevRes() const;
133 double getMeanRes() const;
135 double getStandErrSlope() const;
137 double getChiSquared() const;
139 double getRSD() const;
140
142 static inline double computePointY(double x, double slope, double intercept)
143 {
144 return slope * x + intercept;
145 }
146
147protected:
148
152 double slope_;
156 double lower_;
158 double upper_;
160 double t_star_;
172 double rsd_;
173
175 void computeGoodness_(const std::vector<double>& X, const std::vector<double>& Y, double confidence_interval_P);
176
178 template <typename Iterator>
179 double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept);
180
182 template <typename Iterator>
183 double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept);
184
185private:
186
191
192 }; //class
193
194 //x, y, w must be of same size
195 template <typename Iterator>
196 double LinearRegression::computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept)
197 {
198 double chi_squared = 0.0;
199 Iterator xIter = x_begin;
200 Iterator yIter = y_begin;
201 for (; xIter != x_end; ++xIter, ++yIter)
202 {
203 chi_squared += std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
204 }
205
206 return chi_squared;
207 }
208
209 //x, y, w must be of same size
210 template <typename Iterator>
211 double LinearRegression::computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept)
212 {
213 double chi_squared = 0.0;
214 Iterator xIter = x_begin;
215 Iterator yIter = y_begin;
216 Iterator wIter = w_begin;
217 for (; xIter != x_end; ++xIter, ++yIter, ++wIter)
218 {
219 chi_squared += *wIter * std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
220 }
221
222 return chi_squared;
223 }
224 } // namespace Math
225} // namespace OpenMS
226
227
This class offers functions to perform least-squares fits to a straight line model,...
Definition LinearRegression.h:39
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:162
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:154
double getUpper() const
Non-mutable access to the upper border of confidence interval.
LinearRegression()
Constructor.
Definition LinearRegression.h:43
double lower_
The lower bound of the confidence interval.
Definition LinearRegression.h:156
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:142
double upper_
The upper bound of the confidence interval.
Definition LinearRegression.h:158
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:211
double t_star_
The value of the t-statistic.
Definition LinearRegression.h:160
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:196
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.
LinearRegression & operator=(const LinearRegression &arg)
Not implemented.
double chi_squared_
The value of the Chi Squared statistic.
Definition LinearRegression.h:170
double intercept_
The intercept of the fitted line with the y-axis.
Definition LinearRegression.h:150
double slope_
The slope of the fitted line.
Definition LinearRegression.h:152
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:166
double stand_dev_residuals_
The standard deviation of the residuals.
Definition LinearRegression.h:164
LinearRegression(const LinearRegression &arg)
Not implemented.
double rsd_
the relative standard deviation
Definition LinearRegression.h:172
double getMeanRes() const
Non-mutable access to the residual mean.
double stand_error_slope_
The standard error of the slope.
Definition LinearRegression.h:168
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