Loading...
Searching...
No Matches
covariance.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_COVARIANCE_INCLUDED
#define ETL_COVARIANCE_INCLUDED
#include "platform.h"
#include "functional.h"
#include "type_traits.h"
#include <stdint.h>
namespace etl
{
namespace private_covariance
{
//***************************************************************************
//***************************************************************************
template <typename TInput, typename TCalc>
struct covariance_traits
{
typedef TCalc calc_t;
};
//***************************************************************************
//***************************************************************************
template <typename TCalc>
struct covariance_traits<float, TCalc>
{
typedef float calc_t;
};
//***************************************************************************
//***************************************************************************
template <typename TCalc>
struct covariance_traits<double, TCalc>
{
typedef double calc_t;
};
} // namespace private_covariance
//***************************************************************************
//***************************************************************************
namespace private_covariance
{
template <typename T = void>
struct covariance_type_statics
{
static ETL_CONSTANT bool Sample = false;
static ETL_CONSTANT bool Population = true;
};
template <typename T>
ETL_CONSTANT bool covariance_type_statics<T>::Sample;
template <typename T>
ETL_CONSTANT bool covariance_type_statics<T>::Population;
} // namespace private_covariance
struct covariance_type : public private_covariance::covariance_type_statics<>
{
};
//***************************************************************************
//***************************************************************************
template <bool Covariance_Type, typename TInput, typename TCalc = TInput>
class covariance
: public private_covariance::covariance_traits<TInput, TCalc>
, public etl::binary_function<TInput, TInput, void>
{
private:
static ETL_CONSTANT int Adjustment = (Covariance_Type == covariance_type::Population) ? 0 : 1;
typedef typename private_covariance::covariance_traits<TInput, TCalc>::calc_t calc_t;
public:
//*********************************
//*********************************
covariance()
{
clear();
}
//*********************************
//*********************************
template <typename TIterator>
covariance(TIterator first1, TIterator last1, TIterator first2)
{
clear();
add(first1, last1, first2);
}
//*********************************
//*********************************
void add(TInput value1, TInput value2)
{
inner_product += TCalc(value1 * 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
{
if (recalculate)
{
covariance_value = 0.0;
if (counter != 0)
{
double n = double(counter);
double adjustment = 1.0 / (n * (n - Adjustment));
covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
recalculate = false;
}
}
return covariance_value;
}
//*********************************
//*********************************
operator double() const
{
return get_covariance();
}
//*********************************
//*********************************
size_t count() const
{
return size_t(counter);
}
//*********************************
//*********************************
void clear()
{
inner_product = calc_t(0);
sum1 = calc_t(0);
sum2 = calc_t(0);
counter = 0U;
covariance_value = 0.0;
recalculate = true;
}
private:
calc_t inner_product;
calc_t sum1;
calc_t sum2;
uint32_t counter;
mutable double covariance_value;
mutable bool recalculate;
};
template <bool Covariance_Type, typename TInput, typename TCalc>
ETL_CONSTANT int covariance<Covariance_Type, TInput, TCalc>::Adjustment;
} // namespace etl
#endif
etl::covariance::clear
void clear()
Clear the covariance.
Definition covariance.h:216
etl::covariance::operator()
void operator()(TIterator first1, TIterator last1, TIterator first2)
Definition covariance.h:169
etl::covariance::get_covariance
double get_covariance() const
Get the covariance.
Definition covariance.h:177
etl::covariance::count
size_t count() const
Get the total number added entries.
Definition covariance.h:208
etl::covariance::covariance
covariance()
Constructor.
Definition covariance.h:114
etl::covariance::covariance
covariance(TIterator first1, TIterator last1, TIterator first2)
Constructor.
Definition covariance.h:123
etl::covariance::add
void add(TIterator first1, TIterator last1, TIterator first2)
Add a range.
Definition covariance.h:145
etl::covariance::operator()
void operator()(TInput value1, TInput value2)
Definition covariance.h:159
etl::covariance::add
void add(TInput value1, TInput value2)
Add a pair of values.
Definition covariance.h:132
functional.h
etl::private_covariance
Covariance Type.
Definition covariance.h:43
etl
Definition absolute.h:40
platform.h
etl::binary_function
binary_function
Definition functional.h:192
etl::covariance_type
Definition covariance.h:92
etl::private_covariance::covariance_traits
Types for generic covariance.
Definition covariance.h:49
etl::private_covariance::covariance_type_statics
Definition covariance.h:79
type_traits.h