MCKL
Monte Carlo Kernel Library
poker_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/poker_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_POKER_TEST_HPP
33 #define MCKL_RANDOM_POKER_TEST_HPP
34 
37 #include <bitset>
38 
39 namespace mckl {
40 
46 template <std::size_t D, std::size_t T>
47 class PokerTest : public ChiSquaredTest<PokerTest<D, T>>
48 {
49  static_assert(D > 1, "**PockerTest** used with D less than two");
50 
51  static_assert(T > 0, "**PockerTest** used with T equal to zero");
52 
53  public:
54  PokerTest(std::size_t n) : n_(n), tmin_(0), tmax_(0)
55  {
56  internal::StirlingMatrix2 stirling(T, K_);
57  double d = D;
58  double t = T;
59  double mult = n * std::pow(d, -t);
60  Vector<double> np_all(K_);
61  for (std::size_t k = 1; k <= K_; ++k, d -= 1) {
62  mult *= d;
63  np_all[k - 1] = mult * stirling(T, k);
64  }
65  internal::group_np(static_cast<double>(n), np_all, np_, tmin_, tmax_);
66  }
67 
69 
70  template <typename RNGType, typename U01DistributionType>
71  double operator()(RNGType &rng, U01DistributionType &u01)
72  {
73  using result_type = typename U01DistributionType::result_type;
74 
75  const std::size_t k = internal::BufferSize<result_type, T>::value;
76  const std::size_t m = n_ / k;
77  const std::size_t l = n_ % k;
78  Vector<result_type> r(k * T);
79  count_.resize(np_.size());
80  std::fill(count_.begin(), count_.end(), 0);
81  for (std::size_t i = 0; i != m; ++i) {
82  generate(rng, u01, k, r.data());
83  }
84  generate(rng, u01, l, r.data());
85 
86  return this->stat(np_.size(), count_.data(), np_.data());
87  }
88 
89  double degree_of_freedom() const
90  {
91  return static_cast<double>(np_.size() - 1);
92  }
93 
94  private:
95  static constexpr std::size_t K_ = D < T ? D : T;
96 
97  std::size_t n_;
98  std::size_t tmin_;
99  std::size_t tmax_;
100  Vector<double> np_;
101  Vector<double> count_;
102 
103  template <typename RNGType, typename U01DistributionType>
104  void generate(RNGType &rng, U01DistributionType &u01, std::size_t n,
105  typename U01DistributionType::result_type *r)
106  {
107  rand(rng, u01, n * T, r);
108  mul(n * T, static_cast<typename U01DistributionType::result_type>(D),
109  r, r);
110  for (std::size_t i = 0; i != n; ++i, r += T) {
111  std::size_t t = index(r);
112  if (t <= tmin_) {
113  count_.front() += 1;
114  } else if (t >= tmax_) {
115  count_.back() += 1;
116  } else {
117  count_[t - tmin_] += 1;
118  }
119  }
120  }
121 
122  template <typename ResultType>
123  std::size_t index(const ResultType *r) const
124  {
125  std::bitset<D> occurs;
126  set<0>(r, occurs, std::integral_constant<bool, 0 < T>());
127 
128  return occurs.count() - 1;
129  }
130 
131  template <std::size_t N, typename ResultType>
132  void set(const ResultType *, std::bitset<D> &, std::false_type) const
133  {
134  }
135 
136  template <std::size_t N, typename ResultType>
137  void set(const ResultType *r, std::bitset<D> &occurs, std::true_type) const
138  {
139  std::size_t u = internal::ftoi<std::size_t, D>(r[N]);
140  occurs.set(u);
141  set<N + 1>(r, occurs, std::integral_constant<bool, N + 1 < T>());
142  }
143 }; // class PokerTest
144 
145 } // namespace mckl
146 
147 #endif // MCKL_RANDOM_POKER_TEST_HPP
void pow(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:184
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:124
double stat(std::size_t m, const double *count, const double *np) const
void group_np(double n, const Vector< double > &np_all, Vector< double > &np, std::size_t &kmin, std::size_t &kmax)
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Definition: memory.hpp:435
double degree_of_freedom() const
Definition: poker_test.hpp:89
double operator()(RNGType &rng, U01DistributionType &u01)
Definition: poker_test.hpp:71
RealType u01(UIntType u)
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:88
PokerTest(std::size_t n)
Definition: poker_test.hpp:54
#define MCKL_DEFINE_RANDOM_TEST_OPERATOR(ResultType)
Pocker test (simplified version)
Definition: poker_test.hpp:47
Definition: mcmc.hpp:40
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
Tests based on the -distribution.