Submitted by: brent@worden.org git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140903 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
83855b4cd7
commit
3849e21f3b
|
@ -103,6 +103,14 @@ public abstract class DistributionFactory {
|
|||
public abstract ChiSquaredDistribution createChiSquareDistribution(
|
||||
double degreesOfFreedom);
|
||||
|
||||
/**
|
||||
* Create a new exponential distribution with the given degrees of freedom.
|
||||
* @param mean mean.
|
||||
* @return a new exponential distribution.
|
||||
*/
|
||||
public abstract ExponentialDistribution createExponentialDistribution(
|
||||
double mean);
|
||||
|
||||
/**
|
||||
* Create a new F-distribution with the given degrees of freedom.
|
||||
* @param numeratorDegreesOfFreedom numerator degrees of freedom.
|
||||
|
|
|
@ -99,18 +99,27 @@ public class DistributionFactoryImpl extends DistributionFactory {
|
|||
public TDistribution createTDistribution(double degreesOfFreedom) {
|
||||
return new TDistributionImpl(degreesOfFreedom);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new F-distribution with the given degrees of freedom.
|
||||
* @param numeratorDegreesOfFreedom numerator degrees of freedom.
|
||||
* @param denominatorDegreesOfFreedom denominator degrees of freedom.
|
||||
* @return a new F-distribution.
|
||||
*/
|
||||
public FDistribution createFDistribution(
|
||||
double numeratorDegreesOfFreedom,
|
||||
double denominatorDegreesOfFreedom) {
|
||||
return new FDistributionImpl(numeratorDegreesOfFreedom,
|
||||
public FDistribution createFDistribution(
|
||||
double numeratorDegreesOfFreedom,
|
||||
double denominatorDegreesOfFreedom) {
|
||||
return new FDistributionImpl(numeratorDegreesOfFreedom,
|
||||
denominatorDegreesOfFreedom);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new exponential distribution with the given degrees of freedom.
|
||||
* @param mean mean.
|
||||
* @return a new exponential distribution.
|
||||
*/
|
||||
public ExponentialDistribution createExponentialDistribution(double mean) {
|
||||
return new ExponentialDistributionImpl(mean);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The Exponential Distribution
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Instances of ExponentialDistribution objects should be created using
|
||||
* {@link DistributionFactory#createExponentialDistribution(double)}
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* References:
|
||||
* <ul>
|
||||
* <li><a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
|
||||
* Exponential Distribution</a></li>
|
||||
* </p>
|
||||
*
|
||||
* @author Brent Worden
|
||||
*/
|
||||
public interface ExponentialDistribution extends ContinuousDistribution {
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean the new mean.
|
||||
*/
|
||||
void setMean(double mean);
|
||||
|
||||
/**
|
||||
* Access the mean.
|
||||
* @return the mean.
|
||||
*/
|
||||
double getMean();
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
package org.apache.commons.math.stat.distribution;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link ExponentialDistribution}
|
||||
*
|
||||
* @author Brent Worden
|
||||
*/
|
||||
public class ExponentialDistributionImpl
|
||||
extends AbstractContinuousDistribution
|
||||
implements ExponentialDistribution {
|
||||
|
||||
/** The mean of this distribution. */
|
||||
private double mean;
|
||||
|
||||
/**
|
||||
* Create a exponential distribution with the given mean.
|
||||
* @param degreesOfFreedom degrees of freedom.
|
||||
*/
|
||||
public ExponentialDistributionImpl(double mean) {
|
||||
super();
|
||||
setMean(mean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Access the domain value lower bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value lower bound, i.e.
|
||||
* P(X < <i>lower bound</i>) < <code>p</code>
|
||||
*/
|
||||
protected double getDomainLowerBound(double p){
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the domain value upper bound, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return domain value upper bound, i.e.
|
||||
* P(X < <i>upper bound</i>) > <code>p</code>
|
||||
*/
|
||||
protected double getDomainUpperBound(double p){
|
||||
return Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the initial domain value, based on <code>p</code>, used to
|
||||
* bracket a CDF root. This method is used by
|
||||
* {@link #inverseCummulativeProbability(double)} to find critical values.
|
||||
*
|
||||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
*/
|
||||
protected double getInitialDomain(double p){
|
||||
return getMean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the mean.
|
||||
* @param mean the new mean.
|
||||
*/
|
||||
public void setMean(double mean) {
|
||||
if(mean <= 0.0){
|
||||
throw new IllegalArgumentException("mean must be positive.");
|
||||
}
|
||||
this.mean = mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the mean.
|
||||
* @return the mean.
|
||||
*/
|
||||
public double getMean() {
|
||||
return mean;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* For this disbution, X, this method returns P(X < x).
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The implementation of this method is based on:
|
||||
* <ul>
|
||||
* <li>
|
||||
* <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">
|
||||
* Exponential Distribution</a>, equation (1).</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @param x the value at which the CDF is evaluated.
|
||||
* @return CDF for this distribution.
|
||||
*/
|
||||
public double cummulativeProbability(double x) {
|
||||
double ret;
|
||||
if(x < 0.0){
|
||||
ret = 0.0;
|
||||
} else {
|
||||
ret = 1.0 - Math.exp(-x / getMean());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* For this distribution, X, this method returns the critical point x, such
|
||||
* that P(X < x) = <code>p</code>.
|
||||
*
|
||||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCummulativeProbability(double p){
|
||||
if(p < 0.0 || p > 1.0){
|
||||
throw new IllegalArgumentException(
|
||||
"p must be between 0.0 and 1.0, inclusive.");
|
||||
}
|
||||
return -getMean() * Math.log(1.0 - p);
|
||||
}
|
||||
}
|
|
@ -102,6 +102,32 @@ public class DistributionFactoryImplTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testCreateExponentialDistributionNegative(){
|
||||
try {
|
||||
factory.createExponentialDistribution(-1.0);
|
||||
fail("negative mean. IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateExponentialDistributionZero(){
|
||||
try {
|
||||
factory.createExponentialDistribution(0.0);
|
||||
fail("zero mean. IllegalArgumentException expected");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateExponentialDistributionPositive(){
|
||||
try {
|
||||
factory.createExponentialDistribution(1.0);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
fail("positive mean. IllegalArgumentException is not expected");
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateGammaDistributionNegativePositive(){
|
||||
try {
|
||||
factory.createGammaDistribution(-1.0, 1.0);
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.stat.distribution;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Brent Worden
|
||||
*/
|
||||
public class ExponentialDistributionTest extends TestCase {
|
||||
private ExponentialDistribution exp;
|
||||
|
||||
/**
|
||||
* Constructor for ChiSquareDistributionTest.
|
||||
* @param name
|
||||
*/
|
||||
public ExponentialDistributionTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
exp = DistributionFactory.newInstance().createExponentialDistribution(5.0);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see TestCase#tearDown()
|
||||
*/
|
||||
protected void tearDown() throws Exception {
|
||||
exp = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testLowerTailProbability(){
|
||||
testProbability(0.005003, .001);
|
||||
testProbability(0.050252, .010);
|
||||
testProbability(0.126589, .025);
|
||||
testProbability(0.256566, .050);
|
||||
testProbability(0.526803, .100);
|
||||
}
|
||||
|
||||
public void testUpperTailProbability(){
|
||||
testProbability(34.5388, .999);
|
||||
testProbability(23.0259, .990);
|
||||
testProbability(18.4444, .975);
|
||||
testProbability(14.9787, .950);
|
||||
testProbability(11.5129, .900);
|
||||
}
|
||||
|
||||
public void testLowerTailValues(){
|
||||
testValue(0.005003, .001);
|
||||
testValue(0.050252, .010);
|
||||
testValue(0.126589, .025);
|
||||
testValue(0.256566, .050);
|
||||
testValue(0.526803, .100);
|
||||
}
|
||||
|
||||
public void testUpperTailValues(){
|
||||
testValue(34.5388, .999);
|
||||
testValue(23.0259, .990);
|
||||
testValue(18.4444, .975);
|
||||
testValue(14.9787, .950);
|
||||
testValue(11.5129, .900);
|
||||
}
|
||||
|
||||
private void testProbability(double x, double expected){
|
||||
double actual = exp.cummulativeProbability(x);
|
||||
assertEquals("probability for " + x, expected, actual, 10e-4);
|
||||
}
|
||||
|
||||
private void testValue(double expected, double p){
|
||||
double actual = exp.inverseCummulativeProbability(p);
|
||||
assertEquals("value for " + p, expected, actual, 10e-4);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue