1 #ifndef HOPS_COVARIANCE_HPP
2 #define HOPS_COVARIANCE_HPP
7 template<
typename StateType,
typename MatrixType>
11 unsigned long numberOfSeenDraws) {
12 unsigned long numberOfChains = draws.size();
13 unsigned long numberOfDraws = draws[0]->size();
14 unsigned long dimension = draws[0]->at(0).size();
15 unsigned long numberOfUnseenDraws = numberOfDraws - numberOfSeenDraws;
16 double eta = 1.0 * numberOfSeenDraws / numberOfDraws;
18 StateType meanUnseen = StateType::Zero(dimension);
19 for (
unsigned long i = 0; i < numberOfChains; ++i) {
20 for (
unsigned long j = numberOfSeenDraws; j < numberOfDraws; ++j) {
21 meanUnseen += draws[i]->at(j).transpose();
22 if (meanSeen.size()) {
23 meanUnseen -= meanSeen;
27 meanUnseen.array() /= numberOfDraws * numberOfChains;
29 StateType mean = meanUnseen;
30 if (meanSeen.size()) {
34 MatrixType covarianceUnseen = MatrixType::Zero(dimension, dimension);
35 for (
unsigned long i = 0; i < numberOfChains; ++i) {
36 for (
unsigned long j = numberOfSeenDraws; j < numberOfDraws; ++j) {
37 StateType centered = draws[i]->at(j) - mean;
38 covarianceUnseen += centered * centered.transpose();
41 covarianceUnseen.array() /= numberOfUnseenDraws * numberOfChains;
43 MatrixType covariance = (1 - eta) * covarianceUnseen;
45 covariance += eta * covarianceSeen;
52 template<
typename StateType,
typename MatrixType>
56 unsigned long numberOfSeenDraws) {
57 return computeCovariance<StateType, MatrixType>(std::vector<
const std::vector<StateType>*>{draws},
58 covarianceSeen, meanSeen, numberOfSeenDraws);
61 template<
typename StateType,
typename MatrixType>
65 unsigned long numberOfSeenDraws) {
66 std::vector<const std::vector<StateType>*> drawPtrs;
67 for (
auto& e : draws) {
68 drawPtrs.push_back(&e);
70 return computeCovariance<StateType, MatrixType>(drawPtrs, covarianceSeen, meanSeen, numberOfSeenDraws);
73 template<
typename StateType,
typename MatrixType>
77 unsigned long numberOfSeenDraws) {
78 return computeCovariance<StateType, MatrixType>(std::vector<
const std::vector<StateType>*>{&draws},
79 covarianceSeen, meanSeen, numberOfSeenDraws);
82 template<
typename StateType,
typename MatrixType>
84 MatrixType covariance = MatrixType::Zero(0, 0);
85 StateType mean = StateType::Zero(0);
86 return computeCovariance<StateType, MatrixType>(draws, covariance, mean, 0);
89 template<
typename StateType,
typename MatrixType>
91 return computeCovariance<StateType, MatrixType>(std::vector<
const std::vector<StateType>*>{draws});
94 template<
typename StateType,
typename MatrixType>
96 MatrixType covariance = MatrixType::Zero(0, 0);
97 StateType mean = StateType::Zero(0);
98 std::vector<const std::vector<StateType>*> drawPtrs;
99 for (
auto& e : draws) {
100 drawPtrs.push_back(&e);
102 return computeCovariance<StateType, MatrixType>(drawPtrs, mean, covariance, 0);
105 template<
typename StateType,
typename MatrixType>
107 return computeCovariance<StateType, MatrixType>(std::vector<
const std::vector<StateType>*>{&draws});
111 #endif // HOPS_COVARIANCE_HPP