11#include <tracking/trackFindingVXD/filterTools/DecorrelationMatrixHelper.h>
14#include <Eigen/Eigenvalues>
35 template<
size_t Ndims>
39 using MatrixT = Eigen::Matrix<double, Ndims, Ndims, Eigen::RowMajor>;
65 std::vector<double>
decorrelate(
const std::vector<double>& inputVec)
const;
68 std::vector<double>
decorrelate(
const std::array<double, Ndims>& inputs)
const;
73 std::array<std::vector<double>, Ndims>
decorrelate(
const std::array<std::vector<double>, Ndims>& inputMat)
const;
97 template<
size_t Ndims>
106 template<
size_t Ndims>
110 Eigen::SelfAdjointEigenSolver<MatrixT> eigenSolver(covMatrix);
111 const MatrixT U = eigenSolver.eigenvectors();
116 const MatrixT D = eigenSolver.eigenvalues().cwiseSqrt().cwiseInverse().asDiagonal();
124 template<
size_t Ndims>
127 using RVector = Eigen::Matrix<double, 1, Ndims, Eigen::RowMajor>;
128 Eigen::Map<const RVector> input(inputVec.data(), Ndims);
130 const RVector transform = input * m_matrix;
132 return std::vector<double>(transform.data(), transform.data() + transform.size());
135 template<
size_t Ndims>
138 return decorrelate(std::vector<double>(input.begin(), input.end()));
141 template<
size_t Ndims>
142 std::array<std::vector<double>, Ndims>
145 using DVector = Eigen::Matrix<double, Eigen::Dynamic, 1, Eigen::ColMajor>;
146 using DMatrix = Eigen::Matrix<double, Eigen::Dynamic, Ndims, Eigen::ColMajor>;
148 size_t nSamples = inputMat[0].size();
149 DMatrix dataMatrix(nSamples, Ndims);
150 for (
size_t i = 0; i < Ndims; ++i) {
151 dataMatrix.col(i) = Eigen::Map<const DVector>(inputMat[i].data(), inputMat[i].size());
154 DMatrix transform = dataMatrix * m_matrix;
156 std::array<std::vector<double>, Ndims> output;
157 for (
size_t i = 0; i < Ndims; ++i) {
158 output[i] = std::vector<double>(transform.col(i).data(), transform.col(i).data() + transform.col(i).size());
164 template<
size_t Ndims>
167 std::stringstream sstr{};
168 sstr << std::setprecision(std::numeric_limits<double>::digits10 + 1) << m_matrix;
172 template<
size_t Ndims>
176 using RVector = Eigen::Matrix<double, 1, Ndims, Eigen::RowMajor>;
179 while (iRow < Ndims) {
181 if (is.eof())
return false;
182 std::getline(is, line);
183 if (line.empty())
continue;
184 std::stringstream lstr(line);
185 std::array<double, Ndims> linevalues;
186 for (
double& value : linevalues) {
187 if (lstr.eof())
return false;
190 inMat.row(iRow) = Eigen::Map<const RVector>(linevalues.data(), linevalues.size());
Class holding a Matrix that can be used to decorrelate input data to Machine Learning classifiers.
DecorrelationMatrix(const MatrixT &matrix=MatrixT::Identity())
default constructor: initializes to identity matrix or to passed matrix
MatrixT m_matrix
internal matrix holding the data
const MatrixT & getMatrix() const
get the currently stored matrix
void calculateDecorrMatrix(const MatrixT &covMatrix, bool normalise=true)
calculate the matrix that can be used to decorrelate the data that yield the passed covariance matrix...
DecorrelationMatrix(const DecorrelationMatrix &matrix)=default
copy constructor
DecorrelationMatrix & operator=(const DecorrelationMatrix &rhs)=default
assignment operator
Eigen::Matrix< double, Ndims, Ndims, Eigen::RowMajor > MatrixT
typedef for consistent type across class
std::vector< double > decorrelate(const std::vector< double > &inputVec) const
"decorrelate" one measurement (i.e.
std::vector< double > decorrelate(const std::array< double, Ndims > &inputs) const
"decorrelate" one measurement (i.e.
const Eigen::Matrix< double, Ndims, Ndims, Eigen::RowMajor > calculateCovMatrix(std::array< std::vector< double >, Ndims > inputData)
calculates the empirical covariance matrix from the inputData.
void calculateDecorrMatrix(std::array< std::vector< double >, Ndims > inputData, bool normalise=true)
calculate the transformation matrix that when applied to the input data yields linearly uncorrelated ...
bool readFromStream(std::istream &is)
read from stream.
std::string print() const
print the matrix to a string.
std::array< std::vector< double >, Ndims > decorrelate(const std::array< std::vector< double >, Ndims > &inputMat) const
decorrelate M measurements (i.e.
Abstract base class for different kinds of events.