Changed evaluate method to use embedded moments instead of duplicating code.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141360 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-07-04 22:06:07 +00:00
parent 0f3a32dd90
commit cecda770cb
1 changed files with 9 additions and 21 deletions

View File

@ -36,7 +36,7 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
* one of the threads invokes the <code>increment()</code> or
* <code>clear()</code> method, it must be synchronized externally.
*
* @version $Revision: 1.25 $ $Date: 2004/07/04 09:02:36 $
* @version $Revision: 1.26 $ $Date: 2004/07/04 22:06:07 $
*/
public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Serializable {
@ -145,30 +145,19 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Se
// Initialize the kurtosis
double kurt = Double.NaN;
if (test(values, begin, length) && length > 3) {
Mean mean = new Mean();
// Get the mean and the standard deviation
double m = mean.evaluate(values, begin, length);
if (test(values, begin, length) && length > 3) {
// 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));
// Compute the mean and standard deviation
Variance variance = new Variance();
variance.incrementAll(values, begin, length);
double mean = variance.moment.m1;
double stdDev = Math.sqrt(variance.getResult());
// 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), 4.0);
accum3 += Math.pow((values[i] - mean), 4.0);
}
accum3 /= Math.pow(stdDev, 4.0d);
@ -182,8 +171,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Se
// Calculate kurtosis
kurt = (coefficientOne * accum3) - termTwo;
}
}
return kurt;
}