Last commit got formated with tabs, this is formated with spaces

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140925 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-06-18 13:57:24 +00:00
parent 202a38df83
commit 283716c077
1 changed files with 270 additions and 264 deletions

View File

@ -18,7 +18,7 @@
* *
* 3. The end-user documentation included with the redistribution, if * 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement: * any, must include the following acknowlegement:
* "This sumLog includes software developed by the * "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)." * Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself, * Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear. * if and wherever such third-party acknowlegements normally appear.
@ -71,323 +71,329 @@ import org.apache.commons.math.FixedDoubleArray;
* @author <a href="mailto:mdiggory@apache.org">Mark Diggory</a> * @author <a href="mailto:mdiggory@apache.org">Mark Diggory</a>
* @author Brent Worden * @author Brent Worden
* @author <a href="mailto:HotFusionMan@Yahoo.com">Albert Davidson Chou</a> * @author <a href="mailto:HotFusionMan@Yahoo.com">Albert Davidson Chou</a>
* @version $Revision: 1.10 $ $Date: 2003/06/18 13:47:35 $ * @version $Revision: 1.11 $ $Date: 2003/06/18 13:57:24 $
* *
*/ */
public class UnivariateImpl implements Univariate, Serializable { public class UnivariateImpl implements Univariate, Serializable {
/** hold the window size **/ /** hold the window size **/
private int windowSize = Univariate.INFINITE_WINDOW; private int windowSize = Univariate.INFINITE_WINDOW;
/** Just in case the windowSize is not infinite, we need to /** Just in case the windowSize is not infinite, we need to
* keep an array to remember values 0 to N * keep an array to remember values 0 to N
*/ */
private DoubleArray doubleArray; private DoubleArray doubleArray;
/** count of values that have been added */ /** count of values that have been added */
private int n = 0; private int n = 0;
/** sum of values that have been added */ /** sum of values that have been added */
private double sum = Double.NaN; private double sum = Double.NaN;
/** sum of the square of each value that has been added */ /** sum of the square of each value that has been added */
private double sumsq = Double.NaN; private double sumsq = Double.NaN;
/** min of values that have been added */ /** min of values that have been added */
private double min = Double.NaN; private double min = Double.NaN;
/** max of values that have been added */ /** max of values that have been added */
private double max = Double.NaN; private double max = Double.NaN;
/** sumLog of values that have been added */ /** sumLog of values that have been added */
private double sumLog = Double.NaN; private double sumLog = Double.NaN;
/** mean of values that have been added */ /** mean of values that have been added */
private double mean = Double.NaN; private double mean = Double.NaN;
/** second moment of values that have been added */ /** second moment of values that have been added */
private double s2 = Double.NaN; private double s2 = Double.NaN;
/** third moment of values that have been added */ /** third moment of values that have been added */
private double s3 = Double.NaN; private double s3 = Double.NaN;
/** fourth moment of values that have been added */ /** fourth moment of values that have been added */
private double s4 = Double.NaN; private double s4 = Double.NaN;
/** variance of values that have been added */ /** variance of values that have been added */
private double variance = Double.NaN; private double variance = Double.NaN;
/** skewness of values that have been added */ /** skewness of values that have been added */
private double skewness = Double.NaN; private double skewness = Double.NaN;
/** kurtosis of values that have been added */ /** kurtosis of values that have been added */
private double kurtosis = Double.NaN; private double kurtosis = Double.NaN;
/** Creates new univariate with an infinite window */ /** Creates new univariate with an infinite window */
public UnivariateImpl() { public UnivariateImpl() {
} }
/** Creates a new univariate with a fixed window **/ /** Creates a new univariate with a fixed window **/
public UnivariateImpl(int window) { public UnivariateImpl(int window) {
setWindowSize(window); setWindowSize(window);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getN() * @see org.apache.commons.math.stat.Univariate#getN()
*/ */
public int getN() { public int getN() {
return n; return n;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getSum() * @see org.apache.commons.math.stat.Univariate#getSum()
*/ */
public double getSum() { public double getSum() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.sum(doubleArray.getElements()); return StatUtils.sum(doubleArray.getElements());
} }
return sum; return sum;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getSumsq() * @see org.apache.commons.math.stat.Univariate#getSumsq()
*/ */
public double getSumsq() { public double getSumsq() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.sumSq(doubleArray.getElements()); return StatUtils.sumSq(doubleArray.getElements());
} }
return sumsq; return sumsq;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getMean() * @see org.apache.commons.math.stat.Univariate#getMean()
*/ */
public double getMean() { public double getMean() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.mean(doubleArray.getElements()); return StatUtils.mean(doubleArray.getElements());
} }
return mean; return mean;
} }
/** /**
* Returns the standard deviation for this collection of values * Returns the standard deviation for this collection of values
* @see org.apache.commons.math.stat.Univariate#getStandardDeviation() * @see org.apache.commons.math.stat.Univariate#getStandardDeviation()
*/ */
public double getStandardDeviation() { public double getStandardDeviation() {
double stdDev = Double.NaN; double stdDev = Double.NaN;
if (getN() != 0) { if (getN() != 0) {
stdDev = Math.sqrt(getVariance()); stdDev = Math.sqrt(getVariance());
} }
return (stdDev); return (stdDev);
} }
/** /**
* Returns the variance of the values that have been added via West's * Returns the variance of the values that have been added via West's
* algorithm as described by * algorithm as described by
* <a href="http://doi.acm.org/10.1145/359146.359152">Chan, T. F. and * <a href="http://doi.acm.org/10.1145/359146.359152">Chan, T. F. and
* J. G. Lewis 1979, <i>Communications of the ACM</i>, * J. G. Lewis 1979, <i>Communications of the ACM</i>,
* vol. 22 no. 9, pp. 526-531.</a>. * vol. 22 no. 9, pp. 526-531.</a>.
* *
* @return The variance of a set of values. Double.NaN is returned for * @return The variance of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a &lt;= 1 value set. * an empty set of values and 0.0 is returned for a &lt;= 1 value set.
*/ */
public double getVariance() { public double getVariance() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
variance = StatUtils.variance(doubleArray.getElements()); variance = StatUtils.variance(doubleArray.getElements());
} }
return variance; return variance;
} }
/** /**
* Returns the skewness of the values that have been added as described by * Returns the skewness of the values that have been added as described by
* <a href="http://mathworld.wolfram.com/k-Statistic.html">Equation (6) for k-Statistics</a>. * <a href="http://mathworld.wolfram.com/k-Statistic.html">Equation (6) for k-Statistics</a>.
* *
* @return The skew of a set of values. Double.NaN is returned for * @return The skew of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a &lt;= 2 value set. * an empty set of values and 0.0 is returned for a &lt;= 2 value set.
*/ */
public double getSkewness() { public double getSkewness() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.skewness(doubleArray.getElements()); return StatUtils.skewness(doubleArray.getElements());
} }
return skewness; return skewness;
} }
/** /**
* Returns the kurtosis of the values that have been added as described by * Returns the kurtosis of the values that have been added as described by
* <a href="http://mathworld.wolfram.com/k-Statistic.html">Equation (7) for k-Statistics</a>. * <a href="http://mathworld.wolfram.com/k-Statistic.html">Equation (7) for k-Statistics</a>.
* *
* @return The kurtosis of a set of values. Double.NaN is returned for * @return The kurtosis of a set of values. Double.NaN is returned for
* an empty set of values and 0.0 is returned for a &lt;= 3 value set. * an empty set of values and 0.0 is returned for a &lt;= 3 value set.
*/ */
public double getKurtosis() { public double getKurtosis() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.kurtosis(doubleArray.getElements()); return StatUtils.kurtosis(doubleArray.getElements());
} }
return kurtosis; return kurtosis;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getMax() * @see org.apache.commons.math.stat.Univariate#getMax()
*/ */
public double getMax() { public double getMax() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.max(doubleArray.getElements()); return StatUtils.max(doubleArray.getElements());
} }
return max; return max;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getMin() * @see org.apache.commons.math.stat.Univariate#getMin()
*/ */
public double getMin() { public double getMin() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.min(doubleArray.getElements()); return StatUtils.min(doubleArray.getElements());
} }
return min; return min;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getProduct() * @see org.apache.commons.math.stat.Univariate#getProduct()
*/ */
public double getProduct() { public double getProduct() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.product(doubleArray.getElements()); return StatUtils.product(doubleArray.getElements());
} }
return sumLog; return sumLog;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.stat.Univariate#getGeometricMean() * @see org.apache.commons.math.stat.Univariate#getGeometricMean()
*/ */
public double getGeometricMean() { public double getGeometricMean() {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
return StatUtils.geometricMean(doubleArray.getElements()); return StatUtils.geometricMean(doubleArray.getElements());
} }
if (n == 0) { if (n == 0) {
return Double.NaN; return Double.NaN;
} else { } else {
return Math.exp(sumLog / (double) n); return Math.exp(sumLog / (double) n);
} }
} }
/* If windowSize is set to Infinite, moments are calculated using the following /* If windowSize is set to Infinite, moments are calculated using the following
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf"> * <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy * recursive strategy
* </a>. * </a>.
* Otherwise, stat methods delegate to StatUtils. * Otherwise, stat methods delegate to StatUtils.
* @see org.apache.commons.math.stat.Univariate#addValue(double) * @see org.apache.commons.math.stat.Univariate#addValue(double)
*/ */
public void addValue(double value) { public void addValue(double value) {
if (windowSize != Univariate.INFINITE_WINDOW) { if (windowSize != Univariate.INFINITE_WINDOW) {
/* then all getters deligate to StatUtils /* then all getters deligate to StatUtils
* and this clause simply adds/rolls a value in the storage array * and this clause simply adds/rolls a value in the storage array
*/ */
if (windowSize == n) { if (windowSize == n) {
doubleArray.addElementRolling(value); doubleArray.addElementRolling(value);
} else { } else {
n++; n++;
doubleArray.addElement(value); doubleArray.addElement(value);
} }
} else { } else {
/* If the windowSize is infinite don't store any values and there /* If the windowSize is infinite don't store any values and there
* is no need to discard the influence of any single item. * is no need to discard the influence of any single item.
*/ */
n++; n++;
if (n <= 1) { if (n <= 1) {
/* if n <= 1, initialize the sumLog, min, max, mean, variance and pre-variance */ /* if n <= 1, initialize the sumLog, min, max, mean, variance and pre-variance */
sumLog = 0.0; sumLog = 0.0;
sum = min = max = mean = value; sum = min = max = mean = value;
sumsq = Math.pow(value, 2); sumsq = Math.pow(value, 2);
variance = s2 = 0.0; variance = s2 = 0.0;
skewness = kurtosis = 0.0; skewness = kurtosis = 0.0;
} else { } else {
/* otherwise calc these values */ /* otherwise calc these values */
sumLog += Math.log(value); sumLog += Math.log(value);
sum += value; sum += value;
sumsq += Math.pow(value, 2); sumsq += Math.pow(value, 2);
min = Math.min(min, value); min = Math.min(min, value);
max = Math.max(max, value); max = Math.max(max, value);
double dev = value - mean; double dev = value - mean;
double v = dev / ((double) n); double v = dev / ((double) n);
double v2 = Math.pow(v, 2); double v2 = Math.pow(v, 2);
double n1 = ((double) n - 1); double n1 = ((double) n - 1);
s4 += v s4 += v
* ( * (
- 4.0 * s3 - 4.0 * s3
+ v * (6.0 * s2 + n1 * (1 + Math.pow((double) n, 3)) * v2)); + v
* (6.0 * s2
+ n1 * (1 + Math.pow((double) n, 3)) * v2));
s3 += v * (-3.0 * s2 + (double) n * n1 * (n - 2) * Math.pow(v, 2)); s3 += v
s2 += n1 * dev * v; * (-3.0 * s2 + (double) n * n1 * (n - 2) * Math.pow(v, 2));
s2 += n1 * dev * v;
mean += v; mean += v;
variance = variance = (n <= 1) ? 0.0 : s2 / n1;
(n <= 1) ? 0.0 : s2 / n1; skewness =
skewness = (n <= 2)
(n <= 2) ? 0.0 : s3 / ((double) n * Math.sqrt(variance) * variance); ? 0.0
kurtosis = : s3 / ((double) n * Math.sqrt(variance) * variance);
(n <= 3) ? 0.0 : s4 / ((double) n * Math.pow(variance, 2)) - 3; kurtosis =
} (n <= 3)
} ? 0.0
} : s4 / ((double) n * Math.pow(variance, 2)) - 3;
}
}
}
/** /**
* Generates a text report displaying * Generates a text report displaying
* univariate statistics from values that * univariate statistics from values that
* have been added. * have been added.
* @return String with line feeds displaying statistics * @return String with line feeds displaying statistics
*/ */
public String toString() { public String toString() {
StringBuffer outBuffer = new StringBuffer(); StringBuffer outBuffer = new StringBuffer();
outBuffer.append("UnivariateImpl:\n"); outBuffer.append("UnivariateImpl:\n");
outBuffer.append("n: " + n + "\n"); outBuffer.append("n: " + n + "\n");
outBuffer.append("min: " + min + "\n"); outBuffer.append("min: " + min + "\n");
outBuffer.append("max: " + max + "\n"); outBuffer.append("max: " + max + "\n");
outBuffer.append("mean: " + getMean() + "\n"); outBuffer.append("mean: " + getMean() + "\n");
outBuffer.append("std dev: " + getStandardDeviation() + "\n"); outBuffer.append("std dev: " + getStandardDeviation() + "\n");
outBuffer.append("skewness: " + getSkewness() + "\n"); outBuffer.append("skewness: " + getSkewness() + "\n");
outBuffer.append("kurtosis: " + getKurtosis() + "\n"); outBuffer.append("kurtosis: " + getKurtosis() + "\n");
return outBuffer.toString(); return outBuffer.toString();
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.Univariate#clear() * @see org.apache.commons.math.Univariate#clear()
*/ */
public void clear() { public void clear() {
this.n = 0; this.n = 0;
this.min = this.max = Double.NaN; this.min = this.max = Double.NaN;
this.sumLog = this.mean = Double.NaN; this.sumLog = this.mean = Double.NaN;
this.variance = this.skewness = this.kurtosis = Double.NaN; this.variance = this.skewness = this.kurtosis = Double.NaN;
this.s2 = this.s3 = this.s4 = Double.NaN; this.s2 = this.s3 = this.s4 = Double.NaN;
if (doubleArray != null) if (doubleArray != null)
doubleArray = new FixedDoubleArray(windowSize); doubleArray = new FixedDoubleArray(windowSize);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.Univariate#getWindowSize() * @see org.apache.commons.math.Univariate#getWindowSize()
*/ */
public int getWindowSize() { public int getWindowSize() {
return windowSize; return windowSize;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.apache.commons.math.Univariate#setWindowSize(int) * @see org.apache.commons.math.Univariate#setWindowSize(int)
*/ */
public void setWindowSize(int windowSize) { public void setWindowSize(int windowSize) {
clear(); clear();
this.windowSize = windowSize; this.windowSize = windowSize;
doubleArray = new FixedDoubleArray(windowSize); doubleArray = new FixedDoubleArray(windowSize);
} }
} }