View Javadoc
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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.distribution.discrete;
23  
24  import org.hipparchus.distribution.IntegerDistribution;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Test cases for PascalDistribution.
30   */
31  public class PascalDistributionTest extends IntegerDistributionAbstractTest {
32  
33      // --------------------- Override tolerance  --------------
34      protected double defaultTolerance = 1e-9;
35      @Override
36      public void setUp() {
37          super.setUp();
38          setTolerance(defaultTolerance);
39      }
40  
41      //-------------- Implementations for abstract methods -----------------------
42  
43      /** Creates the default discrete distribution instance to use in tests. */
44      @Override
45      public IntegerDistribution makeDistribution() {
46          return new PascalDistribution(10,0.70);
47      }
48  
49      /** Creates the default probability density test input values */
50      @Override
51      public int[] makeDensityTestPoints() {
52        return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
53      }
54  
55      /** Creates the default probability density test expected values */
56      @Override
57      public double[] makeDensityTestValues() {
58        return new double[] {0, 0.0282475249, 0.0847425747, 0.139825248255, 0.167790297906, 0.163595540458,
59                0.137420253985, 0.103065190489, 0.070673273478, 0.0450542118422, 0.0270325271053,
60                0.0154085404500, 0.0084046584273};
61      }
62  
63      /** Creates the default cumulative probability density test input values */
64      @Override
65      public int[] makeCumulativeTestPoints() {
66        return makeDensityTestPoints();
67      }
68  
69      /** Creates the default cumulative probability density test expected values */
70      @Override
71      public double[] makeCumulativeTestValues() {
72        return new double[] {0, 0.0282475249, 0.1129900996, 0.252815347855, 0.420605645761, 0.584201186219,
73                0.721621440204, 0.824686630693, 0.895359904171, 0.940414116013, 0.967446643119,
74                0.982855183569, 0.991259841996};
75          }
76  
77      /** Creates the default inverse cumulative probability test input values */
78      @Override
79      public double[] makeInverseCumulativeTestPoints() {
80        return new double[] {0.0, 0.001, 0.010, 0.025, 0.050, 0.100, 0.999,
81            0.990, 0.975, 0.950, 0.900, 1.0};
82          }
83  
84      /** Creates the default inverse cumulative probability density test expected values */
85      @Override
86      public int[] makeInverseCumulativeTestValues() {
87        return new int[] {0, 0, 0, 0, 1, 1, 14, 11, 10, 9, 8, Integer.MAX_VALUE};
88      }
89  
90      //----------------- Additional test cases ---------------------------------
91  
92      /** Test degenerate case p = 0   */
93      @Test
94      public void testDegenerate0() {
95          setDistribution(new PascalDistribution(5, 0.0d));
96          setCumulativeTestPoints(new int[] {-1, 0, 1, 5, 10 });
97          setCumulativeTestValues(new double[] {0d, 0d, 0d, 0d, 0d});
98          setDensityTestPoints(new int[] {-1, 0, 1, 10, 11});
99          setDensityTestValues(new double[] {0d, 0d, 0d, 0d, 0d});
100         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
101         setInverseCumulativeTestValues(new int[] {Integer.MAX_VALUE, Integer.MAX_VALUE});
102         verifyDensities();
103         verifyCumulativeProbabilities();
104         verifyInverseCumulativeProbabilities();
105     }
106 
107     /** Test degenerate case p = 1   */
108     @Test
109     public void testDegenerate1() {
110         setDistribution(new PascalDistribution(5, 1.0d));
111         setCumulativeTestPoints(new int[] {-1, 0, 1, 2, 5, 10 });
112         setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d, 1d});
113         setDensityTestPoints(new int[] {-1, 0, 1, 2, 5, 10});
114         setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d, 0d});
115         setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d});
116         setInverseCumulativeTestValues(new int[] {0, 0});
117         verifyDensities();
118         verifyCumulativeProbabilities();
119         verifyInverseCumulativeProbabilities();
120     }
121 
122     @Test
123     public void testMoments() {
124         final double tol = 1e-9;
125         PascalDistribution dist;
126 
127         dist = new PascalDistribution(10, 0.5);
128         Assert.assertEquals(dist.getNumericalMean(), ( 10d * 0.5d ) / 0.5d, tol);
129         Assert.assertEquals(dist.getNumericalVariance(), ( 10d * 0.5d ) / (0.5d * 0.5d), tol);
130 
131         dist = new PascalDistribution(25, 0.7);
132         Assert.assertEquals(dist.getNumericalMean(), ( 25d * 0.3d ) / 0.7d, tol);
133         Assert.assertEquals(dist.getNumericalVariance(), ( 25d * 0.3d ) / (0.7d * 0.7d), tol);
134     }
135 }