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
34
35 // Explicit template instantiation declarations
36 extern template double norm<std::vector<double>::const_iterator>(
37 std::vector<double>::const_iterator, std::vector<double>::const_iterator);
38
39 extern template double norm<std::vector<double>::iterator>(
40 std::vector<double>::iterator, std::vector<double>::iterator);
41
43
47 template <typename Texp, typename Ttheo>
48 double dotProd(Texp intExpBeg, Texp intExpEnd, Ttheo intTheo);
49
51
52 // Explicit template instantiation declarations (tell the compiler these exist)
53 extern template double dotProd<std::vector<double>::const_iterator, std::vector<double>::const_iterator>(
54 std::vector<double>::const_iterator, std::vector<double>::const_iterator, std::vector<double>::const_iterator);
55
56 extern template double dotProd<std::vector<float>::const_iterator, std::vector<float>::const_iterator>(
57 std::vector<float>::const_iterator, std::vector<float>::const_iterator, std::vector<float>::const_iterator);
58
59 extern template double dotProd<std::vector<int>::const_iterator, std::vector<int>::const_iterator>(
60 std::vector<int>::const_iterator, std::vector<int>::const_iterator, std::vector<int>::const_iterator);
61
63
71 OPENSWATHALGO_DLLAPI double dotprodScoring(std::vector<double> intExp, std::vector<double> theorint);
72
76 template <typename Texp, typename Ttheo>
77 double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo);
78
80
81 // Explicit template instantiation declarations
82 extern template double manhattanDist<std::vector<double>::iterator, std::vector<double>::iterator>(
83 std::vector<double>::iterator, std::vector<double>::iterator, std::vector<double>::iterator);
84
85 extern template double manhattanDist<std::vector<double>::const_iterator, std::vector<double>::const_iterator>(
86 std::vector<double>::const_iterator, std::vector<double>::const_iterator, std::vector<double>::const_iterator);
87
89
97 OPENSWATHALGO_DLLAPI double manhattanScoring(std::vector<double> intExp, std::vector<double> theorint);
98
99
104 template <typename TInputIterator, typename TInputIteratorY>
105 [[deprecated("Use Math::pearsonCorrelationCoefficient instead")]]
106 typename std::iterator_traits<TInputIterator>::value_type cor_pearson(
107 TInputIterator xBeg,
108 TInputIterator xEnd,
109 TInputIteratorY yBeg
110 )
111 {
112 typedef typename std::iterator_traits<TInputIterator>::value_type value_type;
113 value_type m1, m2;
114 value_type s1, s2;
115 value_type corr;
116 m1 = m2 = s1 = s2 = 0.0;
117 corr = 0.0;
118 ptrdiff_t n = std::distance(xBeg, xEnd);
119 value_type nd = static_cast<value_type>(n);
120 for (; xBeg != xEnd; ++xBeg, ++yBeg)
121 {
122 corr += *xBeg * *yBeg;
123 m1 += *xBeg;
124 m2 += *yBeg;
125 s1 += *xBeg * *xBeg;
126 s2 += *yBeg * *yBeg;
127 }
128 m1 /= nd;
129 m2 /= nd;
130 s1 -= m1 * m1 * nd;
131 s2 -= m2 * m2 * nd;
132
133 if (s1 < 1.0e-12 || s2 < 1.0e-12)
134 return 0.0;
135 else
136 {
137 corr -= m1 * m2 * (double)n;
138 corr /= sqrt(s1 * s2);
139 return corr;
140 }
141 }
142
146 class OPENSWATHALGO_DLLAPI mean_and_stddev
147 {
148 double m_, q_;
149 unsigned long c_;
150public:
151 typedef double argument_type, result_type;
153 m_(0.0), q_(0.0), c_(0u)
154 {
155 }
156
157 void operator()(double sample)
158 {
159 double const delta = sample - m_;
160 m_ += delta / ++c_;
161 q_ += delta * (sample - m_);
162 }
163
164 double sample_variance() const
165 {
166 return (c_ > 1u) ? (q_ / (c_ - 1)) : 0;
167 }
168
169 double standard_variance() const
170 {
171 return (c_ > 1u) ? (q_ / c_) : 0;
172 }
173
174 double sample_stddev() const
175 {
176 return std::sqrt(sample_variance());
177 }
178
179 double standard_stddev() const
180 {
181 return std::sqrt(standard_variance());
182 }
183
184 double mean() const
185 {
186 return m_;
187 }
188
189 unsigned long count() const
190 {
191 return c_;
192 }
193
194 double variance() const
195 {
196 return sample_variance();
197 }
198
199 double stddev() const
200 {
201 return sample_stddev();
202 }
203
204 double operator()() const
205 {
206 return stddev();
207 }
208
209 };
210
211} //end namespace OpenSwath
212
functor to compute the mean and stddev of sequence using the std::foreach algorithm
Definition StatsHelpers.h:147
double result_type
Definition StatsHelpers.h:151
mean_and_stddev()
Definition StatsHelpers.h:152
double standard_stddev() const
Definition StatsHelpers.h:179
unsigned long c_
Definition StatsHelpers.h:149
double sample_stddev() const
Definition StatsHelpers.h:174
double mean() const
Definition StatsHelpers.h:184
double stddev() const
Definition StatsHelpers.h:199
double argument_type
Definition StatsHelpers.h:151
double variance() const
Definition StatsHelpers.h:194
unsigned long count() const
Definition StatsHelpers.h:189
double standard_variance() const
Definition StatsHelpers.h:169
void operator()(double sample)
Definition StatsHelpers.h:157
double m_
Definition StatsHelpers.h:148
double operator()() const
Definition StatsHelpers.h:204
double sample_variance() const
Definition StatsHelpers.h:164
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
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:106
double manhattanDist(Texp itExpBeg, Texp itExpEnd, Ttheo itTheo)
compute manhattan distance between Exp and Theo