1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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  
35  
36  public class ZipfDistributionTest extends IntegerDistributionAbstractTest {
37  
38      
39  
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      
60  
61      
62      @Override
63      public IntegerDistribution makeDistribution() {
64          return new ZipfDistribution(10, 1);
65      }
66  
67      
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  
75  
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  
85  
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      
96      @Override
97      public int[] makeCumulativeTestPoints() {
98          return makeDensityTestPoints();
99      }
100 
101     
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     
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     
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 }