Skip to content

Commit

Permalink
Use Commons Statistics for significance testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aherbert committed Aug 22, 2024
1 parent 8d97de8 commit 427c7ff
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
3 changes: 1 addition & 2 deletions gdsc-test-rng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ Contains utilities for use with Commons RNG for random tests.
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
<artifactId>commons-statistics-inference</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

import java.util.Arrays;
import java.util.stream.Stream;
import org.apache.commons.math3.stat.inference.ChiSquareTest;
import org.apache.commons.rng.RandomProviderState;
import org.apache.commons.rng.RestorableUniformRandomProvider;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.statistics.inference.ChiSquareTest;
import org.apache.commons.statistics.inference.SignificanceResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.TestInstance;
Expand Down Expand Up @@ -230,9 +231,8 @@ void testNextIntInRange(UniformRandomProvider rng, int limit, int bins) {
}
final double[] expected = new double[bins];
Arrays.fill(expected, (double) samples / bins);
final ChiSquareTest test = new ChiSquareTest();
final double pvalue = test.chiSquareTest(expected, observed);
Assertions.assertFalse(pvalue < 0.01, "P-value = " + pvalue);
final SignificanceResult r = ChiSquareTest.withDefaults().test(expected, observed);
Assertions.assertFalse(r.reject(0.01), () -> "p-value = " + r.getPValue());
}

@ParameterizedTest
Expand All @@ -248,9 +248,8 @@ void testNextLongInRange(UniformRandomProvider rng, long limit, int bins) {
}
final double[] expected = new double[bins];
Arrays.fill(expected, (double) samples / bins);
final ChiSquareTest test = new ChiSquareTest();
final double pvalue = test.chiSquareTest(expected, observed);
Assertions.assertFalse(pvalue < 0.01, "P-value = " + pvalue);
final SignificanceResult r = ChiSquareTest.withDefaults().test(expected, observed);
Assertions.assertFalse(r.reject(0.01), () -> "p-value = " + r.getPValue());
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import java.util.Arrays;
import java.util.SplittableRandom;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.math3.stat.inference.ChiSquareTest;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.statistics.inference.ChiSquareTest;
import org.apache.commons.statistics.inference.SignificanceResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -184,8 +185,7 @@ void testCreateIncrement(long seed) {
}
final double[] expected = new double[16];
Arrays.fill(expected, 1.0 / 16);
final ChiSquareTest test = new ChiSquareTest();
final double pvalue = test.chiSquareTest(expected, observed);
Assertions.assertFalse(pvalue < 0.01, "P-value = " + pvalue);
final SignificanceResult r = ChiSquareTest.withDefaults().test(expected, observed);
Assertions.assertFalse(r.reject(0.01), () -> "p-value = " + r.getPValue());
}
}
3 changes: 1 addition & 2 deletions gdsc-test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ Contains utilities for use with test frameworks.
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
<artifactId>commons-statistics-inference</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

import java.util.Arrays;
import java.util.stream.DoubleStream;
import org.apache.commons.math3.stat.inference.ChiSquareTest;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.statistics.inference.ChiSquareTest;
import org.apache.commons.statistics.inference.SignificanceResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -71,9 +72,8 @@ void testBitScramblerIsUniform() {
}
}
// Do a chi-square test
final ChiSquareTest chiSq = new ChiSquareTest();
final double[] expected = DoubleStream.generate(() -> 1.0 / 256).limit(256).toArray();
final double pValue = chiSq.chiSquareTest(expected, histogram);
Assertions.assertFalse(pValue < 0.01);
final SignificanceResult r = ChiSquareTest.withDefaults().test(expected, histogram);
Assertions.assertFalse(r.reject(0.01), () -> "p-value = " + r.getPValue());
}
}

0 comments on commit 427c7ff

Please sign in to comment.