MATH-1279

Check precondition.
Thanks to David Georg Reichelt for the report.
This commit is contained in:
Gilles 2015-09-30 15:01:32 +02:00
parent 2aa4681cb8
commit 66d0a9a0a0
3 changed files with 20 additions and 3 deletions

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="fix" issue="MATH-1279"> <!-- backported to 3.6 -->
Check precondition (class "o.a.c.m.random.EmpiricalDistribution").
</action>
<action dev="erans" type="add" issue="MATH-1278"> <!-- backported to 3.6 --> <action dev="erans" type="add" issue="MATH-1278"> <!-- backported to 3.6 -->
Deep copy of "Network" (package "o.a.c.m.ml.neuralnet") to allow evaluation of Deep copy of "Network" (package "o.a.c.m.ml.neuralnet") to allow evaluation of
of intermediate states during training. of intermediate states during training.

View File

@ -37,6 +37,7 @@ import org.apache.commons.math4.exception.MathInternalError;
import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.exception.ZeroException; import org.apache.commons.math4.exception.ZeroException;
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.exception.util.LocalizedFormats;
import org.apache.commons.math4.stat.descriptive.StatisticalSummary; import org.apache.commons.math4.stat.descriptive.StatisticalSummary;
import org.apache.commons.math4.stat.descriptive.SummaryStatistics; import org.apache.commons.math4.stat.descriptive.SummaryStatistics;
@ -147,7 +148,8 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
/** /**
* Creates a new EmpiricalDistribution with the specified bin count. * Creates a new EmpiricalDistribution with the specified bin count.
* *
* @param binCount number of bins * @param binCount number of bins. Must be strictly positive.
* @throws NotStrictlyPositiveException if {@code binCount <= 0}.
*/ */
public EmpiricalDistribution(int binCount) { public EmpiricalDistribution(int binCount) {
this(binCount, new RandomDataGenerator()); this(binCount, new RandomDataGenerator());
@ -157,8 +159,9 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
* Creates a new EmpiricalDistribution with the specified bin count using the * Creates a new EmpiricalDistribution with the specified bin count using the
* provided {@link RandomGenerator} as the source of random data. * provided {@link RandomGenerator} as the source of random data.
* *
* @param binCount number of bins * @param binCount number of bins. Must be strictly positive.
* @param generator random data generator (may be null, resulting in default JDK generator) * @param generator random data generator (may be null, resulting in default JDK generator)
* @throws NotStrictlyPositiveException if {@code binCount <= 0}.
* @since 3.0 * @since 3.0
*/ */
public EmpiricalDistribution(int binCount, RandomGenerator generator) { public EmpiricalDistribution(int binCount, RandomGenerator generator) {
@ -180,12 +183,16 @@ public class EmpiricalDistribution extends AbstractRealDistribution {
* Private constructor to allow lazy initialisation of the RNG contained * Private constructor to allow lazy initialisation of the RNG contained
* in the {@link #randomData} instance variable. * in the {@link #randomData} instance variable.
* *
* @param binCount number of bins * @param binCount number of bins. Must be strictly positive.
* @param randomData Random data generator. * @param randomData Random data generator.
* @throws NotStrictlyPositiveException if {@code binCount <= 0}.
*/ */
private EmpiricalDistribution(int binCount, private EmpiricalDistribution(int binCount,
RandomDataGenerator randomData) { RandomDataGenerator randomData) {
super(randomData.getRandomGenerator()); super(randomData.getRandomGenerator());
if (binCount <= 0) {
throw new NotStrictlyPositiveException(binCount);
}
this.binCount = binCount; this.binCount = binCount;
this.randomData = randomData; this.randomData = randomData;
binStats = new ArrayList<SummaryStatistics>(); binStats = new ArrayList<SummaryStatistics>();

View File

@ -35,6 +35,7 @@ import org.apache.commons.math4.distribution.RealDistributionAbstractTest;
import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution;
import org.apache.commons.math4.exception.MathIllegalStateException; import org.apache.commons.math4.exception.MathIllegalStateException;
import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.random.EmpiricalDistribution; import org.apache.commons.math4.random.EmpiricalDistribution;
import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.stat.descriptive.SummaryStatistics; import org.apache.commons.math4.stat.descriptive.SummaryStatistics;
@ -85,6 +86,12 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
} }
} }
// MATH-1279
@Test(expected=NotStrictlyPositiveException.class)
public void testPrecondition1() {
new EmpiricalDistribution(0);
}
/** /**
* Test EmpiricalDistrbution.load() using sample data file.<br> * Test EmpiricalDistrbution.load() using sample data file.<br>
* Check that the sampleCount, mu and sigma match data in * Check that the sampleCount, mu and sigma match data in