Repository navigation
Expand file tree
/
Copy pathStatistics.hpp
More file actions
179 lines (145 loc) · 6 KB
/
Copy pathStatistics.hpp
File metadata and controls
179 lines (145 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once
#include "numerical/math/Math.hpp"
#include "numerical/math/Matrix.hpp"
#include "numerical/math/QNumber.hpp"
namespace math
{
template<typename V>
struct Result
{
V value{};
bool valid{ false };
};
template<typename T, size_t Rows, size_t Cols>
[[nodiscard]] constexpr T Mean(const Matrix<T, Rows, Cols>& data)
{
static_assert(detail::is_supported_type_v<T>,
"Statistical functions only support float or QNumber types");
float sum = 0.0f;
for (size_t i = 0; i < data.size; ++i)
sum += math::ToFloat(data.begin()[i]);
return T{ sum / static_cast<float>(data.size) };
}
template<typename T, size_t Rows, size_t Cols>
[[nodiscard]] constexpr T Variance(const Matrix<T, Rows, Cols>& data, bool sample = true)
{
static_assert(detail::is_supported_type_v<T>,
"Statistical functions only support float or QNumber types");
const T m = Mean(data);
float sum_sq = 0.0f;
for (size_t i = 0; i < data.size; ++i)
{
float diff = math::ToFloat(data.begin()[i] - m);
sum_sq += diff * diff;
}
const float divisor = sample ? static_cast<float>(data.size) - 1.0f : static_cast<float>(data.size);
really_assert(divisor > 0.0f);
return T{ sum_sq / divisor };
}
template<typename T, size_t Rows, size_t Cols>
[[nodiscard]] constexpr T StandardDeviation(const Matrix<T, Rows, Cols>& data, bool sample = true)
{
return T{ math::Sqrt(math::ToFloat(Variance(data, sample))) };
}
template<typename T, size_t Size>
[[nodiscard]] constexpr T MeanSquaredError(const Vector<T, Size>& actual, const Vector<T, Size>& predicted)
{
static_assert(detail::is_supported_type_v<T>,
"Statistical functions only support float or QNumber types");
float sum_sq_error = 0.0f;
for (size_t i = 0; i < Size; ++i)
{
float diff = math::ToFloat(actual.at(i, 0) - predicted.at(i, 0));
sum_sq_error += diff * diff;
}
return T{ sum_sq_error / static_cast<float>(Size) };
}
template<typename T, size_t Size>
[[nodiscard]] constexpr T RootMeanSquaredError(const Vector<T, Size>& actual, const Vector<T, Size>& predicted)
{
float sum_sq_error = 0.0f;
for (size_t i = 0; i < Size; ++i)
{
float diff = math::ToFloat(actual.at(i, 0) - predicted.at(i, 0));
sum_sq_error += diff * diff;
}
return T{ math::Sqrt(sum_sq_error / static_cast<float>(Size)) };
}
template<typename T, size_t Size>
[[nodiscard]] constexpr T MeanAbsoluteError(const Vector<T, Size>& actual, const Vector<T, Size>& predicted)
{
static_assert(detail::is_supported_type_v<T>,
"Statistical functions only support float or QNumber types");
float sum_abs_error = 0.0f;
for (size_t i = 0; i < Size; ++i)
sum_abs_error += math::Abs(math::ToFloat(actual.at(i, 0) - predicted.at(i, 0)));
return T{ sum_abs_error / static_cast<float>(Size) };
}
template<typename T, size_t Size>
[[nodiscard]] constexpr Result<T> RSquaredScore(const Vector<T, Size>& actual, const Vector<T, Size>& predicted)
{
static_assert(detail::is_supported_type_v<T>,
"Statistical functions only support float or QNumber types");
float mean_actual = math::ToFloat(Mean(actual));
float total_ss = 0.0f;
float residual_ss = 0.0f;
for (size_t i = 0; i < Size; ++i)
{
float actual_val = math::ToFloat(actual.at(i, 0));
float pred_val = math::ToFloat(predicted.at(i, 0));
float diff_mean = actual_val - mean_actual;
float diff_pred = actual_val - pred_val;
total_ss += diff_mean * diff_mean;
residual_ss += diff_pred * diff_pred;
}
if (total_ss == 0.0f)
return { T{}, false };
return { T{ 1.0f - (residual_ss / total_ss) }, true };
}
template<typename T, size_t Size>
[[nodiscard]] constexpr Result<Matrix<T, Size, 1>> AutoCorrelation(const Vector<T, Size>& data, size_t maxLag)
{
static_assert(detail::is_supported_type_v<T>,
"Statistical functions only support float or QNumber types");
really_assert(maxLag < Size);
float mean_val = math::ToFloat(Mean(data));
float sum_sq = 0.0f;
for (size_t i = 0; i < Size; ++i)
{
float diff = math::ToFloat(data.at(i, 0)) - mean_val;
sum_sq += diff * diff;
}
if (sum_sq == 0.0f)
return { Matrix<T, Size, 1>{}, false };
Matrix<T, Size, 1> result;
for (size_t lag = 0; lag <= maxLag; ++lag)
{
float sum = 0.0f;
for (size_t t = 0; t < Size - lag; ++t)
sum += (math::ToFloat(data.at(t, 0)) - mean_val) * (math::ToFloat(data.at(t + lag, 0)) - mean_val);
if (lag == 0)
{
if constexpr (std::is_floating_point_v<T>)
result.at(lag, 0) = T{ 1 };
else
result.at(lag, 0) = T{ 0.9999f };
}
else
result.at(lag, 0) = T{ sum / ((sum_sq / static_cast<float>(Size)) * static_cast<float>(Size - lag)) };
}
return { result, true };
}
template<typename T, size_t Rows, size_t Cols>
[[nodiscard]] constexpr Matrix<T, Rows, Cols> ZScore(const Matrix<T, Rows, Cols>& data)
{
static_assert(std::is_same_v<T, float>,
"ZScore function only supports float type");
const T meanValue = Mean(data);
const T standardDev = StandardDeviation(data, false);
really_assert(math::ToFloat(standardDev) > 0.0f);
Matrix<T, Rows, Cols> result;
for (size_t i = 0; i < data.size; ++i)
result.begin()[i] = T{ math::ToFloat(data.begin()[i] - meanValue) / math::ToFloat(standardDev) };
return result;
}
}