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 this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.hipparchus.distribution.discrete;
15  
16  import org.hipparchus.distribution.IntegerDistribution;
17  import org.junit.Assert;
18  import org.junit.Test;
19  
20  /**
21   * Test cases for GeometricDistribution.
22   */
23  public class GeometricDistributionTest extends IntegerDistributionAbstractTest {
24  
25      /**
26       * Constructor to override default tolerance.
27       */
28      public GeometricDistributionTest() {
29          setTolerance(1e-12);
30      }
31  
32      // -------------- Implementations for abstract methods --------------------
33  
34      /** Creates the default discrete distribution instance to use in tests. */
35      @Override
36      public IntegerDistribution makeDistribution() {
37          return new GeometricDistribution(0.40);
38      }
39  
40      /** Creates the default probability density test input values */
41      @Override
42      public int[] makeDensityTestPoints() {
43          return new int[] { -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,
44                              9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
45                             19, 20, 21, 22, 23, 24, 25, 26, 27, 28 };
46      }
47  
48      /**
49       * Creates the default probability density test expected values.
50       * Reference values are from R, version version 2.15.3.
51       */
52      @Override
53      public double[] makeDensityTestValues() {
54          return new double[] {
55              0d, 0.4, 0.24, 0.144, 0.0864, 0.05184, 0.031104, 0.0186624,
56              0.01119744, 0.006718464, 0.0040310784, 0.00241864704,
57              0.001451188224,0.0008707129344, 0.00052242776064, 0.000313456656384,
58              0.00018807399383, 0.000112844396298, 6.77066377789e-05, 4.06239826674e-05,
59              2.43743896004e-05, 1.46246337603e-05, 8.77478025615e-06, 5.26486815369e-06,
60              3.15892089221e-06, 1.89535253533e-06, 1.1372115212e-06, 6.82326912718e-07,
61              4.09396147631e-07, 2.45637688579e-07
62          };
63      }
64  
65      /**
66       * Creates the default log probability density test expected values.
67       * Reference values are from R, version version 2.14.1.
68       */
69      @Override
70      public double[] makeLogDensityTestValues() {
71          return new double[] {
72              Double.NEGATIVE_INFINITY, -0.916290731874155, -1.42711635564015, -1.93794197940614,
73              -2.44876760317213, -2.95959322693812, -3.47041885070411, -3.9812444744701,
74              -4.49207009823609, -5.00289572200208, -5.51372134576807, -6.02454696953406,
75              -6.53537259330005, -7.04619821706604, -7.55702384083203, -8.06784946459802,
76              -8.57867508836402, -9.08950071213001, -9.600326335896, -10.111151959662,
77              -10.621977583428, -11.132803207194, -11.64362883096, -12.154454454726,
78              -12.6652800784919, -13.1761057022579, -13.6869313260239, -14.1977569497899,
79              -14.7085825735559, -15.2194081973219
80          };
81      }
82  
83      /** Creates the default cumulative probability density test input values */
84      @Override
85      public int[] makeCumulativeTestPoints() {
86          return makeDensityTestPoints();
87      }
88  
89      /** Creates the default cumulative probability density test expected values */
90      @Override
91      public double[] makeCumulativeTestValues() {
92          final double[] densities = makeDensityTestValues();
93          final int n = densities.length;
94          final double[] ret = new double[n];
95          ret[0] = densities[0];
96          for (int i = 1; i < n; i++) {
97              ret[i] = ret[i - 1] + densities[i];
98          }
99          return ret;
100     }
101 
102     /** Creates the default inverse cumulative probability test input values */
103     @Override
104     public double[] makeInverseCumulativeTestPoints() {
105         return new double[] {
106             0.000, 0.005, 0.010, 0.015, 0.020, 0.025, 0.030, 0.035, 0.040,
107             0.045, 0.050, 0.055, 0.060, 0.065, 0.070, 0.075, 0.080, 0.085,
108             0.090, 0.095, 0.100, 0.105, 0.110, 0.115, 0.120, 0.125, 0.130,
109             0.135, 0.140, 0.145, 0.150, 0.155, 0.160, 0.165, 0.170, 0.175,
110             0.180, 0.185, 0.190, 0.195, 0.200, 0.205, 0.210, 0.215, 0.220,
111             0.225, 0.230, 0.235, 0.240, 0.245, 0.250, 0.255, 0.260, 0.265,
112             0.270, 0.275, 0.280, 0.285, 0.290, 0.295, 0.300, 0.305, 0.310,
113             0.315, 0.320, 0.325, 0.330, 0.335, 0.340, 0.345, 0.350, 0.355,
114             0.360, 0.365, 0.370, 0.375, 0.380, 0.385, 0.390, 0.395, 0.400,
115             0.405, 0.410, 0.415, 0.420, 0.425, 0.430, 0.435, 0.440, 0.445,
116             0.450, 0.455, 0.460, 0.465, 0.470, 0.475, 0.480, 0.485, 0.490,
117             0.495, 0.500, 0.505, 0.510, 0.515, 0.520, 0.525, 0.530, 0.535,
118             0.540, 0.545, 0.550, 0.555, 0.560, 0.565, 0.570, 0.575, 0.580,
119             0.585, 0.590, 0.595, 0.600, 0.605, 0.610, 0.615, 0.620, 0.625,
120             0.630, 0.635, 0.640, 0.645, 0.650, 0.655, 0.660, 0.665, 0.670,
121             0.675, 0.680, 0.685, 0.690, 0.695, 0.700, 0.705, 0.710, 0.715,
122             0.720, 0.725, 0.730, 0.735, 0.740, 0.745, 0.750, 0.755, 0.760,
123             0.765, 0.770, 0.775, 0.780, 0.785, 0.790, 0.795, 0.800, 0.805,
124             0.810, 0.815, 0.820, 0.825, 0.830, 0.835, 0.840, 0.845, 0.850,
125             0.855, 0.860, 0.865, 0.870, 0.875, 0.880, 0.885, 0.890, 0.895,
126             0.900, 0.905, 0.910, 0.915, 0.920, 0.925, 0.930, 0.935, 0.940,
127             0.945, 0.950, 0.955, 0.960, 0.965, 0.970, 0.975, 0.980, 0.985,
128             0.990, 0.995, 1.000
129         };
130     }
131 
132     /**
133      * Creates the default inverse cumulative probability density test expected
134      * values
135      */
136     @Override
137     public int[] makeInverseCumulativeTestValues() {
138         return new int[] {
139             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
143             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
144             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
145             1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
146             2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
147             3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
148             5, 5, 6, 6, 6, 6, 7, 7, 8, 9, 10, Integer.MAX_VALUE
149         };
150     }
151 
152     // ----------------- Additional test cases ---------------------------------
153 
154     @Test
155     public void testMoments() {
156         final double tol = 1e-9;
157         GeometricDistribution dist;
158 
159         dist = new GeometricDistribution(0.5);
160         Assert.assertEquals(dist.getNumericalMean(), (1.0d - 0.5d) / 0.5d, tol);
161         Assert.assertEquals(dist.getNumericalVariance(), (1.0d - 0.5d) / (0.5d * 0.5d), tol);
162 
163         dist = new GeometricDistribution(0.3);
164         Assert.assertEquals(dist.getNumericalMean(), (1.0d - 0.3d) / 0.3d, tol);
165         Assert.assertEquals(dist.getNumericalVariance(), (1.0d - 0.3d) / (0.3d * 0.3d), tol);
166     }
167 }