hops
Data.hpp
Go to the documentation of this file.
1 #ifndef HOPS_DATA_HPP
2 #define HOPS_DATA_HPP
3 
12 
13 #include <Eigen/Core>
14 
15 #include <vector>
16 #include <memory>
17 
18 namespace hops {
19  class Data {
20  public:
21  Data(long dimension = 0) : dimension(dimension) {
22  //
23  }
24 
25  Data(const std::vector<std::shared_ptr<MarkovChain>>& m_markovChains, long dimension = 0) : dimension(dimension) {
26  linkWithChains(m_markovChains);
27  }
28 
29  void setDimension(long dimension) {
30  this->dimension = dimension;
31  }
32 
33  void linkWithChains(const std::vector<std::shared_ptr<MarkovChain>>& m_markovChains) {
34  chains.resize(m_markovChains.size());
35  for (size_t i = 0; i < m_markovChains.size(); ++i) {
36  m_markovChains[i]->installDataObject(chains[i]);
37  }
38  }
39 
40 
41  std::vector<const std::vector<double>*> getAcceptanceRates() {
42  std::vector<const std::vector<double>*> acceptanceRates(chains.size());
43  for (size_t i = 0; i < acceptanceRates.size(); ++i) {
44  acceptanceRates[i] = chains[i].acceptanceRates.get();
45  }
46  return acceptanceRates;
47  }
48 
49  std::vector<const std::vector<double>*> getNegativeLogLikelihood() {
50  std::vector<const std::vector<double>*> negativeLogLikelihood(chains.size());
51  for (size_t i = 0; i < negativeLogLikelihood.size(); ++i) {
52  negativeLogLikelihood[i] = chains[i].negativeLogLikelihood.get();
53  }
54  return negativeLogLikelihood;
55  }
56 
57  std::vector<const std::vector<Eigen::VectorXd>*> getStates() {
58  std::vector<const std::vector<Eigen::VectorXd>*> states(chains.size());
59  for (size_t i = 0; i < states.size(); ++i) {
60  states[i] = chains[i].states.get();
61  }
62  return states;
63  }
64 
65  std::vector<const std::vector<long>*> getTimestamps() {
66  std::vector<const std::vector<long>*> timestamps(chains.size());
67  for (size_t i = 0; i < timestamps.size(); ++i) {
68  timestamps[i] = chains[i].timestamps.get();
69  }
70  return timestamps;
71  }
72 
73 
75  totalNumberOfSamples = 0;
76  for (size_t i = 0; i < chains.size(); ++i) {
77  totalNumberOfSamples += chains[i].getStates().size();
78  }
79  }
80 
82  acceptanceRate = Eigen::VectorXd(chains.size());
83  for (size_t i = 0; i < chains.size(); ++i) {
84  acceptanceRate(i) = chains[i].getAcceptanceRates().back();
85  }
86  }
87 
89  std::vector<const std::vector<Eigen::VectorXd>*> states(chains.size());
90  if (!chains.size()) {
92  }
93 
94  for (size_t i = 0; i < states.size(); ++i) {
95  states[i] = chains[i].states.get();
96  if (!states[i]) {
98  }
99  }
100  std::vector<double> effectiveSampleSize = ::hops::computeEffectiveSampleSize(states);
101  this->effectiveSampleSize = Eigen::Map<Eigen::VectorXd>(effectiveSampleSize.data(), dimension);
102  }
103 
105  std::vector<const std::vector<Eigen::VectorXd>*> states(chains.size());
106  if (!chains.size()) {
107  throw EmptyChainDataException();
108  }
109 
110  for (size_t i = 0; i < states.size(); ++i) {
111  states[i] = chains[i].states.get();
112  if (!states[i]) {
113  throw EmptyChainDataException();
114  }
115  }
116  std::vector<double> expectedSquaredJumpDistance = ::hops::computeExpectedSquaredJumpDistance<Eigen::VectorXd, Eigen::MatrixXd>(states);
117  this->expectedSquaredJumpDistance = Eigen::Map<Eigen::VectorXd>(expectedSquaredJumpDistance.data(), chains.size());
118  }
119 
121  std::vector<const std::vector<Eigen::VectorXd>*> states(chains.size());
122  if (!chains.size()) {
123  throw EmptyChainDataException();
124  }
125 
126  for (size_t i = 0; i < states.size(); ++i) {
127  states[i] = chains[i].states.get();
128  if (!states[i]) {
129  throw EmptyChainDataException();
130  }
131  }
132  std::vector<double> potentialScaleReductionFactor = ::hops::computePotentialScaleReductionFactor(states);
133  this->potentialScaleReductionFactor = Eigen::Map<Eigen::VectorXd>(potentialScaleReductionFactor.data(), dimension);
134  }
135 
137  totalTimeTaken = Eigen::VectorXd(chains.size());
138  for (size_t i = 0; i < chains.size(); ++i) {
139  totalTimeTaken(i) = chains[i].getTimestamps().back() - chains[i].getTimestamps().front();
140  }
141  }
142 
143  void reset() {
144  for (size_t i = 0; i < chains.size(); ++i) {
145  chains[i].reset();
146  }
147  }
148 
149  void write(const std::string& outputDirectory, bool discardRawData = false) const {
150  if (!discardRawData) {
151  for (size_t i = 0; i < chains.size(); ++i) {
152  auto fileWriter = FileWriterFactory::createFileWriter(outputDirectory + "/chain" + std::to_string(i), FileWriterType::CSV);
153  chains[i].write(fileWriter.get());
154  }
155  }
156 
157  auto statisticsWriter = FileWriterFactory::createFileWriter(outputDirectory + "/statistics", FileWriterType::CSV);
158 
159  if (acceptanceRate.size() > 0) {
160  statisticsWriter->write("acceptanceRate", Eigen::MatrixXd(acceptanceRate.transpose()));
161  }
162 
163  if (expectedSquaredJumpDistance.size() > 0) {
164  statisticsWriter->write("expectedSquaredJumpDistance", Eigen::MatrixXd(expectedSquaredJumpDistance.transpose()));
165  }
166 
167  if (effectiveSampleSize.size() > 0) {
168  statisticsWriter->write("effectiveSampleSize", Eigen::MatrixXd(effectiveSampleSize.transpose()));
169  }
170 
171  if (potentialScaleReductionFactor.size() > 0) {
172  statisticsWriter->write("potentialScaleReductionFactor", Eigen::MatrixXd(potentialScaleReductionFactor.transpose()));
173  }
174 
175  if (totalNumberOfSamples > 0) {
176  statisticsWriter->write("totalNumberOfSamples", Eigen::MatrixXd(totalNumberOfSamples * Eigen::MatrixXd::Identity(1,1)));
177  }
178 
179  if (totalTimeTaken.size() > 0) {
180  statisticsWriter->write("totalTimeTaken", Eigen::MatrixXd(totalTimeTaken.transpose()));
181  }
182 
183  if (totalNumberOfTuningSamples > 0) {
184  auto tuningWriter = FileWriterFactory::createFileWriter(outputDirectory + "/tuning", FileWriterType::CSV);
185  tuningWriter->write("totalNumberOfTuningSamples", std::vector<long>{static_cast<long>(totalNumberOfTuningSamples)});
186  tuningWriter->write("stepSize", std::vector<double>{tunedStepSize});
187  tuningWriter->write("objectiveValue", std::vector<double>{tunedObjectiveValue});
188  tuningWriter->write("totalTimeTaken", std::vector<double>{totalTuningTimeTaken});
189 
190  if (tuningData.size() > 0) {
191  tuningWriter->write("data", tuningData);
192  }
193 
194  if (tuningPosterior.size() > 0) {
195  tuningWriter->write("posterior", tuningPosterior);
196  }
197  }
198  }
199 
200  void setTuningMethod(const std::string& tuningMethod) {
201  this->tuningMethod = tuningMethod;
202  }
203 
204  void setTotalNumberOfTuningSamples(unsigned long totalNumberOfTuningSamples) {
205  this->totalNumberOfTuningSamples = totalNumberOfTuningSamples;
206  }
207 
208  void setTunedStepSize(double tunedStepSize) {
209  this->tunedStepSize = tunedStepSize;
210  }
211 
212  void setTunedObjectiveValue(double tunedObjectiveValue) {
213  this->tunedObjectiveValue = tunedObjectiveValue;
214  }
215 
216  void setTotalTuningTimeTaken(double totalTuningTimeTaken) {
217  this->totalTuningTimeTaken = totalTuningTimeTaken;
218  }
219 
220  void setTuningData(const Eigen::MatrixXd& tuningData) {
221  this->tuningData = tuningData;
222  }
223 
224  void setTuningPosterior(const Eigen::MatrixXd& tuningPosterior) {
225  this->tuningPosterior = tuningPosterior;
226  }
227 
228  private:
229  std::vector<ChainData> chains;
230 
231  double totalNumberOfSamples;
232  Eigen::VectorXd acceptanceRate;
233  Eigen::VectorXd expectedSquaredJumpDistance;
234  Eigen::VectorXd effectiveSampleSize;
235  Eigen::VectorXd potentialScaleReductionFactor;
236  Eigen::VectorXd totalTimeTaken;
237 
238  // tuning data
239  std::string tuningMethod;
240  unsigned long totalNumberOfTuningSamples = 0;
241  double tunedStepSize;
242  double tunedObjectiveValue;
243  double totalTuningTimeTaken;
244 
245  Eigen::MatrixXd tuningData;
246  Eigen::MatrixXd tuningPosterior;
247 
248  std::vector<std::vector<double>> sampleVariances;
249  std::vector<std::vector<double>> intraChainExpectations;
250  std::vector<double> interChainExpectation;
251  unsigned long numSeen = 0;
252 
253  long dimension = 0;
254  friend Eigen::VectorXd computeAcceptanceRate(Data& data);
255  friend Eigen::VectorXd computeExpectedSquaredJumpDistance(Data& data);
256  friend Eigen::VectorXd computeEffectiveSampleSize(Data& data);
257  friend Eigen::VectorXd computePotentialScaleReductionFactor(Data& data);
258  friend double computeTotalNumberOfSamples(Data& data);
259  friend Eigen::VectorXd computeTotalTimeTaken(Data& data);
260  };
261 
262  inline Eigen::VectorXd computeAcceptanceRate(Data& data) {
263  data.computeAcceptanceRate();
264  return data.acceptanceRate;
265  }
266 
267  inline Eigen::VectorXd computeExpectedSquaredJumpDistance(Data& data) {
269  return data.expectedSquaredJumpDistance;
270  }
271 
272  inline Eigen::VectorXd computeEffectiveSampleSize(Data& data) {
274  return data.effectiveSampleSize;
275  }
276 
277  inline Eigen::VectorXd computePotentialScaleReductionFactor(Data& data) {
279  return data.potentialScaleReductionFactor;
280  }
281 
282  inline double computeTotalNumberOfSamples(Data& data) {
284  return data.totalNumberOfSamples;
285  }
286 
287  inline Eigen::VectorXd computeTotalTimeTaken(Data& data) {
288  data.computeTotalTimeTaken();
289  return data.totalTimeTaken;
290  }
291 }
292 
293 #endif // HOPS_DATA_HPP
294 
hops::Data::getStates
std::vector< const std::vector< Eigen::VectorXd > * > getStates()
Definition: Data.hpp:57
hops::Data::linkWithChains
void linkWithChains(const std::vector< std::shared_ptr< MarkovChain >> &m_markovChains)
Definition: Data.hpp:33
hops::Data
Definition: Data.hpp:19
hops::Data::computeTotalNumberOfSamples
void computeTotalNumberOfSamples()
Definition: Data.hpp:74
hops::Data::Data
Data(const std::vector< std::shared_ptr< MarkovChain >> &m_markovChains, long dimension=0)
Definition: Data.hpp:25
hops::EmptyChainDataException
Definition: Exceptions.hpp:21
hops::computeExpectedSquaredJumpDistance
double computeExpectedSquaredJumpDistance(const std::vector< StateType > &draws, unsigned long numUnseen, double esjdSeen, unsigned long numSeen, const MatrixType &sqrtCovariance)
Definition: ExpectedSquaredJumpDistance.hpp:24
hops::Data::computeEffectiveSampleSize
void computeEffectiveSampleSize()
Definition: Data.hpp:88
hops::Data::getAcceptanceRates
std::vector< const std::vector< double > * > getAcceptanceRates()
Definition: Data.hpp:41
hops::computeEffectiveSampleSize
Eigen::VectorXd computeEffectiveSampleSize(Data &data)
Definition: Data.hpp:272
hops::computePotentialScaleReductionFactor
Eigen::VectorXd computePotentialScaleReductionFactor(Data &data)
Definition: Data.hpp:277
hops::Data::setTunedStepSize
void setTunedStepSize(double tunedStepSize)
Definition: Data.hpp:208
hops::Data::computeExpectedSquaredJumpDistance
void computeExpectedSquaredJumpDistance()
Definition: Data.hpp:104
hops::FileWriterType::CSV
@ CSV
hops::FileWriterFactory::createFileWriter
static std::unique_ptr< FileWriter > createFileWriter(const std::string &filename, FileWriterType fileWriterType)
Definition: FileWriterFactory.cpp:6
FileWriterType.hpp
hops::computeTotalTimeTaken
Eigen::VectorXd computeTotalTimeTaken(Data &data)
Definition: Data.hpp:287
hops::Data::write
void write(const std::string &outputDirectory, bool discardRawData=false) const
Definition: Data.hpp:149
hops::computeTotalNumberOfSamples
double computeTotalNumberOfSamples(Data &data)
Definition: Data.hpp:282
FileWriterFactory.hpp
hops::Data::setTuningData
void setTuningData(const Eigen::MatrixXd &tuningData)
Definition: Data.hpp:220
hops::Data::setTotalNumberOfTuningSamples
void setTotalNumberOfTuningSamples(unsigned long totalNumberOfTuningSamples)
Definition: Data.hpp:204
hops::Data::computeAcceptanceRate
void computeAcceptanceRate()
Definition: Data.hpp:81
hops::Data::setTuningMethod
void setTuningMethod(const std::string &tuningMethod)
Definition: Data.hpp:200
ExpectedSquaredJumpDistance.hpp
PotentialScaleReductionFactor.hpp
hops::Data::setTotalTuningTimeTaken
void setTotalTuningTimeTaken(double totalTuningTimeTaken)
Definition: Data.hpp:216
hops::Data::setDimension
void setDimension(long dimension)
Definition: Data.hpp:29
hops::Data::getTimestamps
std::vector< const std::vector< long > * > getTimestamps()
Definition: Data.hpp:65
hops::Data::Data
Data(long dimension=0)
Definition: Data.hpp:21
hops::Data::setTuningPosterior
void setTuningPosterior(const Eigen::MatrixXd &tuningPosterior)
Definition: Data.hpp:224
hops
Definition: CsvReader.hpp:8
ChainData.hpp
hops::Data::setTunedObjectiveValue
void setTunedObjectiveValue(double tunedObjectiveValue)
Definition: Data.hpp:212
MarkovChain.hpp
hops::Data::getNegativeLogLikelihood
std::vector< const std::vector< double > * > getNegativeLogLikelihood()
Definition: Data.hpp:49
hops::Data::computeTotalTimeTaken
void computeTotalTimeTaken()
Definition: Data.hpp:136
hops::computeEffectiveSampleSize
double computeEffectiveSampleSize(const std::vector< const std::vector< StateType > * > &chains, unsigned long dimension)
Definition: EffectiveSampleSize.hpp:12
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
hops::computePotentialScaleReductionFactor
double computePotentialScaleReductionFactor(const std::vector< const std::vector< StateType > * > &chains, unsigned long dimension, unsigned long numUnseen, std::vector< double > &sampleVariancesSeen, std::vector< double > &intraChainExpectationsSeen, double &interChainExpectationSeen, unsigned long &numSeen)
Definition: PotentialScaleReductionFactor.hpp:20
hops::computeAcceptanceRate
Eigen::VectorXd computeAcceptanceRate(Data &data)
Definition: Data.hpp:262
hops::Data::computePotentialScaleReductionFactor
void computePotentialScaleReductionFactor()
Definition: Data.hpp:120
FileWriter.hpp
hops::Data::reset
void reset()
Definition: Data.hpp:143
EffectiveSampleSize.hpp