SpatiumLib
stats.h
Go to the documentation of this file.
1 /*
2  * Program: Spatium Library
3  *
4  * Copyright (C) Martijn Koopman
5  * All Rights Reserved
6  *
7  * This software is distributed WITHOUT ANY WARRANTY; without even
8  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  * PURPOSE.
10  *
11  */
12 
13 #ifndef SPATIUM_STATS_H
14 #define SPATIUM_STATS_H
15 
16 #include "Math.h" // solveQuadratic()
17 #include "Matrix.h"
18 
19 #include <vector> // std::vector
20 
21 namespace spatium {
22 namespace stats {
23 
28 inline double mean(const std::vector<double> &values)
29 {
30  double result = 0;
31 
32  const size_t count = values.size();
33  const double divider = static_cast<double>(1) / static_cast<double>(count);
34  for (size_t i = 0; i < count; i++)
35  {
36  result += values[i] * divider;
37  }
38 
39  return result;
40 }
41 
47 inline double variance(const std::vector<double> &values, bool sample = false)
48 {
49  double result = 0;
50  const double m = mean(values);
51  const size_t count = values.size();
52  const double divider = static_cast<double>(1) / (sample ? static_cast<double>(count) - 1 : static_cast<double>(count));
53  for (size_t i = 0; i < count; i++)
54  {
55  const double diff = values[i] - m;
56  result += (diff * diff) * divider;
57  }
58  return result;
59 }
60 
67 inline double stdDev(const std::vector<double> &values, bool sample = false)
68 {
69  return sqrt(variance(values, sample));
70 }
71 
78 inline double covariance(const std::vector<double> &values1, const std::vector<double> &values2, bool sample = false)
79 {
80  if (values1.size() != values2.size())
81  {
82  return 0;
83  }
84 
85  double result = 0;
86  const double m1 = mean(values1);
87  const double m2 = mean(values2);
88  const size_t count = values1.size();
89  const double divider = static_cast<double>(1) / (sample ? static_cast<double>(count) - 1 : static_cast<double>(count));
90  for (size_t i = 0; i < count; i++)
91  {
92  result += ((values1[i] - m1) * (values2[i] - m2)) * divider;
93  }
94  return result;
95 }
96 
98 inline Matrix covariance(const Matrix &values)
99 {
100  const size_t inputRows = values.rows();
101  const size_t outputSize = values.cols();
102  Matrix result(outputSize, outputSize);
103 
104  for (size_t i = 0; i < outputSize; i++)
105  {
106  // Extract column 1 at i
107  std::vector<double> column1(inputRows);
108  for(size_t k = 0; k < inputRows; k++)
109  {
110  column1[k] = values(k,i);
111  }
112 
113  for (size_t j = 0; j < outputSize; j++)
114  {
115  if (i == j)
116  {
117  // Covariance with itself. Variance
118  result(i, j) = variance(column1);
119  }
120  else
121  {
122  // Covariance with other column
123  // Extract column 2 at j
124  std::vector<double> column2(inputRows);
125  for(size_t l = 0; l < inputRows; l++)
126  {
127  column2[l] = values(l,j);
128  }
129 
130  // Compute covariance
131  result(i, j) = covariance(column1, column2);
132  }
133  }
134  }
135 
136  return result;
137 }
138 
145 inline int eigenvalues2(const Matrix &matrix, double &eigenval1, double &eigenval2)
146 {
147  // Check 2-by-2 matrix
148  if (matrix.rows() != 2 || matrix.cols() != 2)
149  {
150  return 0;
151  }
152 
153  const double p = matrix(0,0);
154  const double q = matrix(0,1);
155  const double r = matrix(1,0);
156  const double s = matrix(1,1);
157 
158  return solveQuadratic(1, -1 * (p + s), p*s - q*r, eigenval1, eigenval2);
159 }
160 
171 inline bool eigenvector2(const Matrix &matrix, double eigenval, std::vector<double> &eigenvec)
172 {
173  // Check 2-by-2 matrix
174  if (matrix.rows() != 2 || matrix.cols() != 2)
175  {
176  return false;
177  }
178 
179  // Check 2 rows vector
180  if (eigenvec.size() != 2)
181  {
182  eigenvec.resize(2);
183  }
184 
185  eigenvec[0] = matrix(0,1);
186  eigenvec[1] = eigenval - matrix(0,0);
187 
188  // Other solution:
189  // eigenvec(0) = eigenval - matrix(1,1);
190  // eigenvec(1) = matrix(1,0);
191 
192  return true;
193 }
194 
195 } // namespace statistics
196 } // namespace spatium
197 
198 #endif // SPATIUM_STATS_H
double mean(const std::vector< double > &values)
Compute mean value.
Definition: stats.h:28
Definition: Vector2.h:19
double covariance(const std::vector< double > &values1, const std::vector< double > &values2, bool sample=false)
Compute the covariance.
Definition: stats.h:78
size_t rows() const
Get the number of rows.
Definition: Matrix.h:137
int eigenvalues2(const Matrix &matrix, double &eigenval1, double &eigenval2)
Compute eigenvalues for 2-by-2 matrix.
Definition: stats.h:145
double variance(const std::vector< double > &values, bool sample=false)
Compute the variance.
Definition: stats.h:47
size_t cols() const
Get the number of columns.
Definition: Matrix.h:145
int solveQuadratic(double a, double b, double c, double &x1, double &x2)
Solve a quadratic equation: ax^2 + bx + c = 0.
Definition: Math.h:69
Mathematical matrix with an arbitrary number number of rows and columns.
Definition: Matrix.h:34
double stdDev(const std::vector< double > &values, bool sample=false)
Compute the standard deviation.
Definition: stats.h:67
bool eigenvector2(const Matrix &matrix, double eigenval, std::vector< double > &eigenvec)
Compute the eigenvector of a 2-by-2 matrix for a given eigenvalue.
Definition: stats.h:171