Embedded Template Library 1.0
standard_deviation.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_STANDARD_DEVIATION_INCLUDED
32#define ETL_STANDARD_DEVIATION_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
41namespace etl
42{
43 namespace private_standard_deviation
44 {
45 //***************************************************************************
47 //***************************************************************************
48 template <typename TInput, typename TCalc>
50 {
51 typedef TCalc calc_t;
52 };
53
54 //***************************************************************************
56 //***************************************************************************
57 template <typename TCalc>
58 struct standard_deviation_traits<float, TCalc>
59 {
60 typedef float calc_t;
61 };
62
63 //***************************************************************************
65 //***************************************************************************
66 template <typename TCalc>
67 struct standard_deviation_traits<double, TCalc>
68 {
69 typedef double calc_t;
70 };
71 }
72
73 //***************************************************************************
75 //***************************************************************************
76 namespace private_standard_deviation
77 {
78 template<typename T = void>
80 {
81 static ETL_CONSTANT bool Sample = false;
82 static ETL_CONSTANT bool Population = true;
83 };
84
85 template <typename T>
87
88 template <typename T>
90 }
91
93 {
94 };
95
96 //***************************************************************************
98 //***************************************************************************
99 template <bool Standard_Deviation_Type, typename TInput, typename TCalc = TInput>
102 , public etl::binary_function<TInput, TInput, void>
103 {
104 private:
105
106 static ETL_CONSTANT int Adjustment = (Standard_Deviation_Type == standard_deviation_type::Population) ? 0 : 1;
107
108 typedef typename private_standard_deviation::standard_deviation_traits<TInput, TCalc>::calc_t calc_t;
109
110 public:
111
112 //*********************************
114 //*********************************
116 {
117 clear();
118 }
119
120 //*********************************
122 //*********************************
123 template <typename TIterator>
124 standard_deviation(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 calculate();
179
180 return variance_value;
181 }
182
183 //*********************************
185 //*********************************
187 {
188 calculate();
189
190 return standard_deviation_value;
191 }
192
193 //*********************************
195 //*********************************
196 operator double() const
197 {
198 return get_standard_deviation();
199 }
200
201 //*********************************
203 //*********************************
204 size_t count() const
205 {
206 return size_t(counter);
207 }
208
209 //*********************************
211 //*********************************
212 void clear()
213 {
214 sum_of_squares = calc_t(0);
215 sum = calc_t(0);
216 counter = 0U;
217 variance_value = 0.0;
218 standard_deviation_value = 0.0;
219 recalculate = true;
220 }
221
222 private:
223
224 //*********************************
226 //*********************************
227 void calculate() const
228 {
229 if (recalculate)
230 {
231 standard_deviation_value = 0.0;
232 variance_value = 0.0;
233
234 if (counter != 0)
235 {
236 double n = double(counter);
237 double adjustment = 1.0 / (n * (n - Adjustment));
238
239 double square_of_sum = (sum * sum);
240
241 variance_value = ((n * sum_of_squares) - square_of_sum) * adjustment;
242
243 if (variance_value > 0)
244 {
245 standard_deviation_value = sqrt(variance_value);
246 }
247 }
248
249 recalculate = false;
250 }
251 }
252
253 calc_t sum_of_squares;
254 calc_t sum;
255 uint32_t counter;
256 mutable double variance_value;
257 mutable double standard_deviation_value;
258 mutable bool recalculate;
259 };
260}
261
262#endif
Standard Deviation.
Definition: standard_deviation.h:103
void operator()(TInput value)
Definition: standard_deviation.h:158
size_t count() const
Get the total number added entries.
Definition: standard_deviation.h:204
double get_standard_deviation() const
Get the standard_deviation.
Definition: standard_deviation.h:186
void clear()
Clear the histogram.
Definition: standard_deviation.h:212
void add(TInput value)
Add a pair of values.
Definition: standard_deviation.h:133
double get_variance() const
Get the variance.
Definition: standard_deviation.h:176
standard_deviation(TIterator first, TIterator last)
Constructor.
Definition: standard_deviation.h:124
standard_deviation()
Constructor.
Definition: standard_deviation.h:115
void add(TIterator first, TIterator last)
Add a range.
Definition: standard_deviation.h:145
bitset_ext
Definition: absolute.h:38
Definition: functional.h:125
Types for generic standard_deviation.
Definition: standard_deviation.h:50
Calculates the smallest value that, when squared, will be not greater than VALUE.
Definition: sqrt.h:47
Definition: standard_deviation.h:93