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

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:21 using doxygen 1.8.5