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,2014, 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>
37 
38 #define VSMC_RUNTIME_ASSERT_CORE_PATH_ITER(func) \
39  VSMC_RUNTIME_ASSERT((iter < iter_size()), \
40  ("**Path::"#func"** INVALID ITERATION NUMBER ARGUMENT"))
41 
42 #define VSMC_RUNTIME_ASSERT_CORE_PATH_FUNCTOR(func, caller, name) \
43  VSMC_RUNTIME_ASSERT(static_cast<bool>(func), \
44  ("**Path::"#caller"** INVALID "#name" OBJECT")) \
45 
46 namespace vsmc {
47 
50 template <typename T>
51 class Path
52 {
53  public :
54 
55  typedef T value_type;
56  typedef cxx11::function<double (
57  std::size_t, const Particle<T> &, double *)> eval_type;
58 
90  explicit Path (const eval_type &eval, bool record_only = false) :
91  eval_(eval), recording_(true), record_only_(record_only),
92  log_zconst_(0) {}
93 
97  std::size_t iter_size () const {return index_.size();}
98 
100  void reserve (std::size_t num)
101  {
102  index_.reserve(num);
103  integrand_.reserve(num);
104  grid_.reserve(num);
105  }
106 
108  bool empty () const {return !static_cast<bool>(eval_);}
109 
114  std::size_t index (std::size_t iter) const
115  {
117 
118  return index_[iter];
119  }
120 
122  double integrand (std::size_t iter) const
123  {
125 
126  return integrand_[iter];
127  }
128 
130  double grid (std::size_t iter) const
131  {
133 
134  return grid_[iter];
135  }
136 
138  const std::size_t *index_data () const {return &index_[0];}
139 
141  const std::size_t *integrand_data () const {return &integrand_[0];}
142 
144  const std::size_t *grid_data () const {return &grid_[0];}
145 
149  template <typename OutputIter>
150  void read_index (OutputIter first) const
151  {std::copy(index_.begin(), index_.end(), first);}
152 
154  template <typename OutputIter>
155  void read_integrand (OutputIter first) const
156  {std::copy(integrand_.begin(), integrand_.end(), first);}
157 
159  template <typename OutputIter>
160  void read_grid (OutputIter first) const
161  {std::copy(grid_.begin(), grid_.end(), first);}
162 
164  void set_eval (const eval_type &new_eval, bool record_only = false)
165  {eval_ = new_eval; record_only_ = record_only;}
166 
170  void eval (std::size_t iter, const Particle<T> &particle)
171  {
172  if (!recording_)
173  return;
174 
175  VSMC_RUNTIME_ASSERT_CORE_PATH_FUNCTOR(eval_, eval, EVALUATION);
176 
177  if (record_only_) {
178  double integrand = 0;
179  double grid = eval_(iter, particle, &integrand);
180  push_back(iter, grid, integrand);
181 
182  return;
183  }
184 
185  const std::size_t N = static_cast<std::size_t>(particle.size());
186  buffer_.resize(N);
187  double *const bptr = &buffer_[0];
188  const double *const wptr = particle.weight_set().weight_data();
189  double grid = eval_(iter, particle, bptr);
190  double integrand = math::dot(N, wptr, bptr);
191  push_back(iter, grid, integrand);
192  }
193 
195  double zconst () const {return std::exp(log_zconst_);}
196 
198  double log_zconst () const {return log_zconst_;}
199 
201  void clear ()
202  {
203  log_zconst_ = 0;
204  index_.clear();
205  integrand_.clear();
206  grid_.clear();
207  }
208 
210  bool recording () const {return recording_;}
211 
213  void turn_on () {recording_ = true;}
214 
216  void turn_off () {recording_ = false;}
217 
218  private :
219 
220  eval_type eval_;
221  bool recording_;
222  bool record_only_;
223  double log_zconst_;
224  std::vector<std::size_t> index_;
225  std::vector<double, AlignedAllocator<double> > integrand_;
226  std::vector<double, AlignedAllocator<double> > grid_;
227  std::vector<double, AlignedAllocator<double> > buffer_;
228 
229  void push_back (std::size_t iter, double grid, double integrand)
230  {
231  index_.push_back(iter);
232  grid_.push_back(grid);
233  integrand_.push_back(integrand);
234  if (iter_size() > 1) {
235  std::size_t i = iter_size() - 1;
236  log_zconst_ += 0.5 * (grid_[i] - grid_[i - 1]) *
237  (integrand_[i] + integrand_[i - 1]);
238  }
239  }
240 }; // class PathSampling
241 
242 } // namespace vsmc
243 
244 #endif // VSMC_CORE_PATH_HPP
cxx11::function< double(std::size_t, const Particle< T > &, double *)> eval_type
Definition: path.hpp:57
const std::size_t * integrand_data() const
Read only access to the raw data of the integrand vector.
Definition: path.hpp:141
Definition: adapter.hpp:37
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:100
double log_zconst() const
Get the logarithm nomralizing constants ratio estimates.
Definition: path.hpp:198
double grid(std::size_t iter) const
Get the Path sampling grid value of a given Path iteration.
Definition: path.hpp:130
void eval(std::size_t iter, const Particle< T > &particle)
Definition: path.hpp:170
void turn_on()
Turn on the recording.
Definition: path.hpp:213
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given monitor iteration.
Definition: path.hpp:114
double zconst() const
Get the nomralizing constants ratio estimates.
Definition: path.hpp:195
const std::size_t * grid_data() const
Read only access to the raw data of the grid vector.
Definition: path.hpp:144
const std::size_t * index_data() const
Read only access to the raw data of the index vector.
Definition: path.hpp:138
bool recording() const
Whether the Path is actively recording restuls.
Definition: path.hpp:210
bool empty() const
Whether the evaluation object is valid.
Definition: path.hpp:108
#define VSMC_RUNTIME_ASSERT_CORE_PATH_ITER(func)
Definition: path.hpp:38
T dot(std::size_t n, const T *x, const T *y)
The dot product.
Definition: cblas.hpp:75
void clear()
Clear all records of the index and integrations.
Definition: path.hpp:201
T value_type
Definition: path.hpp:55
Path(const eval_type &eval, bool record_only=false)
Construct a Path with an evaluation object.
Definition: path.hpp:90
Monitor for Path sampling.
Definition: path.hpp:51
std::size_t iter_size() const
The number of iterations has been recorded.
Definition: path.hpp:97
void turn_off()
Turn off the recording.
Definition: path.hpp:216
double integrand(std::size_t iter) const
Get the Path sampling integrand of a given Path iteration.
Definition: path.hpp:122
void set_eval(const eval_type &new_eval, bool record_only=false)
Set a new evaluation object of type eval_type.
Definition: path.hpp:164
void read_integrand(OutputIter first) const
Read the integrand history through an output iterator.
Definition: path.hpp:155
#define VSMC_RUNTIME_ASSERT_CORE_PATH_FUNCTOR(func, caller, name)
Definition: path.hpp:42
size_type size() const
Number of particles.
Definition: particle.hpp:146
void read_grid(OutputIter first) const
Read the grid history through an output iterator.
Definition: path.hpp:160
void read_index(OutputIter first) const
Read the index history through an output iterator.
Definition: path.hpp:150
weight_set_type & weight_set()
Read and write access to the weight collection object.
Definition: particle.hpp:155