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:
parent
b6871d607e
commit
faa703e40e
|
@ -65,57 +65,18 @@ public abstract class AbstractStorelessUnivariateStatistic
|
|||
extends AbstractUnivariateStatistic
|
||||
implements StorelessUnivariateStatistic {
|
||||
|
||||
protected boolean clearOnEval = true;
|
||||
|
||||
protected boolean init = true;
|
||||
|
||||
/**
|
||||
* This implements the AbstractUnivariateStatistic impl to funnel
|
||||
* calculation off to the instantanious increment method.
|
||||
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
|
||||
*/
|
||||
public double evaluate(double[] d, int start, int length) {
|
||||
|
||||
if (clearOnEval)
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
if (this.test(values, begin, length)) {
|
||||
this.clear();
|
||||
|
||||
for (int i = start; i < start + length; i++) {
|
||||
increment(d[i]);
|
||||
for (int i = begin; i < begin + length; i++) {
|
||||
increment(values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -69,15 +69,15 @@ public abstract class AbstractUnivariateStatistic
|
|||
*
|
||||
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[])
|
||||
*/
|
||||
public double evaluate(double[] d) {
|
||||
return evaluate(d, 0, d.length);
|
||||
public double evaluate(double[] values) {
|
||||
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)
|
||||
*/
|
||||
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
|
||||
|
@ -86,7 +86,7 @@ public abstract class AbstractUnivariateStatistic
|
|||
* @param begin 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)
|
||||
throw new IllegalArgumentException("length > values.length");
|
||||
|
@ -97,5 +97,10 @@ public abstract class AbstractUnivariateStatistic
|
|||
if (values == null)
|
||||
throw new IllegalArgumentException("input value array is null");
|
||||
|
||||
if (values.length == 0 || length == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
|
@ -79,6 +79,7 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
|
|||
*/
|
||||
public double getValue();
|
||||
|
||||
|
||||
/**
|
||||
* Clears all the internal state of the Statistic
|
||||
*/
|
||||
|
@ -90,7 +91,7 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
|
|||
* <code>evaluate(double[])</code> methods.
|
||||
* @return the state
|
||||
*/
|
||||
public boolean isClearOnEval();
|
||||
//public boolean isClearOnEval();
|
||||
|
||||
/**
|
||||
* Sets the behavior of this statistic when evaluating
|
||||
|
@ -100,6 +101,6 @@ public interface StorelessUnivariateStatistic extends UnivariateStatistic {
|
|||
* incrimented.
|
||||
* @param b true | false
|
||||
*/
|
||||
public void setClearOnEval(boolean b);
|
||||
//public void setClearOnEval(boolean b);
|
||||
|
||||
}
|
|
@ -78,6 +78,6 @@ public interface UnivariateStatistic {
|
|||
* @return the result of the evaluation or Double.NaN
|
||||
* if the array is empty
|
||||
*/
|
||||
public double evaluate(double[] d, int begin, int length);
|
||||
public double evaluate(double[] values, int begin, int length);
|
||||
|
||||
}
|
|
@ -53,31 +53,11 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.univariate.moment;
|
||||
|
||||
import org
|
||||
.apache
|
||||
.commons
|
||||
.math
|
||||
.stat
|
||||
.univariate
|
||||
.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @author Mark Diggory
|
||||
*
|
||||
*/
|
||||
public class FourthMoment extends AbstractStorelessUnivariateStatistic {
|
||||
|
||||
/** 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;
|
||||
public class FourthMoment extends ThirdMoment {
|
||||
|
||||
/** fourth moment of values that have been added */
|
||||
protected double m4 = Double.NaN;
|
||||
|
@ -122,11 +102,11 @@ public class FourthMoment extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
n = 0;
|
||||
m1 = m2 = m3 = m4 = Double.NaN;
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
m4 = Double.NaN;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,40 +53,54 @@
|
|||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* @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)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
n++;
|
||||
super.increment(d);
|
||||
geomean = Math.exp( sumLog / (double)n );
|
||||
return geomean;
|
||||
sumLog.increment(d);
|
||||
value = Math.exp( sumLog.increment(d) / (double)n );
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
return geomean;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
geomean = Double.NaN;
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
value = Double.NaN;
|
||||
sumLog.clear();
|
||||
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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -58,7 +58,7 @@ package org.apache.commons.math.stat.univariate.moment;
|
|||
*/
|
||||
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)
|
||||
|
@ -85,11 +85,61 @@ public class Kurtosis extends FourthMoment {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
super.internalClear();
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -54,32 +54,43 @@
|
|||
package org.apache.commons.math.stat.univariate.moment;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
|
||||
import org.apache.commons.math.stat.univariate.summary.Sum;
|
||||
|
||||
/**
|
||||
* @author Mark Diggory
|
||||
*/
|
||||
public class Mean extends AbstractStorelessUnivariateStatistic {
|
||||
|
||||
|
||||
/** count of values that have been added */
|
||||
private int n = 0;
|
||||
protected int n = 0;
|
||||
|
||||
/** 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)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
if (n < 1) {
|
||||
m1 = 0.0;
|
||||
m1 = 0.0;
|
||||
}
|
||||
|
||||
n++;
|
||||
|
||||
m1 += (d - m1) / ((double) n);
|
||||
|
||||
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()
|
||||
*/
|
||||
|
@ -88,11 +99,12 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
m1 = Double.NaN;
|
||||
n = 0;
|
||||
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
|
||||
*/
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
if(test(values,begin,length))
|
||||
return sum.evaluate(values, begin, length) / ((double) length);
|
||||
return Double.NaN;
|
||||
}
|
||||
|
||||
}
|
|
@ -53,19 +53,19 @@
|
|||
*/
|
||||
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
|
||||
*
|
||||
*/
|
||||
public class SecondMoment extends AbstractStorelessUnivariateStatistic {
|
||||
|
||||
/** count of values that have been added */
|
||||
protected int n = 0;
|
||||
|
||||
/** first moment of values that have been added */
|
||||
protected double m1 = Double.NaN;
|
||||
public class SecondMoment extends Mean {
|
||||
|
||||
/** second moment of values that have been added */
|
||||
protected double m2 = Double.NaN;
|
||||
|
@ -75,19 +75,28 @@ public class SecondMoment extends AbstractStorelessUnivariateStatistic {
|
|||
*/
|
||||
public double increment(double d) {
|
||||
if (n < 1) {
|
||||
m2 = m1 = 0.0;
|
||||
m1 = m2 = 0.0;
|
||||
}
|
||||
|
||||
n++;
|
||||
|
||||
double dev = d - m1;
|
||||
double v = dev / ((double) n);
|
||||
|
||||
m2 += ((double)(n - 1)) * dev * v;
|
||||
m1 += v;
|
||||
m2 += (n - 1) * dev * v;
|
||||
|
||||
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()
|
||||
*/
|
||||
|
@ -95,11 +104,4 @@ public class SecondMoment extends AbstractStorelessUnivariateStatistic {
|
|||
return m2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
m2 = Double.NaN;
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ package org.apache.commons.math.stat.univariate.moment;
|
|||
*/
|
||||
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)
|
||||
|
@ -87,11 +87,58 @@ public class Skewness extends ThirdMoment {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
super.internalClear();
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,35 +53,48 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.univariate.moment;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @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)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
std = Math.sqrt(super.increment(d));
|
||||
return std;
|
||||
var.increment(d);
|
||||
value = Math.sqrt(var.getValue());
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
return std;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
super.internalClear();
|
||||
std = Double.NaN;
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
var.clear();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -53,22 +53,11 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.univariate.moment;
|
||||
|
||||
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @author Mark Diggory
|
||||
*
|
||||
*/
|
||||
public class ThirdMoment extends AbstractStorelessUnivariateStatistic{
|
||||
|
||||
/** 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;
|
||||
public class ThirdMoment extends SecondMoment{
|
||||
|
||||
/** third moment of values that have been added */
|
||||
protected double m3 = Double.NaN;
|
||||
|
@ -82,6 +71,7 @@ public class ThirdMoment extends AbstractStorelessUnivariateStatistic{
|
|||
}
|
||||
|
||||
n++;
|
||||
|
||||
double dev = d - m1;
|
||||
double v = dev / ((double) n);
|
||||
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() {
|
||||
n = 0;
|
||||
m1 = m2 = m3 = Double.NaN;
|
||||
public void clear() {
|
||||
super.clear();
|
||||
m3 = Double.NaN;
|
||||
}
|
||||
|
||||
}
|
|
@ -53,20 +53,30 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.univariate.moment;
|
||||
|
||||
import org
|
||||
.apache
|
||||
.commons
|
||||
.math
|
||||
.stat
|
||||
.univariate
|
||||
.AbstractStorelessUnivariateStatistic;
|
||||
|
||||
/**
|
||||
* @author Mark Diggory
|
||||
*
|
||||
*/
|
||||
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)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
super.increment(d);
|
||||
variance = (n <= 1) ? 0.0 : m2 / (double) (n - 1);
|
||||
|
||||
variance = (n < 1) ? 0.0 : m2 / (double)(n - 1);
|
||||
|
||||
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() {
|
||||
super.internalClear();
|
||||
public void clear() {
|
||||
super.clear();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -53,14 +53,20 @@
|
|||
*/
|
||||
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
|
||||
*/
|
||||
public class Max extends AbstractStorelessUnivariateStatistic {
|
||||
|
||||
double value = Double.NaN;
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
public void clear() {
|
||||
value = Double.NaN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
|
@ -76,10 +89,17 @@ public class Max extends AbstractStorelessUnivariateStatistic {
|
|||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic#internalClear()
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
|
||||
*/
|
||||
protected void internalClear() {
|
||||
value = Double.NaN;
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -53,20 +53,34 @@
|
|||
*/
|
||||
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
|
||||
*/
|
||||
public class Min extends AbstractStorelessUnivariateStatistic {
|
||||
|
||||
double value = Double.NaN;
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
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() {
|
||||
value = Double.NaN;
|
||||
public double evaluate(double[] values, int begin, int length) {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -86,36 +86,36 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
* Evaluates the double[] top the specified percentile.
|
||||
* This does not alter the interal percentile state of the
|
||||
* 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.
|
||||
* @return the result of the evaluation or Double.NaN
|
||||
* if the array is empty
|
||||
*/
|
||||
public double evaluate(double[] d, double p) {
|
||||
return evaluate(d, 0,d.length, p);
|
||||
public double evaluate(double[] values, 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[] d, int start, int length) {
|
||||
return evaluate(d, start, length, percentile);
|
||||
public double evaluate(double[] values, int start, 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
|
||||
* 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 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
|
||||
* 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)) {
|
||||
throw new IllegalArgumentException("invalid percentile value");
|
||||
|
@ -125,14 +125,14 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
return Double.NaN;
|
||||
}
|
||||
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 fpos = Math.floor(pos);
|
||||
int intPos = (int) fpos;
|
||||
double dif = pos - fpos;
|
||||
double[] sorted = new double[length];
|
||||
System.arraycopy(d, start,sorted, 0, length);
|
||||
System.arraycopy(values, begin,sorted, 0, length);
|
||||
Arrays.sort(sorted);
|
||||
|
||||
if (pos < 1) {
|
||||
|
@ -158,7 +158,7 @@ public class Percentile extends AbstractUnivariateStatistic {
|
|||
/**
|
||||
* The default internal state of this percentile can be set.
|
||||
* 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) {
|
||||
percentile = p;
|
||||
|
|
|
@ -69,33 +69,47 @@ public class Product extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* The current Running Product.
|
||||
*/
|
||||
double product = Double.NaN;
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
if (init) {
|
||||
init = false;
|
||||
product = d;
|
||||
if (Double.isNaN(value)) {
|
||||
value = d;
|
||||
} else {
|
||||
product *= d;
|
||||
value *= d;
|
||||
}
|
||||
|
||||
return product;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#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() {
|
||||
product = Double.NaN;
|
||||
public void clear() {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -70,33 +70,48 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* The currently running sum.
|
||||
*/
|
||||
protected double sum = Double.NaN;
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
if (init) {
|
||||
init = false;
|
||||
sum = d;
|
||||
if (Double.isNaN(value )) {
|
||||
value = d;
|
||||
} else {
|
||||
sum += d;
|
||||
value += d;
|
||||
}
|
||||
return sum;
|
||||
return value ;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#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() {
|
||||
sum = Double.NaN;
|
||||
public void clear() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -68,35 +68,48 @@ import org
|
|||
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)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
if (init) {
|
||||
sumLog = Math.log(d);
|
||||
init = false;
|
||||
if (Double.isNaN(value )) {
|
||||
value = Math.log(d);
|
||||
} else {
|
||||
sumLog += Math.log(d);
|
||||
value += Math.log(d);
|
||||
}
|
||||
|
||||
return sumLog;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
return sumLog;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
sumLog = Double.NaN;
|
||||
public void clear() {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -70,33 +70,47 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
|
|||
/**
|
||||
* The currently running sumSq
|
||||
*/
|
||||
protected double sumSq = Double.NaN;
|
||||
private double value = Double.NaN;
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
|
||||
*/
|
||||
public double increment(double d) {
|
||||
if (init) {
|
||||
init = false;
|
||||
sumSq = d * d;
|
||||
if (Double.isNaN(value )) {
|
||||
value = d * d;
|
||||
} else {
|
||||
sumSq += d * d;
|
||||
value += d * d;
|
||||
}
|
||||
return sumSq;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
|
||||
*/
|
||||
public double getValue() {
|
||||
return sumSq;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
|
||||
*/
|
||||
protected void internalClear() {
|
||||
sumSq = Double.NaN;
|
||||
public void clear() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue