versatile-mcmc  0.1.7
chain.hpp
Go to the documentation of this file.
1 
12 #ifndef SRC_VMCMC_CHAIN_HPP_
13 #define SRC_VMCMC_CHAIN_HPP_
14 
15 #include <vmcmc/blas.hpp>
16 #include <vmcmc/sample.hpp>
17 
18 #include <vector>
19 #include <deque>
20 #include <unordered_map>
21 
22 #include <boost/optional.hpp>
23 
24 namespace vmcmc
25 {
26 
27 using Chain = std::deque<Sample>;
28 
37 {
38 public:
39  ChainStatistics(const Chain& sampleChain);
40 
41  const Chain& GetChain() const { return fSampleChain; }
42 
43  void Reset();
44 
45  void SelectRange(ptrdiff_t startIndex = 0, ptrdiff_t endIndex = -1);
46  void SelectPercentageRange(double start = 0.0, double end = 1.0);
47 
48  std::pair<size_t, size_t> GetIndices() const;
49  std::pair<Chain::const_iterator, Chain::const_iterator> GetIterators() const;
50 
51  size_t NumberOfParams() const;
52 
53  const Sample& GetMode();
54  Sample& GetMean();
55  double GetMedian(size_t paramIndex);
56 
57  const Vector& GetVariance();
58  const Vector& GetError();
59  const Vector& GetRms();
60 
61  const MatrixLower& GetCovarianceMatrix();
62  const MatrixUnitLower& GetCorrelationMatrix();
63  const MatrixLower& GetCholeskyDecomposition();
64 
65  const Vector& GetAutoCorrelation(size_t lag);
66  const Vector& GetAutoCorrelationTime();
67 
68  std::pair<double, double> GetConfidenceInterval(size_t iParam, double centralValue, double level);
69 
70  double GetAccRate();
71 
72 private:
73  const Chain& fSampleChain;
74  std::pair<ptrdiff_t, ptrdiff_t> fSelectedRange;
75 
76  boost::optional<Sample> fMode;
77  boost::optional<Sample> fMean;
78  std::unordered_map<size_t, double> fMedian;
79 
80  boost::optional<Vector> fVariance;
81  boost::optional<Vector> fError;
82  boost::optional<Vector> fRms;
83 
84  boost::optional<MatrixLower> fCovariance;
85  boost::optional<MatrixUnitLower> fCorrelation;
86  boost::optional<MatrixLower> fCholesky;
87 
88  std::unordered_map<size_t, Vector> fAutoCorrelation;
89  boost::optional<Vector> fAutoCorrelationTime;
90 
91  boost::optional<double> fAccRate;
92 };
93 
99 {
100 public:
101  const std::vector<ChainStatistics>& GetListOfChainStats() const { return fSingleChainStats; }
102 
103  ChainStatistics& AddChain(const Chain& sampleChain);
104  ChainStatistics& GetChainStats(size_t index) { return fSingleChainStats[index]; }
105  size_t Size() const { return fSingleChainStats.size(); }
106  void ClearChains();
107 
108  void Reset();
109 
110  void SelectRange(ptrdiff_t startIndex = 0, ptrdiff_t endIndex = -1);
111  void SelectPercentageRange(double start = 0.0, double end = 1.0);
112 
113  double GetRubinGelman();
114 
115 private:
116  std::vector<ChainStatistics> fSingleChainStats;
117 
118  boost::optional<double> fRubinGelman;
119 };
120 
121 inline size_t ChainStatistics::NumberOfParams() const
122 {
123  return (fSampleChain.empty()) ? 0 : fSampleChain.front().Values().size();
124 }
125 
126 } /* namespace vmcmc */
127 
128 #endif /* SRC_VMCMC_CHAIN_HPP_ */
Definition: algorithm.cpp:28
Manages a list of ChainStatistics and calculates statistical properties regarding sets of individual ...
Definition: chain.hpp:98
Calculates statistical momenta and properties for a given Chain of Samples.
Definition: chain.hpp:36
Contains the Sample class definition for data sampled from the target parameter space.
BLAS (Basic Linear Algebra Subprograms) utility functions.
A &#39;sample&#39; represents a node or data point in a Markov Chain.
Definition: sample.hpp:31