vSMC
vSMC: Scalable Monte Carlo
path.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/core/path.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2015, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_CORE_PATH_HPP
33 #define VSMC_CORE_PATH_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 
37 #define VSMC_RUNTIME_ASSERT_CORE_PATH_ITER(func) \
38  VSMC_RUNTIME_ASSERT((iter < iter_size()), \
39  "**Path::" #func "** INVALID ITERATION NUMBER ARGUMENT")
40 
41 #define VSMC_RUNTIME_ASSERT_CORE_PATH_EVAL \
42  VSMC_RUNTIME_ASSERT( \
43  static_cast<bool>(eval_), "**Path::eval** INVALID EVALUAITON OBJECT")
44 
45 namespace vsmc
46 {
47 
50 template <typename T>
51 class Path
52 {
53  public:
54  using value_type = T;
55  using eval_type =
56  std::function<double(std::size_t, Particle<T> &, double *)>;
57 
95  explicit Path(const eval_type &eval, bool record_only = false)
96  : eval_(eval)
97  , recording_(true)
98  , record_only_(record_only)
99  , log_zconst_(0)
100  {
101  }
102 
106  std::size_t iter_size() const { return index_.size(); }
107 
109  void reserve(std::size_t num)
110  {
111  index_.reserve(num);
112  integrand_.reserve(num);
113  grid_.reserve(num);
114  }
115 
117  bool empty() const { return !static_cast<bool>(eval_); }
118 
123  std::size_t index(std::size_t iter) const
124  {
126 
127  return index_[iter];
128  }
129 
131  double integrand(std::size_t iter) const
132  {
134 
135  return integrand_[iter];
136  }
137 
139  double grid(std::size_t iter) const
140  {
142 
143  return grid_[iter];
144  }
145 
147  const std::size_t *index_data() const { return index_.data(); }
148 
150  const std::size_t *integrand_data() const { return integrand_.data(); }
151 
153  const std::size_t *grid_data() const { return grid_.data(); }
154 
158  template <typename OutputIter>
159  void read_index(OutputIter first) const
160  {
161  std::copy(index_.begin(), index_.end(), first);
162  }
163 
165  template <typename OutputIter>
166  void read_integrand(OutputIter first) const
167  {
168  std::copy(integrand_.begin(), integrand_.end(), first);
169  }
170 
172  template <typename OutputIter>
173  void read_grid(OutputIter first) const
174  {
175  std::copy(grid_.begin(), grid_.end(), first);
176  }
177 
179  void set_eval(const eval_type &new_eval, bool record_only = false)
180  {
181  eval_ = new_eval;
182  record_only_ = record_only;
183  }
184 
188  void eval(std::size_t iter, Particle<T> &particle)
189  {
190  if (!recording_)
191  return;
192 
194 
195  if (record_only_) {
196  double integrand = 0;
197  double grid = eval_(iter, particle, &integrand);
198  push_back(iter, grid, integrand);
199 
200  return;
201  }
202 
203  const std::size_t N = static_cast<std::size_t>(particle.size());
204  buffer_.resize(N);
205  double grid = eval_(iter, particle, buffer_.data());
206  double integrand =
207  dot(N, particle.weight().data(), 1, buffer_.data(), 1);
208  push_back(iter, grid, integrand);
209  }
210 
212  double zconst() const { return std::exp(log_zconst_); }
213 
215  double log_zconst() const { return log_zconst_; }
216 
218  void clear()
219  {
220  log_zconst_ = 0;
221  index_.clear();
222  integrand_.clear();
223  grid_.clear();
224  }
225 
227  bool recording() const { return recording_; }
228 
230  void turn_on() { recording_ = true; }
231 
233  void turn_off() { recording_ = false; }
234 
235  private:
236  eval_type eval_;
237  bool recording_;
238  bool record_only_;
239  double log_zconst_;
240  Vector<std::size_t> index_;
241  Vector<double> integrand_;
242  Vector<double> grid_;
243  Vector<double> buffer_;
244 
245  void push_back(std::size_t iter, double grid, double integrand)
246  {
247  index_.push_back(iter);
248  grid_.push_back(grid);
249  integrand_.push_back(integrand);
250  if (iter_size() > 1) {
251  std::size_t i = iter_size() - 1;
252  log_zconst_ += 0.5 * (grid_[i] - grid_[i - 1]) *
253  (integrand_[i] + integrand_[i - 1]);
254  }
255  }
256 }; // class PathSampling
257 
258 } // namespace vsmc
259 
260 #endif // VSMC_CORE_PATH_HPP
T dot(std::size_t n, const T *x, std::size_t incx, const T *y, std::size_t incy)
Computes a vector-vector dot product.
Definition: cblas.hpp:93
const std::size_t * integrand_data() const
Read only access to the raw data of the integrand vector.
Definition: path.hpp:150
Definition: monitor.hpp:49
Particle class representing the whole particle set.
Definition: particle.hpp:48
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Definition: path.hpp:109
#define VSMC_RUNTIME_ASSERT_CORE_PATH_EVAL
Definition: path.hpp:41
double log_zconst() const
Get the logarithm nomralizing constants ratio estimates.
Definition: path.hpp:215
double grid(std::size_t iter) const
Get the Path sampling grid value of a given Path iteration.
Definition: path.hpp:139
void eval(std::size_t iter, Particle< T > &particle)
Definition: path.hpp:188
#define VSMC_RUNTIME_ASSERT_CORE_PATH_ITER(func)
Definition: path.hpp:37
typename std::conditional< std::is_scalar< T >::value, std::vector< T, AlignedAllocator< T >>, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
void turn_on()
Turn on the recording.
Definition: path.hpp:230
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given monitor iteration.
Definition: path.hpp:123
weight_type & weight()
Read and write access to the weight collection object.
Definition: particle.hpp:131
double zconst() const
Get the nomralizing constants ratio estimates.
Definition: path.hpp:212
const std::size_t * grid_data() const
Read only access to the raw data of the grid vector.
Definition: path.hpp:153
std::function< double(std::size_t, Particle< T > &, double *)> eval_type
Definition: path.hpp:56
const std::size_t * index_data() const
Read only access to the raw data of the index vector.
Definition: path.hpp:147
bool recording() const
Whether the Path is actively recording restuls.
Definition: path.hpp:227
void copy(std::size_t n, const T *x, std::size_t incx, T *y, std::size_t incy)
Copies vector to another vector.
Definition: cblas.hpp:82
bool empty() const
Whether the evaluation object is valid.
Definition: path.hpp:117
T value_type
Definition: path.hpp:54
void exp(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:146
void clear()
Clear all records of the index and integrations.
Definition: path.hpp:218
Path(const eval_type &eval, bool record_only=false)
Construct a Path with an evaluation object.
Definition: path.hpp:95
Monitor for Path sampling.
Definition: path.hpp:51
std::size_t iter_size() const
The number of iterations has been recorded.
Definition: path.hpp:106
void turn_off()
Turn off the recording.
Definition: path.hpp:233
double integrand(std::size_t iter) const
Get the Path sampling integrand of a given Path iteration.
Definition: path.hpp:131
void set_eval(const eval_type &new_eval, bool record_only=false)
Set a new evaluation object of type eval_type.
Definition: path.hpp:179
void read_integrand(OutputIter first) const
Read the integrand history through an output iterator.
Definition: path.hpp:166
size_type size() const
Number of particles.
Definition: particle.hpp:122
void read_grid(OutputIter first) const
Read the grid history through an output iterator.
Definition: path.hpp:173
void read_index(OutputIter first) const
Read the index history through an output iterator.
Definition: path.hpp:159