OpenMS  2.5.0
StatsHelpers.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-2020.
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: Witold Wolski $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/OPENSWATHALGO/OpenSwathAlgoConfig.h>
38 #include <algorithm>
39 #include <cmath>
40 #include <complex>
41 #include <numeric>
42 #include <vector>
43 #include <cstddef>
44 
45 namespace OpenSwath
46 {
47 
51  OPENSWATHALGO_DLLAPI void normalize(const std::vector<double>& intensities, double normalization_factor, std::vector<double>& normalized_intensities);
52 
56  template <typename T>
57  double norm(T beg, T end)
58  {
59  double res = 0.0;
60  for (; beg != end; ++beg)
61  {
62  double tmp = *beg;
63  res += tmp * tmp;
64  }
65  return sqrt(res);
66  }
67 
68  struct mySqrt :
69  std::unary_function<double, double>
70  {
71  double operator()(double x)
72  {
73  return sqrt(x);
74  }
75 
76  };
77 
81  template <typename Texp, typename Ttheo>
82  double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
83  {
84  std::vector<double> res(std::distance(intExpBeg, intExpEnd));
85  std::transform(intExpBeg, intExpEnd, intTheo, res.begin(), std::multiplies<double>());
86  double sum = std::accumulate(res.begin(), res.end(), 0.);
87  return sum;
88  }
89 
97  OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector<double> intExp, std::vector<double> theorint);
98 
102  template <typename Texp, typename Ttheo>
103  double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
104  {
105  double sum = 0.0;
106  for (std::size_t i = 0; itExpBeg < itExpEnd; ++itExpBeg, ++itTheo, ++i)
107  {
108  double x = *itExpBeg - *itTheo;
109  x = fabs(x);
110  sum += x;
111  }
112  return sum;
113  }
114 
122  OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector<double> intExp, std::vector<double> theorint);
123 
124 
128  template <typename TInputIterator, typename TInputIteratorY>
129  typename std::iterator_traits<TInputIterator>::value_type cor_pearson(
130  TInputIterator xBeg,
131  TInputIterator xEnd,
132  TInputIteratorY yBeg
133  )
134  {
135  typedef typename std::iterator_traits<TInputIterator>::value_type value_type;
136  value_type m1, m2;
137  value_type s1, s2;
138  value_type corr;
139  m1 = m2 = s1 = s2 = 0.0;
140  corr = 0.0;
141  ptrdiff_t n = std::distance(xBeg, xEnd);
142  value_type nd = static_cast<value_type>(n);
143  for (; xBeg != xEnd; ++xBeg, ++yBeg)
144  {
145  corr += *xBeg * *yBeg;
146  m1 += *xBeg;
147  m2 += *yBeg;
148  s1 += *xBeg * *xBeg;
149  s2 += *yBeg * *yBeg;
150  }
151  m1 /= nd;
152  m2 /= nd;
153  s1 -= m1 * m1 * nd;
154  s2 -= m2 * m2 * nd;
155 
156  if (s1 < 1.0e-12 || s2 < 1.0e-12)
157  return 0.0;
158  else
159  {
160  corr -= m1 * m2 * (double)n;
161  corr /= sqrt(s1 * s2);
162  return corr;
163  }
164  }
165 
169  class OPENSWATHALGO_DLLAPI mean_and_stddev
170  {
171  double m_, q_;
172  unsigned long c_;
173 public:
174  typedef double argument_type, result_type;
176  m_(0.0), q_(0.0), c_(0u)
177  {
178  }
179 
180  void operator()(double sample)
181  {
182  double const delta = sample - m_;
183  m_ += delta / ++c_;
184  q_ += delta * (sample - m_);
185  }
186 
187  double sample_variance() const
188  {
189  return (c_ > 1u) ? (q_ / (c_ - 1)) : 0;
190  }
191 
192  double standard_variance() const
193  {
194  return (c_ > 1u) ? (q_ / c_) : 0;
195  }
196 
197  double sample_stddev() const
198  {
199  return std::sqrt(sample_variance());
200  }
201 
202  double standard_stddev() const
203  {
204  return std::sqrt(standard_variance());
205  }
206 
207  double mean() const
208  {
209  return m_;
210  }
211 
212  unsigned long count() const
213  {
214  return c_;
215  }
216 
217  double variance() const
218  {
219  return sample_variance();
220  }
221 
222  double stddev() const
223  {
224  return sample_stddev();
225  }
226 
227  double operator()() const
228  {
229  return stddev();
230  }
231 
232  };
233 
234 } //end namespace OpenSwath
235 
OpenSwath::mean_and_stddev::standard_stddev
double standard_stddev() const
Definition: StatsHelpers.h:202
OpenMS::Math::sum
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
double
OpenSwath
Definition: MRMScoring.h:49
OpenSwath::mySqrt
Definition: StatsHelpers.h:68
OpenSwath::cor_pearson
std::iterator_traits< TInputIterator >::value_type cor_pearson(TInputIterator xBeg, TInputIterator xEnd, TInputIteratorY yBeg)
compute pearson correlation of vector x and y
Definition: StatsHelpers.h:129
OpenSwath::norm
double norm(T beg, T end)
compute the norm of the vector
Definition: StatsHelpers.h:57
OpenSwath::dotprodScoring
OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector< double > intExp, std::vector< double > theorint)
the dot product scoring
OpenSwath::mean_and_stddev::operator()
void operator()(double sample)
Definition: StatsHelpers.h:180
OpenSwath::mean_and_stddev::operator()
double operator()() const
Definition: StatsHelpers.h:227
OpenSwath::mean_and_stddev::standard_variance
double standard_variance() const
Definition: StatsHelpers.h:192
OpenSwath::mean_and_stddev
functor to compute the mean and stddev of sequence using the std::foreach algorithm
Definition: StatsHelpers.h:169
OpenSwath::mean_and_stddev::q_
double q_
Definition: StatsHelpers.h:171
OpenSwath::mean_and_stddev::sample_variance
double sample_variance() const
Definition: StatsHelpers.h:187
OpenSwath::mean_and_stddev::c_
unsigned long c_
Definition: StatsHelpers.h:172
OpenSwath::mean_and_stddev::mean
double mean() const
Definition: StatsHelpers.h:207
OpenSwath::mySqrt::operator()
double operator()(double x)
Definition: StatsHelpers.h:71
OpenSwath::mean_and_stddev::result_type
double result_type
Definition: StatsHelpers.h:174
OpenSwath::manhattanDist
double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
compute manhattan distance between Exp and Theo
Definition: StatsHelpers.h:103
OpenSwath::dotProd
double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
compute dotprod of vectors
Definition: StatsHelpers.h:82
OpenSwath::manhattanScoring
OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector< double > intExp, std::vector< double > theorint)
manhattan scoring
OpenSwath::normalize
OPENSWATHALGO_DLLAPI void normalize(const std::vector< double > &intensities, double normalization_factor, std::vector< double > &normalized_intensities)
Normalize intensities in vector by normalization_factor.
OpenSwath::mean_and_stddev::count
unsigned long count() const
Definition: StatsHelpers.h:212
OpenSwath::mean_and_stddev::stddev
double stddev() const
Definition: StatsHelpers.h:222
OpenSwath::mean_and_stddev::sample_stddev
double sample_stddev() const
Definition: StatsHelpers.h:197
OpenSwath::mean_and_stddev::mean_and_stddev
mean_and_stddev()
Definition: StatsHelpers.h:175
OpenSwath::mean_and_stddev::variance
double variance() const
Definition: StatsHelpers.h:217