From b345aa99c18edbdb2d338f325389b771913a64d6 Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Thu, 7 Jun 2007 13:55:36 +0000 Subject: [PATCH] Removed dependency on DistributionFactory. Added settable normal distribution field. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@545184 13f79535-47bb-0310-9956-ffa450edef68 --- .../distribution/PoissonDistributionImpl.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java b/src/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java index 39131edd0..898d16ec0 100644 --- a/src/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java +++ b/src/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java @@ -33,6 +33,8 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution /** Serializable version identifier */ private static final long serialVersionUID = -3349935121172596109L; + private NormalDistribution normal; + /** * Holds the Poisson mean for the distribution. */ @@ -47,7 +49,22 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution * @throws IllegalArgumentException if p ≤ 0 */ public PoissonDistributionImpl(double p) { + this(p, new NormalDistributionImpl()); + } + + /** + * Create a new Poisson distribution with the given the mean. + * The mean value must be positive; otherwise an + * IllegalArgument is thrown. + * + * @param p the Poisson mean + * @param z a normal distribution used to compute normal approximations. + * @throws IllegalArgumentException if p ≤ 0 + * @since 1.2 + */ + public PoissonDistributionImpl(double p, NormalDistribution z) { super(); + setNormal(z); setMean(p); } @@ -74,6 +91,8 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution "The Poisson mean must be positive"); } this.mean = p; + normal.setMean(p); + normal.setStandardDeviation(Math.sqrt(p)); } /** @@ -122,10 +141,6 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution * @throws MathException if an error occurs computing the normal approximation */ public double normalApproximateProbability(int x) throws MathException { - NormalDistribution normal = DistributionFactory.newInstance() - .createNormalDistribution(getMean(), - Math.sqrt(getMean())); - // calculate the probability using half-correction return normal.cumulativeProbability(x + 0.5); } @@ -154,4 +169,15 @@ public class PoissonDistributionImpl extends AbstractIntegerDistribution return Integer.MAX_VALUE; } + /** + * Modify the normal distribution used to compute normal approximations. + * The caller is responsible for insuring the normal distribution has the + * proper parameter settings. + * @param value the new distribution + * @since 1.2 + */ + public void setNormal(NormalDistribution value) { + normal = value; + } + } \ No newline at end of file