58 template <
typename IteratorType>
74 template <
typename IteratorType>
90 template <
typename IteratorType1,
typename IteratorType2>
92 IteratorType1 begin_b, IteratorType1 end_b,
93 IteratorType2 begin_a, IteratorType2 end_a)
95 if (begin_b != end_b && begin_a == end_a)
106 template <
typename IteratorType>
107 static double sum(IteratorType begin, IteratorType end)
109 return std::accumulate(begin, end, 0.0);
119 template <
typename IteratorType>
120 static double mean(IteratorType begin, IteratorType end)
123 return sum(begin, end) / std::distance(begin, end);
137 template <
typename IteratorType>
138 static double median(IteratorType begin, IteratorType end,
144 std::sort(begin, end);
147 Size size = std::distance(begin, end);
150 IteratorType it1 = begin;
151 std::advance(it1, size / 2 - 1);
152 IteratorType it2 = it1;
153 std::advance(it2, 1);
154 return (*it1 + *it2) / 2.0;
158 IteratorType it = begin;
159 std::advance(it, (size - 1) / 2);
184 template <
typename IteratorType>
185 double MAD(IteratorType begin, IteratorType end,
double median_of_numbers)
187 std::vector<double> diffs;
188 diffs.reserve(std::distance(begin, end));
189 for (IteratorType it = begin; it != end; ++it)
191 diffs.push_back(fabs(*it - median_of_numbers));
193 return median(diffs.begin(), diffs.end(),
false);
214 template <
typename IteratorType>
218 for (IteratorType it = begin; it != end; ++it)
220 mean += fabs(*it - mean_of_numbers);
222 return mean / std::distance(begin, end);
238 template <
typename IteratorType>
246 std::sort(begin, end);
249 Size size = std::distance(begin, end);
252 return median(begin, begin + (size/2)-1,
true);
254 return median(begin, begin + (size/2),
true);
270 template <
typename IteratorType>
272 IteratorType begin, IteratorType end,
bool sorted =
false)
277 std::sort(begin, end);
280 Size size = std::distance(begin, end);
281 return median(begin + (size/2)+1, end,
true);
293 template <
typename IteratorType>
294 static double variance(IteratorType begin, IteratorType end,
295 double mean = std::numeric_limits<double>::max())
299 if (
mean == std::numeric_limits<double>::max())
303 for (IteratorType iter=begin; iter!=end; ++iter)
305 double diff = *iter -
mean;
308 return sum / (std::distance(begin, end)-1);
320 template <
typename IteratorType>
321 static double sd(IteratorType begin, IteratorType end,
322 double mean = std::numeric_limits<double>::max())
335 template <
typename IteratorType>
336 static double absdev(IteratorType begin, IteratorType end,
337 double mean = std::numeric_limits<double>::max())
341 if (
mean == std::numeric_limits<double>::max())
345 for (IteratorType iter=begin; iter!=end; ++iter)
349 return sum / std::distance(begin, end);
361 template <
typename IteratorType1,
typename IteratorType2>
362 static double covariance(IteratorType1 begin_a, IteratorType1 end_a,
363 IteratorType2 begin_b, IteratorType2 end_b)
371 IteratorType1 iter_a = begin_a;
372 IteratorType2 iter_b = begin_b;
373 for (; iter_a != end_a; ++iter_a, ++iter_b)
377 sum += (*iter_a - mean_a) * (*iter_b - mean_b);
381 Size n = std::distance(begin_a, end_a);
394 template <
typename IteratorType1,
typename IteratorType2>
396 IteratorType2 begin_b, IteratorType2 end_b)
401 SignedSize dist = std::distance(begin_a, end_a);
403 IteratorType1 iter_a = begin_a;
404 IteratorType2 iter_b = begin_b;
405 for (; iter_a != end_a; ++iter_a, ++iter_b)
410 double tmp(*iter_a - *iter_b);
428 template <
typename IteratorType1,
typename IteratorType2>
430 IteratorType2 begin_b, IteratorType2 end_b)
435 SignedSize dist = std::distance(begin_a, end_a);
437 IteratorType1 iter_a = begin_a;
438 IteratorType2 iter_b = begin_b;
439 for (; iter_a != end_a; ++iter_a, ++iter_b)
443 if ((*iter_a < 0 && *iter_b >= 0) || (*iter_a >= 0 && *iter_b < 0))
452 return double(correct) / dist;
467 template <
typename IteratorType1,
typename IteratorType2>
469 IteratorType1 begin_a, IteratorType1 end_a,
470 IteratorType2 begin_b, IteratorType2 end_b)
479 IteratorType1 iter_a = begin_a;
480 IteratorType2 iter_b = begin_b;
481 for (; iter_a != end_a; ++iter_a, ++iter_b)
486 if (*iter_a < 0 && *iter_b >= 0)
490 else if (*iter_a < 0 && *iter_b < 0)
494 else if (*iter_a >= 0 && *iter_b >= 0)
498 else if (*iter_a >= 0 && *iter_b < 0)
506 return (tp * tn - fp * fn) / std::sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
520 template <
typename IteratorType1,
typename IteratorType2>
522 IteratorType1 begin_a, IteratorType1 end_a,
523 IteratorType2 begin_b, IteratorType2 end_b)
529 SignedSize dist = std::distance(begin_a, end_a);
530 double avg_a = std::accumulate(begin_a, end_a, 0.0) / dist;
531 double avg_b = std::accumulate(begin_b, end_b, 0.0) / dist;
533 double numerator = 0;
534 double denominator_a = 0;
535 double denominator_b = 0;
536 IteratorType1 iter_a = begin_a;
537 IteratorType2 iter_b = begin_b;
538 for (; iter_a != end_a; ++iter_a, ++iter_b)
542 double temp_a = *iter_a - avg_a;
543 double temp_b = *iter_b - avg_b;
544 numerator += (temp_a * temp_b);
545 denominator_a += (temp_a * temp_a);
546 denominator_b += (temp_b * temp_b);
550 return numerator / std::sqrt(denominator_a * denominator_b);
554 template <
typename Value>
560 Size n = (w.size() - 1);
562 std::vector<std::pair<Size, Value> > w_idx;
563 for (
Size j = 0; j < w.size(); ++j)
565 w_idx.push_back(std::make_pair(j, w[j]));
568 std::sort(w_idx.begin(), w_idx.end(),
569 [](
const auto& pair1,
const auto& pair2) { return pair1.second < pair2.second; });
574 if (fabs(w_idx[i + 1].second - w_idx[i].second) > 0.0000001 * fabs(w_idx[i + 1].second))
576 w_idx[i].second = Value(i + 1);
582 for (z = i + 1; (z <= n) && fabs(w_idx[z].second - w_idx[i].second) <= 0.0000001 * fabs(w_idx[z].second); ++z)
586 rank = 0.5 * (i + z + 1);
588 for (
Size v = i; v <= z - 1; ++v)
590 w_idx[v].second = rank;
596 w_idx[n].second = Value(n + 1);
598 for (
Size j = 0; j < w.size(); ++j)
600 w[w_idx[j].first] = w_idx[j].second;
615 template <
typename IteratorType1,
typename IteratorType2>
617 IteratorType1 begin_a, IteratorType1 end_a,
618 IteratorType2 begin_b, IteratorType2 end_b)
624 SignedSize dist = std::distance(begin_a, end_a);
625 std::vector<double> ranks_data;
626 ranks_data.reserve(dist);
627 std::vector<double> ranks_model;
628 ranks_model.reserve(dist);
629 IteratorType1 iter_a = begin_a;
630 IteratorType2 iter_b = begin_b;
631 for (; iter_a != end_a; ++iter_a, ++iter_b)
636 ranks_model.push_back(*iter_a);
637 ranks_data.push_back(*iter_b);
646 double mu = double(ranks_data.size() + 1) / 2.;
650 double sum_model_data = 0;
651 double sqsum_data = 0;
652 double sqsum_model = 0;
654 for (
Int i = 0; i < dist; ++i)
656 sum_model_data += (ranks_data[i] - mu) * (ranks_model[i] - mu);
657 sqsum_data += (ranks_data[i] - mu) * (ranks_data[i] - mu);
658 sqsum_model += (ranks_model[i] - mu) * (ranks_model[i] - mu);
662 if (!sqsum_data || !sqsum_model)
667 return sum_model_data / (std::sqrt(sqsum_data) * std::sqrt(sqsum_model));
690 sort(data.begin(), data.end());
Invalid range exception.
Definition: Exception.h:278
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:429
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:138
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:120
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:362
static double quantile3rd(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the third quantile of a range of values.
Definition: StatisticFunctions.h:271
static void checkIteratorsNotNULL(IteratorType begin, IteratorType end)
Helper function checking if two iterators are not equal.
Definition: StatisticFunctions.h:59
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:468
double MeanAbsoluteDeviation(IteratorType begin, IteratorType end, double mean_of_numbers)
mean absolute deviation (MeanAbsoluteDeviation)
Definition: StatisticFunctions.h:215
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:107
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:336
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:521
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:321
double MAD(IteratorType begin, IteratorType end, double median_of_numbers)
median absolute deviation (MAD)
Definition: StatisticFunctions.h:185
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:616
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:91
static double quantile1st(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the first quantile of a range of values.
Definition: StatisticFunctions.h:239
static void checkIteratorsEqual(IteratorType begin, IteratorType end)
Helper function checking if two iterators are equal.
Definition: StatisticFunctions.h:75
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
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:395
static void computeRank(std::vector< Value > &w)
Replaces the elements in vector w by their ranks.
Definition: StatisticFunctions.h:555
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Helper class to gather (and dump) some statistics from a e.g. vector<double>.
Definition: StatisticFunctions.h:673
double lowerq
Definition: StatisticFunctions.h:701
double variance
Definition: StatisticFunctions.h:701
SummaryStatistics()
Definition: StatisticFunctions.h:674
T::value_type max
Definition: StatisticFunctions.h:702
SummaryStatistics(T &data)
Definition: StatisticFunctions.h:680
double median
Definition: StatisticFunctions.h:701
size_t count
Definition: StatisticFunctions.h:703
double mean
Definition: StatisticFunctions.h:701
double upperq
Definition: StatisticFunctions.h:701
T::value_type min
Definition: StatisticFunctions.h:702