Loading...
Searching...
No Matches
correlation.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_CORRELATION_INCLUDED
#define ETL_CORRELATION_INCLUDED
#include "platform.h"
#include "functional.h"
#include "type_traits.h"
#include <math.h>
#include <stdint.h>
namespace etl
{
namespace private_correlation
{
//***************************************************************************
//***************************************************************************
template <typename TInput, typename TCalc>
struct correlation_traits
{
typedef TCalc calc_t;
};
//***************************************************************************
//***************************************************************************
template <typename TCalc>
struct correlation_traits<float, TCalc>
{
typedef float calc_t;
};
//***************************************************************************
//***************************************************************************
template <typename TCalc>
struct correlation_traits<double, TCalc>
{
typedef double calc_t;
};
} // namespace private_correlation
//***************************************************************************
//***************************************************************************
namespace private_correlation
{
template <typename T = void>
struct correlation_type_statics
{
static ETL_CONSTANT bool Sample = false;
static ETL_CONSTANT bool Population = true;
};
template <typename T>
ETL_CONSTANT bool correlation_type_statics<T>::Sample;
template <typename T>
ETL_CONSTANT bool correlation_type_statics<T>::Population;
} // namespace private_correlation
struct correlation_type : public private_correlation::correlation_type_statics<>
{
};
//***************************************************************************
//***************************************************************************
template <bool Correlation_Type, typename TInput, typename TCalc = TInput>
class correlation
: public private_correlation::correlation_traits<TInput, TCalc>
, public etl::binary_function<TInput, TInput, void>
{
private:
static ETL_CONSTANT int Adjustment = (Correlation_Type == correlation_type::Population) ? 0 : 1;
typedef typename private_correlation::correlation_traits<TInput, TCalc>::calc_t calc_t;
public:
//*********************************
//*********************************
correlation()
{
clear();
}
//*********************************
//*********************************
template <typename TIterator>
correlation(TIterator first1, TIterator last1, TIterator first2)
{
clear();
add(first1, last1, first2);
}
//*********************************
//*********************************
void add(TInput value1, TInput value2)
{
inner_product += TCalc(value1 * value2);
sum_of_squares1 += TCalc(value1 * value1);
sum_of_squares2 += TCalc(value2 * value2);
sum1 += TCalc(value1);
sum2 += TCalc(value2);
++counter;
recalculate = true;
}
//*********************************
//*********************************
template <typename TIterator>
void add(TIterator first1, TIterator last1, TIterator first2)
{
while (first1 != last1)
{
add(*first1, *first2);
++first1;
++first2;
}
}
//*********************************
//*********************************
void operator()(TInput value1, TInput value2)
{
add(value1, value2);
}
//*********************************
//*********************************
template <typename TIterator>
void operator()(TIterator first1, TIterator last1, TIterator first2)
{
add(first1, last1, first2);
}
//*********************************
//*********************************
double get_covariance() const
{
calculate();
return covariance_value;
}
//*********************************
//*********************************
double get_correlation() const
{
calculate();
return correlation_value;
}
//*********************************
//*********************************
operator double() const
{
return get_correlation();
}
//*********************************
//*********************************
size_t count() const
{
return size_t(counter);
}
//*********************************
//*********************************
void clear()
{
inner_product = calc_t(0);
sum_of_squares1 = calc_t(0);
sum_of_squares2 = calc_t(0);
sum1 = calc_t(0);
sum2 = calc_t(0);
counter = 0U;
covariance_value = 0.0;
correlation_value = 0.0;
recalculate = true;
}
private:
//*********************************
//*********************************
void calculate() const
{
if (recalculate)
{
correlation_value = 0.0;
covariance_value = 0.0;
if (counter != 0)
{
double n = double(counter);
double adjustment = 1.0 / (n * (n - Adjustment));
double square_of_sum1 = (sum1 * sum1);
double square_of_sum2 = (sum2 * sum2);
double variance1 = ((n * sum_of_squares1) - square_of_sum1) * adjustment;
double variance2 = ((n * sum_of_squares2) - square_of_sum2) * adjustment;
double stddev1 = 0.0;
double stddev2 = 0.0;
if (variance1 > 0)
{
stddev1 = sqrt(variance1);
}
if (variance2 > 0)
{
stddev2 = sqrt(variance2);
}
covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
if ((stddev1 > 0.0) && (stddev2 > 0.0))
{
correlation_value = covariance_value / (stddev1 * stddev2);
}
}
recalculate = false;
}
}
calc_t inner_product;
calc_t sum_of_squares1;
calc_t sum_of_squares2;
calc_t sum1;
calc_t sum2;
uint32_t counter;
mutable double covariance_value;
mutable double correlation_value;
mutable bool recalculate;
};
template <bool Correlation_Type, typename TInput, typename TCalc>
ETL_CONSTANT int correlation<Correlation_Type, TInput, TCalc>::Adjustment;
} // namespace etl
#endif
etl::correlation::get_covariance
double get_covariance() const
Get the correlation.
Definition correlation.h:180
etl::correlation::add
void add(TIterator first1, TIterator last1, TIterator first2)
Add a range.
Definition correlation.h:148
etl::correlation::add
void add(TInput value1, TInput value2)
Add a pair of values.
Definition correlation.h:133
etl::correlation::clear
void clear()
Clear the correlation.
Definition correlation.h:216
etl::correlation::get_correlation
double get_correlation() const
Get the correlation.
Definition correlation.h:190
etl::correlation::operator()
void operator()(TInput value1, TInput value2)
Definition correlation.h:162
etl::correlation::correlation
correlation(TIterator first1, TIterator last1, TIterator first2)
Constructor.
Definition correlation.h:124
etl::correlation::count
size_t count() const
Get the total number added entries.
Definition correlation.h:208
etl::correlation::correlation
correlation()
Constructor.
Definition correlation.h:115
etl::correlation::operator()
void operator()(TIterator first1, TIterator last1, TIterator first2)
Definition correlation.h:172
functional.h
math.h
etl::private_correlation
Correlation Type.
Definition correlation.h:44
etl
Definition absolute.h:40
platform.h
etl::binary_function
binary_function
Definition functional.h:192
etl::correlation_type
Definition correlation.h:93
etl::private_correlation::correlation_traits
Types for generic correlation.
Definition correlation.h:50
etl::private_correlation::correlation_type_statics
Definition correlation.h:80
etl::sqrt
Definition sqrt.h:48
type_traits.h