2014-02-12 18:39:17 +00:00
|
|
|
/*
|
|
|
|
* StatArray.hpp, part of LatAnalyze 3
|
|
|
|
*
|
2020-01-13 09:57:06 +00:00
|
|
|
* Copyright (C) 2013 - 2020 Antonin Portelli
|
2014-02-12 18:39:17 +00:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_StatArray_hpp_
|
|
|
|
#define Latan_StatArray_hpp_
|
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Global.hpp>
|
2019-02-10 00:23:36 +00:00
|
|
|
#include <LatAnalyze/Core/Mat.hpp>
|
2014-02-12 18:39:17 +00:00
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
#define FOR_STAT_ARRAY(ar, i) \
|
|
|
|
for (Latan::Index i = -(ar).offset; i < (ar).size(); ++i)
|
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
BEGIN_LATAN_NAMESPACE
|
2014-02-12 18:39:17 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Array class with statistics *
|
|
|
|
******************************************************************************/
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os = 0>
|
2016-04-05 15:57:19 +01:00
|
|
|
class StatArray: public Array<T, dynamic, 1>, public IoObject
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-04-07 19:29:47 +01:00
|
|
|
protected:
|
2014-02-26 18:36:29 +00:00
|
|
|
typedef Array<T, dynamic, 1> Base;
|
2014-02-12 18:39:17 +00:00
|
|
|
public:
|
|
|
|
// constructors
|
|
|
|
StatArray(void);
|
2014-03-03 14:21:37 +00:00
|
|
|
explicit StatArray(const Index size);
|
2014-03-17 15:00:59 +00:00
|
|
|
EIGEN_EXPR_CTOR(StatArray, unique_arg(StatArray<T, os>), Base, ArrayExpr)
|
2014-02-12 18:39:17 +00:00
|
|
|
// destructor
|
2014-02-20 22:54:11 +00:00
|
|
|
virtual ~StatArray(void) = default;
|
2014-02-17 18:48:44 +00:00
|
|
|
// access
|
2014-02-26 18:36:29 +00:00
|
|
|
Index size(void) const;
|
2014-03-12 19:03:36 +00:00
|
|
|
void resize(const Index size);
|
2014-02-17 18:48:44 +00:00
|
|
|
// operators
|
2014-03-03 12:41:48 +00:00
|
|
|
T & operator[](const Index s);
|
|
|
|
const T & operator[](const Index s) const;
|
2014-02-12 18:39:17 +00:00
|
|
|
// statistics
|
2014-02-26 18:36:29 +00:00
|
|
|
void bin(Index binSize);
|
2021-12-20 00:25:13 +00:00
|
|
|
T sum(const Index pos = 0, const Index n = -1) const;
|
|
|
|
T meanOld(const Index pos = 0, const Index n = -1) const;
|
2014-03-12 19:03:36 +00:00
|
|
|
T mean(const Index pos = 0, const Index n = -1) const;
|
2021-12-20 00:25:13 +00:00
|
|
|
T covarianceOld(const StatArray<T, os> &array) const;
|
|
|
|
T covariance(const StatArray<T, os> &array) const;
|
|
|
|
T covarianceMatrixOld(const StatArray<T, os> &array, const Index pos = 0,
|
2014-03-12 19:03:36 +00:00
|
|
|
const Index n = -1) const;
|
2021-12-20 00:25:13 +00:00
|
|
|
T varianceOld(void) const;
|
|
|
|
T variance(void) const;
|
|
|
|
T varianceMatrixOld(const Index pos = 0, const Index n = -1) const;
|
|
|
|
T correlationMatrixOld(const Index pos = 0, const Index n = -1) const;
|
2016-04-05 15:57:19 +01:00
|
|
|
// IO type
|
|
|
|
virtual IoType getType(void) const;
|
2014-03-04 17:16:36 +00:00
|
|
|
public:
|
2016-04-05 15:57:19 +01:00
|
|
|
static constexpr Index offset = os;
|
2014-02-12 18:39:17 +00:00
|
|
|
};
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// reduction operations
|
2021-12-20 00:25:13 +00:00
|
|
|
namespace StatOp
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2014-03-12 19:03:36 +00:00
|
|
|
// general templates
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T>
|
2014-03-12 19:03:36 +00:00
|
|
|
inline T prod(const T &a, const T &b);
|
|
|
|
template <typename T>
|
|
|
|
inline T tensProd(const T &v1, const T &v2);
|
2014-02-17 18:48:44 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T sum(const T &a, const T &b);
|
|
|
|
}
|
2014-02-12 18:39:17 +00:00
|
|
|
|
2014-03-12 19:23:22 +00:00
|
|
|
// Sample types
|
2016-04-05 15:57:19 +01:00
|
|
|
const int central = -1;
|
2014-03-12 19:23:22 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2016-04-05 15:57:19 +01:00
|
|
|
using Sample = StatArray<T, 1>;
|
2014-03-12 19:23:22 +00:00
|
|
|
|
2014-04-15 17:29:54 +01:00
|
|
|
typedef Sample<double> DSample;
|
|
|
|
typedef Sample<std::complex<double>> CSample;
|
2014-03-12 19:23:22 +00:00
|
|
|
|
2014-02-12 18:39:17 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* StatArray class template implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
StatArray<T, os>::StatArray(void)
|
|
|
|
: Base(static_cast<typename Base::Index>(os))
|
2014-02-12 18:39:17 +00:00
|
|
|
{}
|
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
StatArray<T, os>::StatArray(const Index size)
|
|
|
|
: Base(static_cast<typename Base::Index>(size + os))
|
2014-02-12 18:39:17 +00:00
|
|
|
{}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
Index StatArray<T, os>::size(void) const
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2014-03-04 17:16:36 +00:00
|
|
|
return Base::size() - os;
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:03:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
void StatArray<T, os>::resize(const Index size)
|
|
|
|
{
|
|
|
|
Base::resize(size + os);
|
|
|
|
}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// operators ///////////////////////////////////////////////////////////////////
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
T & StatArray<T, os>::operator[](const Index s)
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2014-03-04 17:16:36 +00:00
|
|
|
return Base::operator[](s + os);
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
const T & StatArray<T, os>::operator[](const Index s) const
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2014-03-04 17:16:36 +00:00
|
|
|
return Base::operator[](s + os);
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-12 18:39:17 +00:00
|
|
|
// statistics //////////////////////////////////////////////////////////////////
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
|
|
|
void StatArray<T, os>::bin(Index binSize)
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2014-02-26 18:36:29 +00:00
|
|
|
Index q = size()/binSize, r = size()%binSize;
|
2014-02-17 18:48:44 +00:00
|
|
|
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < q; ++i)
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
|
|
|
(*this)[i] = mean(i*binSize, binSize);
|
|
|
|
}
|
|
|
|
if (r != 0)
|
|
|
|
{
|
|
|
|
(*this)[q] = mean(q*binSize, r);
|
2014-03-04 17:16:36 +00:00
|
|
|
this->conservativeResize(os + q + 1);
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-04 17:16:36 +00:00
|
|
|
this->conservativeResize(os + q);
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
2021-12-20 00:25:13 +00:00
|
|
|
T StatArray<T, os>::meanOld(const Index pos, const Index n) const
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-04-08 19:47:09 +01:00
|
|
|
T result = T();
|
2014-03-12 19:03:36 +00:00
|
|
|
const Index m = (n >= 0) ? n : size();
|
2015-03-20 16:05:11 +00:00
|
|
|
|
2014-03-12 19:03:36 +00:00
|
|
|
if (m)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
result = this->segment(pos+os, m).redux(&StatOp::sum<T>);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
2015-03-20 16:05:11 +00:00
|
|
|
return result/static_cast<double>(m);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
2021-12-20 00:25:13 +00:00
|
|
|
T StatArray<T, os>::sum(const Index pos, const Index n) const
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
T result;
|
2014-03-12 19:03:36 +00:00
|
|
|
const Index m = (n >= 0) ? n : size();
|
2021-12-20 00:25:13 +00:00
|
|
|
|
|
|
|
result = (*this)[pos];
|
|
|
|
for (Index i = pos + 1; i < pos + m; ++i)
|
|
|
|
{
|
|
|
|
result += (*this)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, Index os>
|
|
|
|
T StatArray<T, os>::mean(const Index pos, const Index n) const
|
|
|
|
{
|
|
|
|
const Index m = (n >= 0) ? n : size();
|
|
|
|
|
|
|
|
return sum(pos, n)/static_cast<double>(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, Index os>
|
|
|
|
T StatArray<T, os>::covarianceOld(const StatArray<T, os> &array) const
|
|
|
|
{
|
|
|
|
T s1, s2, prs, res = T();
|
|
|
|
const Index m = size();
|
2014-03-12 19:03:36 +00:00
|
|
|
|
|
|
|
if (m)
|
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
auto arraySeg = array.segment(os, m);
|
|
|
|
auto thisSeg = this->segment(os, m);
|
2014-03-12 19:03:36 +00:00
|
|
|
|
2021-12-20 00:25:13 +00:00
|
|
|
s1 = thisSeg.redux(&StatOp::sum<T>);
|
|
|
|
s2 = arraySeg.redux(&StatOp::sum<T>);
|
|
|
|
prs = thisSeg.binaryExpr(arraySeg, &StatOp::prod<T>)
|
|
|
|
.redux(&StatOp::sum<T>);
|
|
|
|
res = prs - StatOp::prod(s1, s2)/static_cast<double>(m);
|
2014-03-12 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res/static_cast<double>(m - 1);
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
2021-12-20 00:25:13 +00:00
|
|
|
T StatArray<T, os>::covariance(const StatArray<T, os> &array) const
|
|
|
|
{
|
|
|
|
T s1, s2, res;
|
|
|
|
|
|
|
|
s1 = array.sum();
|
|
|
|
s2 = this->sum();
|
|
|
|
res = StatOp::prod<T>(array[0], (*this)[0]);
|
|
|
|
for (Index i = 1; i < size(); ++i)
|
|
|
|
{
|
|
|
|
res += StatOp::prod<T>(array[i], (*this)[i]);
|
|
|
|
}
|
|
|
|
res -= StatOp::prod<T>(s1, s2)/static_cast<double>(size());
|
|
|
|
res /= static_cast<double>(size() - 1);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, Index os>
|
|
|
|
T StatArray<T, os>::covarianceMatrixOld(const StatArray<T, os> &array,
|
2014-03-12 19:03:36 +00:00
|
|
|
const Index pos, const Index n) const
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-04-08 19:47:09 +01:00
|
|
|
T s1, s2, prs, res = T();
|
2014-03-12 19:03:36 +00:00
|
|
|
const Index m = (n >= 0) ? n : size();
|
2014-02-12 18:39:17 +00:00
|
|
|
|
2014-03-12 19:03:36 +00:00
|
|
|
if (m)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-03-12 19:03:36 +00:00
|
|
|
auto arraySeg = array.segment(pos+os, m);
|
|
|
|
auto thisSeg = this->segment(pos+os, m);
|
|
|
|
|
2021-12-20 00:25:13 +00:00
|
|
|
s1 = thisSeg.redux(&StatOp::sum<T>);
|
|
|
|
s2 = arraySeg.redux(&StatOp::sum<T>);
|
|
|
|
prs = thisSeg.binaryExpr(arraySeg, &StatOp::tensProd<T>)
|
|
|
|
.redux(&StatOp::sum<T>);
|
|
|
|
res = prs - StatOp::tensProd(s1, s2)/static_cast<double>(m);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 19:03:36 +00:00
|
|
|
return res/static_cast<double>(m - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, Index os>
|
2021-12-20 00:25:13 +00:00
|
|
|
T StatArray<T, os>::variance(void) const
|
|
|
|
{
|
|
|
|
return covariance(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, Index os>
|
|
|
|
T StatArray<T, os>::varianceOld(void) const
|
2014-03-12 19:03:36 +00:00
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
return covarianceOld(*this);
|
2014-02-17 18:48:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:16:36 +00:00
|
|
|
template <typename T, Index os>
|
2021-12-20 00:25:13 +00:00
|
|
|
T StatArray<T, os>::varianceMatrixOld(const Index pos, const Index n) const
|
2014-02-17 18:48:44 +00:00
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
return covarianceMatrixOld(*this, pos, n);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 17:35:10 +01:00
|
|
|
template <typename T, Index os>
|
2021-12-20 00:25:13 +00:00
|
|
|
T StatArray<T, os>::correlationMatrixOld(const Index pos, const Index n) const
|
2015-05-13 17:35:10 +01:00
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
T res = varianceMatrixOld(pos, n);
|
2015-05-13 17:35:10 +01:00
|
|
|
T invDiag(res.rows(), 1);
|
|
|
|
|
|
|
|
invDiag = res.diagonal();
|
|
|
|
invDiag = invDiag.cwiseInverse().cwiseSqrt();
|
|
|
|
res = (invDiag*invDiag.transpose()).cwiseProduct(res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-02-17 18:48:44 +00:00
|
|
|
// reduction operations ////////////////////////////////////////////////////////
|
2021-12-20 00:25:13 +00:00
|
|
|
namespace StatOp
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2021-12-20 00:25:13 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void zero(T &a)
|
|
|
|
{
|
|
|
|
a = 0.;
|
|
|
|
}
|
|
|
|
|
2015-06-15 13:59:10 +01:00
|
|
|
template <typename T>
|
|
|
|
inline T sum(const T &a, const T &b)
|
|
|
|
{
|
|
|
|
return a + b;
|
|
|
|
}
|
2014-02-12 18:39:17 +00:00
|
|
|
|
2015-06-15 13:59:10 +01:00
|
|
|
template <typename T>
|
|
|
|
inline T prod(const T &a, const T &b)
|
|
|
|
{
|
|
|
|
return a*b;
|
|
|
|
}
|
2014-03-12 19:03:36 +00:00
|
|
|
|
2015-06-15 13:59:10 +01:00
|
|
|
template <typename T>
|
2015-06-15 14:20:29 +01:00
|
|
|
inline T tensProd(const T &v1 __dumb, const T &v2 __dumb)
|
2015-06-15 13:59:10 +01:00
|
|
|
{
|
|
|
|
LATAN_ERROR(Implementation,
|
|
|
|
"tensorial product not implemented for this type");
|
|
|
|
}
|
2014-02-12 18:39:17 +00:00
|
|
|
|
2015-06-15 13:59:10 +01:00
|
|
|
template <>
|
|
|
|
inline Mat<double> prod(const Mat<double> &a, const Mat<double> &b)
|
|
|
|
{
|
|
|
|
return a.cwiseProduct(b);
|
|
|
|
}
|
2014-03-12 19:03:36 +00:00
|
|
|
|
2015-06-15 13:59:10 +01:00
|
|
|
template <>
|
|
|
|
inline Mat<double> tensProd(const Mat<double> &v1,
|
|
|
|
const Mat<double> &v2)
|
2014-03-12 19:03:36 +00:00
|
|
|
{
|
2016-04-20 03:21:05 +01:00
|
|
|
if ((v1.cols() != 1) or (v2.cols() != 1))
|
2015-06-15 13:59:10 +01:00
|
|
|
{
|
|
|
|
LATAN_ERROR(Size,
|
|
|
|
"tensorial product is only valid with column vectors");
|
|
|
|
}
|
|
|
|
|
|
|
|
return v1*v2.transpose();
|
2014-03-12 19:03:36 +00:00
|
|
|
}
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 15:57:19 +01:00
|
|
|
// IO type /////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T, Index os>
|
|
|
|
IoObject::IoType StatArray<T, os>::getType(void) const
|
|
|
|
{
|
|
|
|
return IoType::noType;
|
|
|
|
}
|
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
END_LATAN_NAMESPACE
|
2014-02-12 18:39:17 +00:00
|
|
|
|
|
|
|
#endif // Latan_StatArray_hpp_
|