OpenMS  2.7.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-2021.
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
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 
81  mean_(0),
82  variance_(0),
83  sum_(0)
84  {}
85 
88  mean_(arg.mean_),
89  variance_(arg.variance_),
90  sum_(arg.sum_)
91  {}
92 
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 
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 
209  {
210  normalApproximationHelper_(probability, probability.size());
211  return;
212  }
213 
219  typename probability_container::size_type const size
220  )
221  {
222  probability.resize(size);
223  normalApproximationHelper_(probability, size);
224  return;
225  }
226 
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 
259 
260 private:
262 
264 
265 
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 
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 
Calculates some basic statistical parameters of a distribution: sum, mean, variance,...
Definition: BasicStatistics.h:69
void normalApproximationHelper_(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:266
RealType normalDensity(RealType const coordinate) const
See normalDensity_sqrt2pi(). Returns the density of the normal distribution at point.
Definition: BasicStatistics.h:199
std::vector< RealType > coordinate_container
Definition: BasicStatistics.h:77
BasicStatistics()
Default constructor.
Definition: BasicStatistics.h:80
BasicStatistics(BasicStatistics const &arg)
Copy constructor.
Definition: BasicStatistics.h:87
void setMean(RealType const &mean)
Definition: BasicStatistics.h:171
RealT RealType
The real type specified as template argument.
Definition: BasicStatistics.h:74
void normalApproximation(probability_container &probability)
The argument probability is filled with values according to the normal approximation....
Definition: BasicStatistics.h:208
void normalApproximation(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:218
static RealType sqrt2pi()
Returns sqrt( 2 * pi ), which is useful to normalize the result of normalDensity_sqrt2pi().
Definition: BasicStatistics.h:194
void update(ProbabilityIterator probability_begin, ProbabilityIterator const probability_end)
This does the actual calculation.
Definition: BasicStatistics.h:113
RealType mean() const
Returns the mean.
Definition: BasicStatistics.h:170
void update(ProbabilityIterator const probability_begin, ProbabilityIterator const probability_end, CoordinateIterator const coordinate_begin)
This does the actual calculation.
Definition: BasicStatistics.h:140
BasicStatistics & operator=(BasicStatistics const &arg)
Assignment.
Definition: BasicStatistics.h:94
friend std::ostream & operator<<(std::ostream &os, BasicStatistics &arg)
A convenient overload for debugging purposes.
Definition: BasicStatistics.h:245
RealType sum_
Definition: BasicStatistics.h:258
void normalApproximation(probability_container &probability, coordinate_container const &coordinate)
The argument probability is filled with values according to the normal approximation....
Definition: BasicStatistics.h:232
RealType normalDensity_sqrt2pi(RealType coordinate) const
Returns the density of the normal approximation at point, multiplied by sqrt( 2 * pi )....
Definition: BasicStatistics.h:186
RealType variance() const
Returns the variance.
Definition: BasicStatistics.h:174
std::vector< RealType > probability_container
Definition: BasicStatistics.h:76
void setSum(RealType const &sum)
Definition: BasicStatistics.h:179
void normalApproximationHelper_(probability_container &probability, coordinate_container const &coordinate)
Definition: BasicStatistics.h:286
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:103
RealType sum() const
Returns the sum.
Definition: BasicStatistics.h:178
RealType variance_
Definition: BasicStatistics.h:257
RealType mean_
Definition: BasicStatistics.h:256
void setVariance(RealType const &variance)
Definition: BasicStatistics.h:175
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:133
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
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:307
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47