001: /*
002: * Copyright 2003-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.distribution;
018:
019: import org.apache.commons.math.TestUtils;
020:
021: /**
022: * Test cases for HyperGeometriclDistribution.
023: * Extends IntegerDistributionAbstractTest. See class javadoc for
024: * IntegerDistributionAbstractTest for details.
025: *
026: * @version $Revision: 348888 $ $Date: 2005-11-24 23:21:25 -0700 (Thu, 24 Nov 2005) $
027: */
028: public class HypergeometricDistributionTest extends
029: IntegerDistributionAbstractTest {
030:
031: /**
032: * Constructor for ChiSquareDistributionTest.
033: * @param name
034: */
035: public HypergeometricDistributionTest(String name) {
036: super (name);
037: }
038:
039: //-------------- Implementations for abstract methods -----------------------
040:
041: /** Creates the default discrete distribution instance to use in tests. */
042: public IntegerDistribution makeDistribution() {
043: return DistributionFactory.newInstance()
044: .createHypergeometricDistribution(10, 5, 5);
045: }
046:
047: /** Creates the default probability density test input values */
048: public int[] makeDensityTestPoints() {
049: return new int[] { -1, 0, 1, 2, 3, 4, 5, 10 };
050: }
051:
052: /** Creates the default probability density test expected values */
053: public double[] makeDensityTestValues() {
054: return new double[] { 0d, 0.003968d, 0.099206d, 0.396825d,
055: 0.396825d, 0.099206d, 0.003968d, 0d };
056: }
057:
058: /** Creates the default cumulative probability density test input values */
059: public int[] makeCumulativeTestPoints() {
060: return makeDensityTestPoints();
061: }
062:
063: /** Creates the default cumulative probability density test expected values */
064: public double[] makeCumulativeTestValues() {
065: return new double[] { 0d, .003968d, .103175d, .50000d,
066: .896825d, .996032d, 1.00000d, 1d };
067: }
068:
069: /** Creates the default inverse cumulative probability test input values */
070: public double[] makeInverseCumulativeTestPoints() {
071: return new double[] { 0d, 0.001d, 0.010d, 0.025d, 0.050d,
072: 0.100d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1d };
073: }
074:
075: /** Creates the default inverse cumulative probability density test expected values */
076: public int[] makeInverseCumulativeTestValues() {
077: return new int[] { -1, -1, 0, 0, 0, 0, 4, 3, 3, 3, 3, 5 };
078: }
079:
080: //-------------------- Additional test cases ------------------------------
081:
082: /** Verify that if there are no failures, mass is concentrated on sampleSize */
083: public void testDegenerateNoFailures() throws Exception {
084: setDistribution(DistributionFactory.newInstance()
085: .createHypergeometricDistribution(5, 5, 3));
086: setCumulativeTestPoints(new int[] { -1, 0, 1, 3, 10 });
087: setCumulativeTestValues(new double[] { 0d, 0d, 0d, 1d, 1d });
088: setDensityTestPoints(new int[] { -1, 0, 1, 3, 10 });
089: setDensityTestValues(new double[] { 0d, 0d, 0d, 1d, 0d });
090: setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
091: setInverseCumulativeTestValues(new int[] { 2, 2 });
092: verifyDensities();
093: verifyCumulativeProbabilities();
094: verifyInverseCumulativeProbabilities();
095: }
096:
097: /** Verify that if there are no successes, mass is concentrated on 0 */
098: public void testDegenerateNoSuccesses() throws Exception {
099: setDistribution(DistributionFactory.newInstance()
100: .createHypergeometricDistribution(5, 0, 3));
101: setCumulativeTestPoints(new int[] { -1, 0, 1, 3, 10 });
102: setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d });
103: setDensityTestPoints(new int[] { -1, 0, 1, 3, 10 });
104: setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d });
105: setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
106: setInverseCumulativeTestValues(new int[] { -1, -1 });
107: verifyDensities();
108: verifyCumulativeProbabilities();
109: verifyInverseCumulativeProbabilities();
110: }
111:
112: /** Verify that if sampleSize = populationSize, mass is concentrated on numberOfSuccesses */
113: public void testDegenerateFullSample() throws Exception {
114: setDistribution(DistributionFactory.newInstance()
115: .createHypergeometricDistribution(5, 3, 5));
116: setCumulativeTestPoints(new int[] { -1, 0, 1, 3, 10 });
117: setCumulativeTestValues(new double[] { 0d, 0d, 0d, 1d, 1d });
118: setDensityTestPoints(new int[] { -1, 0, 1, 3, 10 });
119: setDensityTestValues(new double[] { 0d, 0d, 0d, 1d, 0d });
120: setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d });
121: setInverseCumulativeTestValues(new int[] { 2, 2 });
122: verifyDensities();
123: verifyCumulativeProbabilities();
124: verifyInverseCumulativeProbabilities();
125: }
126:
127: public void testPopulationSize() {
128: HypergeometricDistribution dist = DistributionFactory
129: .newInstance()
130: .createHypergeometricDistribution(5, 3, 5);
131: try {
132: dist.setPopulationSize(-1);
133: fail("negative population size. IllegalArgumentException expected");
134: } catch (IllegalArgumentException ex) {
135: }
136:
137: dist.setPopulationSize(10);
138: assertEquals(10, dist.getPopulationSize());
139: }
140:
141: public void testLargeValues() {
142: int populationSize = 3456;
143: int sampleSize = 789;
144: int numberOfSucceses = 101;
145: double[][] data = {
146: { 0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0 },
147: { 1.0, 8.55705370142386e-11, 8.83269973602783e-11,
148: 0.999999999997244 },
149: { 2.0, 1.31288129219665e-9, 1.40120828955693e-9,
150: 0.999999999911673 },
151: { 3.0, 1.32724172984193e-8, 1.46736255879763e-8,
152: 0.999999998598792 },
153: { 4.0, 9.94501711734089e-8, 1.14123796761385e-7,
154: 0.999999985326375 },
155: { 5.0, 5.89080768883643e-7, 7.03204565645028e-7,
156: 0.999999885876203 },
157: { 20.0, 0.0760051397707708, 0.27349758476299,
158: 0.802507555007781 },
159: { 21.0, 0.087144222047629, 0.360641806810619,
160: 0.72650241523701 },
161: { 22.0, 0.0940378846881819, 0.454679691498801,
162: 0.639358193189381 },
163: { 23.0, 0.0956897500614809, 0.550369441560282,
164: 0.545320308501199 },
165: { 24.0, 0.0919766921922999, 0.642346133752582,
166: 0.449630558439718 },
167: { 25.0, 0.083641637261095, 0.725987771013677,
168: 0.357653866247418 },
169: { 96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57 },
170: { 97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59 },
171: { 98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61 },
172: { 99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63 },
173: { 100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65 },
174: { 101.0, 8.78623800302957e-68, 1.0,
175: 8.78623800302957e-68 }, };
176:
177: testHypergeometricDistributionProbabilities(populationSize,
178: sampleSize, numberOfSucceses, data);
179: }
180:
181: private void testHypergeometricDistributionProbabilities(
182: int populationSize, int sampleSize, int numberOfSucceses,
183: double[][] data) {
184: HypergeometricDistributionImpl dist = new HypergeometricDistributionImpl(
185: populationSize, numberOfSucceses, sampleSize);
186: for (int i = 0; i < data.length; ++i) {
187: int x = (int) data[i][0];
188: double pdf = data[i][1];
189: double actualPdf = dist.probability(x);
190: TestUtils.assertRelativelyEquals(pdf, actualPdf, 1.0e-9);
191:
192: double cdf = data[i][2];
193: double actualCdf = dist.cumulativeProbability(x);
194: TestUtils.assertRelativelyEquals(cdf, actualCdf, 1.0e-9);
195:
196: double cdf1 = data[i][3];
197: double actualCdf1 = dist.upperCumulativeProbability(x);
198: TestUtils.assertRelativelyEquals(cdf1, actualCdf1, 1.0e-9);
199: }
200: }
201:
202: public void testMoreLargeValues() {
203: int populationSize = 26896;
204: int sampleSize = 895;
205: int numberOfSucceses = 55;
206: double[][] data = {
207: { 0.0, 0.155168304750504, 0.155168304750504, 1.0 },
208: { 1.0, 0.29437545000746, 0.449543754757964,
209: 0.844831695249496 },
210: { 2.0, 0.273841321577003, 0.723385076334967,
211: 0.550456245242036 },
212: { 3.0, 0.166488572570786, 0.889873648905753,
213: 0.276614923665033 },
214: { 4.0, 0.0743969744713231, 0.964270623377076,
215: 0.110126351094247 },
216: { 5.0, 0.0260542785784855, 0.990324901955562,
217: 0.0357293766229237 },
218: { 20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16 },
219: { 21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17 },
220: { 22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18 },
221: { 23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20 },
222: { 24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21 },
223: { 25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23 },
224: { 50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69 },
225: { 51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71 },
226: { 52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74 },
227: { 53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76 },
228: { 54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79 },
229: { 55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82 }, };
230: testHypergeometricDistributionProbabilities(populationSize,
231: sampleSize, numberOfSucceses, data);
232: }
233: }
|