mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2025-04-11 03:20:46 +01:00
remove new covariance routine regression code
This commit is contained in:
parent
57c6004797
commit
24a7b9c203
@ -20,8 +20,7 @@ noinst_PROGRAMS = \
|
|||||||
exPValue \
|
exPValue \
|
||||||
exRand \
|
exRand \
|
||||||
exRootFinder \
|
exRootFinder \
|
||||||
exThreadPool \
|
exThreadPool
|
||||||
exVarBenchmark
|
|
||||||
|
|
||||||
exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
|
exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
|
||||||
exCompiledDoubleFunction_CXXFLAGS = $(COM_CXXFLAGS)
|
exCompiledDoubleFunction_CXXFLAGS = $(COM_CXXFLAGS)
|
||||||
@ -79,8 +78,4 @@ exThreadPool_SOURCES = exThreadPool.cpp
|
|||||||
exThreadPool_CXXFLAGS = $(COM_CXXFLAGS)
|
exThreadPool_CXXFLAGS = $(COM_CXXFLAGS)
|
||||||
exThreadPool_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
exThreadPool_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
||||||
|
|
||||||
exVarBenchmark_SOURCES = exVarBenchmark.cpp
|
|
||||||
exVarBenchmark_CXXFLAGS = $(COM_CXXFLAGS)
|
|
||||||
exVarBenchmark_LDFLAGS = -L../lib/.libs -lLatAnalyze
|
|
||||||
|
|
||||||
ACLOCAL_AMFLAGS = -I .buildutils/m4
|
ACLOCAL_AMFLAGS = -I .buildutils/m4
|
||||||
|
@ -1,119 +0,0 @@
|
|||||||
#include <LatAnalyze/Io/Io.hpp>
|
|
||||||
#include <LatAnalyze/Functional/CompiledFunction.hpp>
|
|
||||||
#include <LatAnalyze/Core/Plot.hpp>
|
|
||||||
#include <LatAnalyze/Statistics/Random.hpp>
|
|
||||||
#include <LatAnalyze/Statistics/MatSample.hpp>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace Latan;
|
|
||||||
|
|
||||||
constexpr Index size = 1000;
|
|
||||||
constexpr Index nSample = 2000;
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
random_device rd;
|
|
||||||
DMat var(size, size);
|
|
||||||
DVec mean(size);
|
|
||||||
DMatSample sample(nSample, size, 1), sample2(nSample, size, 1);
|
|
||||||
|
|
||||||
cout << "-- generating " << nSample << " Gaussian random vectors..." << endl;
|
|
||||||
var = DMat::Random(size, size);
|
|
||||||
var *= var.adjoint();
|
|
||||||
mean = DVec::Random(size);
|
|
||||||
RandomNormal mgauss(mean, var, rd());
|
|
||||||
sample[central] = mgauss();
|
|
||||||
FOR_STAT_ARRAY(sample, s)
|
|
||||||
{
|
|
||||||
sample[s] = mgauss();
|
|
||||||
}
|
|
||||||
sample2[central] = mgauss();
|
|
||||||
FOR_STAT_ARRAY(sample, s)
|
|
||||||
{
|
|
||||||
sample2[s] = mgauss();
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "-- check new routines" << endl;
|
|
||||||
|
|
||||||
DMat v, vo;
|
|
||||||
|
|
||||||
cout << "var" << endl;
|
|
||||||
auto start = chrono::high_resolution_clock::now();
|
|
||||||
vo = sample.varianceOld();
|
|
||||||
auto end = chrono::high_resolution_clock::now();
|
|
||||||
chrono::duration<double> diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
v = sample.variance();
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
cout << "diff " << (v - vo).norm() << endl;
|
|
||||||
|
|
||||||
cout << "cov" << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
vo = sample.covarianceOld(sample2);
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
v = sample.covariance(sample2);
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
cout << "diff " << (v - vo).norm() << endl;
|
|
||||||
|
|
||||||
cout << "mean" << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
vo = sample.meanOld(3, 5);
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
v = sample.mean(3, 5);
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
cout << "diff " << (v - vo).norm() << endl;
|
|
||||||
|
|
||||||
cout << "varmat" << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
vo = sample.varianceMatrixOld();
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
v = sample.varianceMatrix();
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
cout << "diff " << (v - vo).norm() << endl;
|
|
||||||
|
|
||||||
cout << "covarmat" << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
vo = sample.covarianceMatrixOld(sample2);
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
v = sample.covarianceMatrix(sample2);
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
cout << "diff " << (v - vo).norm() << endl;
|
|
||||||
|
|
||||||
cout << "corrmat" << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
vo = sample.correlationMatrixOld();
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
start = chrono::high_resolution_clock::now();
|
|
||||||
v = sample.correlationMatrix();
|
|
||||||
end = chrono::high_resolution_clock::now();
|
|
||||||
diff = end - start;
|
|
||||||
cout << "time " << diff.count() << endl;
|
|
||||||
cout << "diff " << (v - vo).norm() << endl;
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
@ -54,14 +54,8 @@ public:
|
|||||||
T sum(const Index pos = 0, const Index n = -1) const;
|
T sum(const Index pos = 0, const Index n = -1) const;
|
||||||
T meanOld(const Index pos = 0, const Index n = -1) const;
|
T meanOld(const Index pos = 0, const Index n = -1) const;
|
||||||
T mean(const Index pos = 0, const Index n = -1) const;
|
T mean(const Index pos = 0, const Index n = -1) const;
|
||||||
T covarianceOld(const StatArray<T, os> &array) const;
|
|
||||||
T covariance(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,
|
|
||||||
const Index n = -1) const;
|
|
||||||
T varianceOld(void) const;
|
|
||||||
T variance(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;
|
|
||||||
// IO type
|
// IO type
|
||||||
virtual IoType getType(void) const;
|
virtual IoType getType(void) const;
|
||||||
public:
|
public:
|
||||||
@ -151,19 +145,6 @@ void StatArray<T, os>::bin(Index binSize)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, Index os>
|
|
||||||
T StatArray<T, os>::meanOld(const Index pos, const Index n) const
|
|
||||||
{
|
|
||||||
T result = T();
|
|
||||||
const Index m = (n >= 0) ? n : size();
|
|
||||||
|
|
||||||
if (m)
|
|
||||||
{
|
|
||||||
result = this->segment(pos+os, m).redux(&StatOp::sum<T>);
|
|
||||||
}
|
|
||||||
return result/static_cast<double>(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, Index os>
|
template <typename T, Index os>
|
||||||
T StatArray<T, os>::sum(const Index pos, const Index n) const
|
T StatArray<T, os>::sum(const Index pos, const Index n) const
|
||||||
{
|
{
|
||||||
@ -187,27 +168,6 @@ T StatArray<T, os>::mean(const Index pos, const Index n) const
|
|||||||
return sum(pos, n)/static_cast<double>(m);
|
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();
|
|
||||||
|
|
||||||
if (m)
|
|
||||||
{
|
|
||||||
auto arraySeg = array.segment(os, m);
|
|
||||||
auto thisSeg = this->segment(os, m);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res/static_cast<double>(m - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, Index os>
|
template <typename T, Index os>
|
||||||
T StatArray<T, os>::covariance(const StatArray<T, os> &array) const
|
T StatArray<T, os>::covariance(const StatArray<T, os> &array) const
|
||||||
{
|
{
|
||||||
@ -226,105 +186,26 @@ T StatArray<T, os>::covariance(const StatArray<T, os> &array) const
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, Index os>
|
|
||||||
T StatArray<T, os>::covarianceMatrixOld(const StatArray<T, os> &array,
|
|
||||||
const Index pos, const Index n) const
|
|
||||||
{
|
|
||||||
T s1, s2, prs, res = T();
|
|
||||||
const Index m = (n >= 0) ? n : size();
|
|
||||||
|
|
||||||
if (m)
|
|
||||||
{
|
|
||||||
auto arraySeg = array.segment(pos+os, m);
|
|
||||||
auto thisSeg = this->segment(pos+os, m);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res/static_cast<double>(m - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, Index os>
|
template <typename T, Index os>
|
||||||
T StatArray<T, os>::variance(void) const
|
T StatArray<T, os>::variance(void) const
|
||||||
{
|
{
|
||||||
return covariance(*this);
|
return covariance(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, Index os>
|
|
||||||
T StatArray<T, os>::varianceOld(void) const
|
|
||||||
{
|
|
||||||
return covarianceOld(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, Index os>
|
|
||||||
T StatArray<T, os>::varianceMatrixOld(const Index pos, const Index n) const
|
|
||||||
{
|
|
||||||
return covarianceMatrixOld(*this, pos, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, Index os>
|
|
||||||
T StatArray<T, os>::correlationMatrixOld(const Index pos, const Index n) const
|
|
||||||
{
|
|
||||||
T res = varianceMatrixOld(pos, n);
|
|
||||||
T invDiag(res.rows(), 1);
|
|
||||||
|
|
||||||
invDiag = res.diagonal();
|
|
||||||
invDiag = invDiag.cwiseInverse().cwiseSqrt();
|
|
||||||
res = (invDiag*invDiag.transpose()).cwiseProduct(res);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
// reduction operations ////////////////////////////////////////////////////////
|
// reduction operations ////////////////////////////////////////////////////////
|
||||||
namespace StatOp
|
namespace StatOp
|
||||||
{
|
{
|
||||||
template <typename T>
|
|
||||||
inline void zero(T &a)
|
|
||||||
{
|
|
||||||
a = 0.;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
inline T sum(const T &a, const T &b)
|
|
||||||
{
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline T prod(const T &a, const T &b)
|
inline T prod(const T &a, const T &b)
|
||||||
{
|
{
|
||||||
return a*b;
|
return a*b;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
inline T tensProd(const T &v1 __dumb, const T &v2 __dumb)
|
|
||||||
{
|
|
||||||
LATAN_ERROR(Implementation,
|
|
||||||
"tensorial product not implemented for this type");
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline Mat<double> prod(const Mat<double> &a, const Mat<double> &b)
|
inline Mat<double> prod(const Mat<double> &a, const Mat<double> &b)
|
||||||
{
|
{
|
||||||
return a.cwiseProduct(b);
|
return a.cwiseProduct(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
|
||||||
inline Mat<double> tensProd(const Mat<double> &v1,
|
|
||||||
const Mat<double> &v2)
|
|
||||||
{
|
|
||||||
if ((v1.cols() != 1) or (v2.cols() != 1))
|
|
||||||
{
|
|
||||||
LATAN_ERROR(Size,
|
|
||||||
"tensorial product is only valid with column vectors");
|
|
||||||
}
|
|
||||||
|
|
||||||
return v1*v2.transpose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IO type /////////////////////////////////////////////////////////////////////
|
// IO type /////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user