In cases where "getResult" contains a statistical calculation, it now
checks if the internal state of the underlying statistic has changed and only recalculates if this is true. this way muliple calls to getResult when the staistic has not been incremented are less expensive because it is only returning a property value. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5192373270
commit
fe37437c08
|
@ -57,9 +57,8 @@ package org.apache.commons.math.stat.univariate;
|
|||
*
|
||||
* Abstract Implementation for StorelessUnivariateStatistics.
|
||||
* Provides the ability to extend polymophically so that
|
||||
* indiviual statistics do not need to implement these methods
|
||||
*
|
||||
* @author Mark Diggory
|
||||
* indiviual statistics do not need to implement these methods unless
|
||||
* there are better algorithms for handling the calculation.
|
||||
*/
|
||||
public abstract class AbstractStorelessUnivariateStatistic
|
||||
extends AbstractUnivariateStatistic
|
||||
|
@ -67,7 +66,9 @@ public abstract class AbstractStorelessUnivariateStatistic
|
|||
|
||||
/**
|
||||
* This implements the AbstractUnivariateStatistic impl to funnel
|
||||
* calculation off to the instantanious increment method.
|
||||
* calculation off to the instantanious increment method. In most cases of
|
||||
* 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)
|
||||
*/
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
|
@ -77,6 +78,6 @@ public abstract class AbstractStorelessUnivariateStatistic
|
|||
increment(values[i]);
|
||||
}
|
||||
}
|
||||
return getValue();
|
||||
return getResult();
|
||||
}
|
||||
}
|
|
@ -57,8 +57,6 @@ 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.
|
||||
*
|
||||
* @author Mark Diggory
|
||||
*/
|
||||
public abstract class AbstractUnivariateStatistic
|
||||
implements UnivariateStatistic {
|
||||
|
|
|
@ -55,9 +55,11 @@ package org.apache.commons.math.stat.univariate;
|
|||
|
||||
/**
|
||||
* StorelessUnivariate interface provides methods to increment and access
|
||||
* the internal state of the Statistic.
|
||||
*
|
||||
* @author Mark Diggory
|
||||
* the internal state of the Statistic. A StorelessUnivariateStatistic does
|
||||
* not require that a double[] storage structure be maintained with the values
|
||||
* in it. As such only a subset of known statistics can actually be implmented
|
||||
* using it. If a Statistic cannot be implemented in a Storeless approach it
|
||||
* should implement the UnivariateStatistic interface directly instead.
|
||||
*/
|
||||
public interface StorelessUnivariateStatistic extends UnivariateStatistic {
|
||||
|
||||
|
@ -65,19 +67,16 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
|
|||
* Increments the internal state of the Storagless
|
||||
* Implementation.
|
||||
* @param d is the value to increment the state by.
|
||||
* @return the new state of this Statistic. Returns
|
||||
* the same value as <code>getValue()</code>, Double.NaN if it
|
||||
* has been cleared or just instantiated.
|
||||
*/
|
||||
public void increment(double d);
|
||||
|
||||
/**
|
||||
* Returns the current state of the statistic after the
|
||||
* last increment.
|
||||
* @return values of the statistic, Double.NaN if it
|
||||
* @return value of the statistic, Double.NaN if it
|
||||
* has been cleared or just instantiated.
|
||||
*/
|
||||
public double getValue();
|
||||
public double getResult();
|
||||
|
||||
|
||||
/**
|
||||
|
@ -85,22 +84,4 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
|
|||
*/
|
||||
public void clear();
|
||||
|
||||
/**
|
||||
* Returns the behavior of this statistic when evaluating
|
||||
* double[]'s using <code>evaluate(double[], int, int)</code> and
|
||||
* <code>evaluate(double[])</code> methods.
|
||||
* @return the state
|
||||
*/
|
||||
//public boolean isClearOnEval();
|
||||
|
||||
/**
|
||||
* Sets the behavior of this statistic when evaluating
|
||||
* double[]'s using <code>evaluate(double[], int, int)</code> and
|
||||
* <code>evaluate(double[])</code> methods. if true, the internal state
|
||||
* will be cleared between evaluations, otherwise it will be
|
||||
* incrimented.
|
||||
* @param b true | false
|
||||
*/
|
||||
//public void setClearOnEval(boolean b);
|
||||
|
||||
}
|
|
@ -54,8 +54,6 @@
|
|||
package org.apache.commons.math.stat.univariate;
|
||||
|
||||
/**
|
||||
* @author Mark Diggory
|
||||
*
|
||||
* UnivariateStatistic interface provides methods to evaluate
|
||||
* double[] based content using a particular algorithm.
|
||||
*
|
||||
|
|
|
@ -105,7 +105,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return m1;
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ public class FourthMoment extends ThirdMoment {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return m4;
|
||||
}
|
||||
|
||||
|
|
|
@ -61,8 +61,11 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
|
|||
*/
|
||||
public class GeometricMean extends SumOfLogs {
|
||||
|
||||
private int n = 0;
|
||||
protected int n = 0;
|
||||
|
||||
private double geoMean = Double.NaN;
|
||||
|
||||
private double lastSum = 0.0;
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
|
@ -74,8 +77,12 @@ public class GeometricMean extends SumOfLogs {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
return Math.exp( super.getValue() / (double)n );
|
||||
public double getResult() {
|
||||
if (lastSum != super.getResult() || n == 1) {
|
||||
lastSum = super.getResult();
|
||||
geoMean = Math.exp(lastSum / (double) n);
|
||||
}
|
||||
return geoMean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,6 +90,8 @@ public class GeometricMean extends SumOfLogs {
|
|||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
lastSum = 0.0;
|
||||
geoMean = Double.NaN;
|
||||
n = 0;
|
||||
}
|
||||
|
||||
|
@ -96,9 +105,8 @@ public class GeometricMean extends SumOfLogs {
|
|||
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
|
||||
*/
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
return Math.exp(super.evaluate(values, begin, length) / (double) length );
|
||||
return Math.exp(
|
||||
super.evaluate(values, begin, length) / (double) length);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -70,6 +70,10 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
|
||||
protected boolean incMoment = true;
|
||||
|
||||
private double kurtosis = Double.NaN;
|
||||
|
||||
private int n = 0;
|
||||
|
||||
public Kurtosis() {
|
||||
moment = new FourthMoment();
|
||||
}
|
||||
|
@ -91,23 +95,31 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
if (n < moment.n) {
|
||||
if (moment.n <= 0) {
|
||||
kurtosis = Double.NaN;
|
||||
}
|
||||
|
||||
if (moment.n <= 0) {
|
||||
return Double.NaN;
|
||||
double variance =
|
||||
(moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
|
||||
|
||||
if (moment.n <= 3 || variance < 10E-20) {
|
||||
kurtosis = 0.0;
|
||||
} else {
|
||||
kurtosis =
|
||||
(moment.n0 * (moment.n0 + 1) * moment.m4
|
||||
- 3 * moment.m2 * moment.m2 * moment.n1)
|
||||
/ (moment.n1
|
||||
* moment.n2
|
||||
* moment.n3
|
||||
* variance
|
||||
* variance);
|
||||
}
|
||||
n = moment.n;
|
||||
}
|
||||
|
||||
double variance =
|
||||
(moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
|
||||
|
||||
if (moment.n <= 3 || variance < 10E-20) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (moment.n0 * (moment.n0 + 1) * moment.m4
|
||||
- 3 * moment.m2 * moment.m2 * moment.n1)
|
||||
/ (moment.n1 * moment.n2 * moment.n3 * variance * variance);
|
||||
|
||||
return kurtosis;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,6 +129,8 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
if (incMoment) {
|
||||
moment.clear();
|
||||
}
|
||||
kurtosis = Double.NaN;
|
||||
n = 0;
|
||||
}
|
||||
|
||||
/*UnvariateStatistic Approach */
|
||||
|
@ -125,12 +139,13 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
|
|||
|
||||
/**
|
||||
* 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.
|
||||
* 1983, American Statistician, vol. 37, pp. 242?247.
|
||||
* <p/>
|
||||
* <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.
|
||||
* 1983, American Statistician, vol. 37, pp. 242?247.
|
||||
* </p>
|
||||
* 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
|
||||
|
|
|
@ -102,7 +102,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return moment.m1;
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
|
|||
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -80,6 +80,7 @@ public class SecondMoment extends FirstMoment {
|
|||
|
||||
/* increment and return m2 */
|
||||
m2 += n1 * dev * v;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,7 +95,7 @@ public class SecondMoment extends FirstMoment {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return m2;
|
||||
}
|
||||
|
||||
|
|
|
@ -71,6 +71,10 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
|
||||
protected boolean incMoment = true;
|
||||
|
||||
protected double skewness = Double.NaN;
|
||||
|
||||
private int n = 0;
|
||||
|
||||
public Skewness() {
|
||||
moment = new ThirdMoment();
|
||||
}
|
||||
|
@ -92,20 +96,28 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
if (moment.n <= 0) {
|
||||
return Double.NaN;
|
||||
public double getResult() {
|
||||
if (n < moment.n) {
|
||||
if (moment.n <= 0) {
|
||||
skewness = Double.NaN;
|
||||
}
|
||||
|
||||
double variance =
|
||||
(moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
|
||||
|
||||
if (moment.n <= 2 || variance < 10E-20) {
|
||||
skewness = 0.0;
|
||||
} else {
|
||||
skewness =
|
||||
(moment.n0 * moment.m3)
|
||||
/ (moment.n1
|
||||
* moment.n2
|
||||
* Math.sqrt(variance)
|
||||
* variance);
|
||||
}
|
||||
n = moment.n;
|
||||
}
|
||||
|
||||
double variance =
|
||||
(moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
|
||||
|
||||
if (moment.n <= 2 || variance < 10E-20) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (moment.n0 * moment.m3)
|
||||
/ (moment.n1 * moment.n2 * Math.sqrt(variance) * variance);
|
||||
return skewness;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,6 +127,8 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
if (incMoment) {
|
||||
moment.clear();
|
||||
}
|
||||
skewness = Double.NaN;
|
||||
n = 0;
|
||||
}
|
||||
|
||||
/*UnvariateStatistic Approach */
|
||||
|
@ -124,11 +138,12 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* 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/>
|
||||
* 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.
|
||||
* 1983, American Statistician, vol. 37, pp. 242?247.
|
||||
* <p/>
|
||||
* </p>
|
||||
* 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
|
||||
|
|
|
@ -59,11 +59,15 @@ package org.apache.commons.math.stat.univariate.moment;
|
|||
*/
|
||||
public class StandardDeviation extends Variance {
|
||||
|
||||
public StandardDeviation(){
|
||||
protected double std = Double.NaN;
|
||||
|
||||
private double lastVar = 0.0;
|
||||
|
||||
public StandardDeviation() {
|
||||
super();
|
||||
}
|
||||
|
||||
public StandardDeviation(SecondMoment m2){
|
||||
public StandardDeviation(SecondMoment m2) {
|
||||
super(m2);
|
||||
}
|
||||
|
||||
|
@ -77,17 +81,18 @@ public class StandardDeviation extends Variance {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
|
||||
double var = super.getValue();
|
||||
|
||||
if(Double.isNaN(var)){
|
||||
return Double.NaN;
|
||||
}else if (var == 0.0){
|
||||
return 0.0;
|
||||
public double getResult() {
|
||||
if (lastVar != super.getResult()) {
|
||||
lastVar = super.getResult();
|
||||
if (Double.isNaN(lastVar)) {
|
||||
std = Double.NaN;
|
||||
} else if (lastVar == 0.0) {
|
||||
std = 0.0;
|
||||
} else {
|
||||
std = Math.sqrt(lastVar);
|
||||
}
|
||||
}
|
||||
|
||||
return Math.sqrt(var);
|
||||
return std;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,6 +100,7 @@ public class StandardDeviation extends Variance {
|
|||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
lastVar = 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,7 +115,7 @@ public class StandardDeviation extends Variance {
|
|||
public double evaluate(double[] values, int begin, int length) {
|
||||
double var = super.evaluate(values, begin, length);
|
||||
|
||||
if(Double.isNaN(var)){
|
||||
if (Double.isNaN(var)) {
|
||||
return Double.NaN;
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ public class ThirdMoment extends SecondMoment{
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return m3;
|
||||
}
|
||||
|
||||
|
|
|
@ -71,6 +71,10 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
|
||||
protected boolean incMoment = true;
|
||||
|
||||
protected double variance = Double.NaN;
|
||||
|
||||
protected int n = 0;
|
||||
|
||||
public Variance() {
|
||||
moment = new SecondMoment();
|
||||
}
|
||||
|
@ -91,13 +95,19 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
if (moment.n <= 0) {
|
||||
return Double.NaN;
|
||||
} else if (moment.n <= 1) {
|
||||
return 0.0;
|
||||
public double getResult() {
|
||||
if (n < moment.n) {
|
||||
if (moment.n <= 0) {
|
||||
variance = Double.NaN;
|
||||
} else if (moment.n <= 1) {
|
||||
variance = 0.0;
|
||||
} else {
|
||||
variance = moment.m2 / (moment.n0 - 1);
|
||||
}
|
||||
n = moment.n;
|
||||
}
|
||||
return moment.m2 / (moment.n0 - 1);
|
||||
|
||||
return variance;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,6 +117,8 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
if (incMoment) {
|
||||
moment.clear();
|
||||
}
|
||||
variance = Double.NaN;
|
||||
n = 0;
|
||||
}
|
||||
|
||||
/*UnvariateStatistic Approach */
|
||||
|
@ -117,11 +129,12 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
|
|||
* Returns the variance of the available values. This 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/>
|
||||
* 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.
|
||||
* 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
|
||||
|
|
|
@ -85,7 +85,7 @@ public class Max extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ public class Min extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ public class Product extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
public double getResult() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue