MCKL
Monte Carlo Kernel Library
permutation_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/permutation_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_PERMUTATION_TEST_HPP
33 #define MCKL_RANDOM_PERMUTATION_TEST_HPP
34 
37 
38 namespace mckl {
39 
44 template <std::size_t T>
45 class PermutationTest : public ChiSquaredTest<PermutationTest<T>>
46 {
47  static_assert(T > 0, "**PermutationTest** used with T equal to zero");
48 
49  static_assert(T <= 20, "**PermutationTest** used with T larger than 20");
50 
51  public:
55  PermutationTest(std::size_t n) : n_(n), np_(static_cast<double>(n) / M_) {}
56 
58 
59  template <typename RNGType, typename DistributionType>
60  double operator()(RNGType &rng, DistributionType &distribution)
61  {
62  std::array<typename DistributionType::result_type, T> r;
63  Vector<double> count(M_);
64  std::fill(count.begin(), count.end(), 0);
65  for (std::size_t i = 0; i != n_; ++i) {
66  rand(rng, distribution, T, r.data());
67  count[internal::permutation_index<T>(r.data())] += 1;
68  }
69 
70  return this->stat(M_, count.data(), np_);
71  }
72 
73  double degree_of_freedom() const { return M_ - 1; }
74 
75  private:
76  static constexpr std::size_t M_ =
78 
79  std::size_t n_;
80  double np_;
81 }; // class PermutationTest
82 
83 } // namespace mckl
84 
85 #endif // MCKL_RANDOM_PERMUTATION_TEST_HPP
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
#define MCKL_DEFINE_RANDOM_TEST_OPERATOR(ResultType)
double degree_of_freedom() const
Permutation test.
Definition: mcmc.hpp:40
void rand(RNGType &rng, ArcsineDistribution< RealType > &distribution, std::size_t N, RealType *r)
double operator()(RNGType &rng, DistributionType &distribution)
Tests based on the -distribution.
PermutationTest(std::size_t n)
Construct a Permutation test.