hops
Covariance.hpp
Go to the documentation of this file.
1 #ifndef HOPS_COVARIANCE_HPP
2 #define HOPS_COVARIANCE_HPP
3 
4 #include <vector>
5 
6 namespace hops {
7  template<typename StateType, typename MatrixType>
8  MatrixType computeCovariance(const std::vector<const std::vector<StateType>*>& draws,
9  const MatrixType& covarianceSeen,
10  StateType& meanSeen,
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;
17 
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;
24  }
25  }
26  }
27  meanUnseen.array() /= numberOfDraws * numberOfChains;
28 
29  StateType mean = meanUnseen;
30  if (meanSeen.size()) {
31  mean += meanSeen;
32  }
33 
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();
39  }
40  }
41  covarianceUnseen.array() /= numberOfUnseenDraws * numberOfChains;
42 
43  MatrixType covariance = (1 - eta) * covarianceUnseen;
44  if (eta) {
45  covariance += eta * covarianceSeen;
46  }
47 
48  meanSeen = mean;
49  return covariance;
50  }
51 
52  template<typename StateType, typename MatrixType>
53  MatrixType computeCovariance(const std::vector<StateType>* draws,
54  const MatrixType& covarianceSeen,
55  StateType& meanSeen,
56  unsigned long numberOfSeenDraws) {
57  return computeCovariance<StateType, MatrixType>(std::vector<const std::vector<StateType>*>{draws},
58  covarianceSeen, meanSeen, numberOfSeenDraws);
59  }
60 
61  template<typename StateType, typename MatrixType>
62  MatrixType computeCovariance(const std::vector<std::vector<StateType>>& draws,
63  const MatrixType& covarianceSeen,
64  StateType& meanSeen,
65  unsigned long numberOfSeenDraws) {
66  std::vector<const std::vector<StateType>*> drawPtrs;
67  for (auto& e : draws) {
68  drawPtrs.push_back(&e);
69  }
70  return computeCovariance<StateType, MatrixType>(drawPtrs, covarianceSeen, meanSeen, numberOfSeenDraws);
71  }
72 
73  template<typename StateType, typename MatrixType>
74  MatrixType computeCovariance(const std::vector<StateType>& draws,
75  const MatrixType& covarianceSeen,
76  StateType& meanSeen,
77  unsigned long numberOfSeenDraws) {
78  return computeCovariance<StateType, MatrixType>(std::vector<const std::vector<StateType>*>{&draws},
79  covarianceSeen, meanSeen, numberOfSeenDraws);
80  }
81 
82  template<typename StateType, typename MatrixType>
83  MatrixType computeCovariance(const std::vector<const std::vector<StateType>*>& draws) {
84  MatrixType covariance = MatrixType::Zero(0, 0);
85  StateType mean = StateType::Zero(0);
86  return computeCovariance<StateType, MatrixType>(draws, covariance, mean, 0);
87  }
88 
89  template<typename StateType, typename MatrixType>
90  MatrixType computeCovariance(const std::vector<StateType>* draws) {
91  return computeCovariance<StateType, MatrixType>(std::vector<const std::vector<StateType>*>{draws});
92  }
93 
94  template<typename StateType, typename MatrixType>
95  MatrixType computeCovariance(const std::vector<std::vector<StateType>>& draws) {
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);
101  }
102  return computeCovariance<StateType, MatrixType>(drawPtrs, mean, covariance, 0);
103  }
104 
105  template<typename StateType, typename MatrixType>
106  MatrixType computeCovariance(const std::vector<StateType>& draws) {
107  return computeCovariance<StateType, MatrixType>(std::vector<const std::vector<StateType>*>{&draws});
108  }
109 }
110 
111 #endif // HOPS_COVARIANCE_HPP
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
hops::computeCovariance
MatrixType computeCovariance(const std::vector< const std::vector< StateType > * > &draws, const MatrixType &covarianceSeen, StateType &meanSeen, unsigned long numberOfSeenDraws)
Definition: Covariance.hpp:8
hops
Definition: CsvReader.hpp:8