From 19ddf2957ed5cb8072b969bf12ec046ac0ac3ccb Mon Sep 17 00:00:00 2001 From: Brent Worden Date: Thu, 24 Feb 2005 03:59:05 +0000 Subject: [PATCH] added cauchy distribution git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@155159 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/distribution/CauchyDistribution.java | 59 ++++++ .../distribution/CauchyDistributionImpl.java | 194 ++++++++++++++++++ .../distribution/DistributionFactory.java | 17 +- .../distribution/CauchyDistributionTest.java | 96 +++++++++ .../DistributionFactoryImplTest.java | 12 +- .../HypergeometricDistributionTest.java | 15 +- .../distribution/PoissonDistributionTest.java | 25 ++- .../math/distribution/TDistributionTest.java | 6 +- xdocs/changes.xml | 3 + xdocs/userguide/distribution.xml | 5 +- 10 files changed, 414 insertions(+), 18 deletions(-) create mode 100644 src/java/org/apache/commons/math/distribution/CauchyDistribution.java create mode 100644 src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java create mode 100644 src/test/org/apache/commons/math/distribution/CauchyDistributionTest.java diff --git a/src/java/org/apache/commons/math/distribution/CauchyDistribution.java b/src/java/org/apache/commons/math/distribution/CauchyDistribution.java new file mode 100644 index 000000000..3f802e709 --- /dev/null +++ b/src/java/org/apache/commons/math/distribution/CauchyDistribution.java @@ -0,0 +1,59 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.distribution; + +/** + * Cauchy Distribution. + * Instances of CauchyDistribution objects should be created using + * {@link DistributionFactory#createCauchyDistribution(double, double)}.

+ * + *

+ * References:

+ *

+ *

+ * + * @version $Revision: 1.8 $ $Date: 2004-06-23 11:26:18 -0500 (Wed, 23 Jun 2004) $ + */ +public interface CauchyDistribution extends ContinuousDistribution { + + /** + * Access the median. + * @return median for this distribution + */ + double getMedian(); + + /** + * Access the scale parameter. + * @return scale parameter for this distribution + */ + double getScale(); + + /** + * Modify the median. + * @param median for this distribution + */ + void setMedian(double median); + + /** + * Modify the scale parameter. + * @param s scale parameter for this distribution + */ + void setScale(double s); +} diff --git a/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java b/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java new file mode 100644 index 000000000..3bee81611 --- /dev/null +++ b/src/java/org/apache/commons/math/distribution/CauchyDistributionImpl.java @@ -0,0 +1,194 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.distribution; + +import java.io.Serializable; + +/** + * Default implementation of + * {@link org.apache.commons.math.distribution.CauchyDistribution}. + * + * @version $Revision: 1.13 $ $Date: 2004-07-24 16:41:37 -0500 (Sat, 24 Jul 2004) $ + */ +public class CauchyDistributionImpl extends AbstractContinuousDistribution + implements CauchyDistribution, Serializable { + + /** Serializable version identifier */ + static final long serialVersionUID = 8589540077390120676L; + + /** The median of this distribution. */ + private double median = 0; + + /** The scale of this distribution. */ + private double scale = 1; + + /** + * Creates normal distribution with the mean equal to zero and standard + * deviation equal to one. + */ + public CauchyDistributionImpl(){ + this(0.0, 1.0); + } + + /** + * Create a cauchy distribution using the given median and scale. + * @param median median for this distribution + * @param s scale parameter for this distribution + */ + public CauchyDistributionImpl(double median, double s){ + super(); + setMedian(median); + setScale(s); + } + + /** + * For this disbution, X, this method returns P(X < x). + * @param x the value at which the CDF is evaluated. + * @return CDF evaluted at x. + */ + public double cumulativeProbability(double x) { + return 0.5 + (Math.atan((x - median) / scale) / Math.PI); + } + + /** + * Access the median. + * @return median for this distribution + */ + public double getMedian() { + return median; + } + + /** + * Access the scale parameter. + * @return scale parameter for this distribution + */ + public double getScale() { + return scale; + } + + /** + * For this distribution, X, this method returns the critical point x, such + * that P(X < x) = p. + *

+ * Returns Double.NEGATIVE_INFINITY for p=0 and + * Double.POSITIVE_INFINITY for p=1. + * + * @param p the desired probability + * @return x, such that P(X < x) = p + * @throws IllegalArgumentException if p is not a valid + * probability. + */ + public double inverseCumulativeProbability(double p) { + double ret; + if (p < 0.0 || p > 1.0) { + throw new IllegalArgumentException + ("probability argument must be between 0 and 1 (inclusive)"); + } else if (p == 0) { + ret = Double.NEGATIVE_INFINITY; + } else if (p == 1) { + ret = Double.POSITIVE_INFINITY; + } else { + ret = median + scale * Math.tan(Math.PI * (p - .5)); + } + return ret; + } + + /** + * Modify the median. + * @param median for this distribution + */ + public void setMedian(double median) { + this.median = median; + } + + /** + * Modify the scale parameter. + * @param s scale parameter for this distribution + * @throws IllegalArgumentException if sd is not positive. + */ + public void setScale(double s) { + if (s <= 0.0) { + throw new IllegalArgumentException( + "Scale must be positive."); + } + scale = s; + } + + /** + * Access the domain value lower bound, based on p, used to + * bracket a CDF root. This method is used by + * {@link #inverseCumulativeProbability(double)} to find critical values. + * + * @param p the desired probability for the critical value + * @return domain value lower bound, i.e. + * P(X < lower bound) < p + */ + protected double getDomainLowerBound(double p) { + double ret; + + if (p < .5) { + ret = -Double.MAX_VALUE; + } else { + ret = getMedian(); + } + + return ret; + } + + /** + * Access the domain value upper bound, based on p, used to + * bracket a CDF root. This method is used by + * {@link #inverseCumulativeProbability(double)} to find critical values. + * + * @param p the desired probability for the critical value + * @return domain value upper bound, i.e. + * P(X < upper bound) > p + */ + protected double getDomainUpperBound(double p) { + double ret; + + if (p < .5) { + ret = getMedian(); + } else { + ret = Double.MAX_VALUE; + } + + return ret; + } + + /** + * Access the initial domain value, based on p, used to + * bracket a CDF root. This method is used by + * {@link #inverseCumulativeProbability(double)} to find critical values. + * + * @param p the desired probability for the critical value + * @return initial domain value + */ + protected double getInitialDomain(double p) { + double ret; + + if (p < .5) { + ret = getMedian() - getScale(); + } else if (p > .5) { + ret = getMedian() + getScale(); + } else { + ret = getMedian(); + } + + return ret; + } +} diff --git a/src/java/org/apache/commons/math/distribution/DistributionFactory.java b/src/java/org/apache/commons/math/distribution/DistributionFactory.java index afe23b2bf..c0c2a949d 100644 --- a/src/java/org/apache/commons/math/distribution/DistributionFactory.java +++ b/src/java/org/apache/commons/math/distribution/DistributionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2004 The Apache Software Foundation. + * Copyright 2003-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import org.apache.commons.discovery.tools.DiscoverClass; * The following distributions are supported: *