Embedded Template Library 1.0
Loading...
Searching...
No Matches
correlation.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_CORRELATION_INCLUDED
32#define ETL_CORRELATION_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_correlation
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_correlation
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
95
96 //***************************************************************************
98 //***************************************************************************
99 template <bool Correlation_Type, typename TInput, typename TCalc = TInput>
101 : public private_correlation::correlation_traits<TInput, TCalc>
102 , public etl::binary_function<TInput, TInput, void>
103 {
104 private:
105
106 static ETL_CONSTANT int Adjustment = (Correlation_Type == correlation_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 sum_of_squares1 += TCalc(value1 * value1);
137 sum_of_squares2 += TCalc(value2 * value2);
138 sum1 += TCalc(value1);
139 sum2 += TCalc(value2);
140 ++counter;
141 recalculate = true;
142 }
143
144 //*********************************
146 //*********************************
147 template <typename TIterator>
149 {
150 while (first1 != last1)
151 {
152 add(*first1, *first2);
153 ++first1;
154 ++first2;
155 }
156 }
157
158 //*********************************
161 //*********************************
162 void operator ()(TInput value1, TInput value2)
163 {
164 add(value1, value2);
165 }
166
167 //*********************************
170 //*********************************
171 template <typename TIterator>
176
177 //*********************************
179 //*********************************
180 double get_covariance() const
181 {
182 calculate();
183
184 return covariance_value;
185 }
186
187 //*********************************
189 //*********************************
190 double get_correlation() const
191 {
192 calculate();
193
194 return correlation_value;
195 }
196
197 //*********************************
199 //*********************************
200 operator double() const
201 {
202 return get_correlation();
203 }
204
205 //*********************************
207 //*********************************
208 size_t count() const
209 {
210 return size_t(counter);
211 }
212
213 //*********************************
215 //*********************************
216 void clear()
217 {
218 inner_product = calc_t(0);
219 sum_of_squares1 = calc_t(0);
220 sum_of_squares2 = calc_t(0);
221 sum1 = calc_t(0);
222 sum2 = calc_t(0);
223 counter = 0U;
224 covariance_value = 0.0;
225 correlation_value = 0.0;
226 recalculate = true;
227 }
228
229 private:
230
231 //*********************************
233 //*********************************
234 void calculate() const
235 {
236 if (recalculate)
237 {
238 correlation_value = 0.0;
239 covariance_value = 0.0;
240
241 if (counter != 0)
242 {
243 double n = double(counter);
244 double adjustment = 1.0 / (n * (n - Adjustment));
245
246 double square_of_sum1 = (sum1 * sum1);
247 double square_of_sum2 = (sum2 * sum2);
248
249 double variance1 = ((n * sum_of_squares1) - square_of_sum1) * adjustment;
250 double variance2 = ((n * sum_of_squares2) - square_of_sum2) * adjustment;
251
252 double stddev1 = 0.0;
253 double stddev2 = 0.0;
254
255 if (variance1 > 0)
256 {
258 }
259
260 if (variance2 > 0)
261 {
262 stddev2 = sqrt(variance2);
263 }
264
265 covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
266
267 if ((stddev1 > 0.0) && (stddev2 > 0.0))
268 {
269 correlation_value = covariance_value / (stddev1 * stddev2);
270 }
271 }
272
273 recalculate = false;
274 }
275 }
276
277 calc_t inner_product;
278 calc_t sum_of_squares1;
279 calc_t sum_of_squares2;
280 calc_t sum1;
281 calc_t sum2;
282 uint32_t counter;
283 mutable double covariance_value;
284 mutable double correlation_value;
285 mutable bool recalculate;
286 };
287
288 template <bool Correlation_Type, typename TInput, typename TCalc>
289 ETL_CONSTANT int correlation<Correlation_Type, TInput, TCalc>::Adjustment;
290}
291
292#endif
Correlation.
Definition correlation.h:103
double get_covariance() const
Get the correlation.
Definition correlation.h:180
void add(TIterator first1, TIterator last1, TIterator first2)
Add a range.
Definition correlation.h:148
void add(TInput value1, TInput value2)
Add a pair of values.
Definition correlation.h:133
void clear()
Clear the correlation.
Definition correlation.h:216
double get_correlation() const
Get the correlation.
Definition correlation.h:190
void operator()(TInput value1, TInput value2)
Definition correlation.h:162
correlation(TIterator first1, TIterator last1, TIterator first2)
Constructor.
Definition correlation.h:124
size_t count() const
Get the total number added entries.
Definition correlation.h:208
correlation()
Constructor.
Definition correlation.h:115
bitset_ext
Definition absolute.h:38
Definition functional.h:126
Definition correlation.h:93
pair holds two objects of arbitrary type
Definition utility.h:164
Types for generic correlation.
Definition correlation.h:50
Calculates the smallest value that, when squared, will be not greater than VALUE.
Definition sqrt.h:47