ChiSquaredDistribution.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.distribution.continuous;

  22. /**
  23.  * Implementation of the chi-squared distribution.
  24.  *
  25.  * @see <a href="http://en.wikipedia.org/wiki/Chi-squared_distribution">Chi-squared distribution (Wikipedia)</a>
  26.  * @see <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">Chi-squared Distribution (MathWorld)</a>
  27.  */
  28. public class ChiSquaredDistribution extends AbstractRealDistribution {
  29.     /** Serializable version identifier */
  30.     private static final long serialVersionUID = 20160320L;
  31.     /** Internal Gamma distribution. */
  32.     private final GammaDistribution gamma;

  33.     /**
  34.      * Create a Chi-Squared distribution with the given degrees of freedom.
  35.      *
  36.      * @param degreesOfFreedom Degrees of freedom.
  37.      */
  38.     public ChiSquaredDistribution(double degreesOfFreedom) {
  39.         this(degreesOfFreedom, DEFAULT_SOLVER_ABSOLUTE_ACCURACY);
  40.     }

  41.     /**
  42.      * Create a Chi-Squared distribution with the given degrees of freedom and
  43.      * inverse cumulative probability accuracy.
  44.      *
  45.      * @param degreesOfFreedom Degrees of freedom.
  46.      * @param inverseCumAccuracy the maximum absolute error in inverse
  47.      * cumulative probability estimates (defaults to
  48.      * {@link #DEFAULT_SOLVER_ABSOLUTE_ACCURACY}).
  49.      */
  50.     public ChiSquaredDistribution(double degreesOfFreedom,
  51.                                   double inverseCumAccuracy) {
  52.         super(inverseCumAccuracy);

  53.         gamma = new GammaDistribution(degreesOfFreedom / 2, 2);
  54.     }

  55.     /**
  56.      * Access the number of degrees of freedom.
  57.      *
  58.      * @return the degrees of freedom.
  59.      */
  60.     public double getDegreesOfFreedom() {
  61.         return gamma.getShape() * 2.0;
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public double density(double x) {
  66.         return gamma.density(x);
  67.     }

  68.     /** {@inheritDoc} **/
  69.     @Override
  70.     public double logDensity(double x) {
  71.         return gamma.logDensity(x);
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public double cumulativeProbability(double x)  {
  76.         return gamma.cumulativeProbability(x);
  77.     }

  78.     /**
  79.      * {@inheritDoc}
  80.      *
  81.      * For {@code k} degrees of freedom, the mean is {@code k}.
  82.      */
  83.     @Override
  84.     public double getNumericalMean() {
  85.         return getDegreesOfFreedom();
  86.     }

  87.     /**
  88.      * {@inheritDoc}
  89.      *
  90.      * @return {@code 2 * k}, where {@code k} is the number of degrees of freedom.
  91.      */
  92.     @Override
  93.     public double getNumericalVariance() {
  94.         return 2 * getDegreesOfFreedom();
  95.     }

  96.     /**
  97.      * {@inheritDoc}
  98.      *
  99.      * The lower bound of the support is always 0 no matter the
  100.      * degrees of freedom.
  101.      *
  102.      * @return zero.
  103.      */
  104.     @Override
  105.     public double getSupportLowerBound() {
  106.         return 0;
  107.     }

  108.     /**
  109.      * {@inheritDoc}
  110.      *
  111.      * The upper bound of the support is always positive infinity no matter the
  112.      * degrees of freedom.
  113.      *
  114.      * @return {@code Double.POSITIVE_INFINITY}.
  115.      */
  116.     @Override
  117.     public double getSupportUpperBound() {
  118.         return Double.POSITIVE_INFINITY;
  119.     }

  120.     /**
  121.      * {@inheritDoc}
  122.      *
  123.      * The support of this distribution is connected.
  124.      *
  125.      * @return {@code true}
  126.      */
  127.     @Override
  128.     public boolean isSupportConnected() {
  129.         return true;
  130.     }
  131. }