OpenMS
StatsHelpers.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Witold Wolski $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/OPENSWATHALGO/OpenSwathAlgoConfig.h>
12 #include <algorithm>
13 #include <cmath>
14 #include <complex>
15 #include <numeric>
16 #include <vector>
17 #include <cstddef>
18 
19 namespace OpenSwath
20 {
21 
25  OPENSWATHALGO_DLLAPI void normalize(const std::vector<double>& intensities, double normalization_factor, std::vector<double>& normalized_intensities);
26 
30  template <typename T>
31  double norm(T beg, T end)
32  {
33  double res = 0.0;
34  for (; beg != end; ++beg)
35  {
36  double tmp = *beg;
37  res += tmp * tmp;
38  }
39  return sqrt(res);
40  }
41 
45  template <typename Texp, typename Ttheo>
46  double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
47  {
48  std::vector<double> res(std::distance(intExpBeg, intExpEnd));
49  std::transform(intExpBeg, intExpEnd, intTheo, res.begin(), std::multiplies<double>());
50  double sum = std::accumulate(res.begin(), res.end(), 0.);
51  return sum;
52  }
53 
61  OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector<double> intExp, std::vector<double> theorint);
62 
66  template <typename Texp, typename Ttheo>
67  double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
68  {
69  double sum = 0.0;
70  for (std::size_t i = 0; itExpBeg < itExpEnd; ++itExpBeg, ++itTheo, ++i)
71  {
72  double x = *itExpBeg - *itTheo;
73  x = fabs(x);
74  sum += x;
75  }
76  return sum;
77  }
78 
86  OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector<double> intExp, std::vector<double> theorint);
87 
88 
92  template <typename TInputIterator, typename TInputIteratorY>
93  typename std::iterator_traits<TInputIterator>::value_type cor_pearson(
94  TInputIterator xBeg,
95  TInputIterator xEnd,
96  TInputIteratorY yBeg
97  )
98  {
99  typedef typename std::iterator_traits<TInputIterator>::value_type value_type;
100  value_type m1, m2;
101  value_type s1, s2;
102  value_type corr;
103  m1 = m2 = s1 = s2 = 0.0;
104  corr = 0.0;
105  ptrdiff_t n = std::distance(xBeg, xEnd);
106  value_type nd = static_cast<value_type>(n);
107  for (; xBeg != xEnd; ++xBeg, ++yBeg)
108  {
109  corr += *xBeg * *yBeg;
110  m1 += *xBeg;
111  m2 += *yBeg;
112  s1 += *xBeg * *xBeg;
113  s2 += *yBeg * *yBeg;
114  }
115  m1 /= nd;
116  m2 /= nd;
117  s1 -= m1 * m1 * nd;
118  s2 -= m2 * m2 * nd;
119 
120  if (s1 < 1.0e-12 || s2 < 1.0e-12)
121  return 0.0;
122  else
123  {
124  corr -= m1 * m2 * (double)n;
125  corr /= sqrt(s1 * s2);
126  return corr;
127  }
128  }
129 
133  class OPENSWATHALGO_DLLAPI mean_and_stddev
134  {
135  double m_, q_;
136  unsigned long c_;
137 public:
138  typedef double argument_type, result_type;
140  m_(0.0), q_(0.0), c_(0u)
141  {
142  }
143 
144  void operator()(double sample)
145  {
146  double const delta = sample - m_;
147  m_ += delta / ++c_;
148  q_ += delta * (sample - m_);
149  }
150 
151  double sample_variance() const
152  {
153  return (c_ > 1u) ? (q_ / (c_ - 1)) : 0;
154  }
155 
156  double standard_variance() const
157  {
158  return (c_ > 1u) ? (q_ / c_) : 0;
159  }
160 
161  double sample_stddev() const
162  {
163  return std::sqrt(sample_variance());
164  }
165 
166  double standard_stddev() const
167  {
168  return std::sqrt(standard_variance());
169  }
170 
171  double mean() const
172  {
173  return m_;
174  }
175 
176  unsigned long count() const
177  {
178  return c_;
179  }
180 
181  double variance() const
182  {
183  return sample_variance();
184  }
185 
186  double stddev() const
187  {
188  return sample_stddev();
189  }
190 
191  double operator()() const
192  {
193  return stddev();
194  }
195 
196  };
197 
198 } //end namespace OpenSwath
199 
functor to compute the mean and stddev of sequence using the std::foreach algorithm
Definition: StatsHelpers.h:134
double result_type
Definition: StatsHelpers.h:138
mean_and_stddev()
Definition: StatsHelpers.h:139
double standard_stddev() const
Definition: StatsHelpers.h:166
unsigned long c_
Definition: StatsHelpers.h:136
double sample_stddev() const
Definition: StatsHelpers.h:161
double mean() const
Definition: StatsHelpers.h:171
double stddev() const
Definition: StatsHelpers.h:186
double argument_type
Definition: StatsHelpers.h:138
double variance() const
Definition: StatsHelpers.h:181
unsigned long count() const
Definition: StatsHelpers.h:176
double standard_variance() const
Definition: StatsHelpers.h:156
void operator()(double sample)
Definition: StatsHelpers.h:144
double m_
Definition: StatsHelpers.h:135
double operator()() const
Definition: StatsHelpers.h:191
double sample_variance() const
Definition: StatsHelpers.h:151
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:81
Definition: Scoring.h:18
double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
compute manhattan distance between Exp and Theo
Definition: StatsHelpers.h:67
OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector< double > intExp, std::vector< double > theorint)
the dot product scoring
OPENSWATHALGO_DLLAPI void normalize(const std::vector< double > &intensities, double normalization_factor, std::vector< double > &normalized_intensities)
Normalize intensities in vector by normalization_factor.
double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
compute dotprod of vectors
Definition: StatsHelpers.h:46
OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector< double > intExp, std::vector< double > theorint)
manhattan scoring
double norm(T beg, T end)
compute the Euclidean norm of the vector
Definition: StatsHelpers.h:31
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:93