OpenMS
BasicStatistics.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: $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Types.h>
12 #include <vector>
13 #include <ostream>
14 #include <cmath>
15 #include <numeric>
16 
17 namespace OpenMS
18 {
19  namespace Math
20  {
21 
40  template <typename RealT = double>
41  class
43  {
44 
45 public:
46 
48  typedef RealT RealType;
49 
50  typedef std::vector<RealType> probability_container;
51  typedef std::vector<RealType> coordinate_container;
52 
55  mean_(0),
56  variance_(0),
57  sum_(0)
58  {}
59 
62  mean_(arg.mean_),
63  variance_(arg.variance_),
64  sum_(arg.sum_)
65  {}
66 
69  {
70  mean_ = arg.mean_;
71  variance_ = arg.variance_;
72  sum_ = arg.sum_;
73  return *this;
74  }
75 
77  void clear()
78  {
79  mean_ = 0;
80  variance_ = 0;
81  sum_ = 0;
82  }
83 
85 
86  template <typename ProbabilityIterator>
87  void update(ProbabilityIterator probability_begin,
88  ProbabilityIterator const probability_end
89  )
90  {
91  clear();
92  unsigned pos = 0;
93  ProbabilityIterator iter = probability_begin;
94 
95  for (; iter != probability_end; ++iter, ++pos)
96  {
97  sum_ += *iter;
98  mean_ += *iter * pos;
99  }
100  mean_ /= sum_;
101 
102  for (iter = probability_begin, pos = 0; iter != probability_end; ++iter, ++pos)
103  {
104  RealType diff = RealType(pos) - mean_;
105  diff *= diff;
106  variance_ += *iter * diff;
107  }
108  variance_ /= sum_;
109 
110  if (sum_ == 0 && (std::isnan(mean_) || std::isinf(mean_)) )
111  {
112  mean_ = 0;
113  variance_ = 0;
114  }
115  }
116 
118 
119  template <typename ProbabilityIterator, typename CoordinateIterator>
120  void update(ProbabilityIterator const probability_begin,
121  ProbabilityIterator const probability_end,
122  CoordinateIterator const coordinate_begin
123  )
124  {
125  clear();
126  ProbabilityIterator prob_iter = probability_begin;
127  CoordinateIterator coord_iter = coordinate_begin;
128 
129  for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
130  {
131  sum_ += *prob_iter;
132  mean_ += *prob_iter * *coord_iter;
133  }
134  mean_ /= sum_;
135 
136  for (prob_iter = probability_begin, coord_iter = coordinate_begin;
137  prob_iter != probability_end;
138  ++prob_iter, ++coord_iter
139  )
140  {
141  RealType diff = *coord_iter - mean_;
142  diff *= diff;
143  variance_ += *prob_iter * diff;
144  }
145  variance_ /= sum_;
146  return;
147  }
148 
150  RealType mean() const { return mean_; }
151  void setMean(RealType const & mean) { mean_ = mean; }
152 
154  RealType variance() const { return variance_; }
155  void setVariance(RealType const & variance) { variance_ = variance; }
156 
158  RealType sum() const { return sum_; }
159  void setSum(RealType const & sum) { sum_ = sum; }
160 
161 
167  {
168  coordinate -= mean();
169  coordinate *= coordinate;
170  return exp(-coordinate / RealType(2) / variance());
171  }
172 
174  static RealType sqrt2pi() { return 2.50662827463100050240; }
175 
179  inline RealType normalDensity(RealType const coordinate) const
180  {
181  return normalDensity_sqrt2pi(coordinate) / sqrt2pi();
182  }
183 
189  {
190  normalApproximationHelper_(probability, probability.size());
191  return;
192  }
193 
199  typename probability_container::size_type const size
200  )
201  {
202  probability.resize(size);
203  normalApproximationHelper_(probability, size);
204  return;
205  }
206 
213  coordinate_container const & coordinate
214  )
215  {
216  probability.resize(coordinate.size());
217  normalApproximationHelper_(probability, coordinate);
218  return;
219  }
220 
225  friend std::ostream & operator<<(std::ostream & os, BasicStatistics & arg)
226  {
227  os << "BasicStatistics: mean=" << arg.mean() << " variance=" << arg.variance() << " sum=" << arg.sum();
228  return os;
229  }
230 
231 protected:
232 
234 
235 
239 
240 private:
242 
244 
245 
247  typename probability_container::size_type const size
248  )
249  {
250  RealType gaussSum = 0;
251  typename coordinate_container::size_type i;
252 
253  // precondition size == probability.size() is guaranteed by wrappers.
254  for (i = 0; i < size; ++i)
255  {
256  gaussSum += normalDensity_sqrt2pi(RealType(i));
257  }
258 
259  for (i = 0; i < size; ++i)
260  {
261  probability[i] = normalDensity_sqrt2pi(RealType(i)) / gaussSum * sum();
262  }
263  return;
264  }
265 
267  coordinate_container const & coordinate
268  )
269  {
270  RealType gaussSum = 0;
271  typename coordinate_container::size_type i;
272  typename coordinate_container::size_type const size = coordinate.size();
273 
274  for (i = 0; i < size; ++i)
275  {
276  gaussSum += normalDensity_sqrt2pi(coordinate[i]);
277  }
278 
279  for (i = 0; i < size; ++i)
280  {
281  probability[i] = normalDensity_sqrt2pi(coordinate[i]) / gaussSum * sum();
282  }
283  return;
284  }
285 
287 
288  };
289 
290  } // namespace Math
291 
292 } // namespace OpenMS
Calculates some basic statistical parameters of a distribution: sum, mean, variance,...
Definition: BasicStatistics.h:43
void normalApproximationHelper_(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:246
RealType normalDensity(RealType const coordinate) const
See normalDensity_sqrt2pi(). Returns the density of the normal distribution at point.
Definition: BasicStatistics.h:179
std::vector< RealType > coordinate_container
Definition: BasicStatistics.h:51
BasicStatistics()
Default constructor.
Definition: BasicStatistics.h:54
BasicStatistics(BasicStatistics const &arg)
Copy constructor.
Definition: BasicStatistics.h:61
void setMean(RealType const &mean)
Definition: BasicStatistics.h:151
RealT RealType
The real type specified as template argument.
Definition: BasicStatistics.h:48
void normalApproximation(probability_container &probability)
The argument probability is filled with values according to the normal approximation....
Definition: BasicStatistics.h:188
void normalApproximation(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:198
static RealType sqrt2pi()
Returns sqrt( 2 * pi ), which is useful to normalize the result of normalDensity_sqrt2pi().
Definition: BasicStatistics.h:174
void update(ProbabilityIterator probability_begin, ProbabilityIterator const probability_end)
This does the actual calculation.
Definition: BasicStatistics.h:87
RealType mean() const
Returns the mean.
Definition: BasicStatistics.h:150
void update(ProbabilityIterator const probability_begin, ProbabilityIterator const probability_end, CoordinateIterator const coordinate_begin)
This does the actual calculation.
Definition: BasicStatistics.h:120
BasicStatistics & operator=(BasicStatistics const &arg)
Assignment.
Definition: BasicStatistics.h:68
friend std::ostream & operator<<(std::ostream &os, BasicStatistics &arg)
A convenient overload for debugging purposes.
Definition: BasicStatistics.h:225
RealType sum_
Definition: BasicStatistics.h:238
void normalApproximation(probability_container &probability, coordinate_container const &coordinate)
The argument probability is filled with values according to the normal approximation....
Definition: BasicStatistics.h:212
RealType normalDensity_sqrt2pi(RealType coordinate) const
Returns the density of the normal approximation at point, multiplied by sqrt( 2 * pi )....
Definition: BasicStatistics.h:166
RealType variance() const
Returns the variance.
Definition: BasicStatistics.h:154
std::vector< RealType > probability_container
Definition: BasicStatistics.h:50
void setSum(RealType const &sum)
Definition: BasicStatistics.h:159
void normalApproximationHelper_(probability_container &probability, coordinate_container const &coordinate)
Definition: BasicStatistics.h:266
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:77
RealType sum() const
Returns the sum.
Definition: BasicStatistics.h:158
RealType variance_
Definition: BasicStatistics.h:237
RealType mean_
Definition: BasicStatistics.h:236
void setVariance(RealType const &variance)
Definition: BasicStatistics.h:155
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:94
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:81
static double variance(IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
Definition: StatisticFunctions.h:268
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19