mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Merged in mfigurnov/eigen/gamma-der-a (pull request PR-403)
Derivative of the incomplete Gamma function and the sample of a Gamma random variable Approved-by: Benoit Steiner <benoit.steiner.goog@gmail.com>
This commit is contained in:
@@ -33,6 +33,48 @@ igamma(const Eigen::ArrayBase<Derived>& a, const Eigen::ArrayBase<ExponentDerive
|
||||
);
|
||||
}
|
||||
|
||||
/** \cpp11 \returns an expression of the coefficient-wise igamma_der_a(\a a, \a x) to the given arrays.
|
||||
*
|
||||
* This function computes the coefficient-wise derivative of the incomplete
|
||||
* gamma function with respect to the parameter a.
|
||||
*
|
||||
* \note This function supports only float and double scalar types in c++11
|
||||
* mode. To support other scalar types,
|
||||
* or float/double in non c++11 mode, the user has to provide implementations
|
||||
* of igamma_der_a(T,T) for any scalar
|
||||
* type T to be supported.
|
||||
*
|
||||
* \sa Eigen::igamma(), Eigen::lgamma()
|
||||
*/
|
||||
template <typename Derived, typename ExponentDerived>
|
||||
inline const Eigen::CwiseBinaryOp<Eigen::internal::scalar_igamma_der_a_op<typename Derived::Scalar>, const Derived, const ExponentDerived>
|
||||
igamma_der_a(const Eigen::ArrayBase<Derived>& a, const Eigen::ArrayBase<ExponentDerived>& x) {
|
||||
return Eigen::CwiseBinaryOp<Eigen::internal::scalar_igamma_der_a_op<typename Derived::Scalar>, const Derived, const ExponentDerived>(
|
||||
a.derived(),
|
||||
x.derived());
|
||||
}
|
||||
|
||||
/** \cpp11 \returns an expression of the coefficient-wise gamma_sample_der_alpha(\a alpha, \a sample) to the given arrays.
|
||||
*
|
||||
* This function computes the coefficient-wise derivative of the sample
|
||||
* of a Gamma(alpha, 1) random variable with respect to the parameter alpha.
|
||||
*
|
||||
* \note This function supports only float and double scalar types in c++11
|
||||
* mode. To support other scalar types,
|
||||
* or float/double in non c++11 mode, the user has to provide implementations
|
||||
* of gamma_sample_der_alpha(T,T) for any scalar
|
||||
* type T to be supported.
|
||||
*
|
||||
* \sa Eigen::igamma(), Eigen::lgamma()
|
||||
*/
|
||||
template <typename AlphaDerived, typename SampleDerived>
|
||||
inline const Eigen::CwiseBinaryOp<Eigen::internal::scalar_gamma_sample_der_alpha_op<typename AlphaDerived::Scalar>, const AlphaDerived, const SampleDerived>
|
||||
gamma_sample_der_alpha(const Eigen::ArrayBase<AlphaDerived>& alpha, const Eigen::ArrayBase<SampleDerived>& sample) {
|
||||
return Eigen::CwiseBinaryOp<Eigen::internal::scalar_gamma_sample_der_alpha_op<typename AlphaDerived::Scalar>, const AlphaDerived, const SampleDerived>(
|
||||
alpha.derived(),
|
||||
sample.derived());
|
||||
}
|
||||
|
||||
/** \cpp11 \returns an expression of the coefficient-wise igammac(\a a, \a x) to the given arrays.
|
||||
*
|
||||
* This function computes the coefficient-wise complementary incomplete gamma function.
|
||||
|
||||
@@ -41,6 +41,60 @@ struct functor_traits<scalar_igamma_op<Scalar> > {
|
||||
};
|
||||
};
|
||||
|
||||
/** \internal
|
||||
* \brief Template functor to compute the derivative of the incomplete gamma
|
||||
* function igamma_der_a(a, x)
|
||||
*
|
||||
* \sa class CwiseBinaryOp, Cwise::igamma_der_a
|
||||
*/
|
||||
template <typename Scalar>
|
||||
struct scalar_igamma_der_a_op {
|
||||
EIGEN_EMPTY_STRUCT_CTOR(scalar_igamma_der_a_op)
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(const Scalar& a, const Scalar& x) const {
|
||||
using numext::igamma_der_a;
|
||||
return igamma_der_a(a, x);
|
||||
}
|
||||
template <typename Packet>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& x) const {
|
||||
return internal::pigamma_der_a(a, x);
|
||||
}
|
||||
};
|
||||
template <typename Scalar>
|
||||
struct functor_traits<scalar_igamma_der_a_op<Scalar> > {
|
||||
enum {
|
||||
// 2x the cost of igamma
|
||||
Cost = 40 * NumTraits<Scalar>::MulCost + 20 * NumTraits<Scalar>::AddCost,
|
||||
PacketAccess = packet_traits<Scalar>::HasIGammaDerA
|
||||
};
|
||||
};
|
||||
|
||||
/** \internal
|
||||
* \brief Template functor to compute the derivative of the sample
|
||||
* of a Gamma(alpha, 1) random variable with respect to the parameter alpha
|
||||
* gamma_sample_der_alpha(alpha, sample)
|
||||
*
|
||||
* \sa class CwiseBinaryOp, Cwise::gamma_sample_der_alpha
|
||||
*/
|
||||
template <typename Scalar>
|
||||
struct scalar_gamma_sample_der_alpha_op {
|
||||
EIGEN_EMPTY_STRUCT_CTOR(scalar_gamma_sample_der_alpha_op)
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(const Scalar& alpha, const Scalar& sample) const {
|
||||
using numext::gamma_sample_der_alpha;
|
||||
return gamma_sample_der_alpha(alpha, sample);
|
||||
}
|
||||
template <typename Packet>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& alpha, const Packet& sample) const {
|
||||
return internal::pgamma_sample_der_alpha(alpha, sample);
|
||||
}
|
||||
};
|
||||
template <typename Scalar>
|
||||
struct functor_traits<scalar_gamma_sample_der_alpha_op<Scalar> > {
|
||||
enum {
|
||||
// 2x the cost of igamma, minus the lgamma cost (the lgamma cancels out)
|
||||
Cost = 30 * NumTraits<Scalar>::MulCost + 15 * NumTraits<Scalar>::AddCost,
|
||||
PacketAccess = packet_traits<Scalar>::HasGammaSampleDerAlpha
|
||||
};
|
||||
};
|
||||
|
||||
/** \internal
|
||||
* \brief Template functor to compute the complementary incomplete gamma function igammac(a, x)
|
||||
|
||||
@@ -33,6 +33,14 @@ template<> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half erfc(const Eigen::h
|
||||
template<> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half igamma(const Eigen::half& a, const Eigen::half& x) {
|
||||
return Eigen::half(Eigen::numext::igamma(static_cast<float>(a), static_cast<float>(x)));
|
||||
}
|
||||
template <>
|
||||
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half igamma_der_a(const Eigen::half& a, const Eigen::half& x) {
|
||||
return Eigen::half(Eigen::numext::igamma_der_a(static_cast<float>(a), static_cast<float>(x)));
|
||||
}
|
||||
template <>
|
||||
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half gamma_sample_der_alpha(const Eigen::half& alpha, const Eigen::half& sample) {
|
||||
return Eigen::half(Eigen::numext::gamma_sample_der_alpha(static_cast<float>(alpha), static_cast<float>(sample)));
|
||||
}
|
||||
template<> EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half igammac(const Eigen::half& a, const Eigen::half& x) {
|
||||
return Eigen::half(Eigen::numext::igammac(static_cast<float>(a), static_cast<float>(x)));
|
||||
}
|
||||
|
||||
@@ -521,6 +521,197 @@ struct cephes_helper<double> {
|
||||
}
|
||||
};
|
||||
|
||||
enum IgammaComputationMode { VALUE, DERIVATIVE, SAMPLE_DERIVATIVE };
|
||||
|
||||
template <typename Scalar, IgammaComputationMode mode>
|
||||
EIGEN_DEVICE_FUNC
|
||||
int igamma_num_iterations() {
|
||||
/* Returns the maximum number of internal iterations for igamma computation.
|
||||
*/
|
||||
if (mode == VALUE) {
|
||||
return 2000;
|
||||
}
|
||||
|
||||
if (internal::is_same<Scalar, float>::value) {
|
||||
return 200;
|
||||
} else if (internal::is_same<Scalar, double>::value) {
|
||||
return 500;
|
||||
} else {
|
||||
return 2000;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Scalar, IgammaComputationMode mode>
|
||||
struct igammac_cf_impl {
|
||||
/* Computes igamc(a, x) or derivative (depending on the mode)
|
||||
* using the continued fraction expansion of the complementary
|
||||
* incomplete Gamma function.
|
||||
*
|
||||
* Preconditions:
|
||||
* a > 0
|
||||
* x >= 1
|
||||
* x >= a
|
||||
*/
|
||||
EIGEN_DEVICE_FUNC
|
||||
static Scalar run(Scalar a, Scalar x) {
|
||||
const Scalar zero = 0;
|
||||
const Scalar one = 1;
|
||||
const Scalar two = 2;
|
||||
const Scalar machep = cephes_helper<Scalar>::machep();
|
||||
const Scalar big = cephes_helper<Scalar>::big();
|
||||
const Scalar biginv = cephes_helper<Scalar>::biginv();
|
||||
|
||||
if ((numext::isinf)(x)) {
|
||||
return zero;
|
||||
}
|
||||
|
||||
// continued fraction
|
||||
Scalar y = one - a;
|
||||
Scalar z = x + y + one;
|
||||
Scalar c = zero;
|
||||
Scalar pkm2 = one;
|
||||
Scalar qkm2 = x;
|
||||
Scalar pkm1 = x + one;
|
||||
Scalar qkm1 = z * x;
|
||||
Scalar ans = pkm1 / qkm1;
|
||||
|
||||
Scalar dpkm2_da = zero;
|
||||
Scalar dqkm2_da = zero;
|
||||
Scalar dpkm1_da = zero;
|
||||
Scalar dqkm1_da = -x;
|
||||
Scalar dans_da = (dpkm1_da - ans * dqkm1_da) / qkm1;
|
||||
|
||||
for (int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
|
||||
c += one;
|
||||
y += one;
|
||||
z += two;
|
||||
|
||||
Scalar yc = y * c;
|
||||
Scalar pk = pkm1 * z - pkm2 * yc;
|
||||
Scalar qk = qkm1 * z - qkm2 * yc;
|
||||
|
||||
Scalar dpk_da = dpkm1_da * z - pkm1 - dpkm2_da * yc + pkm2 * c;
|
||||
Scalar dqk_da = dqkm1_da * z - qkm1 - dqkm2_da * yc + qkm2 * c;
|
||||
|
||||
if (qk != zero) {
|
||||
Scalar ans_prev = ans;
|
||||
ans = pk / qk;
|
||||
|
||||
Scalar dans_da_prev = dans_da;
|
||||
dans_da = (dpk_da - ans * dqk_da) / qk;
|
||||
|
||||
if (mode == VALUE) {
|
||||
if (numext::abs(ans_prev - ans) <= machep * numext::abs(ans)) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (numext::abs(dans_da - dans_da_prev) <= machep) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pkm2 = pkm1;
|
||||
pkm1 = pk;
|
||||
qkm2 = qkm1;
|
||||
qkm1 = qk;
|
||||
|
||||
dpkm2_da = dpkm1_da;
|
||||
dpkm1_da = dpk_da;
|
||||
dqkm2_da = dqkm1_da;
|
||||
dqkm1_da = dqk_da;
|
||||
|
||||
if (numext::abs(pk) > big) {
|
||||
pkm2 *= biginv;
|
||||
pkm1 *= biginv;
|
||||
qkm2 *= biginv;
|
||||
qkm1 *= biginv;
|
||||
|
||||
dpkm2_da *= biginv;
|
||||
dpkm1_da *= biginv;
|
||||
dqkm2_da *= biginv;
|
||||
dqkm1_da *= biginv;
|
||||
}
|
||||
}
|
||||
|
||||
/* Compute x**a * exp(-x) / gamma(a) */
|
||||
Scalar logax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);
|
||||
Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a);
|
||||
Scalar ax = numext::exp(logax);
|
||||
Scalar dax_da = ax * dlogax_da;
|
||||
|
||||
switch (mode) {
|
||||
case VALUE:
|
||||
return ans * ax;
|
||||
case DERIVATIVE:
|
||||
return ans * dax_da + dans_da * ax;
|
||||
case SAMPLE_DERIVATIVE:
|
||||
return -(dans_da + ans * dlogax_da) * x;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Scalar, IgammaComputationMode mode>
|
||||
struct igamma_series_impl {
|
||||
/* Computes igam(a, x) or its derivative (depending on the mode)
|
||||
* using the series expansion of the incomplete Gamma function.
|
||||
*
|
||||
* Preconditions:
|
||||
* x > 0
|
||||
* a > 0
|
||||
* !(x > 1 && x > a)
|
||||
*/
|
||||
EIGEN_DEVICE_FUNC
|
||||
static Scalar run(Scalar a, Scalar x) {
|
||||
const Scalar zero = 0;
|
||||
const Scalar one = 1;
|
||||
const Scalar machep = cephes_helper<Scalar>::machep();
|
||||
|
||||
/* power series */
|
||||
Scalar r = a;
|
||||
Scalar c = one;
|
||||
Scalar ans = one;
|
||||
|
||||
Scalar dc_da = zero;
|
||||
Scalar dans_da = zero;
|
||||
|
||||
for (int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
|
||||
r += one;
|
||||
Scalar term = x / r;
|
||||
Scalar dterm_da = -x / (r * r);
|
||||
dc_da = term * dc_da + dterm_da * c;
|
||||
dans_da += dc_da;
|
||||
c *= term;
|
||||
ans += c;
|
||||
|
||||
if (mode == VALUE) {
|
||||
if (c <= machep * ans) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (numext::abs(dc_da) <= machep * numext::abs(dans_da)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Compute x**a * exp(-x) / gamma(a + 1) */
|
||||
Scalar logax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a + one);
|
||||
Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a + one);
|
||||
Scalar ax = numext::exp(logax);
|
||||
Scalar dax_da = ax * dlogax_da;
|
||||
|
||||
switch (mode) {
|
||||
case VALUE:
|
||||
return ans * ax;
|
||||
case DERIVATIVE:
|
||||
return ans * dax_da + dans_da * ax;
|
||||
case SAMPLE_DERIVATIVE:
|
||||
return -(dans_da + ans * dlogax_da) * x / a;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#if !EIGEN_HAS_C99_MATH
|
||||
|
||||
template <typename Scalar>
|
||||
@@ -535,8 +726,6 @@ struct igammac_impl {
|
||||
|
||||
#else
|
||||
|
||||
template <typename Scalar> struct igamma_impl; // predeclare igamma_impl
|
||||
|
||||
template <typename Scalar>
|
||||
struct igammac_impl {
|
||||
EIGEN_DEVICE_FUNC
|
||||
@@ -604,97 +793,15 @@ struct igammac_impl {
|
||||
return nan;
|
||||
}
|
||||
|
||||
if ((numext::isnan)(a) || (numext::isnan)(x)) { // propagate nans
|
||||
if ((numext::isnan)(a) || (numext::isnan)(x)) { // propagate nans
|
||||
return nan;
|
||||
}
|
||||
|
||||
if ((x < one) || (x < a)) {
|
||||
/* The checks above ensure that we meet the preconditions for
|
||||
* igamma_impl::Impl(), so call it, rather than igamma_impl::Run().
|
||||
* Calling Run() would also work, but in that case the compiler may not be
|
||||
* able to prove that igammac_impl::Run and igamma_impl::Run are not
|
||||
* mutually recursive. This leads to worse code, particularly on
|
||||
* platforms like nvptx, where recursion is allowed only begrudgingly.
|
||||
*/
|
||||
return (one - igamma_impl<Scalar>::Impl(a, x));
|
||||
return (one - igamma_series_impl<Scalar, VALUE>::run(a, x));
|
||||
}
|
||||
|
||||
return Impl(a, x);
|
||||
}
|
||||
|
||||
private:
|
||||
/* igamma_impl calls igammac_impl::Impl. */
|
||||
friend struct igamma_impl<Scalar>;
|
||||
|
||||
/* Actually computes igamc(a, x).
|
||||
*
|
||||
* Preconditions:
|
||||
* a > 0
|
||||
* x >= 1
|
||||
* x >= a
|
||||
*/
|
||||
EIGEN_DEVICE_FUNC static Scalar Impl(Scalar a, Scalar x) {
|
||||
const Scalar zero = 0;
|
||||
const Scalar one = 1;
|
||||
const Scalar two = 2;
|
||||
const Scalar machep = cephes_helper<Scalar>::machep();
|
||||
const Scalar maxlog = numext::log(NumTraits<Scalar>::highest());
|
||||
const Scalar big = cephes_helper<Scalar>::big();
|
||||
const Scalar biginv = cephes_helper<Scalar>::biginv();
|
||||
const Scalar inf = NumTraits<Scalar>::infinity();
|
||||
|
||||
Scalar ans, ax, c, yc, r, t, y, z;
|
||||
Scalar pk, pkm1, pkm2, qk, qkm1, qkm2;
|
||||
|
||||
if (x == inf) return zero; // std::isinf crashes on CUDA
|
||||
|
||||
/* Compute x**a * exp(-x) / gamma(a) */
|
||||
ax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);
|
||||
if (ax < -maxlog) { // underflow
|
||||
return zero;
|
||||
}
|
||||
ax = numext::exp(ax);
|
||||
|
||||
// continued fraction
|
||||
y = one - a;
|
||||
z = x + y + one;
|
||||
c = zero;
|
||||
pkm2 = one;
|
||||
qkm2 = x;
|
||||
pkm1 = x + one;
|
||||
qkm1 = z * x;
|
||||
ans = pkm1 / qkm1;
|
||||
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
c += one;
|
||||
y += one;
|
||||
z += two;
|
||||
yc = y * c;
|
||||
pk = pkm1 * z - pkm2 * yc;
|
||||
qk = qkm1 * z - qkm2 * yc;
|
||||
if (qk != zero) {
|
||||
r = pk / qk;
|
||||
t = numext::abs((ans - r) / r);
|
||||
ans = r;
|
||||
} else {
|
||||
t = one;
|
||||
}
|
||||
pkm2 = pkm1;
|
||||
pkm1 = pk;
|
||||
qkm2 = qkm1;
|
||||
qkm1 = qk;
|
||||
if (numext::abs(pk) > big) {
|
||||
pkm2 *= biginv;
|
||||
pkm1 *= biginv;
|
||||
qkm2 *= biginv;
|
||||
qkm1 *= biginv;
|
||||
}
|
||||
if (t <= machep) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (ans * ax);
|
||||
return igammac_cf_impl<Scalar, VALUE>::run(a, x);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -704,15 +811,10 @@ struct igammac_impl {
|
||||
* Implementation of igamma (incomplete gamma integral), based on Cephes but requires C++11/C99 *
|
||||
************************************************************************************************/
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_retval {
|
||||
typedef Scalar type;
|
||||
};
|
||||
|
||||
#if !EIGEN_HAS_C99_MATH
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_impl {
|
||||
template <typename Scalar, IgammaComputationMode mode>
|
||||
struct igamma_generic_impl {
|
||||
EIGEN_DEVICE_FUNC
|
||||
static EIGEN_STRONG_INLINE Scalar run(Scalar a, Scalar x) {
|
||||
EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
|
||||
@@ -723,69 +825,17 @@ struct igamma_impl {
|
||||
|
||||
#else
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_impl {
|
||||
template <typename Scalar, IgammaComputationMode mode>
|
||||
struct igamma_generic_impl {
|
||||
EIGEN_DEVICE_FUNC
|
||||
static Scalar run(Scalar a, Scalar x) {
|
||||
/* igam()
|
||||
* Incomplete gamma integral
|
||||
*
|
||||
*
|
||||
*
|
||||
* SYNOPSIS:
|
||||
*
|
||||
* double a, x, y, igam();
|
||||
*
|
||||
* y = igam( a, x );
|
||||
*
|
||||
* DESCRIPTION:
|
||||
*
|
||||
* The function is defined by
|
||||
*
|
||||
* x
|
||||
* -
|
||||
* 1 | | -t a-1
|
||||
* igam(a,x) = ----- | e t dt.
|
||||
* - | |
|
||||
* | (a) -
|
||||
* 0
|
||||
*
|
||||
*
|
||||
* In this implementation both arguments must be positive.
|
||||
* The integral is evaluated by either a power series or
|
||||
* continued fraction expansion, depending on the relative
|
||||
* values of a and x.
|
||||
*
|
||||
* ACCURACY (double):
|
||||
*
|
||||
* Relative error:
|
||||
* arithmetic domain # trials peak rms
|
||||
* IEEE 0,30 200000 3.6e-14 2.9e-15
|
||||
* IEEE 0,100 300000 9.9e-14 1.5e-14
|
||||
*
|
||||
*
|
||||
* ACCURACY (float):
|
||||
*
|
||||
* Relative error:
|
||||
* arithmetic domain # trials peak rms
|
||||
* IEEE 0,30 20000 7.8e-6 5.9e-7
|
||||
*
|
||||
*/
|
||||
/*
|
||||
Cephes Math Library Release 2.2: June, 1992
|
||||
Copyright 1985, 1987, 1992 by Stephen L. Moshier
|
||||
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
|
||||
*/
|
||||
|
||||
|
||||
/* left tail of incomplete gamma function:
|
||||
*
|
||||
* inf. k
|
||||
* a -x - x
|
||||
* x e > ----------
|
||||
* - -
|
||||
* k=0 | (a+k+1)
|
||||
/* Depending on the mode, returns
|
||||
* - VALUE: incomplete Gamma function igamma(a, x)
|
||||
* - DERIVATIVE: derivative of incomplete Gamma function d/da igamma(a, x)
|
||||
* - SAMPLE_DERIVATIVE: implicit derivative of a Gamma random variable
|
||||
* x ~ Gamma(x | a, 1), dx/da = -1 / Gamma(x | a, 1) * d igamma(a, x) / dx
|
||||
*
|
||||
* Derivatives are implemented by forward-mode differentiation.
|
||||
*/
|
||||
const Scalar zero = 0;
|
||||
const Scalar one = 1;
|
||||
@@ -797,71 +847,167 @@ struct igamma_impl {
|
||||
return nan;
|
||||
}
|
||||
|
||||
if ((numext::isnan)(a) || (numext::isnan)(x)) { // propagate nans
|
||||
if ((numext::isnan)(a) || (numext::isnan)(x)) { // propagate nans
|
||||
return nan;
|
||||
}
|
||||
|
||||
if ((x > one) && (x > a)) {
|
||||
/* The checks above ensure that we meet the preconditions for
|
||||
* igammac_impl::Impl(), so call it, rather than igammac_impl::Run().
|
||||
* Calling Run() would also work, but in that case the compiler may not be
|
||||
* able to prove that igammac_impl::Run and igamma_impl::Run are not
|
||||
* mutually recursive. This leads to worse code, particularly on
|
||||
* platforms like nvptx, where recursion is allowed only begrudgingly.
|
||||
*/
|
||||
return (one - igammac_impl<Scalar>::Impl(a, x));
|
||||
}
|
||||
|
||||
return Impl(a, x);
|
||||
}
|
||||
|
||||
private:
|
||||
/* igammac_impl calls igamma_impl::Impl. */
|
||||
friend struct igammac_impl<Scalar>;
|
||||
|
||||
/* Actually computes igam(a, x).
|
||||
*
|
||||
* Preconditions:
|
||||
* x > 0
|
||||
* a > 0
|
||||
* !(x > 1 && x > a)
|
||||
*/
|
||||
EIGEN_DEVICE_FUNC static Scalar Impl(Scalar a, Scalar x) {
|
||||
const Scalar zero = 0;
|
||||
const Scalar one = 1;
|
||||
const Scalar machep = cephes_helper<Scalar>::machep();
|
||||
const Scalar maxlog = numext::log(NumTraits<Scalar>::highest());
|
||||
|
||||
Scalar ans, ax, c, r;
|
||||
|
||||
/* Compute x**a * exp(-x) / gamma(a) */
|
||||
ax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);
|
||||
if (ax < -maxlog) {
|
||||
// underflow
|
||||
return zero;
|
||||
}
|
||||
ax = numext::exp(ax);
|
||||
|
||||
/* power series */
|
||||
r = a;
|
||||
c = one;
|
||||
ans = one;
|
||||
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
r += one;
|
||||
c *= x/r;
|
||||
ans += c;
|
||||
if (c/ans <= machep) {
|
||||
break;
|
||||
Scalar ret = igammac_cf_impl<Scalar, mode>::run(a, x);
|
||||
if (mode == VALUE) {
|
||||
return one - ret;
|
||||
} else {
|
||||
return -ret;
|
||||
}
|
||||
}
|
||||
|
||||
return (ans * ax / a);
|
||||
return igamma_series_impl<Scalar, mode>::run(a, x);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // EIGEN_HAS_C99_MATH
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_retval {
|
||||
typedef Scalar type;
|
||||
};
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_impl : igamma_generic_impl<Scalar, VALUE> {
|
||||
/* igam()
|
||||
* Incomplete gamma integral.
|
||||
*
|
||||
* The CDF of Gamma(a, 1) random variable at the point x.
|
||||
*
|
||||
* Accuracy estimation. For each a in [10^-2, 10^-1...10^3] we sample
|
||||
* 50 Gamma random variables x ~ Gamma(x | a, 1), a total of 300 points.
|
||||
* The ground truth is computed by mpmath. Mean absolute error:
|
||||
* float: 1.26713e-05
|
||||
* double: 2.33606e-12
|
||||
*
|
||||
* Cephes documentation below.
|
||||
*
|
||||
* SYNOPSIS:
|
||||
*
|
||||
* double a, x, y, igam();
|
||||
*
|
||||
* y = igam( a, x );
|
||||
*
|
||||
* DESCRIPTION:
|
||||
*
|
||||
* The function is defined by
|
||||
*
|
||||
* x
|
||||
* -
|
||||
* 1 | | -t a-1
|
||||
* igam(a,x) = ----- | e t dt.
|
||||
* - | |
|
||||
* | (a) -
|
||||
* 0
|
||||
*
|
||||
*
|
||||
* In this implementation both arguments must be positive.
|
||||
* The integral is evaluated by either a power series or
|
||||
* continued fraction expansion, depending on the relative
|
||||
* values of a and x.
|
||||
*
|
||||
* ACCURACY (double):
|
||||
*
|
||||
* Relative error:
|
||||
* arithmetic domain # trials peak rms
|
||||
* IEEE 0,30 200000 3.6e-14 2.9e-15
|
||||
* IEEE 0,100 300000 9.9e-14 1.5e-14
|
||||
*
|
||||
*
|
||||
* ACCURACY (float):
|
||||
*
|
||||
* Relative error:
|
||||
* arithmetic domain # trials peak rms
|
||||
* IEEE 0,30 20000 7.8e-6 5.9e-7
|
||||
*
|
||||
*/
|
||||
/*
|
||||
Cephes Math Library Release 2.2: June, 1992
|
||||
Copyright 1985, 1987, 1992 by Stephen L. Moshier
|
||||
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
|
||||
*/
|
||||
|
||||
/* left tail of incomplete gamma function:
|
||||
*
|
||||
* inf. k
|
||||
* a -x - x
|
||||
* x e > ----------
|
||||
* - -
|
||||
* k=0 | (a+k+1)
|
||||
*
|
||||
*/
|
||||
};
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_der_a_retval : igamma_retval<Scalar> {};
|
||||
|
||||
template <typename Scalar>
|
||||
struct igamma_der_a_impl : igamma_generic_impl<Scalar, DERIVATIVE> {
|
||||
/* Derivative of the incomplete Gamma function with respect to a.
|
||||
*
|
||||
* Computes d/da igamma(a, x) by forward differentiation of the igamma code.
|
||||
*
|
||||
* Accuracy estimation. For each a in [10^-2, 10^-1...10^3] we sample
|
||||
* 50 Gamma random variables x ~ Gamma(x | a, 1), a total of 300 points.
|
||||
* The ground truth is computed by mpmath. Mean absolute error:
|
||||
* float: 6.17992e-07
|
||||
* double: 4.60453e-12
|
||||
*
|
||||
* Reference:
|
||||
* R. Moore. "Algorithm AS 187: Derivatives of the incomplete gamma
|
||||
* integral". Journal of the Royal Statistical Society. 1982
|
||||
*/
|
||||
};
|
||||
|
||||
template <typename Scalar>
|
||||
struct gamma_sample_der_alpha_retval : igamma_retval<Scalar> {};
|
||||
|
||||
template <typename Scalar>
|
||||
struct gamma_sample_der_alpha_impl
|
||||
: igamma_generic_impl<Scalar, SAMPLE_DERIVATIVE> {
|
||||
/* Derivative of a Gamma random variable sample with respect to alpha.
|
||||
*
|
||||
* Consider a sample of a Gamma random variable with the concentration
|
||||
* parameter alpha: sample ~ Gamma(alpha, 1). The reparameterization
|
||||
* derivative that we want to compute is dsample / dalpha =
|
||||
* d igammainv(alpha, u) / dalpha, where u = igamma(alpha, sample).
|
||||
* However, this formula is numerically unstable and expensive, so instead
|
||||
* we use implicit differentiation:
|
||||
*
|
||||
* igamma(alpha, sample) = u, where u ~ Uniform(0, 1).
|
||||
* Apply d / dalpha to both sides:
|
||||
* d igamma(alpha, sample) / dalpha
|
||||
* + d igamma(alpha, sample) / dsample * dsample/dalpha = 0
|
||||
* d igamma(alpha, sample) / dalpha
|
||||
* + Gamma(sample | alpha, 1) dsample / dalpha = 0
|
||||
* dsample/dalpha = - (d igamma(alpha, sample) / dalpha)
|
||||
* / Gamma(sample | alpha, 1)
|
||||
*
|
||||
* Here Gamma(sample | alpha, 1) is the PDF of the Gamma distribution
|
||||
* (note that the derivative of the CDF w.r.t. sample is the PDF).
|
||||
* See the reference below for more details.
|
||||
*
|
||||
* The derivative of igamma(alpha, sample) is computed by forward
|
||||
* differentiation of the igamma code. Division by the Gamma PDF is performed
|
||||
* in the same code, increasing the accuracy and speed due to cancellation
|
||||
* of some terms.
|
||||
*
|
||||
* Accuracy estimation. For each alpha in [10^-2, 10^-1...10^3] we sample
|
||||
* 50 Gamma random variables sample ~ Gamma(sample | alpha, 1), a total of 300
|
||||
* points. The ground truth is computed by mpmath. Mean absolute error:
|
||||
* float: 2.1686e-06
|
||||
* double: 1.4774e-12
|
||||
*
|
||||
* Reference:
|
||||
* M. Figurnov, S. Mohamed, A. Mnih "Implicit Reparameterization Gradients".
|
||||
* 2018
|
||||
*/
|
||||
};
|
||||
|
||||
/*****************************************************************************
|
||||
* Implementation of Riemann zeta function of two arguments, based on Cephes *
|
||||
*****************************************************************************/
|
||||
@@ -1950,6 +2096,18 @@ EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(igamma, Scalar)
|
||||
return EIGEN_MATHFUNC_IMPL(igamma, Scalar)::run(a, x);
|
||||
}
|
||||
|
||||
template <typename Scalar>
|
||||
EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(igamma_der_a, Scalar)
|
||||
igamma_der_a(const Scalar& a, const Scalar& x) {
|
||||
return EIGEN_MATHFUNC_IMPL(igamma_der_a, Scalar)::run(a, x);
|
||||
}
|
||||
|
||||
template <typename Scalar>
|
||||
EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(gamma_sample_der_alpha, Scalar)
|
||||
gamma_sample_der_alpha(const Scalar& a, const Scalar& x) {
|
||||
return EIGEN_MATHFUNC_IMPL(gamma_sample_der_alpha, Scalar)::run(a, x);
|
||||
}
|
||||
|
||||
template <typename Scalar>
|
||||
EIGEN_DEVICE_FUNC inline EIGEN_MATHFUNC_RETVAL(igammac, Scalar)
|
||||
igammac(const Scalar& a, const Scalar& x) {
|
||||
|
||||
@@ -42,6 +42,21 @@ Packet perfc(const Packet& a) { using numext::erfc; return erfc(a); }
|
||||
template<typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
Packet pigamma(const Packet& a, const Packet& x) { using numext::igamma; return igamma(a, x); }
|
||||
|
||||
/** \internal \returns the derivative of the incomplete gamma function
|
||||
* igamma_der_a(\a a, \a x) */
|
||||
template <typename Packet>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pigamma_der_a(const Packet& a, const Packet& x) {
|
||||
using numext::igamma_der_a; return igamma_der_a(a, x);
|
||||
}
|
||||
|
||||
/** \internal \returns compute the derivative of the sample
|
||||
* of Gamma(alpha, 1) random variable with respect to the parameter a
|
||||
* gamma_sample_der_alpha(\a alpha, \a sample) */
|
||||
template <typename Packet>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet pgamma_sample_der_alpha(const Packet& alpha, const Packet& sample) {
|
||||
using numext::gamma_sample_der_alpha; return gamma_sample_der_alpha(alpha, sample);
|
||||
}
|
||||
|
||||
/** \internal \returns the complementary incomplete gamma function igammac(\a a, \a x) */
|
||||
template<typename Packet> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
Packet pigammac(const Packet& a, const Packet& x) { using numext::igammac; return igammac(a, x); }
|
||||
|
||||
@@ -120,6 +120,41 @@ double2 pigamma<double2>(const double2& a, const double2& x)
|
||||
return make_double2(igamma(a.x, x.x), igamma(a.y, x.y));
|
||||
}
|
||||
|
||||
template <>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float4 pigamma_der_a<float4>(
|
||||
const float4& a, const float4& x) {
|
||||
using numext::igamma_der_a;
|
||||
return make_float4(igamma_der_a(a.x, x.x), igamma_der_a(a.y, x.y),
|
||||
igamma_der_a(a.z, x.z), igamma_der_a(a.w, x.w));
|
||||
}
|
||||
|
||||
template <>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double2
|
||||
pigamma_der_a<double2>(const double2& a, const double2& x) {
|
||||
using numext::igamma_der_a;
|
||||
return make_double2(igamma_der_a(a.x, x.x), igamma_der_a(a.y, x.y));
|
||||
}
|
||||
|
||||
template <>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float4 pgamma_sample_der_alpha<float4>(
|
||||
const float4& alpha, const float4& sample) {
|
||||
using numext::gamma_sample_der_alpha;
|
||||
return make_float4(
|
||||
gamma_sample_der_alpha(alpha.x, sample.x),
|
||||
gamma_sample_der_alpha(alpha.y, sample.y),
|
||||
gamma_sample_der_alpha(alpha.z, sample.z),
|
||||
gamma_sample_der_alpha(alpha.w, sample.w));
|
||||
}
|
||||
|
||||
template <>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double2
|
||||
pgamma_sample_der_alpha<double2>(const double2& alpha, const double2& sample) {
|
||||
using numext::gamma_sample_der_alpha;
|
||||
return make_double2(
|
||||
gamma_sample_der_alpha(alpha.x, sample.x),
|
||||
gamma_sample_der_alpha(alpha.y, sample.y));
|
||||
}
|
||||
|
||||
template<> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
float4 pigammac<float4>(const float4& a, const float4& x)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user