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