MCKL
Monte Carlo Kernel Library
poisson_test.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/poisson_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_POISSON_TEST_HPP
33 #define MCKL_RANDOM_POISSON_TEST_HPP
34 
36 
37 namespace mckl {
38 
41 template <typename Derived>
43 {
44  public:
45  using result_type = std::size_t;
46 
47  bool pass(double alpha, result_type s) const
48  {
49  double l = cdf(s);
50  double r = s > 0 ? 1 - cdf(s - 1) : 1;
51  double p = 0;
52  if (r < l) {
53  p = r;
54  } else if (l < 0.5) {
55  p = 1 - l;
56  } else {
57  p = 0.5;
58  }
59 
60  return std::min(p, 1 - p) > 0.5 * alpha;
61  }
62 
63  double pdf(result_type s) const
64  {
65  double mean = static_cast<const Derived *>(this)->mean();
66  double lpdf = s * std::log(mean) - mean -
67  std::lgamma(static_cast<double>(s + 1));
68 
69  return std::exp(lpdf);
70  }
71 
72  double cdf(result_type s) const
73  {
74  double mean = static_cast<const Derived *>(this)->mean();
75 
76  return 1 - gammap(static_cast<double>(s + 1), mean);
77  }
78 }; // class PoissonTest
79 
80 } // namespace mckl
81 
82 #endif // MCKL_RANDOM_POISSON_TEST_HPP
void lgamma(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:303
bool pass(double alpha, result_type s) const
void log(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:226
double gammap(double a, double x)
Regularized lower incomplete Gamma function.
Definition: gamma.hpp:136
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
double pdf(result_type s) const
Tests based on the Poisson distribution.