CauchyDistribution.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.util.FastMath;
  25. import org.hipparchus.util.MathUtils;

  26. /**
  27.  * Implementation of the Cauchy distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/Cauchy_distribution">Cauchy distribution (Wikipedia)</a>
  30.  * @see <a href="http://mathworld.wolfram.com/CauchyDistribution.html">Cauchy Distribution (MathWorld)</a>
  31.  */
  32. public class CauchyDistribution extends AbstractRealDistribution {
  33.     /** Serializable version identifier */
  34.     private static final long serialVersionUID = 20160320L;
  35.     /** The median of this distribution. */
  36.     private final double median;
  37.     /** The scale of this distribution. */
  38.     private final double scale;

  39.     /**
  40.      * Creates a Cauchy distribution with the median equal to zero and scale
  41.      * equal to one.
  42.      */
  43.     public CauchyDistribution() {
  44.         this(0, 1);
  45.     }


  46.     /**
  47.      * Creates a Cauchy distribution.
  48.      *
  49.      * @param median Median for this distribution
  50.      * @param scale Scale parameter for this distribution
  51.      * @throws MathIllegalArgumentException if {@code scale <= 0}
  52.      */
  53.     public CauchyDistribution(double median, double scale)
  54.         throws MathIllegalArgumentException {
  55.         if (scale <= 0) {
  56.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SCALE, scale);
  57.         }

  58.         this.scale = scale;
  59.         this.median = median;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public double cumulativeProbability(double x) {
  64.         return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
  65.     }

  66.     /**
  67.      * Access the median.
  68.      *
  69.      * @return the median for this distribution.
  70.      */
  71.     public double getMedian() {
  72.         return median;
  73.     }

  74.     /**
  75.      * Access the scale parameter.
  76.      *
  77.      * @return the scale parameter for this distribution.
  78.      */
  79.     public double getScale() {
  80.         return scale;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public double density(double x) {
  85.         final double dev = x - median;
  86.         return (1 / FastMath.PI) * (scale / (dev * dev + scale * scale));
  87.     }

  88.     /**
  89.      * {@inheritDoc}
  90.      *
  91.      * Returns {@code Double.NEGATIVE_INFINITY} when {@code p == 0}
  92.      * and {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
  93.      */
  94.     @Override
  95.     public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
  96.         MathUtils.checkRangeInclusive(p, 0, 1);

  97.         double ret;
  98.         if (p == 0) {
  99.             ret = Double.NEGATIVE_INFINITY;
  100.         } else  if (p == 1) {
  101.             ret = Double.POSITIVE_INFINITY;
  102.         } else {
  103.             ret = median + scale * FastMath.tan(FastMath.PI * (p - .5));
  104.         }
  105.         return ret;
  106.     }

  107.     /**
  108.      * {@inheritDoc}
  109.      *
  110.      * The mean is always undefined no matter the parameters.
  111.      *
  112.      * @return mean (always Double.NaN)
  113.      */
  114.     @Override
  115.     public double getNumericalMean() {
  116.         return Double.NaN;
  117.     }

  118.     /**
  119.      * {@inheritDoc}
  120.      *
  121.      * The variance is always undefined no matter the parameters.
  122.      *
  123.      * @return variance (always Double.NaN)
  124.      */
  125.     @Override
  126.     public double getNumericalVariance() {
  127.         return Double.NaN;
  128.     }

  129.     /**
  130.      * {@inheritDoc}
  131.      *
  132.      * The lower bound of the support is always negative infinity no matter
  133.      * the parameters.
  134.      *
  135.      * @return lower bound of the support (always Double.NEGATIVE_INFINITY)
  136.      */
  137.     @Override
  138.     public double getSupportLowerBound() {
  139.         return Double.NEGATIVE_INFINITY;
  140.     }

  141.     /**
  142.      * {@inheritDoc}
  143.      *
  144.      * The upper bound of the support is always positive infinity no matter
  145.      * the parameters.
  146.      *
  147.      * @return upper bound of the support (always Double.POSITIVE_INFINITY)
  148.      */
  149.     @Override
  150.     public double getSupportUpperBound() {
  151.         return Double.POSITIVE_INFINITY;
  152.     }

  153.     /**
  154.      * {@inheritDoc}
  155.      *
  156.      * The support of this distribution is connected.
  157.      *
  158.      * @return {@code true}
  159.      */
  160.     @Override
  161.     public boolean isSupportConnected() {
  162.         return true;
  163.     }
  164. }