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

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:04 using doxygen 1.8.13