Chris@16: // Copyright John Maddock 2006. Chris@16: // Copyright Paul A. Bristow 2007 Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_SPECIAL_FUNCTIONS_IBETA_INVERSE_HPP Chris@16: #define BOOST_MATH_SPECIAL_FUNCTIONS_IBETA_INVERSE_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ namespace detail{ Chris@16: Chris@16: // Chris@16: // Helper object used by root finding Chris@16: // code to convert eta to x. Chris@16: // Chris@16: template Chris@16: struct temme_root_finder Chris@16: { Chris@16: temme_root_finder(const T t_, const T a_) : t(t_), a(a_) {} Chris@16: Chris@16: boost::math::tuple operator()(T x) Chris@16: { Chris@16: BOOST_MATH_STD_USING // ADL of std names Chris@16: Chris@16: T y = 1 - x; Chris@16: if(y == 0) Chris@16: { Chris@16: T big = tools::max_value() / 4; Chris@16: return boost::math::make_tuple(static_cast(-big), static_cast(-big)); Chris@16: } Chris@16: if(x == 0) Chris@16: { Chris@16: T big = tools::max_value() / 4; Chris@16: return boost::math::make_tuple(static_cast(-big), big); Chris@16: } Chris@16: T f = log(x) + a * log(y) + t; Chris@16: T f1 = (1 / x) - (a / (y)); Chris@16: return boost::math::make_tuple(f, f1); Chris@16: } Chris@16: private: Chris@16: T t, a; Chris@16: }; Chris@16: // Chris@16: // See: Chris@16: // "Asymptotic Inversion of the Incomplete Beta Function" Chris@16: // N.M. Temme Chris@16: // Journal of Computation and Applied Mathematics 41 (1992) 145-157. Chris@16: // Section 2. Chris@16: // Chris@16: template Chris@16: T temme_method_1_ibeta_inverse(T a, T b, T z, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING // ADL of std names Chris@16: Chris@16: const T r2 = sqrt(T(2)); Chris@16: // Chris@16: // get the first approximation for eta from the inverse Chris@16: // error function (Eq: 2.9 and 2.10). Chris@16: // Chris@16: T eta0 = boost::math::erfc_inv(2 * z, pol); Chris@16: eta0 /= -sqrt(a / 2); Chris@16: Chris@16: T terms[4] = { eta0 }; Chris@16: T workspace[7]; Chris@16: // Chris@16: // calculate powers: Chris@16: // Chris@16: T B = b - a; Chris@16: T B_2 = B * B; Chris@16: T B_3 = B_2 * B; Chris@16: // Chris@16: // Calculate correction terms: Chris@16: // Chris@16: Chris@16: // See eq following 2.15: Chris@16: workspace[0] = -B * r2 / 2; Chris@16: workspace[1] = (1 - 2 * B) / 8; Chris@16: workspace[2] = -(B * r2 / 48); Chris@16: workspace[3] = T(-1) / 192; Chris@16: workspace[4] = -B * r2 / 3840; Chris@16: terms[1] = tools::evaluate_polynomial(workspace, eta0, 5); Chris@16: // Eq Following 2.17: Chris@16: workspace[0] = B * r2 * (3 * B - 2) / 12; Chris@16: workspace[1] = (20 * B_2 - 12 * B + 1) / 128; Chris@16: workspace[2] = B * r2 * (20 * B - 1) / 960; Chris@16: workspace[3] = (16 * B_2 + 30 * B - 15) / 4608; Chris@16: workspace[4] = B * r2 * (21 * B + 32) / 53760; Chris@16: workspace[5] = (-32 * B_2 + 63) / 368640; Chris@16: workspace[6] = -B * r2 * (120 * B + 17) / 25804480; Chris@16: terms[2] = tools::evaluate_polynomial(workspace, eta0, 7); Chris@16: // Eq Following 2.17: Chris@16: workspace[0] = B * r2 * (-75 * B_2 + 80 * B - 16) / 480; Chris@16: workspace[1] = (-1080 * B_3 + 868 * B_2 - 90 * B - 45) / 9216; Chris@16: workspace[2] = B * r2 * (-1190 * B_2 + 84 * B + 373) / 53760; Chris@16: workspace[3] = (-2240 * B_3 - 2508 * B_2 + 2100 * B - 165) / 368640; Chris@16: terms[3] = tools::evaluate_polynomial(workspace, eta0, 4); Chris@16: // Chris@16: // Bring them together to get a final estimate for eta: Chris@16: // Chris@16: T eta = tools::evaluate_polynomial(terms, T(1/a), 4); Chris@16: // Chris@16: // now we need to convert eta to x, by solving the appropriate Chris@16: // quadratic equation: Chris@16: // Chris@16: T eta_2 = eta * eta; Chris@16: T c = -exp(-eta_2 / 2); Chris@16: T x; Chris@16: if(eta_2 == 0) Chris@16: x = 0.5; Chris@16: else Chris@16: x = (1 + eta * sqrt((1 + c) / eta_2)) / 2; Chris@16: Chris@16: BOOST_ASSERT(x >= 0); Chris@16: BOOST_ASSERT(x <= 1); Chris@16: BOOST_ASSERT(eta * (x - 0.5) >= 0); Chris@16: #ifdef BOOST_INSTRUMENT Chris@16: std::cout << "Estimating x with Temme method 1: " << x << std::endl; Chris@16: #endif Chris@16: return x; Chris@16: } Chris@16: // Chris@16: // See: Chris@16: // "Asymptotic Inversion of the Incomplete Beta Function" Chris@16: // N.M. Temme Chris@16: // Journal of Computation and Applied Mathematics 41 (1992) 145-157. Chris@16: // Section 3. Chris@16: // Chris@16: template Chris@16: T temme_method_2_ibeta_inverse(T /*a*/, T /*b*/, T z, T r, T theta, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING // ADL of std names Chris@16: Chris@16: // Chris@16: // Get first estimate for eta, see Eq 3.9 and 3.10, Chris@16: // but note there is a typo in Eq 3.10: Chris@16: // Chris@16: T eta0 = boost::math::erfc_inv(2 * z, pol); Chris@16: eta0 /= -sqrt(r / 2); Chris@16: Chris@16: T s = sin(theta); Chris@16: T c = cos(theta); Chris@16: // Chris@16: // Now we need to purturb eta0 to get eta, which we do by Chris@16: // evaluating the polynomial in 1/r at the bottom of page 151, Chris@16: // to do this we first need the error terms e1, e2 e3 Chris@16: // which we'll fill into the array "terms". Since these Chris@16: // terms are themselves polynomials, we'll need another Chris@16: // array "workspace" to calculate those... Chris@16: // Chris@16: T terms[4] = { eta0 }; Chris@16: T workspace[6]; Chris@16: // Chris@16: // some powers of sin(theta)cos(theta) that we'll need later: Chris@16: // Chris@16: T sc = s * c; Chris@16: T sc_2 = sc * sc; Chris@16: T sc_3 = sc_2 * sc; Chris@16: T sc_4 = sc_2 * sc_2; Chris@16: T sc_5 = sc_2 * sc_3; Chris@16: T sc_6 = sc_3 * sc_3; Chris@16: T sc_7 = sc_4 * sc_3; Chris@16: // Chris@16: // Calculate e1 and put it in terms[1], see the middle of page 151: Chris@16: // Chris@16: workspace[0] = (2 * s * s - 1) / (3 * s * c); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co1[] = { -1, -5, 5 }; Chris@16: workspace[1] = -tools::evaluate_even_polynomial(co1, s, 3) / (36 * sc_2); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co2[] = { 1, 21, -69, 46 }; Chris@16: workspace[2] = tools::evaluate_even_polynomial(co2, s, 4) / (1620 * sc_3); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co3[] = { 7, -2, 33, -62, 31 }; Chris@16: workspace[3] = -tools::evaluate_even_polynomial(co3, s, 5) / (6480 * sc_4); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co4[] = { 25, -52, -17, 88, -115, 46 }; Chris@16: workspace[4] = tools::evaluate_even_polynomial(co4, s, 6) / (90720 * sc_5); Chris@16: terms[1] = tools::evaluate_polynomial(workspace, eta0, 5); Chris@16: // Chris@16: // Now evaluate e2 and put it in terms[2]: Chris@16: // Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co5[] = { 7, 12, -78, 52 }; Chris@16: workspace[0] = -tools::evaluate_even_polynomial(co5, s, 4) / (405 * sc_3); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co6[] = { -7, 2, 183, -370, 185 }; Chris@16: workspace[1] = tools::evaluate_even_polynomial(co6, s, 5) / (2592 * sc_4); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co7[] = { -533, 776, -1835, 10240, -13525, 5410 }; Chris@16: workspace[2] = -tools::evaluate_even_polynomial(co7, s, 6) / (204120 * sc_5); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co8[] = { -1579, 3747, -3372, -15821, 45588, -45213, 15071 }; Chris@16: workspace[3] = -tools::evaluate_even_polynomial(co8, s, 7) / (2099520 * sc_6); Chris@16: terms[2] = tools::evaluate_polynomial(workspace, eta0, 4); Chris@16: // Chris@16: // And e3, and put it in terms[3]: Chris@16: // Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co9[] = {449, -1259, -769, 6686, -9260, 3704 }; Chris@16: workspace[0] = tools::evaluate_even_polynomial(co9, s, 6) / (102060 * sc_5); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co10[] = { 63149, -151557, 140052, -727469, 2239932, -2251437, 750479 }; Chris@16: workspace[1] = -tools::evaluate_even_polynomial(co10, s, 7) / (20995200 * sc_6); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co11[] = { 29233, -78755, 105222, 146879, -1602610, 3195183, -2554139, 729754 }; Chris@16: workspace[2] = tools::evaluate_even_polynomial(co11, s, 8) / (36741600 * sc_7); Chris@16: terms[3] = tools::evaluate_polynomial(workspace, eta0, 3); Chris@16: // Chris@16: // Bring the correction terms together to evaluate eta, Chris@16: // this is the last equation on page 151: Chris@16: // Chris@16: T eta = tools::evaluate_polynomial(terms, T(1/r), 4); Chris@16: // Chris@16: // Now that we have eta we need to back solve for x, Chris@16: // we seek the value of x that gives eta in Eq 3.2. Chris@16: // The two methods used are described in section 5. Chris@16: // Chris@16: // Begin by defining a few variables we'll need later: Chris@16: // Chris@16: T x; Chris@16: T s_2 = s * s; Chris@16: T c_2 = c * c; Chris@16: T alpha = c / s; Chris@16: alpha *= alpha; Chris@16: T lu = (-(eta * eta) / (2 * s_2) + log(s_2) + c_2 * log(c_2) / s_2); Chris@16: // Chris@16: // Temme doesn't specify what value to switch on here, Chris@16: // but this seems to work pretty well: Chris@16: // Chris@16: if(fabs(eta) < 0.7) Chris@16: { Chris@16: // Chris@16: // Small eta use the expansion Temme gives in the second equation Chris@16: // of section 5, it's a polynomial in eta: Chris@16: // Chris@16: workspace[0] = s * s; Chris@16: workspace[1] = s * c; Chris@16: workspace[2] = (1 - 2 * workspace[0]) / 3; Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co12[] = { 1, -13, 13 }; Chris@16: workspace[3] = tools::evaluate_polynomial(co12, workspace[0], 3) / (36 * s * c); Chris@16: static const BOOST_MATH_INT_TABLE_TYPE(T, int) co13[] = { 1, 21, -69, 46 }; Chris@16: workspace[4] = tools::evaluate_polynomial(co13, workspace[0], 4) / (270 * workspace[0] * c * c); Chris@16: x = tools::evaluate_polynomial(workspace, eta, 5); Chris@16: #ifdef BOOST_INSTRUMENT Chris@16: std::cout << "Estimating x with Temme method 2 (small eta): " << x << std::endl; Chris@16: #endif Chris@16: } Chris@16: else Chris@16: { Chris@16: // Chris@16: // If eta is large we need to solve Eq 3.2 more directly, Chris@16: // begin by getting an initial approximation for x from Chris@16: // the last equation on page 155, this is a polynomial in u: Chris@16: // Chris@16: T u = exp(lu); Chris@16: workspace[0] = u; Chris@16: workspace[1] = alpha; Chris@16: workspace[2] = 0; Chris@16: workspace[3] = 3 * alpha * (3 * alpha + 1) / 6; Chris@16: workspace[4] = 4 * alpha * (4 * alpha + 1) * (4 * alpha + 2) / 24; Chris@16: workspace[5] = 5 * alpha * (5 * alpha + 1) * (5 * alpha + 2) * (5 * alpha + 3) / 120; Chris@16: x = tools::evaluate_polynomial(workspace, u, 6); Chris@16: // Chris@16: // At this point we may or may not have the right answer, Eq-3.2 has Chris@16: // two solutions for x for any given eta, however the mapping in 3.2 Chris@16: // is 1:1 with the sign of eta and x-sin^2(theta) being the same. Chris@16: // So we can check if we have the right root of 3.2, and if not Chris@16: // switch x for 1-x. This transformation is motivated by the fact Chris@16: // that the distribution is *almost* symetric so 1-x will be in the right Chris@16: // ball park for the solution: Chris@16: // Chris@16: if((x - s_2) * eta < 0) Chris@16: x = 1 - x; Chris@16: #ifdef BOOST_INSTRUMENT Chris@16: std::cout << "Estimating x with Temme method 2 (large eta): " << x << std::endl; Chris@16: #endif Chris@16: } Chris@16: // Chris@16: // The final step is a few Newton-Raphson iterations to Chris@16: // clean up our approximation for x, this is pretty cheap Chris@16: // in general, and very cheap compared to an incomplete beta Chris@16: // evaluation. The limits set on x come from the observation Chris@16: // that the sign of eta and x-sin^2(theta) are the same. Chris@16: // Chris@16: T lower, upper; Chris@16: if(eta < 0) Chris@16: { Chris@16: lower = 0; Chris@16: upper = s_2; Chris@16: } Chris@16: else Chris@16: { Chris@16: lower = s_2; Chris@16: upper = 1; Chris@16: } Chris@16: // Chris@16: // If our initial approximation is out of bounds then bisect: Chris@16: // Chris@16: if((x < lower) || (x > upper)) Chris@16: x = (lower+upper) / 2; Chris@16: // Chris@16: // And iterate: Chris@16: // Chris@16: x = tools::newton_raphson_iterate( Chris@16: temme_root_finder(-lu, alpha), x, lower, upper, policies::digits() / 2); Chris@16: Chris@16: return x; Chris@16: } Chris@16: // Chris@16: // See: Chris@16: // "Asymptotic Inversion of the Incomplete Beta Function" Chris@16: // N.M. Temme Chris@16: // Journal of Computation and Applied Mathematics 41 (1992) 145-157. Chris@16: // Section 4. Chris@16: // Chris@16: template Chris@16: T temme_method_3_ibeta_inverse(T a, T b, T p, T q, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING // ADL of std names Chris@16: Chris@16: // Chris@16: // Begin by getting an initial approximation for the quantity Chris@16: // eta from the dominant part of the incomplete beta: Chris@16: // Chris@16: T eta0; Chris@16: if(p < q) Chris@16: eta0 = boost::math::gamma_q_inv(b, p, pol); Chris@16: else Chris@16: eta0 = boost::math::gamma_p_inv(b, q, pol); Chris@16: eta0 /= a; Chris@16: // Chris@16: // Define the variables and powers we'll need later on: Chris@16: // Chris@16: T mu = b / a; Chris@16: T w = sqrt(1 + mu); Chris@16: T w_2 = w * w; Chris@16: T w_3 = w_2 * w; Chris@16: T w_4 = w_2 * w_2; Chris@16: T w_5 = w_3 * w_2; Chris@16: T w_6 = w_3 * w_3; Chris@16: T w_7 = w_4 * w_3; Chris@16: T w_8 = w_4 * w_4; Chris@16: T w_9 = w_5 * w_4; Chris@16: T w_10 = w_5 * w_5; Chris@16: T d = eta0 - mu; Chris@16: T d_2 = d * d; Chris@16: T d_3 = d_2 * d; Chris@16: T d_4 = d_2 * d_2; Chris@16: T w1 = w + 1; Chris@16: T w1_2 = w1 * w1; Chris@16: T w1_3 = w1 * w1_2; Chris@16: T w1_4 = w1_2 * w1_2; Chris@16: // Chris@16: // Now we need to compute the purturbation error terms that Chris@16: // convert eta0 to eta, these are all polynomials of polynomials. Chris@16: // Probably these should be re-written to use tabulated data Chris@16: // (see examples above), but it's less of a win in this case as we Chris@16: // need to calculate the individual powers for the denominator terms Chris@16: // anyway, so we might as well use them for the numerator-polynomials Chris@16: // as well.... Chris@16: // Chris@16: // Refer to p154-p155 for the details of these expansions: Chris@16: // Chris@16: T e1 = (w + 2) * (w - 1) / (3 * w); Chris@16: e1 += (w_3 + 9 * w_2 + 21 * w + 5) * d / (36 * w_2 * w1); Chris@16: e1 -= (w_4 - 13 * w_3 + 69 * w_2 + 167 * w + 46) * d_2 / (1620 * w1_2 * w_3); Chris@16: e1 -= (7 * w_5 + 21 * w_4 + 70 * w_3 + 26 * w_2 - 93 * w - 31) * d_3 / (6480 * w1_3 * w_4); Chris@16: e1 -= (75 * w_6 + 202 * w_5 + 188 * w_4 - 888 * w_3 - 1345 * w_2 + 118 * w + 138) * d_4 / (272160 * w1_4 * w_5); Chris@16: Chris@16: T e2 = (28 * w_4 + 131 * w_3 + 402 * w_2 + 581 * w + 208) * (w - 1) / (1620 * w1 * w_3); Chris@16: e2 -= (35 * w_6 - 154 * w_5 - 623 * w_4 - 1636 * w_3 - 3983 * w_2 - 3514 * w - 925) * d / (12960 * w1_2 * w_4); Chris@16: e2 -= (2132 * w_7 + 7915 * w_6 + 16821 * w_5 + 35066 * w_4 + 87490 * w_3 + 141183 * w_2 + 95993 * w + 21640) * d_2 / (816480 * w_5 * w1_3); Chris@16: e2 -= (11053 * w_8 + 53308 * w_7 + 117010 * w_6 + 163924 * w_5 + 116188 * w_4 - 258428 * w_3 - 677042 * w_2 - 481940 * w - 105497) * d_3 / (14696640 * w1_4 * w_6); Chris@16: Chris@16: T e3 = -((3592 * w_7 + 8375 * w_6 - 1323 * w_5 - 29198 * w_4 - 89578 * w_3 - 154413 * w_2 - 116063 * w - 29632) * (w - 1)) / (816480 * w_5 * w1_2); Chris@16: e3 -= (442043 * w_9 + 2054169 * w_8 + 3803094 * w_7 + 3470754 * w_6 + 2141568 * w_5 - 2393568 * w_4 - 19904934 * w_3 - 34714674 * w_2 - 23128299 * w - 5253353) * d / (146966400 * w_6 * w1_3); Chris@16: e3 -= (116932 * w_10 + 819281 * w_9 + 2378172 * w_8 + 4341330 * w_7 + 6806004 * w_6 + 10622748 * w_5 + 18739500 * w_4 + 30651894 * w_3 + 30869976 * w_2 + 15431867 * w + 2919016) * d_2 / (146966400 * w1_4 * w_7); Chris@16: // Chris@16: // Combine eta0 and the error terms to compute eta (Second eqaution p155): Chris@16: // Chris@16: T eta = eta0 + e1 / a + e2 / (a * a) + e3 / (a * a * a); Chris@16: // Chris@16: // Now we need to solve Eq 4.2 to obtain x. For any given value of Chris@16: // eta there are two solutions to this equation, and since the distribtion Chris@16: // may be very skewed, these are not related by x ~ 1-x we used when Chris@16: // implementing section 3 above. However we know that: Chris@16: // Chris@16: // cross < x <= 1 ; iff eta < mu Chris@16: // x == cross ; iff eta == mu Chris@16: // 0 <= x < cross ; iff eta > mu Chris@16: // Chris@16: // Where cross == 1 / (1 + mu) Chris@16: // Many thanks to Prof Temme for clarifying this point. Chris@16: // Chris@16: // Therefore we'll just jump straight into Newton iterations Chris@16: // to solve Eq 4.2 using these bounds, and simple bisection Chris@16: // as the first guess, in practice this converges pretty quickly Chris@16: // and we only need a few digits correct anyway: Chris@16: // Chris@16: if(eta <= 0) Chris@16: eta = tools::min_value(); Chris@16: T u = eta - mu * log(eta) + (1 + mu) * log(1 + mu) - mu; Chris@16: T cross = 1 / (1 + mu); Chris@16: T lower = eta < mu ? cross : 0; Chris@16: T upper = eta < mu ? 1 : cross; Chris@16: T x = (lower + upper) / 2; Chris@16: x = tools::newton_raphson_iterate( Chris@16: temme_root_finder(u, mu), x, lower, upper, policies::digits() / 2); Chris@16: #ifdef BOOST_INSTRUMENT Chris@16: std::cout << "Estimating x with Temme method 3: " << x << std::endl; Chris@16: #endif Chris@16: return x; Chris@16: } Chris@16: Chris@16: template Chris@16: struct ibeta_roots Chris@16: { Chris@16: ibeta_roots(T _a, T _b, T t, bool inv = false) Chris@16: : a(_a), b(_b), target(t), invert(inv) {} Chris@16: Chris@16: boost::math::tuple operator()(T x) Chris@16: { Chris@16: BOOST_MATH_STD_USING // ADL of std names Chris@16: Chris@16: BOOST_FPU_EXCEPTION_GUARD Chris@16: Chris@16: T f1; Chris@16: T y = 1 - x; Chris@16: T f = ibeta_imp(a, b, x, Policy(), invert, true, &f1) - target; Chris@16: if(invert) Chris@16: f1 = -f1; Chris@16: if(y == 0) Chris@16: y = tools::min_value() * 64; Chris@16: if(x == 0) Chris@16: x = tools::min_value() * 64; Chris@16: Chris@16: T f2 = f1 * (-y * a + (b - 2) * x + 1); Chris@16: if(fabs(f2) < y * x * tools::max_value()) Chris@16: f2 /= (y * x); Chris@16: if(invert) Chris@16: f2 = -f2; Chris@16: Chris@16: // make sure we don't have a zero derivative: Chris@16: if(f1 == 0) Chris@16: f1 = (invert ? -1 : 1) * tools::min_value() * 64; Chris@16: Chris@16: return boost::math::make_tuple(f, f1, f2); Chris@16: } Chris@16: private: Chris@16: T a, b, target; Chris@16: bool invert; Chris@16: }; Chris@16: Chris@16: template Chris@16: T ibeta_inv_imp(T a, T b, T p, T q, const Policy& pol, T* py) Chris@16: { Chris@16: BOOST_MATH_STD_USING // For ADL of math functions. Chris@16: Chris@16: // Chris@16: // The flag invert is set to true if we swap a for b and p for q, Chris@16: // in which case the result has to be subtracted from 1: Chris@16: // Chris@16: bool invert = false; Chris@16: // Chris@16: // Handle trivial cases first: Chris@16: // Chris@16: if(q == 0) Chris@16: { Chris@16: if(py) *py = 0; Chris@16: return 1; Chris@16: } Chris@16: else if(p == 0) Chris@16: { Chris@16: if(py) *py = 1; Chris@16: return 0; Chris@16: } Chris@16: else if(a == 1) Chris@16: { Chris@16: if(b == 1) Chris@16: { Chris@16: if(py) *py = 1 - p; Chris@16: return p; Chris@16: } Chris@16: // Change things around so we can handle as b == 1 special case below: Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: invert = true; Chris@16: } Chris@16: // Chris@16: // Depending upon which approximation method we use, we may end up Chris@16: // calculating either x or y initially (where y = 1-x): Chris@16: // Chris@16: T x = 0; // Set to a safe zero to avoid a Chris@16: // MSVC 2005 warning C4701: potentially uninitialized local variable 'x' used Chris@16: // But code inspection appears to ensure that x IS assigned whatever the code path. Chris@16: T y; Chris@16: Chris@16: // For some of the methods we can put tighter bounds Chris@16: // on the result than simply [0,1]: Chris@16: // Chris@16: T lower = 0; Chris@16: T upper = 1; Chris@16: // Chris@16: // Student's T with b = 0.5 gets handled as a special case, swap Chris@16: // around if the arguments are in the "wrong" order: Chris@16: // Chris@16: if(a == 0.5f) Chris@16: { Chris@16: if(b == 0.5f) Chris@16: { Chris@16: x = sin(p * constants::half_pi()); Chris@16: x *= x; Chris@16: if(py) Chris@16: { Chris@16: *py = sin(q * constants::half_pi()); Chris@16: *py *= *py; Chris@16: } Chris@16: return x; Chris@16: } Chris@16: else if(b > 0.5f) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: invert = !invert; Chris@16: } Chris@16: } Chris@16: // Chris@16: // Select calculation method for the initial estimate: Chris@16: // Chris@16: if((b == 0.5f) && (a >= 0.5f) && (p != 1)) Chris@16: { Chris@16: // Chris@16: // We have a Student's T distribution: Chris@16: x = find_ibeta_inv_from_t_dist(a, p, q, &y, pol); Chris@16: } Chris@16: else if(b == 1) Chris@16: { Chris@16: if(p < q) Chris@16: { Chris@16: if(a > 1) Chris@16: { Chris@16: x = pow(p, 1 / a); Chris@101: y = -boost::math::expm1(log(p) / a, pol); Chris@16: } Chris@16: else Chris@16: { Chris@16: x = pow(p, 1 / a); Chris@16: y = 1 - x; Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@101: x = exp(boost::math::log1p(-q, pol) / a); Chris@101: y = -boost::math::expm1(boost::math::log1p(-q, pol) / a, pol); Chris@16: } Chris@16: if(invert) Chris@16: std::swap(x, y); Chris@16: if(py) Chris@16: *py = y; Chris@16: return x; Chris@16: } Chris@16: else if(a + b > 5) Chris@16: { Chris@16: // Chris@16: // When a+b is large then we can use one of Prof Temme's Chris@16: // asymptotic expansions, begin by swapping things around Chris@16: // so that p < 0.5, we do this to avoid cancellations errors Chris@16: // when p is large. Chris@16: // Chris@16: if(p > 0.5) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: invert = !invert; Chris@16: } Chris@16: T minv = (std::min)(a, b); Chris@16: T maxv = (std::max)(a, b); Chris@16: if((sqrt(minv) > (maxv - minv)) && (minv > 5)) Chris@16: { Chris@16: // Chris@16: // When a and b differ by a small amount Chris@16: // the curve is quite symmetrical and we can use an error Chris@16: // function to approximate the inverse. This is the cheapest Chris@16: // of the three Temme expantions, and the calculated value Chris@16: // for x will never be much larger than p, so we don't have Chris@16: // to worry about cancellation as long as p is small. Chris@16: // Chris@16: x = temme_method_1_ibeta_inverse(a, b, p, pol); Chris@16: y = 1 - x; Chris@16: } Chris@16: else Chris@16: { Chris@16: T r = a + b; Chris@16: T theta = asin(sqrt(a / r)); Chris@16: T lambda = minv / r; Chris@16: if((lambda >= 0.2) && (lambda <= 0.8) && (r >= 10)) Chris@16: { Chris@16: // Chris@16: // The second error function case is the next cheapest Chris@16: // to use, it brakes down when the result is likely to be Chris@16: // very small, if a+b is also small, but we can use a Chris@16: // cheaper expansion there in any case. As before x won't Chris@16: // be much larger than p, so as long as p is small we should Chris@16: // be free of cancellation error. Chris@16: // Chris@16: T ppa = pow(p, 1/a); Chris@16: if((ppa < 0.0025) && (a + b < 200)) Chris@16: { Chris@16: x = ppa * pow(a * boost::math::beta(a, b, pol), 1/a); Chris@16: } Chris@16: else Chris@16: x = temme_method_2_ibeta_inverse(a, b, p, r, theta, pol); Chris@16: y = 1 - x; Chris@16: } Chris@16: else Chris@16: { Chris@16: // Chris@16: // If we get here then a and b are very different in magnitude Chris@16: // and we need to use the third of Temme's methods which Chris@16: // involves inverting the incomplete gamma. This is much more Chris@16: // expensive than the other methods. We also can only use this Chris@16: // method when a > b, which can lead to cancellation errors Chris@16: // if we really want y (as we will when x is close to 1), so Chris@16: // a different expansion is used in that case. Chris@16: // Chris@16: if(a < b) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: invert = !invert; Chris@16: } Chris@16: // Chris@16: // Try and compute the easy way first: Chris@16: // Chris@16: T bet = 0; Chris@16: if(b < 2) Chris@16: bet = boost::math::beta(a, b, pol); Chris@16: if(bet != 0) Chris@16: { Chris@16: y = pow(b * q * bet, 1/b); Chris@16: x = 1 - y; Chris@16: } Chris@16: else Chris@16: y = 1; Chris@16: if(y > 1e-5) Chris@16: { Chris@16: x = temme_method_3_ibeta_inverse(a, b, p, q, pol); Chris@16: y = 1 - x; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: else if((a < 1) && (b < 1)) Chris@16: { Chris@16: // Chris@16: // Both a and b less than 1, Chris@16: // there is a point of inflection at xs: Chris@16: // Chris@16: T xs = (1 - a) / (2 - a - b); Chris@16: // Chris@16: // Now we need to ensure that we start our iteration from the Chris@16: // right side of the inflection point: Chris@16: // Chris@16: T fs = boost::math::ibeta(a, b, xs, pol) - p; Chris@16: if(fabs(fs) / p < tools::epsilon() * 3) Chris@16: { Chris@16: // The result is at the point of inflection, best just return it: Chris@16: *py = invert ? xs : 1 - xs; Chris@16: return invert ? 1-xs : xs; Chris@16: } Chris@16: if(fs < 0) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: invert = !invert; Chris@16: xs = 1 - xs; Chris@16: } Chris@16: T xg = pow(a * p * boost::math::beta(a, b, pol), 1/a); Chris@16: x = xg / (1 + xg); Chris@16: y = 1 / (1 + xg); Chris@16: // Chris@16: // And finally we know that our result is below the inflection Chris@16: // point, so set an upper limit on our search: Chris@16: // Chris@16: if(x > xs) Chris@16: x = xs; Chris@16: upper = xs; Chris@16: } Chris@16: else if((a > 1) && (b > 1)) Chris@16: { Chris@16: // Chris@16: // Small a and b, both greater than 1, Chris@16: // there is a point of inflection at xs, Chris@16: // and it's complement is xs2, we must always Chris@16: // start our iteration from the right side of the Chris@16: // point of inflection. Chris@16: // Chris@16: T xs = (a - 1) / (a + b - 2); Chris@16: T xs2 = (b - 1) / (a + b - 2); Chris@16: T ps = boost::math::ibeta(a, b, xs, pol) - p; Chris@16: Chris@16: if(ps < 0) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: std::swap(xs, xs2); Chris@16: invert = !invert; Chris@16: } Chris@16: // Chris@16: // Estimate x and y, using expm1 to get a good estimate Chris@16: // for y when it's very small: Chris@16: // Chris@16: T lx = log(p * a * boost::math::beta(a, b, pol)) / a; Chris@16: x = exp(lx); Chris@16: y = x < 0.9 ? T(1 - x) : (T)(-boost::math::expm1(lx, pol)); Chris@16: Chris@16: if((b < a) && (x < 0.2)) Chris@16: { Chris@16: // Chris@16: // Under a limited range of circumstances we can improve Chris@16: // our estimate for x, frankly it's clear if this has much effect! Chris@16: // Chris@16: T ap1 = a - 1; Chris@16: T bm1 = b - 1; Chris@16: T a_2 = a * a; Chris@16: T a_3 = a * a_2; Chris@16: T b_2 = b * b; Chris@16: T terms[5] = { 0, 1 }; Chris@16: terms[2] = bm1 / ap1; Chris@16: ap1 *= ap1; Chris@16: terms[3] = bm1 * (3 * a * b + 5 * b + a_2 - a - 4) / (2 * (a + 2) * ap1); Chris@16: ap1 *= (a + 1); Chris@16: terms[4] = bm1 * (33 * a * b_2 + 31 * b_2 + 8 * a_2 * b_2 - 30 * a * b - 47 * b + 11 * a_2 * b + 6 * a_3 * b + 18 + 4 * a - a_3 + a_2 * a_2 - 10 * a_2) Chris@16: / (3 * (a + 3) * (a + 2) * ap1); Chris@16: x = tools::evaluate_polynomial(terms, x, 5); Chris@16: } Chris@16: // Chris@16: // And finally we know that our result is below the inflection Chris@16: // point, so set an upper limit on our search: Chris@16: // Chris@16: if(x > xs) Chris@16: x = xs; Chris@16: upper = xs; Chris@16: } Chris@16: else /*if((a <= 1) != (b <= 1))*/ Chris@16: { Chris@16: // Chris@16: // If all else fails we get here, only one of a and b Chris@16: // is above 1, and a+b is small. Start by swapping Chris@16: // things around so that we have a concave curve with b > a Chris@16: // and no points of inflection in [0,1]. As long as we expect Chris@16: // x to be small then we can use the simple (and cheap) power Chris@16: // term to estimate x, but when we expect x to be large then Chris@16: // this greatly underestimates x and leaves us trying to Chris@16: // iterate "round the corner" which may take almost forever... Chris@16: // Chris@16: // We could use Temme's inverse gamma function case in that case, Chris@16: // this works really rather well (albeit expensively) even though Chris@16: // strictly speaking we're outside it's defined range. Chris@16: // Chris@16: // However it's expensive to compute, and an alternative approach Chris@16: // which models the curve as a distorted quarter circle is much Chris@16: // cheaper to compute, and still keeps the number of iterations Chris@16: // required down to a reasonable level. With thanks to Prof Temme Chris@16: // for this suggestion. Chris@16: // Chris@16: if(b < a) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: invert = !invert; Chris@16: } Chris@16: if(pow(p, 1/a) < 0.5) Chris@16: { Chris@16: x = pow(p * a * boost::math::beta(a, b, pol), 1 / a); Chris@16: if(x == 0) Chris@16: x = boost::math::tools::min_value(); Chris@16: y = 1 - x; Chris@16: } Chris@16: else /*if(pow(q, 1/b) < 0.1)*/ Chris@16: { Chris@16: // model a distorted quarter circle: Chris@16: y = pow(1 - pow(p, b * boost::math::beta(a, b, pol)), 1/b); Chris@16: if(y == 0) Chris@16: y = boost::math::tools::min_value(); Chris@16: x = 1 - y; Chris@16: } Chris@16: } Chris@16: Chris@16: // Chris@16: // Now we have a guess for x (and for y) we can set things up for Chris@16: // iteration. If x > 0.5 it pays to swap things round: Chris@16: // Chris@16: if(x > 0.5) Chris@16: { Chris@16: std::swap(a, b); Chris@16: std::swap(p, q); Chris@16: std::swap(x, y); Chris@16: invert = !invert; Chris@16: T l = 1 - upper; Chris@16: T u = 1 - lower; Chris@16: lower = l; Chris@16: upper = u; Chris@16: } Chris@16: // Chris@16: // lower bound for our search: Chris@16: // Chris@16: // We're not interested in denormalised answers as these tend to Chris@16: // these tend to take up lots of iterations, given that we can't get Chris@16: // accurate derivatives in this area (they tend to be infinite). Chris@16: // Chris@16: if(lower == 0) Chris@16: { Chris@16: if(invert && (py == 0)) Chris@16: { Chris@16: // Chris@16: // We're not interested in answers smaller than machine epsilon: Chris@16: // Chris@16: lower = boost::math::tools::epsilon(); Chris@16: if(x < lower) Chris@16: x = lower; Chris@16: } Chris@16: else Chris@16: lower = boost::math::tools::min_value(); Chris@16: if(x < lower) Chris@16: x = lower; Chris@16: } Chris@16: // Chris@16: // Figure out how many digits to iterate towards: Chris@16: // Chris@16: int digits = boost::math::policies::digits() / 2; Chris@16: if((x < 1e-50) && ((a < 1) || (b < 1))) Chris@16: { Chris@16: // Chris@16: // If we're in a region where the first derivative is very Chris@16: // large, then we have to take care that the root-finder Chris@16: // doesn't terminate prematurely. We'll bump the precision Chris@16: // up to avoid this, but we have to take care not to set the Chris@16: // precision too high or the last few iterations will just Chris@16: // thrash around and convergence may be slow in this case. Chris@16: // Try 3/4 of machine epsilon: Chris@16: // Chris@16: digits *= 3; Chris@16: digits /= 2; Chris@16: } Chris@16: // Chris@16: // Now iterate, we can use either p or q as the target here Chris@16: // depending on which is smaller: Chris@16: // Chris@16: boost::uintmax_t max_iter = policies::get_max_root_iterations(); Chris@16: x = boost::math::tools::halley_iterate( Chris@16: boost::math::detail::ibeta_roots(a, b, (p < q ? p : q), (p < q ? false : true)), x, lower, upper, digits, max_iter); Chris@16: policies::check_root_iterations("boost::math::ibeta<%1%>(%1%, %1%, %1%)", max_iter, pol); Chris@16: // Chris@16: // We don't really want these asserts here, but they are useful for sanity Chris@16: // checking that we have the limits right, uncomment if you suspect bugs *only*. Chris@16: // Chris@16: //BOOST_ASSERT(x != upper); Chris@16: //BOOST_ASSERT((x != lower) || (x == boost::math::tools::min_value()) || (x == boost::math::tools::epsilon())); Chris@16: // Chris@16: // Tidy up, if we "lower" was too high then zero is the best answer we have: Chris@16: // Chris@16: if(x == lower) Chris@16: x = 0; Chris@16: if(py) Chris@16: *py = invert ? x : 1 - x; Chris@16: return invert ? 1-x : x; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibeta_inv(T1 a, T2 b, T3 p, T4* py, const Policy& pol) Chris@16: { Chris@16: static const char* function = "boost::math::ibeta_inv<%1%>(%1%,%1%,%1%)"; Chris@16: BOOST_FPU_EXCEPTION_GUARD Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: typedef typename policies::normalise< Chris@16: Policy, Chris@16: policies::promote_float, Chris@16: policies::promote_double, Chris@16: policies::discrete_quantile<>, Chris@16: policies::assert_undefined<> >::type forwarding_policy; Chris@16: Chris@16: if(a <= 0) Chris@16: return policies::raise_domain_error(function, "The argument a to the incomplete beta function inverse must be greater than zero (got a=%1%).", a, pol); Chris@16: if(b <= 0) Chris@16: return policies::raise_domain_error(function, "The argument b to the incomplete beta function inverse must be greater than zero (got b=%1%).", b, pol); Chris@16: if((p < 0) || (p > 1)) Chris@16: return policies::raise_domain_error(function, "Argument p outside the range [0,1] in the incomplete beta function inverse (got p=%1%).", p, pol); Chris@16: Chris@16: value_type rx, ry; Chris@16: Chris@16: rx = detail::ibeta_inv_imp( Chris@16: static_cast(a), Chris@16: static_cast(b), Chris@16: static_cast(p), Chris@16: static_cast(1 - p), Chris@16: forwarding_policy(), &ry); Chris@16: Chris@16: if(py) *py = policies::checked_narrowing_cast(ry, function); Chris@16: return policies::checked_narrowing_cast(rx, function); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibeta_inv(T1 a, T2 b, T3 p, T4* py) Chris@16: { Chris@16: return ibeta_inv(a, b, p, py, policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibeta_inv(T1 a, T2 b, T3 p) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return ibeta_inv(a, b, p, static_cast(0), policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibeta_inv(T1 a, T2 b, T3 p, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return ibeta_inv(a, b, p, static_cast(0), pol); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibetac_inv(T1 a, T2 b, T3 q, T4* py, const Policy& pol) Chris@16: { Chris@16: static const char* function = "boost::math::ibetac_inv<%1%>(%1%,%1%,%1%)"; Chris@16: BOOST_FPU_EXCEPTION_GUARD Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: typedef typename policies::evaluation::type value_type; Chris@16: typedef typename policies::normalise< Chris@16: Policy, Chris@16: policies::promote_float, Chris@16: policies::promote_double, Chris@16: policies::discrete_quantile<>, Chris@16: policies::assert_undefined<> >::type forwarding_policy; Chris@16: Chris@16: if(a <= 0) Chris@101: return policies::raise_domain_error(function, "The argument a to the incomplete beta function inverse must be greater than zero (got a=%1%).", a, pol); Chris@16: if(b <= 0) Chris@101: return policies::raise_domain_error(function, "The argument b to the incomplete beta function inverse must be greater than zero (got b=%1%).", b, pol); Chris@16: if((q < 0) || (q > 1)) Chris@101: return policies::raise_domain_error(function, "Argument q outside the range [0,1] in the incomplete beta function inverse (got q=%1%).", q, pol); Chris@16: Chris@16: value_type rx, ry; Chris@16: Chris@16: rx = detail::ibeta_inv_imp( Chris@16: static_cast(a), Chris@16: static_cast(b), Chris@16: static_cast(1 - q), Chris@16: static_cast(q), Chris@16: forwarding_policy(), &ry); Chris@16: Chris@16: if(py) *py = policies::checked_narrowing_cast(ry, function); Chris@16: return policies::checked_narrowing_cast(rx, function); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibetac_inv(T1 a, T2 b, T3 q, T4* py) Chris@16: { Chris@16: return ibetac_inv(a, b, q, py, policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibetac_inv(RT1 a, RT2 b, RT3 q) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return ibetac_inv(a, b, q, static_cast(0), policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename tools::promote_args::type Chris@16: ibetac_inv(RT1 a, RT2 b, RT3 q, const Policy& pol) Chris@16: { Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: return ibetac_inv(a, b, q, static_cast(0), pol); Chris@16: } Chris@16: Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_MATH_SPECIAL_FUNCTIONS_IGAMMA_INVERSE_HPP Chris@16: Chris@16: Chris@16: Chris@16: