Just Checkstyle and Javadoc corrections

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140991 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-08-09 04:03:41 +00:00
parent 88b4e3dbc9
commit b3b89139bb
29 changed files with 565 additions and 386 deletions

View File

@ -55,8 +55,7 @@ package org.apache.commons.math;
/** /**
* Signals a configuration problem with any of the factory methods. * Signals a configuration problem with any of the factory methods.
* * @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
* @version $Revision: 1.5 $ $Date: 2003/07/30 21:58:10 $
*/ */
public class MathConfigurationException extends MathException { public class MathConfigurationException extends MathException {
@ -71,7 +70,7 @@ public class MathConfigurationException extends MathException {
* Construct an exception with the given message. * Construct an exception with the given message.
* @param message message describing the problem * @param message message describing the problem
*/ */
public MathConfigurationException(String message) { public MathConfigurationException(final String message) {
super(message); super(message);
} }
@ -80,7 +79,9 @@ public class MathConfigurationException extends MathException {
* @param message message describing the problem * @param message message describing the problem
* @param throwable caught exception causing this problem * @param throwable caught exception causing this problem
*/ */
public MathConfigurationException(String message, Throwable throwable) { public MathConfigurationException(
final String message,
final Throwable throwable) {
super(message, throwable); super(message, throwable);
} }
@ -88,7 +89,7 @@ public class MathConfigurationException extends MathException {
* Construct an exception with the given root cause. * Construct an exception with the given root cause.
* @param throwable caught exception causing this problem * @param throwable caught exception causing this problem
*/ */
public MathConfigurationException(Throwable throwable) { public MathConfigurationException(final Throwable throwable) {
super(throwable); super(throwable);
} }
} }

View File

@ -55,13 +55,12 @@ package org.apache.commons.math;
/** /**
* A generic exception indicating problems in the math package. * A generic exception indicating problems in the math package.
* * @version $Revision: 1.5 $ $Date: 2003/08/09 04:03:41 $
* @version $Revision: 1.4 $ $Date: 2003/07/09 20:02:44 $
*/ */
public class MathException extends Exception { public class MathException extends Exception {
/** /**
* * Constructs a MathException
*/ */
public MathException() { public MathException() {
super(); super();
@ -70,7 +69,7 @@ public class MathException extends Exception {
/** /**
* @param message message describing the problem * @param message message describing the problem
*/ */
public MathException(String message) { public MathException(final String message) {
super(message); super(message);
} }
@ -78,14 +77,14 @@ public class MathException extends Exception {
* @param message message describing the problem * @param message message describing the problem
* @param throwable caught exception causing this problem * @param throwable caught exception causing this problem
*/ */
public MathException(String message, Throwable throwable) { public MathException(final String message, final Throwable throwable) {
super(message, throwable); super(message, throwable);
} }
/** /**
* @param throwable caught exception causing this problem * @param throwable caught exception causing this problem
*/ */
public MathException(Throwable throwable) { public MathException(final Throwable throwable) {
super(throwable); super(throwable);
} }

View File

@ -59,8 +59,7 @@ import org.apache.commons.math.MathException;
* Provide the bisection algorithm for solving for zeros of real univariate * Provide the bisection algorithm for solving for zeros of real univariate
* functions. It will only search for one zero in the given interval. The * functions. It will only search for one zero in the given interval. The
* function is supposed to be continuous but not necessarily smooth. * function is supposed to be continuous but not necessarily smooth.
* * @version $Revision: 1.3 $ $Date: 2003/08/09 04:03:41 $
* @version $Revision: 1.2 $ $Date: 2003/07/09 20:02:43 $
*/ */
public class BisectionSolver extends UnivariateRealSolverImpl { public class BisectionSolver extends UnivariateRealSolverImpl {
/** /**

View File

@ -62,7 +62,7 @@ import org.apache.commons.math.util.BeanTransformer;
* univariate statistics for a List of Java Beans by property. This * univariate statistics for a List of Java Beans by property. This
* implementation uses beanutils' PropertyUtils to get a simple, nested, * implementation uses beanutils' PropertyUtils to get a simple, nested,
* indexed, mapped, or combined property from an element of a List. * indexed, mapped, or combined property from an element of a List.
* @version $Revision: 1.3 $ $Date: 2003/07/09 21:45:23 $ * @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class BeanListUnivariateImpl extends ListUnivariateImpl { public class BeanListUnivariateImpl extends ListUnivariateImpl {
@ -108,9 +108,9 @@ public class BeanListUnivariateImpl extends ListUnivariateImpl {
this.transformer = new BeanTransformer(propertyName); this.transformer = new BeanTransformer(propertyName);
} }
/** /**
* @see org.apache.commons.math.Univariate#addValue(double) * @see org.apache.commons.math.Univariate#addValue(double)
*/ */
public void addValue(double v) { public void addValue(double v) {
String msg = String msg =
"The BeanListUnivariateImpl does not accept values " "The BeanListUnivariateImpl does not accept values "

View File

@ -57,16 +57,22 @@ package org.apache.commons.math.stat;
* StatUtils provides easy static implementations of common double[] based * StatUtils provides easy static implementations of common double[] based
* statistical methods. These return a single result value or in some cases, as * statistical methods. These return a single result value or in some cases, as
* identified in the javadoc for each method, Double.NaN. * identified in the javadoc for each method, Double.NaN.
* @version $Revision: 1.14 $ $Date: 2003/07/09 21:45:23 $ * @version $Revision: 1.15 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class StatUtils { public final class StatUtils {
/**
* Private Constructor
*/
private StatUtils() {
}
/** /**
* The sum of the values that have been added to Univariate. * The sum of the values that have been added to Univariate.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the sum of the values or Double.NaN if the array is empty * @return the sum of the values or Double.NaN if the array is empty
*/ */
public static double sum(double[] values) { public static double sum(final double[] values) {
return sum(values, 0, values.length); return sum(values, 0, values.length);
} }
@ -77,7 +83,10 @@ public class StatUtils {
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the sum of the values or Double.NaN if the array is empty * @return the sum of the values or Double.NaN if the array is empty
*/ */
public static double sum(double[] values, int begin, int length) { public static double sum(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double accum = 0.0; double accum = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -91,7 +100,7 @@ public class StatUtils {
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the sum of the squared values or Double.NaN if the array is empty * @return the sum of the squared values or Double.NaN if the array is empty
*/ */
public static double sumSq(double[] values) { public static double sumSq(final double[] values) {
return sumSq(values, 0, values.length); return sumSq(values, 0, values.length);
} }
@ -102,7 +111,10 @@ public class StatUtils {
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the sum of the squared values or Double.NaN if the array is empty * @return the sum of the squared values or Double.NaN if the array is empty
*/ */
public static double sumSq(double[] values, int begin, int length) { public static double sumSq(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double accum = 0.0; double accum = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -116,7 +128,7 @@ public class StatUtils {
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the product values or Double.NaN if the array is empty * @return the product values or Double.NaN if the array is empty
*/ */
public static double product(double[] values) { public static double product(final double[] values) {
return product(values, 0, values.length); return product(values, 0, values.length);
} }
@ -127,7 +139,10 @@ public class StatUtils {
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the product values or Double.NaN if the array is empty * @return the product values or Double.NaN if the array is empty
*/ */
public static double product(double[] values, int begin, int length) { public static double product(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double product = 1.0; double product = 1.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -141,7 +156,7 @@ public class StatUtils {
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the sumLog value or Double.NaN if the array is empty * @return the sumLog value or Double.NaN if the array is empty
*/ */
public static double sumLog(double[] values) { public static double sumLog(final double[] values) {
return sumLog(values, 0, values.length); return sumLog(values, 0, values.length);
} }
@ -152,7 +167,10 @@ public class StatUtils {
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the sumLog value or Double.NaN if the array is empty * @return the sumLog value or Double.NaN if the array is empty
*/ */
public static double sumLog(double[] values, int begin, int length) { public static double sumLog(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double sumLog = 0.0; double sumLog = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -163,60 +181,66 @@ public class StatUtils {
/** /**
* Returns the <a href=http://www.xycoon.com/arithmetic_mean.htm> * Returns the <a href=http://www.xycoon.com/arithmetic_mean.htm>
* arithmetic mean </a> of the available values * arithmetic mean </a> of the available values
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the mean of the values or Double.NaN if the array is empty * @return the mean of the values or Double.NaN if the array is empty
*/ */
public static double mean(double[] values) { public static double mean(final double[] values) {
return sum(values) / (double) values.length; return sum(values) / (double) values.length;
} }
/** /**
* Returns the <a href=http://www.xycoon.com/arithmetic_mean.htm> * Returns the <a href=http://www.xycoon.com/arithmetic_mean.htm>
* arithmetic mean </a> of the available values * arithmetic mean </a> of the available values
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the mean of the values or Double.NaN if the array is empty * @return the mean of the values or Double.NaN if the array is empty
*/ */
public static double mean(double[] values, int begin, int length) { public static double mean(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
return sum(values, begin, length) / ((double) length); return sum(values, begin, length) / ((double) length);
} }
/** /**
* Returns the variance of the available values. This uses a corrected * Returns the variance of the available values. This uses a corrected
* two pass algorithm of the following * two pass algorithm of the following
* <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf"> * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
* corrected two pass formula (14.1.8)</a>, and also referenced in:<p/> * corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
* "Algorithms for Computing the Sample Variance: Analysis and * "Algorithms for Computing the Sample Variance: Analysis and
* Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
* 1983, American Statistician, vol. 37, pp. 242?247. * 1983, American Statistician, vol. 37, pp. 242?247.
* *
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the result, Double.NaN if no values for an empty array * @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set. * or 0.0 for a single value set.
*/ */
public static double variance(double[] values) { public static double variance(final double[] values) {
return variance(values, 0, values.length); return variance(values, 0, values.length);
} }
/** /**
* Returns the variance of the available values. This uses a corrected * Returns the variance of the available values. This uses a corrected
* two pass algorithm of the following * two pass algorithm of the following
* <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf"> * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
* corrected two pass formula (14.1.8)</a>, and also referenced in:<p/> * corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
* "Algorithms for Computing the Sample Variance: Analysis and * "Algorithms for Computing the Sample Variance: Analysis and
* Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
* 1983, American Statistician, vol. 37, pp. 242?247. * 1983, American Statistician, vol. 37, pp. 242?247.
* *
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the result, Double.NaN if no values for an empty array * @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set. * or 0.0 for a single value set.
*/ */
public static double variance(double[] values, int begin, int length) { public static double variance(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double variance = Double.NaN; double variance = Double.NaN;
@ -231,7 +255,7 @@ public class StatUtils {
accum2 += (values[i] - mean); accum2 += (values[i] - mean);
} }
variance = variance =
(accum - (Math.pow(accum2, 2) / ((double)length))) (accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1); / (double) (length - 1);
} }
return variance; return variance;
@ -242,7 +266,7 @@ public class StatUtils {
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the maximum of the values or Double.NaN if the array is empty * @return the maximum of the values or Double.NaN if the array is empty
*/ */
public static double max(double[] values) { public static double max(final double[] values) {
return max(values, 0, values.length); return max(values, 0, values.length);
} }
@ -253,14 +277,19 @@ public class StatUtils {
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the maximum of the values or Double.NaN if the array is empty * @return the maximum of the values or Double.NaN if the array is empty
*/ */
public static double max(double[] values, int begin, int length) { public static double max(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double max = Double.NaN; double max = Double.NaN;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
if (i == 0) { if (i == 0) {
max = values[i]; max = values[i];
} else { } else {
max = (max > values[i]) ? max : values[i]; if (max < values[i]) {
max = values[i];
}
} }
} }
return max; return max;
@ -271,7 +300,7 @@ public class StatUtils {
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the minimum of the values or Double.NaN if the array is empty * @return the minimum of the values or Double.NaN if the array is empty
*/ */
public static double min(double[] values) { public static double min(final double[] values) {
return min(values, 0, values.length); return min(values, 0, values.length);
} }
@ -282,7 +311,11 @@ public class StatUtils {
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the minimum of the values or Double.NaN if the array is empty * @return the minimum of the values or Double.NaN if the array is empty
*/ */
public static double min(double[] values, int begin, int length) { public static double min(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length); testInput(values, begin, length);
double min = Double.NaN; double min = Double.NaN;
@ -290,29 +323,37 @@ public class StatUtils {
if (i == 0) { if (i == 0) {
min = values[i]; min = values[i];
} else { } else {
min = (min < values[i]) ? min : values[i]; if (min > values[i]) {
min = values[i];
}
} }
} }
return min; return min;
} }
/** /**
* Private testInput method used by all methods to verify the content * Private testInput method used by all methods to verify the content
* of the array and indicies are correct. * of the array and indicies are correct.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
*/ */
private static void testInput(double[] values, int begin, int length) { private static void testInput(
final double[] values,
final int begin,
final int length) {
if (length > values.length) if (length > values.length) {
throw new IllegalArgumentException("length > values.length"); throw new IllegalArgumentException("length > values.length");
}
if (begin + length > values.length) if (begin + length > values.length) {
throw new IllegalArgumentException("begin + length > values.length"); throw new IllegalArgumentException(
"begin + length > values.length");
}
if (values == null) if (values == null) {
throw new IllegalArgumentException("input value array is null"); throw new IllegalArgumentException("input value array is null");
}
} }
} }

View File

@ -59,21 +59,23 @@ package org.apache.commons.math.stat.univariate;
* Provides the ability to extend polymophically so that * Provides the ability to extend polymophically so that
* indiviual statistics do not need to implement these methods unless * indiviual statistics do not need to implement these methods unless
* there are better algorithms for handling the calculation. * there are better algorithms for handling the calculation.
* @version $Revision: 1.6 $ $Date: 2003/07/30 21:58:11 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public abstract class AbstractStorelessUnivariateStatistic public abstract class AbstractStorelessUnivariateStatistic
extends AbstractUnivariateStatistic extends AbstractUnivariateStatistic
implements StorelessUnivariateStatistic { implements StorelessUnivariateStatistic {
/** /**
* This implements the AbstractUnivariateStatistic impl to funnel * This implements the AbstractUnivariateStatistic impl to funnel
* calculation off to the instantanious increment method. In most cases of * calculation off to the instantanious increment method. In most cases of
* StorelessUnivariateStatistic this is never really used because more * StorelessUnivariateStatistic this is never really used because more
* efficient algorithms are available for that statistic. * efficient algorithms are available for that statistic.
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
* UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
if (this.test(values, begin, length)) { if (this.test(values, begin, length)) {
this.clear(); this.clear();
int l = begin + length; int l = begin + length;
@ -84,4 +86,19 @@ public abstract class AbstractStorelessUnivariateStatistic
return getResult(); return getResult();
} }
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public abstract void clear();
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public abstract double getResult();
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public abstract void increment(final double d);
} }

View File

@ -56,8 +56,8 @@ package org.apache.commons.math.stat.univariate;
/** /**
* Abstract Implementation for UnivariateStatistics. * Abstract Implementation for UnivariateStatistics.
* Provides the ability to extend polymophically so that * Provides the ability to extend polymophically so that
* indiviual statistics do not need to implement these methods. * indiviual statistics do not need to implement these methods.
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:37:10 $ * @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
*/ */
public abstract class AbstractUnivariateStatistic public abstract class AbstractUnivariateStatistic
implements UnivariateStatistic { implements UnivariateStatistic {
@ -65,36 +65,43 @@ public abstract class AbstractUnivariateStatistic
/** /**
* This implementation provides a simple wrapper around the double[] * This implementation provides a simple wrapper around the double[]
* and passes the request onto the evaluate(DoubleArray da) method. * and passes the request onto the evaluate(DoubleArray da) method.
* * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[])
* @see org.apache.commons.math.stat.univariate.
* UnivariateStatistic#evaluate(double[])
*/ */
public double evaluate(double[] values) { public double evaluate(final double[] values) {
return evaluate(values, 0, values.length); return evaluate(values, 0, values.length);
} }
/** /**
* Subclasses of AbstractUnivariateStatistc need to implement this method. * Subclasses of AbstractUnivariateStatistc need to implement this method.
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
* UnivariateStatistic#evaluate(double[], int, int)
*/ */
public abstract double evaluate(double[] values, int begin, int length); public abstract double evaluate(
final double[] values,
final int begin,
final int length);
/** /**
* this protected test method used by all methods to verify the content * this protected test method used by all methods to verify the content
* of the array and indicies are correct. * of the array and indicies are correct.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return this is used to determine if the array is of 0 length or not,
* it is used by an individual statistic to determine if continuation
* of a statistical calculation should continue or return NaN.
*/ */
protected boolean test(double[] values, int begin, int length) { protected boolean test(
final double[] values,
final int begin,
final int length) {
if (length > values.length) { if (length > values.length) {
throw new IllegalArgumentException("length > values.length"); throw new IllegalArgumentException("length > values.length");
} }
if (begin + length > values.length) { if (begin + length > values.length) {
throw new IllegalArgumentException("begin + length > values.length"); throw new IllegalArgumentException(
"begin + length > values.length");
} }
if (values == null) { if (values == null) {

View File

@ -56,13 +56,13 @@ package org.apache.commons.math.stat.univariate;
/** /**
* Extends the capabilities of UnivariateStatistic with a statefull incremental * Extends the capabilities of UnivariateStatistic with a statefull incremental
* strategy through three methods for calculating a statistic without having to * strategy through three methods for calculating a statistic without having to
* maintain a double[] of the values. Because a StorelessUnivariateStatistic * maintain a double[] of the values. Because a StorelessUnivariateStatistic
* does not require that a double[] storage structure be maintained with the * does not require that a double[] storage structure be maintained with the
* values in it, there are only a subset of known statistics can actually be * values in it, there are only a subset of known statistics can actually be
* implemented using it. If a Statistic cannot be implemented in a Storeless * implemented using it. If a Statistic cannot be implemented in a Storeless
* approach it should implement the UnivariateStatistic interface directly * approach it should implement the UnivariateStatistic interface directly
* instead. * instead.
* @version $Revision: 1.6 $ $Date: 2003/07/15 03:37:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public interface StorelessUnivariateStatistic extends UnivariateStatistic { public interface StorelessUnivariateStatistic extends UnivariateStatistic {

View File

@ -54,31 +54,31 @@
package org.apache.commons.math.stat.univariate; package org.apache.commons.math.stat.univariate;
/** /**
* UnivariateStatistic interface provides methods to evaluate * UnivariateStatistic interface provides methods to evaluate
* double[] based content using an implemented statistical approach. * double[] based content using an implemented statistical approach.
* The interface provides two "stateless" simple methods to calculate * The interface provides two "stateless" simple methods to calculate
* a statistic from a double[] based parameter. * a statistic from a double[] based parameter.
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:37:10 $ * @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
*/ */
public interface UnivariateStatistic { public interface UnivariateStatistic {
/** /**
* Evaluates the double[] returning the result of the evaluation. * Evaluates the double[] returning the result of the evaluation.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @return the result of the evaluation or Double.NaN * @return the result of the evaluation or Double.NaN
* if the array is empty * if the array is empty
*/ */
double evaluate(double[] values); double evaluate(double[] values);
/** /**
* Evaluates part of a double[] returning the result * Evaluates part of a double[] returning the result
* of the evaluation. * of the evaluation.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the result of the evaluation or Double.NaN * @return the result of the evaluation or Double.NaN
* if the array is empty * if the array is empty
*/ */
double evaluate(double[] values, int begin, int length); double evaluate(double[] values, int begin, int length);
} }

View File

@ -57,12 +57,12 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
/** /**
* FirstMoment.java * FirstMoment.java
* *
* The FirstMoment (arithmentic mean) is calculated using the following * The FirstMoment (arithmentic mean) is calculated using the following
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy * recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach. * </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:36:36 $ * @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class FirstMoment extends AbstractStorelessUnivariateStatistic { public class FirstMoment extends AbstractStorelessUnivariateStatistic {
@ -72,29 +72,28 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
/** first moment of values that have been added */ /** first moment of values that have been added */
protected double m1 = Double.NaN; protected double m1 = Double.NaN;
/** /**
* temporary internal state made available for * temporary internal state made available for
* higher order moments * higher order moments
*/ */
protected double dev = 0.0; protected double dev = 0.0;
/** /**
* temporary internal state made available for * temporary internal state made available for
* higher order moments * higher order moments
*/ */
protected double v = 0.0; protected double v = 0.0;
/** /**
* temporary internal state made available for * temporary internal state made available for
* higher order moments * higher order moments
*/ */
protected double n0 = 0.0; protected double n0 = 0.0;
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
* StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (n < 1) { if (n < 1) {
m1 = 0.0; m1 = 0.0;
} }
@ -108,8 +107,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
* StorelessUnivariateStatistic#clear()
*/ */
public void clear() { public void clear() {
m1 = Double.NaN; m1 = Double.NaN;
@ -120,8 +118,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
* StorelessUnivariateStatistic#getValue()
*/ */
public double getResult() { public double getResult() {
return m1; return m1;

View File

@ -54,49 +54,52 @@
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
/** /**
* The FourthMoment is calculated using the following * The FourthMoment is calculated using the following
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy * recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach. * </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class FourthMoment extends ThirdMoment { public class FourthMoment extends ThirdMoment {
/** fourth moment of values that have been added */ /** fourth moment of values that have been added */
protected double m4 = Double.NaN; protected double m4 = Double.NaN;
/** temporary internal state made available for higher order moments */ /** temporary internal state made available for higher order moments */
protected double prevM3 = 0.0; protected double prevM3 = 0.0;
/** temporary internal state made available for higher order moments */ /** temporary internal state made available for higher order moments */
protected double n3 = 0.0; protected double n3 = 0.0;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (n < 1) { if (n < 1) {
m4 = m3 = m2 = m1 = 0.0; m4 = 0.0;
m3 = 0.0;
m2 = 0.0;
m1 = 0.0;
} }
/* retain previous m3 */ /* retain previous m3 */
prevM3 = m3; prevM3 = m3;
/* increment m1, m2 and m3 (and prevM2, _n0, _n1, _n2, _v, _v2) */ /* increment m1, m2 and m3 (and prevM2, _n0, _n1, _n2, _v, _v2) */
super.increment(d); super.increment(d);
n3 = (double) (n - 3); n3 = (double) (n - 3);
m4 = m4 =
m4 m4
- (4.0 * v * prevM3) - (4.0 * v * prevM3)
+ (6.0 * v2 * prevM2) + (6.0 * v2 * prevM2)
+ ((n0 * n0) - 3 * n1) * (v2 * v2 * n1 * n0); + ((n0 * n0) - 3 * n1) * (v2 * v2 * n1 * n0);
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return m4; return m4;

View File

@ -55,28 +55,32 @@ package org.apache.commons.math.stat.univariate.moment;
import org.apache.commons.math.stat.univariate.summary.SumOfLogs; import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
/** /**
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm"> * Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
* geometric mean </a> of the available values * geometric mean </a> of the available values
* @version $Revision: 1.8 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.9 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class GeometricMean extends SumOfLogs { public class GeometricMean extends SumOfLogs {
/** */
protected int n = 0; protected int n = 0;
/** */
private double geoMean = Double.NaN; private double geoMean = Double.NaN;
/** */
private double lastSum = 0.0; private double lastSum = 0.0;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
n++; n++;
super.increment(d); super.increment(d);
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
if (lastSum != super.getResult() || n == 1) { if (lastSum != super.getResult() || n == 1) {
@ -105,7 +109,10 @@ public class GeometricMean extends SumOfLogs {
* any of the values are &lt;= 0. * any of the values are &lt;= 0.
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
return Math.exp( return Math.exp(
super.evaluate(values, begin, length) / (double) length); super.evaluate(values, begin, length) / (double) length);
} }

View File

@ -62,23 +62,34 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class Kurtosis extends AbstractStorelessUnivariateStatistic { public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/** */
protected FourthMoment moment = null; protected FourthMoment moment = null;
/** */
protected boolean incMoment = true; protected boolean incMoment = true;
/** */
private double kurtosis = Double.NaN; private double kurtosis = Double.NaN;
/** */
private int n = 0; private int n = 0;
/**
* Construct a Kurtosis
*/
public Kurtosis() { public Kurtosis() {
moment = new FourthMoment(); moment = new FourthMoment();
} }
public Kurtosis(FourthMoment m4) { /**
* Construct a Kurtosis with an external moment
* @param m4 external Moment
*/
public Kurtosis(final FourthMoment m4) {
incMoment = false; incMoment = false;
this.moment = m4; this.moment = m4;
} }
@ -86,14 +97,14 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (incMoment) { if (incMoment) {
moment.increment(d); moment.increment(d);
} }
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
if (n < moment.n) { if (n < moment.n) {
@ -118,7 +129,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
} }
n = moment.n; n = moment.n;
} }
return kurtosis; return kurtosis;
} }
@ -135,26 +146,29 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/*UnvariateStatistic Approach */ /*UnvariateStatistic Approach */
/** */
Mean mean = new Mean(); Mean mean = new Mean();
/** /**
* This algorithm uses a corrected two pass algorithm of the following * This algorithm uses a corrected two pass algorithm of the following
* <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf"> * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
* corrected two pass formula (14.1.8)</a>, and also referenced in: * corrected two pass formula (14.1.8)</a>, and also referenced in:
* <p> * <p>
* "Algorithms for Computing the Sample Variance: Analysis and * "Algorithms for Computing the Sample Variance: Analysis and
* Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
* 1983, American Statistician, vol. 37, pp. 242?247. * 1983, American Statistician, vol. 37, pp. 242?247.
* </p> * </p>
* Returns the kurtosis for this collection of values. Kurtosis is a * Returns the kurtosis for this collection of values. Kurtosis is a
* measure of the "peakedness" of a distribution. * measure of the "peakedness" of a distribution.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the kurtosis of the values or Double.NaN if the array is empty * @return the kurtosis of the values or Double.NaN if the array is empty
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
; final double[] values,
final int begin,
final int length) {
// Initialize the kurtosis // Initialize the kurtosis
double kurt = Double.NaN; double kurt = Double.NaN;
@ -167,8 +181,9 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
// Get the mean and the standard deviation // Get the mean and the standard deviation
double m = mean.evaluate(values, begin, length); double m = mean.evaluate(values, begin, length);
// Calc the std, this is implemented here instead of using the // Calc the std, this is implemented here instead
// standardDeviation method eliminate a duplicate pass to get the mean // of using the standardDeviation method eliminate
// a duplicate pass to get the mean
double accum = 0.0; double accum = 0.0;
double accum2 = 0.0; double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -181,7 +196,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
(accum - (Math.pow(accum2, 2) / ((double) length))) (accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1)); / (double) (length - 1));
// Sum the ^4 of the distance from the mean divided by the // Sum the ^4 of the distance from the mean divided by the
// standard deviation // standard deviation
double accum3 = 0.0; double accum3 = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -189,12 +204,12 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
} }
// Get N // Get N
double n = length; double n0 = length;
double coefficientOne = double coefficientOne =
(n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3)); (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
double termTwo = double termTwo =
((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3))); ((3 * Math.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3)));
// Calculate kurtosis // Calculate kurtosis
kurt = (coefficientOne * accum3) - termTwo; kurt = (coefficientOne * accum3) - termTwo;

View File

@ -53,26 +53,38 @@
*/ */
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
import org.apache.commons.math.stat.univariate.summary.Sum; import org.apache.commons.math.stat.univariate.summary.Sum;
/** /**
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm"> * Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
* arithmetic mean </a> of the available values. * arithmetic mean </a> of the available values.
* @version $Revision: 1.7 $ $Date: 2003/07/15 03:36:36 $ * @version $Revision: 1.8 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class Mean extends AbstractStorelessUnivariateStatistic { public class Mean extends AbstractStorelessUnivariateStatistic {
/** first moment of values that have been added */ /** first moment of values that have been added */
protected FirstMoment moment = null; protected FirstMoment moment = null;
/** */
protected boolean incMoment = true; protected boolean incMoment = true;
/** */
public Mean() { public Mean() {
moment = new FirstMoment(); moment = new FirstMoment();
} }
public Mean(FirstMoment m1) { /**
* Constructs a Mean with an External Moment.
* @param m1 the moment
*/
public Mean(final FirstMoment m1) {
this.moment = m1; this.moment = m1;
incMoment = false; incMoment = false;
} }
@ -80,7 +92,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (incMoment) { if (incMoment) {
moment.increment(d); moment.increment(d);
} }
@ -96,15 +108,17 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return moment.m1; return moment.m1;
} }
/*UnvariateStatistic Approach */ /*UnvariateStatistic Approach */
Sum sum = new Sum();
/** */
protected Sum sum = new Sum();
/** /**
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm"> * Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
* arithmetic mean </a> of a double[] of the available values. * arithmetic mean </a> of a double[] of the available values.
@ -114,7 +128,10 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
* @return the mean of the values or Double.NaN if the array is empty * @return the mean of the values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
if (test(values, begin, length)) { if (test(values, begin, length)) {
return sum.evaluate(values) / ((double) length); return sum.evaluate(values) / ((double) length);
} }

View File

@ -54,11 +54,11 @@
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
/** /**
* The SecondMoment is calculated using the following * The SecondMoment is calculated using the following
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy * recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach. * </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class SecondMoment extends FirstMoment { public class SecondMoment extends FirstMoment {
@ -67,20 +67,20 @@ public class SecondMoment extends FirstMoment {
/** temporary internal state made availabel for higher order moments */ /** temporary internal state made availabel for higher order moments */
protected double n1 = 0.0; protected double n1 = 0.0;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (n < 1) { if (n < 1) {
m1 = m2 = 0.0; m1 = m2 = 0.0;
} }
/* increment m1 and _n0, _dev, _v) */ /* increment m1 and _n0, _dev, _v) */
super.increment(d); super.increment(d);
n1 = n0 - 1; n1 = n0 - 1;
/* increment and return m2 */ /* increment and return m2 */
m2 += n1 * dev * v; m2 += n1 * dev * v;
@ -96,7 +96,7 @@ public class SecondMoment extends FirstMoment {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return m2; return m2;

View File

@ -62,24 +62,35 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* *
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class Skewness extends AbstractStorelessUnivariateStatistic { public class Skewness extends AbstractStorelessUnivariateStatistic {
/** */
protected ThirdMoment moment = null; protected ThirdMoment moment = null;
/** */
protected boolean incMoment = true; protected boolean incMoment = true;
/** */
protected double skewness = Double.NaN; protected double skewness = Double.NaN;
/** */
private int n = 0; private int n = 0;
/**
* Constructs a Skewness
*/
public Skewness() { public Skewness() {
moment = new ThirdMoment(); moment = new ThirdMoment();
} }
public Skewness(ThirdMoment m3) { /**
* Constructs a Skewness with an external moment
* @param m3 external moment
*/
public Skewness(final ThirdMoment m3) {
incMoment = false; incMoment = false;
this.moment = m3; this.moment = m3;
} }
@ -87,14 +98,14 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (incMoment) { if (incMoment) {
moment.increment(d); moment.increment(d);
} }
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
if (n < moment.n) { if (n < moment.n) {
@ -130,29 +141,33 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
skewness = Double.NaN; skewness = Double.NaN;
n = 0; n = 0;
} }
/*UnvariateStatistic Approach */ /*UnvariateStatistic Approach */
/** */
Mean mean = new Mean(); Mean mean = new Mean();
/** /**
* This algorithm uses a corrected two pass algorithm of the following * This algorithm uses a corrected two pass algorithm of the following
* <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf"> * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
* corrected two pass formula (14.1.8)</a>, and also referenced in: * corrected two pass formula (14.1.8)</a>, and also referenced in
* <p> * <p>
* "Algorithms for Computing the Sample Variance: Analysis and * "Algorithms for Computing the Sample Variance: Analysis and
* Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
* 1983, American Statistician, vol. 37, pp. 242?247. * 1983, American Statistician, vol. 37, pp. 242?247.
* </p> * </p>
* Returns the skewness of a collection of values. Skewness is a * Returns the skewness of a collection of values. Skewness is a
* measure of the assymetry of a given distribution. * measure of the assymetry of a given distribution.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the skewness of the values or Double.NaN if the array is empty * @return the skewness of the values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
// Initialize the skewness // Initialize the skewness
double skew = Double.NaN; double skew = Double.NaN;
@ -165,8 +180,9 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
// Get the mean and the standard deviation // Get the mean and the standard deviation
double m = mean.evaluate(values, begin, length); double m = mean.evaluate(values, begin, length);
// Calc the std, this is implemented here instead of using the // Calc the std, this is implemented here instead
// standardDeviation method eliminate a duplicate pass to get the mean // of using the standardDeviation method eliminate
// a duplicate pass to get the mean
double accum = 0.0; double accum = 0.0;
double accum2 = 0.0; double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -178,7 +194,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
(accum - (Math.pow(accum2, 2) / ((double) length))) (accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1)); / (double) (length - 1));
// Calculate the skew as the sum the cubes of the distance // Calculate the skew as the sum the cubes of the distance
// from the mean divided by the standard deviation. // from the mean divided by the standard deviation.
double accum3 = 0.0; double accum3 = 0.0;
for (int i = begin; i < begin + length; i++) { for (int i = begin; i < begin + length; i++) {
@ -186,10 +202,10 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
} }
// Get N // Get N
double n = length; double n0 = length;
// Calculate skewness // Calculate skewness
skew = (n / ((n - 1) * (n - 2))) * accum3; skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
} }
} }

View File

@ -54,32 +54,41 @@
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
/** /**
* *
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class StandardDeviation extends Variance { public class StandardDeviation extends Variance {
/** */
protected double std = Double.NaN; protected double std = Double.NaN;
/** */
private double lastVar = 0.0; private double lastVar = 0.0;
/**
* Constructs a StandardDeviation
*/
public StandardDeviation() { public StandardDeviation() {
super(); super();
} }
public StandardDeviation(SecondMoment m2) { /**
* Constructs a StandardDeviation with an external moment
* @param m2 the external moment
*/
public StandardDeviation(final SecondMoment m2) {
super(m2); super(m2);
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
super.increment(d); super.increment(d);
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
if (lastVar != super.getResult()) { if (lastVar != super.getResult()) {
@ -104,15 +113,19 @@ public class StandardDeviation extends Variance {
} }
/** /**
* Returns the Standard Deviation on an array of values. * Returns the Standard Deviation on an array of values.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the result, Double.NaN if no values for an empty array * @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set. * or 0.0 for a single value set.
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double var = super.evaluate(values, begin, length); double var = super.evaluate(values, begin, length);
if (Double.isNaN(var)) { if (Double.isNaN(var)) {

View File

@ -54,40 +54,40 @@
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
/** /**
* The ThirdMoment (arithmentic mean) is calculated using the following * The ThirdMoment (arithmentic mean) is calculated using the following
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy * recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach. * </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class ThirdMoment extends SecondMoment{ public class ThirdMoment extends SecondMoment {
/** third moment of values that have been added */ /** third moment of values that have been added */
protected double m3 = Double.NaN; protected double m3 = Double.NaN;
/** temporary internal state made availabel for higher order moments */ /** temporary internal state made availabel for higher order moments */
protected double v2 = 0.0; protected double v2 = 0.0;
/** temporary internal state made availabel for higher order moments */ /** temporary internal state made availabel for higher order moments */
protected double n2 = 0.0; protected double n2 = 0.0;
/** temporary internal state made availabel for higher order moments */ /** temporary internal state made availabel for higher order moments */
protected double prevM2 = 0.0; protected double prevM2 = 0.0;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (n < 1) { if (n < 1) {
m3 = m2 = m1 = 0.0; m3 = m2 = m1 = 0.0;
} }
/* retain a reference to the last m2*/ /* retain a reference to the last m2*/
prevM2 = m2; prevM2 = m2;
/* increment m1 and m2 (and _n0, _n1, _v) */ /* increment m1 and m2 (and _n0, _n1, _v) */
super.increment(d); super.increment(d);
v2 = v * v; v2 = v * v;
n2 = (double) (n - 2); n2 = (double) (n - 2);
@ -96,7 +96,7 @@ public class ThirdMoment extends SecondMoment{
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return m3; return m3;

View File

@ -57,39 +57,61 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
/** /**
* *
* @version $Revision: 1.7 $ $Date: 2003/07/15 03:36:36 $ * @version $Revision: 1.8 $ $Date: 2003/08/09 04:03:40 $
*/ */
public class Variance extends AbstractStorelessUnivariateStatistic { public class Variance extends AbstractStorelessUnivariateStatistic {
/** SecondMoment is used in incremental calculation of Variance*/
protected SecondMoment moment = null; protected SecondMoment moment = null;
/**
* Boolean test to determine if this Variance should also increment
* the second moment, this evaluates to false when this Variance is
* constructed with an external SecondMoment as a parameter.
*/
protected boolean incMoment = true; protected boolean incMoment = true;
/**
* This property maintains the latest calculated
* variance for efficiency when getResult() is called
* many times between increments.
*/
protected double variance = Double.NaN; protected double variance = Double.NaN;
/**
* Maintains the current count of inrementations that have occured.
* If the external SecondMoment is used, the this is updated from
* that moments counter
*/
protected int n = 0; protected int n = 0;
/**
* Constructs a Variance.
*/
public Variance() { public Variance() {
moment = new SecondMoment(); moment = new SecondMoment();
} }
public Variance(SecondMoment m2) { /**
* Constructs a Variance based on an externalized second moment.
* @param m2 the SecondMoment (Thrid or Fourth moments work
* here as well.)
*/
public Variance(final SecondMoment m2) {
incMoment = false; incMoment = false;
this.moment = m2; this.moment = m2;
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
* StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (incMoment) { if (incMoment) {
moment.increment(d); moment.increment(d);
} }
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
* StorelessUnivariateStatistic#getValue()
*/ */
public double getResult() { public double getResult() {
if (n < moment.n) { if (n < moment.n) {
@ -98,7 +120,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
} else if (moment.n <= 1) { } else if (moment.n <= 1) {
variance = 0.0; variance = 0.0;
} else { } else {
variance = moment.m2 / (moment.n0 - 1); variance = moment.m2 / (moment.n0 - 1);
} }
n = moment.n; n = moment.n;
} }
@ -107,8 +129,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
* StorelessUnivariateStatistic#clear()
*/ */
public void clear() { public void clear() {
if (incMoment) { if (incMoment) {
@ -118,28 +139,30 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
n = 0; n = 0;
} }
/*UnvariateStatistic Approach */ /** Mean to be used in UnvariateStatistic evaluation approach. */
Mean mean = new Mean(); protected Mean mean = new Mean();
/** /**
* Returns the variance of the available values. This uses a corrected * Returns the variance of the available values. This uses a corrected
* two pass algorithm of the following * two pass algorithm of the following
* <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf"> * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
* corrected two pass formula (14.1.8)</a>, and also referenced in: * corrected two pass formula (14.1.8)</a>, and also referenced in:
* <p> * <p>
* "Algorithms for Computing the Sample Variance: Analysis and * "Algorithms for Computing the Sample Variance: Analysis and
* Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
* 1983, American Statistician, vol. 37, pp. 242?247. * 1983, American Statistician, vol. 37, pp. 242?247.
* </p> * </p>
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the result, Double.NaN if no values for an empty array * @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set. * or 0.0 for a single value set.
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
* UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double var = Double.NaN; double var = Double.NaN;

View File

@ -62,16 +62,17 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:12 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class Max extends AbstractStorelessUnivariateStatistic { public class Max extends AbstractStorelessUnivariateStatistic {
/** */
private double value = Double.NaN; private double value = Double.NaN;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
value = Double.isNaN(value) ? d : Math.max(value, d); value = Double.isNaN(value) ? d : Math.max(value, d);
} }
@ -83,16 +84,19 @@ public class Max extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return value; return value;
} }
/* (non-Javadoc) /**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double max = Double.NaN; double max = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
max = values[begin]; max = values[begin];

View File

@ -55,12 +55,15 @@ package org.apache.commons.math.stat.univariate.rank;
/** /**
* @version $Revision: 1.3 $ $Date: 2003/07/09 20:04:12 $ * @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class Median extends Percentile { public class Median extends Percentile {
/**
*
*/
public Median() { public Median() {
super(50.0); super(50.0);
} }
} }

View File

@ -62,16 +62,17 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:12 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class Min extends AbstractStorelessUnivariateStatistic { public class Min extends AbstractStorelessUnivariateStatistic {
/** */
private double value = Double.NaN; private double value = Double.NaN;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
value = Double.isNaN(value) ? d : Math.min(value, d); value = Double.isNaN(value) ? d : Math.min(value, d);
} }
@ -81,9 +82,9 @@ public class Min extends AbstractStorelessUnivariateStatistic {
public void clear() { public void clear() {
value = Double.NaN; value = Double.NaN;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return value; return value;
@ -92,7 +93,10 @@ public class Min extends AbstractStorelessUnivariateStatistic {
/** /**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double min = Double.NaN; double min = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
min = values[begin]; min = values[begin];

View File

@ -57,10 +57,11 @@ import java.util.Arrays;
import org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic; import org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic;
/** /**
* @version $Revision: 1.4 $ $Date: 2003/07/09 20:04:12 $ * @version $Revision: 1.5 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class Percentile extends AbstractUnivariateStatistic { public class Percentile extends AbstractUnivariateStatistic {
/** */
private double percentile = 0.0; private double percentile = 0.0;
/** /**
@ -71,50 +72,58 @@ public class Percentile extends AbstractUnivariateStatistic {
super(); super();
percentile = 50.0; percentile = 50.0;
} }
/** /**
* Constructs a Percentile with the specific percentile value. * Constructs a Percentile with the specific percentile value.
* @param percentile * @param p the percentile
*/ */
public Percentile(double percentile) { public Percentile(final double p) {
this.percentile = percentile; this.percentile = p;
} }
/** /**
* Evaluates the double[] top the specified percentile. * Evaluates the double[] top the specified percentile.
* This does not alter the interal percentile state of the * This does not alter the interal percentile state of the
* statistic. * statistic.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param p Is the percentile to evaluate to. * @param p Is the percentile to evaluate to.
* @return the result of the evaluation or Double.NaN * @return the result of the evaluation or Double.NaN
* if the array is empty * if the array is empty
*/ */
public double evaluate(double[] values, double p) { public double evaluate(final double[] values, final double p) {
return evaluate(values, 0,values.length, p); return evaluate(values, 0, values.length, p);
} }
/** /**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int start, int length) { public double evaluate(
final double[] values,
final int start,
final int length) {
return evaluate(values, start, length, percentile); return evaluate(values, start, length, percentile);
} }
/** /**
* Evaluates the double[] top the specified percentile. * Evaluates the double[] top the specified percentile.
* This does not alter the interal percentile state of the * This does not alter the interal percentile state of the
* statistic. * statistic.
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @param p Is the percentile to evaluate to.* * @param p Is the percentile to evaluate to.*
* @return the result of the evaluation or Double.NaN * @return the result of the evaluation or Double.NaN
* if the array is empty * if the array is empty
*/ */
public double evaluate(double[] values, int begin, int length, double p) { public double evaluate(
final double[] values,
final int begin,
final int length,
final double p) {
test(values, begin, length);
test(values,begin,length);
if ((p > 100) || (p <= 0)) { if ((p > 100) || (p <= 0)) {
throw new IllegalArgumentException("invalid percentile value"); throw new IllegalArgumentException("invalid percentile value");
} }
@ -130,9 +139,9 @@ public class Percentile extends AbstractUnivariateStatistic {
int intPos = (int) fpos; int intPos = (int) fpos;
double dif = pos - fpos; double dif = pos - fpos;
double[] sorted = new double[length]; double[] sorted = new double[length];
System.arraycopy(values, begin,sorted, 0, length); System.arraycopy(values, begin, sorted, 0, length);
Arrays.sort(sorted); Arrays.sort(sorted);
if (pos < 1) { if (pos < 1) {
return sorted[0]; return sorted[0];
} }
@ -143,7 +152,7 @@ public class Percentile extends AbstractUnivariateStatistic {
double upper = sorted[intPos]; double upper = sorted[intPos];
return lower + dif * (upper - lower); return lower + dif * (upper - lower);
} }
/** /**
* The default internal state of this percentile can be set. * The default internal state of this percentile can be set.
* This will return that value. * This will return that value.
@ -158,7 +167,7 @@ public class Percentile extends AbstractUnivariateStatistic {
* This will setthat value. * This will setthat value.
* @param p a value between 0 <= p <= 100 * @param p a value between 0 <= p <= 100
*/ */
public void setPercentile(double p) { public void setPercentile(final double p) {
percentile = p; percentile = p;
} }

View File

@ -62,7 +62,7 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:13 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class Product extends AbstractStorelessUnivariateStatistic { public class Product extends AbstractStorelessUnivariateStatistic {
@ -74,7 +74,7 @@ public class Product extends AbstractStorelessUnivariateStatistic {
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (Double.isNaN(value)) { if (Double.isNaN(value)) {
value = d; value = d;
} else { } else {
@ -83,7 +83,7 @@ public class Product extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return value; return value;
@ -104,7 +104,10 @@ public class Product extends AbstractStorelessUnivariateStatistic {
* @return the product values or Double.NaN if the array is empty * @return the product values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double product = Double.NaN; double product = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
product = 1.0; product = 1.0;

View File

@ -56,7 +56,7 @@ package org.apache.commons.math.stat.univariate.summary;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.8 $ $Date: 2003/07/15 03:38:50 $ * @version $Revision: 1.9 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class Sum extends AbstractStorelessUnivariateStatistic { public class Sum extends AbstractStorelessUnivariateStatistic {
@ -66,10 +66,9 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
private double value = Double.NaN; private double value = Double.NaN;
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
* StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (Double.isNaN(value)) { if (Double.isNaN(value)) {
value = d; value = d;
} else { } else {
@ -78,16 +77,14 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
* StorelessUnivariateStatistic#getValue()
*/ */
public double getResult() { public double getResult() {
return value; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
* StorelessUnivariateStatistic#clear()
*/ */
public void clear() { public void clear() {
value = Double.NaN; value = Double.NaN;
@ -99,10 +96,12 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
* @param begin processing at this point in the array * @param begin processing at this point in the array
* @param length processing at this point in the array * @param length processing at this point in the array
* @return the sum of the values or Double.NaN if the array is empty * @return the sum of the values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate. * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
* UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double sum = Double.NaN; double sum = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
sum = 0.0; sum = 0.0;

View File

@ -62,7 +62,7 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:13 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class SumOfLogs extends AbstractStorelessUnivariateStatistic { public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
@ -71,11 +71,13 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
*/ */
private double value = Double.NaN; private double value = Double.NaN;
/** */
private boolean init = true; private boolean init = true;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (init) { if (init) {
value = Math.log(d); value = Math.log(d);
init = false; init = false;
@ -85,7 +87,7 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return value; return value;
@ -98,7 +100,7 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
value = Double.NaN; value = Double.NaN;
init = true; init = true;
} }
/** /**
* Returns the sum of the natural logs for this collection of values * Returns the sum of the natural logs for this collection of values
* @param values Is a double[] containing the values * @param values Is a double[] containing the values
@ -107,7 +109,10 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
* @return the sumLog value or Double.NaN if the array is empty * @return the sumLog value or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double sumLog = Double.NaN; double sumLog = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
sumLog = 0.0; sumLog = 0.0;

View File

@ -62,7 +62,7 @@ import org
.AbstractStorelessUnivariateStatistic; .AbstractStorelessUnivariateStatistic;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:13 $ * @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class SumOfSquares extends AbstractStorelessUnivariateStatistic { public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
@ -74,8 +74,8 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public void increment(double d) { public void increment(final double d) {
if (Double.isNaN(value )) { if (Double.isNaN(value)) {
value = d * d; value = d * d;
} else { } else {
value += d * d; value += d * d;
@ -83,7 +83,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/ */
public double getResult() { public double getResult() {
return value; return value;
@ -104,7 +104,10 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
* @return the sum of the squared values or Double.NaN if the array is empty * @return the sum of the squared values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
public double evaluate(double[] values, int begin, int length) { public double evaluate(
final double[] values,
final int begin,
final int length) {
double sumSq = Double.NaN; double sumSq = Double.NaN;
if (test(values, begin, length)) { if (test(values, begin, length)) {
sumSq = 0.0; sumSq = 0.0;
@ -114,6 +117,5 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
} }
return sumSq; return sumSq;
} }
} }

View File

@ -58,7 +58,7 @@ import org.apache.commons.beanutils.PropertyUtils;
/** /**
* Uses PropertyUtils to map a Bean getter to a double value. * Uses PropertyUtils to map a Bean getter to a double value.
* @version $Revision: 1.3 $ $Date: 2003/07/09 20:04:12 $ * @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class BeanTransformer implements NumberTransformer { public class BeanTransformer implements NumberTransformer {
@ -68,7 +68,7 @@ public class BeanTransformer implements NumberTransformer {
private String propertyName; private String propertyName;
/** /**
* Create a BeanTransformer * Create a BeanTransformer
*/ */
public BeanTransformer() { public BeanTransformer() {
super(); super();
@ -76,16 +76,16 @@ public class BeanTransformer implements NumberTransformer {
/** /**
* Create a BeanTransformer with a specific PropertyName. * Create a BeanTransformer with a specific PropertyName.
* @param propertyName The property. * @param property The property.
*/ */
public BeanTransformer(String propertyName) { public BeanTransformer(final String property) {
this.propertyName = propertyName; this.propertyName = property;
} }
/** /**
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object) * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
*/ */
public double transform(Object o) { public double transform(final Object o) {
double d = Double.NaN; double d = Double.NaN;
try { try {
d = d =
@ -113,7 +113,7 @@ public class BeanTransformer implements NumberTransformer {
* Set the propertyString * Set the propertyString
* @param string The string to set the property to. * @param string The string to set the property to.
*/ */
public void setPropertyName(String string) { public void setPropertyName(final String string) {
propertyName = string; propertyName = string;
} }

View File

@ -57,55 +57,55 @@ package org.apache.commons.math.util;
/** /**
* Some useful additions to the built-in functions in lang.Math<p> * Some useful additions to the built-in functions in lang.Math<p>
* *
* @version $Revision: 1.2 $ $Date: 2003/07/07 23:19:22 $ * @version $Revision: 1.3 $ $Date: 2003/08/09 04:03:41 $
*/ */
public class MathUtils { public final class MathUtils {
/**
* Private Constructor
*/
private MathUtils() {
}
/** /**
* For a double precision value x, this method returns +1.0 if x >= 0 * For a double precision value x, this method returns +1.0 if x >= 0
* and -1.0 if x < 0. * and -1.0 if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a double * @param x the value, a double
* @return +1.0 or -1.0, depending on the the sign of x * @return +1.0 or -1.0, depending on the the sign of x
*/ */
public static double sign( double x ) { public static double sign(final double x) {
if ( x >= 0.0 ) { if (x >= 0.0) {
return 1.0 ; return 1.0;
} else { } else {
return -1.0 ; return -1.0;
} }
} }
/** /**
* For a float value x, this method returns +1.0F if x >= 0 * For a float value x, this method returns +1.0F if x >= 0
* and -1.0F if x < 0. * and -1.0F if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a float * @param x the value, a float
* @return +1.0F or -1.0F, depending on the the sign of x * @return +1.0F or -1.0F, depending on the the sign of x
*/ */
public static float sign( float x ) { public static float sign(final float x) {
if ( x >= 0.0F ) { if (x >= 0.0F) {
return 1.0F ; return 1.0F;
} else { } else {
return -1.0F ; return -1.0F;
} }
} }
/** /**
* For a byte value x, this method returns (byte)(+1) if x >= 0 * For a byte value x, this method returns (byte)(+1) if x >= 0
* and (byte)(-1) if x < 0. * and (byte)(-1) if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a byte * @param x the value, a byte
* @return (byte)(+1) or (byte)(-1), depending on the the sign of x * @return (byte)(+1) or (byte)(-1), depending on the the sign of x
*/ */
public static byte sign( byte x ) { public static byte sign(final byte x) {
if ( x >= (byte)0 ) { if (x >= (byte) 0) {
return (byte)1 ; return (byte) 1;
} else { } else {
return (byte)(-1) ; return (byte) (-1);
} }
} }
@ -113,15 +113,14 @@ public class MathUtils {
* For a short value x, this method returns (short)(+1) if x >= 0 * For a short value x, this method returns (short)(+1) if x >= 0
* and (short)(-1) if x < 0. * and (short)(-1) if x < 0.
* *
* @author Albert Davidson Chou
* @param x the value, a short * @param x the value, a short
* @return (short)(+1) or (short)(-1), depending on the the sign of x * @return (short)(+1) or (short)(-1), depending on the the sign of x
*/ */
public static short sign( short x ) { public static short sign(final short x) {
if ( x >= (short)0 ) { if (x >= (short) 0) {
return (short)1 ; return (short) 1;
} else { } else {
return (short)(-1) ; return (short) (-1);
} }
} }
@ -129,67 +128,65 @@ public class MathUtils {
* For an int value x, this method returns +1 if x >= 0 * For an int value x, this method returns +1 if x >= 0
* and -1 if x < 0. * and -1 if x < 0.
* *
* @author Albert Davidson Chou
* @param x the value, an int * @param x the value, an int
* @return +1 or -1, depending on the the sign of x * @return +1 or -1, depending on the the sign of x
*/ */
public static int sign( int x ) { public static int sign(final int x) {
if ( x >= 0 ) { if (x >= 0) {
return 1 ; return 1;
} else { } else {
return -1 ; return -1;
} }
} }
/** /**
* For a long value x, this method returns +1L if x >= 0 * For a long value x, this method returns +1L if x >= 0
* and -1L if x < 0. * and -1L if x < 0.
* *
* @author Albert Davidson Chou
* @param x the value, a long * @param x the value, a long
* @return +1L or -1L, depending on the the sign of x * @return +1L or -1L, depending on the the sign of x
*/ */
public static long sign( long x ) { public static long sign(final long x) {
if ( x >= 0L ) { if (x >= 0L) {
return 1L ; return 1L;
} else { } else {
return -1L ; return -1L;
} }
} }
/** /**
* Returns an exact representation of the * Returns an exact representation of the
* <a href="http://mathworld.wolfram.com/BinomialCoefficient.html"> * <a href="http://mathworld.wolfram.com/BinomialCoefficient.html">
* Binomial Coefficient</a>, "<code>n choose k</code>", * Binomial Coefficient</a>, "<code>n choose k</code>",
* the number of <code>k</code>-element subsets that can be selected from * the number of <code>k</code>-element subsets that can be selected from
* an <code>n</code>-element set. * an <code>n</code>-element set.
* <p> * <p>
* <Strong>Preconditions</strong>:<ul> * <Strong>Preconditions</strong>:<ul>
* <li> <code>0 < k <= n </code> (otherwise * <li> <code>0 < k <= n </code> (otherwise
* <li> <code>0 < k <= n </code> (otherwise * <li> <code>0 < k <= n </code> (otherwise
* <code>IllegalArgumentException</code> is thrown)</li> * <code>IllegalArgumentException</code> is thrown)</li>
* <li> The result is small enough to fit into a <code>long</code>. The * <li> The result is small enough to fit into a <code>long</code>. The
* largest value of <code>n</code> for which all coefficients are * largest value of <code>n</code> for which all coefficients are
* <code> < Long.MAX_VALUE</code> is 66. If the computed value * <code> < Long.MAX_VALUE</code> is 66. If the computed value
* <li> The result is small enough to fit into a <code>long</code>. The * <li> The result is small enough to fit into a <code>long</code>. The
* largest value of <code>n</code> for which all coefficients are * largest value of <code>n</code> for which all coefficients are
* <code> < Long.MAX_VALUE</code> is 66. If the computed value * <code> < Long.MAX_VALUE</code> is 66. If the computed value
* exceeds <code>Long.MAX_VALUE</code> an <code>ArithMeticException * exceeds <code>Long.MAX_VALUE</code> an <code>ArithMeticException
* </code> is thrown.</li> * </code> is thrown.</li>
* </ul> * </ul>
* *
* *
* @param n the size of the set * @param n the size of the set
* @param k the size of the subsets to be counted * @param k the size of the subsets to be counted
* @return <code>n choose k</code> * @return <code>n choose k</code>
*/ */
public static long binomialCoefficient(int n, int k) { public static long binomialCoefficient(final int n, final int k) {
if (n < k) { if (n < k) {
throw new IllegalArgumentException throw new IllegalArgumentException(
("must have n >= k for binomial coefficient (n,k)"); "must have n >= k for binomial coefficient (n,k)");
} }
if (n <= 0) { if (n <= 0) {
throw new IllegalArgumentException throw new IllegalArgumentException(
("must have n > 0 for binomial coefficient (n,k)"); "must have n > 0 for binomial coefficient (n,k)");
} }
if ((n == k) || (k == 0)) { if ((n == k) || (k == 0)) {
return 1; return 1;
@ -200,8 +197,8 @@ public class MathUtils {
long result = Math.round(binomialCoefficientDouble(n, k)); long result = Math.round(binomialCoefficientDouble(n, k));
if (result == Long.MAX_VALUE) { if (result == Long.MAX_VALUE) {
throw new ArithmeticException throw new ArithmeticException(
("result too large to represent in a long integer"); "result too large to represent in a long integer");
} }
return result; return result;
} }
@ -226,8 +223,8 @@ public class MathUtils {
* @param k the size of the subsets to be counted * @param k the size of the subsets to be counted
* @return <code>n choose k</code> * @return <code>n choose k</code>
*/ */
public static double binomialCoefficientDouble(int n, int k) { public static double binomialCoefficientDouble(final int n, final int k) {
return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5); return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + 0.5);
} }
/** /**
@ -246,14 +243,14 @@ public class MathUtils {
* @param k the size of the subsets to be counted * @param k the size of the subsets to be counted
* @return <code>n choose k</code> * @return <code>n choose k</code>
*/ */
public static double binomialCoefficientLog(int n, int k) { public static double binomialCoefficientLog(final int n, final int k) {
if (n < k) { if (n < k) {
throw new IllegalArgumentException throw new IllegalArgumentException(
("must have n >= k for binomial coefficient (n,k)"); "must have n >= k for binomial coefficient (n,k)");
} }
if (n <= 0) { if (n <= 0) {
throw new IllegalArgumentException throw new IllegalArgumentException(
("must have n > 0 for binomial coefficient (n,k)"); "must have n > 0 for binomial coefficient (n,k)");
} }
if ((n == k) || (k == 0)) { if ((n == k) || (k == 0)) {
return 0; return 0;
@ -295,11 +292,11 @@ public class MathUtils {
* @param n argument * @param n argument
* @return <code>n!</code> * @return <code>n!</code>
*/ */
public static long factorial(int n) { public static long factorial(final int n) {
long result = Math.round(factorialDouble(n)); long result = Math.round(factorialDouble(n));
if (result == Long.MAX_VALUE) { if (result == Long.MAX_VALUE) {
throw new ArithmeticException throw new ArithmeticException(
("result too large to represent in a long integer"); "result too large to represent in a long integer");
} }
return result; return result;
} }
@ -323,33 +320,31 @@ public class MathUtils {
* @param n argument * @param n argument
* @return <code>n!</code> * @return <code>n!</code>
*/ */
public static double factorialDouble(int n) { public static double factorialDouble(final int n) {
if (n <= 0) { if (n <= 0) {
throw new IllegalArgumentException throw new IllegalArgumentException("must have n > 0 for n!");
("must have n > 0 for n!");
} }
return Math.floor(Math.exp(factorialLog(n)) + 0.5); return Math.floor(Math.exp(factorialLog(n)) + 0.5);
} }
/** /**
* Returns the natural <code>log</code> of <code>n</code> * Returns the natural <code>log</code> of <code>n</code>
* <a href="http://mathworld.wolfram.com/Factorial.html"> * <a href="http://mathworld.wolfram.com/Factorial.html">
* Factorial</a>, or <code>n!</code>, * Factorial</a>, or <code>n!</code>,
* the product of the numbers <code>1,...,n</code>, as as * the product of the numbers <code>1,...,n</code>, as as
* <code>double</code>. * <code>double</code>.
* <p> * <p>
* <Strong>Preconditions</strong>:<ul> * <Strong>Preconditions</strong>:<ul>
* <li> <code>n > 0</code> (otherwise * <li> <code>n > 0</code> (otherwise
* <code>IllegalArgumentException</code> is thrown)</li> * <code>IllegalArgumentException</code> is thrown)</li>
* </ul> * </ul>
* *
* @param n argument * @param n argument
* @return <code>n!</code> * @return <code>n!</code>
*/ */
public static double factorialLog(int n) { public static double factorialLog(final int n) {
if (n <= 0) { if (n <= 0) {
throw new IllegalArgumentException throw new IllegalArgumentException("must have n > 0 for n!");
("must have n > 0 for n!");
} }
double logSum = 0; double logSum = 0;
for (int i = 2; i <= n; i++) { for (int i = 2; i <= n; i++) {