Its more logical not to have increment return a value, this allows a

distict separation between the calculation of moments and the 
calculation of the actually return value of getVlaue in Storageless 
approaches. With this benifit it is no longer neccessary to calculate 
all the statistics moment based statistics on addValue, only the 
underlying moment itself.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140976 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-07-08 03:44:12 +00:00
parent 9a827be03a
commit 7ec4d3874c
17 changed files with 183 additions and 171 deletions

View File

@ -69,7 +69,7 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
* the same value as <code>getValue()</code>, Double.NaN if it
* has been cleared or just instantiated.
*/
public double increment(double d);
public void increment(double d);
/**
* Returns the current state of the statistic after the

View File

@ -78,7 +78,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (n < 1) {
m1 = 0.0;
}
@ -88,7 +88,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
n0 = (double)n;
v = dev / n0;
return m1 += v;
m1 += v;
}
/**

View File

@ -72,7 +72,7 @@ public class FourthMoment extends ThirdMoment {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (n < 1) {
m4 = m3 = m2 = m1 = 0.0;
}
@ -90,8 +90,6 @@ public class FourthMoment extends ThirdMoment {
- (4.0 * v * prevM3)
+ (6.0 * v2 * prevM2)
+ ((n0 * n0) - 3 * n1) * (v2 * v2 * n1 * n0);
return m4;
}
/**

View File

@ -61,23 +61,21 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
*/
public class GeometricMean extends SumOfLogs {
private double geoMean = Double.NaN;
private int n = 0;
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
n++;
return geoMean = Math.exp( super.increment(d) / (double)n );
super.increment(d);
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/
public double getValue() {
return geoMean;
return Math.exp( super.getValue() / (double)n );
}
/**
@ -85,7 +83,6 @@ public class GeometricMean extends SumOfLogs {
*/
public void clear() {
super.clear();
geoMean = Double.NaN;
n = 0;
}

View File

@ -66,8 +66,6 @@ import org
*/
public class Kurtosis extends AbstractStorelessUnivariateStatistic {
private double kurtosis = Double.NaN;
protected FourthMoment moment = null;
protected boolean incMoment = true;
@ -84,29 +82,32 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (incMoment) {
moment.increment(d);
}
double variance =
(moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
kurtosis =
(moment.n <= 3 || variance < 10E-20)
? 0.0
: (moment.n0 * (moment.n0 + 1) * moment.m4
- 3 * moment.m2 * moment.m2 * moment.n1)
/ (moment.n1 * moment.n2 * moment.n3 * variance * variance);
return kurtosis;
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/
public double getValue() {
return kurtosis;
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) {
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);
}
/**
@ -116,7 +117,6 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
if (incMoment) {
moment.clear();
}
kurtosis = Double.NaN;
}
/*UnvariateStatistic Approach */
@ -139,44 +139,53 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
* @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 m = mean.evaluate(values, begin, length);
if (test(values, begin, length)) {
if (length <= 3) {
kurt = 0.0;
} else {
// 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] - m), 2.0);
accum2 += (values[i] - m);
// 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
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);
}
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] - m) / 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;
}
}
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] - m) / 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

@ -84,12 +84,10 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (incMoment) {
moment.increment(d);
}
return moment.m1;
}
/**

View File

@ -68,7 +68,7 @@ public class SecondMoment extends FirstMoment {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (n < 1) {
m1 = m2 = 0.0;
}
@ -80,9 +80,6 @@ public class SecondMoment extends FirstMoment {
/* increment and return m2 */
m2 += n1 * dev * v;
return m2;
}
/**

View File

@ -67,8 +67,6 @@ import org
*/
public class Skewness extends AbstractStorelessUnivariateStatistic {
private double skewness = Double.NaN;
protected ThirdMoment moment = null;
protected boolean incMoment = true;
@ -85,28 +83,29 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (incMoment) {
moment.increment(d);
}
double variance =
(moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
skewness =
(moment.n <= 2 || variance < 10E-20)
? 0.0
: (moment.n0 * moment.m3)
/ (moment.n1 * moment.n2 * Math.sqrt(variance) * variance);
return skewness;
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/
public double getValue() {
return skewness;
if (moment.n <= 0) {
return Double.NaN;
}
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);
}
/**
@ -116,7 +115,6 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
if (incMoment) {
moment.clear();
}
skewness = Double.NaN;
}
/*UnvariateStatistic Approach */
@ -141,39 +139,44 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
*/
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 m = mean.evaluate(values, begin, length);
if (test(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] - m), 2.0);
accum2 += (values[i] - m);
if (length <= 2) {
skew = 0.0;
} else {
// 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
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);
}
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] - m) / stdDev, 3.0);
}
// Get N
double n = length;
// Calculate skewness
skew = (n / ((n - 1) * (n - 2))) * accum3;
}
}
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] - m) / stdDev, 3.0);
}
// Get N
double n = length;
// Calculate skewness
skew = (n / ((n - 1) * (n - 2))) * accum3;
return skew;
}

View File

@ -59,8 +59,6 @@ package org.apache.commons.math.stat.univariate.moment;
*/
public class StandardDeviation extends Variance {
private double std = Double.NaN;
public StandardDeviation(){
super();
}
@ -72,17 +70,24 @@ public class StandardDeviation extends Variance {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
super.increment(d);
std = (variance != 0.0) ? Math.sqrt(variance) : 0.0;
return std;
public void increment(double d) {
super.increment(d);
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/
public double getValue() {
return std;
double var = super.getValue();
if(Double.isNaN(var)){
return Double.NaN;
}else if (var == 0.0){
return 0.0;
}
return Math.sqrt(var);
}
/**
@ -90,7 +95,6 @@ public class StandardDeviation extends Variance {
*/
public void clear() {
super.clear();
std = Double.NaN;
}
/**
@ -103,8 +107,13 @@ public class StandardDeviation extends Variance {
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
double tmp = super.evaluate(values, begin, length);
return tmp != 0.0 ? Math.sqrt(tmp) : 0.0;
double var = super.evaluate(values, begin, length);
if(Double.isNaN(var)){
return Double.NaN;
}
return var != 0.0 ? Math.sqrt(var) : 0.0;
}
}

View File

@ -74,7 +74,7 @@ public class ThirdMoment extends SecondMoment{
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (n < 1) {
m3 = m2 = m1 = 0.0;
}
@ -90,7 +90,6 @@ public class ThirdMoment extends SecondMoment{
m3 = m3 - (3.0 * v * prevM2) + (n0 * n1 * n2 * v2 * v);
return m3;
}
/**

View File

@ -53,46 +53,51 @@
*/
package org.apache.commons.math.stat.univariate.moment;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
/**
*
*
*/
public class Variance extends AbstractStorelessUnivariateStatistic{
protected double variance = Double.NaN;
public class Variance extends AbstractStorelessUnivariateStatistic {
protected SecondMoment moment = null;
protected boolean incMoment = true;
public Variance(){
public Variance() {
moment = new SecondMoment();
}
public Variance(SecondMoment m2){
public Variance(SecondMoment m2) {
incMoment = false;
this.moment = m2;
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (incMoment) {
moment.increment(d);
}
variance = (moment.n < 1) ? 0.0 : moment.m2 / (double)(moment.n - 1);
return variance;
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
*/
public double getValue() {
return variance;
if (moment.n <= 0) {
return Double.NaN;
} else if (moment.n <= 1) {
return 0.0;
}
return moment.m2 / (moment.n0 - 1);
}
/**
@ -102,13 +107,12 @@ public class Variance extends AbstractStorelessUnivariateStatistic{
if (incMoment) {
moment.clear();
}
variance = Double.NaN;
}
/*UnvariateStatistic Approach */
Mean mean = new Mean();
/**
* Returns the variance of the available values. This uses a corrected
* two pass algorithm of the following
@ -126,24 +130,26 @@ public class Variance extends AbstractStorelessUnivariateStatistic{
* @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 = mean.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);
if (test(values, begin, length)) {
if (length == 1) {
var = 0.0;
} else if (length > 1) {
double m = mean.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);
}
var =
(accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1);
}
return var;
}
}

View File

@ -71,8 +71,8 @@ public class Max extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
return value = Double.isNaN(value) ? d : Math.max(value, d);
public void increment(double d) {
value = Double.isNaN(value) ? d : Math.max(value, d);
}
/**

View File

@ -71,9 +71,8 @@ public class Min extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
value = Double.isNaN(value) ? d : Math.min(value, d);
return value;
}
/**

View File

@ -74,14 +74,12 @@ public class Product extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (Double.isNaN(value)) {
value = d;
} else {
value *= d;
}
return value;
}
/**

View File

@ -74,13 +74,12 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (Double.isNaN(value )) {
value = d;
} else {
value += d;
}
return value ;
}
/**

View File

@ -71,17 +71,17 @@ 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 double increment(double d) {
if (Double.isNaN(value )) {
public void increment(double d) {
if (init) {
value = Math.log(d);
init = false;
} else {
value += Math.log(d);
}
return value;
}
/**
@ -96,6 +96,7 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
*/
public void clear() {
value = Double.NaN;
init = true;
}
/**

View File

@ -74,13 +74,12 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public double increment(double d) {
public void increment(double d) {
if (Double.isNaN(value )) {
value = d * d;
} else {
value += d * d;
}
return value;
}
/**