package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
/**
* This class implements random variate generators with the
* beta distribution with shape parameters
* α > 0 and
* β > 0, over the interval (a, b), where a < b.
* The density function of this distribution is
*
*
*
* f (x) = [Γ(α + β)/(Γ(α)Γ(β)(b - a)α+β-1)](x - a)α-1(b - x)β-1 for a < x < b,
*
* and f (x) = 0 elsewhere,
* where
* Γ(x) is the gamma function defined
* in
* {@link GammaGen}.
* Local copies of the parameters α, β, a, and b
* are maintained in this class.
* The (non-static) nextDouble method simply calls inverseF on the
* distribution.
*
*/
public class BetaGen extends RandomVariateGen {
// Distribution parameters
protected double p;
protected double q;
protected double a;
protected double b;
protected int gen;
/**
* Creates a new beta generator with parameters α =
* alpha and β = beta, over the interval
* (a, b), using stream s.
*
*/
public BetaGen (RandomStream s, double alpha, double beta,
double a, double b) {
super (s, new BetaDist (alpha, beta, a, b));
setParams (alpha, beta, a, b);
}
/**
* Creates a new beta generator with parameters α =
* alpha and β = beta, over the interval (0, 1),
* using stream s.
*
*/
public BetaGen (RandomStream s, double alpha, double beta) {
this (s, alpha, beta, 0.0, 1.0);
}
/**
* Creates a new generator for the distribution dist,
* using stream s.
*
*/
public BetaGen (RandomStream s, BetaDist dist) {
super (s, dist);
if (dist != null)
setParams (dist.getAlpha(), dist.getBeta(), dist.getA(), dist.getB());
}
/**
* Generates a variate from the beta distribution with
* parameters α = alpha, β = beta, over the
* interval (a, b), using stream s.
*
*/
public static double nextDouble (RandomStream s, double alpha,
double beta, double a, double b) {
return BetaDist.inverseF (alpha, beta, a, b, 15, s.nextDouble());
}
/**
* Returns the parameter α of this object.
*
*/
public double getAlpha() {
return p;
}
/**
* Returns the parameter β of this object.
*
*/
public double getBeta() {
return q;
}
/**
* Returns the parameter a of this object.
*
*/
public double getA() {
return a;
}
/**
* Returns the parameter b of this object.
*
*/
public double getB() {
return b;
}
protected void setParams (double alpha, double beta, double aa, double bb) {
if (alpha <= 0.0)
throw new IllegalArgumentException ("alpha <= 0");
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
if (aa >= bb)
throw new IllegalArgumentException ("a >= b");
p = alpha;
q = beta;
a = aa;
b = bb;
}
}
Ads help maintain this website.