25package umontreal.ssj.probdist;
27import umontreal.ssj.util.Num;
59 public static double MAXN = 100000;
74 public double prob(
int x) {
75 if (x < supportA || x > supportB)
77 if (pdf ==
null || x < xmin || x > xmax)
78 return prob(m, l, k, x);
82 public double cdf(
int x) {
91 return cdf(m, l, k, x);
96 return 1.0 -
cdf[x + 1 - xmin];
98 return cdf(m, l, k, x);
108 return barF(m, l, k, x);
113 return cdf[x - xmin];
115 return 1.0 -
cdf[x - 1 - xmin];
117 return barF(m, l, k, x);
122 throw new IllegalArgumentException(
"u is not in [0,1]");
124 return Math.max(0, k - l + m);
126 return Math.min(k, m);
129 int x = Math.max(0, k - l + m);
134 p = p * (m - x) * (k - x) / ((x + 1) * (l - m - k + 1.0 + x));
156 public static double prob(
int m,
int l,
int k,
int x) {
160 throw new IllegalArgumentException(
"l must be greater than 0");
162 throw new IllegalArgumentException(
"m is invalid: 1 <= m < l");
164 throw new IllegalArgumentException(
"k is invalid: 1 <= k < l");
165 if (x < Math.max(0, k - l + m) || x > Math.min(k, m))
175 throw new IllegalArgumentException(
"term overflow");
176 return Math.exp(res);
183 public static double cdf(
int m,
int l,
int k,
int x) {
185 throw new IllegalArgumentException(
"l must be greater than 0");
187 throw new IllegalArgumentException(
"m is invalid: 1 <= m < l");
189 throw new IllegalArgumentException(
"k is invalid: 1 <= k < l");
190 int imin = Math.max(0, k - l + m);
191 int imax = Math.min(k, m);
198 for (
int i = imin; i <= x; i++)
199 res +=
prob(m, l, k, i);
210 public static double barF(
int m,
int l,
int k,
int x) {
212 throw new IllegalArgumentException(
"l must be greater than 0");
214 throw new IllegalArgumentException(
"m is invalid: 1 < =m < l");
216 throw new IllegalArgumentException(
"k is invalid: 1 < =k < l");
217 int imin = Math.max(0, k - l + m);
218 int imax = Math.min(k, m);
225 for (
int i = imax; i >= x; i--)
226 res +=
prob(m, l, k, i);
237 public static int inverseF(
int m,
int l,
int k,
double u) {
240 throw new IllegalArgumentException(
"u is not in [0,1]");
242 return Math.max(0, k - l + m);
244 return Math.min(k, m);
253 int x = Math.max(0, k - l + m);
259 p = p * (m - x) * (k - x) / ((x + 1) * (l - m - k + 1.0 + x));
261 }
while (u > p && p > 0);
272 public static double getMean(
int m,
int l,
int k) {
274 throw new IllegalArgumentException(
"l must be greater than 0");
276 throw new IllegalArgumentException(
"m is invalid: 1<=m<l");
278 throw new IllegalArgumentException(
"k is invalid: 1<=k<l");
280 return ((
double) k * (
double) m / (
double) l);
294 throw new IllegalArgumentException(
"l must be greater than 0");
296 throw new IllegalArgumentException(
"m is invalid: 1<=m<l");
298 throw new IllegalArgumentException(
"k is invalid: 1<=k<l");
300 return (((
double) k * (
double) m / (
double) l) * (1 - ((
double) m / (
double) l)) * ((
double) l - (
double) k)
335 private void setHypergeometric() {
336 int imin = Math.max(0, k - l + m);
337 int imax = Math.min(k, m);
340 int ns = imax - imin + 1;
350 double[] P =
new double[ns];
351 double[] F =
new double[ns];
354 int mode = (int) ((k + 1.0) * (m + 1.0) / (l + 2.0));
355 int imid = mode - offset;
357 P[imid] =
prob(m, l, k, mode);
360 while (i > imin && Math.abs(P[i]) >
EPSILON) {
361 P[i - 1] = P[i] * (i + offset) / (m - i - offset + 1) * (l - m - k + i + offset) / (k - i - offset + 1);
367 while (i < imax && Math.abs(P[i]) >
EPSILON) {
368 P[i + 1] = P[i] * (m - i - offset) / (i + offset + 1) * (k - i - offset) / (l - m - k + i + offset + 1);
375 while (i < imax && F[i] < 0.5) {
377 F[i] = F[i - 1] + P[i];
384 F[i] = P[i] + F[i + 1];
388 xmin = imin + offset;
389 xmax = imax + offset;
391 pdf =
new double[imax + 1 - imin];
392 cdf =
new double[imax + 1 - imin];
393 System.arraycopy(P, imin, pdf, 0, imax + 1 - imin);
394 System.arraycopy(F, imin,
cdf, 0, imax + 1 - imin);
402 double[] retour = { m, l, k };
411 throw new IllegalArgumentException(
"l must be greater than 0");
413 throw new IllegalArgumentException(
"m is invalid: 1 <= m < l");
415 throw new IllegalArgumentException(
"k is invalid: 1 <= k < l");
431 return getClass().getSimpleName() +
" : m = " + m +
", l = " + l +
", k = " + k;
Classes implementing discrete distributions over the integers should inherit from this class.
static double EPSILON
Environment variable that determines what probability terms can be considered as negligible when buil...
static double MAXN
If the number of integers in the interval is larger than this constant, the tables will not be preco...
int getL()
Returns the associated with this object.
double getVariance()
Returns the variance of the distribution function.
int getK()
Returns the associated with this object.
int getM()
Returns the associated with this object.
static double getStandardDeviation(int m, int l, int k)
Computes and returns the standard deviation of the hypergeometric distribution with parameters ,...
String toString()
Returns a String containing information about the current distribution.
static double prob(int m, int l, int k, int x)
Computes the hypergeometric probability given by ( fheperg ).
double prob(int x)
Returns , the probability of .
static double getVariance(int m, int l, int k)
Computes and returns the variance.
double barF(int x)
Returns , the complementary distribution function.
double getMean()
Returns the mean of the distribution function.
static double barF(int m, int l, int k, int x)
Computes the complementary distribution function.
HypergeometricDist(int m, int l, int k)
Constructs an hypergeometric distribution with parameters ,.
static double cdf(int m, int l, int k, int x)
Computes the distribution function .
void setParams(int m, int l, int k)
Resets the parameters of this object to , and .
int inverseFInt(double u)
Returns the inverse distribution function , where.
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
static int inverseF(int m, int l, int k, double u)
Computes for the hypergeometric distribution without using precomputed tables.
static double getMean(int m, int l, int k)
Computes and returns the mean of the Hypergeometric distribution with parameters ,...
double getStandardDeviation()
Returns the standard deviation of the distribution function.
double[] getParams()
Return a table containing the parameters of the current distribution.
This class provides various constants and methods to compute numerical quantities such as factorials,...
static double lnFactorial(int n)
Returns the value of , the natural logarithm of factorial .
static final int DBL_MAX_EXP
Largest int such that is representable (approximately) as a double.
static double combination(int n, int s)
Returns the value of , the number of different combinations of objects amongst.
static final double LN2
The values of .