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