Removing Product from Univariate Interface, applying sumLog changes to geomean in AbstractStoreUnivariate.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140941 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8936d6e3a6
commit
9b2b61b777
|
@ -114,8 +114,7 @@ public abstract class AbstractStoreUnivariate implements StoreUnivariate {
|
|||
double n = getN();
|
||||
|
||||
double coefficientOne = (n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
|
||||
double termTwo = ((3 * Math.pow(n - 1, 2.0))
|
||||
/ ((n - 2) * (n - 3)));
|
||||
double termTwo = ((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3)));
|
||||
// Calculate kurtosis
|
||||
kurtosis = (coefficientOne * accum) - termTwo;
|
||||
|
||||
|
@ -156,26 +155,19 @@ public abstract class AbstractStoreUnivariate implements StoreUnivariate {
|
|||
* @see org.apache.commons.math.stat.Univariate#getGeometricMean()
|
||||
*/
|
||||
public double getGeometricMean() {
|
||||
double gMean = Math.pow(getProduct(),(1.0/getN()));
|
||||
double gMean = Double.NaN;
|
||||
|
||||
if (getN() > 0) {
|
||||
double sumLog = 0.0;
|
||||
for (int i = 0; i < getN(); i++) {
|
||||
sumLog += Math.log(getElement(i));
|
||||
}
|
||||
gMean = Math.exp(sumLog / (double)getN() );
|
||||
}
|
||||
|
||||
return gMean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product for this collection of values
|
||||
* @see org.apache.commons.math.stat.Univariate#getProduct()
|
||||
*/
|
||||
public double getProduct() {
|
||||
double product = Double.NaN;
|
||||
if( getN() > 0 ) {
|
||||
product = 1.0;
|
||||
for( int i = 0; i < getN(); i++) {
|
||||
product *= getElement(i);
|
||||
}
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the variance for this collection of values
|
||||
* @see org.apache.commons.math.stat.Univariate#getVariance()
|
||||
|
@ -193,8 +185,8 @@ public abstract class AbstractStoreUnivariate implements StoreUnivariate {
|
|||
|
||||
// Calculate the sum of the squares of the distance between each
|
||||
// value and the mean
|
||||
double accum = 0.0;
|
||||
for (int i = 0; i < getN(); i++){
|
||||
double accum = 0.0;
|
||||
for (int i = 0; i < getN(); i++) {
|
||||
accum += Math.pow((getElement(i) - mean), 2.0);
|
||||
}
|
||||
|
||||
|
@ -283,22 +275,22 @@ public abstract class AbstractStoreUnivariate implements StoreUnivariate {
|
|||
}
|
||||
return accum;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getSortedValues()
|
||||
*
|
||||
*/
|
||||
*/
|
||||
public double[] getSortedValues() {
|
||||
double[] values = getValues();
|
||||
Arrays.sort(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an estimate for the pth percentile of the stored values
|
||||
* @see org.apache.commons.math.stat.StoreUnivariate#getPercentile(double)
|
||||
*/
|
||||
public double getPercentile(double p) {
|
||||
public double getPercentile(double p) {
|
||||
if ((p > 100) || (p <= 0)) {
|
||||
throw new IllegalArgumentException("invalid percentile value");
|
||||
}
|
||||
|
@ -307,7 +299,7 @@ public abstract class AbstractStoreUnivariate implements StoreUnivariate {
|
|||
return Double.NaN;
|
||||
}
|
||||
if (n == 1) {
|
||||
return getElement(0); // always return single value for n = 1
|
||||
return getElement(0); // always return single value for n = 1
|
||||
}
|
||||
double pos = p * (n + 1) / 100;
|
||||
double fpos = Math.floor(pos);
|
||||
|
@ -322,7 +314,7 @@ public abstract class AbstractStoreUnivariate implements StoreUnivariate {
|
|||
}
|
||||
double lower = sorted[intPos - 1];
|
||||
double upper = sorted[intPos];
|
||||
return lower + d * (upper - lower);
|
||||
return lower + d * (upper - lower);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@
|
|||
* @author Phil Steitz
|
||||
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
* @author <a href="mailto:mdiggory@apache.org">Mark Diggory</a>
|
||||
* @version $Revision: 1.3 $ $Date: 2003/06/16 21:24:30 $
|
||||
* @version $Revision: 1.4 $ $Date: 2003/06/21 23:38:27 $
|
||||
*
|
||||
*/
|
||||
public interface Univariate {
|
||||
|
@ -99,12 +99,6 @@ public interface Univariate {
|
|||
*/
|
||||
abstract double getGeometricMean();
|
||||
|
||||
/**
|
||||
* Returns the product of the available values
|
||||
* @return The product or Double.NaN if no values have been added.
|
||||
*/
|
||||
abstract double getProduct();
|
||||
|
||||
/**
|
||||
* Returns the variance of the available values.
|
||||
* @return The variance, Double.NaN if no values have been added
|
||||
|
|
|
@ -71,7 +71,7 @@ import org.apache.commons.math.FixedDoubleArray;
|
|||
* @author <a href="mailto:mdiggory@apache.org">Mark Diggory</a>
|
||||
* @author Brent Worden
|
||||
* @author <a href="mailto:HotFusionMan@Yahoo.com">Albert Davidson Chou</a>
|
||||
* @version $Revision: 1.13 $ $Date: 2003/06/21 02:54:55 $
|
||||
* @version $Revision: 1.14 $ $Date: 2003/06/21 23:38:27 $
|
||||
*
|
||||
*/
|
||||
public class UnivariateImpl implements Univariate, Serializable {
|
||||
|
@ -249,17 +249,6 @@ public class UnivariateImpl implements Univariate, Serializable {
|
|||
return min;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.Univariate#getProduct()
|
||||
*/
|
||||
public double getProduct() {
|
||||
if (windowSize != Univariate.INFINITE_WINDOW) {
|
||||
return StatUtils.product(doubleArray.getElements());
|
||||
}
|
||||
|
||||
return sumLog;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.stat.Univariate#getGeometricMean()
|
||||
*/
|
||||
|
|
|
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
|
|||
* Test cases for the {@link Univariate} class.
|
||||
*
|
||||
* @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
|
||||
* @version $Revision: 1.2 $ $Date: 2003/06/21 23:02:51 $
|
||||
* @version $Revision: 1.3 $ $Date: 2003/06/21 23:38:27 $
|
||||
*/
|
||||
|
||||
public final class ListUnivariateImplTest extends TestCase {
|
||||
|
@ -161,7 +161,6 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
u.addValue( 3.0 );
|
||||
u.addValue( 4.0 );
|
||||
|
||||
//assertEquals( "Product not expected", 24.0, u.getProduct(), Double.MIN_VALUE );
|
||||
assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
|
||||
|
||||
// Now test rolling - UnivariateImpl should discount the contribution
|
||||
|
@ -171,7 +170,6 @@ public final class ListUnivariateImplTest extends TestCase {
|
|||
}
|
||||
// Values should be (2,3,4,5,6,7,8,9,10,11)
|
||||
|
||||
//assertEquals( "Product not expected", 39916800.0, u.getProduct(), 0.00001 );
|
||||
assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
|
||||
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ import junit.framework.TestSuite;
|
|||
*
|
||||
* @author Phil Steitz
|
||||
* @author Tim Obrien
|
||||
* @version $Revision: 1.2 $ $Date: 2003/06/21 02:08:23 $
|
||||
* @version $Revision: 1.3 $ $Date: 2003/06/21 23:38:27 $
|
||||
*/
|
||||
|
||||
public final class UnivariateImplTest extends TestCase {
|
||||
|
@ -175,8 +175,6 @@ public final class UnivariateImplTest extends TestCase {
|
|||
u.addValue( 3.0 );
|
||||
u.addValue( 4.0 );
|
||||
|
||||
assertEquals( "Product not expected", 24.0, u.getProduct(),
|
||||
Double.MIN_VALUE );
|
||||
assertEquals( "Geometric mean not expected", 2.213364,
|
||||
u.getGeometricMean(), 0.00001 );
|
||||
|
||||
|
@ -187,8 +185,6 @@ public final class UnivariateImplTest extends TestCase {
|
|||
}
|
||||
// Values should be (2,3,4,5,6,7,8,9,10,11)
|
||||
|
||||
assertEquals( "Product not expected", 39916800.0,
|
||||
u.getProduct(), 0.00001 );
|
||||
assertEquals( "Geometric mean not expected", 5.755931,
|
||||
u.getGeometricMean(), 0.00001 );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue