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  package org.hipparchus.distribution.continuous;
23  
24  /**
25   * Implementation of the chi-squared distribution.
26   *
27   * @see <a href="http://en.wikipedia.org/wiki/Chi-squared_distribution">Chi-squared distribution (Wikipedia)</a>
28   * @see <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">Chi-squared Distribution (MathWorld)</a>
29   */
30  public class ChiSquaredDistribution extends AbstractRealDistribution {
31      /** Serializable version identifier */
32      private static final long serialVersionUID = 20160320L;
33      /** Internal Gamma distribution. */
34      private final GammaDistribution gamma;
35  
36      /**
37       * Create a Chi-Squared distribution with the given degrees of freedom.
38       *
39       * @param degreesOfFreedom Degrees of freedom.
40       */
41      public ChiSquaredDistribution(double degreesOfFreedom) {
42          this(degreesOfFreedom, DEFAULT_SOLVER_ABSOLUTE_ACCURACY);
43      }
44  
45      /**
46       * Create a Chi-Squared distribution with the given degrees of freedom and
47       * inverse cumulative probability accuracy.
48       *
49       * @param degreesOfFreedom Degrees of freedom.
50       * @param inverseCumAccuracy the maximum absolute error in inverse
51       * cumulative probability estimates (defaults to
52       * {@link #DEFAULT_SOLVER_ABSOLUTE_ACCURACY}).
53       */
54      public ChiSquaredDistribution(double degreesOfFreedom,
55                                    double inverseCumAccuracy) {
56          super(inverseCumAccuracy);
57  
58          gamma = new GammaDistribution(degreesOfFreedom / 2, 2);
59      }
60  
61      /**
62       * Access the number of degrees of freedom.
63       *
64       * @return the degrees of freedom.
65       */
66      public double getDegreesOfFreedom() {
67          return gamma.getShape() * 2.0;
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public double density(double x) {
73          return gamma.density(x);
74      }
75  
76      /** {@inheritDoc} **/
77      @Override
78      public double logDensity(double x) {
79          return gamma.logDensity(x);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public double cumulativeProbability(double x)  {
85          return gamma.cumulativeProbability(x);
86      }
87  
88      /**
89       * {@inheritDoc}
90       *
91       * For {@code k} degrees of freedom, the mean is {@code k}.
92       */
93      @Override
94      public double getNumericalMean() {
95          return getDegreesOfFreedom();
96      }
97  
98      /**
99       * {@inheritDoc}
100      *
101      * @return {@code 2 * k}, where {@code k} is the number of degrees of freedom.
102      */
103     @Override
104     public double getNumericalVariance() {
105         return 2 * getDegreesOfFreedom();
106     }
107 
108     /**
109      * {@inheritDoc}
110      *
111      * The lower bound of the support is always 0 no matter the
112      * degrees of freedom.
113      *
114      * @return zero.
115      */
116     @Override
117     public double getSupportLowerBound() {
118         return 0;
119     }
120 
121     /**
122      * {@inheritDoc}
123      *
124      * The upper bound of the support is always positive infinity no matter the
125      * degrees of freedom.
126      *
127      * @return {@code Double.POSITIVE_INFINITY}.
128      */
129     @Override
130     public double getSupportUpperBound() {
131         return Double.POSITIVE_INFINITY;
132     }
133 
134     /**
135      * {@inheritDoc}
136      *
137      * The support of this distribution is connected.
138      *
139      * @return {@code true}
140      */
141     @Override
142     public boolean isSupportConnected() {
143         return true;
144     }
145 }