25package umontreal.ssj.probdist;
27import umontreal.ssj.util.*;
28import umontreal.ssj.functions.MathFunction;
54 private static final double EPS2 = 100.0 *
EPSILON;
59 protected double mean;
62 public Function(
int m,
double mean,
int R,
int f[]) {
66 this.f =
new int[f.length];
67 System.arraycopy(f, 0, this.f, 0, f.length);
70 public double evaluate(
double x) {
75 for (
int j = 0; j < R; j++)
76 sum += f[j] / (x - (
double) j);
78 return (sum + m * Math.log1p(-mean / x));
90 public static double MAXN = 100000;
129 return prob(n, p, q, x);
131 if (x > xmax || x < xmin)
132 return prob(n, p, q, x);
134 return pdf[x - xmin];
137 public double cdf(
int x) {
159 double term =
prob(x);
161 final double z = (1.0 - p) / p;
163 while (i > 0 && i >= x - RMAX) {
164 term *= z * i / (n - i + 1);
171 return cdf[x - xmin];
174 return 1.0 -
cdf[x + 1 - xmin];
198 final double q = 1.0 - p;
201 sum = term =
prob(x);
205 while (i < n && i < x + IMAX) {
206 term = term * z * (n - i) / (i + 1);
218 return cdf[x - xmin];
220 return 1.0 -
cdf[x - 1 - xmin];
222 return 1.0 -
cdf(n, p, x - 1);
226 if ((
cdf ==
null) || (u <= EPS2))
229 return super.inverseFInt(u);
248 public static double prob(
int n,
double p,
int x) {
249 return prob(n, p, 1.0 - p, x);
263 public static double prob(
int n,
double p,
double q,
int x) {
271 throw new IllegalArgumentException(
"n < 0");
292 if (((n - x) & 1) != 0)
297 Res = Math.pow(p, (
double) x) *
Num.
combination(n, x) * Math.pow(q, (
double) (n - x));
311 throw new IllegalArgumentException(
"term overflow");
316 return signe * Math.exp(Res);
330 public static double cdf(
int n,
double p,
int x) {
331 final int NLIM1 = 10000;
332 final double VARLIM = 50.0;
334 double y, z, q = 1.0 - p;
335 double sum, term, termmid;
337 boolean flag =
false;
339 if (p < 0.0 | p > 1.0)
340 throw new IllegalArgumentException(
"p not in [0,1]");
342 throw new IllegalArgumentException(
"n < 0");
358 mid = (int) ((n + 1) * p);
361 sum = term = termmid =
prob(n, p, 1.0 - p, mid);
365 while (term >=
EPSILON || i >= mid - RMAX) {
366 term *= z * i / (n - i + 1);
375 for (i = mid; i < x; i++) {
376 term *= z * (n - i) / (i + 1);
386 if (p > 0.5 || ((p == 0.5) && (x > n / 2))) {
393 if (n * p * q > VARLIM) {
399 term = Math.pow((x + 1) * q / ((n - x) * p), 1.0 / 3.0);
400 y = term * (9.0 - 1.0 / (x + 1)) - 9.0 + 1.0 / (n - x);
401 z = 3.0 * Math.sqrt(term * term / (x + 1) + 1.0 / (n - x));
413 y = (2.0 * n - x) * p / (2.0 - p);
414 double t = 2.0 * n - x;
415 z = (2.0 * y * y - x * y - x * (double) x - 2.0 * x) / (6 * t * t);
430 public static double barF(
int n,
double p,
int x) {
431 return 1.0 -
cdf(n, p, x - 1);
440 public static int inverseF(
int n,
double p,
double u) {
441 if (u < 0.0 || u > 1.0)
442 throw new IllegalArgumentException(
"u not in [0,1]");
443 if (u <=
prob(n, p, 0))
445 if ((u > 1.0 -
prob(n, p, n)) || (u >= 1.0))
447 final int NLIM1 = 10000;
449 final double q = 1.0 - p;
450 double z, sum, term, termmid;
453 i = (int) ((n + 1) * p);
456 term =
prob(n, p, i);
457 while ((term >= u) && (term > Double.MIN_NORMAL)) {
459 term =
prob(n, p, i);
463 if (term <= Double.MIN_NORMAL) {
465 term =
prob(n, p, i);
466 while (term >= u && (term > Double.MIN_NORMAL)) {
467 term *= z * i / (n - i + 1);
473 term =
prob(n, p, i);
474 sum = termmid = term;
477 for (i = mid; i > 0; i--) {
478 term *= z * i / (n - i + 1);
490 term *= z * i / (n - i + 1);
497 while ((sum < u) && (sum > prev)) {
499 term *= z * (n - i) / (i + 1);
507 for (i = 0; i <= n; i++) {
535 public static double[]
getMLE(
int[] x,
int m) {
537 throw new UnsupportedOperationException(
" m < 2");
542 for (i = 0; i < m; i++) {
550 for (i = 0; i < m; i++)
551 sum += (x[i] - mean) * (x[i] - mean);
552 double var = sum / m;
554 throw new UnsupportedOperationException(
"mean <= variance");
556 int f[] =
new int[r];
557 for (
int j = 0; j < r; j++) {
559 for (i = 0; i < m; i++)
564 double p = 1.0 - var / mean;
565 double rup = (int) (5 * mean / p);
569 Function fct =
new Function(m, mean, r, f);
570 double parameters[] =
new double[2];
572 if (parameters[0] < r)
574 parameters[1] = mean / parameters[0];
588 double parameters[] =
new double[2];
589 parameters =
getMLE(x, m);
590 return new BinomialDist((
int) parameters[0], parameters[1]);
604 public static double[]
getMLE(
int[] x,
int m,
int n) {
606 throw new IllegalArgumentException(
"m <= 0");
608 throw new IllegalArgumentException(
"n <= 0");
610 double parameters[] =
new double[1];
612 for (
int i = 0; i < m; i++)
616 parameters[0] = mean / (double) n;
631 double parameters[] =
new double[1];
632 parameters =
getMLE(x, m, n);
642 public static double getMean(
int n,
double p) {
644 throw new IllegalArgumentException(
"n <= 0");
645 if (p < 0.0 || p > 1.0)
646 throw new IllegalArgumentException(
"p not in range (0, 1)");
660 throw new IllegalArgumentException(
"n <= 0");
661 if (p < 0.0 || p > 1.0)
662 throw new IllegalArgumentException(
"p not in range (0, 1)");
664 return (n * p * (1 - p));
677 private void setBinomial(
int n,
double p) {
687 if (p < 0.0 || p > 1.0)
688 throw new IllegalArgumentException(
"p not in range (0, 1)");
690 throw new IllegalArgumentException(
"n <= 0");
713 P =
new double[1 + n];
714 F =
new double[1 + n];
717 mid = (int) ((n + 1) * Math.abs(p) / (Math.abs(p) + Math.abs(q)));
720 P[mid] =
prob(n, p, q, mid);
722 if (p != 0.0 || p != -0.0)
726 while (i > 0 && Math.abs(P[i]) > EPS) {
727 P[i - 1] = P[i] * z * i / (n - i + 1);
732 if (q != 0.0 || q != -0.0)
736 while (i < n && Math.abs(P[i]) > EPS) {
737 P[i + 1] = P[i] * z * (n - i) / (i + 1);
748 while (i < n && F[i] < 0.5) {
750 F[i] = F[i - 1] + P[i];
762 F[i] = P[i] + F[i + 1];
769 while (i < xmed && F[i] < DiscreteDistributionInt.EPSILON)
775 while (i > xmed && F[i] < DiscreteDistributionInt.EPSILON)
779 pdf =
new double[imax + 1 - imin];
780 cdf =
new double[imax + 1 - imin];
781 System.arraycopy(P, imin, pdf, 0, imax + 1 - imin);
782 System.arraycopy(F, imin,
cdf, 0, imax + 1 - imin);
805 double[] retour = { n, p };
822 return getClass().getSimpleName() +
" : n = " + n +
", p = " + p;
static double[] getMLE(int[] x, int m)
Estimates the parameters of the binomial distribution using the maximum likelihood method,...
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
static double getMean(int n, double p)
Computes the mean of the binomial distribution with parameters and .
double getMean()
Returns the mean of the distribution function.
static double cdf(int n, double p, int x)
Computes , the distribution function of a binomial random variable with parameters and ,...
double prob(int x)
Returns , the probability of .
static BinomialDist getInstanceFromMLE(int[] x, int m, int n)
Creates a new instance of a binomial distribution with given (fixed) parameter , and with parameter ...
static double prob(int n, double p, int x)
Computes and returns the binomial probability in eq.
static double barF(int n, double p, int x)
Returns , the complementary distribution function.
static int inverseF(int n, double p, double u)
Computes , the inverse of the binomial distribution.
double[] getParams()
Returns a table that contains the parameters of the current distribution, in regular order: [ ,...
double getVariance()
Returns the variance of the distribution function.
double barF(int x)
Returns , the complementary distribution function.
static double MAXN
The value of the parameter above which the tables are not precomputed by the constructor.
static double prob(int n, double p, double q, int x)
A generalization of the previous method.
static double[] getMLE(int[] x, int m, int n)
Estimates the parameter of the binomial distribution with given (fixed) parameter ,...
void setParams(int n, double p)
Resets the parameters to these new values and recomputes everything as in the constructor.
String toString()
Returns a String containing information about the current distribution.
int inverseFInt(double u)
Returns the inverse distribution function , where.
static double getVariance(int n, double p)
Computes the variance of the binomial distribution with parameters and .
BinomialDist(int n, double p)
Creates an object that contains the binomial terms ( fmass-binomial ), for , and the corresponding cu...
int getN()
Returns the parameter of this object.
static double getStandardDeviation(int n, double p)
Computes the standard deviation of the Binomial distribution with parameters and .
double getStandardDeviation()
Returns the standard deviation of the distribution function.
double getP()
Returns the parameter of this object.
static BinomialDist getInstanceFromMLE(int[] x, int m)
Creates a new instance of a binomial distribution with both parameters and estimated using the maxi...
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...
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a (page 80)).
static double cdf01(double x)
Same as cdf(0, 1, x).
static double barF01(double x)
Same as barF(0, 1, x).
Extends the class DiscreteDistributionInt for the Poisson distribution slaw00a (page 325) with mean.
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
double barF(int x)
Returns , the complementary distribution function.
This class provides various constants and methods to compute numerical quantities such as factorials,...
static final int DBL_MIN_EXP
Smallest int such that is representable (approximately) as a normalised double.
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 .
This class provides numerical methods to solve non-linear equations.
static double brentDekker(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the Brent-Dekker method.
This interface should be implemented by classes which represent univariate mathematical functions.