Embedded Template Library 1.0
variance.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_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
41namespace etl
42{
43 namespace private_variance
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 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 }
72
73 //***************************************************************************
75 //***************************************************************************
76 namespace private_variance
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>
86 ETL_CONSTANT bool variance_type_helper<T>::Sample;
87
88 template <typename T>
90 }
91
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 //*********************************
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}
235
236#endif
Variance.
Definition: variance.h:103
size_t count() const
Get the total number added entries.
Definition: variance.h:209
double get_variance() const
Get the variance.
Definition: variance.h:176
variance(TIterator first, TIterator last)
Constructor.
Definition: variance.h:124
void add(TInput value)
Add a pair of values.
Definition: variance.h:133
void add(TIterator first, TIterator last)
Add a range.
Definition: variance.h:145
void operator()(TInput value)
Definition: variance.h:158
variance()
Constructor.
Definition: variance.h:115
void clear()
Clear the variance.
Definition: variance.h:217
bitset_ext
Definition: absolute.h:38
Definition: functional.h:125
Types for generic variance.
Definition: variance.h:50
Definition: variance.h:93