mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2024-11-10 08:55:37 +00:00
first implementation of samples and datasets, still need IO for samples
This commit is contained in:
parent
1a0cb2459e
commit
f9e355e3ea
155
latan/Dataset.hpp
Normal file
155
latan/Dataset.hpp
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
/*
|
||||||
|
* Dataset.hpp, part of LatAnalyze 3
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013 - 2014 Antonin Portelli
|
||||||
|
*
|
||||||
|
* 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_Dataset_hpp_
|
||||||
|
#define Latan_Dataset_hpp_
|
||||||
|
|
||||||
|
#include <latan/Global.hpp>
|
||||||
|
#include <latan/Io.hpp>
|
||||||
|
#include <latan/Sample.hpp>
|
||||||
|
#include <latan/RandGen.hpp>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Dataset class *
|
||||||
|
******************************************************************************/
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
class Dataset: public StatArray<T>
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
typedef StatArray<T> Base;
|
||||||
|
public:
|
||||||
|
// constructor
|
||||||
|
Dataset(void);
|
||||||
|
Dataset(const std::string &listFileName, const std::string &dataName);
|
||||||
|
template <typename Derived>
|
||||||
|
Dataset(const Eigen::EigenBase<Derived> &dataset);
|
||||||
|
// destructor
|
||||||
|
virtual ~Dataset(void);
|
||||||
|
// IO
|
||||||
|
void load(const std::string &listFileName, const std::string &dataName);
|
||||||
|
// resampling
|
||||||
|
Sample<T> bootstrapMean(const unsigned int nSample, RandGen& generator);
|
||||||
|
private:
|
||||||
|
// mean from pointer vector for resampling
|
||||||
|
void ptVectorMean(T &m, const std::vector<const T *> &v);
|
||||||
|
private:
|
||||||
|
FileType file_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Dataset template implementation *
|
||||||
|
******************************************************************************/
|
||||||
|
// constructor /////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
Dataset<T, FileType>::Dataset(void)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
Dataset<T, FileType>::Dataset(const std::string &listFileName,
|
||||||
|
const std::string &dataName)
|
||||||
|
{
|
||||||
|
load(listFileName, dataName);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
template <typename Derived>
|
||||||
|
Dataset<T, FileType>::Dataset(const Eigen::EigenBase<Derived> &dataset)
|
||||||
|
: Base(dataset)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
Dataset<T, FileType>::~Dataset(void)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// IO //////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
void Dataset<T, FileType>::load(const std::string &listFileName,
|
||||||
|
const std::string &dataName)
|
||||||
|
{
|
||||||
|
std::ifstream listFile;
|
||||||
|
char dataFileNameBuf[MAX_PATH_LENGTH];
|
||||||
|
std::vector<std::string> dataFileName;
|
||||||
|
|
||||||
|
listFile.open(listFileName, std::ios::in);
|
||||||
|
while (!listFile.eof())
|
||||||
|
{
|
||||||
|
listFile.getline(dataFileNameBuf, MAX_PATH_LENGTH);
|
||||||
|
if (!std::string(dataFileNameBuf).empty())
|
||||||
|
{
|
||||||
|
dataFileName.push_back(dataFileNameBuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
listFile.close();
|
||||||
|
this->resize(dataFileName.size());
|
||||||
|
for (unsigned int i = 0; i < dataFileName.size(); ++i)
|
||||||
|
{
|
||||||
|
file_.open(dataFileName[i], File::Mode::read);
|
||||||
|
(*this)[i] = file_.template read<T>(dataName);
|
||||||
|
file_.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resampling //////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
Sample<T> Dataset<T, FileType>::bootstrapMean(const unsigned int nSample,
|
||||||
|
RandGen& generator)
|
||||||
|
{
|
||||||
|
unsigned int nData = this->size();
|
||||||
|
std::vector<const T *> data(nData);
|
||||||
|
Sample<T> s(nSample);
|
||||||
|
|
||||||
|
for (unsigned int j = 0; j < nData; ++j)
|
||||||
|
{
|
||||||
|
data[j] = &((*this)[j]);
|
||||||
|
}
|
||||||
|
ptVectorMean(s[central], data);
|
||||||
|
for (unsigned int i = 0; i < nSample; ++i)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < nData; ++j)
|
||||||
|
{
|
||||||
|
data[j] = &((*this)[generator.discreteUniform(nData)]);
|
||||||
|
}
|
||||||
|
ptVectorMean(s[i], data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename FileType>
|
||||||
|
void Dataset<T, FileType>::ptVectorMean(T &m, const std::vector<const T *> &v)
|
||||||
|
{
|
||||||
|
if (v.size())
|
||||||
|
{
|
||||||
|
m = *(v[0]);
|
||||||
|
for (unsigned int i = 1; i < v.size(); ++i)
|
||||||
|
{
|
||||||
|
m += *(v[i]);
|
||||||
|
}
|
||||||
|
m /= static_cast<double>(v.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // Latan_Dataset_hpp_
|
@ -35,7 +35,7 @@ public:
|
|||||||
{
|
{
|
||||||
noType = 0,
|
noType = 0,
|
||||||
dMat = 1,
|
dMat = 1,
|
||||||
sample = 2,
|
dMatSample = 2,
|
||||||
rgState = 3
|
rgState = 3
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -45,6 +45,7 @@ liblatan_la_SOURCES = \
|
|||||||
liblatan_ladir = $(pkgincludedir)
|
liblatan_ladir = $(pkgincludedir)
|
||||||
liblatan_la_HEADERS = \
|
liblatan_la_HEADERS = \
|
||||||
CompiledFunction.hpp\
|
CompiledFunction.hpp\
|
||||||
|
Dataset.hpp \
|
||||||
Function.hpp \
|
Function.hpp \
|
||||||
Global.hpp \
|
Global.hpp \
|
||||||
Io.hpp \
|
Io.hpp \
|
||||||
@ -55,7 +56,8 @@ liblatan_la_HEADERS = \
|
|||||||
ParserState.hpp \
|
ParserState.hpp \
|
||||||
Plot.hpp \
|
Plot.hpp \
|
||||||
RandGen.hpp \
|
RandGen.hpp \
|
||||||
Sample.hpp
|
Sample.hpp \
|
||||||
|
StatArray.hpp
|
||||||
liblatan_la_CFLAGS = $(COM_CFLAGS)
|
liblatan_la_CFLAGS = $(COM_CFLAGS)
|
||||||
liblatan_la_CXXFLAGS = $(COM_CXXFLAGS)
|
liblatan_la_CXXFLAGS = $(COM_CXXFLAGS)
|
||||||
|
|
||||||
|
@ -21,29 +21,10 @@
|
|||||||
#include <latan/includes.hpp>
|
#include <latan/includes.hpp>
|
||||||
|
|
||||||
using namespace Latan;
|
using namespace Latan;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
DSample::DSample(void)
|
template <>
|
||||||
: DSampleBase(static_cast<Index>(0))
|
unsigned int Sample<DMat>::getType(void) const
|
||||||
{}
|
|
||||||
|
|
||||||
DSample::DSample(const unsigned int nSample, const unsigned int nRow,
|
|
||||||
const unsigned int nCol)
|
|
||||||
: DSampleBase(static_cast<Index>(nSample + 1))
|
|
||||||
{
|
{
|
||||||
for (int s = 0; s < size(); ++s)
|
return IoType::dMatSample;
|
||||||
{
|
|
||||||
(*this)(s).resize(nRow, nCol);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DMat& DSample::operator()(const int s)
|
|
||||||
{
|
|
||||||
if (s >= 0)
|
|
||||||
{
|
|
||||||
return (*this)(s + 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return (*this)(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -21,26 +21,98 @@
|
|||||||
#define Latan_Sample_hpp_
|
#define Latan_Sample_hpp_
|
||||||
|
|
||||||
#include <latan/Global.hpp>
|
#include <latan/Global.hpp>
|
||||||
|
#include <latan/IoObject.hpp>
|
||||||
#include <latan/Mat.hpp>
|
#include <latan/Mat.hpp>
|
||||||
|
#include <latan/StatArray.hpp>
|
||||||
|
|
||||||
BEGIN_NAMESPACE
|
BEGIN_NAMESPACE
|
||||||
|
|
||||||
const int Central = -1;
|
const int central = -1;
|
||||||
|
|
||||||
typedef Eigen::Array<DMat, Eigen::Dynamic, 1> DSampleBase;
|
/******************************************************************************
|
||||||
|
* Sample class *
|
||||||
class DSample: public DSampleBase
|
******************************************************************************/
|
||||||
|
template <typename T>
|
||||||
|
class Sample: public StatArray<T>, public IoObject
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
typedef StatArray<T> Base;
|
||||||
public:
|
public:
|
||||||
// Constructors/destructor
|
// constructors
|
||||||
DSample(void);
|
Sample(void);
|
||||||
DSample(const unsigned int nSample, const unsigned int nRow,
|
Sample(const unsigned int nSample);
|
||||||
const unsigned int nCol);
|
template <typename Derived>
|
||||||
~DSample(void);
|
Sample(const Eigen::EigenBase<Derived> &s);
|
||||||
// Operators
|
// destructor
|
||||||
DMat& operator()(const int s);
|
virtual ~Sample(void);
|
||||||
|
// operators
|
||||||
|
T& operator[](const int s);
|
||||||
|
// IO type
|
||||||
|
virtual unsigned int getType(void) const;
|
||||||
|
private:
|
||||||
|
// index of the first element to take into account for statistics
|
||||||
|
virtual unsigned int getOffset(void) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
unsigned int Sample<DMat>::getType(void) const;
|
||||||
|
|
||||||
|
// specialization aliases
|
||||||
|
typedef Sample<DMat> DMatSample;
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Sample class template implementation *
|
||||||
|
******************************************************************************/
|
||||||
|
// constructor /////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
Sample<T>::Sample(void)
|
||||||
|
: Base(static_cast<typename Base::Index>(getOffset()))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Sample<T>::Sample(const unsigned int nSample)
|
||||||
|
: Base(static_cast<typename Base::Index>(nSample + getOffset()))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
template <typename Derived>
|
||||||
|
Sample<T>::Sample(const Eigen::EigenBase<Derived> &s)
|
||||||
|
: Base(s)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
Sample<T>::~Sample(void)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// operators ///////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
T& Sample<T>::operator[](const int s)
|
||||||
|
{
|
||||||
|
if (s >= 0)
|
||||||
|
{
|
||||||
|
return Base::operator[](s + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Base::operator[](0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IO type /////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
unsigned int Sample<T>::getType(void) const
|
||||||
|
{
|
||||||
|
return IoType::noType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// statistics //////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
unsigned int Sample<T>::getOffset(void) const
|
||||||
|
{
|
||||||
|
return 1u;
|
||||||
|
}
|
||||||
|
|
||||||
END_NAMESPACE
|
END_NAMESPACE
|
||||||
|
|
||||||
#endif // Latan_Sample_hpp_
|
#endif // Latan_Sample_hpp_
|
||||||
|
141
latan/StatArray.hpp
Normal file
141
latan/StatArray.hpp
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
* StatArray.hpp, part of LatAnalyze 3
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013 - 2014 Antonin Portelli
|
||||||
|
*
|
||||||
|
* 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_
|
||||||
|
|
||||||
|
#include <latan/Global.hpp>
|
||||||
|
#include <latan/Mat.hpp>
|
||||||
|
|
||||||
|
BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Array class with statistics *
|
||||||
|
******************************************************************************/
|
||||||
|
template <typename T>
|
||||||
|
class StatArray: public Eigen::Array<T, Eigen::Dynamic, 1>
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
typedef Eigen::Array<T, Eigen::Dynamic, 1> Base;
|
||||||
|
public:
|
||||||
|
// constructors
|
||||||
|
StatArray(void);
|
||||||
|
StatArray(const unsigned int size);
|
||||||
|
template <typename Derived>
|
||||||
|
StatArray(const Eigen::EigenBase<Derived> &s);
|
||||||
|
// destructor
|
||||||
|
virtual ~StatArray(void);
|
||||||
|
// statistics
|
||||||
|
T mean(void) const;
|
||||||
|
T variance(void) const;
|
||||||
|
private:
|
||||||
|
// index of the first element to take into account for statistics
|
||||||
|
virtual unsigned int getOffset(void) const;
|
||||||
|
// operations for reduction in statistical computations
|
||||||
|
static inline T square(const T &a);
|
||||||
|
static inline T sum(const T &a, const T &b);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline DMat StatArray<DMat>::square(const DMat &a);
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* StatArray class template implementation *
|
||||||
|
******************************************************************************/
|
||||||
|
// constructors ////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
StatArray<T>::StatArray(void)
|
||||||
|
: Base(static_cast<typename Base::Index>(1))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
StatArray<T>::StatArray(const unsigned int size)
|
||||||
|
: Base(static_cast<typename Base::Index>(size))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
template <typename Derived>
|
||||||
|
StatArray<T>::StatArray(const Eigen::EigenBase<Derived> &s)
|
||||||
|
: Base(s)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// destructor //////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
StatArray<T>::~StatArray(void)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// statistics //////////////////////////////////////////////////////////////////
|
||||||
|
template <typename T>
|
||||||
|
T StatArray<T>::mean(void) const
|
||||||
|
{
|
||||||
|
T result;
|
||||||
|
unsigned int size = this->size() - getOffset();
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
result = this->tail(size).redux(&StatArray<T>::sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result/static_cast<double>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T StatArray<T>::variance(void) const
|
||||||
|
{
|
||||||
|
T s, sqs, result;
|
||||||
|
unsigned int size = this->size() - getOffset();
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
s = this->tail(size).redux(&StatArray<T>::sum);
|
||||||
|
sqs = this->tail(size).unaryExpr(&StatArray<T>::square)
|
||||||
|
.redux(&StatArray<T>::sum);
|
||||||
|
result = sqs - square(s)/static_cast<double>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result/static_cast<double>(size - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline T StatArray<T>::sum(const T &a, const T &b)
|
||||||
|
{
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline T StatArray<T>::square(const T &a)
|
||||||
|
{
|
||||||
|
return a*a;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline DMat StatArray<DMat>::square(const DMat &a)
|
||||||
|
{
|
||||||
|
return a.cwiseProduct(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
unsigned int StatArray<T>::getOffset(void) const
|
||||||
|
{
|
||||||
|
return 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // Latan_StatArray_hpp_
|
Loading…
Reference in New Issue
Block a user