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