1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 08:55:37 +00:00

remove new covariance routine regression code

This commit is contained in:
Antonin Portelli 2021-12-20 01:29:29 +01:00
parent 57c6004797
commit 24a7b9c203
3 changed files with 1 additions and 244 deletions

View File

@ -20,8 +20,7 @@ noinst_PROGRAMS = \
exPValue \
exRand \
exRootFinder \
exThreadPool \
exVarBenchmark
exThreadPool
exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
exCompiledDoubleFunction_CXXFLAGS = $(COM_CXXFLAGS)
@ -79,8 +78,4 @@ exThreadPool_SOURCES = exThreadPool.cpp
exThreadPool_CXXFLAGS = $(COM_CXXFLAGS)
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

View File

@ -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;
}

View File

@ -54,14 +54,8 @@ public:
T sum(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 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,
const Index n = -1) const;
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;
// IO type
virtual IoType getType(void) const;
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>
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);
}
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>
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;
}
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>
T StatArray<T, os>::variance(void) const
{
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 ////////////////////////////////////////////////////////
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>
inline T prod(const T &a, const T &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 <>
inline Mat<double> prod(const Mat<double> &a, const Mat<double> &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 /////////////////////////////////////////////////////////////////////