vSMC
vSMC: Scalable Monte Carlo
bernoulli_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/bernoulli_distribution.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_RNG_BERNOULLI_DISTRIBUTION_HPP
33 #define VSMC_RNG_BERNOULLI_DISTRIBUTION_HPP
34 
37 
38 namespace vsmc
39 {
40 
41 namespace internal
42 {
43 
45 {
46  return p >= 0 && p <= 1;
47 }
48 
49 } // namespace vsmc::internal
50 
53 template <typename IntType>
55 {
57  Bernoulli, bernoulli, IntType, double, p, 0.5)
59 
60  public:
61  result_type min VSMC_MNE() const { return static_cast<result_type>(0); }
62  result_type max VSMC_MNE() const { return static_cast<result_type>(1); }
63  void reset() {}
64 
65  private:
66  template <typename RNGType>
67  result_type generate(RNGType &rng, const param_type &param)
68  {
70  double u = runif(rng);
71 
72  return generate(u, param.p(), static_cast<result_type *>(nullptr));
73  }
74 
75  static bool generate(double u, double p, bool *) { return u < p; }
76 
77  template <typename U>
78  static U generate(double u, double p, U *)
79  {
80  return u < p ? 1 : 0;
81  }
82 }; // class BernoulliDistribution
83 
84 namespace internal
85 {
86 
87 template <std::size_t K, typename IntType, typename RNGType>
89  RNGType &rng, std::size_t n, IntType *r, double p)
90 {
91  double u[K];
92  u01_co_distribution(rng, n, u);
93  std::memset(r, 0, sizeof(IntType) * n);
94  for (std::size_t i = 0; i != n; ++i)
95  if (u[i] < p)
96  r[i] = 1;
97 }
98 
99 } // namespace vsmc::internal
100 
103 template <typename IntType, typename RNGType>
105  RNGType &rng, std::size_t n, IntType *r, IntType p)
106 {
107  const std::size_t k = 1000;
108  const std::size_t m = n / k;
109  const std::size_t l = n % k;
110  for (std::size_t i = 0; i != m; ++i)
111  internal::bernoulli_distribution_impl<k>(rng, k, r + i * k, p);
112  internal::bernoulli_distribution_impl<k>(rng, l, r + m * k, p);
113 }
114 
115 template <typename IntType, typename RNGType>
116 inline void rng_rand(RNGType &rng, BernoulliDistribution<IntType> &dist,
117  std::size_t n, IntType *r)
118 {
119  dist(rng, n, r);
120 }
121 
122 } // namespace vsmc
123 
124 #endif // VSMC_RNG_BERNOULLI_DISTRIBUTION_HPP
Definition: monitor.hpp:49
Standard uniform distribution with open/closed variants.
Definition: common.hpp:512
#define VSMC_DEFINE_RNG_DISTRIBUTION_1(Name, name, T, T1, p1, v1)
Definition: common.hpp:45
void bernoulli_distribution(RNGType &rng, std::size_t n, IntType *r, IntType p)
Generating bernoulli random variates.
void rng_rand(RNGType &rng, BernoulliDistribution< IntType > &dist, std::size_t n, IntType *r)
#define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS
Definition: common.hpp:286
#define VSMC_MNE
Definition: defines.hpp:38
void u01_co_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on closed-open interval.
bool bernoulli_distribution_check_param(double p)
void bernoulli_distribution_impl(RNGType &rng, std::size_t n, IntType *r, double p)