Пример #1
0
/* Natural logarithm of gamma function for values > 0. */
double ln_gamma(double alpha) {

  /* Magic coefficients for calculating the gamma function. */
  static double coef[6] = {76.18009173, -86.50532033, 24.01409822,
			     -1.231739516, 0.120858003e-2, -0.536382e-5};
  double ln_factor;
  double series;
  double a;
  int i;

  /* Use the magic coefficients to calculate ln(GAMMA). */
  if (alpha > 1.0)
    {
      ln_factor = alpha + 4.5;
      ln_factor = (alpha - 0.5) * log(ln_factor) - (ln_factor);

      for (i = 0, a = alpha, series = 1.0; i < 6; ++i, a = a + 1.0)
	series = series + coef[i] / a;

      return(ln_factor + log(2.50662827465 * series));
    }

  /* Use the relationship of GAMMA(z) = GAMMA(z+1) / z since
   * the magic coefficients work less well between 0 and 1. */
  else if (alpha > 0.0) return(ln_gamma(alpha + 1.0) - log(alpha));

  /* Magic coefficients do not work with values <= 0. */
  else
    {
      fprintf(stderr,
	      "The function \"ln_gamma()\" only works with values > 0\n");
      return -1;
    }
}
Пример #2
0
/* Determine the incomplete gamma function using the series approximation. */
double gamma_ser(double alpha, double ln_likelihood) {
 
  double sum;                   /* The sum of the series. */
  double alpha_n;               /* "alpha" + "n". */
  double term;                  /* Current term in the series. */
  
  for (alpha_n = alpha + 1.0, sum = term = 1.0/alpha;
       term >= sum * L_ERR; alpha_n = alpha_n + 1.0)
    {
      term = term * ln_likelihood / alpha_n;
      sum = sum + term;
    }
  return(exp(-ln_likelihood + alpha * log(ln_likelihood)
	     + log(sum) - ln_gamma(alpha)));
}
Пример #3
0
/* Determine the natural logarithm of the incomplete gamma function's
 * complement using the continued fraction approximation. */
double ln_gamma_cf(double alpha, double ln_likelihood) {

  double frac_old = 0.0;        /* Old continued fraction. */

  /* Variables A and B for calculating the continued fraction. */
  double a_0 = 0.0, a_1 = 1.0;
  double b_0 = 1.0, b_1 = ln_likelihood;

  /* Variables for calculating A and B. */
  double i;
  double i_alpha;     /* i - alpha. */

  /* Do 2 cycles of the reiteration for each index. */
  for (i = 1.0; ; i = i + 1.0)
    {
      /* Cycle one of reiteration. */
      i_alpha = i - alpha;
      a_0 = a_1 + i_alpha * a_0;
      b_0 = b_1 + i_alpha * b_0;

      /* Cycle two of the reiteration. */
      a_1 = ln_likelihood * a_0 + i * a_1;
      b_1 = ln_likelihood * b_0 + i * b_1;

      /* Determine the continued fraction if the denominator is not 0. */
      if (b_1)
	{
	  a_0 = a_0 / b_1;
	  a_1 = a_1 / b_1;
	  b_0 = b_0 / b_1;
	  b_1 = 1.0;
	  if (fabs((a_1 - frac_old)/a_1) < L_ERR)
	    return(-ln_likelihood + alpha*log(ln_likelihood) + log(a_1)
		   - ln_gamma(alpha));
	  frac_old = a_1;
	}
    }
}
Пример #4
0
double log_chi_inv_cdf(double alpha, double ln_like) {

  return ln_gamma_prob(alpha/2.0, ln_like/2.0)-ln_gamma(alpha/2.0);
}
Пример #5
0
/*!
\brief
Evaluate the log-density for a Gamma distribution.

The Gamma distribution has the following density function
with respect to \f$ x \f$:
\f[
	{\bf p} ( x | a, b) = \frac{b^a}{ \Gamma (a) } x^{a-1} \exp ( - b x )
\f]
where \f$ a \f$ and \f$ b \f$ are parameters and
\f$ \Gamma (a) \f$ is the \ref ln_gamma "gamma function".
This distribution has mean \f$ a / b \f$ and variance \f$ a / b^2 \f$.

\param[in] x
is the value of the Gamma distributed random variate.

\param[in] a
is the shape parameter in the Gamma distribution

\param[in] b
is the reciprical of the rate parameter in the Gamma distribution
(\f$ 1 / b \f$ is called the scale parameter).

\return
The return value is \f$ \log [ {\bf p} (x | a, b) ] \f$.

*/
double ln_gamma_density(double x, double a, double b)
{	return a*log(b) - ln_gamma(a) + (a-1.0)*log(x) - b*x; }
Пример #6
0
/*!
\brief
Evaluate the log-density for a Possion distribution.

The Possion distribution has the following density function
with respect to \f$ y \f$:
\f[
	{\bf p} (y | \lambda ) = \frac{ \lambda^y } { y ! } \exp( - \lambda  )
\f]
where \f$ y \f$ is the number of occurnaces,
and \f$ \lambda  \f$ is a parameter that is equal
to the expected number of occurances in the time interval
(during which the occurances are counted).
This distribution has mean 
\f$ \lambda  \f$ and variance \f$ \lambda  \f$.

\param[in] y
is the number of occurances that occured in the time interval
(must be non-negative).

\param[in] \lambda 
is the the expected number of occurances in the time interval.

\return
The return value is \f$ \log [ {\bf p} ( y | \lambda  ) ] \f$.
*/
double ln_poisson_density(int y, double lambda )
{	return y * log(lambda ) - ln_gamma(y + 1.0) - lambda ; }