vSMC  v3.0.0
Scalable Monte Carlo
algorithm.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/resample/algorithm.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, 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_RESAMPLE_ALGORITHM_HPP
33 #define VSMC_RESAMPLE_ALGORITHM_HPP
34 
35 #include <vsmc/internal/common.hpp>
38 
39 #define VSMC_RUNTIME_ASSERT_RESAMPLE_ALGORITHM_EVAL \
40  VSMC_RUNTIME_ASSERT( \
41  eval_, "**ResampleEval::operator()** INVALID ALGORITHM OBJECT")
42 
43 namespace vsmc
44 {
45 
48 template <typename U01SeqType, bool Residual>
50 {
51  public:
59  template <typename RNGType, typename InputIter, typename OutputIter>
60  OutputIter operator()(std::size_t N, std::size_t M, RNGType &rng,
61  InputIter weight, OutputIter replication) const
62  {
63  return eval(N, M, rng, weight, replication,
64  std::integral_constant<bool, Residual>());
65  }
66 
67  private:
68  U01SeqType u01seq_;
69 
70  template <typename RNGType, typename InputIter, typename OutputIter>
71  OutputIter eval(std::size_t N, std::size_t M, RNGType &rng,
72  InputIter weight, OutputIter replication, std::false_type) const
73  {
74  using real_type = typename std::iterator_traits<InputIter>::value_type;
75 
77  u01seq_(rng, M, u01.data());
78 
79  return resample_trans_u01_rep(N, M, weight, u01.data(), replication);
80  }
81 
82  template <typename RNGType, typename InputIter, typename OutputIter>
83  OutputIter eval(std::size_t N, std::size_t M, RNGType &rng,
84  InputIter weight, OutputIter replication, std::true_type) const
85  {
86  using real_type = typename std::iterator_traits<InputIter>::value_type;
87  using rep_type = typename std::iterator_traits<OutputIter>::value_type;
88 
89  Vector<real_type> resid(N);
90  Vector<rep_type> integ(N);
91  std::size_t R =
92  resample_trans_residual(N, M, weight, resid.data(), integ.data());
93 
95  u01seq_(rng, R, u01.data());
96  resample_trans_u01_rep(N, R, resid.data(), u01.data(), replication);
97  for (std::size_t i = 0; i != N; ++i, ++replication)
98  *replication += integ[i];
99 
100  return replication;
101  }
102 }; // class ResampleAlgorithm
103 
105 template <typename T>
107 {
108  public:
109  using eval_type = std::function<void(std::size_t, std::size_t,
110  typename Particle<T>::rng_type &, const double *,
112 
117  explicit ResampleEval(const eval_type &eval) : eval_(eval) {}
118 
119  static constexpr double always()
120  {
121  return std::numeric_limits<double>::infinity();
122  }
123 
124  static constexpr double never()
125  {
126  return -std::numeric_limits<double>::infinity();
127  }
128 
130  void set_eval(const eval_type &new_eval) { eval_ = new_eval; }
131 
133  std::size_t operator()(std::size_t, Particle<T> &particle) const
134  {
136 
137  using size_type = typename Particle<T>::size_type;
138 
139  const std::size_t N = static_cast<std::size_t>(particle.size());
140  Vector<size_type> rep(N);
141  Vector<size_type> idx(N);
142  eval_(N, N, particle.rng(), particle.weight().data(), rep.data());
143  resample_trans_rep_index(N, N, rep.data(), idx.data());
144  particle.state().select(N, idx.data());
145  particle.weight().set_equal();
146 
147  std::size_t R = 0;
148  for (auto r : rep)
149  if (r != 0)
150  ++R;
151 
152  return R;
153  }
154 
155  private:
156  eval_type eval_;
157 }; // class ResampleEval
158 
162 
166 
170 
174 
179 
184 
187 template <ResampleScheme>
189 
192 template <>
194 {
195  public:
197 }; // class ResampleTypeTrait
198 
201 template <>
203 {
204  public:
206 }; // class ResampleTypeTrait
207 
210 template <>
212 {
213  public:
215 }; // class ResampleTypeTrait
216 
219 template <>
221 {
222  public:
224 }; // class ResampleTypeTrait
225 
228 template <>
230 {
231  public:
233 }; // class ResampleTypeTrait
234 
237 template <>
239 {
240  public:
242 }; // class ResampleTypeTrait
243 
246 template <ResampleScheme Scheme>
248 
249 } // namespace vsmc
250 
251 #endif // VSMC_RESAMPLE_ALGORITHM_HPP
std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
ResampleAlgorithm< U01SequenceSorted, false > ResampleMultinomial
Multinomial resampling.
Definition: algorithm.hpp:161
Definition: monitor.hpp:48
SizeType< T > size_type
Definition: particle.hpp:86
Particle class representing the whole particle set.
Definition: particle.hpp:83
typename rng_set_type::rng_type rng_type
Definition: particle.hpp:90
std::size_t operator()(std::size_t, Particle< T > &particle) const
Returns how many particles having non-zero replication number.
Definition: algorithm.hpp:133
state_type & state()
Read and write access to the state collection object.
Definition: particle.hpp:168
OutputIter resample_trans_rep_index(std::size_t N, std::size_t M, InputIter replication, OutputIter index)
Transform replication numbers into parent indices.
Definition: transform.hpp:130
static constexpr double always()
Definition: algorithm.hpp:119
weight_type & weight()
Read and write access to the weight collection object.
Definition: particle.hpp:174
Stratified resampling on residuals.
Definition: defines.hpp:64
void set_eval(const eval_type &new_eval)
Set a new evaluation object of type eval_type.
Definition: algorithm.hpp:130
OutputIter operator()(std::size_t N, std::size_t M, RNGType &rng, InputIter weight, OutputIter replication) const
Generate replication numbers from normalized weights.
Definition: algorithm.hpp:60
ResampleAlgorithm< U01SequenceStratified, false > ResampleStratified
Stratified resampling.
Definition: algorithm.hpp:165
Multinomial resampling.
Definition: defines.hpp:60
ResampleEval(const eval_type &eval)
Construct a Sampler::move_type object.
Definition: algorithm.hpp:117
ResampleAlgorithm< U01SequenceSystematic, true > ResampleResidualSystematic
Residual systematic resampling.
Definition: algorithm.hpp:183
Sampler<T>::eval_type subtype.
Definition: algorithm.hpp:106
Stratified resampling.
Definition: defines.hpp:61
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:213
ResampleAlgorithm< U01SequenceStratified, true > ResampleResidualStratified
Residual stratified resampling.
Definition: algorithm.hpp:178
Residual resampling.
Definition: defines.hpp:63
Systematic resampling on residuals.
Definition: defines.hpp:65
std::size_t resample_trans_residual(std::size_t N, std::size_t M, InputIter weight, OutputIterR resid, OutputIterI integ)
Transform normalized weights to normalized residual and integrals,.
Definition: transform.hpp:50
OutputIter resample_trans_u01_rep(std::size_t N, std::size_t M, InputIter weight, U01SeqType &&u01seq, OutputIter replication)
Transform uniform [0, 1) sequence into replication numbers.
Definition: transform.hpp:90
rng_type & rng(size_type id)
Get an (parallel) RNG stream for a given particle.
Definition: particle.hpp:186
static constexpr double never()
Definition: algorithm.hpp:124
#define VSMC_RUNTIME_ASSERT_RESAMPLE_ALGORITHM_EVAL
Definition: algorithm.hpp:39
std::function< void(std::size_t, std::size_t, typename Particle< T >::rng_type &, const double *, typename Particle< T >::size_type *)> eval_type
Definition: algorithm.hpp:111
Systematic resampling.
Definition: defines.hpp:62
size_type size() const
Number of particles.
Definition: particle.hpp:120
Resampling algorithm.
Definition: algorithm.hpp:49
Type trait of ResampleScheme parameter.
Definition: algorithm.hpp:188
ResampleAlgorithm< U01SequenceSorted, true > ResampleResidual
Residual resampling.
Definition: algorithm.hpp:173
typename ResampleTypeTrait< Scheme >::type ResampleType
Type of resample class corresponding to ResampleScheme parameter.
Definition: algorithm.hpp:247
ResampleAlgorithm< U01SequenceSystematic, false > ResampleSystematic
Systematic resampling.
Definition: algorithm.hpp:169