mirror of
https://github.com/boostorg/math.git
synced 2026-01-26 06:42:12 +00:00
Commit of more or less complete non-central chi-square distribution: docs still to come.
[SVN r42820]
This commit is contained in:
@@ -96,6 +96,26 @@ inline bool check_x(
|
||||
// leaving this test to catch any NaNs. see Normal and cauchy for example.
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
inline bool check_positive_x(
|
||||
const char* function,
|
||||
RealType x,
|
||||
RealType* result,
|
||||
const Policy& pol)
|
||||
{
|
||||
if(!(boost::math::isfinite)(x) || (x < 0))
|
||||
{
|
||||
*result = policies::raise_domain_error<RealType>(
|
||||
function,
|
||||
"Random variate x is %1%, but must be finite and >= 0!", x, pol);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
// Note that this test catches both infinity and NaN.
|
||||
// Some special cases permit x to be infinite, so these must be tested 1st,
|
||||
// leaving this test to catch any NaNs. see Normal and cauchy for example.
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
inline bool check_non_centrality(
|
||||
const char* function,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
|
||||
#include <boost/math/special_functions/fpclassify.hpp> // isnan.
|
||||
#include <boost/math/tools/roots.hpp> // for root finding.
|
||||
#include <boost/math/tools/minima.hpp> // function minimization for mode
|
||||
|
||||
namespace boost
|
||||
{
|
||||
@@ -394,6 +395,231 @@ namespace boost
|
||||
function);
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
RealType nccs_pdf(const non_central_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
|
||||
{
|
||||
static const char* function = "pdf(non_central_chi_squared_distribution<%1%>, %1%)";
|
||||
typedef typename policies::evaluation<RealType, Policy>::type value_type;
|
||||
typedef typename policies::normalise<
|
||||
Policy,
|
||||
policies::promote_float<false>,
|
||||
policies::promote_double<false>,
|
||||
policies::discrete_quantile<>,
|
||||
policies::assert_undefined<> >::type forwarding_policy;
|
||||
|
||||
value_type k = dist.degrees_of_freedom();
|
||||
value_type l = dist.non_centrality();
|
||||
value_type r;
|
||||
if(!detail::check_df(
|
||||
function,
|
||||
k, &r, Policy())
|
||||
||
|
||||
!detail::check_non_centrality(
|
||||
function,
|
||||
l,
|
||||
&r,
|
||||
Policy())
|
||||
||
|
||||
!detail::check_positive_x(
|
||||
function,
|
||||
(value_type)x,
|
||||
&r,
|
||||
Policy()))
|
||||
return (RealType)r;
|
||||
|
||||
if(l == 0)
|
||||
return pdf(boost::math::chi_squared_distribution<RealType, forwarding_policy>(dist.degrees_of_freedom()), x);
|
||||
|
||||
r = log(x / l) * (k / 4 - 0.5f) - (x + l) / 2;
|
||||
if(r >= tools::log_max_value<RealType>())
|
||||
return policies::raise_overflow_error<RealType>(function, 0, forwarding_policy());
|
||||
if(r <= -tools::log_max_value<RealType>())
|
||||
return policies::raise_underflow_error<RealType>(function, 0, forwarding_policy());
|
||||
|
||||
r = exp(r);
|
||||
r = 0.5f * r
|
||||
* boost::math::cyl_bessel_i(k/2 - 1, sqrt(l * x), forwarding_policy());
|
||||
return policies::checked_narrowing_cast<RealType, forwarding_policy>(
|
||||
r,
|
||||
function);
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
struct pdf_minimizer
|
||||
{
|
||||
pdf_minimizer(const non_central_chi_squared_distribution<RealType, Policy>& d)
|
||||
: dist(d) {}
|
||||
|
||||
RealType operator()(const RealType& x)
|
||||
{
|
||||
return -pdf(dist, x);
|
||||
}
|
||||
private:
|
||||
non_central_chi_squared_distribution<RealType, Policy> dist;
|
||||
};
|
||||
|
||||
template <class RealType, class Policy>
|
||||
RealType nccs_mode(const non_central_chi_squared_distribution<RealType, Policy>& dist)
|
||||
{
|
||||
static const char* function = "mode(non_central_chi_squared_distribution<%1%> const&)";
|
||||
|
||||
RealType k = dist.degrees_of_freedom();
|
||||
RealType l = dist.non_centrality();
|
||||
RealType r;
|
||||
if(!detail::check_df(
|
||||
function,
|
||||
k, &r, Policy())
|
||||
||
|
||||
!detail::check_non_centrality(
|
||||
function,
|
||||
l,
|
||||
&r,
|
||||
Policy()))
|
||||
return (RealType)r;
|
||||
//
|
||||
// Need to begin by bracketing the maxima of the PDF:
|
||||
//
|
||||
RealType maxval;
|
||||
RealType upper_bound = l + k;
|
||||
RealType lower_bound;
|
||||
RealType v = pdf(dist, l + k);
|
||||
do
|
||||
{
|
||||
maxval = v;
|
||||
upper_bound *= 2;
|
||||
v = pdf(dist, upper_bound);
|
||||
}while(maxval < v);
|
||||
|
||||
lower_bound = upper_bound;
|
||||
do
|
||||
{
|
||||
maxval = v;
|
||||
lower_bound /= 2;
|
||||
v = pdf(dist, lower_bound);
|
||||
}while(maxval < v);
|
||||
|
||||
boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
|
||||
|
||||
return tools::brent_find_minima(
|
||||
pdf_minimizer<RealType, Policy>(dist),
|
||||
lower_bound,
|
||||
upper_bound,
|
||||
policies::digits<RealType, Policy>(),
|
||||
max_iter).first;
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
struct degrees_of_freedom_finder
|
||||
{
|
||||
degrees_of_freedom_finder(
|
||||
RealType lam_, RealType x_, RealType p_, bool c)
|
||||
: lam(lam_), x(x_), p(p_), comp(c) {}
|
||||
|
||||
RealType operator()(const RealType& v)
|
||||
{
|
||||
non_central_chi_squared_distribution<RealType, Policy> d(v, lam);
|
||||
return comp ?
|
||||
p - cdf(complement(d, x))
|
||||
: cdf(d, x) - p;
|
||||
}
|
||||
private:
|
||||
RealType lam;
|
||||
RealType x;
|
||||
RealType p;
|
||||
bool comp;
|
||||
};
|
||||
|
||||
template <class RealType, class Policy>
|
||||
inline RealType find_degrees_of_freedom(
|
||||
RealType lam, RealType x, RealType p, RealType q, const Policy& pol)
|
||||
{
|
||||
const char* function = "non_central_chi_squared<%1%>::find_degrees_of_freedom";
|
||||
if((p == 0) || (q == 0))
|
||||
{
|
||||
//
|
||||
// Can't a thing if one of p and q is zero:
|
||||
//
|
||||
return policies::raise_evaluation_error<RealType>(function,
|
||||
"Can't find degrees of freedom when the probability is 0 or 1, only possible answer is %1%",
|
||||
RealType(std::numeric_limits<RealType>::quiet_NaN()), Policy());
|
||||
}
|
||||
degrees_of_freedom_finder<RealType, Policy> f(lam, x, p < q ? p : q, p < q ? false : true);
|
||||
tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
|
||||
boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
|
||||
//
|
||||
// Pick an initial guess that we know will give us a probability
|
||||
// right around 0.5.
|
||||
//
|
||||
RealType guess = x - lam;
|
||||
if(guess < 1)
|
||||
guess = 1;
|
||||
std::pair<RealType, RealType> ir = tools::bracket_and_solve_root(
|
||||
f, guess, RealType(2), false, tol, max_iter, pol);
|
||||
RealType result = ir.first + (ir.second - ir.first) / 2;
|
||||
if(max_iter == policies::get_max_root_iterations<Policy>())
|
||||
{
|
||||
policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
|
||||
" or there is no answer to problem. Current best guess is %1%", result, Policy());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
struct non_centrality_finder
|
||||
{
|
||||
non_centrality_finder(
|
||||
RealType v_, RealType x_, RealType p_, bool c)
|
||||
: v(v_), x(x_), p(p_), comp(c) {}
|
||||
|
||||
RealType operator()(const RealType& lam)
|
||||
{
|
||||
non_central_chi_squared_distribution<RealType, Policy> d(v, lam);
|
||||
return comp ?
|
||||
p - cdf(complement(d, x))
|
||||
: cdf(d, x) - p;
|
||||
}
|
||||
private:
|
||||
RealType v;
|
||||
RealType x;
|
||||
RealType p;
|
||||
bool comp;
|
||||
};
|
||||
|
||||
template <class RealType, class Policy>
|
||||
inline RealType find_non_centrality(
|
||||
RealType v, RealType x, RealType p, RealType q, const Policy& pol)
|
||||
{
|
||||
const char* function = "non_central_chi_squared<%1%>::find_non_centrality";
|
||||
if((p == 0) || (q == 0))
|
||||
{
|
||||
//
|
||||
// Can't do a thing if one of p and q is zero:
|
||||
//
|
||||
return policies::raise_evaluation_error<RealType>(function,
|
||||
"Can't find degrees of freedom when the probability is 0 or 1, only possible answer is %1%",
|
||||
RealType(std::numeric_limits<RealType>::quiet_NaN()), Policy());
|
||||
}
|
||||
non_centrality_finder<RealType, Policy> f(v, x, p < q ? p : q, p < q ? false : true);
|
||||
tools::eps_tolerance<RealType> tol(policies::digits<RealType, Policy>());
|
||||
boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
|
||||
//
|
||||
// Pick an initial guess that we know will give us a probability
|
||||
// right around 0.5.
|
||||
//
|
||||
RealType guess = x - v;
|
||||
if(guess < 1)
|
||||
guess = 1;
|
||||
std::pair<RealType, RealType> ir = tools::bracket_and_solve_root(
|
||||
f, guess, RealType(2), false, tol, max_iter, pol);
|
||||
RealType result = ir.first + (ir.second - ir.first) / 2;
|
||||
if(max_iter == policies::get_max_root_iterations<Policy>())
|
||||
{
|
||||
policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
|
||||
" or there is no answer to problem. Current best guess is %1%", result, Policy());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template <class RealType = double, class Policy = policies::policy<> >
|
||||
@@ -425,6 +651,88 @@ namespace boost
|
||||
{ // Private data getter function.
|
||||
return ncp;
|
||||
}
|
||||
static RealType find_degrees_of_freedom(RealType lam, RealType x, RealType p)
|
||||
{
|
||||
const char* function = "non_central_chi_squared<%1%>::find_degrees_of_freedom";
|
||||
typedef typename policies::evaluation<RealType, Policy>::type value_type;
|
||||
typedef typename policies::normalise<
|
||||
Policy,
|
||||
policies::promote_float<false>,
|
||||
policies::promote_double<false>,
|
||||
policies::discrete_quantile<>,
|
||||
policies::assert_undefined<> >::type forwarding_policy;
|
||||
value_type result = detail::find_degrees_of_freedom(
|
||||
static_cast<value_type>(lam),
|
||||
static_cast<value_type>(x),
|
||||
static_cast<value_type>(p),
|
||||
static_cast<value_type>(1-p),
|
||||
forwarding_policy());
|
||||
return policies::checked_narrowing_cast<RealType, forwarding_policy>(
|
||||
result,
|
||||
function);
|
||||
}
|
||||
template <class A, class B, class C>
|
||||
static RealType find_degrees_of_freedom(const complemented3_type<A,B,C>& c)
|
||||
{
|
||||
const char* function = "non_central_chi_squared<%1%>::find_degrees_of_freedom";
|
||||
typedef typename policies::evaluation<RealType, Policy>::type value_type;
|
||||
typedef typename policies::normalise<
|
||||
Policy,
|
||||
policies::promote_float<false>,
|
||||
policies::promote_double<false>,
|
||||
policies::discrete_quantile<>,
|
||||
policies::assert_undefined<> >::type forwarding_policy;
|
||||
value_type result = detail::find_degrees_of_freedom(
|
||||
static_cast<value_type>(c.dist),
|
||||
static_cast<value_type>(c.param1),
|
||||
static_cast<value_type>(1-c.param2),
|
||||
static_cast<value_type>(c.param2),
|
||||
forwarding_policy());
|
||||
return policies::checked_narrowing_cast<RealType, forwarding_policy>(
|
||||
result,
|
||||
function);
|
||||
}
|
||||
static RealType find_non_centrality(RealType v, RealType x, RealType p)
|
||||
{
|
||||
const char* function = "non_central_chi_squared<%1%>::find_non_centrality";
|
||||
typedef typename policies::evaluation<RealType, Policy>::type value_type;
|
||||
typedef typename policies::normalise<
|
||||
Policy,
|
||||
policies::promote_float<false>,
|
||||
policies::promote_double<false>,
|
||||
policies::discrete_quantile<>,
|
||||
policies::assert_undefined<> >::type forwarding_policy;
|
||||
value_type result = detail::find_non_centrality(
|
||||
static_cast<value_type>(v),
|
||||
static_cast<value_type>(x),
|
||||
static_cast<value_type>(p),
|
||||
static_cast<value_type>(1-p),
|
||||
forwarding_policy());
|
||||
return policies::checked_narrowing_cast<RealType, forwarding_policy>(
|
||||
result,
|
||||
function);
|
||||
}
|
||||
template <class A, class B, class C>
|
||||
static RealType find_non_centrality(const complemented3_type<A,B,C>& c)
|
||||
{
|
||||
const char* function = "non_central_chi_squared<%1%>::find_non_centrality";
|
||||
typedef typename policies::evaluation<RealType, Policy>::type value_type;
|
||||
typedef typename policies::normalise<
|
||||
Policy,
|
||||
policies::promote_float<false>,
|
||||
policies::promote_double<false>,
|
||||
policies::discrete_quantile<>,
|
||||
policies::assert_undefined<> >::type forwarding_policy;
|
||||
value_type result = detail::find_non_centrality(
|
||||
static_cast<value_type>(c.dist),
|
||||
static_cast<value_type>(c.param1),
|
||||
static_cast<value_type>(1-c.param2),
|
||||
static_cast<value_type>(c.param2),
|
||||
forwarding_policy());
|
||||
return policies::checked_narrowing_cast<RealType, forwarding_policy>(
|
||||
result,
|
||||
function);
|
||||
}
|
||||
private:
|
||||
// Data member, initialized by constructor.
|
||||
RealType df; // degrees of freedom.
|
||||
@@ -473,8 +781,7 @@ namespace boost
|
||||
template <class RealType, class Policy>
|
||||
inline RealType mode(const non_central_chi_squared_distribution<RealType, Policy>& dist)
|
||||
{ // mode.
|
||||
// TODO!
|
||||
return 0;
|
||||
return detail::nccs_mode(dist);
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
@@ -548,30 +855,9 @@ namespace boost
|
||||
}
|
||||
|
||||
template <class RealType, class Policy>
|
||||
RealType pdf(const non_central_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
|
||||
inline RealType pdf(const non_central_chi_squared_distribution<RealType, Policy>& dist, const RealType& x)
|
||||
{ // Probability Density/Mass Function.
|
||||
const char* function = "boost::math::non_central_chi_squared_distribution<%1%>::pdf(%1%)";
|
||||
RealType k = dist.degrees_of_freedom();
|
||||
RealType l = dist.non_centrality();
|
||||
RealType r;
|
||||
if(!detail::check_df(
|
||||
function,
|
||||
k, &r, Policy())
|
||||
||
|
||||
!detail::check_non_centrality(
|
||||
function,
|
||||
l,
|
||||
&r,
|
||||
Policy()))
|
||||
return r;
|
||||
|
||||
if(l == 0)
|
||||
return pdf(boost::math::chi_squared_distribution<RealType, Policy>(k), x);
|
||||
|
||||
r = -(x + l) / 2 + log(x / l) * (k / 4 - 0.5f);
|
||||
|
||||
return 0.5f * exp(r)
|
||||
* boost::math::cyl_bessel_i(k/2 - 1, sqrt(l * x), Policy());
|
||||
return detail::nccs_pdf(dist, x);
|
||||
} // pdf
|
||||
|
||||
template <class RealType, class Policy>
|
||||
@@ -589,6 +875,12 @@ namespace boost
|
||||
function,
|
||||
l,
|
||||
&r,
|
||||
Policy())
|
||||
||
|
||||
!detail::check_positive_x(
|
||||
function,
|
||||
x,
|
||||
&r,
|
||||
Policy()))
|
||||
return r;
|
||||
|
||||
@@ -612,6 +904,12 @@ namespace boost
|
||||
function,
|
||||
l,
|
||||
&r,
|
||||
Policy())
|
||||
||
|
||||
!detail::check_positive_x(
|
||||
function,
|
||||
x,
|
||||
&r,
|
||||
Policy()))
|
||||
return r;
|
||||
|
||||
|
||||
@@ -247,6 +247,30 @@ run test_negative_binomial.cpp
|
||||
: # requirements
|
||||
<define>TEST_REAL_CONCEPT
|
||||
: test_negative_binomial_real_concept ;
|
||||
run test_nc_chi_squared.cpp
|
||||
: # command line
|
||||
: # input files
|
||||
: # requirements
|
||||
<define>TEST_FLOAT
|
||||
: test_nc_chi_squared_float ;
|
||||
run test_nc_chi_squared.cpp
|
||||
: # command line
|
||||
: # input files
|
||||
: # requirements
|
||||
<define>TEST_DOUBLE
|
||||
: test_nc_chi_squared_double ;
|
||||
run test_nc_chi_squared.cpp
|
||||
: # command line
|
||||
: # input files
|
||||
: # requirements
|
||||
<define>TEST_LDOUBLE
|
||||
: test_nc_chi_squared_long_double ;
|
||||
run test_nc_chi_squared.cpp
|
||||
: # command line
|
||||
: # input files
|
||||
: # requirements
|
||||
<define>TEST_REAL_CONCEPT
|
||||
: test_nc_chi_squared_real_concept ;
|
||||
run test_normal.cpp ;
|
||||
run test_pareto.cpp ;
|
||||
run test_poisson.cpp
|
||||
|
||||
8858
test/nccs.ipp
8858
test/nccs.ipp
File diff suppressed because it is too large
Load Diff
@@ -7,8 +7,20 @@
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (disable:4127 4512)
|
||||
#endif
|
||||
|
||||
#if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
|
||||
# define TEST_FLOAT
|
||||
# define TEST_DOUBLE
|
||||
# define TEST_LDOUBLE
|
||||
# define TEST_REAL_CONCEPT
|
||||
#endif
|
||||
|
||||
#include <boost/math/concepts/real_concept.hpp> // for real_concept
|
||||
#include <boost/math/distributions/non_central_chi_squared.hpp> // for chi_squared_distribution
|
||||
#include <boost/math/special_functions/cbrt.hpp> // for chi_squared_distribution
|
||||
#include <boost/test/included/test_exec_monitor.hpp> // for test_main
|
||||
#include <boost/test/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
|
||||
|
||||
@@ -31,21 +43,87 @@ using std::numeric_limits;
|
||||
std::cerr << "Failure was at row " << i << std::endl;\
|
||||
std::cerr << std::setprecision(35); \
|
||||
std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
|
||||
std::cerr << " , " << data[i][3] << " , " << data[i][4] << " , " << data[i][5] << " } " << std::endl;\
|
||||
std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
|
||||
}\
|
||||
}
|
||||
|
||||
#define BOOST_CHECK_EX(a, i) \
|
||||
{\
|
||||
unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
|
||||
BOOST_CHECK(a); \
|
||||
if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
|
||||
{\
|
||||
std::cerr << "Failure was at row " << i << std::endl;\
|
||||
std::cerr << std::setprecision(35); \
|
||||
std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
|
||||
std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
|
||||
}\
|
||||
}
|
||||
|
||||
void expected_results()
|
||||
{
|
||||
//
|
||||
// Define the max and mean errors expected for
|
||||
// various compilers and platforms.
|
||||
//
|
||||
const char* largest_type;
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
|
||||
{
|
||||
largest_type = "(long\\s+)?double|real_concept";
|
||||
}
|
||||
else
|
||||
{
|
||||
largest_type = "long double|real_concept";
|
||||
}
|
||||
#else
|
||||
largest_type = "(long\\s+)?double|real_concept";
|
||||
#endif
|
||||
|
||||
//
|
||||
// Catch all cases come last:
|
||||
//
|
||||
add_expected_result(
|
||||
"[^|]*", // compiler
|
||||
"[^|]*", // stdlib
|
||||
"[^|]*", // platform
|
||||
largest_type, // test type(s)
|
||||
"[^|]*medium[^|]*", // test data group
|
||||
"[^|]*", 350, 100); // test function
|
||||
add_expected_result(
|
||||
"[^|]*", // compiler
|
||||
"[^|]*", // stdlib
|
||||
"[^|]*", // platform
|
||||
largest_type, // test type(s)
|
||||
"[^|]*large[^|]*", // test data group
|
||||
"[^|]*", 10000, 3000); // test function
|
||||
|
||||
//
|
||||
// Finish off by printing out the compiler/stdlib/platform names,
|
||||
// we do this to make it easier to mark up expected error rates.
|
||||
//
|
||||
std::cout << "Tests run with " << BOOST_COMPILER << ", "
|
||||
<< BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
|
||||
}
|
||||
|
||||
template <class RealType>
|
||||
RealType naive_pdf(RealType v, RealType lam, RealType x)
|
||||
{
|
||||
// Formula direct from
|
||||
// http://mathworld.wolfram.com/NoncentralChi-SquaredDistribution.html
|
||||
// with no simplification:
|
||||
RealType r = -(x+lam)/2 + log(x) * (v-1)/2 + log(lam) / 2;
|
||||
r -= log(lam * x) * v/4;
|
||||
r = exp(r) / 2;
|
||||
r *= boost::math::cyl_bessel_i(v/2 - 1, sqrt(lam * x));
|
||||
return r;
|
||||
RealType sum, term, prefix(1);
|
||||
RealType eps = boost::math::tools::epsilon<RealType>();
|
||||
term = sum = pdf(boost::math::chi_squared_distribution<RealType>(v), x);
|
||||
for(int i = 1;; ++i)
|
||||
{
|
||||
prefix *= lam / (2 * i);
|
||||
term = prefix * pdf(boost::math::chi_squared_distribution<RealType>(v + 2 * i), x);
|
||||
sum += term;
|
||||
if(term / sum < eps)
|
||||
break;
|
||||
}
|
||||
return sum * exp(-lam/2);
|
||||
}
|
||||
|
||||
template <class RealType>
|
||||
@@ -60,20 +138,32 @@ void test_spot(
|
||||
boost::math::non_central_chi_squared_distribution<RealType> dist(df, ncp);
|
||||
BOOST_CHECK_CLOSE(
|
||||
cdf(dist, cs), P, tol);
|
||||
BOOST_CHECK_CLOSE(
|
||||
pdf(dist, cs), naive_pdf(dist.degrees_of_freedom(), ncp, cs), tol);
|
||||
try{
|
||||
BOOST_CHECK_CLOSE(
|
||||
pdf(dist, cs), naive_pdf(dist.degrees_of_freedom(), ncp, cs), tol * 50);
|
||||
}
|
||||
catch(const std::overflow_error&)
|
||||
{}
|
||||
if((P < 0.99) && (Q < 0.99))
|
||||
{
|
||||
//
|
||||
// We can only check this if P is not too close to 1,
|
||||
// so that we can guarentee Q is free of error:
|
||||
// so that we can guarentee Q is reasonably free of error:
|
||||
//
|
||||
BOOST_CHECK_CLOSE(
|
||||
cdf(complement(dist, cs)), Q, tol);
|
||||
BOOST_CHECK_CLOSE(
|
||||
quantile(dist, P), cs, tol);
|
||||
quantile(dist, P), cs, tol * 10);
|
||||
BOOST_CHECK_CLOSE(
|
||||
quantile(complement(dist, Q)), cs, tol);
|
||||
quantile(complement(dist, Q)), cs, tol * 10);
|
||||
BOOST_CHECK_CLOSE(
|
||||
dist.find_degrees_of_freedom(ncp, cs, P), df, tol * 10);
|
||||
BOOST_CHECK_CLOSE(
|
||||
dist.find_degrees_of_freedom(boost::math::complement(ncp, cs, Q)), df, tol * 10);
|
||||
BOOST_CHECK_CLOSE(
|
||||
dist.find_non_centrality(df, cs, P), ncp, tol * 10);
|
||||
BOOST_CHECK_CLOSE(
|
||||
dist.find_non_centrality(boost::math::complement(df, cs, Q)), ncp, tol * 10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +180,8 @@ void test_spots(RealType)
|
||||
//
|
||||
if(boost::math::tools::digits<RealType>() < 50)
|
||||
tolerance *= 50;
|
||||
if(boost::is_floating_point<RealType>::value != 1)
|
||||
tolerance *= 20; // real_concept special functions are less accurate
|
||||
|
||||
cout << "Tolerance = " << tolerance << "%." << endl;
|
||||
|
||||
@@ -140,7 +232,7 @@ void test_spots(RealType)
|
||||
static_cast<RealType>(38.56038), // Chi Squared statistic
|
||||
static_cast<RealType>(0.8519497361859118e-1), // Probability of result (CDF), P
|
||||
static_cast<RealType>(1-0.8519497361859118e-1), // Q = 1 - P
|
||||
tolerance);
|
||||
tolerance * 2);
|
||||
test_spot(
|
||||
static_cast<RealType>(100), // degrees of freedom
|
||||
static_cast<RealType>(16), // non centrality
|
||||
@@ -205,16 +297,14 @@ void test_spots(RealType)
|
||||
BOOST_CHECK_CLOSE(
|
||||
coefficient_of_variation(dist)
|
||||
, standard_deviation(dist) / mean(dist), tol2);
|
||||
#if 0
|
||||
// mode:
|
||||
BOOST_CHECK_CLOSE(
|
||||
mode(dist)
|
||||
, static_cast<RealType>(6), tol2);
|
||||
#endif
|
||||
, static_cast<RealType>(17.184201151792944), sqrt(tolerance));
|
||||
BOOST_CHECK_CLOSE(
|
||||
median(dist),
|
||||
quantile(
|
||||
non_central_chi_squared_distribution<RealType>(
|
||||
boost::math::non_central_chi_squared_distribution<RealType>(
|
||||
static_cast<RealType>(8),
|
||||
static_cast<RealType>(12)),
|
||||
static_cast<RealType>(0.5)), static_cast<RealType>(tol2));
|
||||
@@ -290,6 +380,13 @@ void quantile_sanity_check(T& data, const char* type_name, const char* test)
|
||||
typedef typename T::value_type row_type;
|
||||
typedef typename row_type::value_type value_type;
|
||||
|
||||
//
|
||||
// Tests with type real_concept take rather too long to run, so
|
||||
// for now we'll disable them:
|
||||
//
|
||||
if(!boost::is_floating_point<value_type>::value)
|
||||
return;
|
||||
|
||||
std::cout << "Testing: " << type_name << " quantile sanity check, with tests " << test << std::endl;
|
||||
|
||||
//
|
||||
@@ -329,17 +426,65 @@ void quantile_sanity_check(T& data, const char* type_name, const char* test)
|
||||
value_type pt = data[i][2];
|
||||
BOOST_CHECK_CLOSE_EX(pt, p, precision, i);
|
||||
}
|
||||
//
|
||||
// Sanity check mode as well, note this may well overflow
|
||||
// since we don't know how to compute PDF's (and hence the mode)
|
||||
// for large values of the parameters, in addition accuracy of
|
||||
// the mode is at *best* the square root of the accuracy of the PDF:
|
||||
//
|
||||
try
|
||||
{
|
||||
value_type m = mode(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]));
|
||||
value_type p = pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m);
|
||||
BOOST_CHECK_EX(pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m * (1 + sqrt(precision))) <= p, i);
|
||||
BOOST_CHECK_EX(pdf(boost::math::non_central_chi_squared_distribution<value_type>(data[i][0], data[i][1]), m * (1 - sqrt(precision))) <= p, i);
|
||||
}
|
||||
catch(const std::overflow_error&)
|
||||
{
|
||||
}
|
||||
//
|
||||
// Sanity check degrees-of-freedom finder, don't bother at float
|
||||
// precision though as there's not enough data in the probability
|
||||
// values to get back to the correct degrees of freedom or
|
||||
// non-cenrality parameter:
|
||||
//
|
||||
if(boost::math::tools::digits<value_type>() > 50)
|
||||
{
|
||||
try{
|
||||
if((data[i][3] < 0.99) && (data[i][3] != 0))
|
||||
{
|
||||
BOOST_CHECK_CLOSE_EX(
|
||||
boost::math::non_central_chi_squared_distribution<value_type>::find_degrees_of_freedom(data[i][1], data[i][2], data[i][3]),
|
||||
data[i][0], precision, i);
|
||||
BOOST_CHECK_CLOSE_EX(
|
||||
boost::math::non_central_chi_squared_distribution<value_type>::find_non_centrality(data[i][0], data[i][2], data[i][3]),
|
||||
data[i][1], precision, i);
|
||||
}
|
||||
if((data[i][4] < 0.99) && (data[i][4] != 0))
|
||||
{
|
||||
BOOST_CHECK_CLOSE_EX(
|
||||
boost::math::non_central_chi_squared_distribution<value_type>::find_degrees_of_freedom(boost::math::complement(data[i][1], data[i][2], data[i][4])),
|
||||
data[i][0], precision, i);
|
||||
BOOST_CHECK_CLOSE_EX(
|
||||
boost::math::non_central_chi_squared_distribution<value_type>::find_non_centrality(boost::math::complement(data[i][0], data[i][2], data[i][4])),
|
||||
data[i][1], precision, i);
|
||||
}
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
BOOST_ERROR(e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void test_accuracy(T, const char* type_name)
|
||||
{
|
||||
#if 0
|
||||
#include "nccs.ipp"
|
||||
do_test_nc_chi_squared(nccs, type_name, "Non Central Chi Squared, medium parameters");
|
||||
quantile_sanity_check(nccs, type_name, "Non Central Chi Squared, medium parameters");
|
||||
#endif
|
||||
|
||||
#include "nccs_big.ipp"
|
||||
do_test_nc_chi_squared(nccs_big, type_name, "Non Central Chi Squared, large parameters");
|
||||
quantile_sanity_check(nccs_big, type_name, "Non Central Chi Squared, large parameters");
|
||||
@@ -349,31 +494,41 @@ int test_main(int, char* [])
|
||||
{
|
||||
BOOST_MATH_CONTROL_FP;
|
||||
// Basic sanity-check spot values.
|
||||
|
||||
expected_results();
|
||||
// (Parameter value, arbitrarily zero, only communicates the floating point type).
|
||||
#ifdef TEST_FLOAT
|
||||
test_spots(0.0F); // Test float.
|
||||
#endif
|
||||
#ifdef TEST_DOUBLE
|
||||
test_spots(0.0); // Test double.
|
||||
#endif
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
#ifdef TEST_LDOUBLE
|
||||
test_spots(0.0L); // Test long double.
|
||||
#endif
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
|
||||
#ifdef TEST_REAL_CONCEPT
|
||||
test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef TEST_FLOAT
|
||||
test_accuracy(0.0F, "float"); // Test float.
|
||||
#endif
|
||||
#ifdef TEST_DOUBLE
|
||||
test_accuracy(0.0, "double"); // Test double.
|
||||
#endif
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
#ifdef TEST_LDOUBLE
|
||||
test_accuracy(0.0L, "long double"); // Test long double.
|
||||
#endif
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
|
||||
#ifdef TEST_REAL_CONCEPT
|
||||
test_accuracy(boost::math::concepts::real_concept(0.), "real_concept"); // Test real concept.
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
} // int test_main(int, char* [])
|
||||
|
||||
/*
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user