vSMC  v3.0.0
Scalable Monte Carlo
normal_distribution.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/normal_distribution.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_NORMAL_DISTRIBUTION_HPP
33 #define VSMC_RNG_NORMAL_DISTRIBUTION_HPP
34 
38 
39 namespace vsmc
40 {
41 
42 namespace internal
43 {
44 
45 template <typename RealType>
46 inline bool normal_distribution_check_param(RealType, RealType stddev)
47 {
48  return stddev > 0;
49 }
50 
51 } // namespace vsmc::internal
52 
55 template <typename RealType>
57 {
58  VSMC_DEFINE_RNG_DISTRIBUTION_2(Normal, normal, mean, 0, stddev, 1)
60 
61  public:
62  result_type min() const
63  {
64  return std::numeric_limits<result_type>::lowest();
65  }
66 
67  result_type max() const { return std::numeric_limits<result_type>::max(); }
68 
69  void reset()
70  {
71  v_ = 0;
72  saved_ = false;
73  }
74 
75  private:
76  template <typename RNGType>
77  result_type generate(RNGType &rng, const param_type &param)
78  {
79  result_type z = 0;
80  if (saved_) {
81  z = v_;
82  saved_ = false;
83  } else {
85  result_type u1 = std::sqrt(-2 * std::log(u01(rng)));
86  result_type u2 = const_pi_2<result_type>() * u01(rng);
87  z = u1 * std::cos(u2);
88  v_ = u1 * std::sin(u2);
89  saved_ = true;
90  }
91 
92  return param.mean() + param.stddev() * z;
93  }
94 }; // class NormalDistribution
95 
96 namespace internal
97 {
98 
99 template <std::size_t K, typename RealType, typename RNGType>
101  RNGType &rng, std::size_t n, RealType *r, RealType mean, RealType stddev)
102 {
103  Array<RealType, K / 2> s;
104  const std::size_t nu = n / 2;
105  RealType *const u1 = r;
106  RealType *const u2 = r + nu;
107  u01_oc_distribution(rng, n, r);
108  log(nu, u1, s.data());
109  mul(nu, static_cast<RealType>(-2), s.data(), s.data());
110  sqrt(nu, s.data(), s.data());
111  mul(nu, const_pi_2<RealType>(), u2, u2);
112  sincos(nu, u2, u1, u2);
113  if (!is_one(stddev))
114  mul(nu, stddev, s.data(), s.data());
115  if (!is_zero(mean)) {
116  fma(nu, s.data(), u1, mean, u1);
117  fma(nu, s.data(), u2, mean, u2);
118  } else {
119  mul(nu, s.data(), u1, u1);
120  mul(nu, s.data(), u2, u2);
121  }
122 }
123 
124 } // namespace vsmc::internal
125 
128 template <typename RealType, typename RNGType>
130  RNGType &rng, std::size_t n, RealType *r, RealType mean, RealType stddev)
131 {
132  static_assert(std::is_floating_point<RealType>::value,
133  "**normal_distribution** USED WITH RealType OTHER THAN FLOATING POINT "
134  "TYPES");
135 
136  const std::size_t k = internal::BufferSize<RealType>::value;
137  const std::size_t m = n / k;
138  const std::size_t l = n % k;
139  for (std::size_t i = 0; i != m; ++i, r += k)
140  internal::normal_distribution_impl<k>(rng, k, r, mean, stddev);
141  internal::normal_distribution_impl<k>(rng, l, r, mean, stddev);
142  if (n % 2 != 0) {
144  RealType u = u01(rng);
145  RealType v = u01(rng);
146  r[l - 1] = mean +
147  stddev * std::sqrt(-2 * std::log(u)) *
148  std::cos(const_pi_2<RealType>() * v);
149  }
150 }
151 
152 VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Normal, normal, mean, stddev)
153 
154 } // namespace vsmc
155 
156 #endif // VSMC_RNG_NORMAL_DISTRIBUTION_HPP
Definition: monitor.hpp:48
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:77
#define VSMC_DEFINE_RNG_DISTRIBUTION_2(Name, name, p1, v1, p2, v2)
void sqrt(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:96
#define VSMC_DEFINE_RNG_DISTRIBUTION_MEMBER_2(T1, m1, T2, m2)
Standard uniform distribution on (0, 1].
void normal_distribution_impl(RNGType &rng, std::size_t n, RealType *r, RealType mean, RealType stddev)
void normal_distribution(RNGType &, std::size_t, RealType *, RealType, RealType)
Generating Normal random variates.
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:213
void cos(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:121
bool normal_distribution_check_param(RealType, RealType stddev)
bool is_one(const T &a)
Definition: basic.hpp:107
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
Definition: vmath.hpp:361
void sincos(std::size_t n, const float *a, float *y, float *z)
Definition: vmath.hpp:123
void sin(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:122
bool is_zero(const T &a)
Definition: basic.hpp:101
#define VSMC_DEFINE_RNG_DISTRIBUTION_RAND_2(Name, name, p1, p2)
std::array with proper alignment
void u01_oc_distribution(RNGType &rng, std::size_t n, RealType *r)
Generate standard uniform random variates on (0, 1].
void log(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:117