FDistribution.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.util.FastMath;

  26. /**
  27.  * Implementation of the F-distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/F-distribution">F-distribution (Wikipedia)</a>
  30.  * @see <a href="http://mathworld.wolfram.com/F-Distribution.html">F-distribution (MathWorld)</a>
  31.  */
  32. public class FDistribution extends AbstractRealDistribution {
  33.     /** Serializable version identifier. */
  34.     private static final long serialVersionUID = 20160320L;
  35.     /** The numerator degrees of freedom. */
  36.     private final double numeratorDegreesOfFreedom;
  37.     /** The numerator degrees of freedom. */
  38.     private final double denominatorDegreesOfFreedom;
  39.     /** Cached numerical variance */
  40.     private final double numericalVariance;

  41.     /**
  42.      * Creates an F distribution using the given degrees of freedom.
  43.      *
  44.      * @param numeratorDegreesOfFreedom Numerator degrees of freedom.
  45.      * @param denominatorDegreesOfFreedom Denominator degrees of freedom.
  46.      * @throws MathIllegalArgumentException if
  47.      * {@code numeratorDegreesOfFreedom <= 0} or
  48.      * {@code denominatorDegreesOfFreedom <= 0}.
  49.      */
  50.     public FDistribution(double numeratorDegreesOfFreedom,
  51.                          double denominatorDegreesOfFreedom)
  52.         throws MathIllegalArgumentException {
  53.         this(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom,
  54.              DEFAULT_SOLVER_ABSOLUTE_ACCURACY);
  55.     }


  56.     /**
  57.      * Creates an F distribution.
  58.      *
  59.      * @param numeratorDegreesOfFreedom Numerator degrees of freedom.
  60.      * @param denominatorDegreesOfFreedom Denominator degrees of freedom.
  61.      * @param inverseCumAccuracy the maximum absolute error in inverse
  62.      * cumulative probability estimates.
  63.      * @throws MathIllegalArgumentException if {@code numeratorDegreesOfFreedom <= 0} or
  64.      * {@code denominatorDegreesOfFreedom <= 0}.
  65.      */
  66.     public FDistribution(double numeratorDegreesOfFreedom,
  67.                          double denominatorDegreesOfFreedom,
  68.                          double inverseCumAccuracy)
  69.         throws MathIllegalArgumentException {
  70.         super(inverseCumAccuracy);

  71.         if (numeratorDegreesOfFreedom <= 0) {
  72.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DEGREES_OF_FREEDOM,
  73.                                                    numeratorDegreesOfFreedom);
  74.         }
  75.         if (denominatorDegreesOfFreedom <= 0) {
  76.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DEGREES_OF_FREEDOM,
  77.                                                    denominatorDegreesOfFreedom);
  78.         }
  79.         this.numeratorDegreesOfFreedom   = numeratorDegreesOfFreedom;
  80.         this.denominatorDegreesOfFreedom = denominatorDegreesOfFreedom;
  81.         this.numericalVariance           = calculateNumericalVariance();
  82.     }

  83.     /**
  84.      * {@inheritDoc}
  85.      */
  86.     @Override
  87.     public double density(double x) {
  88.         return FastMath.exp(logDensity(x));
  89.     }

  90.     /** {@inheritDoc} **/
  91.     @Override
  92.     public double logDensity(double x) {
  93.         final double nhalf = numeratorDegreesOfFreedom / 2;
  94.         final double mhalf = denominatorDegreesOfFreedom / 2;
  95.         final double logx = FastMath.log(x);
  96.         final double logn = FastMath.log(numeratorDegreesOfFreedom);
  97.         final double logm = FastMath.log(denominatorDegreesOfFreedom);
  98.         final double lognxm = FastMath.log(numeratorDegreesOfFreedom * x +
  99.                                            denominatorDegreesOfFreedom);
  100.         return nhalf * logn + nhalf * logx - logx +
  101.                mhalf * logm - nhalf * lognxm - mhalf * lognxm -
  102.                Beta.logBeta(nhalf, mhalf);
  103.     }

  104.     /**
  105.      * {@inheritDoc}
  106.      *
  107.      * The implementation of this method is based on
  108.      * <ul>
  109.      *  <li>
  110.      *   <a href="http://mathworld.wolfram.com/F-Distribution.html">
  111.      *   F-Distribution</a>, equation (4).
  112.      *  </li>
  113.      * </ul>
  114.      */
  115.     @Override
  116.     public double cumulativeProbability(double x)  {
  117.         double ret;
  118.         if (x <= 0) {
  119.             ret = 0;
  120.         } else {
  121.             double n = numeratorDegreesOfFreedom;
  122.             double m = denominatorDegreesOfFreedom;

  123.             ret = Beta.regularizedBeta((n * x) / (m + n * x),
  124.                 0.5 * n,
  125.                 0.5 * m);
  126.         }
  127.         return ret;
  128.     }

  129.     /**
  130.      * Access the numerator degrees of freedom.
  131.      *
  132.      * @return the numerator degrees of freedom.
  133.      */
  134.     public double getNumeratorDegreesOfFreedom() {
  135.         return numeratorDegreesOfFreedom;
  136.     }

  137.     /**
  138.      * Access the denominator degrees of freedom.
  139.      *
  140.      * @return the denominator degrees of freedom.
  141.      */
  142.     public double getDenominatorDegreesOfFreedom() {
  143.         return denominatorDegreesOfFreedom;
  144.     }

  145.     /**
  146.      * {@inheritDoc}
  147.      *
  148.      * For denominator degrees of freedom parameter {@code b}, the mean is
  149.      * <ul>
  150.      *  <li>if {@code b > 2} then {@code b / (b - 2)},</li>
  151.      *  <li>else undefined ({@code Double.NaN}).
  152.      * </ul>
  153.      */
  154.     @Override
  155.     public double getNumericalMean() {
  156.         final double denominatorDF = getDenominatorDegreesOfFreedom();

  157.         if (denominatorDF > 2) {
  158.             return denominatorDF / (denominatorDF - 2);
  159.         }

  160.         return Double.NaN;
  161.     }

  162.     /**
  163.      * {@inheritDoc}
  164.      *
  165.      * For numerator degrees of freedom parameter {@code a} and denominator
  166.      * degrees of freedom parameter {@code b}, the variance is
  167.      * <ul>
  168.      *  <li>
  169.      *    if {@code b > 4} then
  170.      *    {@code [2 * b^2 * (a + b - 2)] / [a * (b - 2)^2 * (b - 4)]},
  171.      *  </li>
  172.      *  <li>else undefined ({@code Double.NaN}).
  173.      * </ul>
  174.      */
  175.     @Override
  176.     public double getNumericalVariance() {
  177.         return numericalVariance;
  178.     }

  179.     /**
  180.      * Calculates the numerical variance.
  181.      *
  182.      * @return the variance of this distribution
  183.      */
  184.     private double calculateNumericalVariance() {
  185.         final double denominatorDF = getDenominatorDegreesOfFreedom();

  186.         if (denominatorDF > 4) {
  187.             final double numeratorDF = getNumeratorDegreesOfFreedom();
  188.             final double denomDFMinusTwo = denominatorDF - 2;

  189.             return ( 2 * (denominatorDF * denominatorDF) * (numeratorDF + denominatorDF - 2) ) /
  190.                    ( (numeratorDF * (denomDFMinusTwo * denomDFMinusTwo) * (denominatorDF - 4)) );
  191.         }

  192.         return Double.NaN;
  193.     }

  194.     /**
  195.      * {@inheritDoc}
  196.      *
  197.      * The lower bound of the support is always 0 no matter the parameters.
  198.      *
  199.      * @return lower bound of the support (always 0)
  200.      */
  201.     @Override
  202.     public double getSupportLowerBound() {
  203.         return 0;
  204.     }

  205.     /**
  206.      * {@inheritDoc}
  207.      *
  208.      * The upper bound of the support is always positive infinity
  209.      * no matter the parameters.
  210.      *
  211.      * @return upper bound of the support (always Double.POSITIVE_INFINITY)
  212.      */
  213.     @Override
  214.     public double getSupportUpperBound() {
  215.         return Double.POSITIVE_INFINITY;
  216.     }

  217.     /**
  218.      * {@inheritDoc}
  219.      *
  220.      * The support of this distribution is connected.
  221.      *
  222.      * @return {@code true}
  223.      */
  224.     @Override
  225.     public boolean isSupportConnected() {
  226.         return true;
  227.     }
  228. }