OpenMS
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-2023.
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  if (sum_ == 0 && (std::isnan(mean_) || std::isinf(mean_)) )
137  {
138  mean_ = 0;
139  variance_ = 0;
140  }
141  }
142 
144 
145  template <typename ProbabilityIterator, typename CoordinateIterator>
146  void update(ProbabilityIterator const probability_begin,
147  ProbabilityIterator const probability_end,
148  CoordinateIterator const coordinate_begin
149  )
150  {
151  clear();
152  ProbabilityIterator prob_iter = probability_begin;
153  CoordinateIterator coord_iter = coordinate_begin;
154 
155  for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
156  {
157  sum_ += *prob_iter;
158  mean_ += *prob_iter * *coord_iter;
159  }
160  mean_ /= sum_;
161 
162  for (prob_iter = probability_begin, coord_iter = coordinate_begin;
163  prob_iter != probability_end;
164  ++prob_iter, ++coord_iter
165  )
166  {
167  RealType diff = *coord_iter - mean_;
168  diff *= diff;
169  variance_ += *prob_iter * diff;
170  }
171  variance_ /= sum_;
172  return;
173  }
174 
176  RealType mean() const { return mean_; }
177  void setMean(RealType const & mean) { mean_ = mean; }
178 
180  RealType variance() const { return variance_; }
181  void setVariance(RealType const & variance) { variance_ = variance; }
182 
184  RealType sum() const { return sum_; }
185  void setSum(RealType const & sum) { sum_ = sum; }
186 
187 
193  {
194  coordinate -= mean();
195  coordinate *= coordinate;
196  return exp(-coordinate / RealType(2) / variance());
197  }
198 
200  static RealType sqrt2pi() { return 2.50662827463100050240; }
201 
205  inline RealType normalDensity(RealType const coordinate) const
206  {
207  return normalDensity_sqrt2pi(coordinate) / sqrt2pi();
208  }
209 
215  {
216  normalApproximationHelper_(probability, probability.size());
217  return;
218  }
219 
225  typename probability_container::size_type const size
226  )
227  {
228  probability.resize(size);
229  normalApproximationHelper_(probability, size);
230  return;
231  }
232 
239  coordinate_container const & coordinate
240  )
241  {
242  probability.resize(coordinate.size());
243  normalApproximationHelper_(probability, coordinate);
244  return;
245  }
246 
251  friend std::ostream & operator<<(std::ostream & os, BasicStatistics & arg)
252  {
253  os << "BasicStatistics: mean=" << arg.mean() << " variance=" << arg.variance() << " sum=" << arg.sum();
254  return os;
255  }
256 
257 protected:
258 
260 
261 
265 
266 private:
268 
270 
271 
273  typename probability_container::size_type const size
274  )
275  {
276  RealType gaussSum = 0;
277  typename coordinate_container::size_type i;
278 
279  // precondition size == probability.size() is guaranteed by wrappers.
280  for (i = 0; i < size; ++i)
281  {
282  gaussSum += normalDensity_sqrt2pi(RealType(i));
283  }
284 
285  for (i = 0; i < size; ++i)
286  {
287  probability[i] = normalDensity_sqrt2pi(RealType(i)) / gaussSum * sum();
288  }
289  return;
290  }
291 
293  coordinate_container const & coordinate
294  )
295  {
296  RealType gaussSum = 0;
297  typename coordinate_container::size_type i;
298  typename coordinate_container::size_type const size = coordinate.size();
299 
300  for (i = 0; i < size; ++i)
301  {
302  gaussSum += normalDensity_sqrt2pi(coordinate[i]);
303  }
304 
305  for (i = 0; i < size; ++i)
306  {
307  probability[i] = normalDensity_sqrt2pi(coordinate[i]) / gaussSum * sum();
308  }
309  return;
310  }
311 
313 
314  };
315 
316  } // namespace Math
317 
318 } // namespace OpenMS
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:272
RealType normalDensity(RealType const coordinate) const
See normalDensity_sqrt2pi(). Returns the density of the normal distribution at point.
Definition: BasicStatistics.h:205
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:177
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:214
void normalApproximation(probability_container &probability, typename probability_container::size_type const size)
Definition: BasicStatistics.h:224
static RealType sqrt2pi()
Returns sqrt( 2 * pi ), which is useful to normalize the result of normalDensity_sqrt2pi().
Definition: BasicStatistics.h:200
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:176
void update(ProbabilityIterator const probability_begin, ProbabilityIterator const probability_end, CoordinateIterator const coordinate_begin)
This does the actual calculation.
Definition: BasicStatistics.h:146
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:251
RealType sum_
Definition: BasicStatistics.h:264
void normalApproximation(probability_container &probability, coordinate_container const &coordinate)
The argument probability is filled with values according to the normal approximation....
Definition: BasicStatistics.h:238
RealType normalDensity_sqrt2pi(RealType coordinate) const
Returns the density of the normal approximation at point, multiplied by sqrt( 2 * pi )....
Definition: BasicStatistics.h:192
RealType variance() const
Returns the variance.
Definition: BasicStatistics.h:180
std::vector< RealType > probability_container
Definition: BasicStatistics.h:76
void setSum(RealType const &sum)
Definition: BasicStatistics.h:185
void normalApproximationHelper_(probability_container &probability, coordinate_container const &coordinate)
Definition: BasicStatistics.h:292
void clear()
Set sum, mean, and variance to zero.
Definition: BasicStatistics.h:103
RealType sum() const
Returns the sum.
Definition: BasicStatistics.h:184
RealType variance_
Definition: BasicStatistics.h:263
RealType mean_
Definition: BasicStatistics.h:262
void setVariance(RealType const &variance)
Definition: BasicStatistics.h:181
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:120
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:107
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:294
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48