11 #ifndef DIGNEA_STATISTICS_H
12 #define DIGNEA_STATISTICS_H
29 if (vec.empty())
return vector<float>(vec.begin(), vec.end());
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; });
48 double mean(
const vector<T> &variables) {
49 if (variables.empty()) {
50 return std::numeric_limits<double>::quiet_NaN();
52 return std::accumulate(variables.begin(), variables.end(), 0.0) /
63 double mean(
const vector<vector<T>> &matrix) {
65 return std::numeric_limits<double>::quiet_NaN();
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);
85 if (variables.size() <= 1u) {
86 return std::numeric_limits<double>::quiet_NaN();
88 auto const square = [
mean](
double sum,
int i) {
93 std::accumulate(variables.begin(), variables.end(), 0.0, square);
94 return total / (variables.size());
105 template <
typename T>
107 if (matrix.size() <= 1u) {
108 return std::numeric_limits<double>::quiet_NaN();
110 double size = matrix.size() * matrix[0].size();
113 for (
const auto &row : matrix) {
114 for (
const T &value : row) {
115 auto d = value -
mean;
119 return result / size;
130 template <
typename T>
143 template <
typename T>
155 template <
typename T>
158 return std::numeric_limits<int>::quiet_NaN();
160 std::vector<T> sorted = v;
161 size_t n = sorted.size() / 2;
162 std::nth_element(sorted.begin(), sorted.begin() + n, sorted.end());
164 if (sorted.size() % 2 == 1) {
167 std::nth_element(sorted.begin(), sorted.begin() + n - 1, sorted.end());
168 return 0.5 * (vn + v[n - 1]);
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