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 
26 
27 #include <cmath>
28 #include <vector>
29 
30 namespace armarx::math
31 {
32  class StatUtils
33  {
34  public:
35  static float GetPercentile(const std::vector<float>& sortedData, float percentile)
36  {
37  if (sortedData.size() == 0)
38  {
39  throw LocalException("GetPercentile not possible for empty vector");
40  }
41 
42  float indexf = (sortedData.size() - 1) * percentile;
43  indexf = std::max(0.f, std::min(sortedData.size() - 1.f, indexf));
44  int index = (int)indexf;
45  float f = indexf - index;
46 
47  if (index == (int)sortedData.size() - 1)
48  {
49  return sortedData.at(sortedData.size() - 1);
50  }
51 
52  return sortedData.at(index) * (1 - f) + sortedData.at(index + 1) * f;
53  }
54  static std::vector<float> GetPercentiles(const std::vector<float>& sortedData, int percentiles)
55  {
56  std::vector<float> result;
57  result.push_back(sortedData.at(0));
58 
59  for (int i = 1; i < percentiles; i++)
60  {
61  result.push_back(GetPercentile(sortedData, 1.f / percentiles * i));
62  }
63 
64  result.push_back(sortedData.at(sortedData.size() - 1));
65  return result;
66  }
67  static float GetMedian(const std::vector<float>& sortedData)
68  {
69  return GetPercentile(sortedData, 0.5f);
70  }
71  };
72 }
73 
74 
LocalException.h
armarx::math::StatUtils::GetPercentile
static float GetPercentile(const std::vector< float > &sortedData, float percentile)
Definition: StatUtils.h:35
index
uint8_t index
Definition: EtherCATFrame.h:59
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::math::StatUtils
Definition: StatUtils.h:32
armarx::math
Definition: LinearizeAngularTrajectory.cpp:27
armarx::math::StatUtils::GetPercentiles
static std::vector< float > GetPercentiles(const std::vector< float > &sortedData, int percentiles)
Definition: StatUtils.h:54
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::math::StatUtils::GetMedian
static float GetMedian(const std::vector< float > &sortedData)
Definition: StatUtils.h:67