24
24
import junit.framework.AssertionFailedError;
26
26
import org.apache.commons.math.RetryTestCase;
27
import org.apache.commons.math.TestUtils;
28
import org.apache.commons.math.distribution.BetaDistributionImpl;
29
import org.apache.commons.math.distribution.BinomialDistributionImpl;
30
import org.apache.commons.math.distribution.BinomialDistributionTest;
31
import org.apache.commons.math.distribution.CauchyDistributionImpl;
32
import org.apache.commons.math.distribution.ChiSquaredDistributionImpl;
33
import org.apache.commons.math.distribution.FDistributionImpl;
34
import org.apache.commons.math.distribution.GammaDistributionImpl;
35
import org.apache.commons.math.distribution.HypergeometricDistributionImpl;
36
import org.apache.commons.math.distribution.HypergeometricDistributionTest;
37
import org.apache.commons.math.distribution.PascalDistributionImpl;
38
import org.apache.commons.math.distribution.PascalDistributionTest;
27
39
import org.apache.commons.math.distribution.PoissonDistribution;
28
40
import org.apache.commons.math.distribution.PoissonDistributionImpl;
41
import org.apache.commons.math.distribution.TDistributionImpl;
42
import org.apache.commons.math.distribution.WeibullDistributionImpl;
43
import org.apache.commons.math.distribution.ZipfDistributionImpl;
44
import org.apache.commons.math.distribution.ZipfDistributionTest;
29
45
import org.apache.commons.math.stat.Frequency;
30
46
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
31
47
import org.apache.commons.math.stat.inference.ChiSquareTest;
32
48
import org.apache.commons.math.stat.inference.ChiSquareTestImpl;
49
import org.apache.commons.math.util.FastMath;
35
52
* Test cases for the RandomData class.
37
* @version $Revision: 924362 $ $Date: 2009-04-05 11:55:59 -0500 (Sun, 05 Apr
54
* @version $Revision: 1042376 $ $Date: 2009-04-05 11:55:59 -0500 (Sun, 05 Apr
45
62
randomData = new RandomDataImpl();
48
protected long smallSampleSize = 1000;
49
protected double[] expected = { 250, 250, 250, 250 };
50
protected int largeSampleSize = 10000;
51
private String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
65
protected final long smallSampleSize = 1000;
66
protected final double[] expected = { 250, 250, 250, 250 };
67
protected final int largeSampleSize = 10000;
68
private final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
52
69
"a", "b", "c", "d", "e", "f" };
53
70
protected RandomDataImpl randomData = null;
54
protected ChiSquareTestImpl testStatistic = new ChiSquareTestImpl();
71
protected final ChiSquareTestImpl testStatistic = new ChiSquareTestImpl();
56
73
public void testNextIntExtremeValues() {
57
74
int x = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE);
774
790
fail("permutation not found");
794
public void testNextInversionDeviate() throws Exception {
795
// Set the seed for the default random generator
796
randomData.reSeed(100);
797
double[] quantiles = new double[10];
798
for (int i = 0; i < 10; i++) {
799
quantiles[i] = randomData.nextUniform(0, 1);
801
// Reseed again so the inversion generator gets the same sequence
802
randomData.reSeed(100);
803
BetaDistributionImpl betaDistribution = new BetaDistributionImpl(2, 4);
805
* Generate a sequence of deviates using inversion - the distribution function
806
* evaluated at the random value from the distribution should match the uniform
807
* random value used to generate it, which is stored in the quantiles[] array.
809
for (int i = 0; i < 10; i++) {
810
double value = randomData.nextInversionDeviate(betaDistribution);
811
assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9);
815
public void testNextBeta() throws Exception {
816
double[] quartiles = TestUtils.getDistributionQuartiles(new BetaDistributionImpl(2,5));
817
long[] counts = new long[4];
818
randomData.reSeed(1000);
819
for (int i = 0; i < 1000; i++) {
820
double value = randomData.nextBeta(2, 5);
821
TestUtils.updateCounts(value, counts, quartiles);
823
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
826
public void testNextCauchy() throws Exception {
827
double[] quartiles = TestUtils.getDistributionQuartiles(new CauchyDistributionImpl(1.2, 2.1));
828
long[] counts = new long[4];
829
randomData.reSeed(1000);
830
for (int i = 0; i < 1000; i++) {
831
double value = randomData.nextCauchy(1.2, 2.1);
832
TestUtils.updateCounts(value, counts, quartiles);
834
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
837
public void testNextChiSquare() throws Exception {
838
double[] quartiles = TestUtils.getDistributionQuartiles(new ChiSquaredDistributionImpl(12));
839
long[] counts = new long[4];
840
randomData.reSeed(1000);
841
for (int i = 0; i < 1000; i++) {
842
double value = randomData.nextChiSquare(12);
843
TestUtils.updateCounts(value, counts, quartiles);
845
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
848
public void testNextF() throws Exception {
849
double[] quartiles = TestUtils.getDistributionQuartiles(new FDistributionImpl(12, 5));
850
long[] counts = new long[4];
851
randomData.reSeed(1000);
852
for (int i = 0; i < 1000; i++) {
853
double value = randomData.nextF(12, 5);
854
TestUtils.updateCounts(value, counts, quartiles);
856
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
859
public void testNextGamma() throws Exception {
860
double[] quartiles = TestUtils.getDistributionQuartiles(new GammaDistributionImpl(4, 2));
861
long[] counts = new long[4];
862
randomData.reSeed(1000);
863
for (int i = 0; i < 1000; i++) {
864
double value = randomData.nextGamma(4, 2);
865
TestUtils.updateCounts(value, counts, quartiles);
867
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
870
public void testNextT() throws Exception {
871
double[] quartiles = TestUtils.getDistributionQuartiles(new TDistributionImpl(10));
872
long[] counts = new long[4];
873
randomData.reSeed(1000);
874
for (int i = 0; i < 1000; i++) {
875
double value = randomData.nextT(10);
876
TestUtils.updateCounts(value, counts, quartiles);
878
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
881
public void testNextWeibull() throws Exception {
882
double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistributionImpl(1.2, 2.1));
883
long[] counts = new long[4];
884
randomData.reSeed(1000);
885
for (int i = 0; i < 1000; i++) {
886
double value = randomData.nextWeibull(1.2, 2.1);
887
TestUtils.updateCounts(value, counts, quartiles);
889
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
892
public void testNextBinomial() throws Exception {
893
BinomialDistributionTest testInstance = new BinomialDistributionTest("");
894
int[] densityPoints = testInstance.makeDensityTestPoints();
895
double[] densityValues = testInstance.makeDensityTestValues();
896
int sampleSize = 1000;
897
int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
898
BinomialDistributionImpl distribution = (BinomialDistributionImpl) testInstance.makeDistribution();
899
double[] expectedCounts = new double[length];
900
long[] observedCounts = new long[length];
901
for (int i = 0; i < length; i++) {
902
expectedCounts[i] = sampleSize * densityValues[i];
904
randomData.reSeed(1000);
905
for (int i = 0; i < sampleSize; i++) {
906
int value = randomData.nextBinomial(distribution.getNumberOfTrials(),
907
distribution.getProbabilityOfSuccess());
908
for (int j = 0; j < length; j++) {
909
if (value == densityPoints[j]) {
914
TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
917
public void testNextHypergeometric() throws Exception {
918
HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest("");
919
int[] densityPoints = testInstance.makeDensityTestPoints();
920
double[] densityValues = testInstance.makeDensityTestValues();
921
int sampleSize = 1000;
922
int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
923
HypergeometricDistributionImpl distribution = (HypergeometricDistributionImpl) testInstance.makeDistribution();
924
double[] expectedCounts = new double[length];
925
long[] observedCounts = new long[length];
926
for (int i = 0; i < length; i++) {
927
expectedCounts[i] = sampleSize * densityValues[i];
929
randomData.reSeed(1000);
930
for (int i = 0; i < sampleSize; i++) {
931
int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
932
distribution.getNumberOfSuccesses(), distribution.getSampleSize());
933
for (int j = 0; j < length; j++) {
934
if (value == densityPoints[j]) {
939
TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
942
public void testNextPascal() throws Exception {
943
PascalDistributionTest testInstance = new PascalDistributionTest("");
944
int[] densityPoints = testInstance.makeDensityTestPoints();
945
double[] densityValues = testInstance.makeDensityTestValues();
946
int sampleSize = 1000;
947
int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
948
PascalDistributionImpl distribution = (PascalDistributionImpl) testInstance.makeDistribution();
949
double[] expectedCounts = new double[length];
950
long[] observedCounts = new long[length];
951
for (int i = 0; i < length; i++) {
952
expectedCounts[i] = sampleSize * densityValues[i];
954
randomData.reSeed(1000);
955
for (int i = 0; i < sampleSize; i++) {
956
int value = randomData.nextPascal(distribution.getNumberOfSuccesses(), distribution.getProbabilityOfSuccess());
957
for (int j = 0; j < length; j++) {
958
if (value == densityPoints[j]) {
963
TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
966
public void testNextZipf() throws Exception {
967
ZipfDistributionTest testInstance = new ZipfDistributionTest("");
968
int[] densityPoints = testInstance.makeDensityTestPoints();
969
double[] densityValues = testInstance.makeDensityTestValues();
970
int sampleSize = 1000;
971
int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
972
ZipfDistributionImpl distribution = (ZipfDistributionImpl) testInstance.makeDistribution();
973
double[] expectedCounts = new double[length];
974
long[] observedCounts = new long[length];
975
for (int i = 0; i < length; i++) {
976
expectedCounts[i] = sampleSize * densityValues[i];
978
randomData.reSeed(1000);
979
for (int i = 0; i < sampleSize; i++) {
980
int value = randomData.nextZipf(distribution.getNumberOfElements(), distribution.getExponent());
981
for (int j = 0; j < length; j++) {
982
if (value == densityPoints[j]) {
987
TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);