MCKL
Monte Carlo Kernel Library
chi_squared_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/chi_squared_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_CHI_SQUARED_TEST_HPP
33 #define MCKL_RANDOM_CHI_SQUARED_TEST_HPP
34 
36 
37 namespace mckl {
38 
41 template <typename Derived>
43 {
44  public:
45  using result_type = double;
46 
47  bool pass(double alpha, result_type s) const
48  {
49  double p = cdf(s);
50 
51  return std::min(p, 1 - p) > 0.5 * alpha;
52  }
53 
54  double pdf(result_type s) const
55  {
56  double k =
57  0.5 * static_cast<const Derived *>(this)->degree_of_freedom();
58  double lpdf = -k * const_ln_2<double>() - std::lgamma(k) +
59  (k - 1) * std::log(s) - 0.5 * s;
60 
61  return std::exp(lpdf);
62  }
63 
64  double cdf(result_type s) const
65  {
66  return gammap(
67  0.5 * static_cast<const Derived *>(this)->degree_of_freedom(),
68  0.5 * s);
69  }
70 
71  protected:
72  double stat(std::size_t m, const double *count, const double *np) const
73  {
74  return stat_dispatch(m, count, np);
75  }
76 
77  double stat(std::size_t m, const double *count, double np) const
78  {
79  return stat_dispatch(m, count, np);
80  }
81 
82  private:
83  template <typename NPType>
84  double stat_dispatch(std::size_t m, const double *count, NPType np) const
85  {
86  Vector<double> tmp(m);
87  sub(m, count, np, tmp.data());
88  sqr(m, tmp.data(), tmp.data());
89  div(m, tmp.data(), np, tmp.data());
90 
91  return std::accumulate(tmp.begin(), tmp.end(), 0.0);
92  }
93 }; // class ChiSquaredTest
94 
95 } // namespace mckl
96 
97 #endif // MCKL_RANDOM_CHI_SQUARED_TEST_HPP
double stat(std::size_t m, const double *count, const double *np) const
void lgamma(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:303
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
double pdf(result_type s) const
double gammap(double a, double x)
Regularized lower incomplete Gamma function.
Definition: gamma.hpp:136
void sub(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:121
bool pass(double alpha, result_type s) const
double stat(std::size_t m, const double *count, double np) const
void sqr(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:123
double cdf(result_type s) const
Definition: mcmc.hpp:40
void exp(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:221
Tests based on the -distribution.
void div(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:175