http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ExponentialDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ExponentialDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ExponentialDistributionTest.java new file mode 100644 index 0000000..649ce7a --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ExponentialDistributionTest.java @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for ExponentialDistribution. + * Extends ContinuousDistributionAbstractTest. See class javadoc for + * ContinuousDistributionAbstractTest for details. + * + */ +public class ExponentialDistributionTest extends ContinuousDistributionAbstractTest { + + // --------------------- Override tolerance -------------- + @Override + public void setUp() { + super.setUp(); + setTolerance(1e-9); + } + + //-------------- Implementations for abstract methods ----------------------- + + /** Creates the default continuous distribution instance to use in tests. */ + @Override + public ExponentialDistribution makeDistribution() { + return new ExponentialDistribution(5.0); + } + + /** Creates the default cumulative probability distribution test input values */ + @Override + public double[] makeCumulativeTestPoints() { + // quantiles computed using R version 2.9.2 + return new double[] {0.00500250166792, 0.0502516792675, 0.126589039921, 0.256466471938, + 0.526802578289, 34.5387763949, 23.0258509299, 18.4443972706, 14.9786613678, 11.5129254650}; + } + + /** Creates the default cumulative probability density test expected values */ + @Override + public double[] makeCumulativeTestValues() { + return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, + 0.990, 0.975, 0.950, 0.900}; + } + + /** Creates the default probability density test expected values */ + @Override + public double[] makeDensityTestValues() { + return new double[] {0.1998, 0.198, 0.195, 0.19, 0.18, 0.000200000000000, + 0.00200000000002, 0.00499999999997, 0.00999999999994, 0.0199999999999}; + } + + //------------ Additional tests ------------------------------------------- + + @Test + public void testCumulativeProbabilityExtremes() { + setCumulativeTestPoints(new double[] {-2, 0}); + setCumulativeTestValues(new double[] {0, 0}); + verifyCumulativeProbabilities(); + } + + @Test + public void testInverseCumulativeProbabilityExtremes() { + setInverseCumulativeTestPoints(new double[] {0, 1}); + setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); + verifyInverseCumulativeProbabilities(); + } + + @Test + public void testCumulativeProbability2() { + double actual = getDistribution().probability(0.25, 0.75); + Assert.assertEquals(0.0905214, actual, 10e-4); + } + + @Test + public void testDensity() { + ExponentialDistribution d1 = new ExponentialDistribution(1); + Assert.assertTrue(Precision.equals(0.0, d1.density(-1e-9), 1)); + Assert.assertTrue(Precision.equals(1.0, d1.density(0.0), 1)); + Assert.assertTrue(Precision.equals(0.0, d1.density(1000.0), 1)); + Assert.assertTrue(Precision.equals(Math.exp(-1), d1.density(1.0), 1)); + Assert.assertTrue(Precision.equals(Math.exp(-2), d1.density(2.0), 1)); + + ExponentialDistribution d2 = new ExponentialDistribution(3); + Assert.assertTrue(Precision.equals(1/3.0, d2.density(0.0), 1)); + // computed using print(dexp(1, rate=1/3), digits=10) in R 2.5 + Assert.assertEquals(0.2388437702, d2.density(1.0), 1e-8); + + // computed using print(dexp(2, rate=1/3), digits=10) in R 2.5 + Assert.assertEquals(0.1711390397, d2.density(2.0), 1e-8); + } + + @Test + public void testMeanAccessors() { + ExponentialDistribution distribution = (ExponentialDistribution) getDistribution(); + Assert.assertEquals(5d, distribution.getMean(), Double.MIN_VALUE); + } + + @Test(expected=IllegalArgumentException.class) + public void testPrecondition1() { + new ExponentialDistribution(0); + } + + @Test + public void testMoments() { + final double tol = 1e-9; + ExponentialDistribution dist; + + dist = new ExponentialDistribution(11d); + Assert.assertEquals(dist.getNumericalMean(), 11d, tol); + Assert.assertEquals(dist.getNumericalVariance(), 11d * 11d, tol); + + dist = new ExponentialDistribution(10.5d); + Assert.assertEquals(dist.getNumericalMean(), 10.5d, tol); + Assert.assertEquals(dist.getNumericalVariance(), 10.5d * 10.5d, tol); + } +}
http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/FDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/FDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/FDistributionTest.java new file mode 100644 index 0000000..b29a0be --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/FDistributionTest.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for FDistribution. + * Extends ContinuousDistributionAbstractTest. See class javadoc for + * ContinuousDistributionAbstractTest for details. + * + */ +public class FDistributionTest extends ContinuousDistributionAbstractTest { + + //-------------- Implementations for abstract methods ----------------------- + + /** Creates the default continuous distribution instance to use in tests. */ + @Override + public FDistribution makeDistribution() { + return new FDistribution(5.0, 6.0); + } + + /** Creates the default cumulative probability distribution test input values */ + @Override + public double[] makeCumulativeTestPoints() { + // quantiles computed using R version 2.9.2 + return new double[] {0.0346808448626, 0.0937009113303, 0.143313661184, 0.202008445998, 0.293728320107, + 20.8026639595, 8.74589525602, 5.98756512605, 4.38737418741, 3.10751166664}; + } + + /** Creates the default cumulative probability density test expected values */ + @Override + public double[] makeCumulativeTestValues() { + return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; + } + + /** Creates the default probability density test expected values */ + @Override + public double[] makeDensityTestValues() { + return new double[] {0.0689156576706, 0.236735653193, 0.364074131941, 0.481570789649, 0.595880479994, + 0.000133443915657, 0.00286681303403, 0.00969192007502, 0.0242883861471, 0.0605491314658}; + } + + // --------------------- Override tolerance -------------- + @Override + public void setUp() { + super.setUp(); + setTolerance(1e-9); + } + + //---------------------------- Additional test cases ------------------------- + + @Test + public void testCumulativeProbabilityExtremes() { + setCumulativeTestPoints(new double[] {-2, 0}); + setCumulativeTestValues(new double[] {0, 0}); + verifyCumulativeProbabilities(); + } + + @Test + public void testInverseCumulativeProbabilityExtremes() { + setInverseCumulativeTestPoints(new double[] {0, 1}); + setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); + verifyInverseCumulativeProbabilities(); + } + + @Test + public void testDfAccessors() { + FDistribution dist = (FDistribution) getDistribution(); + Assert.assertEquals(5d, dist.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE); + Assert.assertEquals(6d, dist.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE); + } + + @Test(expected=IllegalArgumentException.class) + public void testPrecondition1() { + new FDistribution(0, 1); + } + @Test(expected=IllegalArgumentException.class) + public void testPrecondition2() { + new FDistribution(1, 0); + } + + @Test + public void testLargeDegreesOfFreedom() { + FDistribution fd = new FDistribution(100000, 100000); + double p = fd.cumulativeProbability(.999); + double x = fd.inverseCumulativeProbability(p); + Assert.assertEquals(.999, x, 1.0e-5); + } + + @Test + public void testSmallDegreesOfFreedom() { + FDistribution fd = new FDistribution(1, 1); + double p = fd.cumulativeProbability(0.975); + double x = fd.inverseCumulativeProbability(p); + Assert.assertEquals(0.975, x, 1.0e-5); + + fd = new FDistribution(1, 2); + p = fd.cumulativeProbability(0.975); + x = fd.inverseCumulativeProbability(p); + Assert.assertEquals(0.975, x, 1.0e-5); + } + + @Test + public void testMoments() { + final double tol = 1e-9; + FDistribution dist; + + dist = new FDistribution(1, 2); + Assert.assertTrue(Double.isNaN(dist.getNumericalMean())); + Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); + + dist = new FDistribution(1, 3); + Assert.assertEquals(dist.getNumericalMean(), 3d / (3d - 2d), tol); + Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); + + dist = new FDistribution(1, 5); + Assert.assertEquals(dist.getNumericalMean(), 5d / (5d - 2d), tol); + Assert.assertEquals(dist.getNumericalVariance(), (2d * 5d * 5d * 4d) / 9d, tol); + } + + @Test + public void testMath785() { + // this test was failing due to inaccurate results from ContinuedFraction. + + try { + double prob = 0.01; + FDistribution f = new FDistribution(200000, 200000); + double result = f.inverseCumulativeProbability(prob); + Assert.assertTrue(result < 1.0); + } catch (Exception e) { + Assert.fail("Failing to calculate inverse cumulative probability"); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GammaDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GammaDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GammaDistributionTest.java new file mode 100644 index 0000000..b25f251 --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GammaDistributionTest.java @@ -0,0 +1,354 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.statistics.distribution; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.apache.commons.numbers.gamma.LanczosApproximation; +import org.apache.commons.math3.stat.descriptive.SummaryStatistics; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for GammaDistribution. + * Extends ContinuousDistributionAbstractTest. See class javadoc for + * ContinuousDistributionAbstractTest for details. + * + */ +public class GammaDistributionTest extends ContinuousDistributionAbstractTest { + + //-------------- Implementations for abstract methods ----------------------- + + /** Creates the default continuous distribution instance to use in tests. */ + @Override + public GammaDistribution makeDistribution() { + return new GammaDistribution(4d, 2d); + } + + /** Creates the default cumulative probability distribution test input values */ + @Override + public double[] makeCumulativeTestPoints() { + // quantiles computed using R version 2.9.2 + return new double[] {0.857104827257, 1.64649737269, 2.17973074725, 2.7326367935, 3.48953912565, + 26.1244815584, 20.0902350297, 17.5345461395, 15.5073130559, 13.3615661365}; + } + + /** Creates the default cumulative probability density test expected values */ + @Override + public double[] makeCumulativeTestValues() { + return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; + } + + /** Creates the default probability density test expected values */ + @Override + public double[] makeDensityTestValues() { + return new double[] {0.00427280075546, 0.0204117166709, 0.0362756163658, 0.0542113174239, 0.0773195272491, + 0.000394468852816, 0.00366559696761, 0.00874649473311, 0.0166712508128, 0.0311798227954}; + } + + // --------------------- Override tolerance -------------- + @Override + public void setUp() { + super.setUp(); + setTolerance(1e-9); + } + + //---------------------------- Additional test cases ------------------------- + @Test + public void testParameterAccessors() { + GammaDistribution distribution = (GammaDistribution) getDistribution(); + Assert.assertEquals(4d, distribution.getShape(), 0); + Assert.assertEquals(2d, distribution.getScale(), 0); + } + + @Test(expected=IllegalArgumentException.class) + public void testPrecondition1() { + new GammaDistribution(0, 1); + } + @Test(expected=IllegalArgumentException.class) + public void testPrecondition2() { + new GammaDistribution(1, 0); + } + + @Test + public void testProbabilities() { + testProbability(-1.000, 4.0, 2.0, .0000); + testProbability(15.501, 4.0, 2.0, .9499); + testProbability(0.504, 4.0, 1.0, .0018); + testProbability(10.011, 1.0, 2.0, .9933); + testProbability(5.000, 2.0, 2.0, .7127); + } + + @Test + public void testValues() { + testValue(15.501, 4.0, 2.0, .9499); + testValue(0.504, 4.0, 1.0, .0018); + testValue(10.011, 1.0, 2.0, .9933); + testValue(5.000, 2.0, 2.0, .7127); + } + + private void testProbability(double x, double a, double b, double expected) { + GammaDistribution distribution = new GammaDistribution( a, b ); + double actual = distribution.cumulativeProbability(x); + Assert.assertEquals("probability for " + x, expected, actual, 10e-4); + } + + private void testValue(double expected, double a, double b, double p) { + GammaDistribution distribution = new GammaDistribution( a, b ); + double actual = distribution.inverseCumulativeProbability(p); + Assert.assertEquals("critical value for " + p, expected, actual, 10e-4); + } + + @Test + public void testDensity() { + double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5}; + // R2.5: print(dgamma(x, shape=1, rate=1), digits=10) + checkDensity(1, 1, x, new double[]{0.000000000000, 0.999999000001, 0.606530659713, 0.367879441171, 0.135335283237, 0.006737946999}); + // R2.5: print(dgamma(x, shape=2, rate=1), digits=10) + checkDensity(2, 1, x, new double[]{0.000000000000, 0.000000999999, 0.303265329856, 0.367879441171, 0.270670566473, 0.033689734995}); + // R2.5: print(dgamma(x, shape=4, rate=1), digits=10) + checkDensity(4, 1, x, new double[]{0.000000000e+00, 1.666665000e-19, 1.263605541e-02, 6.131324020e-02, 1.804470443e-01, 1.403738958e-01}); + // R2.5: print(dgamma(x, shape=4, rate=10), digits=10) + checkDensity(4, 10, x, new double[]{0.000000000e+00, 1.666650000e-15, 1.403738958e+00, 7.566654960e-02, 2.748204830e-05, 4.018228850e-17}); + // R2.5: print(dgamma(x, shape=.1, rate=10), digits=10) + checkDensity(0.1, 10, x, new double[]{0.000000000e+00, 3.323953832e+04, 1.663849010e-03, 6.007786726e-06, 1.461647647e-10, 5.996008322e-24}); + // R2.5: print(dgamma(x, shape=.1, rate=20), digits=10) + checkDensity(0.1, 20, x, new double[]{0.000000000e+00, 3.562489883e+04, 1.201557345e-05, 2.923295295e-10, 3.228910843e-19, 1.239484589e-45}); + // R2.5: print(dgamma(x, shape=.1, rate=4), digits=10) + checkDensity(0.1, 4, x, new double[]{0.000000000e+00, 3.032938388e+04, 3.049322494e-02, 2.211502311e-03, 2.170613371e-05, 5.846590589e-11}); + // R2.5: print(dgamma(x, shape=.1, rate=1), digits=10) + checkDensity(0.1, 1, x, new double[]{0.000000000e+00, 2.640334143e+04, 1.189704437e-01, 3.866916944e-02, 7.623306235e-03, 1.663849010e-04}); + } + + private void checkDensity(double alpha, double rate, double[] x, double[] expected) { + GammaDistribution d = new GammaDistribution(alpha, 1 / rate); + for (int i = 0; i < x.length; i++) { + Assert.assertEquals(expected[i], d.density(x[i]), 1e-5); + } + } + + @Test + public void testInverseCumulativeProbabilityExtremes() { + setInverseCumulativeTestPoints(new double[] {0, 1}); + setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); + verifyInverseCumulativeProbabilities(); + } + + @Test + public void testMoments() { + final double tol = 1e-9; + GammaDistribution dist; + + dist = new GammaDistribution(1, 2); + Assert.assertEquals(dist.getNumericalMean(), 2, tol); + Assert.assertEquals(dist.getNumericalVariance(), 4, tol); + + dist = new GammaDistribution(1.1, 4.2); + Assert.assertEquals(dist.getNumericalMean(), 1.1d * 4.2d, tol); + Assert.assertEquals(dist.getNumericalVariance(), 1.1d * 4.2d * 4.2d, tol); + } + + private static final double HALF_LOG_2_PI = 0.5 * Math.log(2.0 * Math.PI); + + public static double logGamma(double x) { + /* + * This is a copy of + * double Gamma.logGamma(double) + * prior to MATH-849 + */ + double ret; + + if (Double.isNaN(x) || (x <= 0.0)) { + ret = Double.NaN; + } else { + double sum = LanczosApproximation.value(x); + double tmp = x + LanczosApproximation.g() + .5; + ret = ((x + .5) * Math.log(tmp)) - tmp + + HALF_LOG_2_PI + Math.log(sum / x); + } + + return ret; + } + + public static double density(final double x, + final double shape, + final double scale) { + /* + * This is a copy of + * double GammaDistribution.density(double) + * prior to MATH-753. + */ + if (x < 0) { + return 0; + } + return Math.pow(x / scale, shape - 1) / scale * + Math.exp(-x / scale) / Math.exp(logGamma(shape)); + } + + /* + * MATH-753: large values of x or shape parameter cause density(double) to + * overflow. Reference data is generated with the Maxima script + * gamma-distribution.mac, which can be found in + * src/test/resources/org/apache/commons/math3/distribution. + */ + + private void doTestMath753(final double shape, + final double meanNoOF, final double sdNoOF, + final double meanOF, final double sdOF, + final String resourceName) + throws IOException { + final GammaDistribution distribution = new GammaDistribution(shape, 1.0); + final SummaryStatistics statOld = new SummaryStatistics(); + final SummaryStatistics statNewNoOF = new SummaryStatistics(); + final SummaryStatistics statNewOF = new SummaryStatistics(); + + final InputStream resourceAsStream; + resourceAsStream = this.getClass().getResourceAsStream(resourceName); + Assert.assertNotNull("Could not find resource " + resourceName, + resourceAsStream); + final BufferedReader in; + in = new BufferedReader(new InputStreamReader(resourceAsStream)); + + try { + for (String line = in.readLine(); line != null; line = in.readLine()) { + if (line.startsWith("#")) { + continue; + } + final String[] tokens = line.split(", "); + Assert.assertTrue("expected two floating-point values", + tokens.length == 2); + final double x = Double.parseDouble(tokens[0]); + final String msg = "x = " + x + ", shape = " + shape + + ", scale = 1.0"; + final double expected = Double.parseDouble(tokens[1]); + final double ulp = Math.ulp(expected); + final double actualOld = density(x, shape, 1.0); + final double actualNew = distribution.density(x); + final double errOld, errNew; + errOld = Math.abs((actualOld - expected) / ulp); + errNew = Math.abs((actualNew - expected) / ulp); + + if (Double.isNaN(actualOld) || Double.isInfinite(actualOld)) { + Assert.assertFalse(msg, Double.isNaN(actualNew)); + Assert.assertFalse(msg, Double.isInfinite(actualNew)); + statNewOF.addValue(errNew); + } else { + statOld.addValue(errOld); + statNewNoOF.addValue(errNew); + } + } + if (statOld.getN() != 0) { + /* + * If no overflow occurs, check that new implementation is + * better than old one. + */ + final StringBuilder sb = new StringBuilder("shape = "); + sb.append(shape); + sb.append(", scale = 1.0\n"); + sb.append("Old implementation\n"); + sb.append("------------------\n"); + sb.append(statOld.toString()); + sb.append("New implementation\n"); + sb.append("------------------\n"); + sb.append(statNewNoOF.toString()); + final String msg = sb.toString(); + + final double oldMin = statOld.getMin(); + final double newMin = statNewNoOF.getMin(); + Assert.assertTrue(msg, newMin <= oldMin); + + final double oldMax = statOld.getMax(); + final double newMax = statNewNoOF.getMax(); + Assert.assertTrue(msg, newMax <= oldMax); + + final double oldMean = statOld.getMean(); + final double newMean = statNewNoOF.getMean(); + Assert.assertTrue(msg, newMean <= oldMean); + + final double oldSd = statOld.getStandardDeviation(); + final double newSd = statNewNoOF.getStandardDeviation(); + Assert.assertTrue(msg, newSd <= oldSd); + + Assert.assertTrue(msg, newMean <= meanNoOF); + Assert.assertTrue(msg, newSd <= sdNoOF); + } + if (statNewOF.getN() != 0) { + final double newMean = statNewOF.getMean(); + final double newSd = statNewOF.getStandardDeviation(); + + final StringBuilder sb = new StringBuilder("shape = "); + sb.append(shape); + sb.append(", scale = 1.0"); + sb.append(", max. mean error (ulps) = "); + sb.append(meanOF); + sb.append(", actual mean error (ulps) = "); + sb.append(newMean); + sb.append(", max. sd of error (ulps) = "); + sb.append(sdOF); + sb.append(", actual sd of error (ulps) = "); + sb.append(newSd); + final String msg = sb.toString(); + + Assert.assertTrue(msg, newMean <= meanOF); + Assert.assertTrue(msg, newSd <= sdOF); + } + } catch (IOException e) { + Assert.fail(e.getMessage()); + } finally { + in.close(); + } + } + + + @Test + public void testMath753Shape1() throws IOException { + doTestMath753(1.0, 1.5, 0.5, 0.0, 0.0, "gamma-distribution-shape-1.csv"); + } + + @Test + public void testMath753Shape8() throws IOException { + doTestMath753(8.0, 1.5, 1.0, 0.0, 0.0, "gamma-distribution-shape-8.csv"); + } + + @Test + public void testMath753Shape10() throws IOException { + doTestMath753(10.0, 1.0, 1.0, 0.0, 0.0, "gamma-distribution-shape-10.csv"); + } + + @Test + public void testMath753Shape100() throws IOException { + // XXX Increased tolerance ("1.5" -> "2.0") to make test pass with JDK "Math" + // where CM used "FastMath" (cf. "XXX" comment in main source code). + doTestMath753(100.0, 2.0, 1.0, 0.0, 0.0, "gamma-distribution-shape-100.csv"); + } + + @Test + public void testMath753Shape142() throws IOException { + doTestMath753(142.0, 3.3, 1.6, 40.0, 40.0, "gamma-distribution-shape-142.csv"); + } + + @Test + public void testMath753Shape1000() throws IOException { + // XXX Increased tolerance ("220.0" -> "230.0") to make test pass with JDK "Math" + // where CM used "FastMath" (cf. "XXX" comment in main source code). + doTestMath753(1000.0, 1.0, 1.0, 160.0, 230.0, "gamma-distribution-shape-1000.csv"); + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GeometricDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GeometricDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GeometricDistributionTest.java new file mode 100644 index 0000000..25cf6ed --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GeometricDistributionTest.java @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law + * or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for GeometricDistribution. + * See class javadoc for DiscreteDistributionAbstractTest for details. + */ +public class GeometricDistributionTest extends DiscreteDistributionAbstractTest { + + /** + * Constructor to override default tolerance. + */ + public GeometricDistributionTest() { + setTolerance(1e-12); + } + + // -------------- Implementations for abstract methods -------------------- + + /** Creates the default discrete distribution instance to use in tests. */ + @Override + public DiscreteDistribution makeDistribution() { + return new GeometricDistribution(0.40); + } + + /** Creates the default probability density test input values */ + @Override + public int[] makeDensityTestPoints() { + return new int[] { -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }; + } + + /** + * Creates the default probability density test expected values. + * Reference values are from R, version version 2.15.3. + */ + @Override + public double[] makeDensityTestValues() { + return new double[] { + 0d, 0.4, 0.24, 0.144, 0.0864, 0.05184, 0.031104, 0.0186624, + 0.01119744, 0.006718464, 0.0040310784, 0.00241864704, + 0.001451188224,0.0008707129344, 0.00052242776064, 0.000313456656384, + 0.00018807399383, 0.000112844396298, 6.77066377789e-05, 4.06239826674e-05, + 2.43743896004e-05, 1.46246337603e-05, 8.77478025615e-06, 5.26486815369e-06, + 3.15892089221e-06, 1.89535253533e-06, 1.1372115212e-06, 6.82326912718e-07, + 4.09396147631e-07, 2.45637688579e-07 + }; + } + + /** + * Creates the default log probability density test expected values. + * Reference values are from R, version version 2.14.1. + */ + @Override + public double[] makeLogDensityTestValues() { + return new double[] { + Double.NEGATIVE_INFINITY, -0.916290731874155, -1.42711635564015, -1.93794197940614, + -2.44876760317213, -2.95959322693812, -3.47041885070411, -3.9812444744701, + -4.49207009823609, -5.00289572200208, -5.51372134576807, -6.02454696953406, + -6.53537259330005, -7.04619821706604, -7.55702384083203, -8.06784946459802, + -8.57867508836402, -9.08950071213001, -9.600326335896, -10.111151959662, + -10.621977583428, -11.132803207194, -11.64362883096, -12.154454454726, + -12.6652800784919, -13.1761057022579, -13.6869313260239, -14.1977569497899, + -14.7085825735559, -15.2194081973219 + }; + } + + /** Creates the default cumulative probability density test input values */ + @Override + public int[] makeCumulativeTestPoints() { + return makeDensityTestPoints(); + } + + /** Creates the default cumulative probability density test expected values */ + @Override + public double[] makeCumulativeTestValues() { + final double[] densities = makeDensityTestValues(); + final int n = densities.length; + final double[] ret = new double[n]; + ret[0] = densities[0]; + for (int i = 1; i < n; i++) { + ret[i] = ret[i - 1] + densities[i]; + } + return ret; + } + + /** Creates the default inverse cumulative probability test input values */ + @Override + public double[] makeInverseCumulativeTestPoints() { + return new double[] { + 0.000, 0.005, 0.010, 0.015, 0.020, 0.025, 0.030, 0.035, 0.040, + 0.045, 0.050, 0.055, 0.060, 0.065, 0.070, 0.075, 0.080, 0.085, + 0.090, 0.095, 0.100, 0.105, 0.110, 0.115, 0.120, 0.125, 0.130, + 0.135, 0.140, 0.145, 0.150, 0.155, 0.160, 0.165, 0.170, 0.175, + 0.180, 0.185, 0.190, 0.195, 0.200, 0.205, 0.210, 0.215, 0.220, + 0.225, 0.230, 0.235, 0.240, 0.245, 0.250, 0.255, 0.260, 0.265, + 0.270, 0.275, 0.280, 0.285, 0.290, 0.295, 0.300, 0.305, 0.310, + 0.315, 0.320, 0.325, 0.330, 0.335, 0.340, 0.345, 0.350, 0.355, + 0.360, 0.365, 0.370, 0.375, 0.380, 0.385, 0.390, 0.395, 0.400, + 0.405, 0.410, 0.415, 0.420, 0.425, 0.430, 0.435, 0.440, 0.445, + 0.450, 0.455, 0.460, 0.465, 0.470, 0.475, 0.480, 0.485, 0.490, + 0.495, 0.500, 0.505, 0.510, 0.515, 0.520, 0.525, 0.530, 0.535, + 0.540, 0.545, 0.550, 0.555, 0.560, 0.565, 0.570, 0.575, 0.580, + 0.585, 0.590, 0.595, 0.600, 0.605, 0.610, 0.615, 0.620, 0.625, + 0.630, 0.635, 0.640, 0.645, 0.650, 0.655, 0.660, 0.665, 0.670, + 0.675, 0.680, 0.685, 0.690, 0.695, 0.700, 0.705, 0.710, 0.715, + 0.720, 0.725, 0.730, 0.735, 0.740, 0.745, 0.750, 0.755, 0.760, + 0.765, 0.770, 0.775, 0.780, 0.785, 0.790, 0.795, 0.800, 0.805, + 0.810, 0.815, 0.820, 0.825, 0.830, 0.835, 0.840, 0.845, 0.850, + 0.855, 0.860, 0.865, 0.870, 0.875, 0.880, 0.885, 0.890, 0.895, + 0.900, 0.905, 0.910, 0.915, 0.920, 0.925, 0.930, 0.935, 0.940, + 0.945, 0.950, 0.955, 0.960, 0.965, 0.970, 0.975, 0.980, 0.985, + 0.990, 0.995, 1.000 + }; + } + + /** + * Creates the default inverse cumulative probability density test expected + * values + */ + @Override + public int[] makeInverseCumulativeTestValues() { + return new int[] { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, + 5, 5, 6, 6, 6, 6, 7, 7, 8, 9, 10, Integer.MAX_VALUE + }; + } + + // ----------------- Additional test cases --------------------------------- + + @Test + public void testMoments() { + final double tol = 1e-9; + GeometricDistribution dist; + + dist = new GeometricDistribution(0.5); + Assert.assertEquals(dist.getNumericalMean(), (1.0d - 0.5d) / 0.5d, tol); + Assert.assertEquals(dist.getNumericalVariance(), (1.0d - 0.5d) / (0.5d * 0.5d), tol); + + dist = new GeometricDistribution(0.3); + Assert.assertEquals(dist.getNumericalMean(), (1.0d - 0.3d) / 0.3d, tol); + Assert.assertEquals(dist.getNumericalVariance(), (1.0d - 0.3d) / (0.3d * 0.3d), tol); + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GumbelDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GumbelDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GumbelDistributionTest.java new file mode 100644 index 0000000..7722942 --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/GumbelDistributionTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for GumbelDistribution. + */ +public class GumbelDistributionTest extends ContinuousDistributionAbstractTest { + + @Test + public void testParameters() { + GumbelDistribution d = makeDistribution(); + Assert.assertEquals(0.5, d.getLocation(), Precision.EPSILON); + Assert.assertEquals(2, d.getScale(), Precision.EPSILON); + } + + @Test + public void testSupport() { + GumbelDistribution d = makeDistribution(); + Assert.assertTrue(Double.isInfinite(d.getSupportLowerBound())); + Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound())); + Assert.assertTrue(d.isSupportConnected()); + } + + @Override + public GumbelDistribution makeDistribution() { + return new GumbelDistribution(0.5, 2); + } + + @Override + public double[] makeCumulativeTestPoints() { + return new double[] { + -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 + }; + } + + @Override + public double[] makeDensityTestValues() { + return new double[] { + 1.258262e-06, 3.594689e-04, 9.115766e-03, 5.321100e-02, 1.274352e-01, 1.777864e-01, + 1.787177e-01, 1.472662e-01, 1.075659e-01, 7.302736e-02, 4.742782e-02 + }; + } + + @Override + public double[] makeCumulativeTestValues() { + return new double[] { + 1.608760e-07, 7.577548e-05, 3.168165e-03, 3.049041e-02, 1.203923e-01, 2.769203e-01, + 4.589561e-01, 6.235249e-01, 7.508835e-01, 8.404869e-01, 8.999652e-01 + }; + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/HypergeometricDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/HypergeometricDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/HypergeometricDistributionTest.java new file mode 100644 index 0000000..95be226 --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/HypergeometricDistributionTest.java @@ -0,0 +1,335 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.apache.commons.rng.simple.RandomSource; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for HyperGeometriclDistribution. + * Extends DiscreteDistributionAbstractTest. See class javadoc for + * DiscreteDistributionAbstractTest for details. + * + */ +public class HypergeometricDistributionTest extends DiscreteDistributionAbstractTest { + + /** + * Constructor to override default tolerance. + */ + public HypergeometricDistributionTest() { + setTolerance(1e-12); + } + + //-------------- Implementations for abstract methods ----------------------- + + /** Creates the default discrete distribution instance to use in tests. */ + @Override + public DiscreteDistribution makeDistribution() { + return new HypergeometricDistribution(10, 5, 5); + } + + /** Creates the default probability density test input values */ + @Override + public int[] makeDensityTestPoints() { + return new int[] {-1, 0, 1, 2, 3, 4, 5, 10}; + } + + /** + * Creates the default probability density test expected values + * Reference values are from R, version 2.15.3. + */ + @Override + public double[] makeDensityTestValues() { + return new double[] {0d, 0.00396825396825, 0.0992063492063, 0.396825396825, 0.396825396825, + 0.0992063492063, 0.00396825396825, 0d}; + } + + /** + * Creates the default probability log density test expected values + * Reference values are from R, version 2.14.1. + */ + @Override + public double[] makeLogDensityTestValues() { + //-Inf -Inf + return new double[] {Double.NEGATIVE_INFINITY, -5.52942908751142, -2.31055326264322, -0.924258901523332, + -0.924258901523332, -2.31055326264322, -5.52942908751142, Double.NEGATIVE_INFINITY}; + } + + /** Creates the default cumulative probability density test input values */ + @Override + public int[] makeCumulativeTestPoints() { + return makeDensityTestPoints(); + } + + /** + * Creates the default cumulative probability density test expected values + * Reference values are from R, version 2.15.3. + */ + @Override + public double[] makeCumulativeTestValues() { + return new double[] {0d, 0.00396825396825, 0.103174603175, .5, 0.896825396825, 0.996031746032, + 1, 1}; + } + + /** Creates the default inverse cumulative probability test input values */ + @Override + public double[] makeInverseCumulativeTestPoints() { + return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d, + 0.990d, 0.975d, 0.950d, 0.900d, 1d}; + } + + /** Creates the default inverse cumulative probability density test expected values */ + @Override + public int[] makeInverseCumulativeTestValues() { + return new int[] {0, 0, 1, 1, 1, 1, 5, 4, 4, 4, 4, 5}; + } + + //-------------------- Additional test cases ------------------------------ + + /** Verify that if there are no failures, mass is concentrated on sampleSize */ + @Test + public void testDegenerateNoFailures() { + HypergeometricDistribution dist = new HypergeometricDistribution(5,5,3); + setDistribution(dist); + setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 }); + setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d}); + setDensityTestPoints(new int[] {-1, 0, 1, 3, 10}); + setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d}); + setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); + setInverseCumulativeTestValues(new int[] {3, 3}); + verifyDensities(); + verifyCumulativeProbabilities(); + verifyInverseCumulativeProbabilities(); + Assert.assertEquals(dist.getSupportLowerBound(), 3); + Assert.assertEquals(dist.getSupportUpperBound(), 3); + } + + /** Verify that if there are no successes, mass is concentrated on 0 */ + @Test + public void testDegenerateNoSuccesses() { + HypergeometricDistribution dist = new HypergeometricDistribution(5,0,3); + setDistribution(dist); + setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 }); + setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d}); + setDensityTestPoints(new int[] {-1, 0, 1, 3, 10}); + setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d}); + setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); + setInverseCumulativeTestValues(new int[] {0, 0}); + verifyDensities(); + verifyCumulativeProbabilities(); + verifyInverseCumulativeProbabilities(); + Assert.assertEquals(dist.getSupportLowerBound(), 0); + Assert.assertEquals(dist.getSupportUpperBound(), 0); + } + + /** Verify that if sampleSize = populationSize, mass is concentrated on numberOfSuccesses */ + @Test + public void testDegenerateFullSample() { + HypergeometricDistribution dist = new HypergeometricDistribution(5,3,5); + setDistribution(dist); + setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 }); + setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d}); + setDensityTestPoints(new int[] {-1, 0, 1, 3, 10}); + setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d}); + setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); + setInverseCumulativeTestValues(new int[] {3, 3}); + verifyDensities(); + verifyCumulativeProbabilities(); + verifyInverseCumulativeProbabilities(); + Assert.assertEquals(dist.getSupportLowerBound(), 3); + Assert.assertEquals(dist.getSupportUpperBound(), 3); + } + + @Test(expected=IllegalArgumentException.class) + public void testPrecondition1() { + new HypergeometricDistribution(0, 3, 5); + } + @Test(expected=IllegalArgumentException.class) + public void testPrecondition2() { + new HypergeometricDistribution(5, -1, 5); + } + @Test(expected=IllegalArgumentException.class) + public void testPrecondition3() { + new HypergeometricDistribution(5, 3, -1); + } + @Test(expected=IllegalArgumentException.class) + public void testPrecondition4() { + new HypergeometricDistribution(5, 6, 5); + } + @Test(expected=IllegalArgumentException.class) + public void testPrecondition5() { + new HypergeometricDistribution(5, 3, 6); + } + + @Test + public void testAccessors() { + HypergeometricDistribution dist = new HypergeometricDistribution(5, 3, 4); + Assert.assertEquals(5, dist.getPopulationSize()); + Assert.assertEquals(3, dist.getNumberOfSuccesses()); + Assert.assertEquals(4, dist.getSampleSize()); + } + + @Test + public void testLargeValues() { + int populationSize = 3456; + int sampleSize = 789; + int numberOfSucceses = 101; + double[][] data = { + {0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0}, + {1.0, 8.55705370142386e-11, 8.83269973602783e-11, 0.999999999997244}, + {2.0, 1.31288129219665e-9, 1.40120828955693e-9, 0.999999999911673}, + {3.0, 1.32724172984193e-8, 1.46736255879763e-8, 0.999999998598792}, + {4.0, 9.94501711734089e-8, 1.14123796761385e-7, 0.999999985326375}, + {5.0, 5.89080768883643e-7, 7.03204565645028e-7, 0.999999885876203}, + {20.0, 0.0760051397707708, 0.27349758476299, 0.802507555007781}, + {21.0, 0.087144222047629, 0.360641806810619, 0.72650241523701}, + {22.0, 0.0940378846881819, 0.454679691498801, 0.639358193189381}, + {23.0, 0.0956897500614809, 0.550369441560282, 0.545320308501199}, + {24.0, 0.0919766921922999, 0.642346133752582, 0.449630558439718}, + {25.0, 0.083641637261095, 0.725987771013677, 0.357653866247418}, + {96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57}, + {97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59}, + {98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61}, + {99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63}, + {100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65}, + {101.0, 8.78623800302957e-68, 1.0, 8.78623800302957e-68}, + }; + + testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data); + } + + private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) { + HypergeometricDistribution dist = new HypergeometricDistribution(populationSize, numberOfSucceses, sampleSize); + for (int i = 0; i < data.length; ++i) { + int x = (int)data[i][0]; + double pmf = data[i][1]; + double actualPmf = dist.probability(x); + TestUtils.assertRelativelyEquals("Expected equals for <"+x+"> pmf",pmf, actualPmf, 1.0e-9); + + double cdf = data[i][2]; + double actualCdf = dist.cumulativeProbability(x); + TestUtils.assertRelativelyEquals("Expected equals for <"+x+"> cdf",cdf, actualCdf, 1.0e-9); + + double cdf1 = data[i][3]; + double actualCdf1 = dist.upperCumulativeProbability(x); + TestUtils.assertRelativelyEquals("Expected equals for <"+x+"> cdf1",cdf1, actualCdf1, 1.0e-9); + } + } + + @Test + public void testMoreLargeValues() { + int populationSize = 26896; + int sampleSize = 895; + int numberOfSucceses = 55; + double[][] data = { + {0.0, 0.155168304750504, 0.155168304750504, 1.0}, + {1.0, 0.29437545000746, 0.449543754757964, 0.844831695249496}, + {2.0, 0.273841321577003, 0.723385076334967, 0.550456245242036}, + {3.0, 0.166488572570786, 0.889873648905753, 0.276614923665033}, + {4.0, 0.0743969744713231, 0.964270623377076, 0.110126351094247}, + {5.0, 0.0260542785784855, 0.990324901955562, 0.0357293766229237}, + {20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16}, + {21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17}, + {22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18}, + {23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20}, + {24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21}, + {25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23}, + {50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69}, + {51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71}, + {52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74}, + {53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76}, + {54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79}, + {55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82}, + }; + testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data); + } + + @Test + public void testMoments() { + final double tol = 1e-9; + HypergeometricDistribution dist; + + dist = new HypergeometricDistribution(1500, 40, 100); + Assert.assertEquals(dist.getNumericalMean(), 40d * 100d / 1500d, tol); + Assert.assertEquals(dist.getNumericalVariance(), ( 100d * 40d * (1500d - 100d) * (1500d - 40d) ) / ( (1500d * 1500d * 1499d) ), tol); + + dist = new HypergeometricDistribution(3000, 55, 200); + Assert.assertEquals(dist.getNumericalMean(), 55d * 200d / 3000d, tol); + Assert.assertEquals(dist.getNumericalVariance(), ( 200d * 55d * (3000d - 200d) * (3000d - 55d) ) / ( (3000d * 3000d * 2999d) ), tol); + } + + @Test + public void testMath644() { + int N = 14761461; // population + int m = 1035; // successes in population + int n = 1841; // number of trials + + int k = 0; + final HypergeometricDistribution dist = new HypergeometricDistribution(N, m, n); + + Assert.assertTrue(Precision.compareTo(1.0, dist.upperCumulativeProbability(k), 1) == 0); + Assert.assertTrue(Precision.compareTo(dist.cumulativeProbability(k), 0.0, 1) > 0); + + // another way to calculate the upper cumulative probability + double upper = 1.0 - dist.cumulativeProbability(k) + dist.probability(k); + Assert.assertTrue(Precision.compareTo(1.0, upper, 1) == 0); + } + + @Test + public void testZeroTrial() { + final int n = 11; // population + final int m = 4; // successes in population + final int s = 0; // number of trials + + final HypergeometricDistribution dist = new HypergeometricDistribution(n, m, 0); + + for (int i = 1; i <= n; i++) { + final double p = dist.probability(i); + Assert.assertEquals("p=" + p, 0, p, 0d); + } + } + + @Test + public void testMath1356() { + final int n = 11; // population + final int m = 11; // successes in population + + for (int s = 0; s <= n; s++) { + final HypergeometricDistribution dist = new HypergeometricDistribution(n, m, s); + final double p = dist.probability(s); + Assert.assertEquals("p=" + p, 1, p, 0d); + } + } + + @Test + public void testMath1021() { + final int N = 43130568; + final int m = 42976365; + final int n = 50; + final DiscreteDistribution.Sampler dist = + new HypergeometricDistribution(N, m, n).createSampler(RandomSource.create(RandomSource.WELL_512_A)); + + for (int i = 0; i < 100; i++) { + final int sample = dist.sample(); + Assert.assertTrue("sample=" + sample, 0 <= sample); + Assert.assertTrue("sample=" + sample, sample <= n); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LaplaceDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LaplaceDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LaplaceDistributionTest.java new file mode 100644 index 0000000..d935aca --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LaplaceDistributionTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for LaplaceDistribution. + */ +public class LaplaceDistributionTest extends ContinuousDistributionAbstractTest { + + @Test + public void testParameters() { + LaplaceDistribution d = makeDistribution(); + Assert.assertEquals(0, d.getLocation(), Precision.EPSILON); + Assert.assertEquals(1, d.getScale(), Precision.EPSILON); + } + + @Test + public void testSupport() { + LaplaceDistribution d = makeDistribution(); + Assert.assertTrue(Double.isInfinite(d.getSupportLowerBound())); + Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound())); + Assert.assertTrue(d.isSupportConnected()); + } + + @Override + public LaplaceDistribution makeDistribution() { + return new LaplaceDistribution(0, 1); + } + + @Override + public double[] makeCumulativeTestPoints() { + return new double[] { + -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 + }; + } + + @Override + public double[] makeDensityTestValues() { + return new double[] { + 0.003368973, 0.009157819, 0.024893534, 0.067667642, 0.183939721, + 0.500000000, 0.183939721, 0.067667642, 0.024893534, 0.009157819, 0.003368973 + }; + } + + @Override + public double[] makeCumulativeTestValues() { + return new double[] { + 0.003368973, 0.009157819, 0.024893534, 0.067667642, 0.183939721, + 0.500000000, 0.816060279, 0.932332358, 0.975106466, 0.990842181, 0.996631027 + }; + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LevyDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LevyDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LevyDistributionTest.java new file mode 100644 index 0000000..78b210c --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LevyDistributionTest.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.junit.Assert; +import org.junit.Test; + +public class LevyDistributionTest extends ContinuousDistributionAbstractTest { + + @Test + public void testParameters() { + LevyDistribution d = makeDistribution(); + Assert.assertEquals(1.2, d.getLocation(), Precision.EPSILON); + Assert.assertEquals(0.4, d.getScale(), Precision.EPSILON); + } + + @Test + public void testSupport() { + LevyDistribution d = makeDistribution(); + Assert.assertEquals(d.getLocation(), d.getSupportLowerBound(), Precision.EPSILON); + Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound())); + Assert.assertTrue(d.isSupportConnected()); + } + + @Override + public LevyDistribution makeDistribution() { + return new LevyDistribution(1.2, 0.4); + } + + @Override + public double[] makeCumulativeTestPoints() { + return new double[] { + 1.2001, 1.21, 1.225, 1.25, 1.3, 1.9, 3.4, 5.6 + }; + } + + @Override + public double[] makeCumulativeTestValues() { + // values computed with R and function plevy from rmutil package + return new double[] { + 0, 2.53962850749e-10, 6.33424836662e-05, 0.00467773498105, + 0.0455002638964, 0.449691797969, 0.669815357599, 0.763024600553 + }; + } + + @Override + public double[] makeDensityTestValues() { + // values computed with R and function dlevy from rmutil package + return new double[] { + 0, 5.20056373765e-07, 0.0214128361224, 0.413339707082, 1.07981933026, + 0.323749319161, 0.0706032550094, 0.026122839884 + }; + } + + /** + * Creates the default logarithmic probability density test expected values. + * Reference values are from R, version 2.14.1. + */ + @Override + public double[] makeLogDensityTestValues() { + return new double[] { + -1987.561573341398d, -14.469328620160d, -3.843764717971d, + -0.883485488811d, 0.076793740349d, -1.127785768948d, + -2.650679030597d, -3.644945255983d}; + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogNormalDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogNormalDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogNormalDistributionTest.java new file mode 100644 index 0000000..43b2fdd --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogNormalDistributionTest.java @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.statistics.distribution; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for {@link LogNormalDistribution}. Extends + * {@link ContinuousDistributionAbstractTest}. See class javadoc of that class + * for details. + */ +public class LogNormalDistributionTest extends ContinuousDistributionAbstractTest { + + //-------------- Implementations for abstract methods ----------------------- + + /** Creates the default real distribution instance to use in tests. */ + @Override + public LogNormalDistribution makeDistribution() { + return new LogNormalDistribution(2.1, 1.4); + } + + /** Creates the default cumulative probability distribution test input values */ + @Override + public double[] makeCumulativeTestPoints() { + // quantiles computed using R + return new double[] { -2.226325228634938, -1.156887023657177, + -0.643949578356075, -0.2027950777320613, + 0.305827808237559, 6.42632522863494, + 5.35688702365718, 4.843949578356074, + 4.40279507773206, 3.89417219176244 }; + } + + /** Creates the default cumulative probability density test expected values */ + @Override + public double[] makeCumulativeTestValues() { + return new double[] { 0, 0, 0, 0, 0.00948199951485, 0.432056525076, + 0.381648158697, 0.354555726206, 0.329513316888, + 0.298422824228 }; + } + + /** Creates the default probability density test expected values */ + @Override + public double[] makeDensityTestValues() { + return new double[] { 0, 0, 0, 0, 0.0594218160072, 0.0436977691036, + 0.0508364857798, 0.054873528325, 0.0587182664085, + 0.0636229042785 }; + } + + /** + * Creates the default inverse cumulative probability distribution test + * input values. + */ + @Override + public double[] makeInverseCumulativeTestPoints() { + // Exclude the test points less than zero, as they have cumulative + // probability of zero, meaning the inverse returns zero, and not the + // points less than zero. + double[] points = makeCumulativeTestValues(); + double[] points2 = new double[points.length - 4]; + System.arraycopy(points, 4, points2, 0, points2.length - 4); + return points2; + //return Arrays.copyOfRange(points, 4, points.length - 4); + } + + /** + * Creates the default inverse cumulative probability test expected + * values. + */ + @Override + public double[] makeInverseCumulativeTestValues() { + // Exclude the test points less than zero, as they have cumulative + // probability of zero, meaning the inverse returns zero, and not the + // points less than zero. + double[] points = makeCumulativeTestPoints(); + double[] points2 = new double[points.length - 4]; + System.arraycopy(points, 4, points2, 0, points2.length - 4); + return points2; + //return Arrays.copyOfRange(points, 1, points.length - 4); + } + + // --------------------- Override tolerance -------------- + @Override + public void setUp() { + super.setUp(); + setTolerance(1e-7); + } + + //---------------------------- Additional test cases ------------------------- + + private void verifyQuantiles() { + LogNormalDistribution distribution = (LogNormalDistribution)getDistribution(); + double mu = distribution.getScale(); + double sigma = distribution.getShape(); + setCumulativeTestPoints( new double[] { mu - 2 *sigma, mu - sigma, + mu, mu + sigma, mu + 2 * sigma, + mu + 3 * sigma,mu + 4 * sigma, + mu + 5 * sigma }); + verifyCumulativeProbabilities(); + } + + @Test + public void testQuantiles() { + setCumulativeTestValues(new double[] {0, 0.0396495152787, + 0.16601209243, 0.272533253269, + 0.357618409638, 0.426488363093, + 0.483255136841, 0.530823013877}); + setDensityTestValues(new double[] {0, 0.0873055825147, 0.0847676303432, + 0.0677935186237, 0.0544105523058, + 0.0444614628804, 0.0369750288945, + 0.0312206409653}); + verifyQuantiles(); + verifyDensities(); + + setDistribution(new LogNormalDistribution(0, 1)); + setCumulativeTestValues(new double[] {0, 0, 0, 0.5, 0.755891404214, + 0.864031392359, 0.917171480998, + 0.946239689548}); + setDensityTestValues(new double[] {0, 0, 0, 0.398942280401, + 0.156874019279, 0.07272825614, + 0.0381534565119, 0.0218507148303}); + verifyQuantiles(); + verifyDensities(); + + setDistribution(new LogNormalDistribution(0, 0.1)); + setCumulativeTestValues(new double[] {0, 0, 0, 1.28417563064e-117, + 1.39679883412e-58, + 1.09839325447e-33, + 2.52587961726e-20, + 2.0824223487e-12}); + setDensityTestValues(new double[] {0, 0, 0, 2.96247992535e-114, + 1.1283370232e-55, 4.43812313223e-31, + 5.85346445002e-18, + 2.9446618076e-10}); + verifyQuantiles(); + verifyDensities(); + } + + @Test + public void testInverseCumulativeProbabilityExtremes() { + setInverseCumulativeTestPoints(new double[] {0, 1}); + setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); + verifyInverseCumulativeProbabilities(); + } + + @Test + public void testGetScale() { + LogNormalDistribution distribution = (LogNormalDistribution)getDistribution(); + Assert.assertEquals(2.1, distribution.getScale(), 0); + } + + @Test + public void testGetShape() { + LogNormalDistribution distribution = (LogNormalDistribution)getDistribution(); + Assert.assertEquals(1.4, distribution.getShape(), 0); + } + + @Test(expected=IllegalArgumentException.class) + public void testPrecondition1() { + new LogNormalDistribution(1, 0); + } + + @Test + public void testDensity() { + double [] x = new double[]{-2, -1, 0, 1, 2}; + // R 2.13: print(dlnorm(c(-2,-1,0,1,2)), digits=10) + checkDensity(0, 1, x, new double[] { 0.0000000000, 0.0000000000, + 0.0000000000, 0.3989422804, + 0.1568740193 }); + // R 2.13: print(dlnorm(c(-2,-1,0,1,2), mean=1.1), digits=10) + checkDensity(1.1, 1, x, new double[] { 0.0000000000, 0.0000000000, + 0.0000000000, 0.2178521770, + 0.1836267118}); + } + + private void checkDensity(double scale, + double shape, + double[] x, + double[] expected) { + LogNormalDistribution d = new LogNormalDistribution(scale, shape); + for (int i = 0; i < x.length; i++) { + Assert.assertEquals(expected[i], d.density(x[i]), 1e-9); + } + } + + /** + * Check to make sure top-coding of extreme values works correctly. + * Verifies fixes for JIRA MATH-167, MATH-414 + */ + @Test + public void testExtremeValues() { + LogNormalDistribution d = new LogNormalDistribution(0, 1); + for (int i = 0; i < 1e5; i++) { // make sure no convergence exception + double upperTail = d.cumulativeProbability(i); + if (i <= 72) { // make sure not top-coded + Assert.assertTrue(upperTail < 1.0d); + } + else { // make sure top coding not reversed + Assert.assertTrue(upperTail > 0.99999); + } + } + + Assert.assertEquals(d.cumulativeProbability(Double.MAX_VALUE), 1, 0); + Assert.assertEquals(d.cumulativeProbability(-Double.MAX_VALUE), 0, 0); + Assert.assertEquals(d.cumulativeProbability(Double.POSITIVE_INFINITY), 1, 0); + Assert.assertEquals(d.cumulativeProbability(Double.NEGATIVE_INFINITY), 0, 0); + } + + @Test + public void testMeanVariance() { + final double tol = 1e-9; + LogNormalDistribution dist; + + dist = new LogNormalDistribution(0, 1); + Assert.assertEquals(dist.getNumericalMean(), 1.6487212707001282, tol); + Assert.assertEquals(dist.getNumericalVariance(), + 4.670774270471604, tol); + + dist = new LogNormalDistribution(2.2, 1.4); + Assert.assertEquals(dist.getNumericalMean(), 24.046753552064498, tol); + Assert.assertEquals(dist.getNumericalVariance(), + 3526.913651880464, tol); + + dist = new LogNormalDistribution(-2000.9, 10.4); + Assert.assertEquals(dist.getNumericalMean(), 0.0, tol); + Assert.assertEquals(dist.getNumericalVariance(), 0.0, tol); + } + + @Test + public void testTinyVariance() { + LogNormalDistribution dist = new LogNormalDistribution(0, 1e-9); + double t = dist.getNumericalVariance(); + Assert.assertEquals(1e-18, t, 1e-20); + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogisticsDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogisticsDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogisticsDistributionTest.java new file mode 100644 index 0000000..cfdf6d8 --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/LogisticsDistributionTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for LogisticsDistribution. + */ +public class LogisticsDistributionTest extends ContinuousDistributionAbstractTest { + + @Test + public void testParameters() { + LogisticDistribution d = makeDistribution(); + Assert.assertEquals(2, d.getLocation(), Precision.EPSILON); + Assert.assertEquals(5, d.getScale(), Precision.EPSILON); + } + + @Test + public void testSupport() { + LogisticDistribution d = makeDistribution(); + Assert.assertTrue(Double.isInfinite(d.getSupportLowerBound())); + Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound())); + Assert.assertTrue(d.isSupportConnected()); + } + + @Override + public LogisticDistribution makeDistribution() { + return new LogisticDistribution(2, 5); + } + + @Override + public double[] makeCumulativeTestPoints() { + return new double[] { + -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 + }; + } + + @Override + public double[] makeDensityTestValues() { + return new double[] { + 0.03173698, 0.03557889, 0.03932239, 0.04278194, 0.04575685, 0.04805215, + 0.04950331, 0.05000000, 0.04950331, 0.04805215, 0.04575685 + }; + } + + @Override + public double[] makeCumulativeTestValues() { + return new double[] { + 0.1978161, 0.2314752, 0.2689414, 0.3100255, 0.3543437, 0.4013123, + 0.4501660, 0.5000000, 0.5498340, 0.5986877, 0.6456563 + }; + } +} http://git-wip-us.apache.org/repos/asf/commons-statistics/blob/9c794a15/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/NakagamiDistributionTest.java ---------------------------------------------------------------------- diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/NakagamiDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/NakagamiDistributionTest.java new file mode 100644 index 0000000..6aab2dd --- /dev/null +++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/NakagamiDistributionTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.statistics.distribution; + +import org.apache.commons.numbers.core.Precision; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test cases for NakagamiDistribution. + */ +public class NakagamiDistributionTest extends ContinuousDistributionAbstractTest { + + @Test + public void testParameters() { + NakagamiDistribution d = makeDistribution(); + Assert.assertEquals(0.5, d.getShape(), Precision.EPSILON); + Assert.assertEquals(1, d.getScale(), Precision.EPSILON); + } + + @Test + public void testSupport() { + NakagamiDistribution d = makeDistribution(); + Assert.assertEquals(d.getSupportLowerBound(), 0, Precision.EPSILON); + Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound())); + Assert.assertTrue(d.isSupportConnected()); + } + + @Override + public NakagamiDistribution makeDistribution() { + return new NakagamiDistribution(0.5, 1); + } + + @Override + public double[] makeCumulativeTestPoints() { + return new double[] { + 0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2 + }; + } + + @Override + public double[] makeDensityTestValues() { + return new double[] { + 0.0000000, 0.7820854, 0.7365403, 0.6664492, 0.5793831, 0.4839414, + 0.3883721, 0.2994549, 0.2218417, 0.1579003, 0.1079819 + }; + } + + @Override + public double[] makeCumulativeTestValues() { + return new double[] { + 0.0000000, 0.1585194, 0.3108435, 0.4514938, 0.5762892, 0.6826895, + 0.7698607, 0.8384867, 0.8904014, 0.9281394, 0.9544997 + }; + } +}
