TTK
Loading...
Searching...
No Matches
Statistics.cpp
Go to the documentation of this file.
1#include <Statistics.h>
2
3#include <algorithm>
4
5using namespace std;
6using namespace ttk;
7
8template <typename T>
9T Statistics::mean(const T *v, const int &dimension) {
10 T mean = 0;
11 for(int i = 0; i < dimension; ++i)
12 mean += v[i];
13 return mean / dimension;
14}
15
16template <typename T>
17T Statistics::mean(const std::vector<T> &v) {
18 return mean(v.data(), v.size());
19}
20
21template <typename T>
22T Statistics::var(const T *v, const int &dimension) {
23 return cov(v, v, dimension, dimension);
24}
25
26template <typename T>
27T Statistics::var(const std::vector<T> &v) {
28 return var(v.data(), v.size());
29}
30
31template <typename T>
32T Statistics::cov(const T *v1,
33 const T *v2,
34 const int &dimension1,
35 const int &dimension2) {
36 T cov = 0;
37 T meanV1 = mean(v1, dimension1);
38 T meanV2 = mean(v2, dimension2);
39 for(int i = 0; i < dimension1; ++i)
40 cov += (v1[i] - meanV1) * (v2[i] - meanV2);
41 return cov / (dimension1 - 1);
42}
43
44template <typename T>
45T Statistics::cov(const std::vector<T> &v1, const std::vector<T> &v2) {
46 return cov(v1.data(), v2.data(), v1.size(), v2.size());
47}
48
49template <typename T>
50T Statistics::corr(const T *v1,
51 const T *v2,
52 const int &dimension1,
53 const int &dimension2) {
54 return cov(v1, v2, dimension1, dimension2)
55 / (std::sqrt(var(v1, dimension1)) * std::sqrt(var(v2, dimension2)));
56}
57
58template <typename T>
59T Statistics::corr(const std::vector<T> &v1, const std::vector<T> &v2) {
60 return corr(v1.data(), v2.data(), v1.size(), v2.size());
61}
62
63#define STATISTICS_SPECIALIZE(TYPE) \
64 template TYPE Statistics::mean<TYPE>(TYPE const *, int const &); \
65 template TYPE Statistics::mean<TYPE>(std::vector<TYPE> const &); \
66 template TYPE Statistics::var<TYPE>(TYPE const *, int const &); \
67 template TYPE Statistics::var<TYPE>(std::vector<TYPE> const &); \
68 template TYPE Statistics::cov<TYPE>( \
69 TYPE const *, TYPE const *, int const &, int const &); \
70 template TYPE Statistics::cov<TYPE>( \
71 std::vector<TYPE> const &, std::vector<TYPE> const &); \
72 template TYPE Statistics::corr<TYPE>( \
73 TYPE const *, TYPE const *, int const &, int const &); \
74 template TYPE Statistics::corr<TYPE>( \
75 std::vector<TYPE> const &, std::vector<TYPE> const &);
76
77// explicit specializations for float and double
#define STATISTICS_SPECIALIZE(TYPE)
T mean(const T *v, const int &dimension=3)
Definition Statistics.cpp:9
T cov(const T *v1, const T *v2, const int &dimension1=3, const int &dimension2=3)
T var(const T *v, const int &dimension=3)
T corr(const T *v1, const T *v2, const int &dimension1=3, const int &dimension2=3)
The Topology ToolKit.