25package umontreal.ssj.probdist;
27import umontreal.ssj.util.*;
58 private double lambda;
83 public double prob(
int x) {
87 return prob(lambda, x);
88 if (x > xmax || x < xmin)
89 return prob(lambda, x);
93 public double cdf(
int x) {
121 double term =
prob(lambda, x);
124 while (i > 0 && i >= x - RMAX) {
125 term = term * i / lambda;
133 return cdf[x - xmin];
136 return 1.0 -
cdf[x + 1 - xmin];
165 return cdf[x - xmin];
167 return 1.0 -
cdf[x - 1 - xmin];
173 return super.inverseFInt(u);
197 public static double prob(
double lambda,
int x) {
201 if (lambda >= 100.0) {
202 if ((
double) x >= 10.0 * lambda)
204 }
else if (lambda >= 3.0) {
205 if ((
double) x >= 100.0 * lambda)
208 if ((
double) x >= 200.0 * Math.max(1.0, lambda))
212 final double LAMBDALIM = 20.0;
214 if (lambda < LAMBDALIM && x <= 100)
215 Res = Math.exp(-lambda) * Math.pow(lambda, x) /
Num.
factorial(x);
217 double y = x * Math.log(lambda) -
Num.
lnGamma(x + 1.0) - lambda;
234 public static double cdf(
double lambda,
int x) {
240 throw new IllegalArgumentException(
"lambda < 0");
246 if (lambda >= 100.0) {
247 if ((
double) x >= 10.0 * lambda)
250 if ((
double) x >= 100.0 * Math.max(1.0, lambda))
263 final double LAMBDALIM = 200.0;
264 if (lambda > LAMBDALIM)
273 for (
int j = 1; j <= x; j++) {
277 return sum * Math.exp(-lambda);
285 public static double barF(
double lambda,
int x) {
287 throw new IllegalArgumentException(
"lambda < 0");
291 if (lambda >= 100.0) {
292 if ((
double) x >= 10.0 * lambda)
295 if ((
double) x >= 100 + 100.0 * Math.max(1.0, lambda))
308 final double LAMBDALIM = 200.0;
309 if (lambda > LAMBDALIM)
322 while (term >
EPSILON || i <= x + IMAX) {
334 public static int inverseF(
double lambda,
double u) {
335 if (u < 0.0 || u > 1.0)
336 throw new IllegalArgumentException(
"u is not in range [0,1]");
338 throw new IllegalArgumentException(
"lambda < 0");
340 return Integer.MAX_VALUE;
341 if (u <=
prob(lambda, 0))
345 final double LAMBDALIM = 700.0;
346 if (lambda < LAMBDALIM) {
347 double sumprev = -1.0;
348 double term = Math.exp(-lambda);
351 while (sum < u && sum > sumprev) {
362 while ((term >= u) && (term > Double.MIN_NORMAL)) {
366 if (term <= Double.MIN_NORMAL) {
369 while (term >= u && (term > Double.MIN_NORMAL)) {
376 double termid = term;
378 while (term >=
EPSILON * u && i > 0) {
388 while ((sum < u) && (sum > prev)) {
419 public static double[]
getMLE(
int[] x,
int n) {
421 throw new IllegalArgumentException(
"n <= 0");
424 parameters =
new double[1];
426 for (
int i = 0; i < n; i++) {
430 parameters[0] = (double) sum / (
double) n;
443 double parameters[] =
getMLE(x, n);
455 throw new IllegalArgumentException(
"lambda < 0");
469 throw new IllegalArgumentException(
"lambda < 0");
482 throw new IllegalArgumentException(
"lambda < 0");
484 return Math.sqrt(lambda);
500 throw new IllegalArgumentException(
"lambda < 0");
501 this.lambda = lambda;
519 Nmax = (int) (lambda + 16 * (2 + Math.sqrt(lambda)));
520 P =
new double[1 + Nmax];
530 while (i > 0 && P[i] > epsilon) {
531 P[i - 1] = P[i] * i / lambda;
538 while (P[i] > epsilon) {
539 P[i + 1] = P[i] * lambda / (i + 1);
544 double[] nT =
new double[1 + Nmax];
545 System.arraycopy(P, 0, nT, 0, P.length);
550 F =
new double[1 + Nmax];
553 for (i = imin; i <= imax; i++)
560 while (i < imax && F[i] < 0.5) {
562 F[i] = P[i] + F[i - 1];
573 F[i] = P[i] + F[i + 1];
582 while (i < xmed && F[i] <
EPSILON)
588 while (i > xmed && F[i] <
EPSILON)
592 pdf =
new double[imax + 1 - imin];
593 cdf =
new double[imax + 1 - imin];
594 System.arraycopy(P, imin, pdf, 0, imax - imin + 1);
595 System.arraycopy(F, imin,
cdf, 0, imax - imin + 1);
602 double[] retour = { lambda };
610 return getClass().getSimpleName() +
": lambda = " + lambda;
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 gamma distribution tjoh95a (page 337) with shape pa...
double cdf(double x)
Returns the distribution function .
double barF(double x)
Returns the complementary distribution function.
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
double getStandardDeviation()
Returns the standard deviation of the distribution function.
static double[] getMLE(int[] x, int n)
Estimates the parameter of the Poisson distribution using the maximum likelihood method,...
double barF(int x)
Returns , the complementary distribution function.
static double prob(double lambda, int x)
Computes and returns the Poisson probability for lambda, as defined in ( fmass-Poisson ).
static PoissonDist getInstanceFromMLE(int[] x, int n)
Creates a new instance of a Poisson distribution with parameter.
void setLambda(double lambda)
Sets the associated with this object.
static double barF(double lambda, int x)
Computes and returns the value of the complementary Poisson distribution function,...
static double getStandardDeviation(double lambda)
Computes and returns the standard deviation of the Poisson distribution with parameter .
static double MAXLAMBDA
The value of the parameter above which the tables are not precomputed by the constructor.
double getVariance()
Returns the variance of the distribution function.
static int inverseF(double lambda, double u)
Performs a linear search to get the inverse function without precomputed tables.
static double getVariance(double lambda)
Computes and returns the variance of the Poisson distribution with parameter .
static double cdf(double lambda, int x)
Computes and returns the value of the Poisson distribution function.
double getLambda()
Returns the associated with this object.
double getMean()
Returns the mean of the distribution function.
double prob(int x)
Returns , the probability of .
double[] getParams()
Return a table containing the parameter of the current distribution.
int inverseFInt(double u)
Returns the inverse distribution function , where.
static double getMean(double lambda)
Computes and returns the mean of the Poisson distribution with parameter .
PoissonDist(double lambda)
Creates an object that contains the probability and distribution functions, for the Poisson distribut...
String toString()
Returns a String containing information about the current distribution.
This class provides various constants and methods to compute numerical quantities such as factorials,...
static double lnGamma(double x)
Returns the natural logarithm of the gamma function evaluated at x.
static double factorial(int n)
Returns the value of .