MCKL
Monte Carlo Kernel Library
serial_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/serial_test.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, 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 MCKL_RANDOM_SERIAL_TEST_HPP
33 #define MCKL_RANDOM_SERIAL_TEST_HPP
34 
36 
37 namespace mckl {
38 
39 namespace internal {
40 
41 template <std::size_t, std::size_t, bool>
43 
44 template <std::size_t D, std::size_t T>
45 class SerialTestImpl<D, T, false>
46  : public ChiSquaredTest<SerialTestImpl<D, T, false>>
47 {
48  static_assert(D > 1, "**SerialTest** used with D less than 2");
49 
50  static_assert(T > 0, "**SerialTest** used with T equal to zero");
51 
52  public:
53  SerialTestImpl(std::size_t n)
54  : n_(n), np_(static_cast<double>(n) / M_), count_(M_)
55  {
56  }
57 
59 
60  template <typename RNGType, typename U01DistributionType>
61  double operator()(RNGType &rng, U01DistributionType &u01)
62  {
63  using result_type = typename U01DistributionType::result_type;
64 
65  std::fill(count_.begin(), count_.end(), 0);
66 
67  const std::size_t k = BufferSize<result_type, T>::value;
68  const std::size_t m = n_ / k;
69  const std::size_t l = n_ % k;
70  Vector<result_type> r(k * T);
71  for (std::size_t i = 0; i != m; ++i) {
72  generate(rng, u01, k, r.data());
73  }
74  generate(rng, u01, l, r.data());
75 
76  return this->stat(M_, count_.data(), np_);
77  }
78 
79  double degree_of_freedom() const { return static_cast<double>(M_ - 1); }
80 
81  private:
82  static constexpr std::size_t M_ = Pow<std::size_t, D, T>::value;
83 
84  std::size_t n_;
85  double np_;
86  Vector<double> count_;
87 
88  template <typename RNGType, typename U01DistributionType>
89  void generate(RNGType &rng, U01DistributionType &u01, std::size_t n,
90  typename U01DistributionType::result_type *r)
91  {
92  rand(rng, u01, n * T, r);
93  mul(n * T, static_cast<typename U01DistributionType::result_type>(D),
94  r, r);
95  for (std::size_t i = 0; i != n; ++i, r += T) {
96  count_[serial_index<D, T>(r)] += 1;
97  }
98  }
99 }; // class SerialTestImpl
100 
101 template <std::size_t D, std::size_t T>
102 class SerialTestImpl<D, T, true>
103  : public ChiSquaredTest<SerialTestImpl<D, T, true>>
104 {
105  static_assert(D > 1, "**SerialOverTest** used with D less than 2");
106 
107  static_assert(T > 1, "**SerialOverTest** used with T less than 2");
108 
109  public:
110  SerialTestImpl(std::size_t n)
111  : n_(n)
112  , np1_(static_cast<double>(n) / M1_)
113  , np2_(static_cast<double>(n) / M2_)
114  , count1_(M1_)
115  , count2_(M2_)
116  {
118  n >= T, "**SerialTest** constructed with n less then T");
119  }
120 
122 
123  template <typename RNGType, typename U01DistributionType>
124  double operator()(RNGType &rng, U01DistributionType &u01)
125  {
126  using result_type = typename U01DistributionType::result_type;
127 
128  std::fill(count1_.begin(), count1_.end(), 0);
129  std::fill(count2_.begin(), count2_.end(), 0);
130 
131  const std::size_t k = BufferSize<result_type>::value;
132  Vector<result_type> r(k);
133  std::array<result_type, T> rhead;
134  std::array<result_type, T> rtail;
135 
136  // generate the first tuple
137  rand(rng, u01, k, r.data());
138  mul(k, static_cast<result_type>(D), r.data(), r.data());
139  std::memcpy(rhead.data(), r.data(), sizeof(result_type) * T);
140  std::memcpy(rtail.data(), r.data(), sizeof(result_type) * T);
141 
142  // generate the first n - t + 1 counts
143  cidx_ = serial_index<D, T>(rhead.data());
144  head_ = 0;
145  tail_ = T - 1;
146  std::size_t uidx = T;
147  for (std::size_t i = 0; i != n_ - T + 1; ++i) {
148  if (uidx == k) {
149  rand(rng, u01, k, r.data());
150  mul(k, static_cast<result_type>(D), r.data(), r.data());
151  uidx = 0;
152  }
153  result_type u = r[uidx++];
154  generate(u, rtail.data());
155  }
156 
157  // generate the last t - 1 counts
158  uidx = 1;
159  for (std::size_t i = n_ - T + 1; i != n_; ++i) {
160  result_type u = rhead[uidx++];
161  generate(u, rtail.data());
162  }
163 
164  double s1 = this->stat(M1_, count1_.data(), np1_);
165  double s2 = this->stat(M2_, count2_.data(), np2_);
166 
167  return s1 - s2;
168  }
169 
170  double degree_of_freedom() const { return static_cast<double>(M1_ - M2_); }
171 
172  private:
173  static constexpr std::size_t M1_ = Pow<std::size_t, D, T>::value;
174  static constexpr std::size_t M2_ = Pow<std::size_t, D, T - 1>::value;
175 
176  std::size_t n_;
177  double np1_;
178  double np2_;
179  std::size_t cidx_;
180  std::size_t head_;
181  std::size_t tail_;
182  Vector<double> count1_;
183  Vector<double> count2_;
184 
185  template <typename ResultType>
186  void generate(ResultType u, ResultType *rtail)
187  {
188  cidx_ -= Pow<std::size_t, D, T - 1>::value *
189  static_cast<std::size_t>(rtail[head_]);
190  count2_[cidx_] += 1;
191  head_ = (head_ + 1) % T;
192  tail_ = (tail_ + 1) % T;
193  rtail[tail_] = u;
194  cidx_ = cidx_ * D + static_cast<std::size_t>(u);
195  count1_[cidx_] += 1;
196  }
197 }; // class SerialTestImpl
198 
199 } // namespace internal
200 
207 template <std::size_t D, std::size_t T, bool Overlap>
209 
210 } // namespace mckl
211 
212 #endif // MCKL_RANDOM_SERIAL_TEST_HPP
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:124
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Definition: memory.hpp:435
RealType u01(UIntType u)
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:88
#define MCKL_DEFINE_RANDOM_TEST_OPERATOR(ResultType)
double operator()(RNGType &rng, U01DistributionType &u01)
Definition: serial_test.hpp:61
double operator()(RNGType &rng, U01DistributionType &u01)
Definition: mcmc.hpp:40
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
Tests based on the -distribution.
void runtime_assert(bool cond, const char *msg, bool soft=false)
Definition: assert.hpp:65