/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.distribution; import java.io.Serializable; import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.FastMath; /** * Default implementation of * {@link org.apache.commons.math.distribution.CauchyDistribution}. * * @since 1.1 * @version $Id: CauchyDistributionImpl.java 1131229 2011-06-03 20:49:25Z luc $ */ public class CauchyDistributionImpl extends AbstractContinuousDistribution implements CauchyDistribution, Serializable { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ private static final long serialVersionUID = 8589540077390120676L; /** The median of this distribution. */ private final double median; /** The scale of this distribution. */ private final double scale; /** Inverse cumulative probability accuracy */ private final double solverAbsoluteAccuracy; /** * Creates cauchy distribution with the medain equal to zero and scale * equal to one. */ public CauchyDistributionImpl() { this(0, 1); } /** * Create a cauchy distribution using the given median and scale. * * @param median Median for this distribution. * @param scale Scale parameter for this distribution. */ public CauchyDistributionImpl(double median, double scale) { this(median, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create a cauchy distribution using the given median and scale. * * @param median Median for this distribution. * @param scale Scale parameter for this distribution. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code s <= 0}. * @since 2.1 */ public CauchyDistributionImpl(double median, double scale, double inverseCumAccuracy) { if (scale <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); } this.scale = scale; this.median = median; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * For this distribution, {@code X}, this method returns {@code P(X < x)}. * * @param x Value at which the CDF is evaluated. * @return CDF evaluated at {@code x}. */ public double cumulativeProbability(double x) { return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI); } /** * {@inheritDoc} */ public double getMedian() { return median; } /** * {@inheritDoc} */ public double getScale() { return scale; } /** * {@inheritDoc} */ @Override public double density(double x) { final double dev = x - median; return (1 / FastMath.PI) * (scale / (dev * dev + scale * scale)); } /** * For this distribution, {@code X}, this method returns the critical * point {@code x}, such that {@code P(X < x) = p}. * It will return {@code Double.NEGATIVE_INFINITY} when p = 0 and * {@code Double.POSITIVE_INFINITY} when p = 1. * * @param p Desired probability. * @return {@code x}, such that {@code P(X < x) = p}. * @throws OutOfRangeException if {@code p} is not a valid probability. */ @Override public double inverseCumulativeProbability(double p) { double ret; if (p < 0 || p > 1) { throw new OutOfRangeException(p, 0, 1); } else if (p == 0) { ret = Double.NEGATIVE_INFINITY; } else if (p == 1) { ret = Double.POSITIVE_INFINITY; } else { ret = median + scale * FastMath.tan(FastMath.PI * (p - .5)); } return ret; } /** * Access the domain value lower bound, based on {@code p}, used to * bracket a CDF root. This method is used by * {@link #inverseCumulativeProbability(double)} to find critical values. * * @param p Desired probability for the critical value. * @return domain value lower bound, i.e. {@code P(X < 'lower bound') < p}. */ @Override protected double getDomainLowerBound(double p) { double ret; if (p < 0.5) { ret = -Double.MAX_VALUE; } else { ret = median; } return ret; } /** * Access the domain value upper bound, based on <code>p</code>, used to * bracket a CDF root. This method is used by * {@link #inverseCumulativeProbability(double)} to find critical values. * * @param p Desired probability for the critical value. * @return domain value lower bound, i.e. {@code P(X < 'upper bound') > p}. */ @Override protected double getDomainUpperBound(double p) { double ret; if (p < 0.5) { ret = median; } else { ret = Double.MAX_VALUE; } return ret; } /** * Access the initial domain value, based on {@code p}, used to * bracket a CDF root. This method is used by * {@link #inverseCumulativeProbability(double)} to find critical values. * * @param p Desired probability for the critical value. * @return the initial domain value. */ @Override protected double getInitialDomain(double p) { double ret; if (p < 0.5) { ret = median - scale; } else if (p > 0.5) { ret = median + scale; } else { ret = median; } return ret; } /** * Return the absolute accuracy setting of the solver used to estimate * inverse cumulative probabilities. * * @return the solver absolute accuracy * @since 2.1 */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * The lower bound of the support is always negative infinity no matter * the parameters. * * @return lower bound of the support (always Double.NEGATIVE_INFINITY) */ @Override public double getSupportLowerBound() { return Double.NEGATIVE_INFINITY; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity no matter * the parameters. * * @return upper bound of the support (always Double.POSITIVE_INFINITY) */ @Override public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** * {@inheritDoc} * * The mean is always undefined no matter the parameters. * * @return mean (always Double.NaN) */ @Override protected double calculateNumericalMean() { return Double.NaN; } /** * {@inheritDoc} * * The variance is always undefined no matter the parameters. * * @return variance (always Double.NaN) */ @Override protected double calculateNumericalVariance() { return Double.NaN; } /** * {@inheritDoc} */ @Override public boolean isSupportLowerBoundInclusive() { return false; } /** * {@inheritDoc} */ @Override public boolean isSupportUpperBoundInclusive() { return false; } }