package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
/**
* This class implements random variate generators for the gamma
* distribution. Its parameters are α > 0 and λ > 0.
* Its density function is
*
*
*
* f (x) = λαxα-1e-λx/Γ(α) for x > 0,
*
* where Γ is the gamma function defined by
*
*
*
* Γ(α) = ∫0∞xα-1e-x dx.
*
*
*
* The (non-static) nextDouble method simply calls inverseF on the
* distribution.
*
*/
public class GammaGen extends RandomVariateGen {
protected double alpha = -1.0;
protected double lambda = -1.0;
/**
* Creates a gamma random variate generator with parameters
* α = alpha and λ = lambda,
* using stream s.
*
*/
public GammaGen (RandomStream s, double alpha, double lambda) {
super (s, new GammaDist(alpha, lambda));
setParams (alpha, lambda);
}
/**
* Creates a gamma random variate generator with parameters
* α = alpha and
* λ = 1, using stream s.
*
*/
public GammaGen (RandomStream s, double alpha) {
this (s, alpha, 1.0);
}
/**
* Creates a new generator object for the gamma
* distribution dist and stream s.
*
*/
public GammaGen (RandomStream s, GammaDist dist) {
super (s, dist);
if (dist != null)
setParams (dist.getAlpha(), dist.getLambda());
}
/**
* Generates a new gamma random variate
* with parameters α = alpha and λ = lambda,
* using stream s.
*
*/
public static double nextDouble (RandomStream s,
double alpha, double lambda) {
return GammaDist.inverseF (alpha, lambda, 15, s.nextDouble());
}
/**
* Returns the parameter α of this object.
*
*/
public double getAlpha() {
return alpha;
}
/**
* Returns the parameter λ of this object.
*
*
*/
public double getLambda() {
return lambda;
}
/**
* Sets the parameter α and λ of this object.
*
*/
protected void setParams (double alpha, double lambda) {
if (lambda <= 0.0)
throw new IllegalArgumentException ("lambda <= 0");
if (alpha <= 0.0)
throw new IllegalArgumentException ("alpha <= 0");
this.lambda = lambda;
this.alpha = alpha;
}
}
Ads help maintain this website.