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
31using namespace pappso;
32
34 const pappso::LinearRegression &other)
35 : m_slope(other.m_slope), m_intercept(other.m_intercept), m_data(other.m_data)
36{
37}
38
39
41{
42
43 m_data = data;
44 std::size_t size = data.size();
45 if(size > 2)
46 {
47 pappso::pappso_double x_vec_mean =
48
49 (std::accumulate(data.begin(),
50 data.end(),
51 0,
52 [](double a, const DataPoint &b) { return a + b.x; }) /
53 size);
54 pappso::pappso_double y_vec_mean =
55 (sumYTrace(data.begin(), data.end(), 0) / size);
56
59 for(size_t i = 0; i < size; i++)
60 {
61 sx += std::pow((data[i].x - x_vec_mean), 2);
62 sxy += (data[i].x - x_vec_mean) * (data[i].y - y_vec_mean);
63 }
64 m_slope = sxy / sx;
65
66 m_intercept = y_vec_mean - (m_slope * x_vec_mean);
67 }
68}
69
70std::size_t
72{
73 return m_data.size();
74}
75
76
79{
80 return m_intercept;
81}
84{
85 return m_slope;
86}
89{
90 return (m_slope * x + m_intercept);
91}
92
93double
95{
96
97 std::size_t size = m_data.size();
98 if(size > 2)
99 {
100
101 pappso::pappso_double sum_square_deviation = 0;
102 for(size_t i = 0; i < size; i++)
103 {
104 sum_square_deviation +=
105 std::pow((m_data[i].y - getYfromX(m_data[i].x)), 2);
106 }
107 return sqrt(sum_square_deviation / (double)size);
108 }
109 return 0;
110}
111
112double
114{
115 return (getRmsd() / (maxYDataPoint(m_data.begin(), m_data.end())->y -
116 minYDataPoint(m_data.begin(), m_data.end())->y));
117}
118
119double
121{
122 std::size_t size = m_data.size();
123 if(size > 2)
124 {
125 double meanY = meanYTrace(m_data.begin(), m_data.end());
126 pappso::pappso_double sum_square_deviation = 0;
127 for(size_t i = 0; i < size; i++)
128 {
129 sum_square_deviation +=
130 std::pow((m_data[i].y - getYfromX(m_data[i].x)), 2);
131 }
132 pappso::pappso_double sum_square_total = 0;
133 for(size_t i = 0; i < size; i++)
134 {
135 sum_square_total += std::pow((m_data[i].y - meanY), 2);
136 }
137 return ((double)1.0 - (sum_square_deviation / sum_square_total));
138 }
139 return 0;
140}
std::size_t getSize() const
get data size
double getYfromX(double score) const
double getRmsd() const
get Root-Mean-Square Deviation
double getNrmsd() const
get Normalized Root-Mean-Square Deviation
LinearRegression(const Trace &data)
double getCoefficientOfDetermination() const
get Coefficient of determination (R2)
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:50
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