StatUtils.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Core
17 * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
18 * @date 2015
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 #include <cmath>
26 #include <vector>
27 
29 
30 namespace armarx::math
31 {
32  class StatUtils
33  {
34  public:
35  static float
36  GetPercentile(const std::vector<float>& sortedData, float percentile)
37  {
38  if (sortedData.size() == 0)
39  {
40  throw LocalException("GetPercentile not possible for empty vector");
41  }
42 
43  float indexf = (sortedData.size() - 1) * percentile;
44  indexf = std::max(0.f, std::min(sortedData.size() - 1.f, indexf));
45  int index = (int)indexf;
46  float f = indexf - index;
47 
48  if (index == (int)sortedData.size() - 1)
49  {
50  return sortedData.at(sortedData.size() - 1);
51  }
52 
53  return sortedData.at(index) * (1 - f) + sortedData.at(index + 1) * f;
54  }
55 
56  static std::vector<float>
57  GetPercentiles(const std::vector<float>& sortedData, int percentiles)
58  {
59  std::vector<float> result;
60  result.push_back(sortedData.at(0));
61 
62  for (int i = 1; i < percentiles; i++)
63  {
64  result.push_back(GetPercentile(sortedData, 1.f / percentiles * i));
65  }
66 
67  result.push_back(sortedData.at(sortedData.size() - 1));
68  return result;
69  }
70 
71  static float
72  GetMedian(const std::vector<float>& sortedData)
73  {
74  return GetPercentile(sortedData, 0.5f);
75  }
76  };
77 } // namespace armarx::math
LocalException.h
armarx::math::StatUtils::GetPercentile
static float GetPercentile(const std::vector< float > &sortedData, float percentile)
Definition: StatUtils.h:36
index
uint8_t index
Definition: EtherCATFrame.h:59
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::math::StatUtils
Definition: StatUtils.h:32
armarx::math
Definition: LinearizeAngularTrajectory.cpp:28
armarx::math::StatUtils::GetPercentiles
static std::vector< float > GetPercentiles(const std::vector< float > &sortedData, int percentiles)
Definition: StatUtils.h:57
min
T min(T t1, T t2)
Definition: gdiam.h:44
armarx::math::StatUtils::GetMedian
static float GetMedian(const std::vector< float > &sortedData)
Definition: StatUtils.h:72