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.continuous;
24  
25  import static org.junit.Assert.assertEquals;
26  
27  import org.junit.Test;
28  
29  /**
30   * Test cases for {@link ChiSquaredDistribution}.
31   *
32   * @see RealDistributionAbstractTest
33   */
34  public class ChiSquaredDistributionTest extends RealDistributionAbstractTest {
35  
36      //-------------- Implementations for abstract methods -----------------------
37  
38      /** Creates the default continuous distribution instance to use in tests. */
39      @Override
40      public ChiSquaredDistribution makeDistribution() {
41          return new ChiSquaredDistribution(5.0);
42      }
43  
44      /** Creates the default cumulative probability distribution test input values */
45      @Override
46      public double[] makeCumulativeTestPoints() {
47          // quantiles computed using R version 2.9.2
48          return new double[] {
49              0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696,
50              20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978
51          };
52      }
53  
54      /** Creates the default cumulative probability density test expected values */
55      @Override
56      public double[] makeCumulativeTestValues() {
57          return new double[] { 0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900 };
58      }
59  
60      /** Creates the default inverse cumulative probability test input values */
61      @Override
62      public double[] makeInverseCumulativeTestPoints() {
63          return new double[] { 0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1 };
64      }
65  
66      /** Creates the default inverse cumulative probability density test expected values */
67      @Override
68      public double[] makeInverseCumulativeTestValues() {
69          return new double[] {
70              0, 0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, 20.5150056524,
71              15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978, Double.POSITIVE_INFINITY
72          };
73      }
74  
75      /** Creates the default probability density test expected values */
76      @Override
77      public double[] makeDensityTestValues() {
78          return new double[] {
79              0.0115379817652, 0.0415948507811, 0.0665060119842, 0.0919455953114, 0.121472591024,
80              0.000433630076361, 0.00412780610309, 0.00999340341045, 0.0193246438937, 0.0368460089216
81          };
82      }
83  
84   // --------------------- Override tolerance  --------------
85      @Override
86      public void setUp() {
87          super.setUp();
88          setTolerance(1e-9);
89      }
90  
91   //---------------------------- Additional test cases -------------------------
92  
93      @Test
94      public void testSmallDf() {
95          setDistribution(new ChiSquaredDistribution(0.1d));
96          setTolerance(1E-4);
97          // quantiles computed using R version 1.8.1 (linux version)
98          setCumulativeTestPoints(new double[] {
99              1.168926E-60, 1.168926E-40, 1.063132E-32, 1.144775E-26, 1.168926E-20,
100             5.472917, 2.175255, 1.13438, 0.5318646, 0.1526342
101         });
102         setInverseCumulativeTestValues(getCumulativeTestPoints());
103         setInverseCumulativeTestPoints(getCumulativeTestValues());
104         verifyCumulativeProbabilities();
105         verifyInverseCumulativeProbabilities();
106     }
107 
108     @Test
109     public void testDfAccessors() {
110         ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution();
111         assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
112     }
113 
114     @Test
115     public void testDensity() {
116         double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5};
117         //R 2.5: print(dchisq(x, df=1), digits=10)
118         checkDensity(1, x, new double[] {
119             0.00000000000, 398.94208093034, 0.43939128947, 0.24197072452, 0.10377687436, 0.01464498256
120         });
121         //R 2.5: print(dchisq(x, df=0.1), digits=10)
122         checkDensity(0.1, x, new double[] {
123             0.000000000e+00, 2.486453997e+04, 7.464238732e-02, 3.009077718e-02, 9.447299159e-03, 8.827199396e-04
124         });
125         //R 2.5: print(dchisq(x, df=2), digits=10)
126         checkDensity(2, x, new double[] {
127             0.00000000000, 0.49999975000, 0.38940039154, 0.30326532986, 0.18393972059, 0.04104249931
128         });
129         //R 2.5: print(dchisq(x, df=10), digits=10)
130         checkDensity(10, x, new double[] {
131             0.000000000e+00, 1.302082682e-27, 6.337896998e-05, 7.897534632e-04, 7.664155024e-03, 6.680094289e-02
132         });
133     }
134 
135     private void checkDensity(double df, double[] x, double[] expected) {
136         ChiSquaredDistribution d = new ChiSquaredDistribution(df);
137         for (int i = 0; i < x.length; i++) {
138             assertEquals(expected[i], d.density(x[i]), 1e-5);
139         }
140     }
141 
142     @Test
143     public void testMoments() {
144         final double tol = 1e-9;
145         ChiSquaredDistribution dist;
146 
147         dist = new ChiSquaredDistribution(1500);
148         assertEquals(dist.getNumericalMean(), 1500, tol);
149         assertEquals(dist.getNumericalVariance(), 3000, tol);
150 
151         dist = new ChiSquaredDistribution(1.12);
152         assertEquals(dist.getNumericalMean(), 1.12, tol);
153         assertEquals(dist.getNumericalVariance(), 2.24, tol);
154     }
155 }