libpappsomspp
Library for mass spectrometry
linearregression.cpp
Go to the documentation of this file.
1 /**
2  * \file trace/linearregression.cpp
3  * \date 17/9/2016
4  * \author Olivier Langella
5  * \brief compute linear regression
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2016 Olivier Langella <Olivier.Langella@u-psud.fr>.
10  *
11  * This file is part of peptider.
12  *
13  * peptider is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * peptider is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with peptider. If not, see <http://www.gnu.org/licenses/>.
25  *
26  ******************************************************************************/
27 #include "linearregression.h"
28 #include <numeric>
29 #include <cmath>
30 
31 using namespace pappso;
33 {
34 
35  std::size_t size = data.size();
36  if(size > 2)
37  {
38  pappso::pappso_double x_vec_mean =
39 
40  (std::accumulate(data.begin(),
41  data.end(),
42  0,
43  [](double a, const DataPoint &b) { return a + b.x; }) /
44  size);
45  pappso::pappso_double y_vec_mean =
46  (sumYTrace(data.begin(), data.end(), 0) / size);
47 
48  pappso::pappso_double sx = 0;
49  pappso::pappso_double sxy = 0;
50  for(size_t i = 0; i < size; i++)
51  {
52  sx += std::pow((data[i].x - x_vec_mean), 2);
53  sxy += (data[i].x - x_vec_mean) * (data[i].y - y_vec_mean);
54  }
55  _slope = sxy / sx;
56 
57  _intercept = y_vec_mean - (_slope * x_vec_mean);
58  }
59 }
60 
61 
64 {
65  return _intercept;
66 }
69 {
70  return _slope;
71 }
74 {
75  return (_slope * x + _intercept);
76 }
77 
78 double
79 LinearRegression::getRmsd(const Trace &data) const
80 {
81 
82  std::size_t size = data.size();
83  if(size > 2)
84  {
85 
86  pappso::pappso_double sum_square_deviation = 0;
87  for(size_t i = 0; i < size; i++)
88  {
89  sum_square_deviation +=
90  std::pow((data[i].y - getYfromX(data[i].x)), 2);
91  }
92  return sqrt(sum_square_deviation / (double)size);
93  }
94  return 0;
95 }
96 
97 double
99 {
100  return (getRmsd(data) / (maxYDataPoint(data.begin(), data.end())->y -
101  minYDataPoint(data.begin(), data.end())->y));
102 }
103 
104 double
106 {
107  std::size_t size = data.size();
108  if(size > 2)
109  {
110  double meanY = meanYTrace(data.begin(), data.end());
111  pappso::pappso_double sum_square_deviation = 0;
112  for(size_t i = 0; i < size; i++)
113  {
114  sum_square_deviation +=
115  std::pow((data[i].y - getYfromX(data[i].x)), 2);
116  }
117  pappso::pappso_double sum_square_total = 0;
118  for(size_t i = 0; i < size; i++)
119  {
120  sum_square_total += std::pow((data[i].y - meanY), 2);
121  }
122  return ((double)1.0 - (sum_square_deviation / sum_square_total));
123  }
124  return 0;
125 }
double getNrmsd(const Trace &data) const
get Normalized Root-Mean-Square Deviation
double getYfromX(double score) const
double getCoefficientOfDetermination(const Trace &data) const
get Coefficient of determination (R2)
double getRmsd(const Trace &data) const
get Root-Mean-Square Deviation
LinearRegression(const Trace &data)
A simple container of DataPoint instances.
Definition: trace.h:148
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:180
double pappso_double
A type definition for doubles.
Definition: types.h:49
double meanYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the mean of y value of a trace
Definition: trace.cpp:253
double sumYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double init)
calculate the sum of y value of a trace
Definition: trace.cpp:244
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:158