umontreal.iro.lecuyer.randvar.HypergeometricGen Maven / Gradle / Ivy
Show all versions of ssj Show documentation
/*
* Class: HypergeometricGen
* Description: random variate generators for the hypergeometric distribution
* Environment: Java
* Software: SSJ
* Copyright (C) 2001 Pierre L'Ecuyer and Université de Montréal
* Organization: DIRO, Université de Montréal
* @author
* @since
* SSJ is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License (GPL) as published by the
* Free Software Foundation, either version 3 of the License, or
* any later version.
* SSJ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* A copy of the GNU General Public License is available at
GPL licence site.
*/
package umontreal.iro.lecuyer.randvar;
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
/**
* This class implements random variate generators for the
* hypergeometric distribution. Its mass function is
*
*
*
* p(x) = nCr(m, x)nCr(l - m, k - x)/nCr(l, k) for x = max(0, k - l + m),..., min(k, m)
*
* where nCr(n, x) is the number of possible combinations when choosing
* x elements among a set of n elements,
* m, l and k are integers that satisfy 0 < m <= l and
* 0 < k <= l.
*
*
* The generation method is inversion using the chop-down algorithm
*
*/
public class HypergeometricGen extends RandomVariateGenInt {
private int m;
private int l;
private int k;
/**
* Creates a hypergeometric generator with
* parameters m = m, l = l and k = k,
* using stream s.
*
*/
public HypergeometricGen (RandomStream s, int m, int l, int k) {
super (s, new HypergeometricDist (m, l, k));
setParams (m, l, k);
}
/**
* Creates a new generator for distribution dist,
* using stream s.
*
*/
public HypergeometricGen (RandomStream s, HypergeometricDist dist) {
super (s, dist);
if (dist != null)
setParams (dist.getM(), dist.getL(), dist.getK());
}
/**
* Generates a new variate from the hypergeometric distribution with
* parameters m = m, l = l and k = k,
* using stream s.
*
*/
public static int nextInt (RandomStream s, int m, int l, int k) {
return HypergeometricDist.inverseF (m, l, k, s.nextDouble());
}
/**
* Returns the m associated with this object.
*
*/
public int getM() {
return m;
}
/**
* Returns the l associated with this object.
*
*/
public int getL() {
return l;
}
/**
* Returns the k associated with this object.
*
*
*/
public int getK() {
return k;
}
/**
* Sets the parameter n and p of this object.
*
*/
protected void setParams (int m, int l, int k) {
if (l <= 0)
throw new IllegalArgumentException ("l must be greater than 0");
if (m <= 0 || m > l)
throw new IllegalArgumentException ("m is invalid: 1<=m l)
throw new IllegalArgumentException ("k is invalid: 1<=k