Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
variance.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2021 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
29
******************************************************************************/
30
31
#ifndef ETL_VARIANCE_INCLUDED
32
#define ETL_VARIANCE_INCLUDED
33
34
#include "
platform.h
"
35
#include "
functional.h
"
36
#include "
type_traits.h
"
37
38
// #include <math.h>
39
#include <stdint.h>
40
41
namespace
etl
42
{
43
namespace
private_variance
44
{
45
//***************************************************************************
47
//***************************************************************************
48
template
<
typename
TInput,
typename
TCalc>
49
struct
variance_traits
50
{
51
typedef
TCalc calc_t;
52
};
53
54
//***************************************************************************
56
//***************************************************************************
57
template
<
typename
TCalc>
58
struct
variance_traits
<float, TCalc>
59
{
60
typedef
float
calc_t;
61
};
62
63
//***************************************************************************
65
//***************************************************************************
66
template
<
typename
TCalc>
67
struct
variance_traits
<double, TCalc>
68
{
69
typedef
double
calc_t;
70
};
71
}
// namespace private_variance
72
73
//***************************************************************************
75
//***************************************************************************
76
namespace
private_variance
77
{
78
template
<
typename
T =
void
>
79
struct
variance_type_helper
80
{
81
static
ETL_CONSTANT
bool
Sample =
false
;
82
static
ETL_CONSTANT
bool
Population =
true
;
83
};
84
85
template
<
typename
T>
86
ETL_CONSTANT
bool
variance_type_helper<T>::Sample;
87
88
template
<
typename
T>
89
ETL_CONSTANT
bool
variance_type_helper<T>::Population;
90
}
// namespace private_variance
91
92
struct
variance_type
:
public
private_variance::variance_type_helper
<>
93
{
94
};
95
96
//***************************************************************************
98
//***************************************************************************
99
template
<
bool
Variance_Type,
typename
TInput,
typename
TCalc = TInput>
100
class
variance
101
:
public
private_variance::variance_traits
<TInput, TCalc>
102
,
public
etl::binary_function
<TInput, TInput, void>
103
{
104
private
:
105
106
static
ETL_CONSTANT
int
Adjustment = (Variance_Type == variance_type::Population) ? 0 : 1;
107
108
typedef
typename
private_variance::variance_traits<TInput, TCalc>::calc_t calc_t;
109
110
public
:
111
112
//*********************************
114
//*********************************
115
variance
()
116
{
117
clear
();
118
}
119
120
//*********************************
122
//*********************************
123
template
<
typename
TIterator>
124
variance
(TIterator first, TIterator last)
125
{
126
clear
();
127
add
(first, last);
128
}
129
130
//*********************************
132
//*********************************
133
void
add
(TInput value)
134
{
135
sum_of_squares += TCalc(value * value);
136
sum += TCalc(value);
137
++counter;
138
recalculate =
true
;
139
}
140
141
//*********************************
143
//*********************************
144
template
<
typename
TIterator>
145
void
add
(TIterator first, TIterator last)
146
{
147
while
(first != last)
148
{
149
add
(*first);
150
++first;
151
}
152
}
153
154
//*********************************
157
//*********************************
158
void
operator()
(TInput value)
159
{
160
add
(value);
161
}
162
163
//*********************************
166
//*********************************
167
template
<
typename
TIterator>
168
void
operator()
(TIterator first, TIterator last)
169
{
170
add
(first, last);
171
}
172
173
//*********************************
175
//*********************************
176
double
get_variance
()
const
177
{
178
if
(recalculate)
179
{
180
variance_value = 0.0;
181
182
if
(counter != 0)
183
{
184
double
n = double(counter);
185
double
adjustment = 1.0 / (n * (n - Adjustment));
186
187
double
square_of_sum = sum * sum;
188
189
variance_value = (n * sum_of_squares - square_of_sum) * adjustment;
190
}
191
192
recalculate =
false
;
193
}
194
195
return
variance_value;
196
}
197
198
//*********************************
200
//*********************************
201
operator
double()
const
202
{
203
return
get_variance
();
204
}
205
206
//*********************************
208
//*********************************
209
size_t
count
()
const
210
{
211
return
size_t(counter);
212
}
213
214
//*********************************
216
//*********************************
217
void
clear
()
218
{
219
sum_of_squares = calc_t(0);
220
sum = calc_t(0);
221
counter = 0U;
222
variance_value = 0.0;
223
recalculate =
true
;
224
}
225
226
private
:
227
228
calc_t sum_of_squares;
229
calc_t sum;
230
uint32_t counter;
231
mutable
double
variance_value;
232
mutable
bool
recalculate;
233
};
234
}
// namespace etl
235
236
#endif
etl::variance::count
size_t count() const
Get the total number added entries.
Definition
variance.h:209
etl::variance::get_variance
double get_variance() const
Get the variance.
Definition
variance.h:176
etl::variance::variance
variance(TIterator first, TIterator last)
Constructor.
Definition
variance.h:124
etl::variance::add
void add(TInput value)
Add a pair of values.
Definition
variance.h:133
etl::variance::add
void add(TIterator first, TIterator last)
Add a range.
Definition
variance.h:145
etl::variance::operator()
void operator()(TInput value)
Definition
variance.h:158
etl::variance::operator()
void operator()(TIterator first, TIterator last)
Definition
variance.h:168
etl::variance::variance
variance()
Constructor.
Definition
variance.h:115
etl::variance::clear
void clear()
Clear the variance.
Definition
variance.h:217
functional.h
etl::private_variance
Variance Type.
Definition
variance.h:44
etl
Definition
absolute.h:40
platform.h
etl::binary_function
binary_function
Definition
functional.h:192
etl::private_variance::variance_traits
Types for generic variance.
Definition
variance.h:50
etl::private_variance::variance_type_helper
Definition
variance.h:80
etl::variance_type
Definition
variance.h:93
type_traits.h
include
etl
variance.h
Generated on
for Embedded Template Library by
1.17.0