42 #if OPENMS_BOOST_VERSION_MINOR >= 64
43 #include <boost/serialization/array_wrapper.hpp>
45 #include <boost/accumulators/accumulators.hpp>
46 #include <boost/accumulators/statistics/covariance.hpp>
47 #include <boost/accumulators/statistics/mean.hpp>
48 #include <boost/accumulators/statistics/stats.hpp>
49 #include <boost/accumulators/statistics/variance.hpp>
50 #include <boost/accumulators/statistics/variates/covariate.hpp>
51 #include <boost/function/function_base.hpp>
52 #include <boost/lambda/casts.hpp>
53 #include <boost/lambda/lambda.hpp>
71 template <
typename IteratorType>
87 template <
typename IteratorType>
103 template <
typename IteratorType1,
typename IteratorType2>
105 IteratorType1 begin_b, IteratorType1 end_b,
106 IteratorType2 begin_a, IteratorType2 end_a)
108 if (begin_b != end_b && begin_a == end_a)
119 template <
typename IteratorType>
120 static double sum(IteratorType begin, IteratorType end)
122 return std::accumulate(begin, end, 0.0);
132 template <
typename IteratorType>
133 static double mean(IteratorType begin, IteratorType end)
136 return sum(begin, end) / std::distance(begin, end);
150 template <
typename IteratorType>
151 static double median(IteratorType begin, IteratorType end,
157 std::sort(begin, end);
160 Size size = std::distance(begin, end);
163 IteratorType it1 = begin;
164 std::advance(it1, size / 2 - 1);
165 IteratorType it2 = it1;
166 std::advance(it2, 1);
167 return (*it1 + *it2) / 2.0;
171 IteratorType it = begin;
172 std::advance(it, (size - 1) / 2);
197 template <
typename IteratorType>
198 double MAD(IteratorType begin, IteratorType end,
double median_of_numbers)
200 std::vector<double> diffs;
201 diffs.reserve(std::distance(begin, end));
202 for (IteratorType it = begin; it != end; ++it)
204 diffs.push_back(fabs(*it - median_of_numbers));
206 return median(diffs.begin(), diffs.end(),
false);
227 template <
typename IteratorType>
231 for (IteratorType it = begin; it != end; ++it)
233 mean += fabs(*it - mean_of_numbers);
235 return mean / std::distance(begin, end);
251 template <
typename IteratorType>
259 std::sort(begin, end);
262 Size size = std::distance(begin, end);
265 return median(begin, begin + (size/2)-1,
true);
267 return median(begin, begin + (size/2),
true);
283 template <
typename IteratorType>
285 IteratorType begin, IteratorType end,
bool sorted =
false)
290 std::sort(begin, end);
293 Size size = std::distance(begin, end);
294 return median(begin + (size/2)+1, end,
true);
306 template <
typename IteratorType>
307 static double variance(IteratorType begin, IteratorType end,
308 double mean = std::numeric_limits<double>::max())
312 if (
mean == std::numeric_limits<double>::max())
316 for (IteratorType iter=begin; iter!=end; ++iter)
318 double diff = *iter -
mean;
321 return sum / (std::distance(begin, end)-1);
333 template <
typename IteratorType>
334 static double sd(IteratorType begin, IteratorType end,
335 double mean = std::numeric_limits<double>::max())
348 template <
typename IteratorType>
349 static double absdev(IteratorType begin, IteratorType end,
350 double mean = std::numeric_limits<double>::max())
354 if (
mean == std::numeric_limits<double>::max())
358 for (IteratorType iter=begin; iter!=end; ++iter)
362 return sum / std::distance(begin, end);
374 template <
typename IteratorType1,
typename IteratorType2>
375 static double covariance(IteratorType1 begin_a, IteratorType1 end_a,
376 IteratorType2 begin_b, IteratorType2 end_b)
384 IteratorType1 iter_a = begin_a;
385 IteratorType2 iter_b = begin_b;
386 for (; iter_a != end_a; ++iter_a, ++iter_b)
390 sum += (*iter_a - mean_a) * (*iter_b - mean_b);
394 Size n = std::distance(begin_a, end_a);
407 template <
typename IteratorType1,
typename IteratorType2>
409 IteratorType2 begin_b, IteratorType2 end_b)
414 SignedSize dist = std::distance(begin_a, end_a);
416 IteratorType1 iter_a = begin_a;
417 IteratorType2 iter_b = begin_b;
418 for (; iter_a != end_a; ++iter_a, ++iter_b)
423 double tmp(*iter_a - *iter_b);
441 template <
typename IteratorType1,
typename IteratorType2>
443 IteratorType2 begin_b, IteratorType2 end_b)
448 SignedSize dist = std::distance(begin_a, end_a);
450 IteratorType1 iter_a = begin_a;
451 IteratorType2 iter_b = begin_b;
452 for (; iter_a != end_a; ++iter_a, ++iter_b)
456 if ((*iter_a < 0 && *iter_b >= 0) || (*iter_a >= 0 && *iter_b < 0))
465 return double(correct) / dist;
480 template <
typename IteratorType1,
typename IteratorType2>
482 IteratorType1 begin_a, IteratorType1 end_a,
483 IteratorType2 begin_b, IteratorType2 end_b)
492 IteratorType1 iter_a = begin_a;
493 IteratorType2 iter_b = begin_b;
494 for (; iter_a != end_a; ++iter_a, ++iter_b)
499 if (*iter_a < 0 && *iter_b >= 0)
503 else if (*iter_a < 0 && *iter_b < 0)
507 else if (*iter_a >= 0 && *iter_b >= 0)
511 else if (*iter_a >= 0 && *iter_b < 0)
519 return (tp * tn - fp * fn) / sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
533 template <
typename IteratorType1,
typename IteratorType2>
535 IteratorType1 begin_a, IteratorType1 end_a,
536 IteratorType2 begin_b, IteratorType2 end_b)
542 SignedSize dist = std::distance(begin_a, end_a);
543 double avg_a = std::accumulate(begin_a, end_a, 0.0) / dist;
544 double avg_b = std::accumulate(begin_b, end_b, 0.0) / dist;
546 double numerator = 0;
547 double denominator_a = 0;
548 double denominator_b = 0;
549 IteratorType1 iter_a = begin_a;
550 IteratorType2 iter_b = begin_b;
551 for (; iter_a != end_a; ++iter_a, ++iter_b)
555 double temp_a = *iter_a - avg_a;
556 double temp_b = *iter_b - avg_b;
557 numerator += (temp_a * temp_b);
558 denominator_a += (temp_a * temp_a);
559 denominator_b += (temp_b * temp_b);
563 return numerator / sqrt(denominator_a * denominator_b);
567 template <
typename Value>
573 Size n = (w.size() - 1);
575 std::vector<std::pair<Size, Value> > w_idx;
576 for (
Size j = 0; j < w.size(); ++j)
578 w_idx.push_back(std::make_pair(j, w[j]));
581 std::sort(w_idx.begin(), w_idx.end(),
582 boost::lambda::ret<bool>((&boost::lambda::_1->*& std::pair<Size, Value>::second) <
583 (&boost::lambda::_2->*& std::pair<Size, Value>::second)));
588 if (fabs(w_idx[i + 1].second - w_idx[i].second) > 0.0000001 * fabs(w_idx[i + 1].second))
590 w_idx[i].second = Value(i + 1);
596 for (z = i + 1; (z <= n) && fabs(w_idx[z].second - w_idx[i].second) <= 0.0000001 * fabs(w_idx[z].second); ++z)
600 rank = 0.5 * (i + z + 1);
602 for (
Size v = i; v <= z - 1; ++v)
604 w_idx[v].second = rank;
610 w_idx[n].second = Value(n + 1);
612 for (
Size j = 0; j < w.size(); ++j)
614 w[w_idx[j].first] = w_idx[j].second;
629 template <
typename IteratorType1,
typename IteratorType2>
631 IteratorType1 begin_a, IteratorType1 end_a,
632 IteratorType2 begin_b, IteratorType2 end_b)
638 SignedSize dist = std::distance(begin_a, end_a);
639 std::vector<double> ranks_data;
640 ranks_data.reserve(dist);
641 std::vector<double> ranks_model;
642 ranks_model.reserve(dist);
643 IteratorType1 iter_a = begin_a;
644 IteratorType2 iter_b = begin_b;
645 for (; iter_a != end_a; ++iter_a, ++iter_b)
650 ranks_model.push_back(*iter_a);
651 ranks_data.push_back(*iter_b);
660 double mu = double(ranks_data.size() + 1) / 2.;
664 double sum_model_data = 0;
665 double sqsum_data = 0;
666 double sqsum_model = 0;
668 for (
Int i = 0; i < dist; ++i)
670 sum_model_data += (ranks_data[i] - mu) * (ranks_model[i] - mu);
671 sqsum_data += (ranks_data[i] - mu) * (ranks_data[i] - mu);
672 sqsum_model += (ranks_model[i] - mu) * (ranks_model[i] - mu);
676 if (!sqsum_data || !sqsum_model)
681 return sum_model_data / (sqrt(sqsum_data) * sqrt(sqsum_model));
704 sort(data.begin(), data.end());
Invalid range exception.
Definition: Exception.h:279
int Int
Signed integer type.
Definition: Types.h:102
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:134
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
static double classificationRate(IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
Calculates the classification rate for the values in [begin_a, end_a) and [begin_b,...
Definition: StatisticFunctions.h:442
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:151
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:133
static double covariance(IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
Calculates the covariance of two ranges of values.
Definition: StatisticFunctions.h:375
static double quantile3rd(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the third quantile of a range of values.
Definition: StatisticFunctions.h:284
static void checkIteratorsNotNULL(IteratorType begin, IteratorType end)
Helper function checking if two iterators are not equal.
Definition: StatisticFunctions.h:72
static double matthewsCorrelationCoefficient(IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
Calculates the Matthews correlation coefficient for the values in [begin_a, end_a) and [begin_b,...
Definition: StatisticFunctions.h:481
double MeanAbsoluteDeviation(IteratorType begin, IteratorType end, double mean_of_numbers)
mean absolute deviation (MeanAbsoluteDeviation)
Definition: StatisticFunctions.h:228
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:120
static double absdev(IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
Calculates the absolute deviation of a range of values.
Definition: StatisticFunctions.h:349
static double pearsonCorrelationCoefficient(IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
Calculates the Pearson correlation coefficient for the values in [begin_a, end_a) and [begin_b,...
Definition: StatisticFunctions.h:534
static double sd(IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
Calculates the standard deviation of a range of values.
Definition: StatisticFunctions.h:334
double MAD(IteratorType begin, IteratorType end, double median_of_numbers)
median absolute deviation (MAD)
Definition: StatisticFunctions.h:198
static double rankCorrelationCoefficient(IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
calculates the rank correlation coefficient for the values in [begin_a, end_a) and [begin_b,...
Definition: StatisticFunctions.h:630
static void checkIteratorsAreValid(IteratorType1 begin_b, IteratorType1 end_b, IteratorType2 begin_a, IteratorType2 end_a)
Helper function checking if an iterator and a co-iterator both have a next element.
Definition: StatisticFunctions.h:104
static double quantile1st(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the first quantile of a range of values.
Definition: StatisticFunctions.h:252
static void checkIteratorsEqual(IteratorType begin, IteratorType end)
Helper function checking if two iterators are equal.
Definition: StatisticFunctions.h:88
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
static double meanSquareError(IteratorType1 begin_a, IteratorType1 end_a, IteratorType2 begin_b, IteratorType2 end_b)
Calculates the mean square error for the values in [begin_a, end_a) and [begin_b, end_b)
Definition: StatisticFunctions.h:408
static void computeRank(std::vector< Value > &w)
Replaces the elements in vector w by their ranks.
Definition: StatisticFunctions.h:568
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Helper class to gather (and dump) some statistics from a e.g. vector<double>.
Definition: StatisticFunctions.h:687
double lowerq
Definition: StatisticFunctions.h:715
double variance
Definition: StatisticFunctions.h:715
SummaryStatistics()
Definition: StatisticFunctions.h:688
T::value_type max
Definition: StatisticFunctions.h:716
SummaryStatistics(T &data)
Definition: StatisticFunctions.h:694
double median
Definition: StatisticFunctions.h:715
size_t count
Definition: StatisticFunctions.h:717
double mean
Definition: StatisticFunctions.h:715
double upperq
Definition: StatisticFunctions.h:715
T::value_type min
Definition: StatisticFunctions.h:716