MCKL
Monte Carlo Kernel Library
birthday_spacings_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/birthday_spacings_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_BIRTHDAY_SPACINGS_TEST_HPP
33 #define MCKL_RANDOM_BIRTHDAY_SPACINGS_TEST_HPP
34 
37 
38 namespace mckl {
39 
45 template <std::size_t D, std::size_t T>
46 class BirthdaySpacingsTest : public PoissonTest<BirthdaySpacingsTest<D, T>>
47 {
48  static_assert(D > 1, "**BirthdaySpacingsTest** used with D less than two");
49 
50  static_assert(T > 0, "**BirthdaySpacingsTest** used with T equal to zero");
51 
52  public:
53  BirthdaySpacingsTest(std::size_t n) : n_(n)
54  {
55  mean_ = std::exp(3 * std::log(static_cast<double>(n)) -
56  std::log(static_cast<double>(K_)) - 2 * const_ln_2<double>());
57  }
58 
60 
61  template <typename RNGType, typename U01DistributionType>
62  std::size_t operator()(RNGType &rng, U01DistributionType &u01)
63  {
64  using result_type = typename U01DistributionType::result_type;
65 
66  const std::size_t k = internal::BufferSize<result_type, T>::value;
67  const std::size_t m = n_ / k;
68  const std::size_t l = n_ % k;
69  Vector<result_type> r(k * T);
70  Vector<std::size_t> spacings(n_);
71  std::size_t *s = spacings.data();
72  for (std::size_t i = 0; i != m; ++i, s += k) {
73  generate(rng, u01, k, r.data(), s);
74  }
75  generate(rng, u01, l, r.data(), s);
76 
77  std::size_t d0 = spacings.front();
78  std::sort(spacings.begin(), spacings.end());
79  for (std::size_t i = 0; i != n_ - 1; ++i) {
80  spacings[i] = spacings[i + 1] - spacings[i];
81  }
82  spacings.back() = K_ - spacings.back() + d0;
83  std::sort(spacings.begin(), spacings.end());
84  std::size_t e = 0;
85  for (std::size_t i = 0; i != n_ - 1; ++i) {
86  if (spacings[i] == spacings[i + 1]) {
87  ++e;
88  }
89  }
90 
91  return e;
92  }
93 
94  double mean() const { return mean_; }
95 
96  private:
97  static constexpr std::size_t K_ = internal::Pow<std::size_t, D, T>::value;
98 
99  std::size_t n_;
100  double mean_;
101 
102  template <typename RNGType, typename U01DistributionType>
103  void generate(RNGType &rng, U01DistributionType &u01, std::size_t n,
104  typename U01DistributionType::result_type *r, std::size_t *s)
105  {
106  rand(rng, u01, n * T, r);
107  mul(n * T, static_cast<typename U01DistributionType::result_type>(D),
108  r, r);
109  for (std::size_t i = 0; i != n; ++i, r += T) {
110  s[i] = internal::serial_index<D, T>(r);
111  }
112  }
113 }; // class BirthdaySpacingsTest
114 
115 } // namespace mckl
116 
117 #endif // MCKL_RANDOM_BIRTHDAY_SPACINGS_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
void log(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:226
constexpr double const_ln_2< double >() noexcept
Definition: constants.hpp:296
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)
Definition: mcmc.hpp:40
void exp(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:221
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
Tests based on the Poisson distribution.
std::size_t operator()(RNGType &rng, U01DistributionType &u01)