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  
23  package org.hipparchus.distribution.discrete;
24  
25  import org.hipparchus.distribution.IntegerDistribution;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.util.FastMath;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertThrows;
32  
33  /**
34   * Test cases for {@link ZipfDistribution}.
35   */
36  public class ZipfDistributionTest extends IntegerDistributionAbstractTest {
37  
38      /**
39       * Constructor to override default tolerance.
40       */
41      public ZipfDistributionTest() {
42          setTolerance(1e-12);
43      }
44  
45      @Test
46      void testPreconditions1() {
47          assertThrows(MathIllegalArgumentException.class, () -> {
48              new ZipfDistribution(0, 1);
49          });
50      }
51  
52      @Test
53      void testPreconditions2() {
54          assertThrows(MathIllegalArgumentException.class, () -> {
55              new ZipfDistribution(1, 0);
56          });
57      }
58  
59      //-------------- Implementations for abstract methods -----------------------
60  
61      /** Creates the default discrete distribution instance to use in tests. */
62      @Override
63      public IntegerDistribution makeDistribution() {
64          return new ZipfDistribution(10, 1);
65      }
66  
67      /** Creates the default probability density test input values */
68      @Override
69      public int[] makeDensityTestPoints() {
70          return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
71      }
72  
73      /**
74       * Creates the default probability density test expected values.
75       * Reference values are from R, version 2.15.3 (VGAM package 0.9-0).
76       */
77      @Override
78      public double[] makeDensityTestValues() {
79          return new double[] {0d, 0d, 0.341417152147, 0.170708576074, 0.113805717382, 0.0853542880369, 0.0682834304295,
80              0.0569028586912, 0.0487738788782, 0.0426771440184, 0.0379352391275, 0.0341417152147, 0};
81      }
82  
83      /**
84       * Creates the default logarithmic probability density test expected values.
85       * Reference values are from R, version 2.14.1.
86       */
87      @Override
88      public double[] makeLogDensityTestValues() {
89          return new double[] {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY,
90              -1.07465022926458, -1.76779740982453, -2.17326251793269, -2.46094459038447,
91              -2.68408814169868, -2.86640969849264, -3.0205603783199, -3.15409177094442,
92              -3.2718748066008, -3.37723532225863, Double.NEGATIVE_INFINITY};
93      }
94  
95      /** Creates the default cumulative probability density test input values */
96      @Override
97      public int[] makeCumulativeTestPoints() {
98          return makeDensityTestPoints();
99      }
100 
101     /** Creates the default cumulative probability density test expected values */
102     @Override
103     public double[] makeCumulativeTestValues() {
104         return new double[] {0, 0, 0.341417152147, 0.512125728221, 0.625931445604, 0.71128573364,
105             0.77956916407, 0.836472022761, 0.885245901639, 0.927923045658, 0.965858284785, 1d, 1d};
106         }
107 
108     /** Creates the default inverse cumulative probability test input values */
109     @Override
110     public double[] makeInverseCumulativeTestPoints() {
111         return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.3413d, 0.3415d, 0.999d,
112                 0.990d, 0.975d, 0.950d, 0.900d, 1d};
113         }
114 
115     /** Creates the default inverse cumulative probability density test expected values */
116     @Override
117     public int[] makeInverseCumulativeTestValues() {
118         return new int[] {1, 1, 1, 1, 1, 1, 2, 10, 10, 10, 9, 8, 10};
119     }
120 
121     @Test
122     void testMoments() {
123         final double tol = 1e-9;
124         ZipfDistribution dist;
125 
126         dist = new ZipfDistribution(2, 0.5);
127         assertEquals(dist.getNumericalMean(), FastMath.sqrt(2), tol);
128         assertEquals(0.24264068711928521, dist.getNumericalVariance(), tol);
129     }
130 
131 }