35 #ifndef OPENMS_MATH_STATISTICS_BASICSTATISTICS_H 36 #define OPENMS_MATH_STATISTICS_BASICSTATISTICS_H 67 template <
typename RealT =
double>
90 variance_(arg.variance_),
113 template <
typename ProbabilityIterator>
114 void update(ProbabilityIterator probability_begin,
115 ProbabilityIterator
const probability_end
120 ProbabilityIterator iter = probability_begin;
122 for (; iter != probability_end; ++iter, ++pos)
125 mean_ += *iter * pos;
129 for (iter = probability_begin, pos = 0; iter != probability_end; ++iter, ++pos)
131 RealType diff = RealType(pos) - mean_;
133 variance_ += *iter * diff;
140 template <
typename ProbabilityIterator,
typename CoordinateIterator>
141 void update(ProbabilityIterator
const probability_begin,
142 ProbabilityIterator
const probability_end,
143 CoordinateIterator
const coordinate_begin
147 ProbabilityIterator prob_iter = probability_begin;
148 CoordinateIterator coord_iter = coordinate_begin;
150 for (; prob_iter != probability_end; ++prob_iter, ++coord_iter)
153 mean_ += *prob_iter * *coord_iter;
157 for (prob_iter = probability_begin, coord_iter = coordinate_begin;
158 prob_iter != probability_end;
159 ++prob_iter, ++coord_iter
162 RealType diff = *coord_iter - mean_;
164 variance_ += *prob_iter * diff;
171 RealType
mean()
const {
return mean_; }
179 RealType
sum()
const {
return sum_; }
189 coordinate -=
mean();
190 coordinate *= coordinate;
191 return exp(-coordinate / RealType(2) /
variance());
195 static RealType
sqrt2pi() {
return 2.50662827463100050240; }
202 return normalDensity_sqrt2pi(coordinate) / sqrt2pi();
211 normalApproximationHelper_(probability, probability.size());
220 typename probability_container::size_type
const size
223 probability.resize(size);
224 normalApproximationHelper_(probability, size);
234 coordinate_container
const & coordinate
237 probability.resize(coordinate.size());
238 normalApproximationHelper_(probability, coordinate);
248 os <<
"BasicStatistics: mean=" << arg.
mean() <<
" variance=" << arg.
variance() <<
" sum=" << arg.
sum();
268 typename probability_container::size_type
const size
271 RealType gaussSum = 0;
272 typename coordinate_container::size_type i;
275 for (i = 0; i < size; ++i)
277 gaussSum += normalDensity_sqrt2pi(RealType(i));
280 for (i = 0; i < size; ++i)
282 probability[i] = normalDensity_sqrt2pi(RealType(i)) / gaussSum *
sum();
288 coordinate_container
const & coordinate
291 RealType gaussSum = 0;
292 typename coordinate_container::size_type i;
293 typename coordinate_container::size_type
const size = coordinate.size();
295 for (i = 0; i < size; ++i)
297 gaussSum += normalDensity_sqrt2pi(coordinate[i]);
300 for (i = 0; i < size; ++i)
302 probability[i] = normalDensity_sqrt2pi(coordinate[i]) / gaussSum *
sum();
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