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> 58 using std::iterator_traits;
72 template <
typename IteratorType>
88 template <
typename IteratorType>
104 template <
typename IteratorType1,
typename IteratorType2>
106 IteratorType1 begin_b, IteratorType1 end_b,
107 IteratorType2 begin_a, IteratorType2 end_a)
109 if (begin_b != end_b && begin_a == end_a)
120 template <
typename IteratorType>
121 static double sum(IteratorType begin, IteratorType end)
123 return std::accumulate(begin, end, 0.0);
133 template <
typename IteratorType>
134 static double mean(IteratorType begin, IteratorType end)
137 return sum(begin, end) / std::distance(begin, end);
151 template <
typename IteratorType>
152 static double median(IteratorType begin, IteratorType end,
158 std::sort(begin, end);
161 Size size = std::distance(begin, end);
164 IteratorType it1 = begin;
165 std::advance(it1, size / 2 - 1);
166 IteratorType it2 = it1;
167 std::advance(it2, 1);
168 return (*it1 + *it2) / 2.0;
172 IteratorType it = begin;
173 std::advance(it, (size - 1) / 2);
198 template <
typename IteratorType>
199 double MAD(IteratorType begin, IteratorType end,
double median_of_numbers)
201 std::vector<double> diffs;
202 diffs.reserve(std::distance(begin, end));
203 for (IteratorType it = begin; it != end; ++it)
205 diffs.push_back(fabs(*it - median_of_numbers));
207 return median(diffs.begin(), diffs.end(),
false);
223 template <
typename IteratorType>
231 std::sort(begin, end);
234 Size size = std::distance(begin, end);
237 return median(begin, begin + (size/2)-1,
true);
239 return median(begin, begin + (size/2),
true);
255 template <
typename IteratorType>
257 IteratorType begin, IteratorType end,
bool sorted =
false)
262 std::sort(begin, end);
265 Size size = std::distance(begin, end);
266 return median(begin + (size/2)+1, end,
true);
278 template <
typename IteratorType>
279 static double variance(IteratorType begin, IteratorType end,
280 double mean = std::numeric_limits<double>::max())
284 if (
mean == std::numeric_limits<double>::max())
288 for (IteratorType iter=begin; iter!=end; ++iter)
290 double diff = *iter -
mean;
293 return sum / (std::distance(begin, end)-1);
305 template <
typename IteratorType>
306 static double sd(IteratorType begin, IteratorType end,
307 double mean = std::numeric_limits<double>::max())
320 template <
typename IteratorType>
321 static double absdev(IteratorType begin, IteratorType end,
322 double mean = std::numeric_limits<double>::max())
326 if (
mean == std::numeric_limits<double>::max())
330 for (IteratorType iter=begin; iter!=end; ++iter)
334 return sum / std::distance(begin, end);
346 template <
typename IteratorType1,
typename IteratorType2>
347 static double covariance(IteratorType1 begin_a, IteratorType1 end_a,
348 IteratorType2 begin_b, IteratorType2 end_b)
356 IteratorType1 iter_a = begin_a;
357 IteratorType2 iter_b = begin_b;
358 for (; iter_a != end_a; ++iter_a, ++iter_b)
362 sum += (*iter_a - mean_a) * (*iter_b - mean_b);
366 Size n = std::distance(begin_a, end_a);
382 template <
typename IteratorType1,
typename IteratorType2>
384 IteratorType2 begin_b, IteratorType2 end_b)
389 SignedSize dist = std::distance(begin_a, end_a);
391 IteratorType1 iter_a = begin_a;
392 IteratorType2 iter_b = begin_b;
393 for (; iter_a != end_a; ++iter_a, ++iter_b)
398 double tmp(*iter_a - *iter_b);
416 template <
typename IteratorType1,
typename IteratorType2>
418 IteratorType2 begin_b, IteratorType2 end_b)
423 SignedSize dist = std::distance(begin_a, end_a);
425 IteratorType1 iter_a = begin_a;
426 IteratorType2 iter_b = begin_b;
427 for (; iter_a != end_a; ++iter_a, ++iter_b)
431 if ((*iter_a < 0 && *iter_b >= 0) || (*iter_a >= 0 && *iter_b < 0))
440 return double(correct) / dist;
455 template <
typename IteratorType1,
typename IteratorType2>
457 IteratorType1 begin_a, IteratorType1 end_a,
458 IteratorType2 begin_b, IteratorType2 end_b)
467 IteratorType1 iter_a = begin_a;
468 IteratorType2 iter_b = begin_b;
469 for (; iter_a != end_a; ++iter_a, ++iter_b)
474 if (*iter_a < 0 && *iter_b >= 0)
478 else if (*iter_a < 0 && *iter_b < 0)
482 else if (*iter_a >= 0 && *iter_b >= 0)
486 else if (*iter_a >= 0 && *iter_b < 0)
494 return (tp * tn - fp * fn) / sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
508 template <
typename IteratorType1,
typename IteratorType2>
510 IteratorType1 begin_a, IteratorType1 end_a,
511 IteratorType2 begin_b, IteratorType2 end_b)
517 SignedSize dist = std::distance(begin_a, end_a);
518 double avg_a = std::accumulate(begin_a, end_a, 0.0) / dist;
519 double avg_b = std::accumulate(begin_b, end_b, 0.0) / dist;
521 double numerator = 0;
522 double denominator_a = 0;
523 double denominator_b = 0;
524 IteratorType1 iter_a = begin_a;
525 IteratorType2 iter_b = begin_b;
526 for (; iter_a != end_a; ++iter_a, ++iter_b)
530 double temp_a = *iter_a - avg_a;
531 double temp_b = *iter_b - avg_b;
532 numerator += (temp_a * temp_b);
533 denominator_a += (temp_a * temp_a);
534 denominator_b += (temp_b * temp_b);
538 return numerator / sqrt(denominator_a * denominator_b);
542 template <
typename Value>
548 Size n = (w.size() - 1);
550 std::vector<std::pair<Size, Value> > w_idx;
551 for (
Size j = 0; j < w.size(); ++j)
553 w_idx.push_back(std::make_pair(j, w[j]));
556 std::sort(w_idx.begin(), w_idx.end(),
557 boost::lambda::ret<bool>((&boost::lambda::_1->*& std::pair<Size, Value>::second) <
558 (&boost::lambda::_2->*& std::pair<Size, Value>::second)));
563 if (fabs(w_idx[i + 1].second - w_idx[i].second) > 0.0000001 * fabs(w_idx[i + 1].second))
565 w_idx[i].second =
Value(i + 1);
571 for (z = i + 1; (z <= n) && fabs(w_idx[z].second - w_idx[i].second) <= 0.0000001 * fabs(w_idx[z].second); ++z)
575 rank = 0.5 * (i + z + 1);
577 for (
Size v = i; v <= z - 1; ++v)
579 w_idx[v].second = rank;
585 w_idx[n].second =
Value(n + 1);
587 for (
Size j = 0; j < w.size(); ++j)
589 w[w_idx[j].first] = w_idx[j].second;
604 template <
typename IteratorType1,
typename IteratorType2>
606 IteratorType1 begin_a, IteratorType1 end_a,
607 IteratorType2 begin_b, IteratorType2 end_b)
613 SignedSize dist = std::distance(begin_a, end_a);
614 std::vector<double> ranks_data;
615 ranks_data.reserve(dist);
616 std::vector<double> ranks_model;
617 ranks_model.reserve(dist);
618 IteratorType1 iter_a = begin_a;
619 IteratorType2 iter_b = begin_b;
620 for (; iter_a != end_a; ++iter_a, ++iter_b)
625 ranks_model.push_back(*iter_a);
626 ranks_data.push_back(*iter_b);
635 double mu =
double(ranks_data.size() + 1) / 2.;
639 double sum_model_data = 0;
640 double sqsum_data = 0;
641 double sqsum_model = 0;
643 for (
Int i = 0; i < dist; ++i)
645 sum_model_data += (ranks_data[i] - mu) * (ranks_model[i] - mu);
646 sqsum_data += (ranks_data[i] - mu) * (ranks_data[i] - mu);
647 sqsum_model += (ranks_model[i] - mu) * (ranks_model[i] - mu);
651 if (!sqsum_data || !sqsum_model)
656 return sum_model_data / (sqrt(sqsum_data) * sqrt(sqsum_model));
679 sort(data.begin(), data.end());
SummaryStatistics()
Definition: StatisticFunctions.h:663
double variance
Definition: StatisticFunctions.h:690
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:383
T::value_type max
Definition: StatisticFunctions.h:691
double MAD(IteratorType begin, IteratorType end, double median_of_numbers)
median absolute deviation (MAD)
Definition: StatisticFunctions.h:199
double lowerq
Definition: StatisticFunctions.h:690
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:279
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:121
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:105
static double quantile1st(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the first quantile of a range of values.
Definition: StatisticFunctions.h:224
static void computeRank(std::vector< Value > &w)
Replaces the elements in vector w by their ranks.
Definition: StatisticFunctions.h:543
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:347
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:134
static void checkIteratorsEqual(IteratorType begin, IteratorType end)
Helper function checking if two iterators are equal.
Definition: StatisticFunctions.h:89
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:134
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:306
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:456
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:321
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:509
Helper class to gather (and dump) some statistics from a e.g. vector<double>.
Definition: StatisticFunctions.h:661
static void checkIteratorsNotNULL(IteratorType begin, IteratorType end)
Helper function checking if two iterators are not equal.
Definition: StatisticFunctions.h:73
double upperq
Definition: StatisticFunctions.h:690
static double quantile3rd(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the third quantile of a range of values.
Definition: StatisticFunctions.h:256
double median
Definition: StatisticFunctions.h:690
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:152
SummaryStatistics(T &data)
Definition: StatisticFunctions.h:669
Invalid range exception.
Definition: Exception.h:285
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:605
size_t count
Definition: StatisticFunctions.h:692
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
T::value_type min
Definition: StatisticFunctions.h:691
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, end_b)
Definition: StatisticFunctions.h:417
double mean
Definition: StatisticFunctions.h:690
int Int
Signed integer type.
Definition: Types.h:102