1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math.distribution;
19  
20  import org.apache.commons.math.TestUtils;
21  
22  /**
23   * Test cases for HyperGeometriclDistribution.
24   * Extends IntegerDistributionAbstractTest.  See class javadoc for
25   * IntegerDistributionAbstractTest for details.
26   * 
27   * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
28   */
29  public class HypergeometricDistributionTest extends IntegerDistributionAbstractTest {
30  
31      /**
32       * Constructor for ChiSquareDistributionTest.
33       * @param name
34       */
35      public HypergeometricDistributionTest(String name) {
36          super(name);
37      }
38  
39  //-------------- Implementations for abstract methods -----------------------
40      
41      /** Creates the default discrete distribution instance to use in tests. */
42      @Override
43      public IntegerDistribution makeDistribution() {
44          return new HypergeometricDistributionImpl(10,5, 5);
45      }
46      
47      /** Creates the default probability density test input values */
48      @Override
49      public int[] makeDensityTestPoints() {
50          return new int[] {-1, 0, 1, 2, 3, 4, 5, 10};
51      }
52      
53      /** Creates the default probability density test expected values */
54      @Override
55      public double[] makeDensityTestValues() {
56          return new double[] {0d, 0.003968d, 0.099206d, 0.396825d, 0.396825d, 
57                  0.099206d, 0.003968d, 0d};
58      }
59      
60      /** Creates the default cumulative probability density test input values */
61      @Override
62      public int[] makeCumulativeTestPoints() {
63          return makeDensityTestPoints();
64      }
65      
66      /** Creates the default cumulative probability density test expected values */
67      @Override
68      public double[] makeCumulativeTestValues() {
69          return new double[] {0d, .003968d, .103175d, .50000d, .896825d, .996032d,
70                  1.00000d, 1d};
71      }
72      
73      /** Creates the default inverse cumulative probability test input values */
74      @Override
75      public double[] makeInverseCumulativeTestPoints() {
76          return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d,
77                  0.990d, 0.975d, 0.950d, 0.900d, 1d}; 
78      }
79      
80      /** Creates the default inverse cumulative probability density test expected values */
81      @Override
82      public int[] makeInverseCumulativeTestValues() {
83          return new int[] {-1, -1, 0, 0, 0, 0, 4, 3, 3, 3, 3, 5};
84      }
85      
86      //-------------------- Additional test cases ------------------------------
87      
88      /** Verify that if there are no failures, mass is concentrated on sampleSize */
89      public void testDegenerateNoFailures() throws Exception {
90          setDistribution(new HypergeometricDistributionImpl(5,5,3));
91          setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
92          setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
93          setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
94          setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
95          setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
96          setInverseCumulativeTestValues(new int[] {2, 2});
97          verifyDensities();
98          verifyCumulativeProbabilities();
99          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 }