MCKL
Monte Carlo Kernel Library
gap_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/gap_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_GAP_TEST_HPP
33 #define MCKL_RANDOM_GAP_TEST_HPP
34 
37 
38 namespace mckl {
39 
44 template <std::size_t NTrialMax = std::numeric_limits<std::size_t>::max()>
46 {
47  public:
60  GapTest(std::size_t n, double alpha, double beta)
61  : n_(n), alpha_(alpha), beta_(beta)
62  {
64  alpha >= 0, "GapTest used with alpha smaller than zero");
65 
66  runtime_assert(beta <= 1, "GapTest used with beta larger than one");
67 
69  alpha < beta, "GapTest used with alpha not less than beta");
70 
71  double p = beta - alpha;
72  double q = 1 - p;
73  double a = p;
74  double b = q;
75  double c = 5.0 / n;
76  np_.push_back(a);
77  while (true) {
78  a *= q;
79  b *= q;
80  if (a < c || b < c) {
81  break;
82  }
83  np_.push_back(a);
84  }
85  mul(np_.size(), static_cast<double>(n), np_.data(), np_.data());
86  np_.push_back(n - std::accumulate(np_.begin(), np_.end(), 0.0));
87  }
88 
90 
91  template <typename RNGType, typename U01DistributionType>
92  double operator()(RNGType &rng, U01DistributionType &u01)
93  {
94  using result_type = typename U01DistributionType::result_type;
95 
96  const std::size_t t = np_.size() - 1;
97  count_.resize(np_.size());
98  std::fill(count_.begin(), count_.end(), 0);
99 
100  std::size_t s = 0;
101  while (s < n_) {
102  std::size_t r = 0;
103  while (true) {
104  if (r >= NTrialMax) {
105  return 0;
106  }
107  result_type u = u01(rng);
108  if (u >= alpha_ && u < beta_) {
109  count_[std::min(r, t)] += 1;
110  ++s;
111  break;
112  }
113  ++r;
114  }
115  }
116 
117  return this->stat(t + 1, count_.data(), np_.data());
118  }
119 
120  double degree_of_freedom() const
121  {
122  return static_cast<double>(np_.size() - 1);
123  }
124 
125  private:
126  std::size_t n_;
127  double alpha_;
128  double beta_;
129  Vector<double> np_;
130  Vector<double> count_;
131 }; // class GapTest
132 
133 } // namespace mckl
134 
135 #endif // MCKL_RANDOM_GAP_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
double degree_of_freedom() const
Definition: gap_test.hpp:120
RealType u01(UIntType u)
Convert uniform unsigned integers to floating points within [0, 1].
Definition: u01.hpp:88
Gap test.
Definition: gap_test.hpp:45
GapTest(std::size_t n, double alpha, double beta)
Construct a Gap test.
Definition: gap_test.hpp:60
double operator()(RNGType &rng, U01DistributionType &u01)
Definition: gap_test.hpp:92
#define MCKL_DEFINE_RANDOM_TEST_OPERATOR(ResultType)
Definition: mcmc.hpp:40
Tests based on the -distribution.
void runtime_assert(bool cond, const char *msg, bool soft=false)
Definition: assert.hpp:65