| 1 |
// (C) Copyright John Maddock 2006. |
| 2 |
// Use, modification and distribution are subject to the |
| 3 |
// Boost Software License, Version 1.0. (See accompanying file |
| 4 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
// |
| 7 |
// This is not a complete header file, it is included by beta.hpp |
| 8 |
// after it has defined it's definitions. This inverts the incomplete |
| 9 |
// beta functions ibeta and ibetac on the first parameters "a" |
| 10 |
// and "b" using a generic root finding algorithm (TOMS Algorithm 748). |
| 11 |
// |
| 12 |
|
| 13 |
#ifndef BOOST_MATH_SP_DETAIL_BETA_INV_AB |
| 14 |
#define BOOST_MATH_SP_DETAIL_BETA_INV_AB |
| 15 |
|
| 16 |
#ifdef _MSC_VER |
| 17 |
#pragma once |
| 18 |
#endif |
| 19 |
|
| 20 |
#include <boost/math/tools/config.hpp> |
| 21 |
#include <boost/math/tools/toms748_solve.hpp> |
| 22 |
#include <boost/math/tools/precision.hpp> |
| 23 |
#include <boost/math/tools/tuple.hpp> |
| 24 |
#include <boost/math/policies/error_handling.hpp> |
| 25 |
|
| 26 |
namespace boost{ namespace math{ namespace detail{ |
| 27 |
|
| 28 |
template <class T, class Policy> |
| 29 |
struct beta_inv_ab_t |
| 30 |
{ |
| 31 |
BOOST_MATH_GPU_ENABLED beta_inv_ab_t(T b_, T z_, T p_, bool invert_, bool swap_ab_) : b(b_), z(z_), p(p_), invert(invert_), swap_ab(swap_ab_) {} |
| 32 |
BOOST_MATH_GPU_ENABLED T operator()(T a) |
| 33 |
{ |
| 34 |
return invert ? |
| 35 |
p - boost::math::ibetac(swap_ab ? b : a, swap_ab ? a : b, z, Policy()) |
| 36 |
: boost::math::ibeta(swap_ab ? b : a, swap_ab ? a : b, z, Policy()) - p; |
| 37 |
} |
| 38 |
private: |
| 39 |
T b, z, p; |
| 40 |
bool invert, swap_ab; |
| 41 |
}; |
| 42 |
|
| 43 |
template <class T, class Policy> |
| 44 |
BOOST_MATH_GPU_ENABLED T inverse_negative_binomial_cornish_fisher(T n, T sf, T sfc, T p, T q, const Policy& pol) |
| 45 |
{ |
| 46 |
BOOST_MATH_STD_USING |
| 47 |
// mean: |
| 48 |
T m = n * (sfc) / sf; |
| 49 |
T t = sqrt(n * (sfc)); |
| 50 |
// standard deviation: |
| 51 |
T sigma = t / sf; |
| 52 |
// skewness |
| 53 |
T sk = (1 + sfc) / t; |
| 54 |
// kurtosis: |
| 55 |
T k = (6 - sf * (5+sfc)) / (n * (sfc)); |
| 56 |
// Get the inverse of a std normal distribution: |
| 57 |
T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>(); |
| 58 |
// Set the sign: |
| 59 |
if(p < 0.5) |
| 60 |
x = -x; |
| 61 |
T x2 = x * x; |
| 62 |
// w is correction term due to skewness |
| 63 |
T w = x + sk * (x2 - 1) / 6; |
| 64 |
// |
| 65 |
// Add on correction due to kurtosis. |
| 66 |
// |
| 67 |
if(n >= 10) |
| 68 |
w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36; |
| 69 |
|
| 70 |
w = m + sigma * w; |
| 71 |
if(w < tools::min_value<T>()) |
| 72 |
return tools::min_value<T>(); |
| 73 |
return w; |
| 74 |
} |
| 75 |
|
| 76 |
template <class T, class Policy> |
| 77 |
BOOST_MATH_GPU_ENABLED T ibeta_inv_ab_imp(const T& b, const T& z, const T& p, const T& q, bool swap_ab, const Policy& pol) |
| 78 |
{ |
| 79 |
BOOST_MATH_STD_USING // for ADL of std lib math functions |
| 80 |
// |
| 81 |
// Special cases first: |
| 82 |
// |
| 83 |
BOOST_MATH_INSTRUMENT_CODE("b = " << b << " z = " << z << " p = " << p << " q = " << " swap = " << swap_ab); |
| 84 |
if(p == 0) |
| 85 |
{ |
| 86 |
return swap_ab ? tools::min_value<T>() : tools::max_value<T>(); |
| 87 |
} |
| 88 |
if(q == 0) |
| 89 |
{ |
| 90 |
return swap_ab ? tools::max_value<T>() : tools::min_value<T>(); |
| 91 |
} |
| 92 |
// |
| 93 |
// Function object, this is the functor whose root |
| 94 |
// we have to solve: |
| 95 |
// |
| 96 |
beta_inv_ab_t<T, Policy> f(b, z, (p < q) ? p : q, (p < q) ? false : true, swap_ab); |
| 97 |
// |
| 98 |
// Tolerance: full precision. |
| 99 |
// |
| 100 |
tools::eps_tolerance<T> tol(policies::digits<T, Policy>()); |
| 101 |
// |
| 102 |
// Now figure out a starting guess for what a may be, |
| 103 |
// we'll start out with a value that'll put p or q |
| 104 |
// right bang in the middle of their range, the functions |
| 105 |
// are quite sensitive so we should need too many steps |
| 106 |
// to bracket the root from there: |
| 107 |
// |
| 108 |
T guess = 0; |
| 109 |
T factor = 5; |
| 110 |
// |
| 111 |
// Convert variables to parameters of a negative binomial distribution: |
| 112 |
// |
| 113 |
T n = b; |
| 114 |
T sf = swap_ab ? z : 1-z; |
| 115 |
T sfc = swap_ab ? 1-z : z; |
| 116 |
T u = swap_ab ? p : q; |
| 117 |
T v = swap_ab ? q : p; |
| 118 |
if(u <= pow(sf, n)) |
| 119 |
{ |
| 120 |
// |
| 121 |
// Result is less than 1, negative binomial approximation |
| 122 |
// is useless.... |
| 123 |
// |
| 124 |
if((p < q) != swap_ab) |
| 125 |
{ |
| 126 |
guess = BOOST_MATH_GPU_SAFE_MIN(T(b * 2), T(1)); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
guess = BOOST_MATH_GPU_SAFE_MIN(T(b / 2), T(1)); |
| 131 |
} |
| 132 |
} |
| 133 |
if(n * n * n * u * sf > 0.005) |
| 134 |
guess = 1 + inverse_negative_binomial_cornish_fisher(n, sf, sfc, u, v, pol); |
| 135 |
|
| 136 |
if(guess < 10) |
| 137 |
{ |
| 138 |
// |
| 139 |
// Negative binomial approximation not accurate in this area: |
| 140 |
// |
| 141 |
if((p < q) != swap_ab) |
| 142 |
{ |
| 143 |
guess = BOOST_MATH_GPU_SAFE_MIN(T(b * 2), T(10)); |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
guess = BOOST_MATH_GPU_SAFE_MIN(T(b / 2), T(10)); |
| 148 |
} |
| 149 |
} |
| 150 |
else |
| 151 |
factor = (v < sqrt(tools::epsilon<T>())) ? 2 : (guess < 20 ? 1.2f : 1.1f); |
| 152 |
BOOST_MATH_INSTRUMENT_CODE("guess = " << guess); |
| 153 |
// |
| 154 |
// Max iterations permitted: |
| 155 |
// |
| 156 |
boost::math::uintmax_t max_iter = policies::get_max_root_iterations<Policy>(); |
| 157 |
boost::math::pair<T, T> r = bracket_and_solve_root(f, guess, factor, swap_ab ? true : false, tol, max_iter, pol); |
| 158 |
if(max_iter >= policies::get_max_root_iterations<Policy>()) |
| 159 |
return policies::raise_evaluation_error<T>("boost::math::ibeta_invab_imp<%1%>(%1%,%1%,%1%)", "Unable to locate the root within a reasonable number of iterations, closest approximation so far was %1%", r.first, pol); |
| 160 |
return (r.first + r.second) / 2; |
| 161 |
} |
| 162 |
|
| 163 |
} // namespace detail |
| 164 |
|
| 165 |
template <class RT1, class RT2, class RT3, class Policy> |
| 166 |
BOOST_MATH_GPU_ENABLED typename tools::promote_args<RT1, RT2, RT3>::type |
| 167 |
ibeta_inva(RT1 b, RT2 x, RT3 p, const Policy& pol) |
| 168 |
{ |
| 169 |
typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type; |
| 170 |
typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 171 |
typedef typename policies::normalise< |
| 172 |
Policy, |
| 173 |
policies::promote_float<false>, |
| 174 |
policies::promote_double<false>, |
| 175 |
policies::discrete_quantile<>, |
| 176 |
policies::assert_undefined<> >::type forwarding_policy; |
| 177 |
|
| 178 |
constexpr auto function = "boost::math::ibeta_inva<%1%>(%1%,%1%,%1%)"; |
| 179 |
if(p == 0) |
| 180 |
{ |
| 181 |
return policies::raise_overflow_error<result_type>(function, 0, Policy()); |
| 182 |
} |
| 183 |
if(p == 1) |
| 184 |
{ |
| 185 |
return tools::min_value<result_type>(); |
| 186 |
} |
| 187 |
|
| 188 |
return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 189 |
detail::ibeta_inv_ab_imp( |
| 190 |
static_cast<value_type>(b), |
| 191 |
static_cast<value_type>(x), |
| 192 |
static_cast<value_type>(p), |
| 193 |
static_cast<value_type>(1 - static_cast<value_type>(p)), |
| 194 |
false, pol), |
| 195 |
function); |
| 196 |
} |
| 197 |
|
| 198 |
template <class RT1, class RT2, class RT3, class Policy> |
| 199 |
BOOST_MATH_GPU_ENABLED typename tools::promote_args<RT1, RT2, RT3>::type |
| 200 |
ibetac_inva(RT1 b, RT2 x, RT3 q, const Policy& pol) |
| 201 |
{ |
| 202 |
typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type; |
| 203 |
typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 204 |
typedef typename policies::normalise< |
| 205 |
Policy, |
| 206 |
policies::promote_float<false>, |
| 207 |
policies::promote_double<false>, |
| 208 |
policies::discrete_quantile<>, |
| 209 |
policies::assert_undefined<> >::type forwarding_policy; |
| 210 |
|
| 211 |
constexpr auto function = "boost::math::ibetac_inva<%1%>(%1%,%1%,%1%)"; |
| 212 |
if(q == 1) |
| 213 |
{ |
| 214 |
return policies::raise_overflow_error<result_type>(function, 0, Policy()); |
| 215 |
} |
| 216 |
if(q == 0) |
| 217 |
{ |
| 218 |
return tools::min_value<result_type>(); |
| 219 |
} |
| 220 |
|
| 221 |
return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 222 |
detail::ibeta_inv_ab_imp( |
| 223 |
static_cast<value_type>(b), |
| 224 |
static_cast<value_type>(x), |
| 225 |
static_cast<value_type>(1 - static_cast<value_type>(q)), |
| 226 |
static_cast<value_type>(q), |
| 227 |
false, pol), |
| 228 |
function); |
| 229 |
} |
| 230 |
|
| 231 |
template <class RT1, class RT2, class RT3, class Policy> |
| 232 |
BOOST_MATH_GPU_ENABLED typename tools::promote_args<RT1, RT2, RT3>::type |
| 233 |
ibeta_invb(RT1 a, RT2 x, RT3 p, const Policy& pol) |
| 234 |
{ |
| 235 |
typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type; |
| 236 |
typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 237 |
typedef typename policies::normalise< |
| 238 |
Policy, |
| 239 |
policies::promote_float<false>, |
| 240 |
policies::promote_double<false>, |
| 241 |
policies::discrete_quantile<>, |
| 242 |
policies::assert_undefined<> >::type forwarding_policy; |
| 243 |
|
| 244 |
constexpr auto function = "boost::math::ibeta_invb<%1%>(%1%,%1%,%1%)"; |
| 245 |
if(p == 0) |
| 246 |
{ |
| 247 |
return tools::min_value<result_type>(); |
| 248 |
} |
| 249 |
if(p == 1) |
| 250 |
{ |
| 251 |
return policies::raise_overflow_error<result_type>(function, 0, Policy()); |
| 252 |
} |
| 253 |
|
| 254 |
return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 255 |
detail::ibeta_inv_ab_imp( |
| 256 |
static_cast<value_type>(a), |
| 257 |
static_cast<value_type>(x), |
| 258 |
static_cast<value_type>(p), |
| 259 |
static_cast<value_type>(1 - static_cast<value_type>(p)), |
| 260 |
true, pol), |
| 261 |
function); |
| 262 |
} |
| 263 |
|
| 264 |
template <class RT1, class RT2, class RT3, class Policy> |
| 265 |
BOOST_MATH_GPU_ENABLED typename tools::promote_args<RT1, RT2, RT3>::type |
| 266 |
ibetac_invb(RT1 a, RT2 x, RT3 q, const Policy& pol) |
| 267 |
{ |
| 268 |
constexpr auto function = "boost::math::ibeta_invb<%1%>(%1%, %1%, %1%)"; |
| 269 |
typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type; |
| 270 |
typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 271 |
typedef typename policies::normalise< |
| 272 |
Policy, |
| 273 |
policies::promote_float<false>, |
| 274 |
policies::promote_double<false>, |
| 275 |
policies::discrete_quantile<>, |
| 276 |
policies::assert_undefined<> >::type forwarding_policy; |
| 277 |
|
| 278 |
if(q == 1) |
| 279 |
{ |
| 280 |
return tools::min_value<result_type>(); |
| 281 |
} |
| 282 |
if(q == 0) |
| 283 |
{ |
| 284 |
return policies::raise_overflow_error<result_type>(function, 0, Policy()); |
| 285 |
} |
| 286 |
|
| 287 |
return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 288 |
detail::ibeta_inv_ab_imp( |
| 289 |
static_cast<value_type>(a), |
| 290 |
static_cast<value_type>(x), |
| 291 |
static_cast<value_type>(1 - static_cast<value_type>(q)), |
| 292 |
static_cast<value_type>(q), |
| 293 |
true, pol), |
| 294 |
function); |
| 295 |
} |
| 296 |
|
| 297 |
template <class RT1, class RT2, class RT3> |
| 298 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<RT1, RT2, RT3>::type |
| 299 |
ibeta_inva(RT1 b, RT2 x, RT3 p) |
| 300 |
{ |
| 301 |
return boost::math::ibeta_inva(b, x, p, policies::policy<>()); |
| 302 |
} |
| 303 |
|
| 304 |
template <class RT1, class RT2, class RT3> |
| 305 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<RT1, RT2, RT3>::type |
| 306 |
ibetac_inva(RT1 b, RT2 x, RT3 q) |
| 307 |
{ |
| 308 |
return boost::math::ibetac_inva(b, x, q, policies::policy<>()); |
| 309 |
} |
| 310 |
|
| 311 |
template <class RT1, class RT2, class RT3> |
| 312 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<RT1, RT2, RT3>::type |
| 313 |
ibeta_invb(RT1 a, RT2 x, RT3 p) |
| 314 |
{ |
| 315 |
return boost::math::ibeta_invb(a, x, p, policies::policy<>()); |
| 316 |
} |
| 317 |
|
| 318 |
template <class RT1, class RT2, class RT3> |
| 319 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<RT1, RT2, RT3>::type |
| 320 |
ibetac_invb(RT1 a, RT2 x, RT3 q) |
| 321 |
{ |
| 322 |
return boost::math::ibetac_invb(a, x, q, policies::policy<>()); |
| 323 |
} |
| 324 |
|
| 325 |
} // namespace math |
| 326 |
} // namespace boost |
| 327 |
|
| 328 |
#endif // BOOST_MATH_SP_DETAIL_BETA_INV_AB |
| 329 |
|
| 330 |
|
| 331 |
|