34 #ifndef OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H
35 #define OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H
41 #include <boost/accumulators/accumulators.hpp>
42 #include <boost/accumulators/statistics/covariance.hpp>
43 #include <boost/accumulators/statistics/mean.hpp>
44 #include <boost/accumulators/statistics/stats.hpp>
45 #include <boost/accumulators/statistics/variance.hpp>
46 #include <boost/accumulators/statistics/variates/covariate.hpp>
47 #include <boost/function/function_base.hpp>
48 #include <boost/lambda/casts.hpp>
49 #include <boost/lambda/lambda.hpp>
56 using std::iterator_traits;
70 template <
typename IteratorType>
72 IteratorType begin, IteratorType end)
86 template <
typename IteratorType>
88 IteratorType begin, IteratorType end)
102 template <
typename IteratorType1,
typename IteratorType2>
104 IteratorType1 begin_b, IteratorType1 end_b,
105 IteratorType2 begin_a, IteratorType2 end_a)
107 if(begin_b != end_b && begin_a == end_a)
117 template <
typename IteratorType>
118 static double sum(IteratorType begin, IteratorType end)
120 return std::accumulate(begin, end, 0.0);
130 template <
typename IteratorType>
131 static double mean(IteratorType begin, IteratorType end)
134 return sum(begin, end) / std::distance(begin, end);
148 template <
typename IteratorType>
149 static double median(IteratorType begin, IteratorType end,
bool sorted =
false)
154 std::sort(begin, end);
157 Size size = std::distance(begin, end);
160 IteratorType it1 = begin;
161 std::advance(it1, size / 2 - 1);
162 IteratorType it2 = it1;
163 std::advance(it2, 1);
164 return (*it1 + *it2) / 2.0;
168 IteratorType it = begin;
169 std::advance(it, (size - 1) / 2);
194 template <
typename IteratorType>
195 double MAD(IteratorType begin, IteratorType end,
double median_of_numbers)
197 std::vector<double> diffs;
198 diffs.reserve(std::distance(begin, end));
199 for (IteratorType it = begin; it != end; ++it)
201 diffs.push_back(fabs(*it - median_of_numbers));
203 return median(diffs.begin(), diffs.end(),
false);
219 template <
typename IteratorType>
221 IteratorType begin, IteratorType end,
bool sorted =
false)
227 std::sort(begin, end);
230 Size size = std::distance(begin, end);
233 return median(begin, begin + (size/2)-1,
true);
235 return median(begin, begin + (size/2),
true);
251 template <
typename IteratorType>
253 IteratorType begin, IteratorType end,
bool sorted =
false)
258 std::sort(begin, end);
261 Size size = std::distance(begin, end);
262 return median(begin + (size/2)+1, end,
true);
274 template <
typename IteratorType>
276 IteratorType begin, IteratorType end,
277 double mean = std::numeric_limits<double>::max())
281 if (
mean == std::numeric_limits<double>::max())
285 for (IteratorType iter=begin; iter!=end; ++iter)
287 double diff = *iter -
mean;
290 return sum / (std::distance(begin, end)-1);
302 template <
typename IteratorType>
304 IteratorType begin, IteratorType end,
305 double mean = std::numeric_limits<double>::max())
318 template <
typename IteratorType>
320 IteratorType begin, IteratorType end,
321 double mean = std::numeric_limits<double>::max())
325 if (
mean == std::numeric_limits<double>::max())
329 for (IteratorType iter=begin; iter!=end; ++iter)
333 return sum / std::distance(begin, end);
345 template <
typename IteratorType1,
typename IteratorType2>
347 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 IteratorType1 begin_a, IteratorType1 end_a,
385 IteratorType2 begin_b, IteratorType2 end_b)
390 SignedSize dist = std::distance(begin_a, end_a);
392 IteratorType1 iter_a = begin_a;
393 IteratorType2 iter_b = begin_b;
394 for (; iter_a != end_a; ++iter_a, ++iter_b)
399 double tmp(*iter_a - *iter_b);
417 template <
typename IteratorType1,
typename IteratorType2>
419 IteratorType1 begin_a, IteratorType1 end_a,
420 IteratorType2 begin_b, IteratorType2 end_b)
425 SignedSize dist = std::distance(begin_a, end_a);
427 IteratorType1 iter_a = begin_a;
428 IteratorType2 iter_b = begin_b;
429 for (; iter_a != end_a; ++iter_a, ++iter_b)
433 if ((*iter_a < 0 && *iter_b >= 0) || (*iter_a >= 0 && *iter_b < 0))
442 return double(correct) / dist;
457 template <
typename IteratorType1,
typename IteratorType2>
459 IteratorType1 begin_a, IteratorType1 end_a,
460 IteratorType2 begin_b, IteratorType2 end_b)
469 IteratorType1 iter_a = begin_a;
470 IteratorType2 iter_b = begin_b;
471 for (; iter_a != end_a; ++iter_a, ++iter_b)
476 if (*iter_a < 0 && *iter_b >= 0)
480 else if (*iter_a < 0 && *iter_b < 0)
484 else if (*iter_a >= 0 && *iter_b >= 0)
488 else if (*iter_a >= 0 && *iter_b < 0)
496 return (tp * tn - fp * fn) / sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
510 template <
typename IteratorType1,
typename IteratorType2>
512 IteratorType1 begin_a, IteratorType1 end_a,
513 IteratorType2 begin_b, IteratorType2 end_b)
519 SignedSize dist = std::distance(begin_a, end_a);
520 double avg_a = std::accumulate(begin_a, end_a, 0.0) / dist;
521 double avg_b = std::accumulate(begin_b, end_b, 0.0) / dist;
523 double numerator = 0;
524 double denominator_a = 0;
525 double denominator_b = 0;
526 IteratorType1 iter_a = begin_a;
527 IteratorType2 iter_b = begin_b;
528 for (; iter_a != end_a; ++iter_a, ++iter_b)
532 double temp_a = *iter_a - avg_a;
533 double temp_b = *iter_b - avg_b;
534 numerator += (temp_a * temp_b);
535 denominator_a += (temp_a * temp_a);
536 denominator_b += (temp_b * temp_b);
540 return numerator / sqrt(denominator_a * denominator_b);
544 template <
typename Value>
550 Size n = (w.size() - 1);
552 std::vector<std::pair<Size, Value> > w_idx;
553 for (
Size j = 0; j < w.size(); ++j)
555 w_idx.push_back(std::make_pair(j, w[j]));
558 std::sort(w_idx.begin(), w_idx.end(),
559 boost::lambda::ret<bool>((&boost::lambda::_1->*& std::pair<Size, Value>::second) <
560 (&boost::lambda::_2->*& std::pair<Size, Value>::second)));
565 if (fabs(w_idx[i + 1].second - w_idx[i].second) > 0.0000001 * fabs(w_idx[i + 1].second))
567 w_idx[i].second = Value(i + 1);
573 for (z = i + 1; (z <= n) && fabs(w_idx[z].second - w_idx[i].second) <= 0.0000001 * fabs(w_idx[z].second); ++z)
577 rank = 0.5 * (i + z + 1);
579 for (
Size v = i; v <= z - 1; ++v)
581 w_idx[v].second = rank;
587 w_idx[n].second = Value(n + 1);
589 for (
Size j = 0; j < w.size(); ++j)
591 w[w_idx[j].first] = w_idx[j].second;
606 template <
typename IteratorType1,
typename IteratorType2>
608 IteratorType1 begin_a, IteratorType1 end_a,
609 IteratorType2 begin_b, IteratorType2 end_b)
615 SignedSize dist = std::distance(begin_a, end_a);
616 std::vector<double> ranks_data;
617 ranks_data.reserve(dist);
618 std::vector<double> ranks_model;
619 ranks_model.reserve(dist);
620 IteratorType1 iter_a = begin_a;
621 IteratorType2 iter_b = begin_b;
622 for (; iter_a != end_a; ++iter_a, ++iter_b)
627 ranks_model.push_back(*iter_a);
628 ranks_data.push_back(*iter_b);
637 double mu =
double(ranks_data.size() + 1) / 2.;
641 double sum_model_data = 0;
642 double sqsum_data = 0;
643 double sqsum_model = 0;
645 for (
Int i = 0; i < dist; ++i)
647 sum_model_data += (ranks_data[i] - mu) * (ranks_model[i] - mu);
648 sqsum_data += (ranks_data[i] - mu) * (ranks_data[i] - mu);
649 sqsum_model += (ranks_model[i] - mu) * (ranks_model[i] - mu);
653 if (!sqsum_data || !sqsum_model)
658 return sum_model_data / (sqrt(sqsum_data) * sqrt(sqsum_model));
681 sort(data.begin(), data.end());
700 #endif // OPENMS_MATH_STATISTICS_STATISTICFUNCTIONS_H
SummaryStatistics()
Definition: StatisticFunctions.h:665
double variance
Definition: StatisticFunctions.h:692
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:693
double MAD(IteratorType begin, IteratorType end, double median_of_numbers)
median absolute deviation (MAD)
Definition: StatisticFunctions.h:195
double lowerq
Definition: StatisticFunctions.h:692
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:275
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition: StatisticFunctions.h:118
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:103
static double quantile1st(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the first quantile of a range of values.
Definition: StatisticFunctions.h:220
static void computeRank(std::vector< Value > &w)
Replaces the elements in vector w by their ranks.
Definition: StatisticFunctions.h:545
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:346
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:135
static void checkIteratorsEqual(IteratorType begin, IteratorType end)
Helper function checking if two iterators are equal.
Definition: StatisticFunctions.h:87
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:131
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:303
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:458
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:319
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:511
Helper class to gather (and dump) some statistics from a e.g. vector.
Definition: StatisticFunctions.h:663
static void checkIteratorsNotNULL(IteratorType begin, IteratorType end)
Helper function checking if two iterators are not equal.
Definition: StatisticFunctions.h:71
double upperq
Definition: StatisticFunctions.h:692
static double quantile3rd(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the third quantile of a range of values.
Definition: StatisticFunctions.h:252
double median
Definition: StatisticFunctions.h:692
static double median(IteratorType begin, IteratorType end, bool sorted=false)
Calculates the median of a range of values.
Definition: StatisticFunctions.h:149
SummaryStatistics(T &data)
Definition: StatisticFunctions.h:671
Invalid range exception.
Definition: Exception.h:286
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:607
size_t count
Definition: StatisticFunctions.h:694
T::value_type min
Definition: StatisticFunctions.h:693
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:418
double mean
Definition: StatisticFunctions.h:692
int Int
Signed integer type.
Definition: Types.h:103