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:
parent
88b4e3dbc9
commit
b3b89139bb
|
@ -55,8 +55,7 @@ package org.apache.commons.math;
|
|||
|
||||
/**
|
||||
* Signals a configuration problem with any of the factory methods.
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/07/30 21:58:10 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
|
||||
*/
|
||||
public class MathConfigurationException extends MathException {
|
||||
|
||||
|
@ -71,7 +70,7 @@ public class MathConfigurationException extends MathException {
|
|||
* Construct an exception with the given message.
|
||||
* @param message message describing the problem
|
||||
*/
|
||||
public MathConfigurationException(String message) {
|
||||
public MathConfigurationException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
@ -80,7 +79,9 @@ public class MathConfigurationException extends MathException {
|
|||
* @param message message describing the 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);
|
||||
}
|
||||
|
||||
|
@ -88,7 +89,7 @@ public class MathConfigurationException extends MathException {
|
|||
* Construct an exception with the given root cause.
|
||||
* @param throwable caught exception causing this problem
|
||||
*/
|
||||
public MathConfigurationException(Throwable throwable) {
|
||||
public MathConfigurationException(final Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,13 +55,12 @@ package org.apache.commons.math;
|
|||
|
||||
/**
|
||||
* A generic exception indicating problems in the math package.
|
||||
*
|
||||
* @version $Revision: 1.4 $ $Date: 2003/07/09 20:02:44 $
|
||||
* @version $Revision: 1.5 $ $Date: 2003/08/09 04:03:41 $
|
||||
*/
|
||||
public class MathException extends Exception {
|
||||
|
||||
/**
|
||||
*
|
||||
* Constructs a MathException
|
||||
*/
|
||||
public MathException() {
|
||||
super();
|
||||
|
@ -70,7 +69,7 @@ public class MathException extends Exception {
|
|||
/**
|
||||
* @param message message describing the problem
|
||||
*/
|
||||
public MathException(String message) {
|
||||
public MathException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
@ -78,14 +77,14 @@ public class MathException extends Exception {
|
|||
* @param message message describing the 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param throwable caught exception causing this problem
|
||||
*/
|
||||
public MathException(Throwable throwable) {
|
||||
public MathException(final Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
|
||||
|
|
|
@ -59,8 +59,7 @@ import org.apache.commons.math.MathException;
|
|||
* Provide the bisection algorithm for solving for zeros of real univariate
|
||||
* functions. It will only search for one zero in the given interval. The
|
||||
* function is supposed to be continuous but not necessarily smooth.
|
||||
*
|
||||
* @version $Revision: 1.2 $ $Date: 2003/07/09 20:02:43 $
|
||||
* @version $Revision: 1.3 $ $Date: 2003/08/09 04:03:41 $
|
||||
*/
|
||||
public class BisectionSolver extends UnivariateRealSolverImpl {
|
||||
/**
|
||||
|
|
|
@ -62,7 +62,7 @@ import org.apache.commons.math.util.BeanTransformer;
|
|||
* univariate statistics for a List of Java Beans by property. This
|
||||
* implementation uses beanutils' PropertyUtils to get a simple, nested,
|
||||
* 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 {
|
||||
|
||||
|
@ -108,9 +108,9 @@ public class BeanListUnivariateImpl extends ListUnivariateImpl {
|
|||
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) {
|
||||
String msg =
|
||||
"The BeanListUnivariateImpl does not accept values "
|
||||
|
|
|
@ -57,16 +57,22 @@ package org.apache.commons.math.stat;
|
|||
* StatUtils provides easy static implementations of common double[] based
|
||||
* statistical methods. These return a single result value or in some cases, as
|
||||
* 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.
|
||||
* @param values Is a double[] containing the values
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -77,7 +83,10 @@ public class StatUtils {
|
|||
* @param length processing at this point in the array
|
||||
* @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);
|
||||
double accum = 0.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -91,7 +100,7 @@ public class StatUtils {
|
|||
* @param values Is a double[] containing the values
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -102,7 +111,10 @@ public class StatUtils {
|
|||
* @param length processing at this point in the array
|
||||
* @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);
|
||||
double accum = 0.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -116,7 +128,7 @@ public class StatUtils {
|
|||
* @param values Is a double[] containing the values
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -127,7 +139,10 @@ public class StatUtils {
|
|||
* @param length processing at this point in the array
|
||||
* @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);
|
||||
double product = 1.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -141,7 +156,7 @@ public class StatUtils {
|
|||
* @param values Is a double[] containing the values
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -152,7 +167,10 @@ public class StatUtils {
|
|||
* @param length processing at this point in the array
|
||||
* @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);
|
||||
double sumLog = 0.0;
|
||||
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>
|
||||
* arithmetic mean </a> of the available values
|
||||
* arithmetic mean </a> of the available values
|
||||
* @param values Is a double[] containing the values
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 begin 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
|
||||
*/
|
||||
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);
|
||||
return sum(values, begin, length) / ((double) length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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">
|
||||
* corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
|
||||
* "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.
|
||||
*
|
||||
*
|
||||
* @param values Is a double[] containing the values
|
||||
* @return the result, Double.NaN if no values for an empty array
|
||||
* or 0.0 for a single value set.
|
||||
* @return the result, Double.NaN if no values for an empty array
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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">
|
||||
* corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
|
||||
* "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.
|
||||
*
|
||||
*
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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
|
||||
* or 0.0 for a single value set.
|
||||
* @return the result, Double.NaN if no values for an empty array
|
||||
* 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);
|
||||
|
||||
double variance = Double.NaN;
|
||||
|
@ -231,7 +255,7 @@ public class StatUtils {
|
|||
accum2 += (values[i] - mean);
|
||||
}
|
||||
variance =
|
||||
(accum - (Math.pow(accum2, 2) / ((double)length)))
|
||||
(accum - (Math.pow(accum2, 2) / ((double) length)))
|
||||
/ (double) (length - 1);
|
||||
}
|
||||
return variance;
|
||||
|
@ -242,7 +266,7 @@ public class StatUtils {
|
|||
* @param values Is a double[] containing the values
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -253,14 +277,19 @@ public class StatUtils {
|
|||
* @param length processing at this point in the array
|
||||
* @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);
|
||||
double max = Double.NaN;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
if (i == 0) {
|
||||
max = values[i];
|
||||
} else {
|
||||
max = (max > values[i]) ? max : values[i];
|
||||
if (max < values[i]) {
|
||||
max = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return max;
|
||||
|
@ -271,7 +300,7 @@ public class StatUtils {
|
|||
* @param values Is a double[] containing the values
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -282,7 +311,11 @@ public class StatUtils {
|
|||
* @param length processing at this point in the array
|
||||
* @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);
|
||||
|
||||
double min = Double.NaN;
|
||||
|
@ -290,29 +323,37 @@ public class StatUtils {
|
|||
if (i == 0) {
|
||||
min = values[i];
|
||||
} else {
|
||||
min = (min < values[i]) ? min : values[i];
|
||||
if (min > values[i]) {
|
||||
min = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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");
|
||||
}
|
||||
|
||||
if (begin + length > values.length)
|
||||
throw new IllegalArgumentException("begin + length > values.length");
|
||||
if (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");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,21 +59,23 @@ package org.apache.commons.math.stat.univariate;
|
|||
* Provides the ability to extend polymophically so that
|
||||
* indiviual statistics do not need to implement these methods unless
|
||||
* 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
|
||||
extends AbstractUnivariateStatistic
|
||||
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
|
||||
* StorelessUnivariateStatistic this is never really used because more
|
||||
* StorelessUnivariateStatistic this is never really used because more
|
||||
* efficient algorithms are available for that statistic.
|
||||
* @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 (this.test(values, begin, length)) {
|
||||
this.clear();
|
||||
int l = begin + length;
|
||||
|
@ -84,4 +86,19 @@ public abstract class AbstractStorelessUnivariateStatistic
|
|||
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);
|
||||
|
||||
}
|
|
@ -56,8 +56,8 @@ package org.apache.commons.math.stat.univariate;
|
|||
/**
|
||||
* Abstract Implementation for UnivariateStatistics.
|
||||
* Provides the ability to extend polymophically so that
|
||||
* indiviual statistics do not need to implement these methods.
|
||||
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:37:10 $
|
||||
* indiviual statistics do not need to implement these methods.
|
||||
* @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
|
||||
*/
|
||||
public abstract class AbstractUnivariateStatistic
|
||||
implements UnivariateStatistic {
|
||||
|
@ -65,36 +65,43 @@ public abstract class AbstractUnivariateStatistic
|
|||
/**
|
||||
* This implementation provides a simple wrapper around the double[]
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses of AbstractUnivariateStatistc need to implement this method.
|
||||
* @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 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.
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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) {
|
||||
throw new IllegalArgumentException("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) {
|
||||
|
|
|
@ -56,13 +56,13 @@ package org.apache.commons.math.stat.univariate;
|
|||
/**
|
||||
* Extends the capabilities of UnivariateStatistic with a statefull incremental
|
||||
* strategy through three methods for calculating a statistic without having to
|
||||
* maintain a double[] of the values. Because a StorelessUnivariateStatistic
|
||||
* 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
|
||||
* implemented using it. If a Statistic cannot be implemented in a Storeless
|
||||
* approach it should implement the UnivariateStatistic interface directly
|
||||
* maintain a double[] of the values. Because a StorelessUnivariateStatistic
|
||||
* 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
|
||||
* implemented using it. If a Statistic cannot be implemented in a Storeless
|
||||
* approach it should implement the UnivariateStatistic interface directly
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -54,31 +54,31 @@
|
|||
package org.apache.commons.math.stat.univariate;
|
||||
|
||||
/**
|
||||
* UnivariateStatistic interface provides methods to evaluate
|
||||
* double[] based content using an implemented statistical approach.
|
||||
* The interface provides two "stateless" simple methods to calculate
|
||||
* UnivariateStatistic interface provides methods to evaluate
|
||||
* double[] based content using an implemented statistical approach.
|
||||
* The interface provides two "stateless" simple methods to calculate
|
||||
* 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 {
|
||||
|
||||
|
||||
/**
|
||||
* Evaluates the double[] returning the result of the evaluation.
|
||||
* @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
|
||||
*/
|
||||
double evaluate(double[] values);
|
||||
|
||||
/**
|
||||
* Evaluates part of a double[] returning the result
|
||||
* Evaluates part of a double[] returning the result
|
||||
* of the evaluation.
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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
|
||||
*/
|
||||
double evaluate(double[] values, int begin, int length);
|
||||
double evaluate(double[] values, int begin, int length);
|
||||
|
||||
}
|
|
@ -57,12 +57,12 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
|
|||
|
||||
/**
|
||||
* 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">
|
||||
* recursive strategy
|
||||
* </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 {
|
||||
|
||||
|
@ -72,29 +72,28 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
|
|||
/** first moment of values that have been added */
|
||||
protected double m1 = Double.NaN;
|
||||
|
||||
/**
|
||||
/**
|
||||
* temporary internal state made available for
|
||||
* higher order moments
|
||||
* higher order moments
|
||||
*/
|
||||
protected double dev = 0.0;
|
||||
|
||||
/**
|
||||
/**
|
||||
* temporary internal state made available for
|
||||
* higher order moments
|
||||
* higher order moments
|
||||
*/
|
||||
protected double v = 0.0;
|
||||
|
||||
/**
|
||||
/**
|
||||
* temporary internal state made available for
|
||||
* higher order moments
|
||||
* higher order moments
|
||||
*/
|
||||
protected double n0 = 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) {
|
||||
m1 = 0.0;
|
||||
}
|
||||
|
@ -108,8 +107,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* StorelessUnivariateStatistic#clear()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
m1 = Double.NaN;
|
||||
|
@ -120,8 +118,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
return m1;
|
||||
|
|
|
@ -54,49 +54,52 @@
|
|||
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">
|
||||
* recursive strategy
|
||||
* </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 {
|
||||
|
||||
/** fourth moment of values that have been added */
|
||||
protected double m4 = Double.NaN;
|
||||
|
||||
|
||||
/** temporary internal state made available for higher order moments */
|
||||
protected double prevM3 = 0.0;
|
||||
|
||||
/** temporary internal state made available for higher order moments */
|
||||
protected double n3 = 0.0;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (n < 1) {
|
||||
m4 = m3 = m2 = m1 = 0.0;
|
||||
m4 = 0.0;
|
||||
m3 = 0.0;
|
||||
m2 = 0.0;
|
||||
m1 = 0.0;
|
||||
}
|
||||
|
||||
/* retain previous m3 */
|
||||
prevM3 = m3;
|
||||
|
||||
|
||||
/* increment m1, m2 and m3 (and prevM2, _n0, _n1, _n2, _v, _v2) */
|
||||
super.increment(d);
|
||||
|
||||
n3 = (double) (n - 3);
|
||||
|
||||
|
||||
m4 =
|
||||
m4
|
||||
- (4.0 * v * prevM3)
|
||||
+ (6.0 * v2 * prevM2)
|
||||
+ ((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() {
|
||||
return m4;
|
||||
|
|
|
@ -55,28 +55,32 @@ package org.apache.commons.math.stat.univariate.moment;
|
|||
|
||||
import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
|
||||
|
||||
/**
|
||||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
|
||||
* 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 {
|
||||
|
||||
/** */
|
||||
protected int n = 0;
|
||||
|
||||
/** */
|
||||
private double geoMean = Double.NaN;
|
||||
|
||||
|
||||
/** */
|
||||
private double lastSum = 0.0;
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
n++;
|
||||
super.increment(d);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
if (lastSum != super.getResult() || n == 1) {
|
||||
|
@ -105,7 +109,10 @@ public class GeometricMean extends SumOfLogs {
|
|||
* any of the values are <= 0.
|
||||
* @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(
|
||||
super.evaluate(values, begin, length) / (double) length);
|
||||
}
|
||||
|
|
|
@ -62,23 +62,34 @@ import org
|
|||
.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 {
|
||||
|
||||
/** */
|
||||
protected FourthMoment moment = null;
|
||||
|
||||
/** */
|
||||
protected boolean incMoment = true;
|
||||
|
||||
/** */
|
||||
private double kurtosis = Double.NaN;
|
||||
|
||||
/** */
|
||||
private int n = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a Kurtosis
|
||||
*/
|
||||
public Kurtosis() {
|
||||
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;
|
||||
this.moment = m4;
|
||||
}
|
||||
|
@ -86,14 +97,14 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (incMoment) {
|
||||
moment.increment(d);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
if (n < moment.n) {
|
||||
|
@ -118,7 +129,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
n = moment.n;
|
||||
}
|
||||
|
||||
|
||||
return kurtosis;
|
||||
}
|
||||
|
||||
|
@ -135,26 +146,29 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
|
||||
/*UnvariateStatistic Approach */
|
||||
|
||||
/** */
|
||||
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">
|
||||
* corrected two pass formula (14.1.8)</a>, and also referenced in:
|
||||
* <p>
|
||||
* "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.
|
||||
* </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.
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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
|
||||
*/
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
;
|
||||
public double evaluate(
|
||||
final double[] values,
|
||||
final int begin,
|
||||
final int length) {
|
||||
|
||||
// Initialize the kurtosis
|
||||
double kurt = Double.NaN;
|
||||
|
@ -167,8 +181,9 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
// Get the mean and the standard deviation
|
||||
double m = mean.evaluate(values, begin, length);
|
||||
|
||||
// Calc the std, this is implemented here instead of using the
|
||||
// standardDeviation method eliminate a duplicate pass to get the mean
|
||||
// Calc the std, this is implemented here instead
|
||||
// of using the standardDeviation method eliminate
|
||||
// a duplicate pass to get the mean
|
||||
double accum = 0.0;
|
||||
double accum2 = 0.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -181,7 +196,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
(accum - (Math.pow(accum2, 2) / ((double) length)))
|
||||
/ (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
|
||||
double accum3 = 0.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -189,12 +204,12 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
// Get N
|
||||
double n = length;
|
||||
double n0 = length;
|
||||
|
||||
double coefficientOne =
|
||||
(n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
|
||||
(n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
|
||||
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
|
||||
kurt = (coefficientOne * accum3) - termTwo;
|
||||
|
|
|
@ -53,26 +53,38 @@
|
|||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
|
||||
* 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 {
|
||||
|
||||
/** first moment of values that have been added */
|
||||
protected FirstMoment moment = null;
|
||||
|
||||
/** */
|
||||
protected boolean incMoment = true;
|
||||
|
||||
/** */
|
||||
public Mean() {
|
||||
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;
|
||||
incMoment = false;
|
||||
}
|
||||
|
@ -80,7 +92,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (incMoment) {
|
||||
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() {
|
||||
return moment.m1;
|
||||
}
|
||||
|
||||
/*UnvariateStatistic Approach */
|
||||
Sum sum = new Sum();
|
||||
|
||||
|
||||
/** */
|
||||
protected Sum sum = new Sum();
|
||||
|
||||
/**
|
||||
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
|
||||
* 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
|
||||
* @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)) {
|
||||
return sum.evaluate(values) / ((double) length);
|
||||
}
|
||||
|
|
|
@ -54,11 +54,11 @@
|
|||
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">
|
||||
* recursive strategy
|
||||
* </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 {
|
||||
|
||||
|
@ -67,20 +67,20 @@ public class SecondMoment extends FirstMoment {
|
|||
|
||||
/** temporary internal state made availabel for higher order moments */
|
||||
protected double n1 = 0.0;
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (n < 1) {
|
||||
m1 = m2 = 0.0;
|
||||
}
|
||||
|
||||
|
||||
/* increment m1 and _n0, _dev, _v) */
|
||||
super.increment(d);
|
||||
|
||||
n1 = n0 - 1;
|
||||
|
||||
|
||||
/* increment and return m2 */
|
||||
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() {
|
||||
return m2;
|
||||
|
|
|
@ -62,24 +62,35 @@ import org
|
|||
.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 {
|
||||
|
||||
/** */
|
||||
protected ThirdMoment moment = null;
|
||||
|
||||
/** */
|
||||
protected boolean incMoment = true;
|
||||
|
||||
/** */
|
||||
protected double skewness = Double.NaN;
|
||||
|
||||
/** */
|
||||
private int n = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a Skewness
|
||||
*/
|
||||
public Skewness() {
|
||||
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;
|
||||
this.moment = m3;
|
||||
}
|
||||
|
@ -87,14 +98,14 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (incMoment) {
|
||||
moment.increment(d);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
if (n < moment.n) {
|
||||
|
@ -130,29 +141,33 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
skewness = Double.NaN;
|
||||
n = 0;
|
||||
}
|
||||
|
||||
|
||||
/*UnvariateStatistic Approach */
|
||||
|
||||
/** */
|
||||
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">
|
||||
* 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>
|
||||
* "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.
|
||||
* </p>
|
||||
* Returns the skewness of a collection of values. Skewness is a
|
||||
* measure of the assymetry of a given distribution.
|
||||
* Returns the skewness of a collection of values. Skewness is a
|
||||
* measure of the assymetry of a given distribution.
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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
|
||||
* @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
|
||||
double skew = Double.NaN;
|
||||
|
@ -165,8 +180,9 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
// Get the mean and the standard deviation
|
||||
double m = mean.evaluate(values, begin, length);
|
||||
|
||||
// Calc the std, this is implemented here instead of using the
|
||||
// standardDeviation method eliminate a duplicate pass to get the mean
|
||||
// Calc the std, this is implemented here instead
|
||||
// of using the standardDeviation method eliminate
|
||||
// a duplicate pass to get the mean
|
||||
double accum = 0.0;
|
||||
double accum2 = 0.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -178,7 +194,7 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
(accum - (Math.pow(accum2, 2) / ((double) length)))
|
||||
/ (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.
|
||||
double accum3 = 0.0;
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
|
@ -186,10 +202,10 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
// Get N
|
||||
double n = length;
|
||||
double n0 = length;
|
||||
|
||||
// Calculate skewness
|
||||
skew = (n / ((n - 1) * (n - 2))) * accum3;
|
||||
skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -54,32 +54,41 @@
|
|||
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 {
|
||||
|
||||
/** */
|
||||
protected double std = Double.NaN;
|
||||
|
||||
/** */
|
||||
private double lastVar = 0.0;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a StandardDeviation
|
||||
*/
|
||||
public StandardDeviation() {
|
||||
super();
|
||||
}
|
||||
|
||||
public StandardDeviation(SecondMoment m2) {
|
||||
/**
|
||||
* Constructs a StandardDeviation with an external moment
|
||||
* @param m2 the external moment
|
||||
*/
|
||||
public StandardDeviation(final SecondMoment m2) {
|
||||
super(m2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double 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() {
|
||||
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 begin 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
|
||||
* or 0.0 for a single value set.
|
||||
* @return the result, Double.NaN if no values for an empty array
|
||||
* or 0.0 for a single value set.
|
||||
* @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);
|
||||
|
||||
if (Double.isNaN(var)) {
|
||||
|
|
|
@ -54,40 +54,40 @@
|
|||
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">
|
||||
* recursive strategy
|
||||
* </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 */
|
||||
protected double m3 = Double.NaN;
|
||||
|
||||
/** temporary internal state made availabel for higher order moments */
|
||||
protected double v2 = 0.0;
|
||||
|
||||
|
||||
/** temporary internal state made availabel for higher order moments */
|
||||
protected double n2 = 0.0;
|
||||
|
||||
|
||||
/** temporary internal state made availabel for higher order moments */
|
||||
protected double prevM2 = 0.0;
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (n < 1) {
|
||||
m3 = m2 = m1 = 0.0;
|
||||
}
|
||||
|
||||
|
||||
/* retain a reference to the last m2*/
|
||||
prevM2 = m2;
|
||||
|
||||
|
||||
/* increment m1 and m2 (and _n0, _n1, _v) */
|
||||
super.increment(d);
|
||||
|
||||
|
||||
v2 = v * v;
|
||||
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() {
|
||||
return m3;
|
||||
|
|
|
@ -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 {
|
||||
|
||||
/** SecondMoment is used in incremental calculation of Variance*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* This property maintains the latest calculated
|
||||
* variance for efficiency when getResult() is called
|
||||
* many times between increments.
|
||||
*/
|
||||
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;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a Variance.
|
||||
*/
|
||||
public Variance() {
|
||||
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;
|
||||
this.moment = 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) {
|
||||
if (incMoment) {
|
||||
moment.increment(d);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
if (n < moment.n) {
|
||||
|
@ -98,7 +120,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
} else if (moment.n <= 1) {
|
||||
variance = 0.0;
|
||||
} else {
|
||||
variance = moment.m2 / (moment.n0 - 1);
|
||||
variance = moment.m2 / (moment.n0 - 1);
|
||||
}
|
||||
n = moment.n;
|
||||
}
|
||||
|
@ -107,8 +129,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* StorelessUnivariateStatistic#clear()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
if (incMoment) {
|
||||
|
@ -118,28 +139,30 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
n = 0;
|
||||
}
|
||||
|
||||
/*UnvariateStatistic Approach */
|
||||
Mean mean = new Mean();
|
||||
/** Mean to be used in UnvariateStatistic evaluation approach. */
|
||||
protected Mean mean = new Mean();
|
||||
|
||||
/**
|
||||
* 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">
|
||||
* corrected two pass formula (14.1.8)</a>, and also referenced in:
|
||||
* <p>
|
||||
* "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.
|
||||
* </p>
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin 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
|
||||
* or 0.0 for a single value set.
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* UnivariateStatistic#evaluate(double[], int, int)
|
||||
* @return the result, Double.NaN if no values for an empty array
|
||||
* or 0.0 for a single value set.
|
||||
* @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 = Double.NaN;
|
||||
|
||||
|
|
|
@ -62,16 +62,17 @@ import org
|
|||
.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 {
|
||||
|
||||
/** */
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/**
|
||||
* @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;
|
||||
if (test(values, begin, length)) {
|
||||
max = values[begin];
|
||||
|
|
|
@ -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 Median() {
|
||||
super(50.0);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -62,16 +62,17 @@ import org
|
|||
.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 {
|
||||
|
||||
/** */
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
@ -81,9 +82,9 @@ public class Min extends AbstractStorelessUnivariateStatistic {
|
|||
public void clear() {
|
||||
value = Double.NaN;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
return value;
|
||||
|
@ -92,7 +93,10 @@ public class Min extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @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;
|
||||
if (test(values, begin, length)) {
|
||||
min = values[begin];
|
||||
|
|
|
@ -57,10 +57,11 @@ import java.util.Arrays;
|
|||
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 {
|
||||
|
||||
/** */
|
||||
private double percentile = 0.0;
|
||||
|
||||
/**
|
||||
|
@ -71,50 +72,58 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
super();
|
||||
percentile = 50.0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a Percentile with the specific percentile value.
|
||||
* @param percentile
|
||||
* @param p the percentile
|
||||
*/
|
||||
public Percentile(double percentile) {
|
||||
this.percentile = percentile;
|
||||
public Percentile(final double p) {
|
||||
this.percentile = p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the double[] top the specified percentile.
|
||||
* This does not alter the interal percentile state of the
|
||||
* Evaluates the double[] top the specified percentile.
|
||||
* This does not alter the interal percentile state of the
|
||||
* statistic.
|
||||
* @param values Is a double[] containing the values
|
||||
* @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
|
||||
*/
|
||||
public double evaluate(double[] values, double p) {
|
||||
return evaluate(values, 0,values.length, p);
|
||||
public double evaluate(final double[] values, final double p) {
|
||||
return evaluate(values, 0, values.length, p);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the double[] top the specified percentile.
|
||||
* This does not alter the interal percentile state of the
|
||||
* Evaluates the double[] top the specified percentile.
|
||||
* This does not alter the interal percentile state of the
|
||||
* statistic.
|
||||
* @param values Is a double[] containing the values
|
||||
* @param begin processing at this point in the array
|
||||
* @param length processing at this point in the array
|
||||
* @param p Is the percentile to evaluate to.*
|
||||
* @return the result of the evaluation or Double.NaN
|
||||
* @param p Is the percentile to evaluate to.*
|
||||
* @return the result of the evaluation or Double.NaN
|
||||
* 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)) {
|
||||
throw new IllegalArgumentException("invalid percentile value");
|
||||
}
|
||||
|
@ -130,9 +139,9 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
int intPos = (int) fpos;
|
||||
double dif = pos - fpos;
|
||||
double[] sorted = new double[length];
|
||||
System.arraycopy(values, begin,sorted, 0, length);
|
||||
System.arraycopy(values, begin, sorted, 0, length);
|
||||
Arrays.sort(sorted);
|
||||
|
||||
|
||||
if (pos < 1) {
|
||||
return sorted[0];
|
||||
}
|
||||
|
@ -143,7 +152,7 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
double upper = sorted[intPos];
|
||||
return lower + dif * (upper - lower);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The default internal state of this percentile can be set.
|
||||
* This will return that value.
|
||||
|
@ -158,7 +167,7 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
* This will setthat value.
|
||||
* @param p a value between 0 <= p <= 100
|
||||
*/
|
||||
public void setPercentile(double p) {
|
||||
public void setPercentile(final double p) {
|
||||
percentile = p;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ import org
|
|||
.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 {
|
||||
|
||||
|
@ -74,7 +74,7 @@ public class Product extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @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)) {
|
||||
value = d;
|
||||
} 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() {
|
||||
return value;
|
||||
|
@ -104,7 +104,10 @@ public class Product extends AbstractStorelessUnivariateStatistic {
|
|||
* @return the product values or Double.NaN if the array is empty
|
||||
* @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;
|
||||
if (test(values, begin, length)) {
|
||||
product = 1.0;
|
||||
|
|
|
@ -56,7 +56,7 @@ package org.apache.commons.math.stat.univariate.summary;
|
|||
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 {
|
||||
|
||||
|
@ -66,10 +66,9 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
|
|||
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) {
|
||||
if (Double.isNaN(value)) {
|
||||
value = d;
|
||||
} else {
|
||||
|
@ -78,16 +77,14 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* StorelessUnivariateStatistic#getValue()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
|
||||
*/
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.
|
||||
* StorelessUnivariateStatistic#clear()
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
value = Double.NaN;
|
||||
|
@ -99,10 +96,12 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
|
|||
* @param begin 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
|
||||
* @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 sum = Double.NaN;
|
||||
if (test(values, begin, length)) {
|
||||
sum = 0.0;
|
||||
|
|
|
@ -62,7 +62,7 @@ import org
|
|||
.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 {
|
||||
|
||||
|
@ -71,11 +71,13 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
|
|||
*/
|
||||
private double value = Double.NaN;
|
||||
|
||||
/** */
|
||||
private boolean init = true;
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
public void increment(final double d) {
|
||||
if (init) {
|
||||
value = Math.log(d);
|
||||
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() {
|
||||
return value;
|
||||
|
@ -98,7 +100,7 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
|
|||
value = Double.NaN;
|
||||
init = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the sum of the natural logs for this collection of 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
|
||||
* @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;
|
||||
if (test(values, begin, length)) {
|
||||
sumLog = 0.0;
|
||||
|
|
|
@ -62,7 +62,7 @@ import org
|
|||
.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 {
|
||||
|
||||
|
@ -74,8 +74,8 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public void increment(double d) {
|
||||
if (Double.isNaN(value )) {
|
||||
public void increment(final double d) {
|
||||
if (Double.isNaN(value)) {
|
||||
value = d * d;
|
||||
} else {
|
||||
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() {
|
||||
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
|
||||
* @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;
|
||||
if (test(values, begin, length)) {
|
||||
sumSq = 0.0;
|
||||
|
@ -114,6 +117,5 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
return sumSq;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -58,7 +58,7 @@ import org.apache.commons.beanutils.PropertyUtils;
|
|||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
|
@ -68,7 +68,7 @@ public class BeanTransformer implements NumberTransformer {
|
|||
private String propertyName;
|
||||
|
||||
/**
|
||||
* Create a BeanTransformer
|
||||
* Create a BeanTransformer
|
||||
*/
|
||||
public BeanTransformer() {
|
||||
super();
|
||||
|
@ -76,16 +76,16 @@ public class BeanTransformer implements NumberTransformer {
|
|||
|
||||
/**
|
||||
* Create a BeanTransformer with a specific PropertyName.
|
||||
* @param propertyName The property.
|
||||
* @param property The property.
|
||||
*/
|
||||
public BeanTransformer(String propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
public BeanTransformer(final String property) {
|
||||
this.propertyName = property;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
try {
|
||||
d =
|
||||
|
@ -113,7 +113,7 @@ public class BeanTransformer implements NumberTransformer {
|
|||
* Set the propertyString
|
||||
* @param string The string to set the property to.
|
||||
*/
|
||||
public void setPropertyName(String string) {
|
||||
public void setPropertyName(final String string) {
|
||||
propertyName = string;
|
||||
}
|
||||
|
||||
|
|
|
@ -57,55 +57,55 @@ package org.apache.commons.math.util;
|
|||
/**
|
||||
* 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
|
||||
* and -1.0 if x < 0.
|
||||
*
|
||||
* @author Albert Davidson Chou
|
||||
* @param x the value, a double
|
||||
* @return +1.0 or -1.0, depending on the the sign of x
|
||||
*/
|
||||
public static double sign( double x ) {
|
||||
if ( x >= 0.0 ) {
|
||||
return 1.0 ;
|
||||
public static double sign(final double x) {
|
||||
if (x >= 0.0) {
|
||||
return 1.0;
|
||||
} else {
|
||||
return -1.0 ;
|
||||
return -1.0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For a float value x, this method returns +1.0F if x >= 0
|
||||
* and -1.0F if x < 0.
|
||||
*
|
||||
* @author Albert Davidson Chou
|
||||
* @param x the value, a float
|
||||
* @return +1.0F or -1.0F, depending on the the sign of x
|
||||
*/
|
||||
public static float sign( float x ) {
|
||||
if ( x >= 0.0F ) {
|
||||
return 1.0F ;
|
||||
public static float sign(final float x) {
|
||||
if (x >= 0.0F) {
|
||||
return 1.0F;
|
||||
} else {
|
||||
return -1.0F ;
|
||||
return -1.0F;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For a byte value x, this method returns (byte)(+1) if x >= 0
|
||||
* and (byte)(-1) if x < 0.
|
||||
*
|
||||
* @author Albert Davidson Chou
|
||||
* @param x the value, a byte
|
||||
* @return (byte)(+1) or (byte)(-1), depending on the the sign of x
|
||||
*/
|
||||
public static byte sign( byte x ) {
|
||||
if ( x >= (byte)0 ) {
|
||||
return (byte)1 ;
|
||||
public static byte sign(final byte x) {
|
||||
if (x >= (byte) 0) {
|
||||
return (byte) 1;
|
||||
} 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
|
||||
* and (short)(-1) if x < 0.
|
||||
*
|
||||
* @author Albert Davidson Chou
|
||||
* @param x the value, a short
|
||||
* @return (short)(+1) or (short)(-1), depending on the the sign of x
|
||||
*/
|
||||
public static short sign( short x ) {
|
||||
if ( x >= (short)0 ) {
|
||||
return (short)1 ;
|
||||
public static short sign(final short x) {
|
||||
if (x >= (short) 0) {
|
||||
return (short) 1;
|
||||
} 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
|
||||
* and -1 if x < 0.
|
||||
*
|
||||
* @author Albert Davidson Chou
|
||||
* @param x the value, an int
|
||||
* @return +1 or -1, depending on the the sign of x
|
||||
*/
|
||||
public static int sign( int x ) {
|
||||
if ( x >= 0 ) {
|
||||
return 1 ;
|
||||
public static int sign(final int x) {
|
||||
if (x >= 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1 ;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For a long value x, this method returns +1L if x >= 0
|
||||
* and -1L if x < 0.
|
||||
*
|
||||
* @author Albert Davidson Chou
|
||||
* @param x the value, a long
|
||||
* @return +1L or -1L, depending on the the sign of x
|
||||
*/
|
||||
public static long sign( long x ) {
|
||||
if ( x >= 0L ) {
|
||||
return 1L ;
|
||||
public static long sign(final long x) {
|
||||
if (x >= 0L) {
|
||||
return 1L;
|
||||
} else {
|
||||
return -1L ;
|
||||
return -1L;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns an exact representation of the
|
||||
* <a href="http://mathworld.wolfram.com/BinomialCoefficient.html">
|
||||
* Binomial Coefficient</a>, "<code>n choose k</code>",
|
||||
* the number of <code>k</code>-element subsets that can be selected from
|
||||
* an <code>n</code>-element set.
|
||||
* <p>
|
||||
* <Strong>Preconditions</strong>:<ul>
|
||||
* <li> <code>0 < k <= n </code> (otherwise
|
||||
* <li> <code>0 < k <= n </code> (otherwise
|
||||
* <code>IllegalArgumentException</code> is thrown)</li>
|
||||
* <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
|
||||
* <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
|
||||
* largest value of <code>n</code> for which all coefficients are
|
||||
* <code> < Long.MAX_VALUE</code> is 66. If the computed value
|
||||
* exceeds <code>Long.MAX_VALUE</code> an <code>ArithMeticException
|
||||
* </code> is thrown.</li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* @param n the size of the set
|
||||
* @param k the size of the subsets to be counted
|
||||
* @return <code>n choose k</code>
|
||||
*/
|
||||
public static long binomialCoefficient(int n, int k) {
|
||||
/**
|
||||
* Returns an exact representation of the
|
||||
* <a href="http://mathworld.wolfram.com/BinomialCoefficient.html">
|
||||
* Binomial Coefficient</a>, "<code>n choose k</code>",
|
||||
* the number of <code>k</code>-element subsets that can be selected from
|
||||
* an <code>n</code>-element set.
|
||||
* <p>
|
||||
* <Strong>Preconditions</strong>:<ul>
|
||||
* <li> <code>0 < k <= n </code> (otherwise
|
||||
* <li> <code>0 < k <= n </code> (otherwise
|
||||
* <code>IllegalArgumentException</code> is thrown)</li>
|
||||
* <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
|
||||
* <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
|
||||
* largest value of <code>n</code> for which all coefficients are
|
||||
* <code> < Long.MAX_VALUE</code> is 66. If the computed value
|
||||
* exceeds <code>Long.MAX_VALUE</code> an <code>ArithMeticException
|
||||
* </code> is thrown.</li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* @param n the size of the set
|
||||
* @param k the size of the subsets to be counted
|
||||
* @return <code>n choose k</code>
|
||||
*/
|
||||
public static long binomialCoefficient(final int n, final int k) {
|
||||
if (n < k) {
|
||||
throw new IllegalArgumentException
|
||||
("must have n >= k for binomial coefficient (n,k)");
|
||||
throw new IllegalArgumentException(
|
||||
"must have n >= k for binomial coefficient (n,k)");
|
||||
}
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException
|
||||
("must have n > 0 for binomial coefficient (n,k)");
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"must have n > 0 for binomial coefficient (n,k)");
|
||||
}
|
||||
if ((n == k) || (k == 0)) {
|
||||
return 1;
|
||||
|
@ -200,8 +197,8 @@ public class MathUtils {
|
|||
|
||||
long result = Math.round(binomialCoefficientDouble(n, k));
|
||||
if (result == Long.MAX_VALUE) {
|
||||
throw new ArithmeticException
|
||||
("result too large to represent in a long integer");
|
||||
throw new ArithmeticException(
|
||||
"result too large to represent in a long integer");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -226,8 +223,8 @@ public class MathUtils {
|
|||
* @param k the size of the subsets to be counted
|
||||
* @return <code>n choose k</code>
|
||||
*/
|
||||
public static double binomialCoefficientDouble(int n, int k) {
|
||||
return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5);
|
||||
public static double binomialCoefficientDouble(final int n, final int k) {
|
||||
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
|
||||
* @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) {
|
||||
throw new IllegalArgumentException
|
||||
("must have n >= k for binomial coefficient (n,k)");
|
||||
throw new IllegalArgumentException(
|
||||
"must have n >= k for binomial coefficient (n,k)");
|
||||
}
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException
|
||||
("must have n > 0 for binomial coefficient (n,k)");
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"must have n > 0 for binomial coefficient (n,k)");
|
||||
}
|
||||
if ((n == k) || (k == 0)) {
|
||||
return 0;
|
||||
|
@ -295,11 +292,11 @@ public class MathUtils {
|
|||
* @param n argument
|
||||
* @return <code>n!</code>
|
||||
*/
|
||||
public static long factorial(int n) {
|
||||
public static long factorial(final int n) {
|
||||
long result = Math.round(factorialDouble(n));
|
||||
if (result == Long.MAX_VALUE) {
|
||||
throw new ArithmeticException
|
||||
("result too large to represent in a long integer");
|
||||
throw new ArithmeticException(
|
||||
"result too large to represent in a long integer");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -323,33 +320,31 @@ public class MathUtils {
|
|||
* @param n argument
|
||||
* @return <code>n!</code>
|
||||
*/
|
||||
public static double factorialDouble(int n) {
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException
|
||||
("must have n > 0 for n!");
|
||||
public static double factorialDouble(final int n) {
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException("must have n > 0 for n!");
|
||||
}
|
||||
return Math.floor(Math.exp(factorialLog(n)) + 0.5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the natural <code>log</code> of <code>n</code>
|
||||
* <a href="http://mathworld.wolfram.com/Factorial.html">
|
||||
* Factorial</a>, or <code>n!</code>,
|
||||
* the product of the numbers <code>1,...,n</code>, as as
|
||||
* <code>double</code>.
|
||||
* <p>
|
||||
* <Strong>Preconditions</strong>:<ul>
|
||||
* <li> <code>n > 0</code> (otherwise
|
||||
* <code>IllegalArgumentException</code> is thrown)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param n argument
|
||||
* @return <code>n!</code>
|
||||
*/
|
||||
public static double factorialLog(int n) {
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException
|
||||
("must have n > 0 for n!");
|
||||
/**
|
||||
* Returns the natural <code>log</code> of <code>n</code>
|
||||
* <a href="http://mathworld.wolfram.com/Factorial.html">
|
||||
* Factorial</a>, or <code>n!</code>,
|
||||
* the product of the numbers <code>1,...,n</code>, as as
|
||||
* <code>double</code>.
|
||||
* <p>
|
||||
* <Strong>Preconditions</strong>:<ul>
|
||||
* <li> <code>n > 0</code> (otherwise
|
||||
* <code>IllegalArgumentException</code> is thrown)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param n argument
|
||||
* @return <code>n!</code>
|
||||
*/
|
||||
public static double factorialLog(final int n) {
|
||||
if (n <= 0) {
|
||||
throw new IllegalArgumentException("must have n > 0 for n!");
|
||||
}
|
||||
double logSum = 0;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
|
|
Loading…
Reference in New Issue