vSMC
vSMC: Scalable Monte Carlo
u01_sequence.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/u01_sequence.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_RNG_U01_SEQUENCE_HPP
33 #define VSMC_RNG_U01_SEQUENCE_HPP
34 
37 
38 #define VSMC_RUNTIME_ASSERT_RNG_U01_SEQUENCE(Method) \
39  VSMC_RUNTIME_ASSERT((n < N_ && (n == n_ || n == n_ + 1 || n_ == N_)), \
40  "**U01Sequence" #Method "::operator[]** INVALID INDEX")
41 
42 namespace vsmc
43 {
44 
53 template <typename RealType>
54 inline void u01_sorted(std::size_t N, const RealType *u01, RealType *u01seq)
55 {
56  log(N, u01, u01seq);
57  RealType lmax = 0;
58  for (std::size_t i = 0; i != N; ++i) {
59  lmax += u01seq[i] / (N - i);
60  u01seq[i] = 1 - std::exp(lmax);
61  }
62 }
63 
67 template <typename RealType>
68 inline void u01_stratified(
69  std::size_t N, const RealType *u01, RealType *u01seq)
70 {
71  RealType delta = 1 / static_cast<RealType>(N);
72  for (std::size_t i = 0; i != N; ++i)
73  u01seq[i] = u01[i] * delta + i * delta;
74 }
75 
79 template <typename RealType>
80 inline void u01_systematic(std::size_t N, RealType u01, RealType *u01seq)
81 {
82  if (N == 0)
83  return;
84 
85  RealType delta = 1.0 / static_cast<RealType>(N);
86  u01seq[0] = u01 * delta;
87  for (std::size_t i = 1; i != N; ++i)
88  u01seq[i] = u01seq[i - 1] + delta;
89 }
90 
130 template <typename RNGType, typename RealType = double>
132 {
133  public:
134  using result_type = RealType;
135 
136  U01SequenceSorted(std::size_t N, RNGType &rng)
137  : N_(N), n_(N), u_(0), lmax_(0), rng_(rng)
138  {
139  }
140 
141  result_type operator[](std::size_t n)
142  {
144 
145  if (n == n_)
146  return u_;
147 
149  lmax_ += std::log(u01(rng_)) / (N_ - n);
150  n_ = n;
151  u_ = 1 - std::exp(lmax_);
152 
153  return u_;
154  }
155 
156  result_type operator()(std::size_t n) { return operator[](n); }
157 
158  private:
159  std::size_t N_;
160  std::size_t n_;
161  result_type u_;
162  result_type lmax_;
163  RNGType &rng_;
164 }; // U01SequenceSorted
165 
176 template <typename RNGType, typename RealType = double>
178 {
179  public:
180  using result_type = RealType;
181 
182  U01SequenceStratified(std::size_t N, RNGType &rng)
183  : N_(N)
184  , n_(N)
185  , u_(0)
186  , delta_(1 / static_cast<result_type>(N))
187  , rng_(rng)
188  {
189  }
190 
191  result_type operator[](std::size_t n)
192  {
194 
195  if (n == n_)
196  return u_;
197 
199  n_ = n;
200  u_ = u01(rng_) * delta_ + n * delta_;
201 
202  return u_;
203  }
204 
205  result_type operator()(std::size_t n) { return operator[](n); }
206 
207  private:
208  std::size_t N_;
209  std::size_t n_;
210  result_type u_;
211  result_type delta_;
212  RNGType &rng_;
213 }; // class U01SequenceStratified
214 
225 template <typename RNGType, typename RealType = double>
227 {
228  public:
229  using result_type = RealType;
230 
231  U01SequenceSystematic(std::size_t N, RNGType &rng)
232  : N_(N), n_(N), u_(0), u0_(0), delta_(1 / static_cast<result_type>(N))
233  {
235  u0_ = u01(rng) * delta_;
236  }
237 
238  result_type operator[](std::size_t n)
239  {
241 
242  if (n == n_)
243  return u_;
244 
245  n_ = n;
246  u_ = u0_ + n * delta_;
247 
248  return u_;
249  }
250 
251  result_type operator()(std::size_t n) { return operator[](n); }
252 
253  private:
254  std::size_t N_;
255  std::size_t n_;
256  result_type u_;
257  result_type u0_;
258  result_type delta_;
259 }; // class U01SequenceSystematic
260 
261 } // namespace vsmc
262 
263 #endif // VSMC_RNG_U01_SEQUENCE_HPP
Definition: monitor.hpp:49
result_type operator()(std::size_t n)
U01SequenceStratified(std::size_t N, RNGType &rng)
Generate a fixed length sequence of uniform random variates by stratified sampling.
result_type operator[](std::size_t n)
Generate a fixed length sequence of uniform random variates by systematic sampling.
result_type operator[](std::size_t n)
void u01_systematic(std::size_t N, RealType u01, RealType *u01seq)
Transform a single standard uniform random number to a systematic sequence.
Stratified resampling.
Definition: defines.hpp:59
void u01_stratified(std::size_t N, const RealType *u01, RealType *u01seq)
Transform a sequence of standard uniform random numbers to a stratified sequence. ...
U01SequenceSorted(std::size_t N, RNGType &rng)
void exp(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:146
result_type operator[](std::size_t n)
Generate a fixed length sequence of uniform random variates by sorting.
result_type operator()(std::size_t n)
#define VSMC_RUNTIME_ASSERT_RNG_U01_SEQUENCE(Method)
void u01_sorted(std::size_t N, const RealType *u01, RealType *u01seq)
Tranform a sequence of standard uniform random numbers to sorted sequence.
result_type operator()(std::size_t n)
Systematic resampling.
Definition: defines.hpp:60
Standard uniform distribution.
Definition: common.hpp:597
U01SequenceSystematic(std::size_t N, RNGType &rng)
void log(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:148