versatile-mcmc  0.1.7
algorithm.hpp
Go to the documentation of this file.
1 
13 #ifndef VMCMC_ALGORITHM_H_
14 #define VMCMC_ALGORITHM_H_
15 
16 #include <vmcmc/parameter.hpp>
17 #include <vmcmc/chain.hpp>
18 #include <vmcmc/typetraits.hpp>
19 
20 #include <functional>
21 #include <vector>
22 #include <cmath>
23 
24 namespace vmcmc
25 {
26 
27 class Writer;
28 
39 class Algorithm
40 {
41 public:
42  using DefaultCallable = std::function<double (const std::vector<double>&)>;
43 
44 public:
45  Algorithm();
46  virtual ~Algorithm();
47 
56  void SetParameterConfig(const ParameterConfig& paramConfig);
57  const ParameterConfig& GetParameterConfig() const { return fParameterConfig; }
58 
59  void SetPrior(DefaultCallable prior);
60  template <size_t NParams, typename CallableT>
61  void SetPrior(CallableT prior);
62 
63  void SetLikelihood(DefaultCallable likelihood);
64  template <size_t NParams, typename CallableT>
65  void SetLikelihood(CallableT likelihood);
66 
67  void SetNegLogLikelihood(DefaultCallable negLoglikelihood);
68  template <size_t NParams, typename CallableT>
69  void SetNegLogLikelihood(CallableT negLoglikelihood);
70 
71  void SetTotalLength(size_t length) { fTotalLength = length; }
72  size_t GetTotalLength() const { return fTotalLength; }
73 
79  template <typename WriterT, typename... ArgsT>
80  void AddWriter(ArgsT&&... args);
81  void AddWriter(std::shared_ptr<Writer> writer) { fWriters.push_back( writer ); }
82 
88  double EvaluatePrior(const std::vector<double>& paramValues) const;
89 
95  double EvaluateLikelihood(const std::vector<double>& paramValues) const;
96 
102  double EvaluateNegLogLikelihood(const std::vector<double>& paramValues) const;
103 
112  bool Evaluate(Sample& sample) const;
113 
117  void Run();
118 
119  virtual void Initialize();
120 
121  virtual void Advance(size_t nSteps = 1) = 0;
122 
123  virtual void Finalize();
124 
125  virtual size_t NumberOfChains() = 0;
126  virtual const Chain& GetChain(size_t cIndex = 0) = 0;
127 
128  ChainSetStatistics& GetStatistics() { return fStatistics; }
129  const ChainSetStatistics& GetStatistics() const { return fStatistics; }
130 
131 protected:
132  ParameterConfig fParameterConfig;
133  DefaultCallable fPrior;
134 
135  DefaultCallable fLikelihood;
136  DefaultCallable fNegLogLikelihood;
137 
138  size_t fTotalLength;
139  size_t fCycleLength;
140 
141  std::vector<std::shared_ptr<Writer>> fWriters;
142 
143  ChainSetStatistics fStatistics;
144 };
145 
146 namespace detail
147 {
148 
159 template <size_t NParams, typename C, typename... Ts>
160 typename std::enable_if<NParams == sizeof...(Ts), double>::type
161 inline apply_first_n(C f, const std::vector<double> &v, Ts&&... ts)
162 {
163  return f(std::forward<Ts>(ts)...);
164 }
165 
169 template <size_t NParams, typename C, typename... Ts>
170 typename std::enable_if<NParams != sizeof...(Ts), double>::type
171 inline apply_first_n(C f, const std::vector<double> &v, Ts&&... ts)
172 {
173  constexpr ptrdiff_t index = NParams - sizeof...(Ts) - 1;
174  static_assert(index >= 0, "invalid number of function parameters");
175  return apply_first_n<NParams>(f, v, *(std::begin(v) + index), std::forward<Ts>(ts)...);
176 }
177 
178 } /* namespace detail */
179 
180 inline void Algorithm::SetPrior(DefaultCallable prior)
181 {
182  fPrior = prior;
183 }
184 
185 template <size_t NParams, typename CallableT>
186 inline void Algorithm::SetPrior(CallableT f)
187 {
188  fPrior = [=](const std::vector<double>& v) {
189  return detail::apply_first_n<NParams>(f, v);
190  };
191 }
192 
193 inline void Algorithm::SetLikelihood(DefaultCallable likelihood)
194 {
195  fLikelihood = likelihood;
196  fNegLogLikelihood = nullptr;
197 }
198 
199 template <size_t NParams, typename CallableT>
200 inline void Algorithm::SetLikelihood(CallableT f)
201 {
202  fLikelihood = [=](const std::vector<double>& v) {
203  return detail::apply_first_n<NParams>(f, v);
204  };
205  fNegLogLikelihood = nullptr;
206 }
207 
208 inline void Algorithm::SetNegLogLikelihood(DefaultCallable negLoglikelihood)
209 {
210  fLikelihood = nullptr;
211  fNegLogLikelihood = negLoglikelihood;
212 }
213 
214 template <size_t NParams, typename CallableT>
215 inline void Algorithm::SetNegLogLikelihood(CallableT f)
216 {
217  fLikelihood = nullptr;
218  fNegLogLikelihood = [=](const std::vector<double>& v) {
219  return detail::apply_first_n<NParams>(f, v);
220  };
221 }
222 
223 template <typename WriterT, typename... ArgsT>
224 inline void Algorithm::AddWriter(ArgsT&&... args)
225 {
226  fWriters.emplace_back( std::make_shared<WriterT>(std::forward<ArgsT>(args)...) );
227 }
228 
229 } /* namespace vmcmc */
230 
231 #endif /* VMCMC_ALGORITHM_H_ */
Definition: algorithm.cpp:28
A list of parameters, describing the parameter space of the target function to be evaluated...
Definition: parameter.hpp:109
double EvaluateNegLogLikelihood(const std::vector< double > &paramValues) const
Evaluate the target function -log(likelihood) for the given parameter values.
Definition: algorithm.cpp:147
Manages a list of ChainStatistics and calculates statistical properties regarding sets of individual ...
Definition: chain.hpp:98
double EvaluatePrior(const std::vector< double > &paramValues) const
Evaluate the prior for the given parameter values.
Definition: algorithm.cpp:132
void SetParameterConfig(const ParameterConfig &paramConfig)
Set the parameter configuration.
Definition: algorithm.cpp:40
Utility classes for calculation statistical momenta and diagnostics.
void Run()
Start sampling!
Definition: algorithm.cpp:185
bool Evaluate(Sample &sample) const
Evalutate the target function prior, likelihood and -log(likelihood) at the position defined by the s...
Definition: algorithm.cpp:154
Typetrait utility structs.
Abstract base class for the core MCMC sampling algorithms.
Definition: algorithm.hpp:39
void AddWriter(ArgsT &&...args)
Add an output writer by specifying its type and passing constructor arguments.
Definition: algorithm.hpp:224
Class definitions representing input parameter configurations.
A &#39;sample&#39; represents a node or data point in a Markov Chain.
Definition: sample.hpp:31
std::enable_if< NParams==sizeof...(Ts), double >::type apply_first_n(C f, const std::vector< double > &v, Ts &&...ts)
Call a variadic function using a vector of arguments.
Definition: algorithm.hpp:161
double EvaluateLikelihood(const std::vector< double > &paramValues) const
Evaluate the target function likelihood for the given parameter values.
Definition: algorithm.cpp:140