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: 545192 $ $Date: 2007-06-07 07:35:04 -0700 (Thu, 07 Jun 2007) $
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      public IntegerDistribution makeDistribution() {
43          return new HypergeometricDistributionImpl(10,5, 5);
44      }
45      
46      /** Creates the default probability density test input values */
47      public int[] makeDensityTestPoints() {
48          return new int[] {-1, 0, 1, 2, 3, 4, 5, 10};
49      }
50      
51      /** Creates the default probability density test expected values */
52      public double[] makeDensityTestValues() {
53          return new double[] {0d, 0.003968d, 0.099206d, 0.396825d, 0.396825d, 
54                  0.099206d, 0.003968d, 0d};
55      }
56      
57      /** Creates the default cumulative probability density test input values */
58      public int[] makeCumulativeTestPoints() {
59          return makeDensityTestPoints();
60      }
61      
62      /** Creates the default cumulative probability density test expected values */
63      public double[] makeCumulativeTestValues() {
64          return new double[] {0d, .003968d, .103175d, .50000d, .896825d, .996032d,
65                  1.00000d, 1d};
66      }
67      
68      /** Creates the default inverse cumulative probability test input values */
69      public double[] makeInverseCumulativeTestPoints() {
70          return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d,
71                  0.990d, 0.975d, 0.950d, 0.900d, 1d}; 
72      }
73      
74      /** Creates the default inverse cumulative probability density test expected values */
75      public int[] makeInverseCumulativeTestValues() {
76          return new int[] {-1, -1, 0, 0, 0, 0, 4, 3, 3, 3, 3, 5};
77      }
78      
79      //-------------------- Additional test cases ------------------------------
80      
81      /** Verify that if there are no failures, mass is concentrated on sampleSize */
82      public void testDegenerateNoFailures() throws Exception {
83          setDistribution(new HypergeometricDistributionImpl(5,5,3));
84          setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
85          setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
86          setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
87          setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
88          setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
89          setInverseCumulativeTestValues(new int[] {2, 2});
90          verifyDensities();
91          verifyCumulativeProbabilities();
92          verifyInverseCumulativeProbabilities();     
93      }
94      
95      /** Verify that if there are no successes, mass is concentrated on 0 */
96      public void testDegenerateNoSuccesses() throws Exception {
97          setDistribution(new HypergeometricDistributionImpl(5,0,3));
98          setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
99          setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d});
100         setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
101         setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d});
102         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
103         setInverseCumulativeTestValues(new int[] {-1, -1});
104         verifyDensities();
105         verifyCumulativeProbabilities();
106         verifyInverseCumulativeProbabilities();     
107     }
108     
109     /** Verify that if sampleSize = populationSize, mass is concentrated on numberOfSuccesses */
110     public void testDegenerateFullSample() throws Exception {
111         setDistribution(new HypergeometricDistributionImpl(5,3,5));
112         setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
113         setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
114         setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
115         setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d});
116         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
117         setInverseCumulativeTestValues(new int[] {2, 2});
118         verifyDensities();
119         verifyCumulativeProbabilities();
120         verifyInverseCumulativeProbabilities();     
121     }
122 
123     public void testPopulationSize() {
124         HypergeometricDistribution dist = new HypergeometricDistributionImpl(5,3,5);
125         try {
126             dist.setPopulationSize(-1);
127             fail("negative population size.  IllegalArgumentException expected");
128         } catch(IllegalArgumentException ex) {
129         }
130         
131         dist.setPopulationSize(10);
132         assertEquals(10, dist.getPopulationSize());
133     }
134     
135     public void testLargeValues() {
136         int populationSize = 3456;
137         int sampleSize = 789;
138         int numberOfSucceses = 101;
139         double[][] data = {
140             {0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0},
141             {1.0, 8.55705370142386e-11, 8.83269973602783e-11, 0.999999999997244},
142             {2.0, 1.31288129219665e-9, 1.40120828955693e-9, 0.999999999911673},
143             {3.0, 1.32724172984193e-8, 1.46736255879763e-8, 0.999999998598792},
144             {4.0, 9.94501711734089e-8, 1.14123796761385e-7, 0.999999985326375},
145             {5.0, 5.89080768883643e-7, 7.03204565645028e-7, 0.999999885876203},
146             {20.0, 0.0760051397707708, 0.27349758476299, 0.802507555007781}, 
147             {21.0, 0.087144222047629, 0.360641806810619, 0.72650241523701}, 
148             {22.0, 0.0940378846881819, 0.454679691498801, 0.639358193189381}, 
149             {23.0, 0.0956897500614809, 0.550369441560282, 0.545320308501199}, 
150             {24.0, 0.0919766921922999, 0.642346133752582, 0.449630558439718}, 
151             {25.0, 0.083641637261095, 0.725987771013677, 0.357653866247418}, 
152             {96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57},
153             {97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59}, 
154             {98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61},
155             {99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63}, 
156             {100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65},
157             {101.0, 8.78623800302957e-68, 1.0, 8.78623800302957e-68},
158         };
159         
160         testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
161     }
162 
163     private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) {
164         HypergeometricDistributionImpl dist = new HypergeometricDistributionImpl(populationSize, numberOfSucceses, sampleSize);
165         for (int i = 0; i < data.length; ++i) {
166             int x = (int)data[i][0];
167             double pdf = data[i][1];
168             double actualPdf = dist.probability(x);
169             TestUtils.assertRelativelyEquals(pdf, actualPdf, 1.0e-9);
170 
171             double cdf = data[i][2];
172             double actualCdf = dist.cumulativeProbability(x);
173             TestUtils.assertRelativelyEquals(cdf, actualCdf, 1.0e-9);
174 
175             double cdf1 = data[i][3];
176             double actualCdf1 = dist.upperCumulativeProbability(x);
177             TestUtils.assertRelativelyEquals(cdf1, actualCdf1, 1.0e-9);
178         }
179     }
180     
181     public void testMoreLargeValues() {
182         int populationSize = 26896;
183         int sampleSize = 895;
184         int numberOfSucceses = 55;
185         double[][] data = {
186             {0.0, 0.155168304750504, 0.155168304750504, 1.0}, 
187             {1.0, 0.29437545000746, 0.449543754757964, 0.844831695249496}, 
188             {2.0, 0.273841321577003, 0.723385076334967, 0.550456245242036}, 
189             {3.0, 0.166488572570786, 0.889873648905753, 0.276614923665033}, 
190             {4.0, 0.0743969744713231, 0.964270623377076, 0.110126351094247}, 
191             {5.0, 0.0260542785784855, 0.990324901955562, 0.0357293766229237}, 
192             {20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16}, 
193             {21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17}, 
194             {22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18}, 
195             {23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20}, 
196             {24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21}, 
197             {25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23}, 
198             {50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69}, 
199             {51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71}, 
200             {52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74}, 
201             {53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76}, 
202             {54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79}, 
203             {55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82},        
204         };
205         testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data);
206     }
207 }