Unused classes (in "src/test").
This commit is contained in:
parent
9cfd17601b
commit
53cb2cce5f
|
@ -1,159 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright ownership.
|
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
* (the "License"); you may not use this file except in compliance with
|
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.commons.math4.legacy.stat.descriptive;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
|
|
||||||
import org.apache.commons.math4.legacy.util.FastMath;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable {
|
|
||||||
/** Serializable version identifier */
|
|
||||||
private static final long serialVersionUID = -8837442489133392138L;
|
|
||||||
/**
|
|
||||||
* Holds a reference to a list - GENERICs are going to make
|
|
||||||
* our lives easier here as we could only accept List<Number>
|
|
||||||
*/
|
|
||||||
protected List<Double> list = new ArrayList<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a ListUnivariate with a specific List.
|
|
||||||
* @param list The list that will back this DescriptiveStatistics
|
|
||||||
*/
|
|
||||||
public ListUnivariateImpl(List<Double> list) {
|
|
||||||
this.list = list;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default constructor
|
|
||||||
*/
|
|
||||||
public ListUnivariateImpl() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public double[] getValues() {
|
|
||||||
int length = list.size();
|
|
||||||
|
|
||||||
// If the window size is not INFINITE_WINDOW AND
|
|
||||||
// the current list is larger that the window size, we need to
|
|
||||||
// take into account only the last n elements of the list
|
|
||||||
// as defined by windowSize
|
|
||||||
|
|
||||||
final int wSize = getWindowSize();
|
|
||||||
if (wSize != DescriptiveStatistics.INFINITE_WINDOW && wSize < list.size()) {
|
|
||||||
length = list.size() - FastMath.max(0, list.size() - wSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create an array to hold all values
|
|
||||||
double[] copiedArray = new double[length];
|
|
||||||
|
|
||||||
for (int i = 0; i < copiedArray.length; i++) {
|
|
||||||
copiedArray[i] = getElement(i);
|
|
||||||
}
|
|
||||||
return copiedArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public double getElement(int index) {
|
|
||||||
double value = Double.NaN;
|
|
||||||
int calcIndex = index;
|
|
||||||
|
|
||||||
final int wSize = getWindowSize();
|
|
||||||
if (wSize != DescriptiveStatistics.INFINITE_WINDOW && wSize < list.size()) {
|
|
||||||
calcIndex = (list.size() - wSize) + index;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
value = list.get(calcIndex);
|
|
||||||
} catch (MathIllegalArgumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public long getN() {
|
|
||||||
int n = 0;
|
|
||||||
|
|
||||||
final int wSize = getWindowSize();
|
|
||||||
if (wSize != DescriptiveStatistics.INFINITE_WINDOW) {
|
|
||||||
if (list.size() > wSize) {
|
|
||||||
n = wSize;
|
|
||||||
} else {
|
|
||||||
n = list.size();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
n = list.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public void addValue(double v) {
|
|
||||||
list.add(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears all statistics.
|
|
||||||
* <p>
|
|
||||||
* <strong>N.B.: </strong> This method has the side effect of clearing the underlying list.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
list.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply the given statistic to this univariate collection.
|
|
||||||
* @param stat the statistic to apply
|
|
||||||
* @return the computed value of the statistic.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public double apply(UnivariateStatistic stat) {
|
|
||||||
final double[] v = this.getValues();
|
|
||||||
|
|
||||||
if (v != null) {
|
|
||||||
return stat.evaluate(v, 0, v.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Double.NaN;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public void setWindowSize(int windowSize) {
|
|
||||||
super.setWindowSize(windowSize);
|
|
||||||
// Discard elements from the front of the list if "windowSize"
|
|
||||||
// is less than the size of the list.
|
|
||||||
final int extra = list.size() - windowSize;
|
|
||||||
if (extra > 0) {
|
|
||||||
list.subList(0, extra).clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,157 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright ownership.
|
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
* (the "License"); you may not use this file except in compliance with
|
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.commons.math4.legacy.stat.descriptive;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.commons.math4.legacy.TestUtils;
|
|
||||||
import org.apache.commons.math4.legacy.util.FastMath;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test cases for the {@link ListUnivariateImpl} class.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
public final class ListUnivariateImplTest {
|
|
||||||
|
|
||||||
private double one = 1;
|
|
||||||
private float two = 2;
|
|
||||||
private int three = 3;
|
|
||||||
|
|
||||||
private double mean = 2;
|
|
||||||
private double sumSq = 18;
|
|
||||||
private double sum = 8;
|
|
||||||
private double var = 0.666666666666666666667;
|
|
||||||
private double std = FastMath.sqrt(var);
|
|
||||||
private double n = 4;
|
|
||||||
private double min = 1;
|
|
||||||
private double max = 3;
|
|
||||||
private double tolerance = 10E-15;
|
|
||||||
|
|
||||||
/** test stats */
|
|
||||||
@Test
|
|
||||||
public void testStats() {
|
|
||||||
List<Double> externalList = new ArrayList<>();
|
|
||||||
|
|
||||||
DescriptiveStatistics u = new ListUnivariateImpl( externalList );
|
|
||||||
|
|
||||||
Assert.assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
u.addValue(one);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(three);
|
|
||||||
Assert.assertEquals("N",n,u.getN(),tolerance);
|
|
||||||
Assert.assertEquals("sum",sum,u.getSum(),tolerance);
|
|
||||||
Assert.assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
|
|
||||||
Assert.assertEquals("var",var,u.getVariance(),tolerance);
|
|
||||||
Assert.assertEquals("std",std,u.getStandardDeviation(),tolerance);
|
|
||||||
Assert.assertEquals("mean",mean,u.getMean(),tolerance);
|
|
||||||
Assert.assertEquals("min",min,u.getMin(),tolerance);
|
|
||||||
Assert.assertEquals("max",max,u.getMax(),tolerance);
|
|
||||||
u.clear();
|
|
||||||
Assert.assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testN0andN1Conditions() {
|
|
||||||
List<Double> list = new ArrayList<>();
|
|
||||||
|
|
||||||
DescriptiveStatistics u = new ListUnivariateImpl(list);
|
|
||||||
|
|
||||||
Assert.assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
|
|
||||||
Assert.assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
|
|
||||||
Assert.assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
|
|
||||||
|
|
||||||
list.add( Double.valueOf(one));
|
|
||||||
|
|
||||||
Assert.assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
|
|
||||||
Assert.assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
|
|
||||||
Assert.assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSkewAndKurtosis() {
|
|
||||||
DescriptiveStatistics u = new DescriptiveStatistics();
|
|
||||||
|
|
||||||
double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
|
|
||||||
9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
|
|
||||||
for( int i = 0; i < testArray.length; i++) {
|
|
||||||
u.addValue( testArray[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.assertEquals("mean", 12.40455, u.getMean(), 0.0001);
|
|
||||||
Assert.assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
|
|
||||||
Assert.assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
|
|
||||||
Assert.assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testProductAndGeometricMean() {
|
|
||||||
ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList<>());
|
|
||||||
u.setWindowSize(10);
|
|
||||||
|
|
||||||
u.addValue( 1.0 );
|
|
||||||
u.addValue( 2.0 );
|
|
||||||
u.addValue( 3.0 );
|
|
||||||
u.addValue( 4.0 );
|
|
||||||
|
|
||||||
Assert.assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
|
|
||||||
|
|
||||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
|
||||||
// of a discarded element
|
|
||||||
for( int i = 0; i < 10; i++ ) {
|
|
||||||
u.addValue( i + 2 );
|
|
||||||
}
|
|
||||||
// Values should be (2,3,4,5,6,7,8,9,10,11)
|
|
||||||
|
|
||||||
Assert.assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/** test stats */
|
|
||||||
@Test
|
|
||||||
public void testSerialization() {
|
|
||||||
|
|
||||||
DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList<>());
|
|
||||||
|
|
||||||
Assert.assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
u.addValue(one);
|
|
||||||
u.addValue(two);
|
|
||||||
|
|
||||||
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
|
|
||||||
|
|
||||||
u2.addValue(two);
|
|
||||||
u2.addValue(three);
|
|
||||||
|
|
||||||
Assert.assertEquals("N",n,u2.getN(),tolerance);
|
|
||||||
Assert.assertEquals("sum",sum,u2.getSum(),tolerance);
|
|
||||||
Assert.assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
|
|
||||||
Assert.assertEquals("var",var,u2.getVariance(),tolerance);
|
|
||||||
Assert.assertEquals("std",std,u2.getStandardDeviation(),tolerance);
|
|
||||||
Assert.assertEquals("mean",mean,u2.getMean(),tolerance);
|
|
||||||
Assert.assertEquals("min",min,u2.getMin(),tolerance);
|
|
||||||
Assert.assertEquals("max",max,u2.getMax(),tolerance);
|
|
||||||
|
|
||||||
u2.clear();
|
|
||||||
Assert.assertEquals("total count",0,u2.getN(),tolerance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,167 +0,0 @@
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright ownership.
|
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
* (the "License"); you may not use this file except in compliance with
|
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.commons.math4.legacy.stat.descriptive;
|
|
||||||
|
|
||||||
import org.apache.commons.math4.legacy.util.FastMath;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test cases for the {@link ListUnivariateImpl} class.
|
|
||||||
*/
|
|
||||||
public final class MixedListUnivariateImplTest {
|
|
||||||
private final double one = 1;
|
|
||||||
private final float two = 2;
|
|
||||||
private final int three = 3;
|
|
||||||
|
|
||||||
private final double mean = 2;
|
|
||||||
private final double sumSq = 18;
|
|
||||||
private final double sum = 8;
|
|
||||||
private final double var = 0.666666666666666666667;
|
|
||||||
private final double std = FastMath.sqrt(var);
|
|
||||||
private final double n = 4;
|
|
||||||
private final double min = 1;
|
|
||||||
private final double max = 3;
|
|
||||||
private final double tolerance = 10E-15;
|
|
||||||
|
|
||||||
|
|
||||||
public MixedListUnivariateImplTest() {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/** test stats */
|
|
||||||
@Test
|
|
||||||
public void testStats() {
|
|
||||||
List<Double> externalList = new ArrayList<>();
|
|
||||||
|
|
||||||
DescriptiveStatistics u = new ListUnivariateImpl(externalList);
|
|
||||||
|
|
||||||
Assert.assertEquals("total count", 0, u.getN(), tolerance);
|
|
||||||
u.addValue(one);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(three);
|
|
||||||
Assert.assertEquals("N", n, u.getN(), tolerance);
|
|
||||||
Assert.assertEquals("sum", sum, u.getSum(), tolerance);
|
|
||||||
Assert.assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);
|
|
||||||
Assert.assertEquals("var", var, u.getVariance(), tolerance);
|
|
||||||
Assert.assertEquals("std", std, u.getStandardDeviation(), tolerance);
|
|
||||||
Assert.assertEquals("mean", mean, u.getMean(), tolerance);
|
|
||||||
Assert.assertEquals("min", min, u.getMin(), tolerance);
|
|
||||||
Assert.assertEquals("max", max, u.getMax(), tolerance);
|
|
||||||
u.clear();
|
|
||||||
Assert.assertEquals("total count", 0, u.getN(), tolerance);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testN0andN1Conditions() {
|
|
||||||
DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList<>());
|
|
||||||
|
|
||||||
Assert.assertTrue(
|
|
||||||
"Mean of n = 0 set should be NaN",
|
|
||||||
Double.isNaN(u.getMean()));
|
|
||||||
Assert.assertTrue(
|
|
||||||
"Standard Deviation of n = 0 set should be NaN",
|
|
||||||
Double.isNaN(u.getStandardDeviation()));
|
|
||||||
Assert.assertTrue(
|
|
||||||
"Variance of n = 0 set should be NaN",
|
|
||||||
Double.isNaN(u.getVariance()));
|
|
||||||
|
|
||||||
u.addValue(one);
|
|
||||||
|
|
||||||
Assert.assertTrue(
|
|
||||||
"Mean of n = 1 set should be value of single item n1, instead it is " + u.getMean() ,
|
|
||||||
u.getMean() == one);
|
|
||||||
|
|
||||||
Assert.assertTrue(
|
|
||||||
"StdDev of n = 1 set should be zero, instead it is: "
|
|
||||||
+ u.getStandardDeviation(),
|
|
||||||
u.getStandardDeviation() == 0);
|
|
||||||
Assert.assertTrue(
|
|
||||||
"Variance of n = 1 set should be zero",
|
|
||||||
u.getVariance() == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSkewAndKurtosis() {
|
|
||||||
ListUnivariateImpl u =
|
|
||||||
new ListUnivariateImpl(new ArrayList<>());
|
|
||||||
|
|
||||||
u.addValue(12.5);
|
|
||||||
u.addValue(12);
|
|
||||||
u.addValue(11.8);
|
|
||||||
u.addValue(14.2);
|
|
||||||
u.addValue(14.5);
|
|
||||||
u.addValue(14.9);
|
|
||||||
u.addValue(12.0);
|
|
||||||
u.addValue(21);
|
|
||||||
u.addValue(8.2);
|
|
||||||
u.addValue(10.3);
|
|
||||||
u.addValue(11.3);
|
|
||||||
u.addValue(14.1f);
|
|
||||||
u.addValue(9.9);
|
|
||||||
u.addValue(12.2);
|
|
||||||
u.addValue(12.1);
|
|
||||||
u.addValue(11);
|
|
||||||
u.addValue(19.8);
|
|
||||||
u.addValue(11);
|
|
||||||
u.addValue(10);
|
|
||||||
u.addValue(8.8);
|
|
||||||
u.addValue(9);
|
|
||||||
u.addValue(12.3);
|
|
||||||
|
|
||||||
|
|
||||||
Assert.assertEquals("mean", 12.40455, u.getMean(), 0.0001);
|
|
||||||
Assert.assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
|
|
||||||
Assert.assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
|
|
||||||
Assert.assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testProductAndGeometricMean() {
|
|
||||||
ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList<>());
|
|
||||||
u.setWindowSize(10);
|
|
||||||
|
|
||||||
u.addValue(1.0);
|
|
||||||
u.addValue(2.0);
|
|
||||||
u.addValue(3.0);
|
|
||||||
u.addValue(4.0);
|
|
||||||
|
|
||||||
Assert.assertEquals(
|
|
||||||
"Geometric mean not expected",
|
|
||||||
2.213364,
|
|
||||||
u.getGeometricMean(),
|
|
||||||
0.00001);
|
|
||||||
|
|
||||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
|
||||||
// of a discarded element
|
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
u.addValue(i + 2);
|
|
||||||
}
|
|
||||||
// Values should be (2,3,4,5,6,7,8,9,10,11)
|
|
||||||
Assert.assertEquals(
|
|
||||||
"Geometric mean not expected",
|
|
||||||
5.755931,
|
|
||||||
u.getGeometricMean(),
|
|
||||||
0.00001);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue