You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/07/30 17:17:46 UTC
[commons-statistics] branch master updated (c2c4cc3 -> 530da73)
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git.
from c2c4cc3 Remove use of explicit return value variable.
new 9cefb39 Add test assumption to the discrete distribution sampling test
new 530da73 Update sampling test to create the expected first
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../ContinuousDistributionAbstractTest.java | 6 ++---
.../DiscreteDistributionAbstractTest.java | 28 ++++++++++++++++------
2 files changed, 24 insertions(+), 10 deletions(-)
[commons-statistics] 01/02: Add test assumption to the discrete
distribution sampling test
Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 9cefb392a8829ccc48f53df648c45eb09ac9e98c
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Jul 30 18:15:17 2021 +0100
Add test assumption to the discrete distribution sampling test
This requires the distribution is tested against at least 50% of the
PMF.
---
.../DiscreteDistributionAbstractTest.java | 28 ++++++++++++++++------
1 file changed, 21 insertions(+), 7 deletions(-)
diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/DiscreteDistributionAbstractTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/DiscreteDistributionAbstractTest.java
index fcc46b3..763c67f 100644
--- a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/DiscreteDistributionAbstractTest.java
+++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/DiscreteDistributionAbstractTest.java
@@ -465,29 +465,43 @@ abstract class DiscreteDistributionAbstractTest {
@Test
void testSampling() {
+ // This test uses the points that are used to test the distribution PMF.
+ // The sum of the probability values does not have to be 1 (or very close to 1).
+ // Any value generated by the sampler that is not an expected point will
+ // be ignored. If the sum of probabilities is above 0.5 then at least half
+ // of the samples should be counted and the test will verify these occur with
+ // the expected relative frequencies. Note: The expected values are normalised
+ // to 1 (i.e. relative frequencies) by the Chi-square test.
+ final int[] probabilityPoints = makeProbabilityTestPoints();
+ final double[] probabilityValues = makeProbabilityTestValues();
+ final int length = TestUtils.eliminateZeroMassPoints(probabilityPoints, probabilityValues);
+ final double[] expected = Arrays.copyOf(probabilityValues, length);
+
+ // This test will not be valid if the points do not represent enough of the PMF.
+ // Require at least 50%.
+ final double sum = Arrays.stream(expected).sum();
+ if (sum < 0.5) {
+ Assertions.fail("Not enough of the PMF is tested during sampling: " + sum);
+ }
+
// Use fixed seed.
final int sampleSize = 1000;
final DiscreteDistribution.Sampler sampler =
getDistribution().createSampler(RandomSource.create(RandomSource.WELL_512_A, 1000));
final int[] sample = TestUtils.sample(sampleSize, sampler);
- final int[] densityPoints = makeProbabilityTestPoints();
- final double[] densityValues = makeProbabilityTestValues();
- final int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
- final double[] expected = Arrays.copyOf(densityValues, length);
-
final long[] counts = new long[length];
for (int i = 0; i < sampleSize; i++) {
final int x = sample[i];
for (int j = 0; j < length; j++) {
- if (x == densityPoints[j]) {
+ if (x == probabilityPoints[j]) {
counts[j]++;
break;
}
}
}
- TestUtils.assertChiSquareAccept(densityPoints, expected, counts, 0.001);
+ TestUtils.assertChiSquareAccept(probabilityPoints, expected, counts, 0.001);
}
/**
[commons-statistics] 02/02: Update sampling test to create the
expected first
Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 530da736167f7e619a64c7e3483936924d0e52e3
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Jul 30 18:15:57 2021 +0100
Update sampling test to create the expected first
This matches the ContinuousDistributionAbstractTest
---
.../statistics/distribution/ContinuousDistributionAbstractTest.java | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ContinuousDistributionAbstractTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ContinuousDistributionAbstractTest.java
index b7e380b..c714397 100644
--- a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ContinuousDistributionAbstractTest.java
+++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ContinuousDistributionAbstractTest.java
@@ -520,15 +520,15 @@ abstract class ContinuousDistributionAbstractTest {
@Test
void testSampling() {
+ final double[] quartiles = TestUtils.getDistributionQuartiles(getDistribution());
+ final double[] expected = {0.25, 0.25, 0.25, 0.25};
+
// Use fixed seed.
final int sampleSize = 1000;
final ContinuousDistribution.Sampler sampler =
getDistribution().createSampler(RandomSource.create(RandomSource.WELL_19937_C, 123456789L));
final double[] sample = TestUtils.sample(sampleSize, sampler);
- final double[] quartiles = TestUtils.getDistributionQuartiles(getDistribution());
- final double[] expected = {250, 250, 250, 250};
-
final long[] counts = new long[4];
for (int i = 0; i < sampleSize; i++) {
TestUtils.updateCounts(sample[i], counts, quartiles);