Submitted by: brent@worden.org git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140901 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
431f303889
commit
16c19dcec0
|
@ -71,12 +71,12 @@ public abstract class ContinuedFraction {
|
|||
/** Maximum allowed numerical error. */
|
||||
private static final double DEFAULT_EPSILON = 10e-9;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
protected ContinuedFraction() {
|
||||
super();
|
||||
}
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
protected ContinuedFraction() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the n-th a coefficient of the continued fraction. Since a can be
|
||||
|
@ -101,7 +101,7 @@ public abstract class ContinuedFraction {
|
|||
* @param x the evaluation point.
|
||||
* @return the value of the continued fraction evaluated at x.
|
||||
*/
|
||||
public double evaluate(double x){
|
||||
public double evaluate(double x) {
|
||||
return evaluate(x, DEFAULT_EPSILON, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ public abstract class ContinuedFraction {
|
|||
* @param epsilon maximum error allowed.
|
||||
* @return the value of the continued fraction evaluated at x.
|
||||
*/
|
||||
public double evaluate(double x, double epsilon){
|
||||
public double evaluate(double x, double epsilon) {
|
||||
return evaluate(x, epsilon, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
@ -121,7 +121,7 @@ public abstract class ContinuedFraction {
|
|||
* @param maxIterations maximum number of convergents
|
||||
* @return the value of the continued fraction evaluated at x.
|
||||
*/
|
||||
public double evaluate(double x, int maxIterations){
|
||||
public double evaluate(double x, int maxIterations) {
|
||||
return evaluate(x, DEFAULT_EPSILON, maxIterations);
|
||||
}
|
||||
|
||||
|
@ -159,7 +159,8 @@ public abstract class ContinuedFraction {
|
|||
}
|
||||
|
||||
/**
|
||||
* Evaluates the n-th convergent, fn = pn / qn, for this continued fraction at the value x.
|
||||
* Evaluates the n-th convergent, fn = pn / qn, for this continued fraction
|
||||
* at the value x.
|
||||
* @param n the convergent to compute.
|
||||
* @param x the evaluation point.
|
||||
* @param a (n-1)-th convergent matrix. (Input)
|
||||
|
@ -167,9 +168,11 @@ public abstract class ContinuedFraction {
|
|||
* @param f the n-th convergent matrix. (Output)
|
||||
* @param epsilon maximum error allowed.
|
||||
* @param maxIterations maximum number of convergents
|
||||
* @return the value of the the n-th convergent for this continued fraction evaluated at x.
|
||||
* @return the value of the the n-th convergent for this continued fraction
|
||||
* evaluated at x.
|
||||
*/
|
||||
private double evaluate(int n, double x, double[][] a, double[][] an, double[][] f, double epsilon, int maxIterations) {
|
||||
private double evaluate(int n, double x, double[][] a, double[][] an,
|
||||
double[][] f, double epsilon, int maxIterations) {
|
||||
double ret;
|
||||
|
||||
// create next matrix
|
||||
|
@ -185,14 +188,17 @@ public abstract class ContinuedFraction {
|
|||
f[1][1] = (a[1][0] * an[0][1]) + (a[1][1] * an[1][1]);
|
||||
|
||||
// determine if we're close enough
|
||||
if(Math.abs((f[0][0] * f[1][1]) - (f[1][0] * f[0][1])) < Math.abs(epsilon * f[1][0] * f[1][1])){
|
||||
if(Math.abs((f[0][0] * f[1][1]) - (f[1][0] * f[0][1])) <
|
||||
Math.abs(epsilon * f[1][0] * f[1][1])){
|
||||
ret = f[0][0] / f[1][0];
|
||||
} else {
|
||||
if(n >= maxIterations){
|
||||
throw new ConvergenceException("Continued fraction convergents failed to converge.");
|
||||
throw new ConvergenceException(
|
||||
"Continued fraction convergents failed to converge.");
|
||||
}
|
||||
// compute next
|
||||
ret = evaluate(n + 1, x, f /* new a */, an /* reuse an */, a /* new f */, epsilon, maxIterations);
|
||||
ret = evaluate(n + 1, x, f /* new a */, an /* reuse an */,
|
||||
a /* new f */, epsilon, maxIterations);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -60,19 +60,21 @@ package org.apache.commons.math;
|
|||
* @author Brent Worden
|
||||
*/
|
||||
public class ConvergenceException extends RuntimeException {
|
||||
/**
|
||||
* Construct an exception with the given message.
|
||||
* @param message descriptive error message.
|
||||
*/
|
||||
public ConvergenceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public ConvergenceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public ConvergenceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
/**
|
||||
* Construct an exception with the given message and root cause.
|
||||
* @param message descriptive error message.
|
||||
* @param cause root cause.
|
||||
*/
|
||||
public ConvergenceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ public class RootFinding {
|
|||
/**
|
||||
* Default constructor. Prohibit construction.
|
||||
*/
|
||||
private RootFinding(){
|
||||
private RootFinding() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -83,7 +83,7 @@ public class RootFinding {
|
|||
public static double[] bracket(UnivariateFunction function,
|
||||
double initial,
|
||||
double lowerBound,
|
||||
double upperBound){
|
||||
double upperBound) {
|
||||
return bracket( function, initial, lowerBound, upperBound, Integer.MAX_VALUE ) ;
|
||||
}
|
||||
|
||||
|
@ -103,7 +103,7 @@ public class RootFinding {
|
|||
double initial,
|
||||
double lowerBound,
|
||||
double upperBound,
|
||||
int maximumIterations){
|
||||
int maximumIterations) {
|
||||
double a = initial;
|
||||
double b = initial;
|
||||
double fa;
|
||||
|
@ -132,13 +132,12 @@ public class RootFinding {
|
|||
*/
|
||||
public static double bisection(UnivariateFunction function,
|
||||
double a,
|
||||
double b){
|
||||
double b) {
|
||||
double m;
|
||||
double fm;
|
||||
double fa;
|
||||
|
||||
if ( b < a )
|
||||
{
|
||||
if ( b < a ) {
|
||||
double xtemp = a ;
|
||||
a = b ;
|
||||
b = xtemp ;
|
||||
|
@ -146,11 +145,11 @@ public class RootFinding {
|
|||
|
||||
fa = function.evaluate(a);
|
||||
|
||||
while(Math.abs(a - b) > EPSILON){
|
||||
while(Math.abs(a - b) > EPSILON) {
|
||||
m = (a + b) * 0.5; // midpoint
|
||||
fm = function.evaluate(m);
|
||||
|
||||
if(fm * fa > 0.0){
|
||||
if(fm * fa > 0.0) {
|
||||
// b and m bracket the root.
|
||||
a = m;
|
||||
fa = fm;
|
||||
|
|
|
@ -93,8 +93,8 @@ public abstract class AbstractContinuousDistribution
|
|||
* @param p the desired probability
|
||||
* @return x, such that P(X < x) = <code>p</code>
|
||||
*/
|
||||
public double inverseCummulativeProbability(final double p){
|
||||
if(p < 0.0 || p > 1.0){
|
||||
public double inverseCummulativeProbability(final double p) {
|
||||
if (p < 0.0 || p > 1.0){
|
||||
throw new IllegalArgumentException(
|
||||
"p must be between 0.0 and 1.0, inclusive.");
|
||||
}
|
||||
|
|
|
@ -1,3 +1,56 @@
|
|||
/* ====================================================================
|
||||
* 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;
|
||||
|
||||
/**
|
||||
|
@ -16,7 +69,7 @@ public class ChiSquaredDistributionImpl
|
|||
* Create a Chi-Squared distribution with the given degrees of freedom.
|
||||
* @param degreesOfFreedom degrees of freedom.
|
||||
*/
|
||||
public ChiSquaredDistributionImpl(double degreesOfFreedom){
|
||||
public ChiSquaredDistributionImpl(double degreesOfFreedom) {
|
||||
super();
|
||||
setGamma(DistributionFactory.newInstance().createGammaDistribution(
|
||||
degreesOfFreedom / 2.0, 2.0));
|
||||
|
@ -56,7 +109,7 @@ public class ChiSquaredDistributionImpl
|
|||
* @return domain value lower bound, i.e.
|
||||
* P(X < <i>lower bound</i>) < <code>p</code>
|
||||
*/
|
||||
protected double getDomainLowerBound(double p){
|
||||
protected double getDomainLowerBound(double p) {
|
||||
return Double.MIN_VALUE * getGamma().getBeta();
|
||||
}
|
||||
|
||||
|
@ -69,13 +122,13 @@ public class ChiSquaredDistributionImpl
|
|||
* @return domain value upper bound, i.e.
|
||||
* P(X < <i>upper bound</i>) > <code>p</code>
|
||||
*/
|
||||
protected double getDomainUpperBound(double p){
|
||||
protected double getDomainUpperBound(double p) {
|
||||
// NOTE: chi squared is skewed to the left
|
||||
// NOTE: therefore, P(X < μ) > .5
|
||||
|
||||
double ret;
|
||||
|
||||
if(p < .5){
|
||||
if (p < .5) {
|
||||
// use mean
|
||||
ret = getDegreesOfFreedom();
|
||||
} else {
|
||||
|
@ -94,13 +147,13 @@ public class ChiSquaredDistributionImpl
|
|||
* @param p the desired probability for the critical value
|
||||
* @return initial domain value
|
||||
*/
|
||||
protected double getInitialDomain(double p){
|
||||
protected double getInitialDomain(double p) {
|
||||
// NOTE: chi squared is skewed to the left
|
||||
// NOTE: therefore, P(X < μ) > .5
|
||||
|
||||
double ret;
|
||||
|
||||
if(p < .5){
|
||||
if (p < .5) {
|
||||
// use 1/2 mean
|
||||
ret = getDegreesOfFreedom() * .5;
|
||||
} else {
|
||||
|
|
|
@ -60,7 +60,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
*
|
||||
* <p>
|
||||
* Instances of FDistribution objects should be created using
|
||||
* {@link DistributionFactory#createFDistribution(double)}
|
||||
* {@link DistributionFactory#createFDistribution(double,double)}
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
|
|
|
@ -60,7 +60,7 @@ package org.apache.commons.math.stat.distribution;
|
|||
*
|
||||
* <p>
|
||||
* Instances of GammaDistribution objects should be created using
|
||||
* {@link DistributionFactory#createGammaDistribution(double)}
|
||||
* {@link DistributionFactory#createGammaDistribution(double,double)}
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
|
|
Loading…
Reference in New Issue