Skip to content

Commit

Permalink
Improve error handling in non-central distributions. (#963)
Browse files Browse the repository at this point in the history
* Improve error handling in non-central distributions.

* Try turning debug symbols off for msvc-14.0.
So we don't run out of disk space on CI.
  • Loading branch information
jzmaddock committed Mar 7, 2023
1 parent bf3bc2e commit cf6cd75
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ jobs:
run: config_info_travis
working-directory: ../boost-root/libs/config/test
- name: Test
run: ..\..\..\b2 --hash %ARGS% define=CI_SUPPRESS_KNOWN_ISSUES ${{ matrix.suite }}
run: ..\..\..\b2 --hash %ARGS% define=CI_SUPPRESS_KNOWN_ISSUES debug-symbols=off ${{ matrix.suite }}
working-directory: ../boost-root/libs/math/test
windows_gcc:
runs-on: windows-2019
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ inline bool check_non_centrality(
RealType* result,
const Policy& pol)
{
if((ncp < 0) || !(boost::math::isfinite)(ncp))
{ // Assume scale == 0 is NOT valid for any distribution.
static const RealType upper_limit = static_cast<RealType>((std::numeric_limits<long long>::max)()) - boost::math::policies::get_max_root_iterations<Policy>();
if((ncp < 0) || !(boost::math::isfinite)(ncp) || ncp > upper_limit)
{
*result = policies::raise_domain_error<RealType>(
function,
"Non centrality parameter is %1%, but must be > 0 !", ncp, pol);
"Non centrality parameter is %1%, but must be > 0, and a countable value such that x+1 != x", ncp, pol);
return false;
}
return true;
Expand Down
10 changes: 5 additions & 5 deletions include/boost/math/distributions/non_central_beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ namespace boost
static_cast<value_type>(p),
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);
//
// Special cases first:
//
Expand Down Expand Up @@ -624,7 +624,7 @@ namespace boost
static_cast<value_type>(x),
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);

if(l == 0)
return pdf(boost::math::beta_distribution<RealType, Policy>(dist.alpha(), dist.beta()), x);
Expand Down Expand Up @@ -761,7 +761,7 @@ namespace boost
l,
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);
RealType c = a + b + l / 2;
RealType mean = 1 - (b / c) * (1 + l / (2 * c * c));
return detail::generic_find_mode_01(
Expand Down Expand Up @@ -872,7 +872,7 @@ namespace boost
x,
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);

if(l == 0)
return cdf(beta_distribution<RealType, Policy>(a, b), x);
Expand Down Expand Up @@ -909,7 +909,7 @@ namespace boost
x,
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);

if(l == 0)
return cdf(complement(beta_distribution<RealType, Policy>(a, b), x));
Expand Down
24 changes: 12 additions & 12 deletions include/boost/math/distributions/non_central_chi_squared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace boost
// stable direction for the gamma function
// recurrences:
//
int i;
long long i;
for(i = k; static_cast<std::uintmax_t>(i-k) < max_iter; ++i)
{
T term = poisf * gamf;
Expand Down Expand Up @@ -299,7 +299,7 @@ namespace boost
if(pois == 0)
return 0;
T poisb = pois;
for(int i = k; ; ++i)
for(long long i = k; ; ++i)
{
sum += pois;
if(pois / sum < errtol)
Expand All @@ -310,7 +310,7 @@ namespace boost
"Series did not converge, closest value was %1%", sum, pol);
pois *= l2 * x2 / ((i + 1) * (n2 + i));
}
for(int i = k - 1; i >= 0; --i)
for(long long i = k - 1; i >= 0; --i)
{
poisb *= (i + 1) * (n2 + i) / (l2 * x2);
sum += poisb;
Expand Down Expand Up @@ -428,7 +428,7 @@ namespace boost
static_cast<value_type>(p),
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);
//
// Special cases get short-circuited first:
//
Expand Down Expand Up @@ -519,7 +519,7 @@ namespace boost
(value_type)x,
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);

if(l == 0)
return pdf(boost::math::chi_squared_distribution<RealType, forwarding_policy>(dist.degrees_of_freedom()), x);
Expand Down Expand Up @@ -821,7 +821,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return static_cast<RealType>(r);
return k + l;
} // mean

Expand All @@ -842,7 +842,7 @@ namespace boost
l,
&r,
Policy()))
return (RealType)r;
return static_cast<RealType>(r);
bool asymptotic_mode = k < l/4;
RealType starting_point = asymptotic_mode ? k + l - RealType(3) : RealType(1) + k;
return detail::generic_find_mode(dist, starting_point, function);
Expand All @@ -864,7 +864,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return static_cast<RealType>(r);
return 2 * (2 * l + k);
}

Expand All @@ -887,7 +887,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return static_cast<RealType>(r);
BOOST_MATH_STD_USING
return pow(2 / (k + 2 * l), RealType(3)/2) * (k + 3 * l);
}
Expand All @@ -908,7 +908,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return static_cast<RealType>(r);
return 12 * (k + 4 * l) / ((k + 2 * l) * (k + 2 * l));
} // kurtosis_excess

Expand Down Expand Up @@ -946,7 +946,7 @@ namespace boost
x,
&r,
Policy()))
return r;
return static_cast<RealType>(r);

return detail::non_central_chi_squared_cdf(x, k, l, false, Policy());
} // cdf
Expand Down Expand Up @@ -975,7 +975,7 @@ namespace boost
x,
&r,
Policy()))
return r;
return static_cast<RealType>(r);

return detail::non_central_chi_squared_cdf(x, k, l, true, Policy());
} // ccdf
Expand Down
14 changes: 7 additions & 7 deletions include/boost/math/distributions/non_central_f.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return r;
if(v2 <= 2)
return policies::raise_domain_error(
function,
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return r;
RealType guess = m > 2 ? RealType(m * (n + l) / (n * (m - 2))) : RealType(1);
return detail::generic_find_mode(
dist,
Expand Down Expand Up @@ -166,7 +166,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return r;
if(m <= 4)
return policies::raise_domain_error(
function,
Expand Down Expand Up @@ -203,7 +203,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return r;
if(m <= 6)
return policies::raise_domain_error(
function,
Expand Down Expand Up @@ -240,7 +240,7 @@ namespace boost
l,
&r,
Policy()))
return r;
return r;
if(m <= 8)
return policies::raise_domain_error(
function,
Expand Down Expand Up @@ -309,7 +309,7 @@ namespace boost
dist.non_centrality(),
&r,
Policy()))
return r;
return r;

if((x < 0) || !(boost::math::isfinite)(x))
{
Expand Down Expand Up @@ -350,7 +350,7 @@ namespace boost
c.dist.non_centrality(),
&r,
Policy()))
return r;
return r;

if((c.param < 0) || !(boost::math::isfinite)(c.param))
{
Expand Down
Loading

0 comments on commit cf6cd75

Please sign in to comment.