hops
ExpectedSquaredJumpDistance.hpp
Go to the documentation of this file.
1 #ifndef HOPS_EXPECTEDSQUAREDJUMPDISTANCE_HPP
2 #define HOPS_EXPECTEDSQUAREDJUMPDISTANCE_HPP
3 
6 
7 #include <Eigen/Core>
8 #include <Eigen/Cholesky>
9 
10 #include <string>
11 #include <stdexcept>
12 #include <vector>
13 #include <cmath>
14 #include <cassert>
15 #include <memory>
16 
17 namespace hops {
18  /*
19  * Compute Expected Squared Jump Distance incrementally on a single vector of draws.
20  * The Expected Squared Jump Distance is defined as
21  * \[ ESJD = \frac{1}{N-1} \sum_{n=1}^(N-1) \| \theta_{n+1} - \theta_n \|^2_{\Sigma} \]
22  */
23  template<typename StateType, typename MatrixType>
24  double computeExpectedSquaredJumpDistance(const std::vector<StateType>& draws,
25  unsigned long numUnseen,
26  double esjdSeen,
27  unsigned long numSeen,
28  const MatrixType& sqrtCovariance) {
29  size_t numDraws = draws.size(),
30  correction = 0;
31  // account for missing jump between two batches of samples
32  if (numSeen > 0 && draws.size() > numUnseen) {
33  correction = 1;
34  }
35 
36  // in order to guarantee eta to be 1, we have to set it to 1.
37  if (numSeen == 0) {
38  ++numSeen;
39  }
40 
41  double esjd = 0,
42  eta = 1.0 * (numSeen - 1) / (numSeen + numUnseen - 1),
43  squaredDistance;
44 
45  if (!isConstantChain<StateType>({&draws})) {
46  for (unsigned long i = numDraws - numUnseen - correction; i < numDraws - 1; ++i) {
47  StateType distance = sqrtCovariance.template triangularView<Eigen::Lower>().solve(draws[i] - draws[i+1]);
48  distance = sqrtCovariance.template triangularView<Eigen::Lower>().transpose().solve(distance);
49  squaredDistance = (draws[i] - draws[i+1]).transpose() * distance;
50  esjd += squaredDistance;
51  }
52  esjd /= numUnseen - 1 + correction;
53  }
54 
55  return eta * esjdSeen + (1 - eta) * esjd;
56  }
57 
58  /*
59  * Compute Expected Squared Jump Distance non-incrementally on all draws passed.
60  */
61  template<typename StateType, typename MatrixType>
62  double computeExpectedSquaredJumpDistance(const std::vector<StateType>& draws, const MatrixType& sqrtCovariance) {
63  return computeExpectedSquaredJumpDistance<StateType, MatrixType>(draws, draws.size(), 0, 0, sqrtCovariance);
64  }
65 
66  /*
67  * Compute Expected Squared Jump Distance non-incrementally on all draws passed.
68  */
69  template<typename StateType, typename MatrixType>
70  double computeExpectedSquaredJumpDistance(const std::vector<StateType>& draws) {
71  MatrixType covariance = computeCovariance<StateType, MatrixType>(draws);
72  MatrixType sqrtCovariance = covariance.llt().matrixL();
73  return computeExpectedSquaredJumpDistance<StateType, MatrixType>(draws, sqrtCovariance);
74  }
75 
76  /*
77  * Compute Expected Squared Jump Distance for every chain in \c chains incrementally.
78  */
79  template<typename StateType, typename MatrixType>
80  std::vector<double> computeExpectedSquaredJumpDistance(const std::vector<std::vector<StateType>>& chains,
81  unsigned long numUnseen,
82  std::vector<double> esjdSeen,
83  unsigned long numSeen,
84  const MatrixType& sqrtCovariance) {
85  std::vector<double> esjds(chains.size());
86  for (size_t i = 0; i < chains.size(); ++i) {
87  esjds[i] = computeExpectedSquaredJumpDistance<StateType, MatrixType>(chains[i], numUnseen, esjdSeen[i], numSeen, sqrtCovariance);
88  }
89  return esjds;
90  }
91 
92  /*
93  * Compute Expected Squared Jump Distance non-incrementally for every chain in \c chains.
94  */
95  template<typename StateType, typename MatrixType>
96  std::vector<double> computeExpectedSquaredJumpDistance(const std::vector<std::vector<StateType>>& chains, const MatrixType& sqrtCovariance) {
97  return computeExpectedSquaredJumpDistance<StateType, MatrixType>(chains, chains[0].size(), std::vector<double>(chains.size()), 0, sqrtCovariance);
98  }
99 
100  /*
101  * Compute Expected Squared Jump Distance non-incrementally for every chain in \c chains.
102  */
103  template<typename StateType, typename MatrixType>
104  std::vector<double> computeExpectedSquaredJumpDistance(const std::vector<std::vector<StateType>>& chains) {
105  MatrixType covariance = computeCovariance<StateType, MatrixType>(chains);
106  MatrixType sqrtCovariance = covariance.llt().matrixL();
107  return computeExpectedSquaredJumpDistance<StateType, MatrixType>(chains, sqrtCovariance);
108  }
109 
110  /*
111  * Compute Expected Squared Jump Distance for every chain in \c chains incrementally.
112  * \c chains is supposed to be a vector of pointers to the actual chains.
113  */
114  template<typename StateType, typename MatrixType>
115  std::vector<double> computeExpectedSquaredJumpDistance(const std::vector<const std::vector<StateType>*>& chains,
116  unsigned long numUnseen,
117  std::vector<double> esjdSeen,
118  unsigned long numSeen,
119  const MatrixType& sqrtCovariance) {
120  std::vector<double> esjds(chains.size());
121  for (size_t i = 0; i < chains.size(); ++i) {
122  esjds[i] = computeExpectedSquaredJumpDistance<StateType, MatrixType>(*chains[i], numUnseen, esjdSeen[i], numSeen, sqrtCovariance);
123  }
124  return esjds;
125  }
126 
127  /*
128  * Compute Expected Squared Jump Distance non-incrementally for every chain in \c chains.
129  * \c chains is supposed to be a vector of pointers to the actual chains.
130  */
131  template<typename StateType, typename MatrixType>
132  std::vector<double> computeExpectedSquaredJumpDistance(const std::vector<const std::vector<StateType>*>& chains,
133  const MatrixType& sqrtCovariance) {
134  return computeExpectedSquaredJumpDistance<StateType, MatrixType>(chains, chains[0]->size(), std::vector<double>(chains.size()), 0, sqrtCovariance);
135  }
136 
137  /*
138  * Compute Expected Squared Jump Distance non-incrementally for every chain in \c chains.
139  * \c chains is supposed to be a vector of pointers to the actual chains.
140  */
141  template<typename StateType, typename MatrixType>
142  std::vector<double> computeExpectedSquaredJumpDistance(const std::vector<const std::vector<StateType>*>& chains) {
143  MatrixType covariance = computeCovariance<StateType, MatrixType>(chains);
144  MatrixType sqrtCovariance = covariance.llt().matrixL();
145  return computeExpectedSquaredJumpDistance<StateType, MatrixType>(chains, sqrtCovariance);
146  }
147 }
148 
149 
150 #endif //HOPS_EXPECTEDSQUAREDJUMPDISTANCE_HPP
151 
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
IsConstantChain.hpp
hops::computeExpectedSquaredJumpDistance
double computeExpectedSquaredJumpDistance(const std::vector< StateType > &draws, unsigned long numUnseen, double esjdSeen, unsigned long numSeen, const MatrixType &sqrtCovariance)
Definition: ExpectedSquaredJumpDistance.hpp:24
Covariance.hpp
hops
Definition: CsvReader.hpp:8