| OpenMS 2.5.0

BasicStatistics.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-2020.
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: $
33 // --------------------------------------------------------------------------
34
35 #pragma once
36
37 #include <OpenMS/CONCEPT/Types.h>
38 #include <vector>
39 #include <ostream>
40 #include <cmath>
41 #include <numeric>
42
43 namespace OpenMS
44 {
45 namespace Math
46 {
47
66 template <typename RealT = double>
67 class
68 BasicStatistics
69 {
70
71 public:
72
74 typedef RealT RealType;
75
76 typedef std::vector<RealType> probability_container;
77 typedef std::vector<RealType> coordinate_container;
78
80 BasicStatistics() :
81 mean_(0),
82 variance_(0),
83 sum_(0)
84 {}
85
87 BasicStatistics(BasicStatistics const & arg) :
88 mean_(arg.mean_),
89 variance_(arg.variance_),
90 sum_(arg.sum_)
91 {}
92
94 BasicStatistics & operator=(BasicStatistics const & arg)
95 {
96 mean_ = arg.mean_;
97 variance_ = arg.variance_;
98 sum_ = arg.sum_;
99 return *this;
100 }
101
103 void clear()
104 {
105 mean_ = 0;
106 variance_ = 0;
107 sum_ = 0;
108 }
109
111
112 template <typename ProbabilityIterator>
113 void update(ProbabilityIterator probability_begin,
114 ProbabilityIterator const probability_end
115 )
116 {
117 clear();
118 unsigned pos = 0;
119 ProbabilityIterator iter = probability_begin;
120
121 for (; iter != probability_end; ++iter, ++pos)
122 {
123 sum_ += *iter;
124 mean_ += *iter * pos;
125 }
126 mean_ /= sum_;
127
128 for (iter = probability_begin, pos = 0; iter != probability_end; ++iter, ++pos)
129 {
130 RealType diff = RealType(pos) - mean_;
131 diff *= diff;
132 variance_ += *iter * diff;
133 }
134 variance_ /= sum_;
135 }
136
138
139 template <typename ProbabilityIterator, typename CoordinateIterator>
140 void update(ProbabilityIterator const probability_begin,
141 ProbabilityIterator const probability_end,
142 CoordinateIterator const coordinate_begin
143 )
144 {
145 clear();
146 ProbabilityIterator prob_iter = probability_begin;
147 CoordinateIterator coord_iter = coordinate_begin;
148
149 for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
150 {
151 sum_ += *prob_iter;
152 mean_ += *prob_iter * *coord_iter;
153 }
154 mean_ /= sum_;
155
156 for (prob_iter = probability_begin, coord_iter = coordinate_begin;
157 prob_iter != probability_end;
158 ++prob_iter, ++coord_iter
159 )
160 {
161 RealType diff = *coord_iter - mean_;
162 diff *= diff;
163 variance_ += *prob_iter * diff;
164 }
165 variance_ /= sum_;
166 return;
167 }
168
170 RealType mean() const { return mean_; }
171 void setMean(RealType const & mean) { mean_ = mean; }
172
174 RealType variance() const { return variance_; }
175 void setVariance(RealType const & variance) { variance_ = variance; }
176
178 RealType sum() const { return sum_; }
179 void setSum(RealType const & sum) { sum_ = sum; }
180
181
186 RealType normalDensity_sqrt2pi(RealType coordinate) const
187 {
188 coordinate -= mean();
189 coordinate *= coordinate;
190 return exp(-coordinate / RealType(2) / variance());
191 }
192
194 static RealType sqrt2pi() { return 2.50662827463100050240; }
195
199 inline RealType normalDensity(RealType const coordinate) const
200 {
201 return normalDensity_sqrt2pi(coordinate) / sqrt2pi();
202 }
203
208 void normalApproximation(probability_container & probability)
209 {
210 normalApproximationHelper_(probability, probability.size());
211 return;
212 }
213
218 void normalApproximation(probability_container & probability,
219 typename probability_container::size_type const size
220 )
221 {
222 probability.resize(size);
223 normalApproximationHelper_(probability, size);
224 return;
225 }
226
232 void normalApproximation(probability_container & probability,
233 coordinate_container const & coordinate
234 )
235 {
236 probability.resize(coordinate.size());
237 normalApproximationHelper_(probability, coordinate);
238 return;
239 }
240
245 friend std::ostream & operator<<(std::ostream & os, BasicStatistics & arg)
246 {
247 os << "BasicStatistics: mean=" << arg.mean() << " variance=" << arg.variance() << " sum=" << arg.sum();
248 return os;
249 }
250
251 protected:
252
254
255
256 RealType mean_;
257 RealType variance_;
258 RealType sum_;
259
260 private:
262
264
265
266 void normalApproximationHelper_(probability_container & probability,
267 typename probability_container::size_type const size
268 )
269 {
270 RealType gaussSum = 0;
271 typename coordinate_container::size_type i;
272
273 // precondition size == probability.size() is guaranteed by wrappers.
274 for (i = 0; i < size; ++i)
275 {
276 gaussSum += normalDensity_sqrt2pi(RealType(i));
277 }
278
279 for (i = 0; i < size; ++i)
280 {
281 probability[i] = normalDensity_sqrt2pi(RealType(i)) / gaussSum * sum();
282 }
283 return;
284 }
285
286 void normalApproximationHelper_(probability_container & probability,
287 coordinate_container const & coordinate
288 )
289 {
290 RealType gaussSum = 0;
291 typename coordinate_container::size_type i;
292 typename coordinate_container::size_type const size = coordinate.size();
293
294 for (i = 0; i < size; ++i)
295 {
296 gaussSum += normalDensity_sqrt2pi(coordinate[i]);
297 }
298
299 for (i = 0; i < size; ++i)
300 {
301 probability[i] = normalDensity_sqrt2pi(coordinate[i]) / gaussSum * sum();
302 }
303 return;
304 }
305
307
308 };
309
310 } // namespace Math
311
312 } // namespace OpenMS
313
OpenMS::Math::BasicStatistics::variance_
RealType variance_
Definition: BasicStatistics.h:257
OpenMS::Math::BasicStatistics::normalDensity_sqrt2pi
RealType normalDensity_sqrt2pi(RealType coordinate) const
Returns the density of the normal approximation at point, multiplied by sqrt( 2 * pi )...
Definition: BasicStatistics.h:186
OpenMS::Math::BasicStatistics::sum_
RealType sum_
Definition: BasicStatistics.h:258
OpenMS::Math::BasicStatistics::probability_container
std::vector< RealType > probability_container
Definition: BasicStatistics.h:76
OpenMS::Math::BasicStatistics::RealType
RealT RealType
The real type specified as template argument.
Definition: BasicStatistics.h:74
OpenMS::Math::BasicStatistics::clear
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:103
OpenMS::Math::BasicStatistics
Calculates some basic statistical parameters of a distribution: sum, mean, variance, and provides the normal approximation.
Definition: BasicStatistics.h:67
OpenMS::Math::BasicStatistics::normalApproximation
void normalApproximation(probability_container &probability)
The argument probability is filled with values according to the normal approximation. Its size() is not changed. The approximation takes place at coordinate positions 0, 1, ..., size()-1.
Definition: BasicStatistics.h:208
OpenMS::Math::BasicStatistics::sum
RealType sum() const
Returns the sum.
Definition: BasicStatistics.h:178
OpenMS::Math::mean
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:133
Types.h
OpenMS::Math::BasicStatistics::mean
RealType mean() const
Returns the mean.
Definition: BasicStatistics.h:170
OpenMS::Math::BasicStatistics::setSum
void setSum(RealType const &sum)
Definition: BasicStatistics.h:179
OpenMS::Math::variance
static double variance(IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
Calculates the variance of a range of values.
Definition: StatisticFunctions.h:278
OpenMS::Math::BasicStatistics::variance
RealType variance() const
Returns the variance.
Definition: BasicStatistics.h:174
OpenMS::Math::BasicStatistics::coordinate_container
std::vector< RealType > coordinate_container
Definition: BasicStatistics.h:77
OpenMS::Math::BasicStatistics::update
void update(ProbabilityIterator const probability_begin, ProbabilityIterator const probability_end, CoordinateIterator const coordinate_begin)
This does the actual calculation.
Definition: BasicStatistics.h:140
OpenMS::Math::BasicStatistics::setMean
void setMean(RealType const &mean)
Definition: BasicStatistics.h:171
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::Math::BasicStatistics::normalDensity
RealType normalDensity(RealType const coordinate) const
See normalDensity_sqrt2pi(). Returns the density of the normal distribution at point.
Definition: BasicStatistics.h:199
Math
OpenMS::Math::BasicStatistics::normalApproximation
void normalApproximation(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:218
OpenMS::Math::BasicStatistics::setVariance
void setVariance(RealType const &variance)
Definition: BasicStatistics.h:175
OpenMS::Math::BasicStatistics::sqrt2pi
static RealType sqrt2pi()
Returns sqrt( 2 * pi ), which is useful to normalize the result of normalDensity_sqrt2pi().
Definition: BasicStatistics.h:194
OpenMS::Math::BasicStatistics::BasicStatistics
BasicStatistics()
Default constructor.
Definition: BasicStatistics.h:80
OpenMS::Math::BasicStatistics::normalApproximationHelper_
void normalApproximationHelper_(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:266
OpenMS::Math::BasicStatistics::BasicStatistics
BasicStatistics(BasicStatistics const &arg)
Copy constructor.
Definition: BasicStatistics.h:87
OpenMS::Math::BasicStatistics::normalApproximationHelper_
void normalApproximationHelper_(probability_container &probability, coordinate_container const &coordinate)
Definition: BasicStatistics.h:286
OpenMS::Math::sum
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
OpenMS::Math::BasicStatistics::normalApproximation
void normalApproximation(probability_container &probability, coordinate_container const &coordinate)
The argument probability is filled with values according to the normal approximation. The second argument coordinate contains the positions where the approximation takes place. probability.size() is set to coordinate.size().
Definition: BasicStatistics.h:232
OpenMS::Math::BasicStatistics::operator=
BasicStatistics & operator=(BasicStatistics const &arg)
Assignment.
Definition: BasicStatistics.h:94
OpenMS::Math::BasicStatistics::operator<<
friend std::ostream & operator<<(std::ostream &os, BasicStatistics &arg)
A convenient overload for debugging purposes.
Definition: BasicStatistics.h:245
OpenMS::Math::BasicStatistics::mean_
RealType mean_
Definition: BasicStatistics.h:256
OpenMS::Math::BasicStatistics::update
void update(ProbabilityIterator probability_begin, ProbabilityIterator const probability_end)
This does the actual calculation.
Definition: BasicStatistics.h:113