Boost-Geometry-Utils
view release on metacpan or search on metacpan
src/boost/math/special_functions/beta.hpp view on Meta::CPAN
// Series approximation to the incomplete beta:
//
template <class T>
struct ibeta_series_t
{
typedef T result_type;
ibeta_series_t(T a_, T b_, T x_, T mult) : result(mult), x(x_), apn(a_), poch(1-b_), n(1) {}
T operator()()
{
T r = result / apn;
apn += 1;
result *= poch * x / n;
++n;
poch += 1;
return r;
}
private:
T result, x, apn, poch;
int n;
};
template <class T, class Lanczos, class Policy>
T ibeta_series(T a, T b, T x, T s0, const Lanczos&, bool normalised, T* p_derivative, T y, const Policy& pol)
{
BOOST_MATH_STD_USING
T result;
BOOST_ASSERT((p_derivative == 0) || normalised);
if(normalised)
{
T c = a + b;
// incomplete beta power term, combined with the Lanczos approximation:
T agh = a + Lanczos::g() - T(0.5);
T bgh = b + Lanczos::g() - T(0.5);
T cgh = c + Lanczos::g() - T(0.5);
result = Lanczos::lanczos_sum_expG_scaled(c) / (Lanczos::lanczos_sum_expG_scaled(a) * Lanczos::lanczos_sum_expG_scaled(b));
if(a * b < bgh * 10)
result *= exp((b - 0.5f) * boost::math::log1p(a / bgh, pol));
else
result *= pow(cgh / bgh, b - 0.5f);
result *= pow(x * cgh / agh, a);
result *= sqrt(agh / boost::math::constants::e<T>());
if(p_derivative)
{
*p_derivative = result * pow(y, b);
BOOST_ASSERT(*p_derivative >= 0);
}
}
else
{
// Non-normalised, just compute the power:
result = pow(x, a);
}
if(result < tools::min_value<T>())
return s0; // Safeguard: series can't cope with denorms.
ibeta_series_t<T> s(a, b, x, result);
boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, s0);
policies::check_series_iterations<T>("boost::math::ibeta<%1%>(%1%, %1%, %1%) in ibeta_series (with lanczos)", max_iter, pol);
return result;
}
//
// Incomplete Beta series again, this time without Lanczos support:
//
template <class T, class Policy>
T ibeta_series(T a, T b, T x, T s0, const boost::math::lanczos::undefined_lanczos&, bool normalised, T* p_derivative, T y, const Policy& pol)
{
BOOST_MATH_STD_USING
T result;
BOOST_ASSERT((p_derivative == 0) || normalised);
if(normalised)
{
T c = a + b;
// figure out integration limits for the gamma function:
//T la = (std::max)(T(10), a);
//T lb = (std::max)(T(10), b);
//T lc = (std::max)(T(10), a+b);
T la = a + 5;
T lb = b + 5;
T lc = a + b + 5;
// calculate the gamma parts:
T sa = detail::lower_gamma_series(a, la, pol) / a;
sa += detail::upper_gamma_fraction(a, la, ::boost::math::policies::get_epsilon<T, Policy>());
T sb = detail::lower_gamma_series(b, lb, pol) / b;
sb += detail::upper_gamma_fraction(b, lb, ::boost::math::policies::get_epsilon<T, Policy>());
T sc = detail::lower_gamma_series(c, lc, pol) / c;
sc += detail::upper_gamma_fraction(c, lc, ::boost::math::policies::get_epsilon<T, Policy>());
// and their combined power-terms:
T b1 = (x * lc) / la;
T b2 = lc/lb;
T e1 = lc - la - lb;
T lb1 = a * log(b1);
T lb2 = b * log(b2);
if((lb1 >= tools::log_max_value<T>())
|| (lb1 <= tools::log_min_value<T>())
|| (lb2 >= tools::log_max_value<T>())
|| (lb2 <= tools::log_min_value<T>())
|| (e1 >= tools::log_max_value<T>())
|| (e1 <= tools::log_min_value<T>()) )
{
T p = lb1 + lb2 - e1;
result = exp(p);
}
else
{
result = pow(b1, a);
if(a * b < lb * 10)
result *= exp(b * boost::math::log1p(a / lb, pol));
else
result *= pow(b2, b);
result /= exp(e1);
}
// and combine the results:
result /= sa * sb / sc;
if(p_derivative)
{
*p_derivative = result * pow(y, b);
BOOST_ASSERT(*p_derivative >= 0);
}
}
else
{
// Non-normalised, just compute the power:
result = pow(x, a);
}
if(result < tools::min_value<T>())
return s0; // Safeguard: series can't cope with denorms.
ibeta_series_t<T> s(a, b, x, result);
boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, s0);
policies::check_series_iterations<T>("boost::math::ibeta<%1%>(%1%, %1%, %1%) in ibeta_series (without lanczos)", max_iter, pol);
return result;
}
//
// Continued fraction for the incomplete beta:
//
template <class T>
struct ibeta_fraction2_t
{
typedef std::pair<T, T> result_type;
ibeta_fraction2_t(T a_, T b_, T x_, T y_) : a(a_), b(b_), x(x_), y(y_), m(0) {}
result_type operator()()
{
T aN = (a + m - 1) * (a + b + m - 1) * m * (b - m) * x * x;
T denom = (a + 2 * m - 1);
aN /= denom * denom;
T bN = m;
bN += (m * (b - m) * x) / (a + 2*m - 1);
bN += ((a + m) * (a * y - b * x + 1 + m *(2 - x))) / (a + 2*m + 1);
++m;
return std::make_pair(aN, bN);
}
private:
T a, b, x, y;
int m;
};
//
// Evaluate the incomplete beta via the continued fraction representation:
//
template <class T, class Policy>
inline T ibeta_fraction2(T a, T b, T x, T y, const Policy& pol, bool normalised, T* p_derivative)
{
typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
BOOST_MATH_STD_USING
T result = ibeta_power_terms(a, b, x, y, lanczos_type(), normalised, pol);
if(p_derivative)
{
*p_derivative = result;
BOOST_ASSERT(*p_derivative >= 0);
}
if(result == 0)
return result;
ibeta_fraction2_t<T> f(a, b, x, y);
T fract = boost::math::tools::continued_fraction_b(f, boost::math::policies::get_epsilon<T, Policy>());
BOOST_MATH_INSTRUMENT_VARIABLE(fract);
BOOST_MATH_INSTRUMENT_VARIABLE(result);
return result / fract;
}
//
// Computes the difference between ibeta(a,b,x) and ibeta(a+k,b,x):
//
template <class T, class Policy>
T ibeta_a_step(T a, T b, T x, T y, int k, const Policy& pol, bool normalised, T* p_derivative)
src/boost/math/special_functions/beta.hpp view on Meta::CPAN
//
// This is DiDonato and Morris's BGRAT routine, see Eq's 9 through 9.6.
//
// Some values we'll need later, these are Eq 9.1:
//
T bm1 = b - 1;
T t = a + bm1 / 2;
T lx, u;
if(y < 0.35)
lx = boost::math::log1p(-y, pol);
else
lx = log(x);
u = -t * lx;
// and from from 9.2:
T prefix;
T h = regularised_gamma_prefix(b, u, pol, lanczos_type());
if(h <= tools::min_value<T>())
return s0;
if(normalised)
{
prefix = h / boost::math::tgamma_delta_ratio(a, b, pol);
prefix /= pow(t, b);
}
else
{
prefix = full_igamma_prefix(b, u, pol) / pow(t, b);
}
prefix *= mult;
//
// now we need the quantity Pn, unfortunatately this is computed
// recursively, and requires a full history of all the previous values
// so no choice but to declare a big table and hope it's big enough...
//
T p[ ::boost::math::detail::Pn_size<T>::value ] = { 1 }; // see 9.3.
//
// Now an initial value for J, see 9.6:
//
T j = boost::math::gamma_q(b, u, pol) / h;
//
// Now we can start to pull things together and evaluate the sum in Eq 9:
//
T sum = s0 + prefix * j; // Value at N = 0
// some variables we'll need:
unsigned tnp1 = 1; // 2*N+1
T lx2 = lx / 2;
lx2 *= lx2;
T lxp = 1;
T t4 = 4 * t * t;
T b2n = b;
for(unsigned n = 1; n < sizeof(p)/sizeof(p[0]); ++n)
{
/*
// debugging code, enable this if you want to determine whether
// the table of Pn's is large enough...
//
static int max_count = 2;
if(n > max_count)
{
max_count = n;
std::cerr << "Max iterations in BGRAT was " << n << std::endl;
}
*/
//
// begin by evaluating the next Pn from Eq 9.4:
//
tnp1 += 2;
p[n] = 0;
T mbn = b - n;
unsigned tmp1 = 3;
for(unsigned m = 1; m < n; ++m)
{
mbn = m * b - n;
p[n] += mbn * p[n-m] / boost::math::unchecked_factorial<T>(tmp1);
tmp1 += 2;
}
p[n] /= n;
p[n] += bm1 / boost::math::unchecked_factorial<T>(tnp1);
//
// Now we want Jn from Jn-1 using Eq 9.6:
//
j = (b2n * (b2n + 1) * j + (u + b2n + 1) * lxp) / t4;
lxp *= lx2;
b2n += 2;
//
// pull it together with Eq 9:
//
T r = prefix * p[n] * j;
sum += r;
if(r > 1)
{
if(fabs(r) < fabs(tools::epsilon<T>() * sum))
break;
}
else
{
if(fabs(r / tools::epsilon<T>()) < fabs(sum))
break;
}
}
return sum;
} // template <class T, class Lanczos>T beta_small_b_large_a_series(T a, T b, T x, T y, T s0, T mult, const Lanczos& l, bool normalised)
//
// For integer arguments we can relate the incomplete beta to the
// complement of the binomial distribution cdf and use this finite sum.
//
template <class T>
inline T binomial_ccdf(T n, T k, T x, T y)
{
BOOST_MATH_STD_USING // ADL of std names
T result = pow(x, n);
T term = result;
for(unsigned i = itrunc(T(n - 1)); i > k; --i)
{
term *= ((i + 1) * y) / ((n - i) * x) ;
result += term;
}
return result;
}
( run in 0.856 second using v1.01-cache-2.11-cpan-96521ef73a4 )