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