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

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:21:59 using doxygen 1.8.13