dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
Statistics.h
Go to the documentation of this file.
1 
11 #ifndef DIGNEA_STATISTICS_H
12 #define DIGNEA_STATISTICS_H
13 
14 #include <algorithm>
15 #include <cmath>
16 #include <numeric>
17 #include <ranges>
18 #include <vector>
19 
27 template <typename T>
28 std::vector<float> normalize(vector<T> &vec) {
29  if (vec.empty()) return vector<float>(vec.begin(), vec.end());
30 
31  std::vector<float> normalized = vector<float>(vec.begin(), vec.end());
32  T magnitude = std::sqrt(std::inner_product(
33  normalized.begin(), normalized.end(), normalized.begin(), 0.0f));
34  if (magnitude != 0.0f) {
35  std::transform(normalized.begin(), normalized.end(), normalized.begin(),
36  [magnitude](float value) { return value / magnitude; });
37  }
38  return normalized;
39 }
40 
47 template <typename T>
48 double mean(const vector<T> &variables) {
49  if (variables.empty()) {
50  return std::numeric_limits<double>::quiet_NaN();
51  }
52  return std::accumulate(variables.begin(), variables.end(), 0.0) /
53  variables.size();
54 }
55 
62 template <typename T>
63 double mean(const vector<vector<T>> &matrix) {
64  if (matrix.empty()) {
65  return std::numeric_limits<double>::quiet_NaN();
66  }
67  double size = matrix.size() * matrix[0].size();
68  auto sum = std::accumulate(
69  matrix.cbegin(), matrix.cend(), 0, [](auto lhs, const auto &rhs) {
70  return std::accumulate(rhs.cbegin(), rhs.cend(), lhs);
71  });
72  return sum / size;
73 }
74 
83 template <typename T>
84 double variance(const double &mean, const vector<T> &variables) {
85  if (variables.size() <= 1u) {
86  return std::numeric_limits<double>::quiet_NaN();
87  }
88  auto const square = [mean](double sum, int i) {
89  auto d = i - mean;
90  return sum + d * d;
91  };
92  double total =
93  std::accumulate(variables.begin(), variables.end(), 0.0, square);
94  return total / (variables.size());
95 }
96 
105 template <typename T>
106 double variance(const double &mean, const vector<vector<T>> &matrix) {
107  if (matrix.size() <= 1u) {
108  return std::numeric_limits<double>::quiet_NaN();
109  }
110  double size = matrix.size() * matrix[0].size();
111 
112  double result = 0.0;
113  for (const auto &row : matrix) {
114  for (const T &value : row) {
115  auto d = value - mean;
116  result += d * d;
117  }
118  }
119  return result / size;
120 }
121 
130 template <typename T>
131 double standardDev(double mean, const vector<T> &variables) {
132  return std::sqrt(variance(mean, variables));
133 }
134 
143 template <typename T>
144 double standardDev(double mean, const vector<vector<T>> &matrix) {
145  return std::sqrt(variance(mean, matrix));
146 }
147 
155 template <typename T>
156 T median(const std::vector<T> &v) {
157  if (v.empty()) {
158  return std::numeric_limits<int>::quiet_NaN();
159  }
160  std::vector<T> sorted = v;
161  size_t n = sorted.size() / 2;
162  std::nth_element(sorted.begin(), sorted.begin() + n, sorted.end());
163  int vn = sorted[n];
164  if (sorted.size() % 2 == 1) {
165  return vn;
166  } else {
167  std::nth_element(sorted.begin(), sorted.begin() + n - 1, sorted.end());
168  return 0.5 * (vn + v[n - 1]);
169  }
170 }
171 #endif // DIGNEA_STATISTICS_H
double variance(const double &mean, const vector< T > &variables)
Computes the variance of the vector based on the variables and the mean. Returns NaN if empty.
Definition: Statistics.h:84
T median(const std::vector< T > &v)
Computes the median of the vector. Returns NaN if empty.
Definition: Statistics.h:156
std::vector< float > normalize(vector< T > &vec)
Normalizes a vector. Returns a copy of the vector normalized.
Definition: Statistics.h:28
double standardDev(double mean, const vector< T > &variables)
Computes the Standard Deviation of the vector based on the mean value. Returns NaN if empty.
Definition: Statistics.h:131
double mean(const vector< T > &variables)
Computes the mean value of the given vector. Returns NaN if empty.
Definition: Statistics.h:48