Embedded Template Library 1.0
Loading...
Searching...
No Matches
covariance.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_COVARIANCE_INCLUDED
32#define ETL_COVARIANCE_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_covariance
44 {
45 //***************************************************************************
47 //***************************************************************************
48 template <typename TInput, typename TCalc>
50 {
51 typedef TCalc calc_t;
52 };
53
54 //***************************************************************************
56 //***************************************************************************
57 template <typename TCalc>
59 {
60 typedef float calc_t;
61 };
62
63 //***************************************************************************
65 //***************************************************************************
66 template <typename TCalc>
68 {
69 typedef double calc_t;
70 };
71 }
72
73 //***************************************************************************
75 //***************************************************************************
76 namespace private_covariance
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 covariance_type_statics<T>::Sample;
87
88 template<typename T>
90 }
91
95
96 //***************************************************************************
98 //***************************************************************************
99 template <bool Covariance_Type, typename TInput, typename TCalc = TInput>
101 : public private_covariance::covariance_traits<TInput, TCalc>
102 , public etl::binary_function<TInput, TInput, void>
103 {
104 private:
105
106 static ETL_CONSTANT int Adjustment = (Covariance_Type == covariance_type::Population) ? 0 : 1;
107
109
110 public:
111
112 //*********************************
114 //*********************************
116 {
117 clear();
118 }
119
120 //*********************************
122 //*********************************
123 template <typename TIterator>
129
130 //*********************************
132 //*********************************
133 void add(TInput value1, TInput value2)
134 {
135 inner_product += TCalc(value1 * value2);
136 sum1 += TCalc(value1);
137 sum2 += TCalc(value2);
138 ++counter;
139 recalculate = true;
140 }
141
142 //*********************************
144 //*********************************
145 template <typename TIterator>
147 {
148 while (first1 != last1)
149 {
150 add(*first1, *first2);
151 ++first1;
152 ++first2;
153 }
154 }
155
156 //*********************************
159 //*********************************
160 void operator ()(TInput value1, TInput value2)
161 {
162 add(value1, value2);
163 }
164
165 //*********************************
168 //*********************************
169 template <typename TIterator>
174
175 //*********************************
177 //*********************************
178 double get_covariance() const
179 {
180 if (recalculate)
181 {
182 covariance_value = 0.0;
183
184 if (counter != 0)
185 {
186 double n = double(counter);
187 double adjustment = 1.0 / (n * (n - Adjustment));
188
189 covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
190
191 recalculate = false;
192 }
193 }
194
195 return covariance_value;
196 }
197
198 //*********************************
200 //*********************************
201 operator double() const
202 {
203 return get_covariance();
204 }
205
206 //*********************************
208 //*********************************
209 size_t count() const
210 {
211 return size_t(counter);
212 }
213
214 //*********************************
216 //*********************************
217 void clear()
218 {
219 inner_product = calc_t(0);
220 sum1 = calc_t(0);
221 sum2 = calc_t(0);
222 counter = 0U;
223 covariance_value = 0.0;
224 recalculate = true;
225 }
226
227 private:
228
229 calc_t inner_product;
230 calc_t sum1;
231 calc_t sum2;
232 uint32_t counter;
233 mutable double covariance_value;
234 mutable bool recalculate;
235 };
236
237 template <bool Covariance_Type, typename TInput, typename TCalc>
238 ETL_CONSTANT int covariance<Covariance_Type, TInput, TCalc>::Adjustment;
239}
240
241#endif
Covariance.
Definition covariance.h:103
void clear()
Clear the covariance.
Definition covariance.h:217
double get_covariance() const
Get the covariance.
Definition covariance.h:178
size_t count() const
Get the total number added entries.
Definition covariance.h:209
covariance()
Constructor.
Definition covariance.h:115
covariance(TIterator first1, TIterator last1, TIterator first2)
Constructor.
Definition covariance.h:124
void add(TIterator first1, TIterator last1, TIterator first2)
Add a range.
Definition covariance.h:146
void operator()(TInput value1, TInput value2)
Definition covariance.h:160
void add(TInput value1, TInput value2)
Add a pair of values.
Definition covariance.h:133
bitset_ext
Definition absolute.h:38
Definition functional.h:125
Definition covariance.h:93
pair holds two objects of arbitrary type
Definition utility.h:164
Types for generic covariance.
Definition covariance.h:50