MATH-1426: Add constructor that takes a "Double[]" argument.

Thanks to Karl Richter.
This commit is contained in:
Gilles 2017-08-09 14:32:56 +02:00
parent 496934effc
commit bd82fe9fc0
4 changed files with 116 additions and 0 deletions

View File

@ -299,6 +299,9 @@
<contributor> <contributor>
<name>S&#233;bastien Riou</name> <name>S&#233;bastien Riou</name>
</contributor> </contributor>
<contributor>
<name>Karl Richter</name>
</contributor>
<contributor> <contributor>
<name>Benedikt Ritter</name> <name>Benedikt Ritter</name>
</contributor> </contributor>

View File

@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</release> </release>
<release version="4.0" date="XXXX-XX-XX" description=""> <release version="4.0" date="XXXX-XX-XX" description="">
<action dev="erans" type="add" issue="MATH-1426" due-to="Karl Richter">
"DescriptiveStatistics": allow "Double[]" as initializer.
</action>
<action dev="erans" type="fix" issue="MATH-1419" due-to="Hang Park"> <action dev="erans" type="fix" issue="MATH-1419" due-to="Hang Park">
"PolynomialSplineFunction": incorrect usage of exception. "PolynomialSplineFunction": incorrect usage of exception.
</action> </action>

View File

@ -142,6 +142,23 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable {
} }
} }
/**
* Construct a DescriptiveStatistics instance with an infinite window
* and the initial data values in {@code initialDoubleArray}.
* If {@code initialDoubleArray} is {@code null}, then this constructor
* corresponds to {@link #DescriptiveStatistics() }.
*
* @param initialDoubleArray the initial Double[].
*/
public DescriptiveStatistics(Double[] initialDoubleArray) {
if (initialDoubleArray != null) {
eDA = new ResizableDoubleArray(initialDoubleArray.length);
for(double initialValue : initialDoubleArray) {
eDA.addElement(initialValue);
}
}
}
/** /**
* Copy constructor. Construct a new {@code DescriptiveStatistics} instance * Copy constructor. Construct a new {@code DescriptiveStatistics} instance
* that is a copy of {@code original}. * that is a copy of {@code original}.

View File

@ -17,7 +17,9 @@ import java.util.Locale;
import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math4.stat.descriptive.DescriptiveStatistics;
import static org.apache.commons.math4.stat.descriptive.DescriptiveStatistics.copy;
import org.apache.commons.math4.stat.descriptive.SummaryStatistics; import org.apache.commons.math4.stat.descriptive.SummaryStatistics;
import org.apache.commons.math4.stat.descriptive.UnivariateStatistic; import org.apache.commons.math4.stat.descriptive.UnivariateStatistic;
import org.apache.commons.math4.stat.descriptive.moment.GeometricMean; import org.apache.commons.math4.stat.descriptive.moment.GeometricMean;
@ -28,14 +30,19 @@ import org.apache.commons.math4.stat.descriptive.rank.Min;
import org.apache.commons.math4.stat.descriptive.rank.Percentile; import org.apache.commons.math4.stat.descriptive.rank.Percentile;
import org.apache.commons.math4.stat.descriptive.summary.Sum; import org.apache.commons.math4.stat.descriptive.summary.Sum;
import org.apache.commons.math4.stat.descriptive.summary.SumOfSquares; import org.apache.commons.math4.stat.descriptive.summary.SumOfSquares;
import org.apache.commons.math4.util.ResizableDoubleArray;
import org.apache.commons.numbers.core.Precision; import org.apache.commons.numbers.core.Precision;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
/** /**
* Test cases for the {@link DescriptiveStatistics} class. * Test cases for the {@link DescriptiveStatistics} class.
*/ */
public class DescriptiveStatisticsTest { public class DescriptiveStatisticsTest {
private static UniformRandomProvider random = RandomSource.create(RandomSource.WELL_1024_A, 2345789432894l);
protected DescriptiveStatistics createDescriptiveStatistics() { protected DescriptiveStatistics createDescriptiveStatistics() {
return new DescriptiveStatistics(); return new DescriptiveStatistics();
@ -292,6 +299,83 @@ public class DescriptiveStatisticsTest {
Assert.assertTrue(iqr >= 0); Assert.assertTrue(iqr >= 0);
} }
@Test
public void testInit0() {
//test window constructor
int window = 1 + random.nextInt(Integer.MAX_VALUE-1);
DescriptiveStatistics instance = new DescriptiveStatistics(window);
Assert.assertEquals(window,
instance.getWindowSize());
}
@Test
public void testInitDouble() {
//test double[] constructor
double[] initialDoubleArray = null;
new DescriptiveStatistics(initialDoubleArray);
//a null argument corresponds to DescriptiveStatistics(), so test
//that no exception is thrown
int initialDoubleArraySize = random.nextInt(1024 //some random
//memory consumption and test size limitation
);
// System.out.println(String.format("initialDoubleArraySize: %s",
// initialDoubleArraySize));
initialDoubleArray = new double[initialDoubleArraySize];
for(int i = 0; i < initialDoubleArraySize; i++) {
double value = random.nextDouble();
initialDoubleArray[i] = value;
}
new DescriptiveStatistics(initialDoubleArray);
}
@Test
public void testInitDoubleWrapper() {
//test Double[] constructor
Double[] initialDoubleWrapperArray = null;
new DescriptiveStatistics(initialDoubleWrapperArray);
int initialDoubleWrapperArraySize = random.nextInt(1024 //some random
//memory consumption and test size limitation
);
initialDoubleWrapperArray = generateInitialDoubleArray(initialDoubleWrapperArraySize);
new DescriptiveStatistics(initialDoubleWrapperArray);
}
@Test
public void testInitCopy() {
//test copy constructor
int initialDoubleArray = random.nextInt(1024 //some random
//memory consumption and test size limitation
);
DescriptiveStatistics original = new DescriptiveStatistics(initialDoubleArray);
DescriptiveStatistics instance = new DescriptiveStatistics(original);
Assert.assertEquals(original.getGeometricMean(),
instance.getGeometricMean(),
0);
Assert.assertEquals(original.getKurtosis(),
instance.getKurtosis(),
0);
Assert.assertEquals(original.getMax(),
instance.getMax(),
0);
Assert.assertEquals(original.getMean(),
instance.getMean(),
0);
Assert.assertEquals(original.getMin(),
instance.getMin(),
0);
Assert.assertEquals(original.getN(),
instance.getN());
Assert.assertEquals(original.getSkewness(),
instance.getSkewness(),
0);
Assert.assertArrayEquals(original.getValues(),
instance.getValues(),
0);
Assert.assertEquals(original.getWindowSize(),
instance.getWindowSize());
//doesn't implement equals
}
public void checkremoval(DescriptiveStatistics dstat, int wsize, public void checkremoval(DescriptiveStatistics dstat, int wsize,
double mean1, double mean2, double mean3) { double mean1, double mean2, double mean3) {
@ -310,6 +394,15 @@ public class DescriptiveStatisticsTest {
} }
private Double[] generateInitialDoubleArray(int size) {
Double[] retValue = new Double[size];
for(int i = 0; i < size; i++) {
Double value = random.nextDouble();
retValue[i] = value;
}
return retValue;
}
// Test UnivariateStatistics impls for setter injection tests // Test UnivariateStatistics impls for setter injection tests
/** /**