1   /*
2    * Copyright 2003-2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.math.distribution;
18  
19  import org.apache.commons.math.TestUtils;
20  
21  /**
22   * Test cases for HyperGeometriclDistribution.
23   * Extends IntegerDistributionAbstractTest.  See class javadoc for
24   * IntegerDistributionAbstractTest for details.
25   * 
26   * @version $Revision: 348888 $ $Date: 2005-11-24 23:21:25 -0700 (Thu, 24 Nov 2005) $
27   */
28  public class HypergeometricDistributionTest extends IntegerDistributionAbstractTest {
29  
30      /**
31       * Constructor for ChiSquareDistributionTest.
32       * @param name
33       */
34      public HypergeometricDistributionTest(String name) {
35          super(name);
36      }
37  
38  //-------------- Implementations for abstract methods -----------------------
39      
40      /** Creates the default discrete distribution instance to use in tests. */
41      public IntegerDistribution makeDistribution() {
42          return DistributionFactory.newInstance().createHypergeometricDistribution(10,5, 5);
43      }
44      
45      /** Creates the default probability density test input values */
46      public int[] makeDensityTestPoints() {
47          return new int[] {-1, 0, 1, 2, 3, 4, 5, 10};
48      }
49      
50      /** Creates the default probability density test expected values */
51      public double[] makeDensityTestValues() {
52          return new double[] {0d, 0.003968d, 0.099206d, 0.396825d, 0.396825d, 
53                  0.099206d, 0.003968d, 0d};
54      }
55      
56      /** Creates the default cumulative probability density test input values */
57      public int[] makeCumulativeTestPoints() {
58          return makeDensityTestPoints();
59      }
60      
61      /** Creates the default cumulative probability density test expected values */
62      public double[] makeCumulativeTestValues() {
63          return new double[] {0d, .003968d, .103175d, .50000d, .896825d, .996032d,
64                  1.00000d, 1d};
65      }
66      
67      /** Creates the default inverse cumulative probability test input values */
68      public double[] makeInverseCumulativeTestPoints() {
69          return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d,
70                  0.990d, 0.975d, 0.950d, 0.900d, 1d}; 
71      }
72      
73      /** Creates the default inverse cumulative probability density test expected values */
74      public int[] makeInverseCumulativeTestValues() {
75          return new int[] {-1, -1, 0, 0, 0, 0, 4, 3, 3, 3, 3, 5};
76      }
77      
78      //-------------------- Additional test cases ------------------------------
79      
80      /** Verify that if there are no failures, mass is concentrated on sampleSize */
81      public void testDegenerateNoFailures() throws Exception {
82          setDistribution(DistributionFactory.newInstance().createHypergeometricDistribution(5,5,3));
83          setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
84          setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
85          setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
86          setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
87          setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
88          setInverseCumulativeTestValues(new int[] {2, 2});
89          verifyDensities();
90          verifyCumulativeProbabilities();
91          verifyInverseCumulativeProbabilities();     
92      }
93      
94      /** Verify that if there are no successes, mass is concentrated on 0 */
95      public void testDegenerateNoSuccesses() throws Exception {
96          setDistribution(DistributionFactory.newInstance().createHypergeometricDistribution(5,0,3));
97          setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
98          setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d});
99          setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
100         setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d});
101         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
102         setInverseCumulativeTestValues(new int[] {-1, -1});
103         verifyDensities();
104         verifyCumulativeProbabilities();
105         verifyInverseCumulativeProbabilities();     
106     }
107     
108     /** Verify that if sampleSize = populationSize, mass is concentrated on numberOfSuccesses */
109     public void testDegenerateFullSample() throws Exception {
110         setDistribution(DistributionFactory.newInstance().createHypergeometricDistribution(5,3,5));
111         setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
112         setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
113         setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
114         setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
115         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
116         setInverseCumulativeTestValues(new int[] {2, 2});
117         verifyDensities();
118         verifyCumulativeProbabilities();
119         verifyInverseCumulativeProbabilities();     
120     }
121 
122     public void testPopulationSize() {
123         HypergeometricDistribution dist = DistributionFactory.newInstance().createHypergeometricDistribution(5,3,5);
124         try {
125             dist.setPopulationSize(-1);
126             fail("negative population size.  IllegalArgumentException expected");
127         } catch(IllegalArgumentException ex) {
128         }
129         
130         dist.setPopulationSize(10);
131         assertEquals(10, dist.getPopulationSize());
132     }
133     
134     public void testLargeValues() {
135         int populationSize = 3456;
136         int sampleSize = 789;
137         int numberOfSucceses = 101;
138         double[][] data = {
139             {0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0},
140             {1.0, 8.55705370142386e-11, 8.83269973602783e-11, 0.999999999997244},
141             {2.0, 1.31288129219665e-9, 1.40120828955693e-9, 0.999999999911673},
142             {3.0, 1.32724172984193e-8, 1.46736255879763e-8, 0.999999998598792},
143             {4.0, 9.94501711734089e-8, 1.14123796761385e-7, 0.999999985326375},
144             {5.0, 5.89080768883643e-7, 7.03204565645028e-7, 0.999999885876203},
145             {20.0, 0.0760051397707708, 0.27349758476299, 0.802507555007781}, 
146             {21.0, 0.087144222047629, 0.360641806810619, 0.72650241523701}, 
147             {22.0, 0.0940378846881819, 0.454679691498801, 0.639358193189381}, 
148             {23.0, 0.0956897500614809, 0.550369441560282, 0.545320308501199}, 
149             {24.0, 0.0919766921922999, 0.642346133752582, 0.449630558439718}, 
150             {25.0, 0.083641637261095, 0.725987771013677, 0.357653866247418}, 
151             {96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57},
152             {97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59}, 
153             {98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61},
154             {99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63}, 
155             {100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65},
156             {101.0, 8.78623800302957e-68, 1.0, 8.78623800302957e-68},
157         };
158         
159         testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
160     }
161 
162     private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) {
163         HypergeometricDistributionImpl dist = new HypergeometricDistributionImpl(populationSize, numberOfSucceses, sampleSize);
164         for (int i = 0; i < data.length; ++i) {
165             int x = (int)data[i][0];
166             double pdf = data[i][1];
167             double actualPdf = dist.probability(x);
168             TestUtils.assertRelativelyEquals(pdf, actualPdf, 1.0e-9);
169 
170             double cdf = data[i][2];
171             double actualCdf = dist.cumulativeProbability(x);
172             TestUtils.assertRelativelyEquals(cdf, actualCdf, 1.0e-9);
173 
174             double cdf1 = data[i][3];
175             double actualCdf1 = dist.upperCumulativeProbability(x);
176             TestUtils.assertRelativelyEquals(cdf1, actualCdf1, 1.0e-9);
177         }
178     }
179     
180     public void testMoreLargeValues() {
181         int populationSize = 26896;
182         int sampleSize = 895;
183         int numberOfSucceses = 55;
184         double[][] data = {
185             {0.0, 0.155168304750504, 0.155168304750504, 1.0}, 
186             {1.0, 0.29437545000746, 0.449543754757964, 0.844831695249496}, 
187             {2.0, 0.273841321577003, 0.723385076334967, 0.550456245242036}, 
188             {3.0, 0.166488572570786, 0.889873648905753, 0.276614923665033}, 
189             {4.0, 0.0743969744713231, 0.964270623377076, 0.110126351094247}, 
190             {5.0, 0.0260542785784855, 0.990324901955562, 0.0357293766229237}, 
191             {20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16}, 
192             {21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17}, 
193             {22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18}, 
194             {23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20}, 
195             {24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21}, 
196             {25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23}, 
197             {50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69}, 
198             {51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71}, 
199             {52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74}, 
200             {53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76}, 
201             {54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79}, 
202             {55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82},        
203         };
204         testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
205     }
206 }