30 #include <type_traits>
51 template<
typename dtype>
57 const auto numVars = x.
numRows();
58 const auto numObs = x.
numCols();
59 const auto normilizationFactor = bias ?
static_cast<double>(numObs) :
static_cast<double>(numObs - 1);
60 using IndexType =
typename std::remove_const<decltype(numVars)>::type;
64 for (IndexType i = 0; i < numVars; ++i)
66 const auto var1Mean = varMeans[i];
68 for (IndexType
j = i;
j < numVars; ++
j)
70 const auto var2Mean = varMeans[
j];
73 for (IndexType iObs = 0; iObs < numObs; ++iObs)
75 sum += (x(i, iObs) - var1Mean) * (x(
j, iObs) - var2Mean);
78 covariance(i,
j) =
sum / normilizationFactor;
83 for (IndexType
j = 0;
j < numVars; ++
j)
85 for (IndexType i =
j + 1; i < numVars; ++i)
87 covariance(i,
j) = covariance(
j, i);
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3388
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3400
constexpr auto j
Definition: Constants.hpp:41
Definition: Coordinate.hpp:45
NdArray< double > cov(const NdArray< dtype > &x, bool bias=false)
Definition: cov.hpp:52
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:52