OpenMS
Loading...
Searching...
No Matches
StatsHelpers.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
19namespace 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
49
50 // Explicit template instantiation declarations (tell the compiler these exist)
51 extern template double dotProd<std::vector<double>::const_iterator, std::vector<double>::const_iterator>(
52 std::vector<double>::const_iterator, std::vector<double>::const_iterator, std::vector<double>::const_iterator);
53
54 extern template double dotProd<std::vector<float>::const_iterator, std::vector<float>::const_iterator>(
55 std::vector<float>::const_iterator, std::vector<float>::const_iterator, std::vector<float>::const_iterator);
56
57 extern template double dotProd<std::vector<int>::const_iterator, std::vector<int>::const_iterator>(
58 std::vector<int>::const_iterator, std::vector<int>::const_iterator, std::vector<int>::const_iterator);
59
61
69 OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector<double> intExp, std::vector<double> theorint);
70
74 template <typename Texp, typename Ttheo>
75 double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
76 {
77 double sum = 0.0;
78 for (; itExpBeg < itExpEnd; ++itExpBeg, ++itTheo)
79 {
80 sum += fabs(*itExpBeg - *itTheo);
81 }
82 return sum;
83 }
84
92 OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector<double> intExp, std::vector<double> theorint);
93
94
98 template <typename TInputIterator, typename TInputIteratorY>
99 typename std::iterator_traits<TInputIterator>::value_type cor_pearson(
100 TInputIterator xBeg,
101 TInputIterator xEnd,
102 TInputIteratorY yBeg
103 )
104 {
105 typedef typename std::iterator_traits<TInputIterator>::value_type value_type;
106 value_type m1, m2;
107 value_type s1, s2;
108 value_type corr;
109 m1 = m2 = s1 = s2 = 0.0;
110 corr = 0.0;
111 ptrdiff_t n = std::distance(xBeg, xEnd);
112 value_type nd = static_cast<value_type>(n);
113 for (; xBeg != xEnd; ++xBeg, ++yBeg)
114 {
115 corr += *xBeg * *yBeg;
116 m1 += *xBeg;
117 m2 += *yBeg;
118 s1 += *xBeg * *xBeg;
119 s2 += *yBeg * *yBeg;
120 }
121 m1 /= nd;
122 m2 /= nd;
123 s1 -= m1 * m1 * nd;
124 s2 -= m2 * m2 * nd;
125
126 if (s1 < 1.0e-12 || s2 < 1.0e-12)
127 return 0.0;
128 else
129 {
130 corr -= m1 * m2 * (double)n;
131 corr /= sqrt(s1 * s2);
132 return corr;
133 }
134 }
135
139 class OPENSWATHALGO_DLLAPI mean_and_stddev
140 {
141 double m_, q_;
142 unsigned long c_;
143public:
144 typedef double argument_type, result_type;
146 m_(0.0), q_(0.0), c_(0u)
147 {
148 }
149
150 void operator()(double sample)
151 {
152 double const delta = sample - m_;
153 m_ += delta / ++c_;
154 q_ += delta * (sample - m_);
155 }
156
157 double sample_variance() const
158 {
159 return (c_ > 1u) ? (q_ / (c_ - 1)) : 0;
160 }
161
162 double standard_variance() const
163 {
164 return (c_ > 1u) ? (q_ / c_) : 0;
165 }
166
167 double sample_stddev() const
168 {
169 return std::sqrt(sample_variance());
170 }
171
172 double standard_stddev() const
173 {
174 return std::sqrt(standard_variance());
175 }
176
177 double mean() const
178 {
179 return m_;
180 }
181
182 unsigned long count() const
183 {
184 return c_;
185 }
186
187 double variance() const
188 {
189 return sample_variance();
190 }
191
192 double stddev() const
193 {
194 return sample_stddev();
195 }
196
197 double operator()() const
198 {
199 return stddev();
200 }
201
202 };
203
204} //end namespace OpenSwath
205
functor to compute the mean and stddev of sequence using the std::foreach algorithm
Definition StatsHelpers.h:140
double result_type
Definition StatsHelpers.h:144
mean_and_stddev()
Definition StatsHelpers.h:145
double standard_stddev() const
Definition StatsHelpers.h:172
unsigned long c_
Definition StatsHelpers.h:142
double sample_stddev() const
Definition StatsHelpers.h:167
double mean() const
Definition StatsHelpers.h:177
double stddev() const
Definition StatsHelpers.h:192
double argument_type
Definition StatsHelpers.h:144
double variance() const
Definition StatsHelpers.h:187
unsigned long count() const
Definition StatsHelpers.h:182
double standard_variance() const
Definition StatsHelpers.h:162
void operator()(double sample)
Definition StatsHelpers.h:150
double m_
Definition StatsHelpers.h:141
double operator()() const
Definition StatsHelpers.h:197
double sample_variance() const
Definition StatsHelpers.h:157
Definition Scoring.h:18
double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo)
compute dotprod of vectors
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.
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:99
double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
compute manhattan distance between Exp and Theo
Definition StatsHelpers.h:75