Stride Reference Manual  - generated for commit 9643b11
BoxPlotData.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Kuylen E, Willem L, Broeckhove J
14  */
20 #pragma once
21 
22 #include <algorithm>
23 #include <cassert>
24 #include <iostream>
25 #include <numeric>
26 #include <vector>
27 
28 namespace stride {
29 namespace util {
30 
34 template <typename T>
36 {
37 public:
39 
41  T m_min;
42  T m_max;
46 
48  static BoxPlotData Calculate(const std::vector<T>& data)
49  {
50  assert(data.size() >= 1 && "TestResult> cannot calculate stats for empty vector.");
51  BoxPlotData stats;
52 
53  // Sort the durations.
54  auto temp(data);
55  std::sort(temp.begin(), temp.end());
56 
57  // Total, min, max
58  stats.m_total = std::accumulate(temp.cbegin(), temp.cend(), T());
59  stats.m_min = temp.front();
60  stats.m_max = temp.back();
61 
62  // Median, quartiles.
63  const std::size_t size = temp.size();
64  const std::size_t sizeHalf = size / 2;
65  if (size >= 4) {
66  const std::size_t quartile = sizeHalf / 2;
67  if ((size % 2) == 0) {
68  stats.m_median = (temp[sizeHalf - 1] + temp[sizeHalf]) / 2;
69  stats.m_quartile1 = temp[quartile];
70  stats.m_quartile3 = temp[sizeHalf + quartile];
71  } else {
72  stats.m_median = temp[sizeHalf];
73  stats.m_quartile1 = (temp[quartile - 1] + temp[quartile]) / 2;
74  stats.m_quartile3 = (temp[sizeHalf + (quartile - 1)] + temp[sizeHalf + quartile]) / 2;
75  }
76  } else if (size > 0) {
77  stats.m_median = temp[sizeHalf];
78  stats.m_quartile1 = temp.front();
79  stats.m_quartile3 = temp.back();
80  }
81 
82  return stats;
83  }
84 };
85 
86 } // namespace util
87 } // namespace stride
BoxPlotData calculates total, minimum, maximum, median, lower and upper quartile of the values in the...
Definition: BoxPlotData.h:35
static BoxPlotData Calculate(const std::vector< T > &data)
Definition: BoxPlotData.h:48
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28