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);
222 template <
typename IteratorType>
230 std::sort(begin, end);
233 Size size = std::distance(begin, end);
236 return median(begin, begin + (size/2)-1,
true);
238 return median(begin, begin + (size/2),
true);
254 template <
typename IteratorType>
256 IteratorType begin, IteratorType end,
bool sorted =
false)
261 std::sort(begin, end);
264 Size size = std::distance(begin, end);
265 return median(begin + (size/2)+1, end,
true);
277 template <
typename IteratorType>
278 static double variance(IteratorType begin, IteratorType end,
279 double mean = std::numeric_limits<double>::max())
283 if (
mean == std::numeric_limits<double>::max())
287 for (IteratorType iter=begin; iter!=end; ++iter)
289 double diff = *iter -
mean;
292 return sum / (std::distance(begin, end)-1);
304 template <
typename IteratorType>
305 static double sd(IteratorType begin, IteratorType end,
306 double mean = std::numeric_limits<double>::max())
319 template <
typename IteratorType>
320 static double absdev(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>
346 static double covariance(IteratorType1 begin_a, IteratorType1 end_a,
347 IteratorType2 begin_b, IteratorType2 end_b)
355 IteratorType1 iter_a = begin_a;
356 IteratorType2 iter_b = begin_b;
357 for (; iter_a != end_a; ++iter_a, ++iter_b)
361 sum += (*iter_a - mean_a) * (*iter_b - mean_b);
365 Size n = std::distance(begin_a, end_a);
378 template <
typename IteratorType1,
typename IteratorType2>
380 IteratorType2 begin_b, IteratorType2 end_b)
385 SignedSize dist = std::distance(begin_a, end_a);
387 IteratorType1 iter_a = begin_a;
388 IteratorType2 iter_b = begin_b;
389 for (; iter_a != end_a; ++iter_a, ++iter_b)
394 double tmp(*iter_a - *iter_b);
412 template <
typename IteratorType1,
typename IteratorType2>
414 IteratorType2 begin_b, IteratorType2 end_b)
419 SignedSize dist = std::distance(begin_a, end_a);
421 IteratorType1 iter_a = begin_a;
422 IteratorType2 iter_b = begin_b;
423 for (; iter_a != end_a; ++iter_a, ++iter_b)
427 if ((*iter_a < 0 && *iter_b >= 0) || (*iter_a >= 0 && *iter_b < 0))
436 return double(correct) / dist;
451 template <
typename IteratorType1,
typename IteratorType2>
453 IteratorType1 begin_a, IteratorType1 end_a,
454 IteratorType2 begin_b, IteratorType2 end_b)
463 IteratorType1 iter_a = begin_a;
464 IteratorType2 iter_b = begin_b;
465 for (; iter_a != end_a; ++iter_a, ++iter_b)
470 if (*iter_a < 0 && *iter_b >= 0)
474 else 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)
490 return (tp * tn - fp * fn) / sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
504 template <
typename IteratorType1,
typename IteratorType2>
506 IteratorType1 begin_a, IteratorType1 end_a,
507 IteratorType2 begin_b, IteratorType2 end_b)
513 SignedSize dist = std::distance(begin_a, end_a);
514 double avg_a = std::accumulate(begin_a, end_a, 0.0) / dist;
515 double avg_b = std::accumulate(begin_b, end_b, 0.0) / dist;
517 double numerator = 0;
518 double denominator_a = 0;
519 double denominator_b = 0;
520 IteratorType1 iter_a = begin_a;
521 IteratorType2 iter_b = begin_b;
522 for (; iter_a != end_a; ++iter_a, ++iter_b)
526 double temp_a = *iter_a - avg_a;
527 double temp_b = *iter_b - avg_b;
528 numerator += (temp_a * temp_b);
529 denominator_a += (temp_a * temp_a);
530 denominator_b += (temp_b * temp_b);
534 return numerator / sqrt(denominator_a * denominator_b);
538 template <
typename Value>
544 Size n = (w.size() - 1);
546 std::vector<std::pair<Size, Value> > w_idx;
547 for (
Size j = 0; j < w.size(); ++j)
549 w_idx.push_back(std::make_pair(j, w[j]));
552 std::sort(w_idx.begin(), w_idx.end(),
553 boost::lambda::ret<bool>((&boost::lambda::_1->*& std::pair<Size, Value>::second) <
554 (&boost::lambda::_2->*& std::pair<Size, Value>::second)));
559 if (fabs(w_idx[i + 1].second - w_idx[i].second) > 0.0000001 * fabs(w_idx[i + 1].second))
561 w_idx[i].second =
Value(i + 1);
567 for (z = i + 1; (z <= n) && fabs(w_idx[z].second - w_idx[i].second) <= 0.0000001 * fabs(w_idx[z].second); ++z)
571 rank = 0.5 * (i + z + 1);
573 for (
Size v = i; v <= z - 1; ++v)
575 w_idx[v].second = rank;
581 w_idx[n].second =
Value(n + 1);
583 for (
Size j = 0; j < w.size(); ++j)
585 w[w_idx[j].first] = w_idx[j].second;
600 template <
typename IteratorType1,
typename IteratorType2>
602 IteratorType1 begin_a, IteratorType1 end_a,
603 IteratorType2 begin_b, IteratorType2 end_b)
609 SignedSize dist = std::distance(begin_a, end_a);
610 std::vector<double> ranks_data;
611 ranks_data.reserve(dist);
612 std::vector<double> ranks_model;
613 ranks_model.reserve(dist);
614 IteratorType1 iter_a = begin_a;
615 IteratorType2 iter_b = begin_b;
616 for (; iter_a != end_a; ++iter_a, ++iter_b)
621 ranks_model.push_back(*iter_a);
622 ranks_data.push_back(*iter_b);
631 double mu =
double(ranks_data.size() + 1) / 2.;
635 double sum_model_data = 0;
636 double sqsum_data = 0;
637 double sqsum_model = 0;
639 for (
Int i = 0; i < dist; ++i)
641 sum_model_data += (ranks_data[i] - mu) * (ranks_model[i] - mu);
642 sqsum_data += (ranks_data[i] - mu) * (ranks_data[i] - mu);
643 sqsum_model += (ranks_model[i] - mu) * (ranks_model[i] - mu);
647 if (!sqsum_data || !sqsum_model)
652 return sum_model_data / (sqrt(sqsum_data) * sqrt(sqsum_model));
675 sort(data.begin(), data.end());