versatile-mcmc  0.1.7
sample.hpp
Go to the documentation of this file.
1 
14 #ifndef VMCMC_SAMPLE_H_
15 #define VMCMC_SAMPLE_H_
16 
17 #include <vmcmc/blas.hpp>
18 #include <vmcmc/numeric.hpp>
19 
20 #include <initializer_list>
21 
22 namespace vmcmc
23 {
24 
31 class Sample
32 {
33 public:
34  Sample(const Vector& pValues);
35  Sample(std::initializer_list<double> pValues);
36  Sample(size_t nParams = 0);
37  virtual ~Sample() { }
38 
39  void SetGeneration(size_t value) { fGeneration = value; }
40  size_t GetGeneration() const { return fGeneration; }
41  size_t IncrementGeneration() { return ++fGeneration; }
42 
43  Vector& Values() { return fParameterValues; }
44  const Vector& Values() const { return fParameterValues; }
45 
46  double& operator[](size_t index) { return fParameterValues[index]; }
47  const double& operator[](size_t index) const { return fParameterValues[index]; }
48  size_t size() const { return fParameterValues.size(); }
49 
50  Sample& operator= (std::initializer_list<double> pValues);
51 
56  void Reset();
57 
58  void SetLikelihood(double value) { fLikelihood = value; }
59  double GetLikelihood() const { return fLikelihood; }
60 
61  void SetNegLogLikelihood(double value) { fNegLogLikelihood = value; }
62  double GetNegLogLikelihood() const { return fNegLogLikelihood; }
63 
64  void SetPrior(double value) { fPrior = value; }
65  double GetPrior() const { return fPrior; }
66 
67  void SetAccepted(bool value) { fAccepted = value; }
68  double IsAccepted() const { return fAccepted; }
69 
70  void operator+= (const Sample& other);
71  void operator-= (const Sample& other);
72  void operator*= (double factor);
73  void operator/= (double factor);
74 
75  bool operator== (const Sample& other) const { return !(*this != other); }
76  bool operator!= (const Sample& other) const;
77 
78  friend Sample operator+(const Sample& s1, const Sample& s2);
79  friend Sample operator-(const Sample& s1, const Sample& s2);
80  friend Sample operator*(const Sample& s1, double f);
81  friend Sample operator/(const Sample& s1, double f);
82 
83 private:
84  size_t fGeneration = 0;
85  Vector fParameterValues;
86  double fLikelihood = 0.0;
87  double fNegLogLikelihood = -numeric::inf();
88  double fPrior = 0.0;
89  bool fAccepted = false;
90 };
91 
92 inline Sample::Sample(const Vector& pValues) :
93  fParameterValues( pValues )
94 { }
95 
96 inline Sample::Sample(std::initializer_list<double> pValues) :
97  fParameterValues( pValues )
98 { }
99 
100 inline Sample::Sample(size_t nParams) :
101  fParameterValues( nParams, 0.0 )
102 { }
103 
104 inline Sample& Sample::operator= (std::initializer_list<double> pValues)
105 {
106  fParameterValues = Vector(pValues);
107  return *this;
108 }
109 
110 inline void Sample::Reset()
111 {
112  fPrior = fLikelihood = 0.0;
113  fNegLogLikelihood = -numeric::inf();
114  fAccepted = false;
115 }
116 
117 // unary operators
118 
119 inline void Sample::operator+= (const Sample& other)
120 {
121  fParameterValues += other.fParameterValues;
122  Reset();
123 }
124 
125 inline void Sample::operator-= (const Sample& other)
126 {
127  fParameterValues -= other.fParameterValues;
128  Reset();
129 }
130 
131 inline void Sample::operator*= (double factor)
132 {
133  fParameterValues *= factor;
134  Reset();
135 }
136 
137 inline void Sample::operator/= (double factor)
138 {
139  fParameterValues /= factor;
140  Reset();
141 }
142 
143 inline bool Sample::operator!= (const Sample& other) const
144 {
145  return fParameterValues != other.fParameterValues;
146 }
147 
148 // free binary operators
149 
150 inline Sample operator+(const Sample& s1, const Sample& s2)
151 {
152  return Sample( s1.fParameterValues + s2.fParameterValues );
153 }
154 
155 inline Sample operator-(const Sample& s1, const Sample& s2)
156 {
157  return Sample( s1.fParameterValues - s2.fParameterValues );
158 }
159 
160 inline Sample operator*(const Sample& s1, double f)
161 {
162  return Sample( s1.fParameterValues * f );
163 }
164 
165 inline Sample operator/(const Sample& s1, double f)
166 {
167  return Sample( s1.fParameterValues / f );
168 }
169 
170 } /* namespace vmcmc */
171 
172 #endif /* VMCMC_SAMPLE_H_ */
void Reset()
Reset ::fLikelihood, ::fNegLogLikelihood and ::fPrior to their default values.
Definition: sample.hpp:110
Definition: algorithm.cpp:28
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
Numeric utility functions.