# The Gamma Distribution

The gamma distribution can be used to model the time until an event occurs
a specified number of times. For example, if a system has *n-1* backups
all with identical exponential distributions, then the time until
the original system and all its backups have failed can be modeled
using a gamma distribution. From this example, it is obvious that
the exponential distribution is a special case of the gamma distribution.

The gamma distribution has a scale parameter and a shape parameter
often called the order. These parameters are usually denoted by the Greek
letters θ and α. In the above example, the scale parameter θ
corresponds to the mean time to failure of each system,
while the shape parameter equals *n-1*.

The probability density function is:

The gamma distribution may also have a location parameter, which translates the distribution functions up or down the X axis by the specified amount.

If the shape parameter is an integer, and the location parameter is 0, then the distribution is an The Erlang Distribution. The The Chi Square distribution is also a special case of the gamma distribution, with location parameter 0, scale parameter 2, and the shape parameter equal to the degrees of freedom divided by 2.

The gamma distribution is implemented by the GammaDistribution class. It has five constructors with one to three arguments. The first argument is always the shape parameter. The second argument, if present, is the scale parameter. The default value is 1. The third argument specifies the location, with a default of zero.

The following constructs the same gamma distribution of order 4.2, scale parameter 1 and location parameter 0 using each of the three constructors:

```
var gamma1 = new GammaDistribution(4.2, 1.0, 0.0);
var gamma2 = new GammaDistribution(4.2, 1.0);
var gamma3 = new GammaDistribution(4.2);
```

The GammaDistribution class has three specific properties, ShapeParameter, ScaleParameter, and LocationParameter, which return the shape, scale and location parameters of the distribution.

If a variable is assumed to have a gamma distribution, then the parameter of the distribution can be estimated using the method of maximum likelihood or the method of matching moments. The fourth and fifth constructors perform this calculation. The first argument is a Vector<T> whose distribution is to be estimated. The optional second parameter is a EstimationMethod value that specifies the method to be used. The default is the method of matching moments.

Note that parameter estimation says nothing about how well the estimated distribution fits the variable's distribution. Use one of the goodness-of-fit tests to verify the appropriateness of the choice of distribution.

GammaDistribution has one static (*Shared* in Visual Basic) method, Sample, which
generates a random sample using a user-supplied uniform random number generator. It has three overloads, that take
from 2 to 4 parameters. The first argument is the random number generator. The second to fourth parameters, if
present, have the same meaning as the parameters of the constructor above.

```
var random = new MersenneTwister();
double sample1 = GammaDistribution.Sample(random, 4.2, 1.0, 0.0);
double sample2 = GammaDistribution.Sample(random, 4.2, 1.0);
double sample3 = GammaDistribution.Sample(random, 4.2);
```

The above example uses the MersenneTwister to generate uniform random numbers.

For details of the properties and methods common to all continuous distribution classes, see the topic on continuous distributions.