src/pgm_density.c
/*
 * Approximate the logarithm of the density function of PG(h, z).
 *
 * logsumexp is applied as an attempt to prevent underflow. The sum of terms
 * is truncated at `PGM_MAX_SERIES_TERMS` terms.
 */
Approximate the logarithm of the density function of PG(h, z).
logsumexp is applied as an attempt to prevent underflow. The sum of terms
is truncated at `PGM_MAX_SERIES_TERMS` terms.
Approximate the logarithm of the density function of PG ( h z ) . logsumexp is applied as an attempt to prevent underflow . The sum of terms is truncated at ` PGM_MAX_SERIES_TERMS ` terms .
double
pgm_polyagamma_logpdf(double x, double h, double z)
{
    if (islessequal(x, 0.) || isinf(x)) {
        return -INFINITY;
    }
    double lg = pgm_lgamma(h);
    double a = (fabs(z) > 0. ? h * log(cosh(0.5 * z)) - 0.5 * z * z * x : 0.) +
               (h - 1.) * PGM_LOG2 - PGM_LS2PI - 1.5 * log(x) - lg;
    double first = lg - 0.125 * h * h / x;
    double sum = 1.;
    double sign = -1.;
    for (unsigned int n = 1; n < PGM_MAX_SERIES_TERMS; n++, sign = -sign) {
        double t = 2 * n + h;
        double curr = pgm_lgamma(n + h) - 0.125 * t * t / x - pgm_lgamma(n + 1);
        sum += sign * exp(curr - first) * t / h;
    }
    return a + (log(h) + first + log(sum));
}
double pgm_polyagamma_logpdf ( double x , double h , double z ) { if ( islessequal ( x , 0. ) || isinf ( x ) ) { return - INFINITY ; } double lg = pgm_lgamma ( h ) ; double a = ( fabs ( z ) > 0. ? h * log ( cosh ( 0.5 * z ) ) - 0.5 * z * z * x : 0. ) + ( h - 1. ) * PGM_LOG2 - PGM_LS2PI - 1.5 * log ( x ) - lg ; double first = lg - 0.125 * h * h / x ; double sum = 1. ; double sign = -1. ; for ( unsigned int n = 1 ; n < PGM_MAX_SERIES_TERMS ; n ++ , sign = - sign ) { double t = 2 * n + h ; double curr = pgm_lgamma ( n + h ) - 0.125 * t * t / x - pgm_lgamma ( n + 1 ) ; sum += sign * exp ( curr - first ) * t / h ; } return a + ( log ( h ) + first + log ( sum ) ) ; }
Approximate the logarithm of the density function of PG(h, z).
Approximate the logarithm of the density function of PG ( h z ) .