Addition of optimal array based evaluations available in StatUtils. These are not delegates to StatUtils, they are implementations.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140969 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-07-07 02:15:19 +00:00
parent b6871d607e
commit faa703e40e
20 changed files with 439 additions and 231 deletions

View File

@ -65,57 +65,18 @@ public abstract class AbstractStorelessUnivariateStatistic
extends AbstractUnivariateStatistic extends AbstractUnivariateStatistic
implements StorelessUnivariateStatistic { implements StorelessUnivariateStatistic {
protected boolean clearOnEval = true;
protected boolean init = true;
/** /**
* This implements the AbstractUnivariateStatistic impl to funnel * This implements the AbstractUnivariateStatistic impl to funnel
* calculation off to the instantanious increment method. * calculation off to the instantanious increment 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 double evaluate(double[] d, int start, int length) { public double evaluate(double[] values, int begin, int length) {
if (this.test(values, begin, length)) {
if (clearOnEval)
this.clear(); this.clear();
for (int i = begin; i < begin + length; i++) {
for (int i = start; i < start + length; i++) { increment(values[i]);
increment(d[i]); }
} }
return getValue(); return getValue();
} }
/**
* Implement this delegated internalClear()
* to cleanup the state of your implementation on clear().
*/
protected abstract void internalClear();
/**
* This implementation is finalized so the implementor does not have to manage
* clearing its state. They just need to implement their delegated internalClear()
* to cleanup the state of their implementation on clear().
*
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public final void clear(){
init = true;
internalClear();
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#isClearOnEval()
*/
public boolean isClearOnEval() {
return clearOnEval;
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#setClearOnEval(boolean)
*/
public void setClearOnEval(boolean b) {
clearOnEval = b;
}
} }

View File

@ -69,15 +69,15 @@ public abstract class AbstractUnivariateStatistic
* *
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[]) * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[])
*/ */
public double evaluate(double[] d) { public double evaluate(double[] values) {
return evaluate(d, 0, d.length); return evaluate(values, 0, values.length);
} }
/** /**
* Subclasses of AbstractUnivariateStatistc need to implementation this method. * 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[] d, int start, int length); public abstract double evaluate(double[] values, int begin, 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
@ -86,7 +86,7 @@ public abstract class AbstractUnivariateStatistic
* @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
*/ */
protected void test(double[] values, int begin, int length) { protected boolean test(double[] values, int begin, int length) {
if (length > values.length) if (length > values.length)
throw new IllegalArgumentException("length > values.length"); throw new IllegalArgumentException("length > values.length");
@ -97,5 +97,10 @@ public abstract class AbstractUnivariateStatistic
if (values == null) if (values == null)
throw new IllegalArgumentException("input value array is null"); throw new IllegalArgumentException("input value array is null");
if (values.length == 0 || length == 0)
return false;
return true;
} }
} }

View File

@ -79,6 +79,7 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
*/ */
public double getValue(); public double getValue();
/** /**
* Clears all the internal state of the Statistic * Clears all the internal state of the Statistic
*/ */
@ -90,7 +91,7 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
* <code>evaluate(double[])</code> methods. * <code>evaluate(double[])</code> methods.
* @return the state * @return the state
*/ */
public boolean isClearOnEval(); //public boolean isClearOnEval();
/** /**
* Sets the behavior of this statistic when evaluating * Sets the behavior of this statistic when evaluating
@ -100,6 +101,6 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
* incrimented. * incrimented.
* @param b true | false * @param b true | false
*/ */
public void setClearOnEval(boolean b); //public void setClearOnEval(boolean b);
} }

View File

@ -78,6 +78,6 @@ public interface UnivariateStatistic {
* @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[] d, int begin, int length); public double evaluate(double[] values, int begin, int length);
} }

View File

@ -53,31 +53,11 @@
*/ */
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
/** /**
* @author Mark Diggory * @author Mark Diggory
* *
*/ */
public class FourthMoment extends AbstractStorelessUnivariateStatistic { public class FourthMoment extends ThirdMoment {
/** count of values that have been added */
protected int n = 0;
/** first moment of values that have been added */
protected double m1 = Double.NaN;
/** second moment of values that have been added */
protected double m2 = Double.NaN;
/** third moment of values that have been added */
protected double m3 = Double.NaN;
/** 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;
@ -122,11 +102,11 @@ public class FourthMoment extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
n = 0; super.clear();
m1 = m2 = m3 = m4 = Double.NaN; m4 = Double.NaN;
} }
} }

View File

@ -53,40 +53,54 @@
*/ */
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.summary.SumOfLogs; import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
/** /**
* @author Mark Diggory * @author Mark Diggory
* *
*/ */
public class GeometricMean extends SumOfLogs { public class GeometricMean extends AbstractStorelessUnivariateStatistic {
protected double geomean = Double.NaN; private SumOfLogs sumLog = new SumOfLogs();
protected int n = 0; private double value = Double.NaN;
private int n = 0;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public double increment(double d) { public double increment(double d) {
n++; n++;
super.increment(d); sumLog.increment(d);
geomean = Math.exp( sumLog / (double)n ); value = Math.exp( sumLog.increment(d) / (double)n );
return geomean; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
public double getValue() { public double getValue() {
return geomean; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
geomean = Double.NaN; value = Double.NaN;
sumLog.clear();
n = 0; n = 0;
} }
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
return Math.exp(sumLog.evaluate(values, begin, length) / (double) length );
}
} }

View File

@ -58,7 +58,7 @@ package org.apache.commons.math.stat.univariate.moment;
*/ */
public class Kurtosis extends FourthMoment { public class Kurtosis extends FourthMoment {
protected double kurtosis = Double.NaN; private double kurtosis = Double.NaN;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
@ -85,11 +85,61 @@ public class Kurtosis extends FourthMoment {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
super.internalClear(); super.clear();
kurtosis = Double.NaN; kurtosis = Double.NaN;
} }
/**
* 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) {
test(values, begin, length);
// Initialize the kurtosis
double kurt = Double.NaN;
// Get the mean and the standard deviation
double mean = super.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
double accum = 0.0;
double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) {
accum += Math.pow((values[i] - mean), 2.0);
accum2 += (values[i] - mean);
}
double stdDev =
Math.sqrt(
(accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1));
// 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++) {
accum3 += Math.pow((values[i] - mean) / stdDev, 4.0);
}
// Get N
double n = length;
double coefficientOne = (n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
double termTwo = ((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3)));
// Calculate kurtosis
kurt = (coefficientOne * accum3) - termTwo;
return kurt;
}
} }

View File

@ -54,32 +54,43 @@
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;
/** /**
* @author Mark Diggory * @author Mark Diggory
*/ */
public class Mean extends AbstractStorelessUnivariateStatistic { public class Mean extends AbstractStorelessUnivariateStatistic {
/** count of values that have been added */ /** count of values that have been added */
private int n = 0; protected int n = 0;
/** first moment of values that have been added */ /** first moment of values that have been added */
private double m1 = Double.NaN; protected double m1 = Double.NaN;
private Sum sum = new Sum();
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public double increment(double d) { public double increment(double d) {
if (n < 1) { if (n < 1) {
m1 = 0.0; m1 = 0.0;
} }
n++; n++;
m1 += (d - m1) / ((double) n); m1 += (d - m1) / ((double) n);
return m1; return m1;
} }
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
m1 = Double.NaN;
n = 0;
}
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
@ -88,11 +99,12 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
protected void internalClear() { public double evaluate(double[] values, int begin, int length) {
m1 = Double.NaN; if(test(values,begin,length))
n = 0; return sum.evaluate(values, begin, length) / ((double) length);
return Double.NaN;
} }
} }

View File

@ -53,19 +53,19 @@
*/ */
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;
/** /**
* @author Mark Diggory * @author Mark Diggory
* *
*/ */
public class SecondMoment extends AbstractStorelessUnivariateStatistic { public class SecondMoment extends Mean {
/** count of values that have been added */
protected int n = 0;
/** first moment of values that have been added */
protected double m1 = Double.NaN;
/** second moment of values that have been added */ /** second moment of values that have been added */
protected double m2 = Double.NaN; protected double m2 = Double.NaN;
@ -75,19 +75,28 @@ public class SecondMoment extends AbstractStorelessUnivariateStatistic {
*/ */
public double increment(double d) { public double increment(double d) {
if (n < 1) { if (n < 1) {
m2 = m1 = 0.0; m1 = m2 = 0.0;
} }
n++; n++;
double dev = d - m1; double dev = d - m1;
double v = dev / ((double) n); double v = dev / ((double) n);
m2 += ((double)(n - 1)) * dev * v;
m1 += v; m1 += v;
m2 += (n - 1) * dev * v;
return m2; return m2;
} }
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
super.clear();
m2 = Double.NaN;
}
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
@ -95,11 +104,4 @@ public class SecondMoment extends AbstractStorelessUnivariateStatistic {
return m2; return m2;
} }
/**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
*/
protected void internalClear() {
m2 = Double.NaN;
n = 0;
}
} }

View File

@ -59,7 +59,7 @@ package org.apache.commons.math.stat.univariate.moment;
*/ */
public class Skewness extends ThirdMoment { public class Skewness extends ThirdMoment {
protected double skewness = Double.NaN; private double skewness = Double.NaN;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
@ -87,11 +87,58 @@ public class Skewness extends ThirdMoment {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
super.internalClear(); super.clear();
skewness = Double.NaN; skewness = Double.NaN;
} }
/**
* 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
*/
public double evaluate(double[] values, int begin, int length) {
test(values, begin, length);
// Initialize the skewness
double skew = Double.NaN;
// Get the mean and the standard deviation
double mean = super.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
double accum = 0.0;
double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) {
accum += Math.pow((values[i] - mean), 2.0);
accum2 += (values[i] - mean);
}
double stdDev =
Math.sqrt(
(accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1));
// 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++) {
accum3 += Math.pow((values[i] - mean) / stdDev, 3.0);
}
// Get N
double n = length;
// Calculate skewness
skew = (n / ((n - 1) * (n - 2))) * accum3;
return skew;
}
} }

View File

@ -53,35 +53,48 @@
*/ */
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
/** /**
* @author Mark Diggory * @author Mark Diggory
* *
*/ */
public class StandardDeviation extends Variance { public class StandardDeviation extends AbstractStorelessUnivariateStatistic {
protected double std = Double.NaN; private double value = Double.NaN;
private Variance var = new Variance();
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public double increment(double d) { public double increment(double d) {
std = Math.sqrt(super.increment(d)); var.increment(d);
return std; value = Math.sqrt(var.getValue());
return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
public double getValue() { public double getValue() {
return std; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
super.internalClear(); var.clear();
std = Double.NaN; value = Double.NaN;
}
/* (non-Javadoc)
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double tmp = var.evaluate(values, begin, length);
return tmp != 0.0 ? Math.sqrt(tmp) : 0.0;
} }
} }

View File

@ -53,22 +53,11 @@
*/ */
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
/** /**
* @author Mark Diggory * @author Mark Diggory
* *
*/ */
public class ThirdMoment extends AbstractStorelessUnivariateStatistic{ public class ThirdMoment extends SecondMoment{
/** count of values that have been added */
protected int n = 0;
/** first moment of values that have been added */
protected double m1 = Double.NaN;
/** second moment of values that have been added */
protected double m2 = Double.NaN;
/** 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;
@ -82,6 +71,7 @@ public class ThirdMoment extends AbstractStorelessUnivariateStatistic{
} }
n++; n++;
double dev = d - m1; double dev = d - m1;
double v = dev / ((double) n); double v = dev / ((double) n);
double v2 = v * v; double v2 = v * v;
@ -108,10 +98,11 @@ public class ThirdMoment extends AbstractStorelessUnivariateStatistic{
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
n = 0; super.clear();
m1 = m2 = m3 = Double.NaN; m3 = Double.NaN;
} }
} }

View File

@ -53,20 +53,30 @@
*/ */
package org.apache.commons.math.stat.univariate.moment; package org.apache.commons.math.stat.univariate.moment;
import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
/** /**
* @author Mark Diggory * @author Mark Diggory
* *
*/ */
public class Variance extends SecondMoment { public class Variance extends SecondMoment {
protected double variance = Double.NaN; private double variance = Double.NaN;
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double) * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/ */
public double increment(double d) { public double increment(double d) {
super.increment(d); super.increment(d);
variance = (n <= 1) ? 0.0 : m2 / (double) (n - 1);
variance = (n < 1) ? 0.0 : m2 / (double)(n - 1);
return variance; return variance;
} }
@ -78,10 +88,35 @@ public class Variance extends SecondMoment {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
super.internalClear(); super.clear();
variance = Double.NaN; variance = Double.NaN;
} }
/* (non-Javadoc)
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double var = Double.NaN;
if (values.length == 1) {
var = 0;
} else if (values.length > 1) {
double m = super.evaluate(values, begin, length);
double accum = 0.0;
double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) {
accum += Math.pow((values[i] - m), 2.0);
accum2 += (values[i] - m);
}
var =
(accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1);
}
return var;
}
} }

View File

@ -53,14 +53,20 @@
*/ */
package org.apache.commons.math.stat.univariate.rank; package org.apache.commons.math.stat.univariate.rank;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
/** /**
* @author Mark Diggory * @author Mark Diggory
*/ */
public class Max extends AbstractStorelessUnivariateStatistic { public class Max extends AbstractStorelessUnivariateStatistic {
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)
@ -69,6 +75,13 @@ public class Max extends AbstractStorelessUnivariateStatistic {
return value = Double.isNaN(value) ? d : Math.max(value, d); return value = Double.isNaN(value) ? d : Math.max(value, d);
} }
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
value = Double.NaN;
}
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
@ -76,10 +89,17 @@ public class Max extends AbstractStorelessUnivariateStatistic {
return value; return value;
} }
/** /* (non-Javadoc)
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
protected void internalClear() { public double evaluate(double[] values, int begin, int length) {
value = Double.NaN; double max = Double.NaN;
if (test(values, begin, length)) {
max = values[begin];
for (int i = begin; i < begin + length; i++) {
max = (max > values[i]) ? max : values[i];
}
}
return max;
} }
} }

View File

@ -53,20 +53,34 @@
*/ */
package org.apache.commons.math.stat.univariate.rank; package org.apache.commons.math.stat.univariate.rank;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
/** /**
* @author Mark Diggory * @author Mark Diggory
*/ */
public class Min extends AbstractStorelessUnivariateStatistic { public class Min extends AbstractStorelessUnivariateStatistic {
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 double increment(double d) { public double increment(double d) {
return value = Double.isNaN(value) ? d : Math.min(value, d); value = Double.isNaN(value) ? d : Math.min(value, d);
return value;
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
value = Double.NaN;
} }
/** /**
@ -77,9 +91,16 @@ public class Min extends AbstractStorelessUnivariateStatistic {
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/ */
protected void internalClear() { public double evaluate(double[] values, int begin, int length) {
value = Double.NaN; double min = Double.NaN;
if (test(values, begin, length)) {
min = values[begin];
for (int i = begin; i < begin + length; i++) {
min = (min < values[i]) ? min : values[i];
}
}
return min;
} }
} }

View File

@ -86,36 +86,36 @@ public class Percentile extends AbstractUnivariateStatistic {
* 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 d 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[] d, double p) { public double evaluate(double[] values, double p) {
return evaluate(d, 0,d.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[] d, int start, int length) { public double evaluate(double[] values, int start, int length) {
return evaluate(d, 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 d 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[] d, int start, int length, double p) { public double evaluate(double[] values, int begin, int length, double p) {
test(d,start,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");
@ -125,14 +125,14 @@ public class Percentile extends AbstractUnivariateStatistic {
return Double.NaN; return Double.NaN;
} }
if (n == 1) { if (n == 1) {
return d[start]; // always return single value for n = 1 return values[begin]; // always return single value for n = 1
} }
double pos = p * (n + 1) / 100; double pos = p * (n + 1) / 100;
double fpos = Math.floor(pos); double fpos = Math.floor(pos);
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(d, start,sorted, 0, length); System.arraycopy(values, begin,sorted, 0, length);
Arrays.sort(sorted); Arrays.sort(sorted);
if (pos < 1) { if (pos < 1) {
@ -158,7 +158,7 @@ public class Percentile extends AbstractUnivariateStatistic {
/** /**
* The default internal state of this percentile can be set. * The default internal state of this percentile can be set.
* This will setthat value. * This will setthat value.
* @param d a value between 0 <= p <= 100 * @param p a value between 0 <= p <= 100
*/ */
public void setPercentile(double p) { public void setPercentile(double p) {
percentile = p; percentile = p;

View File

@ -69,33 +69,47 @@ public class Product extends AbstractStorelessUnivariateStatistic {
/** /**
* The current Running Product. * The current Running Product.
*/ */
double product = 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 double increment(double d) { public double increment(double d) {
if (init) { if (Double.isNaN(value)) {
init = false; value = d;
product = d;
} else { } else {
product *= d; value *= d;
} }
return product; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
public double getValue() { public double getValue() {
return product; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
product = Double.NaN; value = Double.NaN;
} }
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double product = Double.NaN;
if (test(values, begin, length)) {
product = 1.0;
for (int i = begin; i < begin + length; i++) {
product *= values[i];
}
}
return product;
}
} }

View File

@ -70,33 +70,48 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
/** /**
* The currently running sum. * The currently running sum.
*/ */
protected double sum = 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 double increment(double d) { public double increment(double d) {
if (init) { if (Double.isNaN(value )) {
init = false; value = d;
sum = d;
} else { } else {
sum += d; value += d;
} }
return sum; return value ;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
public double getValue() { public double getValue() {
return sum; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
sum = Double.NaN; value = Double.NaN;
} }
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double sum = Double.NaN;
if (test(values, begin, length)) {
sum = 0.0;
for (int i = begin; i < begin + length; i++) {
sum += values[i];
}
}
return sum;
}
} }

View File

@ -68,35 +68,48 @@ import org
public class SumOfLogs extends AbstractStorelessUnivariateStatistic { public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
/** /**
* The currently running sumLog * The currently running value
*/ */
protected double sumLog = 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 double increment(double d) { public double increment(double d) {
if (init) { if (Double.isNaN(value )) {
sumLog = Math.log(d); value = Math.log(d);
init = false;
} else { } else {
sumLog += Math.log(d); value += Math.log(d);
} }
return sumLog; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
public double getValue() { public double getValue() {
return sumLog; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
sumLog = Double.NaN; value = Double.NaN;
}
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double sumLog = Double.NaN;
if (test(values, begin, length)) {
sumLog = 0.0;
for (int i = begin; i < begin + length; i++) {
sumLog += Math.log(values[i]);
}
}
return sumLog;
} }
} }

View File

@ -70,33 +70,47 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
/** /**
* The currently running sumSq * The currently running sumSq
*/ */
protected double sumSq = 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 double increment(double d) { public double increment(double d) {
if (init) { if (Double.isNaN(value )) {
init = false; value = d * d;
sumSq = d * d;
} else { } else {
sumSq += d * d; value += d * d;
} }
return sumSq; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/ */
public double getValue() { public double getValue() {
return sumSq; return value;
} }
/** /**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear() * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/ */
protected void internalClear() { public void clear() {
sumSq = Double.NaN; value = Double.NaN;
} }
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double sumSq = Double.NaN;
if (test(values, begin, length)) {
sumSq = 0.0;
for (int i = begin; i < begin + length; i++) {
sumSq += Math.pow(values[i], 2.0);
}
}
return sumSq;
}
} }