52 template<
typename dtype>
58 const auto numVars = x.
numRows();
59 const auto numObs = x.
numCols();
60 const auto normilizationFactor =
61 bias ==
Bias::YES ?
static_cast<double>(numObs) :
static_cast<double>(numObs - 1);
62 using IndexType =
typename std::remove_const<
decltype(numVars)>::type;
66 for (IndexType i = 0; i < numVars; ++i)
68 const auto var1Mean = varMeans[i];
70 for (IndexType j = i; j < numVars; ++j)
72 const auto var2Mean = varMeans[j];
75 for (IndexType iObs = 0; iObs < numObs; ++iObs)
77 sum += (x(i, iObs) - var1Mean) * (x(j, iObs) - var2Mean);
80 covariance(i, j) =
sum / normilizationFactor;
85 for (IndexType j = 0; j < numVars; ++j)
87 for (IndexType i = j + 1; i < numVars; ++i)
89 covariance(i, j) = covariance(j, i);
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
size_type numCols() const noexcept
Definition NdArrayCore.hpp:3545
size_type numRows() const noexcept
Definition NdArrayCore.hpp:3557
Definition Cartesian.hpp:40
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition mean.hpp:52
@ COL
Definition Enums.hpp:39
NdArray< double > cov(const NdArray< dtype > &x, Bias bias=Bias::NO)
Definition cov.hpp:53
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:46
Bias
Bias boolean.
Definition Enums.hpp:65
@ YES
Definition Enums.hpp:66
@ NO
Definition Enums.hpp:67