MCKL
Monte Carlo Kernel Library
maximum_of_t_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/maximum_of_t_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_MAXIMUM_OF_T_TEST_HPP
33 #define MCKL_RANDOM_MAXIMUM_OF_T_TEST_HPP
34 
37 
38 namespace mckl {
39 
45 template <std::size_t D, std::size_t T>
46 class MaximumOfTTest : public ChiSquaredTest<MaximumOfTTest<D, T>>
47 {
48  static_assert(D > 1, "**MaximumOfTTest** used with D less than two");
49 
50  static_assert(T > 0, "**MaximumOfTTest** used with T equal to zero");
51 
52  public:
53  MaximumOfTTest(std::size_t n) : n_(n) {}
54 
56 
57  template <typename RNGType, typename U01DistributionType>
58  double operator()(RNGType &rng, U01DistributionType &u01)
59  {
60  using result_type = typename U01DistributionType::result_type;
61 
62  Vector<double> count(D);
63  std::fill(count.begin(), count.end(), 0);
64 
65  const std::size_t k = internal::BufferSize<result_type>::value;
66  const std::size_t m = n_ / k;
67  const std::size_t l = n_ % k;
68  Vector<result_type> r(k * T);
70  for (std::size_t i = 0; i != m; ++i) {
71  generate(rng, u01, k, r.data(), s.data(), count.data());
72  }
73  generate(rng, u01, l, r.data(), s.data(), count.data());
74 
75  return this->stat(D, count.data(), static_cast<double>(n_) / D);
76  }
77 
78  double degree_of_freedom() const { return D - 1; }
79 
80  private:
81  std::size_t n_;
82 
83  template <typename RNGType, typename U01DistributionType>
84  void generate(RNGType &rng, U01DistributionType &u01, std::size_t n,
85  typename U01DistributionType::result_type *r,
86  typename U01DistributionType::result_type *s, double *count)
87  {
88  using result_type = typename U01DistributionType::result_type;
89 
90  rand(rng, u01, n * T, r);
91  for (std::size_t i = 0; i != n; ++i, r += T) {
92  s[i] = *std::max_element(r, r + T);
93  }
94  pow(n, s, static_cast<result_type>(T), s);
95  mul(n, static_cast<result_type>(D), s, s);
96  for (std::size_t i = 0; i != n; ++i) {
97  ++count[internal::ftoi<std::size_t, D>(s[i])];
98  }
99  }
100 }; // class MaximumOfTTest
101 
102 } // namespace mckl
103 
104 #endif // MCKL_RANDOM_MAXIMUM_OF_T_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 degree_of_freedom() const
double stat(std::size_t m, const double *count, const double *np) const
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: mcmc.hpp:40
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
Tests based on the maximum-of-t method.
Tests based on the -distribution.
MaximumOfTTest(std::size_t n)