Microsoft KB Archive/87864

= Microsoft Knowledge Base =

Excel: ATP Definition: GAMMADIST
Last reviewed: January 30, 1996

Article ID: Q87864

The information in this article applies to:


 * Microsoft Excel for Windows 95, version 7.0
 * Microsoft Excel for Windows, versions 4.0, 5.0
 * Microsoft Excel for the Macintosh, versions 4.0, 5.0

SUMMARY
The versions of Microsoft Excel listed above provide a set of special analysis tools called the Analysis ToolPak. This article is part of a series of articles that provide information on the underlying formulas used in the Analysis ToolPak.

This article covers the following function(s):

GAMMADIST(x,alpha,beta,cumulative)

MORE INFORMATION
The GAMMADIST function returns the gamma function for a particular value of the random variable X. The Excel function is implemented with the following C routine, GAMMP, which in turn depends on the GSER and GCF functions listed below:

  GSER function:
 * 1) include 


 * 1) define ITMAX 100
 * 2) define EPS 3.0e-7

void gser(gamser,a,x,gln) 

float a,x,*gamser,*gln;

{

int n;    float sum,del,ap; float gammln void nrerror;

*gln=gammln(a); if (x <= 0.0) { if (x < 0.0) nrerror(&quot;x less than 0 in routine GSER&quot;); *gamser==0.0; return; } else { ap=a; del=sum=1.0/a; for (n=1;n<=ITMAX;n++) { ap += 1.0; del += x/ap; sum += del; if (fabs(del) < fabs(sum)*EPS) { *gamser=sum*exp(-x+a*log(x)-(*gln)); return; }         }          nrerror(&quot;a too large, ITMAX too small in routine GSER&quot;); return; } }

  GCF function:
 * 1) include 


 * 1) define ITMAX 100
 * 2) define EPS 3.0e-7

void gcf(gammcf,a,x,gln) 

float a,x,*gammcf,gln;

{

int n;    float gold=0.0,g,fac=1.0,b1=1.0; float b0=0.0,anf,ana,an,a1,a0=1.0; float gammaln void nrerror;

*gln=gammln(a); a1=x; for (n=1;n<=ITMAX,n++) { an= (float) n;         ana=an-a; a0=(a1+a0*ana)*fac; b0=(b1+b0*ana)*fac; anf=an*fac; a1=x*a0+anf*a1; b1=x*b0+anf*b1; if (a1) { fac=1.0/a1; g=b1*fac; if (fabs((g-gold)/g) < EPS) { *gammcf=exp(-x+a*log(x)-(*gln))*g; return; }              gold=g; }    }     nrerror(&quot;a too large, ITMAX too small in routine GCF&quot;) }


 * 1) GAMMP function:

float gammp(a,x) float a,x;

{

float gamser,gammcf,gln; void gser,gcf(,nrerror;

if (x < 0.0 || a <= 0.0) nrerror(&quot;Invalid arguments in routine GAMMP&quot;);

if (x < (a+1.0)) { gser(&gamser,a,x,&gln); return gamser; } else { gcf(&gammcf,a,x,&gln); return 1.0-gammcf; } }

The Excel function GAMMADIST(x,alpha,beta,cumulative) uses the GAMMP routine to return the gamma distribution function. The two subroutines GSER, and GCF are called by GAMMP. The gamma distribution is often used to model events with skewed distributions and is commonly applied to queuing analysis.