12#include <framework/logging/Logger.h>
36 template<
size_t Ndims>
37 const Eigen::Matrix<double, Ndims, Ndims, Eigen::RowMajor>
calculateCovMatrix(std::array<std::vector<double>, Ndims> inputData)
40 using RVector = Eigen::Matrix<double, 1, Ndims, Eigen::RowMajor>;
41 using DMatrix = Eigen::Matrix<double, Eigen::Dynamic, Ndims, Eigen::ColMajor>;
42 using DVector = Eigen::Matrix<double, Eigen::Dynamic, 1>;
45 size_t nSamples = inputData[0].size();
46 for (
size_t i = 1; i < Ndims; ++i) {
47 if (inputData[i].size() != nSamples) {
48 B2ERROR(
"The input data is no MxN matrix, cannot calculate covariance matrix! Returning identity");
49 return Eigen::Matrix<double, Ndims, Ndims, Eigen::RowMajor>::Identity();
55 for (
size_t i = 0; i < Ndims; ++i) {
56 meanVector(i) = std::accumulate(inputData[i].begin(), inputData[i].end(), 0.0) / inputData[i].size();
60 DMatrix dataMatrix(nSamples, Ndims);
61 for (
size_t i = 0; i < Ndims; ++i) {
62 dataMatrix.col(i) = Eigen::Map<const DVector>(inputData[i].data(), inputData[i].size());
66 DMatrix meanMatrix(nSamples, Ndims);
67 for (
size_t i = 0; i < Ndims; ++i) {
68 meanMatrix.col(i) = DVector::Ones(nSamples) * meanVector(i);
73 return (dataMatrix - meanMatrix).transpose() * (dataMatrix - meanMatrix) / (nSamples);
const Eigen::Matrix< double, Ndims, Ndims, Eigen::RowMajor > calculateCovMatrix(std::array< std::vector< double >, Ndims > inputData)
calculates the empirical covariance matrix from the inputData.
Abstract base class for different kinds of events.