TDistribution.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. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.special.Beta;
  25. import org.hipparchus.special.Gamma;
  26. import org.hipparchus.util.FastMath;

  27. /**
  28.  * Implementation of Student's t-distribution.
  29.  */
  30. public class TDistribution extends AbstractRealDistribution {
  31.     /** Serializable version identifier */
  32.     private static final long serialVersionUID = 20160320L;
  33.     /** The degrees of freedom. */
  34.     private final double degreesOfFreedom;
  35.     /** Static computation factor based on degreesOfFreedom. */
  36.     private final double factor;

  37.     /**
  38.      * Create a t distribution using the given degrees of freedom.
  39.      *
  40.      * @param degreesOfFreedom Degrees of freedom.
  41.      * @throws MathIllegalArgumentException if {@code degreesOfFreedom <= 0}
  42.      */
  43.     public TDistribution(double degreesOfFreedom)
  44.         throws MathIllegalArgumentException {
  45.         this(degreesOfFreedom, DEFAULT_SOLVER_ABSOLUTE_ACCURACY);
  46.     }

  47.     /**
  48.      * Create a t distribution using the given degrees of freedom and the
  49.      * specified inverse cumulative probability absolute accuracy.
  50.      *
  51.      * @param degreesOfFreedom Degrees of freedom.
  52.      * @param inverseCumAccuracy the maximum absolute error in inverse
  53.      * cumulative probability estimates
  54.      * (defaults to {@link #DEFAULT_SOLVER_ABSOLUTE_ACCURACY}).
  55.      * @throws MathIllegalArgumentException if {@code degreesOfFreedom <= 0}
  56.      */
  57.     public TDistribution(double degreesOfFreedom, double inverseCumAccuracy)
  58.         throws MathIllegalArgumentException {
  59.         super(inverseCumAccuracy);

  60.         if (degreesOfFreedom <= 0) {
  61.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DEGREES_OF_FREEDOM,
  62.                                                    degreesOfFreedom);
  63.         }
  64.         this.degreesOfFreedom = degreesOfFreedom;

  65.         final double n = degreesOfFreedom;
  66.         final double nPlus1Over2 = (n + 1) / 2;
  67.         factor = Gamma.logGamma(nPlus1Over2) -
  68.                  0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) -
  69.                  Gamma.logGamma(n / 2);
  70.     }

  71.     /**
  72.      * Access the degrees of freedom.
  73.      *
  74.      * @return the degrees of freedom.
  75.      */
  76.     public double getDegreesOfFreedom() {
  77.         return degreesOfFreedom;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public double density(double x) {
  82.         return FastMath.exp(logDensity(x));
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public double logDensity(double x) {
  87.         final double n = degreesOfFreedom;
  88.         final double nPlus1Over2 = (n + 1) / 2;
  89.         return factor - nPlus1Over2 * FastMath.log(1 + x * x / n);
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public double cumulativeProbability(double x) {
  94.         double ret;
  95.         if (x == 0) {
  96.             ret = 0.5;
  97.         } else {
  98.             double t =
  99.                 Beta.regularizedBeta(
  100.                     degreesOfFreedom / (degreesOfFreedom + (x * x)),
  101.                     0.5 * degreesOfFreedom,
  102.                     0.5);
  103.             if (x < 0.0) {
  104.                 ret = 0.5 * t;
  105.             } else {
  106.                 ret = 1.0 - 0.5 * t;
  107.             }
  108.         }

  109.         return ret;
  110.     }

  111.     /**
  112.      * {@inheritDoc}
  113.      *
  114.      * For degrees of freedom parameter {@code df}, the mean is
  115.      * <ul>
  116.      *  <li>if {@code df > 1} then {@code 0},</li>
  117.      * <li>else undefined ({@code Double.NaN}).</li>
  118.      * </ul>
  119.      */
  120.     @Override
  121.     public double getNumericalMean() {
  122.         final double df = getDegreesOfFreedom();

  123.         if (df > 1) {
  124.             return 0;
  125.         }

  126.         return Double.NaN;
  127.     }

  128.     /**
  129.      * {@inheritDoc}
  130.      *
  131.      * For degrees of freedom parameter {@code df}, the variance is
  132.      * <ul>
  133.      *  <li>if {@code df > 2} then {@code df / (df - 2)},</li>
  134.      *  <li>if {@code 1 < df <= 2} then positive infinity
  135.      *  ({@code Double.POSITIVE_INFINITY}),</li>
  136.      *  <li>else undefined ({@code Double.NaN}).</li>
  137.      * </ul>
  138.      */
  139.     @Override
  140.     public double getNumericalVariance() {
  141.         final double df = getDegreesOfFreedom();

  142.         if (df > 2) {
  143.             return df / (df - 2);
  144.         }

  145.         if (df > 1 && df <= 2) {
  146.             return Double.POSITIVE_INFINITY;
  147.         }

  148.         return Double.NaN;
  149.     }

  150.     /**
  151.      * {@inheritDoc}
  152.      *
  153.      * The lower bound of the support is always negative infinity no matter the
  154.      * parameters.
  155.      *
  156.      * @return lower bound of the support (always
  157.      * {@code Double.NEGATIVE_INFINITY})
  158.      */
  159.     @Override
  160.     public double getSupportLowerBound() {
  161.         return Double.NEGATIVE_INFINITY;
  162.     }

  163.     /**
  164.      * {@inheritDoc}
  165.      *
  166.      * The upper bound of the support is always positive infinity no matter the
  167.      * parameters.
  168.      *
  169.      * @return upper bound of the support (always
  170.      * {@code Double.POSITIVE_INFINITY})
  171.      */
  172.     @Override
  173.     public double getSupportUpperBound() {
  174.         return Double.POSITIVE_INFINITY;
  175.     }

  176.     /**
  177.      * {@inheritDoc}
  178.      *
  179.      * The support of this distribution is connected.
  180.      *
  181.      * @return {@code true}
  182.      */
  183.     @Override
  184.     public boolean isSupportConnected() {
  185.         return true;
  186.     }
  187. }